-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathapi.spec.ts
More file actions
542 lines (453 loc) · 15.6 KB
/
Copy pathapi.spec.ts
File metadata and controls
542 lines (453 loc) · 15.6 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
import { type ModelInfo, type ProviderSettings, ANTHROPIC_DEFAULT_MAX_TOKENS } from "@roo-code/types"
import { getModelMaxOutputTokens, shouldUseReasoningBudget, shouldUseReasoningEffort } from "../api"
describe("getModelMaxOutputTokens", () => {
const mockModel: ModelInfo = {
maxTokens: 8192,
contextWindow: 200000,
supportsPromptCache: true,
}
test("should return model maxTokens when maxTokens is within 20% of context window", () => {
const settings: ProviderSettings = {
apiProvider: "anthropic",
}
// mockModel has maxTokens: 8192 and contextWindow: 200000
// 8192 is 4.096% of 200000, which is <= 20%, so it should use model.maxTokens
const result = getModelMaxOutputTokens({
modelId: "claude-3-5-sonnet-20241022",
model: mockModel,
settings,
})
expect(result).toBe(8192)
})
test("should handle reasoning budget models correctly", () => {
const reasoningModel: ModelInfo = {
...mockModel,
supportsReasoningBudget: true,
requiredReasoningBudget: true,
}
const settings: ProviderSettings = {
apiProvider: "anthropic",
enableReasoningEffort: true,
modelMaxTokens: 32000,
}
const result = getModelMaxOutputTokens({
modelId: "claude-3-7-sonnet-20250219",
model: reasoningModel,
settings,
})
expect(result).toBe(32000)
})
test("should return default of 8192 when maxTokens is undefined", () => {
const modelWithoutMaxTokens: ModelInfo = {
contextWindow: 100000,
supportsPromptCache: true,
}
const result = getModelMaxOutputTokens({
modelId: "some-model",
model: modelWithoutMaxTokens,
settings: {},
})
expect(result).toBe(8192)
})
test("should return ANTHROPIC_DEFAULT_MAX_TOKENS for Anthropic models that support reasoning budget but aren't using it", () => {
const anthropicModelId = "claude-sonnet-4-20250514"
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningBudget: true,
maxTokens: 64_000, // This should be ignored
}
const settings: ProviderSettings = {
apiProvider: "anthropic",
enableReasoningEffort: false, // Not using reasoning
}
const result = getModelMaxOutputTokens({ modelId: anthropicModelId, model, settings })
expect(result).toBe(ANTHROPIC_DEFAULT_MAX_TOKENS) // Should be 8192, not 64_000
})
test("should preserve Anthropic hybrid token handling when a model also supports binary reasoning", () => {
const model: ModelInfo = {
contextWindow: 1_000_000,
supportsPromptCache: true,
supportsReasoningBudget: true,
supportsReasoningBinary: true,
maxTokens: 128_000,
}
expect(
getModelMaxOutputTokens({
modelId: "claude-opus-4-7",
model,
settings: { apiProvider: "anthropic", enableReasoningEffort: false },
}),
).toBe(ANTHROPIC_DEFAULT_MAX_TOKENS)
expect(
getModelMaxOutputTokens({
modelId: "claude-opus-4-7",
model,
settings: { apiProvider: "anthropic", enableReasoningEffort: true, modelMaxTokens: 32_768 },
}),
).toBe(32_768)
})
test("should return model.maxTokens for non-Anthropic models that support reasoning budget but aren't using it", () => {
const geminiModelId = "gemini-2.5-flash-preview-04-17"
const model: ModelInfo = {
contextWindow: 1_048_576,
supportsPromptCache: false,
supportsReasoningBudget: true,
maxTokens: 65_535, // 65_535 is ~6.25% of 1_048_576, which is <= 20%
}
const settings: ProviderSettings = {
apiProvider: "gemini",
enableReasoningEffort: false, // Not using reasoning
}
const result = getModelMaxOutputTokens({ modelId: geminiModelId, model, settings })
expect(result).toBe(65_535) // Should use model.maxTokens since it's within 20% threshold
})
test("should clamp maxTokens to 20% of context window when maxTokens exceeds threshold", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: false,
maxTokens: 50_000, // 50% of context window, exceeds 20% threshold
}
const settings: ProviderSettings = {
apiProvider: "openai",
}
const result = getModelMaxOutputTokens({
modelId: "gpt-4",
model,
settings,
format: "openai",
})
// Should clamp to 20% of context window: 100_000 * 0.2 = 20_000
expect(result).toBe(20_000)
})
test("should clamp maxTokens to 20% of context window for Anthropic models when maxTokens exceeds threshold", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: true,
maxTokens: 50_000, // 50% of context window, exceeds 20% threshold
}
const settings: ProviderSettings = {
apiProvider: "anthropic",
}
const result = getModelMaxOutputTokens({
modelId: "claude-3-5-sonnet-20241022",
model,
settings,
})
// Should clamp to 20% of context window: 100_000 * 0.2 = 20_000
expect(result).toBe(20_000)
})
test("should use model.maxTokens when exactly at 20% threshold", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: false,
maxTokens: 20_000, // Exactly 20% of context window
}
const settings: ProviderSettings = {
apiProvider: "openai",
}
const result = getModelMaxOutputTokens({
modelId: "gpt-4",
model,
settings,
format: "openai",
})
expect(result).toBe(20_000) // Should use model.maxTokens since it's exactly at 20%
})
test("should bypass 20% cap for GPT-5 models and use exact configured max tokens", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: false,
maxTokens: 128_000, // 64% of context window, normally would be capped
}
const settings: ProviderSettings = {
apiProvider: "openai",
}
// Test various GPT-5 model IDs
const gpt5ModelIds = ["gpt-5", "gpt-5-turbo", "GPT-5", "openai/gpt-5-preview", "gpt-5-32k", "GPT-5-TURBO"]
gpt5ModelIds.forEach((modelId) => {
const result = getModelMaxOutputTokens({
modelId,
model,
settings,
format: "openai",
})
// Should use full 128k tokens, not capped to 20% (40k)
expect(result).toBe(128_000)
})
})
test("should still apply 20% cap to non-GPT-5 models", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: false,
maxTokens: 128_000, // 64% of context window, should be capped
}
const settings: ProviderSettings = {
apiProvider: "openai",
}
// Test non-GPT-5 model IDs
const nonGpt5ModelIds = ["gpt-4", "gpt-4-turbo", "gpt-3.5-turbo", "claude-3-5-sonnet", "gemini-pro"]
nonGpt5ModelIds.forEach((modelId) => {
const result = getModelMaxOutputTokens({
modelId,
model,
settings,
format: "openai",
})
// Should be capped to 20% of context window: 200_000 * 0.2 = 40_000
expect(result).toBe(40_000)
})
})
test("should handle GPT-5 models with various max token configurations", () => {
const testCases = [
{
maxTokens: 128_000,
contextWindow: 200_000,
expected: 128_000, // Uses full 128k
},
{
maxTokens: 64_000,
contextWindow: 200_000,
expected: 64_000, // Uses configured 64k
},
{
maxTokens: 256_000,
contextWindow: 400_000,
expected: 256_000, // Uses full 256k even though it's 64% of context
},
]
testCases.forEach(({ maxTokens, contextWindow, expected }) => {
const model: ModelInfo = {
contextWindow,
supportsPromptCache: false,
maxTokens,
}
const result = getModelMaxOutputTokens({
modelId: "gpt-5-turbo",
model,
settings: { apiProvider: "openai" },
format: "openai",
})
expect(result).toBe(expected)
})
})
test("should still clamp Z.ai models to 20% of context window by default", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
maxTokens: 131_072,
supportsReasoningEffort: ["disable", "medium"],
}
const result = getModelMaxOutputTokens({
modelId: "glm-5.1",
model,
settings: { apiProvider: "zai" },
format: "openai",
})
expect(result).toBe(40_000)
})
test("should still clamp non-Z.ai models with high maxTokens to 20% of context window", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: false,
maxTokens: 131_072,
}
const result = getModelMaxOutputTokens({
modelId: "glm-5.1",
model,
settings: { apiProvider: "openai" },
format: "openai",
})
expect(result).toBe(40_000)
})
test("should return modelMaxTokens from settings when reasoning budget is required", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
requiredReasoningBudget: true,
maxTokens: 8000,
}
const settings: ProviderSettings = {
modelMaxTokens: 4000,
}
expect(getModelMaxOutputTokens({ modelId: "test", model, settings })).toBe(4000)
})
test("should return default 16_384 for reasoning budget models when modelMaxTokens not provided", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
requiredReasoningBudget: true,
maxTokens: 8000,
}
const settings = {}
expect(getModelMaxOutputTokens({ modelId: "test", model, settings })).toBe(16_384)
})
})
describe("shouldUseReasoningBudget", () => {
test("should return true when model has requiredReasoningBudget", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
requiredReasoningBudget: true,
}
// Should return true regardless of settings
expect(shouldUseReasoningBudget({ model })).toBe(true)
expect(shouldUseReasoningBudget({ model, settings: {} })).toBe(true)
expect(shouldUseReasoningBudget({ model, settings: { enableReasoningEffort: false } })).toBe(true)
})
test("should return true when model supports reasoning budget and settings enable reasoning effort", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningBudget: true,
}
const settings: ProviderSettings = {
enableReasoningEffort: true,
}
expect(shouldUseReasoningBudget({ model, settings })).toBe(true)
})
test("should return false when model supports reasoning budget but settings don't enable reasoning effort", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningBudget: true,
}
const settings: ProviderSettings = {
enableReasoningEffort: false,
}
expect(shouldUseReasoningBudget({ model, settings })).toBe(false)
expect(shouldUseReasoningBudget({ model, settings: {} })).toBe(false)
expect(shouldUseReasoningBudget({ model })).toBe(false)
})
test("should return false when model doesn't support reasoning budget", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
}
const settings: ProviderSettings = {
enableReasoningEffort: true,
}
expect(shouldUseReasoningBudget({ model, settings })).toBe(false)
expect(shouldUseReasoningBudget({ model })).toBe(false)
})
})
describe("shouldUseReasoningEffort", () => {
test("should return true when model has reasoningEffort property", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
reasoningEffort: "medium",
}
expect(shouldUseReasoningEffort({ model })).toBe(true)
expect(shouldUseReasoningEffort({ model, settings: {} })).toBe(true)
expect(shouldUseReasoningEffort({ model, settings: { reasoningEffort: undefined } })).toBe(true)
})
test("should return false when enableReasoningEffort is false, even if reasoningEffort is set", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningEffort: true,
}
const settings: ProviderSettings = {
enableReasoningEffort: false,
reasoningEffort: "medium",
}
expect(shouldUseReasoningEffort({ model, settings })).toBe(false)
})
test("should return false when enableReasoningEffort is false, even if model has reasoningEffort property", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
reasoningEffort: "medium",
}
const settings: ProviderSettings = {
enableReasoningEffort: false,
}
expect(shouldUseReasoningEffort({ model, settings })).toBe(false)
})
test("should return true when model supports reasoning effort and settings provide reasoning effort", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningEffort: true,
}
const settings: ProviderSettings = {
reasoningEffort: "high",
}
expect(shouldUseReasoningEffort({ model, settings })).toBe(true)
})
test("should return false when model supports reasoning effort but settings don't provide reasoning effort", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningEffort: true,
}
const settings: ProviderSettings = {
reasoningEffort: undefined,
}
expect(shouldUseReasoningEffort({ model, settings })).toBe(false)
expect(shouldUseReasoningEffort({ model, settings: {} })).toBe(false)
expect(shouldUseReasoningEffort({ model })).toBe(false)
})
test("should return false when model doesn't support reasoning effort and has no default", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
}
const settings: ProviderSettings = {
reasoningEffort: "high",
}
expect(shouldUseReasoningEffort({ model, settings })).toBe(false)
expect(shouldUseReasoningEffort({ model })).toBe(false)
})
test("should handle different reasoning effort values", () => {
const model: ModelInfo = {
contextWindow: 200_000,
supportsPromptCache: true,
supportsReasoningEffort: true,
}
const settingsLow: ProviderSettings = { reasoningEffort: "low" }
const settingsMedium: ProviderSettings = { reasoningEffort: "medium" }
const settingsHigh: ProviderSettings = { reasoningEffort: "high" }
expect(shouldUseReasoningEffort({ model, settings: settingsLow })).toBe(true)
expect(shouldUseReasoningEffort({ model, settings: settingsMedium })).toBe(true)
expect(shouldUseReasoningEffort({ model, settings: settingsHigh })).toBe(true)
})
// New cases for extended capability surface
test("array capability includes 'disable' with selection 'disable' -> false", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: true,
supportsReasoningEffort: ["disable", "low", "medium", "high"] as unknown as any,
}
const settings: ProviderSettings = { enableReasoningEffort: true, reasoningEffort: "disable" as any }
expect(shouldUseReasoningEffort({ model, settings })).toBe(false)
})
test("array capability includes 'none' with settings.reasoningEffort='none' -> true", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: true,
supportsReasoningEffort: ["none", "minimal", "low", "medium", "high"] as unknown as any,
}
const settings: ProviderSettings = { enableReasoningEffort: true, reasoningEffort: "none" as any }
expect(shouldUseReasoningEffort({ model, settings })).toBe(true)
})
test("array capability includes 'minimal' with settings.reasoningEffort='minimal' -> true", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: true,
supportsReasoningEffort: ["none", "minimal", "low", "medium", "high"] as unknown as any,
}
const settings: ProviderSettings = { enableReasoningEffort: true, reasoningEffort: "minimal" as any }
expect(shouldUseReasoningEffort({ model, settings })).toBe(true)
})
test("boolean true with 'none' and 'minimal' -> true", () => {
const model: ModelInfo = {
contextWindow: 100_000,
supportsPromptCache: true,
supportsReasoningEffort: true,
}
expect(shouldUseReasoningEffort({ model, settings: { reasoningEffort: "none" as any } })).toBe(true)
expect(shouldUseReasoningEffort({ model, settings: { reasoningEffort: "minimal" as any } })).toBe(true)
})
})