import { describe, it, expect, beforeEach } from "vitest" import { createHash } from "crypto" import { OpenAiNativeHandler } from "../openai-native" import { OpenAiServiceTier, openAiNativeModels, type ModelInfo } from "@roo-code/types" import type { ApiStreamUsageChunk } from "../../transform/stream" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" // The streaming entry point reaches the SDK client, so stub the client surface the // handler actually calls. Without this the real SDK would attempt a network request. const mockResponsesCreate = vitest.fn() vitest.mock("openai", () => ({ __esModule: true, default: vitest.fn().mockImplementation(function () { return { responses: { create: mockResponsesCreate, }, } }), })) 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"], } const getGpt6AstraModel = () => new OpenAiNativeHandler({ openAiNativeApiKey: "test-key", apiModelId: "gpt-6-astra" }).getModel() 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 bill nested cache write tokens at the Astra rate", () => { const usage = { input_tokens: 100_000, output_tokens: 1_000, input_tokens_details: { cached_tokens: 20_000, cache_write_tokens: 30_000, cache_miss_tokens: 50_000, }, } const result = handler["normalizeUsage"](usage, getGpt6AstraModel()) expect(result).toMatchObject({ inputTokens: 100_000, outputTokens: 1_000, cacheReadTokens: 20_000, cacheWriteTokens: 30_000, }) if (!result) throw new Error("Expected usage") expect(result.totalCost).toBeCloseTo(0.945, 6) }) it("should derive totals from nested Astra cache details", () => { const result = handler["normalizeUsage"]( { output_tokens: 0, input_tokens_details: { cached_tokens: 20, cache_write_tokens: 30, cache_miss_tokens: 50, }, }, getGpt6AstraModel(), ) expect(result).toMatchObject({ inputTokens: 100, cacheReadTokens: 20, cacheWriteTokens: 30 }) }) 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("OpenAiNativeHandler - prompt cache key", () => { let handler: OpenAiNativeHandler beforeEach(() => { handler = new OpenAiNativeHandler({ openAiNativeApiKey: "test-key", }) }) const buildRequestBody = (modelId: string, taskId?: string) => { handler["options"].apiModelId = modelId const model = handler.getModel() const metadata = taskId === undefined ? undefined : { taskId } return handler["buildRequestBody"](model, [], "", model.verbosity, undefined, metadata) } it("should send the task id as the body prompt_cache_key", () => { const body = buildRequestBody("gpt-5.1", "task-abc") expect(body.prompt_cache_key).toBe("task-abc") }) it("should keep the key stable across turns of the same task", () => { const firstTurn = buildRequestBody("gpt-5.1", "task-stable") const secondTurn = buildRequestBody("gpt-5.1", "task-stable") expect(firstTurn.prompt_cache_key).toBe("task-stable") expect(secondTurn.prompt_cache_key).toBe(firstTurn.prompt_cache_key) }) it("should give distinct keys to distinct tasks", () => { const first = buildRequestBody("gpt-5.1", "task-one") const second = buildRequestBody("gpt-5.1", "task-two") expect(first.prompt_cache_key).toBe("task-one") expect(second.prompt_cache_key).toBe("task-two") expect(first.prompt_cache_key).not.toBe(second.prompt_cache_key) }) it("should omit the key when no task id is supplied", () => { // No task-scoped identity means no accumulating prefix, so the field is omitted // rather than filled with a per-call random value that would override default routing. const body = buildRequestBody("gpt-5.1") expect(body).not.toHaveProperty("prompt_cache_key") }) it("should omit the key 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 = buildRequestBody(modelId, "task-abc") expect(body.prompt_cache_key).toBeUndefined() }) it("should omit the key for a non-caching model through the streaming entry point", async () => { // The builder-level test above cannot see the streaming path: createMessage forwards // metadata into buildRequestBody, so a key added anywhere along that path would still // pass it. Drive the public entry point with a task id to pin the model gate there too. const modelId = "codex-mini-latest" expect(openAiNativeModels[modelId as keyof typeof openAiNativeModels].supportsPromptCache).toBe(false) mockResponsesCreate.mockClear() mockResponsesCreate.mockResolvedValue(asyncStreamFrom([])) const streamingHandler = new OpenAiNativeHandler({ openAiNativeApiKey: "test-key", apiModelId: modelId, }) await collectStream( streamingHandler.createMessage("You are a helpful assistant.", [{ role: "user", content: "Hello!" }], { taskId: "task-abc", }), ) expect(mockResponsesCreate).toHaveBeenCalledTimes(1) const [body] = mockResponsesCreate.mock.calls[0] expect(body).not.toHaveProperty("prompt_cache_key") }) it("should clamp an over-length task id to OpenAI's 64-character limit", () => { const longTaskId = "t".repeat(80) const body = buildRequestBody("gpt-5.1", longTaskId) expect(body.prompt_cache_key).toHaveLength(64) // A hash, not a slice: truncation would collapse ids that share a prefix. expect(body.prompt_cache_key).toBe(createHash("sha256").update(longTaskId).digest("hex")) expect(body.prompt_cache_key).not.toBe(longTaskId.slice(0, 64)) }) it("should keep prompt_cache_retention alongside the key for 24h models", () => { const body = buildRequestBody("gpt-5.1", "task-abc") expect(body.prompt_cache_key).toBe("task-abc") expect(body.prompt_cache_retention).toBe("24h") }) }) describe("cost calculation", () => { it.each([ { tier: OpenAiServiceTier.Default, expectedCost: 4.525 }, { tier: OpenAiServiceTier.Flex, expectedCost: 2.2625 }, { tier: OpenAiServiceTier.Priority, expectedCost: 9.05 }, ])("applies Astra long-context cache pricing for the $tier tier", ({ tier, expectedCost }) => { handler = new OpenAiNativeHandler({ openAiNativeApiKey: "test-key", openAiNativeServiceTier: tier, }) const result = handler["normalizeUsage"]( { input_tokens: 300_000, output_tokens: 1_000, input_tokens_details: { cached_tokens: 100_000, cache_write_tokens: 50_000 }, }, getGpt6AstraModel(), ) if (!result) throw new Error("Expected usage") expect(result.totalCost).toBeCloseTo(expectedCost, 6) }) it.each([ // gpt-6-astra: default input 10.0 / 12h 25.0; flex input 5.0; priority input 20.0. // The 12h rate must follow the tier's own input price (2.5x), not the // standard-tier 12h rate, or a flex/priority 12h write is mis-billed. { tier: OpenAiServiceTier.Default, expectedCost: 2.5 }, { tier: OpenAiServiceTier.Flex, expectedCost: 1.25 }, { tier: OpenAiServiceTier.Priority, expectedCost: 5.0 }, ])("derives the 12h rate from the $tier tier's own input price", ({ tier, expectedCost }) => { handler = new OpenAiNativeHandler({ openAiNativeApiKey: "test-key", openAiNativeServiceTier: tier, }) const result = handler["normalizeUsage"]( { input_tokens: 100_000, output_tokens: 0, input_tokens_details: { cache_write_tokens: 100_000, cache_write_tokens_details: { ttl_12h_tokens: 100_000 }, }, }, getGpt6AstraModel(), ) if (!result) throw new Error("Expected usage") // All input is consumed by the write, so the whole cost is the 12h class. expect(result.totalCost).toBeCloseTo(expectedCost, 6) }) 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: OpenAiServiceTier.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) }) }) describe("cache-write TTL split (Responses API)", () => { // `normalizeUsage` is private. A narrow structural cast keeps the call typed // without widening the handler to `any`; the return is asserted non-null because // every call site below passes a usage object that yields a chunk. const normalizeUsage = ( h: OpenAiNativeHandler, usage: unknown, model: { id: string; info: ModelInfo }, ): ApiStreamUsageChunk => { const result = ( h as unknown as { normalizeUsage(u: unknown, m: { id: string; info: ModelInfo }): ApiStreamUsageChunk | undefined } ).normalizeUsage(usage, model) expect(result).toBeDefined() return result as ApiStreamUsageChunk } it("should read ttl_12h_tokens and bill it at the 12h rate", () => { // gpt-5.6-sol: input 4.0, cacheWritesPrice 5.0 (30m class), cacheWritesPrice12h 10.0. const model = getGpt6AstraModel() const usage = { input_tokens: 100_000, output_tokens: 0, input_tokens_details: { cache_write_tokens: 100_000, cache_write_tokens_details: { ttl_30m_tokens: 0, ttl_12h_tokens: 100_000, }, }, } const result = normalizeUsage(handler, usage, model) expect(result).toMatchObject({ type: "usage", cacheWriteTokens: 100_000, cacheWrite12hTokens: 100_000, }) // gpt-6-astra: input 10.0, 12h rate 25.0. // Base input: (10.0 / 1_000_000) * (100_000 - 100_000) = 0 // 12h writes: (25.0 / 1_000_000) * 100_000 = 2.5 // The pre-split behaviour charged the 30m rate: 12.5 * 0.1 = 1.25 (50% low). expect(result.totalCost).toBeCloseTo(2.5, 6) expect(result.totalCost).not.toBeCloseTo(1.25, 6) }) it("should split a mixed 30m/12h write total without double counting", () => { const model = getGpt6AstraModel() const usage = { input_tokens: 100_000, output_tokens: 0, input_tokens_details: { cache_write_tokens: 100_000, cache_write_tokens_details: { ttl_30m_tokens: 60_000, ttl_12h_tokens: 40_000, }, }, } const result = normalizeUsage(handler, usage, model) expect(result).toMatchObject({ cacheWriteTokens: 100_000, cacheWrite12hTokens: 40_000, }) // 30m remainder: 60_000 at 12.5 => 0.75 // 12h: 40_000 at 25.0 => 1.0 // Total: 1.75 expect(result.totalCost).toBeCloseTo(1.75, 6) }) it("should leave the split undefined when the response omits it", () => { const model = getGpt6AstraModel() const usage = { input_tokens: 100_000, output_tokens: 0, input_tokens_details: { cache_write_tokens: 100_000, }, } const result = normalizeUsage(handler, usage, model) expect(result.cacheWrite12hTokens).toBeUndefined() // All writes bill at the 30m rate: (12.5 / 1_000_000) * 100_000 = 1.25 expect(result.totalCost).toBeCloseTo(1.25, 6) }) it("should clamp a 12h split that exceeds the write total", () => { const model = getGpt6AstraModel() const usage = { input_tokens: 100_000, output_tokens: 0, input_tokens_details: { cache_write_tokens: 50_000, cache_write_tokens_details: { ttl_12h_tokens: 80_000, }, }, } const result = normalizeUsage(handler, usage, model) // Clamped to the write total so no more 12h tokens are billed than written. expect(result.cacheWrite12hTokens).toBe(50_000) // Base input: (10.0 / 1_000_000) * (100_000 - 50_000) = 0.5 // 12h writes: (25.0 / 1_000_000) * 50_000 = 1.25 // Total: 0.5 + 1.25 = 1.75 expect(result.totalCost).toBeCloseTo(1.75, 6) }) it("should clamp a negative 12h split at zero instead of emitting a negative count", () => { // A misbehaving gateway can report a negative ttl_12h_tokens. Math.min alone // leaves it negative, so the negative count reaches the Task accumulator and // the webview. Shared costing clamps, but the emitted chunk must not lie. const model = getGpt6AstraModel() const usage = { input_tokens: 100_000, output_tokens: 0, input_tokens_details: { cache_write_tokens: 100_000, cache_write_tokens_details: { ttl_12h_tokens: -40_000, }, }, } const result = normalizeUsage(handler, usage, model) // A zero split is indistinguishable from "no split", so the field stays unset. expect(result.cacheWrite12hTokens).toBeUndefined() expect(result.cacheWrite12hTokens ?? 0).toBeGreaterThanOrEqual(0) // All 100k writes bill at the 30m rate: (12.5 / 1_000_000) * 100_000 = 1.25 expect(result.totalCost).toBeCloseTo(1.25, 6) }) }) })