|
| 1 | +/** |
| 2 | + * Issue #404: one OpenAI-compatible gateway can front models that speak different |
| 3 | + * wires. Grok needs the Responses API for hosted web_search; a sibling model on the |
| 4 | + * same provider is fine on chat completions. Without a per-model override the |
| 5 | + * provider-wide adapter wins and the hosted tool is dropped on the way out. |
| 6 | + */ |
| 7 | +import { describe, expect, test } from "bun:test"; |
| 8 | +import { resolveWireProtocolOverride } from "../src/server/adapter-resolve"; |
| 9 | +import type { OcxProviderConfig } from "../src/types"; |
| 10 | + |
| 11 | +function gateway(overrides: Partial<OcxProviderConfig> = {}): OcxProviderConfig { |
| 12 | + return { |
| 13 | + adapter: "openai-chat", |
| 14 | + baseUrl: "https://gateway.example/v1", |
| 15 | + authMode: "key", |
| 16 | + apiKey: "test-key", |
| 17 | + ...overrides, |
| 18 | + } as OcxProviderConfig; |
| 19 | +} |
| 20 | + |
| 21 | +describe("per-model wire override (#404)", () => { |
| 22 | + test("selects the responses wire for the configured model only", () => { |
| 23 | + const provider = gateway({ modelAdapters: { "grok-4.5": "openai-responses" } }); |
| 24 | + |
| 25 | + expect(resolveWireProtocolOverride("localmodels", "grok-4.5", provider).adapter) |
| 26 | + .toBe("openai-responses"); |
| 27 | + // A sibling model on the same provider keeps the provider default. |
| 28 | + expect(resolveWireProtocolOverride("localmodels", "gemini-3-pro", provider).adapter) |
| 29 | + .toBe("openai-chat"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("a provider without the field behaves exactly as before", () => { |
| 33 | + expect(resolveWireProtocolOverride("localmodels", "grok-4.5", gateway()).adapter) |
| 34 | + .toBe("openai-chat"); |
| 35 | + }); |
| 36 | + |
| 37 | + test("does not mutate the provider it was given", () => { |
| 38 | + const provider = gateway({ modelAdapters: { "grok-4.5": "openai-responses" } }); |
| 39 | + const resolved = resolveWireProtocolOverride("localmodels", "grok-4.5", provider); |
| 40 | + |
| 41 | + expect(provider.adapter).toBe("openai-chat"); |
| 42 | + // Credentials and destination must survive a wire swap untouched. |
| 43 | + expect(resolved.apiKey).toBe("test-key"); |
| 44 | + expect(resolved.authMode).toBe("key"); |
| 45 | + expect(resolved.baseUrl).toBe("https://gateway.example/v1"); |
| 46 | + }); |
| 47 | + |
| 48 | + test("a hard-pinned model ignores the override", () => { |
| 49 | + const provider = gateway({ |
| 50 | + adapter: "openai-chat", |
| 51 | + modelAdapters: { "minimax-m3": "openai-chat" }, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(resolveWireProtocolOverride("opencode-go", "minimax-m3", provider).adapter) |
| 55 | + .toBe("anthropic"); |
| 56 | + }); |
| 57 | + |
| 58 | + test("a pinned model survives a second resolve pass", () => { |
| 59 | + // The resolver runs twice per request (route time and adapter build). A pin check |
| 60 | + // phrased against the current adapter would pass the first time and then let the |
| 61 | + // override win on the second. |
| 62 | + const provider = gateway({ modelAdapters: { "minimax-m3": "openai-chat" } }); |
| 63 | + const once = resolveWireProtocolOverride("opencode-go", "minimax-m3", provider); |
| 64 | + const twice = resolveWireProtocolOverride("opencode-go", "minimax-m3", once); |
| 65 | + |
| 66 | + expect(once.adapter).toBe("anthropic"); |
| 67 | + expect(twice.adapter).toBe("anthropic"); |
| 68 | + }); |
| 69 | + |
| 70 | + test("values outside the allowed wires are ignored at resolve time", () => { |
| 71 | + // Hand-edited config, or one written by a build that allowed more values. |
| 72 | + for (const disallowed of ["cursor", "kiro", "google", "anthropic"]) { |
| 73 | + const provider = gateway({ modelAdapters: { "grok-4.5": disallowed } }); |
| 74 | + expect(resolveWireProtocolOverride("localmodels", "grok-4.5", provider).adapter) |
| 75 | + .toBe("openai-chat"); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + test("a canonical forward provider never takes an override", () => { |
| 80 | + // The chat adapter only sends provider.apiKey, so switching wires here would drop |
| 81 | + // the caller's forwarded credential entirely. |
| 82 | + const forward = gateway({ |
| 83 | + adapter: "openai-responses", |
| 84 | + authMode: "forward", |
| 85 | + baseUrl: "https://chatgpt.com/backend-api/codex", |
| 86 | + modelAdapters: { "gpt-5.5": "openai-chat" }, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(resolveWireProtocolOverride("openai", "gpt-5.5", forward).adapter) |
| 90 | + .toBe("openai-responses"); |
| 91 | + }); |
| 92 | +}); |
0 commit comments