| 667 | } |
| 668 | |
| 669 | class PromptJsonWriter implements PromptTurnWriter { |
| 670 | private assistantText = ''; |
| 671 | private readonly toolCalls: PromptJsonToolCall[] = []; |
| 672 | |
| 673 | constructor(private readonly stdout: PromptOutput) {} |
| 674 | |
| 675 | writeAssistantDelta(delta: string): void { |
| 676 | this.assistantText += delta; |
| 677 | } |
| 678 | |
| 679 | writeHookResult(event: HookResultEvent): void { |
| 680 | this.flushAssistant(); |
| 681 | this.writeJsonLine({ |
| 682 | role: 'assistant', |
| 683 | content: formatHookResultPlain(event), |
| 684 | }); |
| 685 | } |
| 686 | |
| 687 | writeThinkingDelta(): void {} |
| 688 | |
| 689 | writeToolCall(toolCallId: string, name: string, args: unknown): void { |
| 690 | const existing = this.toolCalls.find((toolCall) => toolCall.id === toolCallId); |
| 691 | if (existing !== undefined) { |
| 692 | existing.function.name = name; |
| 693 | existing.function.arguments = stringifyJsonValue(args); |
| 694 | return; |
| 695 | } |
| 696 | this.toolCalls.push({ |
| 697 | type: 'function', |
| 698 | id: toolCallId, |
| 699 | function: { |
| 700 | name, |
| 701 | arguments: stringifyJsonValue(args), |
| 702 | }, |
| 703 | }); |
| 704 | } |
| 705 | |
| 706 | writeToolCallDelta( |
| 707 | toolCallId: string, |
| 708 | name: string | undefined, |
| 709 | argumentsPart: string | undefined, |
| 710 | ): void { |
| 711 | const toolCall = this.findOrCreateToolCall(toolCallId, name ?? ''); |
| 712 | if (name !== undefined) { |
| 713 | toolCall.function.name = name; |
| 714 | } |
| 715 | if (argumentsPart !== undefined) { |
| 716 | toolCall.function.arguments += argumentsPart; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | writeToolResult(toolCallId: string, output: unknown): void { |
| 721 | this.flushAssistant(); |
| 722 | this.writeJsonLine({ |
| 723 | role: 'tool', |
| 724 | tool_call_id: toolCallId, |
| 725 | content: stringifyToolOutput(output), |
| 726 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected