zen: better error
This commit is contained in:
@@ -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()
|
||||||
const usageInfo = providerInfo.normalizeUsage(json.usage)
|
|
||||||
const costInfo = calculateCost(modelInfo, usageInfo)
|
|
||||||
await trialLimiter?.track(usageInfo)
|
|
||||||
await rateLimiter?.track()
|
await rateLimiter?.track()
|
||||||
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, costInfo)
|
if (json.usage) {
|
||||||
await reload(billingSource, authInfo, costInfo)
|
const usageInfo = providerInfo.normalizeUsage(json.usage)
|
||||||
|
const costInfo = calculateCost(modelInfo, usageInfo)
|
||||||
|
await trialLimiter?.track(usageInfo)
|
||||||
|
await trackUsage(sessionId, billingSource, authInfo, modelInfo, providerInfo, usageInfo, 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)
|
||||||
|
|||||||
@@ -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(),
|
||||||
|
|||||||
Reference in New Issue
Block a user