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 pathNativeToolCallParser.spec.ts
More file actions
449 lines (388 loc) · 13.8 KB
/
Copy pathNativeToolCallParser.spec.ts
File metadata and controls
449 lines (388 loc) · 13.8 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { NativeToolCallParser } from "../NativeToolCallParser"
describe("NativeToolCallParser", () => {
beforeEach(() => {
NativeToolCallParser.clearAllStreamingToolCalls()
NativeToolCallParser.clearRawChunkState()
})
describe("parseToolCall", () => {
describe("read_file tool", () => {
it("should parse minimal single-file read_file args", () => {
const toolCall = {
id: "toolu_123",
name: "read_file" as const,
arguments: JSON.stringify({
path: "src/core/task/Task.ts",
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.nativeArgs).toBeDefined()
const nativeArgs = result.nativeArgs as { path: string }
expect(nativeArgs.path).toBe("src/core/task/Task.ts")
}
})
it("should parse slice-mode params", () => {
const toolCall = {
id: "toolu_123",
name: "read_file" as const,
arguments: JSON.stringify({
path: "src/core/task/Task.ts",
mode: "slice",
offset: 10,
limit: 20,
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
const nativeArgs = result.nativeArgs as {
path: string
mode?: string
offset?: number
limit?: number
}
expect(nativeArgs.path).toBe("src/core/task/Task.ts")
expect(nativeArgs.mode).toBe("slice")
expect(nativeArgs.offset).toBe(10)
expect(nativeArgs.limit).toBe(20)
}
})
it("should parse indentation-mode params", () => {
const toolCall = {
id: "toolu_123",
name: "read_file" as const,
arguments: JSON.stringify({
path: "src/utils.ts",
mode: "indentation",
indentation: {
anchor_line: 123,
max_levels: 2,
include_siblings: true,
include_header: false,
},
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
const nativeArgs = result.nativeArgs as {
path: string
mode?: string
indentation?: {
anchor_line?: number
max_levels?: number
include_siblings?: boolean
include_header?: boolean
}
}
expect(nativeArgs.path).toBe("src/utils.ts")
expect(nativeArgs.mode).toBe("indentation")
expect(nativeArgs.indentation?.anchor_line).toBe(123)
expect(nativeArgs.indentation?.include_siblings).toBe(true)
expect(nativeArgs.indentation?.include_header).toBe(false)
}
})
// Legacy format backward compatibility tests
describe("legacy format backward compatibility", () => {
it("should parse legacy files array format with single file", () => {
const toolCall = {
id: "toolu_legacy_1",
name: "read_file" as const,
arguments: JSON.stringify({
files: [{ path: "src/legacy/file.ts" }],
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBe(true)
const nativeArgs = result.nativeArgs as { files: Array<{ path: string }>; _legacyFormat: true }
expect(nativeArgs._legacyFormat).toBe(true)
expect(nativeArgs.files).toHaveLength(1)
expect(nativeArgs.files[0].path).toBe("src/legacy/file.ts")
}
})
it("should parse legacy files array format with multiple files", () => {
const toolCall = {
id: "toolu_legacy_2",
name: "read_file" as const,
arguments: JSON.stringify({
files: [{ path: "src/file1.ts" }, { path: "src/file2.ts" }, { path: "src/file3.ts" }],
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBe(true)
const nativeArgs = result.nativeArgs as { files: Array<{ path: string }>; _legacyFormat: true }
expect(nativeArgs.files).toHaveLength(3)
expect(nativeArgs.files[0].path).toBe("src/file1.ts")
expect(nativeArgs.files[1].path).toBe("src/file2.ts")
expect(nativeArgs.files[2].path).toBe("src/file3.ts")
}
})
it("should parse legacy line_ranges as tuples", () => {
const toolCall = {
id: "toolu_legacy_3",
name: "read_file" as const,
arguments: JSON.stringify({
files: [
{
path: "src/task.ts",
line_ranges: [
[1, 50],
[100, 150],
],
},
],
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBe(true)
const nativeArgs = result.nativeArgs as {
files: Array<{ path: string; lineRanges?: Array<{ start: number; end: number }> }>
_legacyFormat: true
}
expect(nativeArgs.files[0].lineRanges).toHaveLength(2)
expect(nativeArgs.files[0].lineRanges?.[0]).toEqual({ start: 1, end: 50 })
expect(nativeArgs.files[0].lineRanges?.[1]).toEqual({ start: 100, end: 150 })
}
})
it("should parse legacy line_ranges as objects", () => {
const toolCall = {
id: "toolu_legacy_4",
name: "read_file" as const,
arguments: JSON.stringify({
files: [
{
path: "src/task.ts",
line_ranges: [
{ start: 10, end: 20 },
{ start: 30, end: 40 },
],
},
],
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBe(true)
const nativeArgs = result.nativeArgs as {
files: Array<{ path: string; lineRanges?: Array<{ start: number; end: number }> }>
}
expect(nativeArgs.files[0].lineRanges).toHaveLength(2)
expect(nativeArgs.files[0].lineRanges?.[0]).toEqual({ start: 10, end: 20 })
expect(nativeArgs.files[0].lineRanges?.[1]).toEqual({ start: 30, end: 40 })
}
})
it("should parse legacy line_ranges as strings", () => {
const toolCall = {
id: "toolu_legacy_5",
name: "read_file" as const,
arguments: JSON.stringify({
files: [
{
path: "src/task.ts",
line_ranges: ["1-50", "100-150"],
},
],
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBe(true)
const nativeArgs = result.nativeArgs as {
files: Array<{ path: string; lineRanges?: Array<{ start: number; end: number }> }>
}
expect(nativeArgs.files[0].lineRanges).toHaveLength(2)
expect(nativeArgs.files[0].lineRanges?.[0]).toEqual({ start: 1, end: 50 })
expect(nativeArgs.files[0].lineRanges?.[1]).toEqual({ start: 100, end: 150 })
}
})
it("should parse double-stringified files array (model quirk)", () => {
// This tests the real-world case where some models double-stringify the files array
// e.g., { files: "[{\"path\": \"...\"}]" } instead of { files: [{path: "..."}] }
const toolCall = {
id: "toolu_double_stringify",
name: "read_file" as const,
arguments: JSON.stringify({
files: JSON.stringify([
{ path: "src/services/browser/browserDiscovery.ts" },
{ path: "src/services/mcp/McpServerManager.ts" },
]),
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBe(true)
const nativeArgs = result.nativeArgs as {
files: Array<{ path: string }>
_legacyFormat: true
}
expect(nativeArgs._legacyFormat).toBe(true)
expect(nativeArgs.files).toHaveLength(2)
expect(nativeArgs.files[0].path).toBe("src/services/browser/browserDiscovery.ts")
expect(nativeArgs.files[1].path).toBe("src/services/mcp/McpServerManager.ts")
}
})
it("should NOT set usedLegacyFormat for new format", () => {
const toolCall = {
id: "toolu_new",
name: "read_file" as const,
arguments: JSON.stringify({
path: "src/new/format.ts",
mode: "slice",
offset: 1,
limit: 100,
}),
}
const result = NativeToolCallParser.parseToolCall(toolCall)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
expect(result.usedLegacyFormat).toBeUndefined()
}
})
})
})
})
describe("processStreamingChunk", () => {
describe("read_file tool", () => {
it("should emit a partial ToolUse with nativeArgs.path during streaming", () => {
const id = "toolu_streaming_123"
NativeToolCallParser.startStreamingToolCall(id, "read_file")
// Simulate streaming chunks
const fullArgs = JSON.stringify({ path: "src/test.ts" })
// Process the complete args as a single chunk for simplicity
const result = NativeToolCallParser.processStreamingChunk(id, fullArgs)
expect(result).not.toBeNull()
expect(result?.nativeArgs).toBeDefined()
const nativeArgs = result?.nativeArgs as { path: string }
expect(nativeArgs.path).toBe("src/test.ts")
})
})
})
describe("finalizeStreamingToolCall", () => {
describe("read_file tool", () => {
it("should parse read_file args on finalize", () => {
const id = "toolu_finalize_123"
NativeToolCallParser.startStreamingToolCall(id, "read_file")
// Add the complete arguments
NativeToolCallParser.processStreamingChunk(
id,
JSON.stringify({
path: "finalized.ts",
mode: "slice",
offset: 1,
limit: 10,
}),
)
const result = NativeToolCallParser.finalizeStreamingToolCall(id)
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
if (result?.type === "tool_use") {
const nativeArgs = result.nativeArgs as { path: string; offset?: number; limit?: number }
expect(nativeArgs.path).toBe("finalized.ts")
expect(nativeArgs.offset).toBe(1)
expect(nativeArgs.limit).toBe(10)
}
})
})
})
describe("processFinishReason", () => {
describe("tool call tracking synchronization", () => {
it("should emit tool_call_end only for tool calls that have started", () => {
// Simulate a tool call with both ID and name (will start)
NativeToolCallParser.processRawChunk({
index: 0,
id: "call_started_123",
name: "read_file",
})
const events = NativeToolCallParser.processFinishReason("tool_calls")
expect(events).toHaveLength(1)
expect(events[0]).toEqual({
type: "tool_call_end",
id: "call_started_123",
})
})
it("should NOT emit tool_call_end for tool calls without a name (never started)", () => {
// Simulate a tool call with ID but NO name - this happens when models
// send malformed tool calls or split ID/name across chunks incorrectly
NativeToolCallParser.processRawChunk({
index: 0,
id: "call_no_name_456",
// No name provided - tool_call_start will not be emitted
})
// Capture console.warn to verify warning is logged
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
const events = NativeToolCallParser.processFinishReason("tool_calls")
// Should NOT emit tool_call_end since tool was never started
expect(events).toHaveLength(0)
// Should log a warning about the unstarted tool call
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining("Skipping tool_call_end for unstarted tool call"),
)
warnSpy.mockRestore()
})
it("should handle mixed started and unstarted tool calls correctly", () => {
// Tool call with ID and name (will start)
NativeToolCallParser.processRawChunk({
index: 0,
id: "call_with_name",
name: "read_file",
})
// Tool call with only ID (will not start)
NativeToolCallParser.processRawChunk({
index: 1,
id: "call_without_name",
// No name
})
// Another tool call with ID and name (will start)
NativeToolCallParser.processRawChunk({
index: 2,
id: "call_also_with_name",
name: "write_to_file",
})
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
const events = NativeToolCallParser.processFinishReason("tool_calls")
// Should only emit tool_call_end for the two started tool calls
expect(events).toHaveLength(2)
expect(events.map((e) => e.id)).toContain("call_with_name")
expect(events.map((e) => e.id)).toContain("call_also_with_name")
expect(events.map((e) => e.id)).not.toContain("call_without_name")
// Should log warning for the unstarted tool call
expect(warnSpy).toHaveBeenCalledTimes(1)
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("call_without_name"))
warnSpy.mockRestore()
})
it("should return empty array when finish_reason is not tool_calls", () => {
NativeToolCallParser.processRawChunk({
index: 0,
id: "call_123",
name: "read_file",
})
const events = NativeToolCallParser.processFinishReason("stop")
expect(events).toHaveLength(0)
})
it("should return empty array when no tool calls are tracked", () => {
const events = NativeToolCallParser.processFinishReason("tool_calls")
expect(events).toHaveLength(0)
})
})
})
})