({
body,
userId,
stripeCustomerId,
agentId,
fetch,
logger,
insertMessageBigquery,
}: {
body: ChatCompletionRequestBody
userId: string
stripeCustomerId?: string | null
agentId: string
fetch: typeof globalThis.fetch
logger: Logger
insertMessageBigquery: InsertMessageBigqueryFn
})
| 267 | } |
| 268 | |
| 269 | export async function handleMoonshotNonStream({ |
| 270 | body, |
| 271 | userId, |
| 272 | stripeCustomerId, |
| 273 | agentId, |
| 274 | fetch, |
| 275 | logger, |
| 276 | insertMessageBigquery, |
| 277 | }: { |
| 278 | body: ChatCompletionRequestBody |
| 279 | userId: string |
| 280 | stripeCustomerId?: string | null |
| 281 | agentId: string |
| 282 | fetch: typeof globalThis.fetch |
| 283 | logger: Logger |
| 284 | insertMessageBigquery: InsertMessageBigqueryFn |
| 285 | }) { |
| 286 | const originalModel = body.model |
| 287 | const startTime = new Date() |
| 288 | const { clientId, clientRequestId, costMode } = extractRequestMetadata({ |
| 289 | body, |
| 290 | logger, |
| 291 | }) |
| 292 | const auditRequest = createRequestAuditRecord(body) |
| 293 | |
| 294 | const response = await createMoonshotRequest({ body, originalModel, fetch }) |
| 295 | if (!response.ok) { |
| 296 | throw await parseMoonshotError(response) |
| 297 | } |
| 298 | |
| 299 | const data = await response.json() |
| 300 | const content = data.choices?.[0]?.message?.content ?? '' |
| 301 | const reasoningText = |
| 302 | data.choices?.[0]?.message?.reasoning_content ?? |
| 303 | data.choices?.[0]?.message?.reasoning ?? |
| 304 | '' |
| 305 | const usageData = extractUsageAndCost(data.usage, originalModel) |
| 306 | |
| 307 | insertMessageToBigQuery({ |
| 308 | messageId: data.id, |
| 309 | userId, |
| 310 | startTime, |
| 311 | request: auditRequest, |
| 312 | reasoningText, |
| 313 | responseText: content, |
| 314 | usageData, |
| 315 | logger, |
| 316 | insertMessageBigquery, |
| 317 | }).catch((error) => { |
| 318 | logger.error({ error }, 'Failed to insert message into BigQuery') |
| 319 | }) |
| 320 | |
| 321 | const billedCredits = await consumeCreditsForMessage({ |
| 322 | messageId: data.id, |
| 323 | userId, |
| 324 | stripeCustomerId, |
| 325 | agentId, |
| 326 | clientId, |
no test coverage detected