* Helper generator to process tool calls from a stream chunk. * Tracks active tool call IDs and yields tool_call_partial and tool_call_end events. * @param delta - The delta object from the stream chunk * @param finishReason - The finish_reason from the stream chunk * @param activeToolCallId
( delta: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta | undefined, finishReason: string | null | undefined, activeToolCallIds: Set<string>, )
| 468 | * @param activeToolCallIds - Set to track active tool call IDs (mutated in place) |
| 469 | */ |
| 470 | protected *processToolCalls( |
| 471 | delta: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta | undefined, |
| 472 | finishReason: string | null | undefined, |
| 473 | activeToolCallIds: Set<string>, |
| 474 | ): Generator< |
| 475 | | { type: "tool_call_partial"; index: number; id?: string; name?: string; arguments?: string } |
| 476 | | { type: "tool_call_end"; id: string } |
| 477 | > { |
| 478 | if (delta?.tool_calls) { |
| 479 | for (const toolCall of delta.tool_calls) { |
| 480 | if (toolCall.id) { |
| 481 | activeToolCallIds.add(toolCall.id) |
| 482 | } |
| 483 | yield { |
| 484 | type: "tool_call_partial", |
| 485 | index: toolCall.index, |
| 486 | id: toolCall.id, |
| 487 | name: toolCall.function?.name, |
| 488 | arguments: toolCall.function?.arguments, |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Emit tool_call_end events when finish_reason is "tool_calls" |
| 494 | // This ensures tool calls are finalized even if the stream doesn't properly close |
| 495 | if (finishReason === "tool_calls" && activeToolCallIds.size > 0) { |
| 496 | for (const id of activeToolCallIds) { |
| 497 | yield { type: "tool_call_end", id } |
| 498 | } |
| 499 | activeToolCallIds.clear() |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | protected _getUrlHost(baseUrl?: string): string { |
| 504 | try { |
no test coverage detected