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 pathresolveToolProtocol.spec.ts
More file actions
610 lines (564 loc) · 18.5 KB
/
Copy pathresolveToolProtocol.spec.ts
File metadata and controls
610 lines (564 loc) · 18.5 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import { describe, it, expect } from "vitest"
import { resolveToolProtocol, detectToolProtocolFromHistory } from "../resolveToolProtocol"
import { TOOL_PROTOCOL, openAiModelInfoSaneDefaults } from "@roo-code/types"
import type { ProviderSettings, ModelInfo } from "@roo-code/types"
import type { Anthropic } from "@anthropic-ai/sdk"
describe("resolveToolProtocol", () => {
describe("Precedence Level 1: User Profile Setting", () => {
it("should use profile toolProtocol when explicitly set to xml", () => {
const settings: ProviderSettings = {
toolProtocol: "xml",
apiProvider: "anthropic",
}
const result = resolveToolProtocol(settings)
expect(result).toBe(TOOL_PROTOCOL.XML)
})
it("should use profile toolProtocol when explicitly set to native", () => {
const settings: ProviderSettings = {
toolProtocol: "native",
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true, // Model supports native tools
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
it("should override model default when profile setting is present", () => {
const settings: ProviderSettings = {
toolProtocol: "xml",
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
defaultToolProtocol: "native",
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Profile setting wins
})
it("should override model capability when profile setting is present", () => {
const settings: ProviderSettings = {
toolProtocol: "xml",
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Profile setting wins
})
})
describe("Precedence Level 2: Model Default", () => {
it("should use model defaultToolProtocol when no profile setting", () => {
const settings: ProviderSettings = {
apiProvider: "roo",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
defaultToolProtocol: "native",
supportsNativeTools: true, // Model must support native tools
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.NATIVE) // Model default wins when experiment is disabled
})
it("should override model capability when model default is present", () => {
const settings: ProviderSettings = {
apiProvider: "roo",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
defaultToolProtocol: "xml",
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Model default wins over capability
})
})
describe("Support Validation", () => {
it("should fall back to XML when model doesn't support native", () => {
const settings: ProviderSettings = {
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: false,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML)
})
it("should fall back to XML when user prefers native but model doesn't support it", () => {
const settings: ProviderSettings = {
toolProtocol: "native", // User wants native
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: false, // But model doesn't support it
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Falls back to XML due to lack of support
})
it("should fall back to XML when user prefers native but model support is undefined", () => {
const settings: ProviderSettings = {
toolProtocol: "native", // User wants native
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
// supportsNativeTools is undefined (not specified)
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Falls back to XML - undefined treated as unsupported
})
})
describe("Precedence Level 3: Native Fallback", () => {
it("should use Native fallback when no model default is specified and model supports native", () => {
const settings: ProviderSettings = {
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.NATIVE) // Native fallback
})
})
describe("Complete Precedence Chain", () => {
it("should respect full precedence: Profile > Model Default > Native Fallback", () => {
// Set up a scenario with all levels defined
const settings: ProviderSettings = {
toolProtocol: "native", // Level 1: User profile setting
apiProvider: "roo",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
defaultToolProtocol: "xml", // Level 2: Model default
supportsNativeTools: true, // Support check
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.NATIVE) // Profile setting wins
})
it("should skip to model default when profile setting is undefined", () => {
const settings: ProviderSettings = {
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
defaultToolProtocol: "xml", // Level 2
supportsNativeTools: true, // Support check
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Model default wins
})
it("should skip to Native fallback when profile and model default are undefined", () => {
const settings: ProviderSettings = {
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.NATIVE) // Native fallback
})
it("should skip to XML fallback when model info is unavailable", () => {
const settings: ProviderSettings = {
apiProvider: "anthropic",
}
const result = resolveToolProtocol(settings, undefined)
expect(result).toBe(TOOL_PROTOCOL.XML) // XML fallback (no model info means no native support)
})
})
describe("Locked Protocol (Precedence Level 0)", () => {
it("should return lockedProtocol when provided, ignoring all other settings", () => {
const settings: ProviderSettings = {
toolProtocol: "xml", // User wants XML
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true,
defaultToolProtocol: "xml",
}
// lockedProtocol overrides everything
const result = resolveToolProtocol(settings, modelInfo, "native")
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
it("should return XML lockedProtocol even when model supports native", () => {
const settings: ProviderSettings = {
toolProtocol: "native", // User wants native
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true, // Model supports native
defaultToolProtocol: "native",
}
// lockedProtocol forces XML
const result = resolveToolProtocol(settings, modelInfo, "xml")
expect(result).toBe(TOOL_PROTOCOL.XML)
})
it("should fall through to normal resolution when lockedProtocol is undefined", () => {
const settings: ProviderSettings = {
toolProtocol: "xml",
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true,
}
// undefined lockedProtocol should use normal precedence
const result = resolveToolProtocol(settings, modelInfo, undefined)
expect(result).toBe(TOOL_PROTOCOL.XML) // User setting wins
})
})
describe("Edge Cases", () => {
it("should handle missing provider name gracefully", () => {
const settings: ProviderSettings = {}
const result = resolveToolProtocol(settings)
expect(result).toBe(TOOL_PROTOCOL.XML) // Falls back to XML (no model info)
})
it("should handle undefined model info gracefully", () => {
const settings: ProviderSettings = {
apiProvider: "openai-native",
}
const result = resolveToolProtocol(settings, undefined)
expect(result).toBe(TOOL_PROTOCOL.XML) // XML fallback (no model info)
})
it("should fall back to XML when model doesn't support native", () => {
const settings: ProviderSettings = {
apiProvider: "roo",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: false, // Model doesn't support native
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Falls back to XML due to lack of support
})
})
describe("Real-world Scenarios", () => {
it("should use Native fallback for models without defaultToolProtocol", () => {
const settings: ProviderSettings = {
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.NATIVE) // Native fallback
})
it("should use XML for Claude models with Anthropic provider", () => {
const settings: ProviderSettings = {
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 8192,
contextWindow: 200000,
supportsPromptCache: true,
supportsNativeTools: false,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML)
})
it("should allow user to force XML on native-supporting model", () => {
const settings: ProviderSettings = {
toolProtocol: "xml", // User explicitly wants XML
apiProvider: "openai-native",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: true, // Model supports native but user wants XML
defaultToolProtocol: "native",
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // User preference wins
})
it("should not allow user to force native when model doesn't support it", () => {
const settings: ProviderSettings = {
toolProtocol: "native", // User wants native
apiProvider: "anthropic",
}
const modelInfo: ModelInfo = {
maxTokens: 4096,
contextWindow: 128000,
supportsPromptCache: false,
supportsNativeTools: false, // Model doesn't support native
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Falls back to XML due to lack of support
})
it("should use model default when available", () => {
const settings: ProviderSettings = {
apiProvider: "roo",
}
const modelInfo: ModelInfo = {
maxTokens: 8192,
contextWindow: 200000,
supportsPromptCache: true,
defaultToolProtocol: "xml",
supportsNativeTools: true,
}
const result = resolveToolProtocol(settings, modelInfo)
expect(result).toBe(TOOL_PROTOCOL.XML) // Model default wins
})
it("should use XML for OpenAI compatible provider with default model info", () => {
const settings: ProviderSettings = {
apiProvider: "openai",
}
// OpenAI Compatible defaults to XML protocol since third-party proxies
// have varying levels of support for native tool calling.
// Users who want native can explicitly enable it in Advanced Settings.
const result = resolveToolProtocol(settings, openAiModelInfoSaneDefaults)
expect(result).toBe(TOOL_PROTOCOL.XML) // Should use XML by default for better compatibility
})
})
})
describe("detectToolProtocolFromHistory", () => {
// Helper type for API messages in tests
type ApiMessageForTest = Anthropic.MessageParam & { ts?: number }
describe("Native Protocol Detection", () => {
it("should detect native protocol when tool_use block has an id", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{
role: "assistant",
content: [
{
type: "tool_use",
id: "toolu_01abc123", // Native protocol always has an ID
name: "read_file",
input: { path: "test.ts" },
},
],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
it("should detect native protocol from the first tool_use block found", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "First message" },
{ role: "assistant", content: "Let me help you" },
{ role: "user", content: "Second message" },
{
role: "assistant",
content: [
{
type: "tool_use",
id: "toolu_first",
name: "read_file",
input: { path: "first.ts" },
},
],
},
{ role: "user", content: "Third message" },
{
role: "assistant",
content: [
{
type: "tool_use",
id: "toolu_second",
name: "write_to_file",
input: { path: "second.ts", content: "test" },
},
],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
})
describe("XML Protocol Detection", () => {
it("should detect XML protocol when tool_use block has no id", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{
role: "assistant",
content: [
{
type: "tool_use",
// No id field - XML protocol tool calls never have an ID
name: "read_file",
input: { path: "test.ts" },
} as Anthropic.ToolUseBlock, // Cast to bypass type check for missing id
],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.XML)
})
it("should detect XML protocol when id is empty string", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{
role: "assistant",
content: [
{
type: "tool_use",
id: "", // Empty string should be treated as no id
name: "read_file",
input: { path: "test.ts" },
},
],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.XML)
})
})
describe("No Tool Calls", () => {
it("should return undefined when no messages", () => {
const messages: ApiMessageForTest[] = []
const result = detectToolProtocolFromHistory(messages)
expect(result).toBeUndefined()
})
it("should return undefined when only user messages", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{ role: "user", content: "How are you?" },
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBeUndefined()
})
it("should return undefined when assistant messages have no tool_use", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi! How can I help?" },
{ role: "user", content: "What's the weather?" },
{
role: "assistant",
content: [{ type: "text", text: "I don't have access to weather data." }],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBeUndefined()
})
it("should return undefined when content is string", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there!" },
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBeUndefined()
})
})
describe("Mixed Content", () => {
it("should detect protocol from tool_use even with mixed content", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Read this file" },
{
role: "assistant",
content: [
{ type: "text", text: "I'll read that file for you." },
{
type: "tool_use",
id: "toolu_mixed",
name: "read_file",
input: { path: "test.ts" },
},
],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
it("should skip user messages and only check assistant messages", () => {
const messages: ApiMessageForTest[] = [
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "toolu_user",
content: "result",
},
],
},
{
role: "assistant",
content: [
{
type: "tool_use",
id: "toolu_assistant",
name: "write_to_file",
input: { path: "out.ts", content: "test" },
},
],
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
})
describe("Edge Cases", () => {
it("should handle messages with empty content array", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello" },
{ role: "assistant", content: [] },
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBeUndefined()
})
it("should handle messages with ts field (ApiMessage format)", () => {
const messages: ApiMessageForTest[] = [
{ role: "user", content: "Hello", ts: Date.now() },
{
role: "assistant",
content: [
{
type: "tool_use",
id: "toolu_with_ts",
name: "read_file",
input: { path: "test.ts" },
},
],
ts: Date.now(),
},
]
const result = detectToolProtocolFromHistory(messages)
expect(result).toBe(TOOL_PROTOCOL.NATIVE)
})
})
})