forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai-native-usage.spec.ts
More file actions
491 lines (411 loc) · 12.8 KB
/
openai-native-usage.spec.ts
File metadata and controls
491 lines (411 loc) · 12.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
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
import { describe, it, expect, beforeEach } from "vitest"
import { OpenAiNativeHandler } from "../openai-native"
import { openAiNativeModels } from "@roo-code/types"
describe("OpenAiNativeHandler - normalizeUsage", () => {
let handler: OpenAiNativeHandler
const mockModel = {
id: "gpt-4o",
info: openAiNativeModels["gpt-4o"],
}
const gpt54Model = {
id: "gpt-5.4",
info: openAiNativeModels["gpt-5.4"],
}
beforeEach(() => {
handler = new OpenAiNativeHandler({
openAiNativeApiKey: "test-key",
})
})
describe("detailed token shapes (Responses API)", () => {
it("should handle detailed shapes with cached and miss tokens", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
input_tokens_details: {
cached_tokens: 30,
cache_miss_tokens: 70,
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 0, // miss tokens are NOT cache writes
})
})
it("should derive total input tokens from details when totals are missing", () => {
const usage = {
// No input_tokens or prompt_tokens
output_tokens: 50,
input_tokens_details: {
cached_tokens: 30,
cache_miss_tokens: 70,
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100, // Derived from 30 + 70
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 0, // miss tokens are NOT cache writes
})
})
it("should handle prompt_tokens_details variant", () => {
const usage = {
prompt_tokens: 100,
completion_tokens: 50,
prompt_tokens_details: {
cached_tokens: 30,
cache_miss_tokens: 70,
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 0, // miss tokens are NOT cache writes
})
})
it("should handle cache_creation_input_tokens for actual cache writes", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cache_creation_input_tokens: 20,
input_tokens_details: {
cached_tokens: 30,
cache_miss_tokens: 50, // 50 miss + 30 cached + 20 creation = 100 total
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 20, // Actual cache writes from cache_creation_input_tokens
})
})
it("should handle reasoning tokens in output details", () => {
const usage = {
input_tokens: 100,
output_tokens: 150,
output_tokens_details: {
reasoning_tokens: 50,
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 150,
reasoningTokens: 50,
})
})
})
describe("legacy field names", () => {
it("should handle cache_creation_input_tokens and cache_read_input_tokens", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cache_creation_input_tokens: 20,
cache_read_input_tokens: 30,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 20,
})
})
it("should handle cache_write_tokens and cache_read_tokens", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cache_write_tokens: 20,
cache_read_tokens: 30,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 20,
})
})
it("should handle cached_tokens field", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cached_tokens: 30,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
})
})
it("should handle prompt_tokens and completion_tokens", () => {
const usage = {
prompt_tokens: 100,
completion_tokens: 50,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 0,
cacheWriteTokens: 0,
})
})
})
describe("SSE-only events", () => {
it("should handle SSE events with minimal usage data", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 0,
cacheWriteTokens: 0,
})
})
it("should handle SSE events with no cache information", () => {
const usage = {
prompt_tokens: 100,
completion_tokens: 50,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 0,
cacheWriteTokens: 0,
})
})
})
describe("edge cases", () => {
it("should handle undefined usage", () => {
const result = (handler as any).normalizeUsage(undefined, mockModel)
expect(result).toBeUndefined()
})
it("should handle null usage", () => {
const result = (handler as any).normalizeUsage(null, mockModel)
expect(result).toBeUndefined()
})
it("should handle empty usage object", () => {
const usage = {}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 0,
outputTokens: 0,
cacheReadTokens: 0,
cacheWriteTokens: 0,
})
})
it("should handle missing details but with cache fields", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cache_read_input_tokens: 30,
// No input_tokens_details
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 0,
})
})
it("should use all available cache information with proper fallbacks", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cached_tokens: 20, // Legacy field (will be used as fallback)
input_tokens_details: {
cached_tokens: 30, // Detailed shape
cache_miss_tokens: 70,
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
// The implementation uses nullish coalescing, so it will use the first non-nullish value:
// cache_read_input_tokens ?? cache_read_tokens ?? cached_tokens ?? cachedFromDetails
// Since none of the first two exist, it falls back to cached_tokens (20) before cachedFromDetails
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 20, // From cached_tokens (legacy field comes before details in fallback chain)
cacheWriteTokens: 0, // miss tokens are NOT cache writes
})
})
it("should use detailed shapes when legacy fields are not present", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
// No cached_tokens legacy field
input_tokens_details: {
cached_tokens: 30,
cache_miss_tokens: 70,
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheReadTokens: 30, // From details since no legacy field exists
cacheWriteTokens: 0, // miss tokens are NOT cache writes
})
})
it("should handle totals missing with only partial details", () => {
const usage = {
// No input_tokens or prompt_tokens
output_tokens: 50,
input_tokens_details: {
cached_tokens: 30,
// No cache_miss_tokens
},
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toMatchObject({
type: "usage",
inputTokens: 30, // Derived from cached_tokens only
outputTokens: 50,
cacheReadTokens: 30,
cacheWriteTokens: 0,
})
})
})
describe("OpenAiNativeHandler - prompt cache retention", () => {
let handler: OpenAiNativeHandler
beforeEach(() => {
handler = new OpenAiNativeHandler({
openAiNativeApiKey: "test-key",
})
})
const buildRequestBodyForModel = (modelId: string) => {
// Force the handler to use the requested model ID
;(handler as any).options.apiModelId = modelId
const model = handler.getModel()
// Minimal formatted input/systemPrompt/verbosity/metadata for building the body
return (handler as any).buildRequestBody(model, [], "", model.verbosity, undefined, undefined)
}
it("should set prompt_cache_retention=24h for gpt-5.1 models that support prompt caching", () => {
const body = buildRequestBodyForModel("gpt-5.1")
expect(body.prompt_cache_retention).toBe("24h")
const codexBody = buildRequestBodyForModel("gpt-5.1-codex")
expect(codexBody.prompt_cache_retention).toBe("24h")
const codexMiniBody = buildRequestBodyForModel("gpt-5.1-codex-mini")
expect(codexMiniBody.prompt_cache_retention).toBe("24h")
})
it("should not set prompt_cache_retention for non-gpt-5.1 models even if they support prompt caching", () => {
const body = buildRequestBodyForModel("gpt-5")
expect(body.prompt_cache_retention).toBeUndefined()
const fourOBody = buildRequestBodyForModel("gpt-4o")
expect(fourOBody.prompt_cache_retention).toBeUndefined()
const gpt54Body = buildRequestBodyForModel("gpt-5.4")
expect(gpt54Body.prompt_cache_retention).toBeUndefined()
const chatModelBody = buildRequestBodyForModel("gpt-5.3-chat-latest")
expect(chatModelBody.prompt_cache_retention).toBeUndefined()
})
it("should not set prompt_cache_retention when the model does not support prompt caching", () => {
const modelId = "codex-mini-latest"
expect(openAiNativeModels[modelId as keyof typeof openAiNativeModels].supportsPromptCache).toBe(false)
const body = buildRequestBodyForModel(modelId)
expect(body.prompt_cache_retention).toBeUndefined()
})
})
describe("cost calculation", () => {
it("should pass total input tokens to calculateApiCostOpenAI", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
cache_read_input_tokens: 30,
cache_creation_input_tokens: 20,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toHaveProperty("totalCost")
expect(result.totalCost).toBeGreaterThan(0)
// calculateApiCostOpenAI handles subtracting cache tokens internally
// It will compute: 100 - 30 - 20 = 50 uncached input tokens
})
it("should handle cost calculation with no cache reads", () => {
const usage = {
input_tokens: 100,
output_tokens: 50,
}
const result = (handler as any).normalizeUsage(usage, mockModel)
expect(result).toHaveProperty("totalCost")
expect(result.totalCost).toBeGreaterThan(0)
// Cost should be calculated with full input tokens since no cache reads
})
it("should use standard GPT-5.4 pricing within the base context threshold", () => {
const usage = {
input_tokens: 100_000,
output_tokens: 1_000,
cache_read_input_tokens: 20_000,
}
const result = (handler as any).normalizeUsage(usage, gpt54Model)
expect(result).toMatchObject({
type: "usage",
inputTokens: 100_000,
outputTokens: 1_000,
cacheReadTokens: 20_000,
})
expect(result.totalCost).toBeCloseTo(0.22, 6)
})
it("should apply GPT-5.4 long-context pricing above the threshold", () => {
const usage = {
input_tokens: 300_000,
output_tokens: 1_000,
cache_read_input_tokens: 100_000,
}
const result = (handler as any).normalizeUsage(usage, gpt54Model)
expect(result).toMatchObject({
type: "usage",
inputTokens: 300_000,
outputTokens: 1_000,
cacheReadTokens: 100_000,
})
expect(result.totalCost).toBeCloseTo(1.0475, 6)
})
it("should not apply GPT-5.4 long-context pricing to priority tier", () => {
handler = new OpenAiNativeHandler({
openAiNativeApiKey: "test-key",
openAiNativeServiceTier: "priority",
})
const usage = {
input_tokens: 300_000,
output_tokens: 1_000,
cache_read_input_tokens: 100_000,
}
const result = (handler as any).normalizeUsage(usage, gpt54Model)
expect(result).toMatchObject({
type: "usage",
inputTokens: 300_000,
outputTokens: 1_000,
cacheReadTokens: 100_000,
})
expect(result.totalCost).toBeCloseTo(1.08, 6)
})
})
})