-
Notifications
You must be signed in to change notification settings - Fork 0
Jorge/usage aggregate #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e3024b2
Fix usage on journl agent
jorgebaralt da9f4ec
remove log
jorgebaralt 84a34e0
- processUsageEvent
jorgebaralt 31dc6b1
Add free plan to stripe
jorgebaralt 6e3b106
clean up and fix
jorgebaralt 144c296
turn into drizzle transactions
jorgebaralt dcdbd36
get rid of `usd`
jorgebaralt 1eb38b6
Fix agent streaming using `after`
jorgebaralt 532459e
move subscription shared logic
jorgebaralt d3c2d37
changes to usage period:
jorgebaralt 2ba2cad
Final updates and fixes.
jorgebaralt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { zUsageEventWebhook } from "@acme/db/schema"; | ||
| import { NextResponse } from "next/server"; | ||
| import { api } from "~/trpc/server"; | ||
| import { handler } from "../_lib/webhook-handler"; | ||
|
|
||
| /** | ||
| * This webhook processes usage events when they are created or updated. | ||
| * Usage events are created when AI features are used (chat, embedding, etc.) | ||
| */ | ||
| export const POST = handler(zUsageEventWebhook, async (payload) => { | ||
| // Skip DELETE events | ||
| if (payload.type === "DELETE") { | ||
| return NextResponse.json({ success: true }); | ||
| } | ||
|
|
||
| // Skip processing if the event is already processed | ||
| if (payload.record?.status === "processed") { | ||
| return NextResponse.json({ success: true }); | ||
| } | ||
|
|
||
| try { | ||
| // Process the usage event with the usage period | ||
| const result = await api.usage.processUsageEvent({ | ||
| usage_event_id: payload.record.id, | ||
| user_id: payload.record.user_id, | ||
| }); | ||
|
|
||
| return NextResponse.json({ result, success: true }); | ||
| } catch (error) { | ||
| return NextResponse.json( | ||
| { | ||
| details: error instanceof Error ? error.message : "Unknown error", | ||
| error: "Processing failed", | ||
| success: false, | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { and, desc, eq, lte } from "@acme/db"; | ||
| import { ModelPricing, zInsertModelPricing } from "@acme/db/schema"; | ||
| import { TRPCError, type TRPCRouterRecord } from "@trpc/server"; | ||
| import { z } from "zod/v4"; | ||
| import { publicProcedure } from "../trpc.js"; | ||
|
|
||
| export const modelPricingRouter = { | ||
| getAllPricingForModel: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| model_id: z.string(), | ||
| model_provider: z.string(), | ||
| }), | ||
| ) | ||
| .query(async ({ ctx, input }) => { | ||
| try { | ||
| const pricing = await ctx.db.query.ModelPricing.findMany({ | ||
| orderBy: [desc(ModelPricing.effective_date)], | ||
| where: and( | ||
| eq(ModelPricing.model_id, input.model_id), | ||
| eq(ModelPricing.model_provider, input.model_provider), | ||
| lte(ModelPricing.effective_date, new Date()), | ||
| ), | ||
| }); | ||
|
|
||
| return pricing; | ||
| } catch (error) { | ||
| console.error( | ||
| "Database error in modelPricing.getAllPricingForModel:", | ||
| error, | ||
| ); | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: "Failed to get model pricing", | ||
| }); | ||
| } | ||
| }), | ||
| getCurrentPricing: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| model_id: z.string(), | ||
| model_provider: z.string(), | ||
| unit_type: z.string(), | ||
| }), | ||
| ) | ||
| .query(async ({ ctx, input }) => { | ||
| try { | ||
| const pricing = await ctx.db.query.ModelPricing.findFirst({ | ||
| orderBy: [desc(ModelPricing.effective_date)], | ||
| where: and( | ||
| eq(ModelPricing.model_id, input.model_id), | ||
| eq(ModelPricing.model_provider, input.model_provider), | ||
| eq(ModelPricing.unit_type, input.unit_type), | ||
| lte(ModelPricing.effective_date, new Date()), | ||
| ), | ||
| }); | ||
|
|
||
| return pricing; | ||
| } catch (error) { | ||
| console.error( | ||
| "Database error in modelPricing.getCurrentPricing:", | ||
| error, | ||
| ); | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: "Failed to get current pricing", | ||
| }); | ||
| } | ||
| }), | ||
|
|
||
| upsertPricing: publicProcedure | ||
| .input(zInsertModelPricing) | ||
| .mutation(async ({ ctx, input }) => { | ||
| try { | ||
| const [pricing] = await ctx.db | ||
| .insert(ModelPricing) | ||
| .values(input) | ||
| .onConflictDoUpdate({ | ||
| set: { | ||
| price_per_unit: input.price_per_unit, | ||
| updated_at: new Date(), | ||
| }, | ||
| target: [ | ||
| ModelPricing.model_id, | ||
| ModelPricing.model_provider, | ||
| ModelPricing.unit_type, | ||
| ModelPricing.effective_date, | ||
| ], | ||
| }) | ||
| .returning(); | ||
|
|
||
| return pricing; | ||
| } catch (error) { | ||
| console.error("Database error in modelPricing.upsertPricing:", error); | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: "Failed to upsert pricing", | ||
| }); | ||
| } | ||
| }), | ||
| } satisfies TRPCRouterRecord; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.