Skip to content

Commit 47af00b

Browse files
committed
zen: better error
1 parent 004a928 commit 47af00b

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,23 @@ export async function handler(
222222
logger.debug("STATUS: " + res.status + " " + res.statusText)
223223

224224
// Handle non-streaming response
225-
if (!isStream) {
225+
if (!isStream || res.status === 429) {
226226
const json = await res.json()
227-
const usageInfo = providerInfo.normalizeUsage(json.usage)
228-
const costInfo = calculateCost(modelInfo, usageInfo)
229-
await trialLimiter?.track(usageInfo)
230227
await rateLimiter?.track()
231-
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
232-
await reload(billingSource, authInfo, costInfo)
228+
if (json.usage) {
229+
const usageInfo = providerInfo.normalizeUsage(json.usage)
230+
const costInfo = calculateCost(modelInfo, usageInfo)
231+
await trialLimiter?.track(usageInfo)
232+
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
233+
await reload(billingSource, authInfo, costInfo)
234+
json.cost = calculateOccurredCost(billingSource, costInfo)
235+
}
236+
if (json.error?.message) {
237+
json.error.message = `Error from provider${providerInfo.displayName ? ` (${providerInfo.displayName})` : ""}: ${json.error.message}`
238+
}
233239

234240
const responseConverter = createResponseConverter(providerInfo.format, opts.format)
235-
const body = JSON.stringify(
236-
responseConverter({
237-
...json,
238-
cost: calculateOccurredCost(billingSource, costInfo),
239-
}),
240-
)
241+
const body = JSON.stringify(responseConverter(json))
241242
logger.metric({ response_length: body.length })
242243
logger.debug("RESPONSE: " + body)
243244
dataDumper?.provideResponse(body)

packages/console/core/src/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export namespace ZenData {
4444
})
4545

4646
const ProviderSchema = z.object({
47+
displayName: z.string().optional(),
4748
api: z.string(),
4849
apiKey: z.union([z.string(), z.record(z.string(), z.string())]),
4950
format: FormatSchema.optional(),

0 commit comments

Comments
 (0)