Skip to content

Commit a758bfc

Browse files
committed
feat(providers): allow a per-model wire override on mixed gateways (lidge-jun#404)
A gateway can front models that do not agree on a wire. Grok needs the Responses API for hosted web_search; a sibling model on the same provider is fine on chat completions. Since the adapter was a provider-wide setting, one of them always lost — and the way it lost was quiet. The responses parser drops hosted tools when translating to chat, so the request went out without web_search, Grok answered with reasoning and no message, and the bridge closed that as a normal completed turn. An empty answer, no error anywhere. providers.<name>.modelAdapters maps an upstream native model id to the wire it should use. Absent means the provider-wide adapter applies to everything, exactly as before. Only openai-chat and openai-responses may be selected. The adapter registry also holds cursor, kiro, and google, and those are not interchangeable wires: a cursor override would send the caller's Authorization header to this provider's base URL. For the same reason the canonical ChatGPT forward provider refuses overrides outright — the chat adapter only ever sends provider.apiKey, so switching its wire would drop the forwarded credential. Hard wire pins still win. The pin check is deliberately phrased without looking at the current adapter, because the resolver runs twice per request; a check like "pin differs from the current adapter" would pass on the way in and then let the override displace the pin on the second pass. The pin map moved to types.ts so the config validator can consult the same predicate the resolver uses, instead of keeping a second copy that drifts. The validator rejects what the resolver would ignore — a disallowed wire, a pinned model, an override on the forward provider — rather than accepting config the user believes is in effect. Verified: typecheck clean, full suite 4132 pass with the same 2 pre-existing GUI module-resolution failures, privacy scan clean. The end-to-end tests serve both wires from one mock upstream and assert which path each model actually took.
1 parent aeff2df commit a758bfc

13 files changed

Lines changed: 493 additions & 11 deletions

‎docs-site/src/content/docs/ko/reference/configuration.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ token 대신 쓸 수 있습니다. 모든 후보는 timing side channel을 막
155155
| `reasoningEfforts?` | `string[]` | 알리고 전송할 프로바이더 단위 Codex reasoning 레이블(`low`, `medium`, `high`, `xhigh`, `max`, `ultra`). |
156156
| `modelReasoningEfforts?` | `Record<string,string[]>` | 모델별 reasoning 레이블. 빈 배열은 해당 모델의 effort control을 숨깁니다. |
157157
| `modelSupportsReasoningSummaries?` | `Record<string,boolean>` | 모델별 reasoning summary capability. 모델 값을 `false`로 두면 summary 지원을 알리지 않고 `openai-responses` 요청 전에 summary-delivery 필드를 제거합니다. |
158+
| `modelAdapters?` | `Record<string,string>` | 여러 wire를 쓰는 모델이 한 게이트웨이에 섞여 있을 때의 모델별 wire 지정. 키는 upstream native 모델 ID이고 값은 `openai-chat` 또는 `openai-responses`만 허용합니다. `web_search` 같은 hosted tool 때문에 한 모델만 Responses API가 필요할 때 씁니다. upstream이 wire를 고정한 모델과 canonical ChatGPT forward provider에서는 override가 거부됩니다. |
158159
| `reasoningEffortMap?` | `Record<string,string>` | 프로바이더 단위 reasoning 레이블 wire alias. 업스트림이 다른 값을 요구할 때만 사용합니다. |
159160
| `modelReasoningEffortMap?` | `Record<string,Record<string,string>>` | 모델별 reasoning 레이블 wire alias. |
160161
| `noReasoningModels?` | `string[]` | reasoning/thinking 파라미터를 거부하는 모델. 어댑터가 `reasoning_effort`를 제거합니다. |

‎docs-site/src/content/docs/reference/configuration.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ network. Only do this on trusted networks, and always set a strong `OPENCODEX_AP
170170
| `reasoningEfforts?` | `string[]` | Provider-wide Codex reasoning labels to advertise and send (`low`, `medium`, `high`, `xhigh`, `max`, `ultra`). |
171171
| `modelReasoningEfforts?` | `Record<string,string[]>` | Model-specific reasoning labels. An empty list hides the effort control for that model. |
172172
| `modelSupportsReasoningSummaries?` | `Record<string,boolean>` | Model-specific reasoning-summary capability. Set a model to `false` to stop advertising summaries and strip summary-delivery fields before an `openai-responses` request. |
173+
| `modelAdapters?` | `Record<string,string>` | Per-model wire override for a gateway that fronts models speaking different wires. Keys are upstream native model ids; values must be `openai-chat` or `openai-responses`. Useful when one model needs the Responses API for hosted tools such as `web_search` while its siblings are fine on chat completions. Models the upstream pins to a single wire, and the canonical ChatGPT forward provider, reject overrides. |
173174
| `reasoningEffortMap?` | `Record<string,string>` | Provider-wide wire aliases for reasoning labels. Use only when the upstream expects a different value. |
174175
| `modelReasoningEffortMap?` | `Record<string,Record<string,string>>` | Model-specific wire aliases for reasoning labels. |
175176
| `noReasoningModels?` | `string[]` | Models that reject a reasoning/thinking param — the adapter drops `reasoning_effort` for them. |

‎src/config.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import { comboConfigIssues } from "./combos/types";
77
import { hardenSecretDir, hardenSecretPath } from "./lib/windows-secret-acl";
88
import { providerDestinationConfigError } from "./lib/destination-policy";
99
import { openRouterRoutingConfigError } from "./providers/openrouter-routing";
10-
import { OPENAI_PROVIDER_TIER_VERSION, type OcxConfig } from "./types";
10+
import {
11+
isWirePinnedModel,
12+
MODEL_ADAPTER_OVERRIDE_ALLOWED,
13+
OPENAI_PROVIDER_TIER_VERSION,
14+
type OcxConfig,
15+
type OcxProviderConfig,
16+
} from "./types";
17+
import { isCanonicalOpenAiForwardProvider } from "./providers/openai-tiers";
1118

1219
let _atomicSeq = 0;
1320

@@ -434,6 +441,41 @@ export function booleanRecordConfigError(value: unknown, field: string): string
434441
return null;
435442
}
436443

444+
/**
445+
* Validate a provider's per-model wire override map (#404).
446+
*
447+
* Rejects, rather than silently ignoring, configurations the resolver would refuse:
448+
* a value outside the allowed wires, a model the upstream pins to one wire, and any
449+
* override on a canonical forward provider (where switching wires would drop the
450+
* caller's forwarded credential). Silently dropping them would leave the user
451+
* believing an override is in effect.
452+
*/
453+
export function modelAdapterRecordConfigError(
454+
value: unknown,
455+
field: string,
456+
providerName: string,
457+
provider: { adapter?: unknown; authMode?: unknown; baseUrl?: unknown },
458+
): string | null {
459+
if (value === undefined) return null;
460+
if (!value || typeof value !== "object" || Array.isArray(value)) return `${field} must be a plain object`;
461+
const prototype = Object.getPrototypeOf(value);
462+
if (prototype !== Object.prototype && prototype !== null) return `${field} must be a plain object with own properties`;
463+
const entries = Object.entries(value);
464+
if (entries.length > 0 && isCanonicalOpenAiForwardProvider(provider as OcxProviderConfig)) {
465+
return `${field} is not supported on the canonical ChatGPT forward provider`;
466+
}
467+
for (const [key, entry] of entries) {
468+
if (!key.trim()) return `${field} keys must be nonblank model ids`;
469+
if (typeof entry !== "string" || !MODEL_ADAPTER_OVERRIDE_ALLOWED.has(entry)) {
470+
return `${field}.${key} must be one of: ${[...MODEL_ADAPTER_OVERRIDE_ALLOWED].join(", ")}`;
471+
}
472+
if (isWirePinnedModel(providerName, key.trim())) {
473+
return `${field}.${key} cannot be overridden: the upstream only speaks one wire for this model`;
474+
}
475+
}
476+
return null;
477+
}
478+
437479
const configSchema = z.object({
438480
port: z.number().int().min(0).max(65535).default(10100),
439481
providers: z.record(z.string(), providerConfigSchema),
@@ -511,6 +553,19 @@ const configSchema = z.object({
511553
message: headersError,
512554
});
513555
}
556+
const modelAdaptersError = modelAdapterRecordConfigError(
557+
(provider as { modelAdapters?: unknown }).modelAdapters,
558+
"modelAdapters",
559+
name,
560+
provider,
561+
);
562+
if (modelAdaptersError) {
563+
ctx.addIssue({
564+
code: "custom",
565+
path: ["providers", name, "modelAdapters"],
566+
message: modelAdaptersError,
567+
});
568+
}
514569
const maxInputError = positiveIntegerRecordConfigError(
515570
(provider as { modelMaxInputTokens?: unknown }).modelMaxInputTokens,
516571
"modelMaxInputTokens",

‎src/server/adapter-resolve.ts‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,32 @@ import { createMimoFreeAdapter } from "../adapters/mimo-free";
77
import { createOpenAIChatAdapter } from "../adapters/openai-chat";
88
import { createResponsesPassthroughAdapter } from "../adapters/openai-responses";
99
import type { OcxProviderConfig } from "../types";
10+
import { isWirePinnedModel, MODEL_ADAPTER_OVERRIDE_ALLOWED, pinnedWireAdapter } from "../types";
11+
import { isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers";
1012

11-
/** Providers whose listed model ids must be driven over the Anthropic wire even if the provider's
12-
* configured adapter is something else (the upstream only speaks Anthropic for these models). */
13-
const ANTHROPIC_WIRE_MODELS: Record<string, Set<string>> = {
14-
"opencode-go": new Set(["minimax-m2.5", "minimax-m2.7", "minimax-m3"]),
15-
};
16-
17-
/** Return a provider config whose adapter is forced to "anthropic" when the model id is wire-pinned. */
13+
/**
14+
* Resolve the wire a single model should use: a hard pin first, then a configured
15+
* per-model override, then the provider's own adapter.
16+
*
17+
* Safe to call more than once on its own output — the pin check does not look at the
18+
* current adapter, so a second pass cannot let an override displace a pin.
19+
*/
1820
export function resolveWireProtocolOverride(providerName: string, modelId: string, providerConfig: OcxProviderConfig): OcxProviderConfig {
19-
const overrideSet = ANTHROPIC_WIRE_MODELS[providerName];
20-
if (overrideSet?.has(modelId) && providerConfig.adapter !== "anthropic") {
21-
return { ...providerConfig, adapter: "anthropic" };
21+
const pinned = pinnedWireAdapter(providerName, modelId);
22+
if (pinned && providerConfig.adapter !== pinned) {
23+
return { ...providerConfig, adapter: pinned };
24+
}
25+
// Re-check the allow-list here, not just in the config validator: the file may have
26+
// been hand-edited, or written by a build that allowed more values.
27+
const requested = providerConfig.modelAdapters?.[modelId];
28+
if (requested
29+
&& MODEL_ADAPTER_OVERRIDE_ALLOWED.has(requested)
30+
&& requested !== providerConfig.adapter
31+
&& !isWirePinnedModel(providerName, modelId)
32+
// A forward provider hands the caller's own credential upstream; the chat adapter
33+
// only ever sends provider.apiKey, so switching wires here would drop the auth.
34+
&& !isCanonicalOpenAiForwardProvider(providerConfig)) {
35+
return { ...providerConfig, adapter: requested };
2236
}
2337
return providerConfig;
2438
}

‎src/server/auth-cors.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { timingSafeEqual } from "node:crypto";
22
import { formatErrorResponse } from "../bridge";
33
import {
44
booleanRecordConfigError,
5+
modelAdapterRecordConfigError,
56
codexAutoStartEnabled,
67
positiveIntegerConfigError,
78
positiveIntegerRecordConfigError,
@@ -234,6 +235,8 @@ export function providerManagementConfigError(name: unknown, provider: unknown):
234235
if (maxInputError) return `provider ${name} ${maxInputError}`;
235236
const reasoningSummariesError = booleanRecordConfigError(raw.modelSupportsReasoningSummaries, "modelSupportsReasoningSummaries");
236237
if (reasoningSummariesError) return `provider ${name} ${reasoningSummariesError}`;
238+
const modelAdaptersError = modelAdapterRecordConfigError(raw.modelAdapters, "modelAdapters", name, typed);
239+
if (modelAdaptersError) return `provider ${name} ${modelAdaptersError}`;
237240
const defaultMaxOutputError = positiveIntegerConfigError(raw.defaultMaxOutputTokens, "defaultMaxOutputTokens");
238241
if (defaultMaxOutputError) return `provider ${name} ${defaultMaxOutputError}`;
239242
const maxOutputError = positiveIntegerRecordConfigError(raw.modelMaxOutputTokens, "modelMaxOutputTokens");

‎src/server/chat-completions.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "../chat/outbound";
1717
import { estimateTokens } from "../lib/token-estimate";
1818
import { routeModel } from "../router";
19+
import { resolveWireProtocolOverride } from "./adapter-resolve";
1920
import type { OcxConfig } from "../types";
2021
import { readJsonRequestBody } from "./request-decompress";
2122
import {
@@ -68,6 +69,9 @@ export async function handleChatCompletions(
6869
let directRoute = false;
6970
try {
7071
const route = routeModel(config, internalBody.model as string);
72+
// Settle the wire once so every branch below reads the adapter this model will
73+
// actually use, not the provider-wide default (#404).
74+
route.provider = resolveWireProtocolOverride(route.providerName, route.modelId, route.provider);
7175
logCtx.model = route.modelId;
7276
logCtx.providerAdapter = route.provider.adapter;
7377
logCtx.requestedModel = requestedModel;

‎src/server/claude-messages.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { clearableDeadline, idleDeadline } from "../lib/abort";
2424
import { estimateTokens } from "../lib/token-estimate";
2525
import { routeModel } from "../router";
26+
import { resolveWireProtocolOverride } from "./adapter-resolve";
2627
import type { OcxConfig } from "../types";
2728
import { readJsonRequestBody } from "./request-decompress";
2829
import { addFinalRequestLog, httpStatusForTerminalStatus, recordFirstOutput, type RequestLogContext, type RequestLogEntry } from "./request-log";
@@ -570,6 +571,9 @@ export async function handleClaudeMessages(
570571
let nativeRoute = false;
571572
try {
572573
const route = routeModel(config, internalBody.model as string);
574+
// Settle the wire once so the sampling decision below reads the effective
575+
// adapter rather than the provider-wide default (#404).
576+
route.provider = resolveWireProtocolOverride(route.providerName, route.modelId, route.provider);
573577
if (route.provider.adapter === "openai-responses") {
574578
nativeRoute = true;
575579
delete internalBody.max_output_tokens;

‎src/server/responses/core.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ export async function handleResponses(
732732
}
733733
parsed.modelId = route.modelId;
734734
}
735+
// Settle the wire once, right after the native model id is known, so logging,
736+
// fast-mode injection, auth, and sidecar decisions all read the adapter this
737+
// request will actually use rather than the provider-wide default (#404).
738+
route.provider = resolveWireProtocolOverride(route.providerName, route.modelId, route.provider);
735739
logCtx.model = route.modelId;
736740
logCtx.provider = route.providerName;
737741
logCtx.providerAdapter = route.provider.adapter;

‎src/types.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,17 @@ export interface ResponsesItemIdRepairConfig {
720720

721721
export interface OcxProviderConfig {
722722
adapter: string;
723+
/**
724+
* Per-model wire override, keyed by the upstream native model id (after namespace
725+
* and combo resolution). A single gateway can front models that speak different
726+
* wires — Grok needs the Responses API for hosted web_search while a sibling model
727+
* is fine on chat completions (#404).
728+
*
729+
* Only OpenAI-shaped wires may be selected; see MODEL_ADAPTER_OVERRIDE_ALLOWED.
730+
* Absent or empty means the provider-wide `adapter` applies to everything, exactly
731+
* as before.
732+
*/
733+
modelAdapters?: Record<string, string>;
723734
baseUrl: string;
724735
/**
725736
* Optional relative resource path for key-auth openai-responses requests. Must start with `/`
@@ -932,6 +943,45 @@ export type CodexAccountMode = "direct" | "pool";
932943

933944
export const OPENAI_PROVIDER_TIER_VERSION = 2 as const;
934945

946+
/**
947+
* Wires that a per-model `modelAdapters` override may select.
948+
*
949+
* Deliberately narrow: provider-specific adapters (cursor, kiro, google, ...) carry
950+
* their own credential and base-URL semantics, so exposing them here would widen the
951+
* auth boundary rather than pick a wire. Widening this set needs a per-adapter
952+
* credential threat model first (#404).
953+
*/
954+
export const MODEL_ADAPTER_OVERRIDE_ALLOWED: ReadonlySet<string> = new Set([
955+
"openai-chat",
956+
"openai-responses",
957+
]);
958+
959+
/**
960+
* Providers whose listed model ids must be driven over the Anthropic wire even when
961+
* the provider's configured adapter says otherwise — the upstream only speaks
962+
* Anthropic for these models.
963+
*/
964+
const ANTHROPIC_WIRE_MODELS: Record<string, ReadonlySet<string>> = {
965+
"opencode-go": new Set(["minimax-m2.5", "minimax-m2.7", "minimax-m3"]),
966+
};
967+
968+
/**
969+
* True when the upstream speaks exactly one wire for this model, so a configured
970+
* override must not apply.
971+
*
972+
* Deliberately independent of the provider's current adapter: the wire resolver runs
973+
* more than once per request, and a check phrased as "pin differs from the current
974+
* adapter" would pass on the first pass and then let the override win on the second.
975+
*/
976+
export function isWirePinnedModel(providerName: string, modelId: string): boolean {
977+
return ANTHROPIC_WIRE_MODELS[providerName]?.has(modelId) ?? false;
978+
}
979+
980+
/** The wire a pinned model must use, or undefined when the model is not pinned. */
981+
export function pinnedWireAdapter(providerName: string, modelId: string): string | undefined {
982+
return isWirePinnedModel(providerName, modelId) ? "anthropic" : undefined;
983+
}
984+
935985
export interface CodexAccount {
936986
id: string;
937987
email: string;

‎tests/adapter-resolve.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)