({
userId,
stripeCustomerId,
agentId,
clientId,
clientRequestId,
costMode,
byok,
startTime,
request,
line,
state,
logger,
insertMessage,
}: {
userId: string
stripeCustomerId?: string | null
agentId: string
clientId: string | null
clientRequestId: string | null
costMode: string | undefined
byok: boolean
startTime: Date
request: unknown
line: string
state: StreamState
logger: Logger
insertMessage: InsertMessageBigqueryFn
})
| 544 | } |
| 545 | |
| 546 | async function handleLine({ |
| 547 | userId, |
| 548 | stripeCustomerId, |
| 549 | agentId, |
| 550 | clientId, |
| 551 | clientRequestId, |
| 552 | costMode, |
| 553 | byok, |
| 554 | startTime, |
| 555 | request, |
| 556 | line, |
| 557 | state, |
| 558 | logger, |
| 559 | insertMessage, |
| 560 | }: { |
| 561 | userId: string |
| 562 | stripeCustomerId?: string | null |
| 563 | agentId: string |
| 564 | clientId: string | null |
| 565 | clientRequestId: string | null |
| 566 | costMode: string | undefined |
| 567 | byok: boolean |
| 568 | startTime: Date |
| 569 | request: unknown |
| 570 | line: string |
| 571 | state: StreamState |
| 572 | logger: Logger |
| 573 | insertMessage: InsertMessageBigqueryFn |
| 574 | }): Promise<LineResult> { |
| 575 | if (!line.startsWith('data: ')) { |
| 576 | return { state } |
| 577 | } |
| 578 | |
| 579 | const raw = line.slice('data: '.length) |
| 580 | if (raw === '[DONE]\n') { |
| 581 | return { state } |
| 582 | } |
| 583 | |
| 584 | // Parse the string into an object |
| 585 | let obj |
| 586 | try { |
| 587 | obj = JSON.parse(raw) |
| 588 | } catch (error) { |
| 589 | logger.warn( |
| 590 | { error: getErrorObject(error, { includeRawError: true }) }, |
| 591 | 'Received non-JSON OpenRouter response', |
| 592 | ) |
| 593 | return { state } |
| 594 | } |
| 595 | |
| 596 | // Extract usage |
| 597 | const parsed = OpenRouterStreamChatCompletionChunkSchema.safeParse(obj) |
| 598 | if (!parsed.success) { |
| 599 | logger.warn( |
| 600 | { error: getErrorObject(parsed.error, { includeRawError: true }) }, |
| 601 | 'Unable to parse OpenRouter response', |
| 602 | ) |
| 603 | return { state } |
no test coverage detected