({
body,
userId,
stripeCustomerId,
agentId,
fetch,
logger,
insertMessageBigquery,
}: {
body: ChatCompletionRequestBody
userId: string
stripeCustomerId?: string | null
agentId: string
fetch: typeof globalThis.fetch
logger: Logger
insertMessageBigquery: InsertMessageBigqueryFn
})
| 324 | } |
| 325 | |
| 326 | export async function handleOpenCodeZenStream({ |
| 327 | body, |
| 328 | userId, |
| 329 | stripeCustomerId, |
| 330 | agentId, |
| 331 | fetch, |
| 332 | logger, |
| 333 | insertMessageBigquery, |
| 334 | }: { |
| 335 | body: ChatCompletionRequestBody |
| 336 | userId: string |
| 337 | stripeCustomerId?: string | null |
| 338 | agentId: string |
| 339 | fetch: typeof globalThis.fetch |
| 340 | logger: Logger |
| 341 | insertMessageBigquery: InsertMessageBigqueryFn |
| 342 | }) { |
| 343 | const originalModel = body.model |
| 344 | const startTime = new Date() |
| 345 | const { clientId, clientRequestId, costMode } = extractRequestMetadata({ |
| 346 | body, |
| 347 | logger, |
| 348 | }) |
| 349 | const auditRequest = createRequestAuditRecord(body) |
| 350 | |
| 351 | const response = await createOpenCodeZenRequest({ |
| 352 | body, |
| 353 | originalModel, |
| 354 | fetch, |
| 355 | }) |
| 356 | if (!response.ok) { |
| 357 | throw await parseOpenCodeZenError(response) |
| 358 | } |
| 359 | |
| 360 | const reader = response.body?.getReader() |
| 361 | if (!reader) { |
| 362 | throw new Error('Failed to get response reader') |
| 363 | } |
| 364 | |
| 365 | let heartbeatInterval: NodeJS.Timeout |
| 366 | let state: StreamState = { |
| 367 | responseText: '', |
| 368 | reasoningText: '', |
| 369 | ttftMs: null, |
| 370 | billedAlready: false, |
| 371 | } |
| 372 | let clientDisconnected = false |
| 373 | |
| 374 | const stream = new ReadableStream({ |
| 375 | async start(controller) { |
| 376 | const decoder = new TextDecoder() |
| 377 | let buffer = '' |
| 378 | |
| 379 | controller.enqueue( |
| 380 | new TextEncoder().encode(`: connected ${new Date().toISOString()}\n`), |
| 381 | ) |
| 382 | |
| 383 | heartbeatInterval = setInterval(() => { |
no test coverage detected