|
| 1 | +vitest.mock("@roo-code/telemetry", () => ({ |
| 2 | + TelemetryService: { instance: { captureException: vitest.fn() } }, |
| 3 | +})) |
| 4 | + |
| 5 | +import type { ApiHandlerOptions } from "../../../shared/api" |
| 6 | +import { openAiCodexOAuthManager } from "../../../integrations/openai-codex/oauth" |
| 7 | +import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" |
| 8 | +import { OpenAiNativeHandler } from "../openai-native" |
| 9 | +import { OpenAiCodexHandler } from "../openai-codex" |
| 10 | + |
| 11 | +const reasoningCases: { options: ApiHandlerOptions; native: string | undefined; codex: string }[] = [ |
| 12 | + { options: {}, native: "medium", codex: "low" }, |
| 13 | + { options: { reasoningEffort: "none" }, native: "none", codex: "low" }, |
| 14 | + { options: { reasoningEffort: "minimal" }, native: "medium", codex: "low" }, |
| 15 | + { options: { reasoningEffort: "disable" }, native: undefined, codex: "low" }, |
| 16 | + { options: { enableReasoningEffort: false }, native: undefined, codex: "low" }, |
| 17 | + ...(["low", "medium", "high", "xhigh", "max"] as const).map((effort) => ({ |
| 18 | + options: { reasoningEffort: effort }, |
| 19 | + native: effort, |
| 20 | + codex: effort, |
| 21 | + })), |
| 22 | +] |
| 23 | + |
| 24 | +describe("GPT-6.1 Terra requests", () => { |
| 25 | + afterEach(() => vitest.restoreAllMocks()) |
| 26 | + |
| 27 | + it.each(reasoningCases)("uses supported reasoning for $options", ({ options, native, codex }) => { |
| 28 | + const settings = { apiModelId: "gpt-6.1-terra", openAiNativeApiKey: "test-key", ...options } |
| 29 | + const nativeHandler = new OpenAiNativeHandler(settings) |
| 30 | + const codexHandler = new OpenAiCodexHandler(settings) |
| 31 | + expect(nativeHandler.getModel().id).toBe("gpt-6.1-terra") |
| 32 | + expect(codexHandler.getModel().id).toBe("gpt-6.1-terra") |
| 33 | + expect(nativeHandler["getReasoningEffort"](nativeHandler.getModel())).toBe(native) |
| 34 | + expect(codexHandler["getReasoningEffort"](codexHandler.getModel())).toBe(codex) |
| 35 | + }) |
| 36 | + |
| 37 | + it("builds native Responses tool requests with verified pricing and no temperature", () => { |
| 38 | + const handler = new OpenAiNativeHandler({ |
| 39 | + apiModelId: "gpt-6.1-terra", |
| 40 | + openAiNativeApiKey: "test-key", |
| 41 | + openAiNativeServiceTier: "priority", |
| 42 | + modelTemperature: 0.7, |
| 43 | + }) |
| 44 | + const model = handler.getModel() |
| 45 | + const body = handler["buildRequestBody"](model, [], "System", "low", handler["getReasoningEffort"](model), { |
| 46 | + taskId: "terra-task", |
| 47 | + tools: [ |
| 48 | + { type: "function", function: { name: "read_file", parameters: { type: "object", properties: {} } } }, |
| 49 | + ], |
| 50 | + }) |
| 51 | + expect(body).toMatchObject({ |
| 52 | + model: "gpt-6.1-terra", |
| 53 | + max_output_tokens: 128_000, |
| 54 | + reasoning: { effort: "medium" }, |
| 55 | + service_tier: "priority", |
| 56 | + tools: [{ type: "function", name: "read_file", strict: true }], |
| 57 | + }) |
| 58 | + expect(body).not.toHaveProperty("temperature") |
| 59 | + expect(handler["applyServiceTierPricing"](model.info, "priority")).toMatchObject({ |
| 60 | + inputPrice: 2, |
| 61 | + outputPrice: 10, |
| 62 | + cacheReadsPrice: 0.1, |
| 63 | + cacheWritesPrice: 2.5, |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + it("routes Codex tool requests through Responses Lite with Fast mode and required reasoning", async () => { |
| 68 | + const handler = new OpenAiCodexHandler({ |
| 69 | + apiModelId: "gpt-6.1-terra", |
| 70 | + reasoningEffort: "none", |
| 71 | + openAiCodexServiceTier: "priority", |
| 72 | + }) |
| 73 | + vitest.spyOn(openAiCodexOAuthManager, "getAccessToken").mockResolvedValue("test-token") |
| 74 | + vitest.spyOn(openAiCodexOAuthManager, "getAccountId").mockResolvedValue("test-account") |
| 75 | + const create = vitest.fn().mockResolvedValue(asyncStreamFrom([])) |
| 76 | + Reflect.set(handler, "client", { responses: { create } }) |
| 77 | + await collectStream( |
| 78 | + handler.createMessage("System", [{ role: "user", content: "Read the file" }], { |
| 79 | + taskId: "terra-task", |
| 80 | + tools: [ |
| 81 | + { |
| 82 | + type: "function", |
| 83 | + function: { name: "read_file", parameters: { type: "object", properties: {} } }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }), |
| 87 | + ) |
| 88 | + expect(create).toHaveBeenCalledWith( |
| 89 | + expect.objectContaining({ |
| 90 | + model: "gpt-6.1-terra", |
| 91 | + service_tier: "priority", |
| 92 | + prompt_cache_key: "terra-task", |
| 93 | + reasoning: { effort: "low", summary: "auto", context: "all_turns" }, |
| 94 | + tool_choice: "auto", |
| 95 | + parallel_tool_calls: false, |
| 96 | + input: expect.arrayContaining([ |
| 97 | + expect.objectContaining({ |
| 98 | + type: "additional_tools", |
| 99 | + tools: [expect.objectContaining({ type: "function", name: "read_file" })], |
| 100 | + }), |
| 101 | + ]), |
| 102 | + }), |
| 103 | + expect.objectContaining({ |
| 104 | + headers: expect.objectContaining({ |
| 105 | + "x-openai-internal-codex-responses-lite": "true", |
| 106 | + "session-id": "terra-task", |
| 107 | + }), |
| 108 | + }), |
| 109 | + ) |
| 110 | + const body = create.mock.calls[0][0] |
| 111 | + expect(body).not.toHaveProperty("instructions") |
| 112 | + expect(body).not.toHaveProperty("tools") |
| 113 | + expect(body).not.toHaveProperty("max_output_tokens") |
| 114 | + expect(body).not.toHaveProperty("temperature") |
| 115 | + }) |
| 116 | +}) |
0 commit comments