Open source CapCut alternative.
TypeScript
feat(site): record conversation id on inference usage events
David Lefccced7b9b69
diff --git a/site/prisma/Credits.prisma b/site/prisma/Credits.prismaindex ea158282..ec327b75 100644--- a/site/prisma/Credits.prisma+++ b/site/prisma/Credits.prisma@@ -78,6 +78,7 @@ model InferenceUsageEvent {userId Stringuser User @relation(fields: [userId], references: [id], onDelete: Cascade)clientId String?+ conversationId String?route StringrequestKind Stringprovider Stringdiff --git a/site/src/app/api/billing/usage/route.ts b/site/src/app/api/billing/usage/route.tsindex 4eb62914..c5e45147 100644--- a/site/src/app/api/billing/usage/route.ts+++ b/site/src/app/api/billing/usage/route.ts@@ -20,6 +20,7 @@ const recentPerProduct = 125;const recentSelect = {billingStatus: true,+ conversationId: true,createdAt: true,creditCostMicros: true,errorCode: true,@@ -36,6 +37,9 @@ type RecentEvent = Prisma.InferenceUsageEventGetPayload<{function toRecentCall(event: RecentEvent, product: "app" | "vision") {return {billingStatus: event.billingStatus,+ // The app conversation this call belongs to; null for background work and+ // pre-grouping rows. The usage UI groups rows by this.+ conversationId: event.conversationId,// Credits are dollar-denominated (1 credit = $1), so this string is USD.costCredits: creditMicrosToString(event.creditCostMicros),createdAt: event.createdAt.toISOString(),diff --git a/site/src/app/api/browser/run/route.ts b/site/src/app/api/browser/run/route.tsindex 08c06d4b..07091642 100644--- a/site/src/app/api/browser/run/route.ts+++ b/site/src/app/api/browser/run/route.ts@@ -72,6 +72,7 @@ export const POST = withDonkeyAuth(async (request) => {await recordFailedInferenceUsage({userId: request.donkey.userId,clientId: client.clientId,+ conversationId: request.donkey.conversationId,route: inferenceUsageRoutes.browserRun,requestKind: "browser_automation",provider: browserUseProvider,@@ -95,6 +96,7 @@ export const POST = withDonkeyAuth(async (request) => {const recorded = await recordInferenceUsage({userId: request.donkey.userId,clientId: client.clientId,+ conversationId: request.donkey.conversationId,route: inferenceUsageRoutes.browserRun,requestKind: "browser_automation",provider: browserUseProvider,diff --git a/site/src/app/api/inference/assets/refresh/route.ts b/site/src/app/api/inference/assets/refresh/route.tsindex a26bed7c..2d2aacaa 100644--- a/site/src/app/api/inference/assets/refresh/route.ts+++ b/site/src/app/api/inference/assets/refresh/route.ts@@ -47,6 +47,7 @@ export const POST = withDonkeyAuth(async (request) => {const result = await registry.refresh(parsed.data);const recordedUsage = await recordInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,metadata: {assetKind: parsed.data.kind,},@@ -71,6 +72,7 @@ export const POST = withDonkeyAuth(async (request) => {} catch (error) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: inferenceErrorCode(error),metadata: {assetKind: parsed.data.kind,diff --git a/site/src/app/api/inference/assets/route.ts b/site/src/app/api/inference/assets/route.tsindex cf8a528d..e098aa96 100644--- a/site/src/app/api/inference/assets/route.ts+++ b/site/src/app/api/inference/assets/route.ts@@ -78,6 +78,7 @@ export const POST = withDonkeyAuth(async (request) => {if (!result) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: "asset_generation_unavailable",metadata: {assetKind: parsed.data.kind,@@ -99,6 +100,7 @@ export const POST = withDonkeyAuth(async (request) => {const recordedUsage = await recordInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,metadata: {assetKind: parsed.data.kind,},@@ -118,6 +120,7 @@ export const POST = withDonkeyAuth(async (request) => {} catch (error) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: inferenceErrorCode(error),metadata: {assetKind: parsed.data.kind,diff --git a/site/src/app/api/inference/chat/completions/route.ts b/site/src/app/api/inference/chat/completions/route.tsindex 449d0504..520003e8 100644--- a/site/src/app/api/inference/chat/completions/route.ts+++ b/site/src/app/api/inference/chat/completions/route.ts@@ -68,6 +68,7 @@ export const POST = withDonkeyAuth(async (request) => {if (!result) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: "streaming_unavailable",model: requestedModel,provider: failedUsageProvider,@@ -86,6 +87,7 @@ export const POST = withDonkeyAuth(async (request) => {const recordedUsage = await recordInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,model: result.model,provider: result.provider,requestKind: "chat_completions",@@ -112,6 +114,7 @@ export const POST = withDonkeyAuth(async (request) => {if (!result) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: "completion_unavailable",model: requestedModel,provider: failedUsageProvider,@@ -130,6 +133,7 @@ export const POST = withDonkeyAuth(async (request) => {const recordedUsage = await recordInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,model: result.model,provider: result.provider,requestKind: "chat_completions",@@ -149,6 +153,7 @@ export const POST = withDonkeyAuth(async (request) => {} catch (error) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: inferenceErrorCode(error),model: requestedModel,provider: failedUsageProvider,diff --git a/site/src/app/api/inference/responses/route.ts b/site/src/app/api/inference/responses/route.tsindex e3f8b975..dd8d6877 100644--- a/site/src/app/api/inference/responses/route.ts+++ b/site/src/app/api/inference/responses/route.ts@@ -79,6 +79,7 @@ export const POST = withDonkeyAuth(async (request) => {if (!bypassCredits) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: "responses_unavailable",model: requestedModel,provider: failedUsageProvider,@@ -102,6 +103,7 @@ export const POST = withDonkeyAuth(async (request) => {} else {const recordedUsage = await recordInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,model: result.model,provider: result.provider,requestKind: "responses",@@ -139,6 +141,7 @@ export const POST = withDonkeyAuth(async (request) => {if (!bypassCredits) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: inferenceErrorCode(error),model: requestedModel,provider: failedUsageProvider,diff --git a/site/src/app/api/inference/screenshots/parse/route.ts b/site/src/app/api/inference/screenshots/parse/route.tsindex e69751ed..b310779a 100644--- a/site/src/app/api/inference/screenshots/parse/route.ts+++ b/site/src/app/api/inference/screenshots/parse/route.ts@@ -85,6 +85,7 @@ export const POST = withDonkeyAuth(async (request) => {if (parsed.data.stream) {return streamScreenshotParseResponse({clientId: client.clientId,+ conversationId: request.donkey.conversationId,model,parserProvider,request: parsed.data,@@ -96,6 +97,7 @@ export const POST = withDonkeyAuth(async (request) => {const result = await parseScreenshot(parsed.data, parserProvider);const recordedUsage = await recordInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,metadata: {parserProvider: parserProvider.id,},@@ -118,6 +120,7 @@ export const POST = withDonkeyAuth(async (request) => {} catch (error) {await recordFailedInferenceUsage({clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: inferenceErrorCode(error),metadata: {parserProvider: parserProvider.id,@@ -142,6 +145,7 @@ export const POST = withDonkeyAuth(async (request) => {function streamScreenshotParseResponse(input: {clientId: string;+ conversationId: string | null;model: string;parserProvider: ReturnType<typeof createScreenshotParserProvider>;request: ScreenshotParseRequest;@@ -159,6 +163,7 @@ function streamScreenshotParseResponse(input: {const recordedUsage = await recordInferenceUsage({clientId: input.clientId,+ conversationId: input.conversationId,metadata: {parserProvider: input.parserProvider.id,streaming: true,@@ -186,6 +191,7 @@ function streamScreenshotParseResponse(input: {} catch (error) {await recordFailedInferenceUsage({clientId: input.clientId,+ conversationId: input.conversationId,errorCode: inferenceErrorCode(error),metadata: {parserProvider: input.parserProvider.id,diff --git a/site/src/app/api/vision/route.ts b/site/src/app/api/vision/route.tsindex 4caa0a18..8f70e19a 100644--- a/site/src/app/api/vision/route.ts+++ b/site/src/app/api/vision/route.ts@@ -188,6 +188,7 @@ async function visionParseHandler(request: DonkeyAuthenticatedRequest) {const recordedUsage = await recordInferenceUsage({billingMode: isApiKey ? "included" : "credits",clientId: client.clientId,+ conversationId: request.donkey.conversationId,metadata: { grounded: String(instruction !== undefined) },model: billedModel,provider: inferenceProvider,@@ -222,6 +223,7 @@ async function visionParseHandler(request: DonkeyAuthenticatedRequest) {await recordFailedInferenceUsage({billingMode: isApiKey ? "included" : "credits",clientId: client.clientId,+ conversationId: request.donkey.conversationId,errorCode: inferenceErrorCode(error),metadata: { grounded: String(instruction !== undefined) },model: billedModel,diff --git a/site/src/lib/credits/inference.ts b/site/src/lib/credits/inference.tsindex 9721c03e..70299636 100644--- a/site/src/lib/credits/inference.ts+++ b/site/src/lib/credits/inference.ts@@ -63,6 +63,9 @@ type InferenceUsageStatus = "failed" | "succeeded";type InferenceUsageInput = {userId: string;clientId: string | null;+ // The app conversation this call belongs to, for grouping usage history. Null+ // for background work and non-app (Vision API) callers.+ conversationId?: string | null;route: string;requestKind: string;provider: string;@@ -357,6 +360,7 @@ export async function recordInferenceUsage(input: InferenceUsageInput) {accountId: account.id,billingStatus,clientId: input.clientId,+ conversationId: input.conversationId ?? null,creditCostMicros,errorCode: input.errorCode,metadata: prismaJson(input.metadata),@@ -452,6 +456,7 @@ export async function recordInferenceUsage(input: InferenceUsageInput) {export async function recordFailedInferenceUsage(input: {userId: string;clientId: string | null;+ conversationId?: string | null;route: string;requestKind: string;provider: string;diff --git a/site/src/lib/donkey-api-auth.ts b/site/src/lib/donkey-api-auth.tsindex 7328ea54..6a17f216 100644--- a/site/src/lib/donkey-api-auth.ts+++ b/site/src/lib/donkey-api-auth.ts@@ -8,6 +8,9 @@ export type DonkeyAuthContext = {app: "donkey";method: "session-cookie" | "dev-bypass" | "api-key";clientId: string | null;+ // The app's active conversation for this request, from x-donkey-conversation-id.+ // Null for background work (vision warming) and non-app callers (Vision API keys).+ conversationId: string | null;userId: string;apiKeyId: string | null;};@@ -52,9 +55,15 @@ export type DonkeyAuthHandler<> = (request: TReq, ...args: TArgs) => Promise<Response> | Response;const clientIdHeader = "x-donkey-client-id";+const conversationIdHeader = "x-donkey-conversation-id";const devAuthBypassHeader = "x-donkey-dev-auth-bypass";const devAuthBypassUserID = "donkey-dev-auth-bypass";+function conversationIdFromHeaders(headers: Headers): string | null {+ const value = headers.get(conversationIdHeader)?.trim();+ return value ? value : null;+}+export async function getDonkeyAuthContext(headers: Headers,): Promise<DonkeyAuthContext | null> {@@ -82,6 +91,7 @@ export async function getDonkeyAuthContext(app: "donkey",method: "session-cookie",clientId: clientId ? clientId : null,+ conversationId: conversationIdFromHeaders(headers),userId: session.user.id,apiKeyId: null,};@@ -120,6 +130,8 @@ async function apiKeyAuthContext(// The Vision API does not require x-donkey-client-id; default it to the key// id so downstream usage records and rate-limit buckets stay per-key.clientId: clientId ? clientId : verified.key.id,+ // Vision API keys have no app conversation; honor the header if sent, else null.+ conversationId: conversationIdFromHeaders(headers),userId: verified.key.referenceId,apiKeyId: verified.key.id,};@@ -146,6 +158,7 @@ function devAuthBypassContext(headers: Headers): DonkeyAuthContext | null {app: "donkey",method: "dev-bypass",clientId: clientId ? clientId : null,+ conversationId: conversationIdFromHeaders(headers),userId: devAuthBypassUserID,apiKeyId: null,};diff --git a/site/src/queries/billing.ts b/site/src/queries/billing.tsindex c585ce1f..1672e05b 100644--- a/site/src/queries/billing.ts+++ b/site/src/queries/billing.ts@@ -51,6 +51,9 @@ export type VisionUsage = {// Recent inference calls across products (app + Vision API), newest first.recent: {createdAt: string;+ // The app conversation this call belongs to; null for background/warm calls+ // and rows recorded before grouping existed. Drives the grouped rendering.+ conversationId: string | null;// "app" = Pro/credit-billed app inference; "vision" = Vision API call.product: "app" | "vision";requestKind: string;