(stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>)
| 433 | } |
| 434 | |
| 435 | private async *handleStreamResponse(stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>): ApiStream { |
| 436 | const activeToolCallIds = new Set<string>() |
| 437 | |
| 438 | for await (const chunk of stream) { |
| 439 | const delta = chunk.choices?.[0]?.delta |
| 440 | const finishReason = chunk.choices?.[0]?.finish_reason |
| 441 | |
| 442 | if (delta) { |
| 443 | if (delta.content) { |
| 444 | yield { |
| 445 | type: "text", |
| 446 | text: delta.content, |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | yield* this.processToolCalls(delta, finishReason, activeToolCallIds) |
| 451 | } |
| 452 | |
| 453 | if (chunk.usage) { |
| 454 | yield { |
| 455 | type: "usage", |
| 456 | inputTokens: chunk.usage.prompt_tokens || 0, |
| 457 | outputTokens: chunk.usage.completion_tokens || 0, |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Helper generator to process tool calls from a stream chunk. |
no test coverage detected