({
userId,
stripeCustomerId,
agentId,
clientId,
clientRequestId,
costMode,
startTime,
request,
originalModel,
line,
state,
logger,
insertMessage,
}: {
userId: string
stripeCustomerId?: string | null
agentId: string
clientId: string | null
clientRequestId: string | null
costMode: string | undefined
startTime: Date
request: unknown
originalModel: string
line: string
state: StreamState
logger: Logger
insertMessage: InsertMessageBigqueryFn
})
| 498 | } |
| 499 | |
| 500 | async function handleLine({ |
| 501 | userId, |
| 502 | stripeCustomerId, |
| 503 | agentId, |
| 504 | clientId, |
| 505 | clientRequestId, |
| 506 | costMode, |
| 507 | startTime, |
| 508 | request, |
| 509 | originalModel, |
| 510 | line, |
| 511 | state, |
| 512 | logger, |
| 513 | insertMessage, |
| 514 | }: { |
| 515 | userId: string |
| 516 | stripeCustomerId?: string | null |
| 517 | agentId: string |
| 518 | clientId: string | null |
| 519 | clientRequestId: string | null |
| 520 | costMode: string | undefined |
| 521 | startTime: Date |
| 522 | request: unknown |
| 523 | originalModel: string |
| 524 | line: string |
| 525 | state: StreamState |
| 526 | logger: Logger |
| 527 | insertMessage: InsertMessageBigqueryFn |
| 528 | }): Promise<LineResult> { |
| 529 | if (!line.startsWith('data: ')) { |
| 530 | return { state, patchedLine: line } |
| 531 | } |
| 532 | |
| 533 | const raw = line.slice('data: '.length) |
| 534 | if (raw === '[DONE]\n' || raw === '[DONE]') { |
| 535 | return { state, patchedLine: line } |
| 536 | } |
| 537 | |
| 538 | let obj: Record<string, unknown> |
| 539 | try { |
| 540 | obj = JSON.parse(raw) |
| 541 | } catch (error) { |
| 542 | logger.warn( |
| 543 | { error: getErrorObject(error, { includeRawError: true }) }, |
| 544 | 'Received non-JSON Moonshot response', |
| 545 | ) |
| 546 | return { state, patchedLine: line } |
| 547 | } |
| 548 | |
| 549 | if (obj.model) obj.model = originalModel |
| 550 | if (!obj.provider) obj.provider = 'Moonshot' |
| 551 | |
| 552 | const result = await handleResponse({ |
| 553 | userId, |
| 554 | stripeCustomerId, |
| 555 | agentId, |
| 556 | clientId, |
| 557 | clientRequestId, |
no test coverage detected