(controller)
| 436 | |
| 437 | const stream = new ReadableStream({ |
| 438 | async start(controller) { |
| 439 | const decoder = new TextDecoder() |
| 440 | let buffer = '' |
| 441 | |
| 442 | controller.enqueue( |
| 443 | new TextEncoder().encode(`: connected ${new Date().toISOString()}\n`), |
| 444 | ) |
| 445 | |
| 446 | heartbeatInterval = setInterval(() => { |
| 447 | if (!clientDisconnected) { |
| 448 | try { |
| 449 | controller.enqueue( |
| 450 | new TextEncoder().encode( |
| 451 | `: heartbeat ${new Date().toISOString()}\n\n`, |
| 452 | ), |
| 453 | ) |
| 454 | } catch { |
| 455 | // client disconnected |
| 456 | } |
| 457 | } |
| 458 | }, 30000) |
| 459 | |
| 460 | try { |
| 461 | let done = false |
| 462 | while (!done) { |
| 463 | const result = await reader.read() |
| 464 | done = result.done |
| 465 | const value = result.value |
| 466 | |
| 467 | if (done) { |
| 468 | break |
| 469 | } |
| 470 | |
| 471 | buffer += decoder.decode(value, { stream: true }) |
| 472 | let lineEnd = buffer.indexOf('\n') |
| 473 | |
| 474 | while (lineEnd !== -1) { |
| 475 | const line = buffer.slice(0, lineEnd + 1) |
| 476 | buffer = buffer.slice(lineEnd + 1) |
| 477 | |
| 478 | let billedCredits: number | undefined |
| 479 | |
| 480 | if (line.startsWith('data: ')) { |
| 481 | const raw = line.slice('data: '.length).trim() |
| 482 | if (raw !== '[DONE]') { |
| 483 | try { |
| 484 | const obj = JSON.parse(raw) |
| 485 | const delta = obj.choices?.[0]?.delta |
| 486 | |
| 487 | // Track time to first token (TTFT) - set on first meaningful delta (content, reasoning, or tool_calls) |
| 488 | const hasContentDelta = delta?.content && responseText.length === 0 |
| 489 | const hasReasoningDelta = delta?.reasoning && reasoningText.length === 0 |
| 490 | const hasToolCallsDelta = delta?.tool_calls && delta.tool_calls.length > 0 |
| 491 | if (ttftMs === null && (hasContentDelta || hasReasoningDelta || hasToolCallsDelta)) { |
| 492 | ttftMs = Date.now() - startTime.getTime() |
| 493 | } |
| 494 | |
| 495 | if (delta?.content && responseText.length < MAX_BUFFER_SIZE) { |
nothing calls this directly
no test coverage detected