The open source coding agent.
TypeScript
wip: zen
Frank7f870cc9d4d9
diff --git a/packages/console/app/src/routes/zen/util/handler.ts b/packages/console/app/src/routes/zen/util/handler.tsindex 22f887567dc..ac487a341e7 100644--- a/packages/console/app/src/routes/zen/util/handler.ts+++ b/packages/console/app/src/routes/zen/util/handler.ts@@ -561,53 +561,52 @@ export async function handler(if (!authInfo) returnconst cost = authInfo.isFree || authInfo.provider?.credentials ? 0 : centsToMicroCents(totalCostInCent)- await Database.transaction(async (tx) => {- await tx.insert(UsageTable).values({- workspaceID: authInfo.workspaceID,- id: Identifier.create("usage"),- model: modelInfo.id,- provider: providerInfo.id,- inputTokens,- outputTokens,- reasoningTokens,- cacheReadTokens,- cacheWrite5mTokens,- cacheWrite1hTokens,- cost,- keyID: authInfo.apiKeyId,- })- await tx- .update(BillingTable)- .set({- balance: sql`${BillingTable.balance} - ${cost}`,- monthlyUsage: sql`+ await Database.use((db) =>+ Promise.all([+ db.insert(UsageTable).values({+ workspaceID: authInfo.workspaceID,+ id: Identifier.create("usage"),+ model: modelInfo.id,+ provider: providerInfo.id,+ inputTokens,+ outputTokens,+ reasoningTokens,+ cacheReadTokens,+ cacheWrite5mTokens,+ cacheWrite1hTokens,+ cost,+ keyID: authInfo.apiKeyId,+ }),+ db+ .update(BillingTable)+ .set({+ balance: sql`${BillingTable.balance} - ${cost}`,+ monthlyUsage: sql`CASEWHEN MONTH(${BillingTable.timeMonthlyUsageUpdated}) = MONTH(now()) AND YEAR(${BillingTable.timeMonthlyUsageUpdated}) = YEAR(now()) THEN ${BillingTable.monthlyUsage} + ${cost}ELSE ${cost}END`,- timeMonthlyUsageUpdated: sql`now()`,- })- .where(eq(BillingTable.workspaceID, authInfo.workspaceID))- await tx- .update(UserTable)- .set({- monthlyUsage: sql`+ timeMonthlyUsageUpdated: sql`now()`,+ })+ .where(eq(BillingTable.workspaceID, authInfo.workspaceID)),+ db+ .update(UserTable)+ .set({+ monthlyUsage: sql`CASEWHEN MONTH(${UserTable.timeMonthlyUsageUpdated}) = MONTH(now()) AND YEAR(${UserTable.timeMonthlyUsageUpdated}) = YEAR(now()) THEN ${UserTable.monthlyUsage} + ${cost}ELSE ${cost}END`,- timeMonthlyUsageUpdated: sql`now()`,- })- .where(and(eq(UserTable.workspaceID, authInfo.workspaceID), eq(UserTable.id, authInfo.user.id)))- })-- await Database.use((tx) =>- tx- .update(KeyTable)- .set({ timeUsed: sql`now()` })- .where(and(eq(KeyTable.workspaceID, authInfo.workspaceID), eq(KeyTable.id, authInfo.apiKeyId))),+ timeMonthlyUsageUpdated: sql`now()`,+ })+ .where(and(eq(UserTable.workspaceID, authInfo.workspaceID), eq(UserTable.id, authInfo.user.id))),+ db+ .update(KeyTable)+ .set({ timeUsed: sql`now()` })+ .where(and(eq(KeyTable.workspaceID, authInfo.workspaceID), eq(KeyTable.id, authInfo.apiKeyId))),+ ]),)return { costInMicroCents: cost }