zen: better error

This commit is contained in:
Frank
2026-04-15 09:19:28 -04:00
parent 004a9284af
commit 47af00b245
2 changed files with 14 additions and 12 deletions
@@ -222,22 +222,23 @@ export async function handler(
logger.debug("STATUS: " + res.status + " " + res.statusText) logger.debug("STATUS: " + res.status + " " + res.statusText)
// Handle non-streaming response // Handle non-streaming response
if (!isStream) { if (!isStream || res.status === 429) {
const json = await res.json() const json = await res.json()
await rateLimiter?.track()
if (json.usage) {
const usageInfo = providerInfo.normalizeUsage(json.usage) const usageInfo = providerInfo.normalizeUsage(json.usage)
const costInfo = calculateCost(modelInfo, usageInfo) const costInfo = calculateCost(modelInfo, usageInfo)
await trialLimiter?.track(usageInfo) await trialLimiter?.track(usageInfo)
await rateLimiter?.track()
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo) await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
await reload(billingSource, authInfo, costInfo) await reload(billingSource, authInfo, costInfo)
json.cost = calculateOccurredCost(billingSource, costInfo)
}
if (json.error?.message) {
json.error.message = `Error from provider${providerInfo.displayName ? ` (${providerInfo.displayName})` : ""}: ${json.error.message}`
}
const responseConverter = createResponseConverter(providerInfo.format, opts.format) const responseConverter = createResponseConverter(providerInfo.format, opts.format)
const body = JSON.stringify( const body = JSON.stringify(responseConverter(json))
responseConverter({
...json,
cost: calculateOccurredCost(billingSource, costInfo),
}),
)
logger.metric({ response_length: body.length }) logger.metric({ response_length: body.length })
logger.debug("RESPONSE: " + body) logger.debug("RESPONSE: " + body)
dataDumper?.provideResponse(body) dataDumper?.provideResponse(body)
+1
View File
@@ -44,6 +44,7 @@ export namespace ZenData {
}) })
const ProviderSchema = z.object({ const ProviderSchema = z.object({
displayName: z.string().optional(),
api: z.string(), api: z.string(),
apiKey: z.union([z.string(), z.record(z.string(), z.string())]), apiKey: z.union([z.string(), z.record(z.string(), z.string())]),
format: FormatSchema.optional(), format: FormatSchema.optional(),