({
body,
userId,
stripeCustomerId,
agentId,
fetch,
logger,
insertMessageBigquery,
}: {
body: ChatCompletionRequestBody
userId: string
stripeCustomerId?: string | null
agentId: string
fetch: typeof globalThis.fetch
logger: Logger
insertMessageBigquery: InsertMessageBigqueryFn
})
| 342 | } |
| 343 | |
| 344 | export async function handleFireworksStream({ |
| 345 | body, |
| 346 | userId, |
| 347 | stripeCustomerId, |
| 348 | agentId, |
| 349 | fetch, |
| 350 | logger, |
| 351 | insertMessageBigquery, |
| 352 | }: { |
| 353 | body: ChatCompletionRequestBody |
| 354 | userId: string |
| 355 | stripeCustomerId?: string | null |
| 356 | agentId: string |
| 357 | fetch: typeof globalThis.fetch |
| 358 | logger: Logger |
| 359 | insertMessageBigquery: InsertMessageBigqueryFn |
| 360 | }) { |
| 361 | const originalModel = body.model |
| 362 | const startTime = new Date() |
| 363 | const { clientId, clientRequestId, costMode } = extractRequestMetadata({ |
| 364 | body, |
| 365 | logger, |
| 366 | }) |
| 367 | const auditRequest = createRequestAuditRecord(body) |
| 368 | |
| 369 | const response = await createFireworksRequestWithFallback({ |
| 370 | body, |
| 371 | originalModel, |
| 372 | fetch, |
| 373 | logger, |
| 374 | sessionId: userId, |
| 375 | }) |
| 376 | |
| 377 | if (!response.ok) { |
| 378 | throw await parseFireworksError(response) |
| 379 | } |
| 380 | |
| 381 | const reader = response.body?.getReader() |
| 382 | if (!reader) { |
| 383 | throw new Error('Failed to get response reader') |
| 384 | } |
| 385 | |
| 386 | let heartbeatInterval: NodeJS.Timeout |
| 387 | let state: StreamState = { responseText: '', reasoningText: '', ttftMs: null } |
| 388 | let clientDisconnected = false |
| 389 | |
| 390 | const stream = new ReadableStream({ |
| 391 | async start(controller) { |
| 392 | const decoder = new TextDecoder() |
| 393 | let buffer = '' |
| 394 | |
| 395 | controller.enqueue( |
| 396 | new TextEncoder().encode(`: connected ${new Date().toISOString()}\n`), |
| 397 | ) |
| 398 | |
| 399 | heartbeatInterval = setInterval(() => { |
| 400 | if (!clientDisconnected) { |
| 401 | try { |
no test coverage detected