This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathpresentAssistantMessage-images.spec.ts
More file actions
326 lines (280 loc) · 10 KB
/
Copy pathpresentAssistantMessage-images.spec.ts
File metadata and controls
326 lines (280 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-images.spec.ts
import { describe, it, expect, beforeEach, vi } from "vitest"
import { Anthropic } from "@anthropic-ai/sdk"
import { presentAssistantMessage } from "../presentAssistantMessage"
import { Task } from "../../task/Task"
// Mock dependencies
vi.mock("../../task/Task")
vi.mock("../../tools/validateToolUse", () => ({
validateToolUse: vi.fn(),
isValidToolName: vi.fn((toolName: string) =>
["read_file", "write_to_file", "ask_followup_question", "attempt_completion", "use_mcp_tool"].includes(
toolName,
),
),
}))
vi.mock("@roo-code/telemetry", () => ({
TelemetryService: {
instance: {
captureToolUsage: vi.fn(),
captureConsecutiveMistakeError: vi.fn(),
},
},
}))
describe("presentAssistantMessage - Image Handling in Native Tool Calling", () => {
let mockTask: any
beforeEach(() => {
// Create a mock Task with minimal properties needed for testing
mockTask = {
taskId: "test-task-id",
instanceId: "test-instance",
abort: false,
presentAssistantMessageLocked: false,
presentAssistantMessageHasPendingUpdates: false,
currentStreamingContentIndex: 0,
assistantMessageContent: [],
userMessageContent: [],
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
consecutiveMistakeCount: 0,
api: {
getModel: () => ({ id: "test-model", info: {} }),
},
browserSession: {
closeBrowser: vi.fn().mockResolvedValue(undefined),
},
recordToolUsage: vi.fn(),
recordToolError: vi.fn(),
sayAndCreateMissingParamError: vi.fn().mockResolvedValue("mock error message"),
toolRepetitionDetector: {
check: vi.fn().mockReturnValue({ allowExecution: true }),
},
providerRef: {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
}),
}),
},
say: vi.fn().mockResolvedValue(undefined),
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
}
// Add pushToolResultToUserContent method after mockTask is created so it can reference mockTask
mockTask.pushToolResultToUserContent = vi.fn().mockImplementation((toolResult: any) => {
const existingResult = mockTask.userMessageContent.find(
(block: any) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id,
)
if (existingResult) {
return false
}
mockTask.userMessageContent.push(toolResult)
return true
})
})
it("should preserve images in tool_result for native tool calling", async () => {
// Set up a tool_use block with an ID (indicates native tool calling)
const toolCallId = "tool_call_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId, // ID indicates native tool calling
name: "ask_followup_question",
params: { questions: ["What do you see?"] },
nativeArgs: { questions: ["What do you see?"], follow_up: [] },
},
]
// Create a mock askApproval that includes images in the response
const imageBlock: Anthropic.ImageBlockParam = {
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: "base64ImageData",
},
}
mockTask.ask = vi.fn().mockResolvedValue({
response: "yesButtonClicked",
text: "I see a cat",
images: ["data:image/png;base64,base64ImageData"],
})
// Execute presentAssistantMessage
await presentAssistantMessage(mockTask)
// Verify that userMessageContent was populated
expect(mockTask.userMessageContent.length).toBeGreaterThan(0)
// Find the tool_result block
const toolResult = mockTask.userMessageContent.find(
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId,
)
expect(toolResult).toBeDefined()
expect(toolResult.tool_use_id).toBe(toolCallId)
// For native tool calling, tool_result content should be a string (text only)
expect(typeof toolResult.content).toBe("string")
expect(toolResult.content).toContain("I see a cat")
// Images should be added as separate blocks AFTER the tool_result
const imageBlocks = mockTask.userMessageContent.filter((item: any) => item.type === "image")
expect(imageBlocks.length).toBeGreaterThan(0)
expect(imageBlocks[0].source.data).toBe("base64ImageData")
})
it("should convert to string when no images are present (native tool calling)", async () => {
// Set up a tool_use block with an ID (indicates native protocol)
const toolCallId = "tool_call_456"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "ask_followup_question",
params: { questions: ["What is your name?"] },
nativeArgs: { questions: ["What is your name?"], follow_up: [] },
},
]
// Response with text but NO images
mockTask.ask = vi.fn().mockResolvedValue({
response: "yesButtonClicked",
text: "My name is Alice",
images: undefined,
})
await presentAssistantMessage(mockTask)
const toolResult = mockTask.userMessageContent.find(
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId,
)
expect(toolResult).toBeDefined()
// When no images, content should be a string
expect(typeof toolResult.content).toBe("string")
})
it("should fail fast when tool_use is missing id (legacy/XML-style tool call)", async () => {
// tool_use without an id is treated as legacy/XML-style tool call and must be rejected.
mockTask.assistantMessageContent = [
{
type: "tool_use",
name: "ask_followup_question",
params: { question: "What do you see?" },
},
]
mockTask.ask = vi.fn().mockResolvedValue({
response: "yesButtonClicked",
text: "I see a dog",
images: ["data:image/png;base64,dogImageData"],
})
await presentAssistantMessage(mockTask)
const textBlocks = mockTask.userMessageContent.filter((item: any) => item.type === "text")
expect(textBlocks.length).toBeGreaterThan(0)
expect(textBlocks.some((b: any) => String(b.text).includes("XML tool calls are no longer supported"))).toBe(
true,
)
// Should not proceed to execute tool or add images as tool output.
expect(mockTask.userMessageContent.some((item: any) => item.type === "image")).toBe(false)
})
it("should handle empty tool result gracefully", async () => {
const toolCallId = "tool_call_789"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "attempt_completion",
params: { result: "Task completed" },
},
]
// Empty response
mockTask.ask = vi.fn().mockResolvedValue({
response: "yesButtonClicked",
text: undefined,
images: undefined,
})
await presentAssistantMessage(mockTask)
const toolResult = mockTask.userMessageContent.find(
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId,
)
expect(toolResult).toBeDefined()
// Should have fallback text
expect(toolResult.content).toBeTruthy()
})
describe("Multiple tool calls handling", () => {
it("should send tool_result with is_error for skipped tools in native tool calling when didRejectTool is true", async () => {
// Simulate multiple tool calls with native protocol (all have IDs)
const toolCallId1 = "tool_call_001"
const toolCallId2 = "tool_call_002"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId1,
name: "read_file",
params: { path: "test.txt" },
},
{
type: "tool_use",
id: toolCallId2,
name: "write_to_file",
params: { path: "output.txt", content: "test" },
},
]
// First tool is rejected
mockTask.didRejectTool = true
// Process the second tool (should be skipped)
mockTask.currentStreamingContentIndex = 1
await presentAssistantMessage(mockTask)
// Find the tool_result for the second tool
const toolResult = mockTask.userMessageContent.find(
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId2,
)
// Verify that a tool_result block was created (not a text block)
expect(toolResult).toBeDefined()
expect(toolResult.tool_use_id).toBe(toolCallId2)
expect(toolResult.is_error).toBe(true)
expect(toolResult.content).toContain("due to user rejecting a previous tool")
// Ensure no text blocks were added for this rejection
const textBlocks = mockTask.userMessageContent.filter(
(item: any) => item.type === "text" && item.text.includes("due to user rejecting"),
)
expect(textBlocks.length).toBe(0)
})
it("should reject subsequent tool calls when a legacy/XML-style tool call is encountered", async () => {
mockTask.assistantMessageContent = [
{
type: "tool_use",
name: "read_file",
params: { path: "test.txt" },
},
{
type: "tool_use",
name: "write_to_file",
params: { path: "output.txt", content: "test" },
},
]
// First tool is rejected
mockTask.didRejectTool = true
// Process the second tool (should be skipped)
mockTask.currentStreamingContentIndex = 1
await presentAssistantMessage(mockTask)
const textBlocks = mockTask.userMessageContent.filter((item: any) => item.type === "text")
expect(textBlocks.some((b: any) => String(b.text).includes("XML tool calls are no longer supported"))).toBe(
true,
)
// Ensure no tool_result blocks were added
expect(mockTask.userMessageContent.some((item: any) => item.type === "tool_result")).toBe(false)
})
it("should handle partial tool blocks when didRejectTool is true in native tool calling", async () => {
const toolCallId = "tool_call_005"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "write_to_file",
params: { path: "output.txt", content: "test" },
partial: true, // Partial tool block
},
]
mockTask.didRejectTool = true
await presentAssistantMessage(mockTask)
// Find the tool_result
const toolResult = mockTask.userMessageContent.find(
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId,
)
// Verify tool_result was created for partial block
expect(toolResult).toBeDefined()
expect(toolResult.is_error).toBe(true)
expect(toolResult.content).toContain("was interrupted and not executed")
})
})
})