|
1 | 1 | import { askFollowupQuestionTool } from "../AskFollowupQuestionTool" |
2 | 2 | import { ToolUse } from "../../../shared/tools" |
| 3 | +import { NativeToolCallParser } from "../../assistant-message/NativeToolCallParser" |
3 | 4 |
|
4 | 5 | describe("askFollowupQuestionTool", () => { |
5 | 6 | let mockCline: any |
@@ -101,4 +102,104 @@ describe("askFollowupQuestionTool", () => { |
101 | 102 | false, |
102 | 103 | ) |
103 | 104 | }) |
| 105 | + |
| 106 | + describe("handlePartial with native protocol", () => { |
| 107 | + it("should only send question during partial streaming to avoid raw JSON display", async () => { |
| 108 | + const block: ToolUse<"ask_followup_question"> = { |
| 109 | + type: "tool_use", |
| 110 | + name: "ask_followup_question", |
| 111 | + params: { |
| 112 | + question: "What would you like to do?", |
| 113 | + }, |
| 114 | + partial: true, |
| 115 | + nativeArgs: { |
| 116 | + question: "What would you like to do?", |
| 117 | + follow_up: [{ text: "Option 1", mode: "code" }, { text: "Option 2" }], |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + await askFollowupQuestionTool.handle(mockCline, block, { |
| 122 | + askApproval: vi.fn(), |
| 123 | + handleError: vi.fn(), |
| 124 | + pushToolResult: mockPushToolResult, |
| 125 | + removeClosingTag: vi.fn((tag, content) => content || ""), |
| 126 | + toolProtocol: "native", |
| 127 | + }) |
| 128 | + |
| 129 | + // During partial streaming, only the question should be sent (not JSON with suggestions) |
| 130 | + expect(mockCline.ask).toHaveBeenCalledWith("followup", "What would you like to do?", true) |
| 131 | + }) |
| 132 | + |
| 133 | + it("should handle partial with question from params", async () => { |
| 134 | + const block: ToolUse<"ask_followup_question"> = { |
| 135 | + type: "tool_use", |
| 136 | + name: "ask_followup_question", |
| 137 | + params: { |
| 138 | + question: "Choose wisely", |
| 139 | + }, |
| 140 | + partial: true, |
| 141 | + } |
| 142 | + |
| 143 | + await askFollowupQuestionTool.handle(mockCline, block, { |
| 144 | + askApproval: vi.fn(), |
| 145 | + handleError: vi.fn(), |
| 146 | + pushToolResult: mockPushToolResult, |
| 147 | + removeClosingTag: vi.fn((tag, content) => content || ""), |
| 148 | + toolProtocol: "xml", |
| 149 | + }) |
| 150 | + |
| 151 | + expect(mockCline.ask).toHaveBeenCalledWith("followup", "Choose wisely", true) |
| 152 | + }) |
| 153 | + }) |
| 154 | + |
| 155 | + describe("NativeToolCallParser.createPartialToolUse for ask_followup_question", () => { |
| 156 | + beforeEach(() => { |
| 157 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 158 | + NativeToolCallParser.clearRawChunkState() |
| 159 | + }) |
| 160 | + |
| 161 | + it("should build nativeArgs with question and follow_up during streaming", () => { |
| 162 | + // Start a streaming tool call |
| 163 | + NativeToolCallParser.startStreamingToolCall("call_123", "ask_followup_question") |
| 164 | + |
| 165 | + // Simulate streaming JSON chunks |
| 166 | + const chunk1 = '{"question":"What would you like?","follow_up":[{"text":"Option 1","mode":"code"}' |
| 167 | + const result1 = NativeToolCallParser.processStreamingChunk("call_123", chunk1) |
| 168 | + |
| 169 | + expect(result1).not.toBeNull() |
| 170 | + expect(result1?.name).toBe("ask_followup_question") |
| 171 | + expect(result1?.params.question).toBe("What would you like?") |
| 172 | + expect(result1?.nativeArgs).toBeDefined() |
| 173 | + // Use type assertion to access the specific fields |
| 174 | + const nativeArgs = result1?.nativeArgs as { |
| 175 | + question: string |
| 176 | + follow_up?: Array<{ text: string; mode?: string }> |
| 177 | + } |
| 178 | + expect(nativeArgs?.question).toBe("What would you like?") |
| 179 | + // partial-json should parse the incomplete array |
| 180 | + expect(nativeArgs?.follow_up).toBeDefined() |
| 181 | + }) |
| 182 | + |
| 183 | + it("should finalize with complete nativeArgs", () => { |
| 184 | + NativeToolCallParser.startStreamingToolCall("call_456", "ask_followup_question") |
| 185 | + |
| 186 | + // Add complete JSON |
| 187 | + const completeJson = |
| 188 | + '{"question":"Choose an option","follow_up":[{"text":"Yes","mode":"code"},{"text":"No","mode":null}]}' |
| 189 | + NativeToolCallParser.processStreamingChunk("call_456", completeJson) |
| 190 | + |
| 191 | + const result = NativeToolCallParser.finalizeStreamingToolCall("call_456") |
| 192 | + |
| 193 | + expect(result).not.toBeNull() |
| 194 | + expect(result?.name).toBe("ask_followup_question") |
| 195 | + expect(result?.partial).toBe(false) |
| 196 | + expect(result?.nativeArgs).toEqual({ |
| 197 | + question: "Choose an option", |
| 198 | + follow_up: [ |
| 199 | + { text: "Yes", mode: "code" }, |
| 200 | + { text: "No", mode: null }, |
| 201 | + ], |
| 202 | + }) |
| 203 | + }) |
| 204 | + }) |
104 | 205 | }) |
0 commit comments