Skip to content

Commit 6a64177

Browse files
authored
fix(zen): emit cost chunk in client-facing format, not upstream format (anomalyco#16817)
1 parent 5dc4790 commit 6a64177

6 files changed

Lines changed: 19 additions & 11 deletions

File tree

packages/console/app/src/routes/zen/util/handler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
FreeUsageLimitError,
2525
SubscriptionUsageLimitError,
2626
} from "./error"
27-
import { createBodyConverter, createStreamPartConverter, createResponseConverter, UsageInfo } from "./provider/provider"
27+
import { buildCostChunk, createBodyConverter, createStreamPartConverter, createResponseConverter, UsageInfo } from "./provider/provider"
2828
import { anthropicHelper } from "./provider/anthropic"
2929
import { googleHelper } from "./provider/google"
3030
import { openaiHelper } from "./provider/openai"
@@ -90,7 +90,7 @@ export async function handler(
9090
const projectId = input.request.headers.get("x-opencode-project") ?? ""
9191
const ocClient = input.request.headers.get("x-opencode-client") ?? ""
9292
logger.metric({
93-
is_tream: isStream,
93+
is_stream: isStream,
9494
session: sessionId,
9595
request: requestId,
9696
client: ocClient,
@@ -230,7 +230,7 @@ export async function handler(
230230
const body = JSON.stringify(
231231
responseConverter({
232232
...json,
233-
cost: calculateOccuredCost(billingSource, costInfo),
233+
cost: calculateOccurredCost(billingSource, costInfo),
234234
}),
235235
)
236236
logger.metric({ response_length: body.length })
@@ -274,8 +274,8 @@ export async function handler(
274274
await trialLimiter?.track(usageInfo)
275275
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
276276
await reload(billingSource, authInfo, costInfo)
277-
const cost = calculateOccuredCost(billingSource, costInfo)
278-
c.enqueue(encoder.encode(usageParser.buidlCostChunk(cost)))
277+
const cost = calculateOccurredCost(billingSource, costInfo)
278+
c.enqueue(encoder.encode(buildCostChunk(opts.format, cost)))
279279
}
280280
c.close()
281281
return
@@ -818,7 +818,7 @@ export async function handler(
818818
}
819819
}
820820

821-
function calculateOccuredCost(billingSource: BillingSource, costInfo: CostInfo) {
821+
function calculateOccurredCost(billingSource: BillingSource, costInfo: CostInfo) {
822822
return billingSource === "balance" ? (costInfo.totalCostInCent / 100).toFixed(8) : "0"
823823
}
824824

packages/console/app/src/routes/zen/util/provider/anthropic.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ export const anthropicHelper: ProviderHelper = ({ reqModel, providerModel }) =>
167167
}
168168
},
169169
retrieve: () => usage,
170-
buidlCostChunk: (cost: string) => `event: ping\ndata: ${JSON.stringify({ type: "ping", cost })}\n\n`,
171170
}
172171
},
173172
normalizeUsage: (usage: Usage) => ({

packages/console/app/src/routes/zen/util/provider/google.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const googleHelper: ProviderHelper = ({ providerModel }) => ({
5656
usage = json.usageMetadata
5757
},
5858
retrieve: () => usage,
59-
buidlCostChunk: (cost: string) => `data: ${JSON.stringify({ type: "ping", cost })}\n\n`,
6059
}
6160
},
6261
normalizeUsage: (usage: Usage) => {

packages/console/app/src/routes/zen/util/provider/openai-compatible.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export const oaCompatHelper: ProviderHelper = () => ({
5454
usage = json.usage
5555
},
5656
retrieve: () => usage,
57-
buidlCostChunk: (cost: string) => `data: ${JSON.stringify({ choices: [], cost })}\n\n`,
5857
}
5958
},
6059
normalizeUsage: (usage: Usage) => {

packages/console/app/src/routes/zen/util/provider/openai.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const openaiHelper: ProviderHelper = () => ({
4444
usage = json.response.usage
4545
},
4646
retrieve: () => usage,
47-
buidlCostChunk: (cost: string) => `event: ping\ndata: ${JSON.stringify({ type: "ping", cost })}\n\n`,
4847
}
4948
},
5049
normalizeUsage: (usage: Usage) => {

packages/console/app/src/routes/zen/util/provider/provider.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export type ProviderHelper = (input: { reqModel: string; providerModel: string }
4343
createUsageParser: () => {
4444
parse: (chunk: string) => void
4545
retrieve: () => any
46-
buidlCostChunk: (cost: string) => string
4746
}
4847
normalizeUsage: (usage: any) => UsageInfo
4948
}
@@ -162,6 +161,19 @@ export interface CommonChunk {
162161
}
163162
}
164163

164+
export function buildCostChunk(format: ZenData.Format, cost: string): string {
165+
switch (format) {
166+
case "anthropic":
167+
return `event: ping\ndata: ${JSON.stringify({ type: "ping", cost })}\n\n`
168+
case "openai":
169+
return `event: ping\ndata: ${JSON.stringify({ type: "ping", cost })}\n\n`
170+
case "oa-compat":
171+
return `data: ${JSON.stringify({ choices: [], cost })}\n\n`
172+
default:
173+
return `data: ${JSON.stringify({ type: "ping", cost })}\n\n`
174+
}
175+
}
176+
165177
export function createBodyConverter(from: ZenData.Format, to: ZenData.Format) {
166178
return (body: any): any => {
167179
if (from === to) return body

0 commit comments

Comments
 (0)