Skip to content

Commit 9a4aeb6

Browse files
committed
feat(provider): add Chat Completions API mode for gateway/proxy providers
When a custom provider sets `"api": "chat"` in its config, OpenCode now uses the Chat Completions API (`/chat/completions`) instead of the default Responses API (`/responses`). This enables GPT-5.x models to work through OpenAI-compatible gateways and proxies (including AWS Bedrock) that only implement the Chat Completions endpoint. Additionally fixes two compatibility issues with gateway/proxy deployments: 1. Renames `max_tokens` → `max_completion_tokens` in outgoing requests when `api: "chat"` is set. GPT-5.x models reject the legacy `max_tokens` parameter, and `@ai-sdk/openai-compatible` only sends `max_tokens` for unrecognized model IDs (e.g. custom gateway model names). 2. Inlines JSON Schema `$ref` references in tool parameter definitions. AWS Bedrock's OpenAI-compatible layer crashes with a 500 when tool schemas contain `$ref`/`$defs` — a common pattern in MCP tool outputs. Usage: ```json { "provider": { "my-gateway": { "npm": "@ai-sdk/openai-compatible", "api": "chat", "options": { "baseURL": "https://my-gateway.example.com/v1", "apiKey": "{env:MY_KEY}" }, "models": { "gpt-5": { "name": "GPT-5", "tool_call": true } } } } } ``` Closes anomalyco#7793 Related: anomalyco#22623, anomalyco#20622
1 parent 65ba1f6 commit 9a4aeb6

2 files changed

Lines changed: 215 additions & 3 deletions

File tree

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,47 @@ function useLanguageModel(sdk: any) {
138138
return sdk.responses === undefined && sdk.chat === undefined
139139
}
140140

141+
/**
142+
* Resolve a language model from the SDK using the configured API mode.
143+
*
144+
* When a provider config sets `"api": "chat"`, this calls `sdk.chat(modelID)`
145+
* which forces the Chat Completions API endpoint (`/chat/completions`) and
146+
* correct parameter naming (`max_completion_tokens`) for GPT-5.x models.
147+
*
148+
* When `"api": "responses"`, uses the Responses API explicitly.
149+
* Otherwise, falls back to `sdk.languageModel()` (default SDK behavior).
150+
*/
151+
function resolveLanguageModel(sdk: any, modelID: string, apiMode?: string) {
152+
if (apiMode === "chat" && typeof sdk.chat === "function") {
153+
return sdk.chat(modelID)
154+
}
155+
if (apiMode === "responses" && typeof sdk.responses === "function") {
156+
return sdk.responses(modelID)
157+
}
158+
return sdk.languageModel(modelID)
159+
}
160+
161+
/**
162+
* Inline JSON Schema `$ref` references so that tool parameter schemas are
163+
* self-contained. Some providers (e.g. GPT-5.x via Bedrock) crash on `$ref`.
164+
*/
165+
function resolveRefs(obj: any, defs: Record<string, any>): void {
166+
if (!obj || typeof obj !== "object") return
167+
for (const key of Object.keys(obj)) {
168+
const val = obj[key]
169+
if (key === "$ref" && typeof val === "string") {
170+
const name = val.replace(/^#\/\$defs\//, "")
171+
const resolved = defs[name]
172+
if (resolved) {
173+
delete obj["$ref"]
174+
Object.assign(obj, JSON.parse(JSON.stringify(resolved)))
175+
}
176+
} else if (typeof val === "object" && val !== null) {
177+
resolveRefs(val, defs)
178+
}
179+
}
180+
}
181+
141182
function custom(dep: CustomDep): Record<string, CustomLoader> {
142183
return {
143184
anthropic: () =>
@@ -1124,11 +1165,19 @@ const layer: Layer.Layer<
11241165
// extend database from config
11251166
for (const [providerID, provider] of configProviders) {
11261167
const existing = database[providerID]
1168+
const configOptions = mergeDeep(existing?.options ?? {}, provider.options ?? {})
1169+
// When provider.api is a known SDK method name (e.g. "chat", "responses"),
1170+
// store it as apiMode so getLanguage can call sdk.chat() / sdk.responses()
1171+
// instead of the default sdk.languageModel().
1172+
const API_MODES = ["chat", "responses"]
1173+
if (provider.api && API_MODES.includes(provider.api)) {
1174+
configOptions["apiMode"] = provider.api
1175+
}
11271176
const parsed: Info = {
11281177
id: ProviderID.make(providerID),
11291178
name: provider.name ?? existing?.name ?? providerID,
11301179
env: provider.env ?? existing?.env ?? [],
1131-
options: mergeDeep(existing?.options ?? {}, provider.options ?? {}),
1180+
options: configOptions,
11321181
source: "config",
11331182
models: existing?.models ?? {},
11341183
}
@@ -1152,7 +1201,12 @@ const layer: Layer.Layer<
11521201
api: {
11531202
id: apiID,
11541203
npm: apiNpm,
1155-
url: model.provider?.api ?? provider?.api ?? existingModel?.api.url ?? modelsDev[providerID]?.api ?? "",
1204+
url: iife(() => {
1205+
// Skip provider.api if it's a mode selector (e.g. "chat", "responses")
1206+
// rather than a URL — the mode is already captured in options.apiMode.
1207+
const providerApiUrl = provider?.api && !API_MODES.includes(provider.api) ? provider.api : undefined
1208+
return model.provider?.api ?? providerApiUrl ?? existingModel?.api.url ?? modelsDev[providerID]?.api ?? ""
1209+
}),
11561210
},
11571211
status: model.status ?? existingModel?.status ?? "active",
11581212
name,
@@ -1480,6 +1534,34 @@ const layer: Layer.Layer<
14801534
}
14811535
}
14821536

1537+
// When apiMode is "chat", fix request body for gateway/proxy compatibility:
1538+
// 1. Rename max_tokens → max_completion_tokens (GPT-5.x rejects max_tokens)
1539+
// 2. Resolve $ref in tool parameter schemas (Bedrock/proxies don't support $ref)
1540+
if (options["apiMode"] === "chat" && opts.body && opts.method === "POST") {
1541+
const body = JSON.parse(opts.body as string)
1542+
let changed = false
1543+
1544+
if ("max_tokens" in body && !("max_completion_tokens" in body)) {
1545+
body.max_completion_tokens = body.max_tokens
1546+
delete body.max_tokens
1547+
changed = true
1548+
}
1549+
1550+
if (Array.isArray(body.tools)) {
1551+
for (const tool of body.tools) {
1552+
const params = tool?.function?.parameters
1553+
if (params && params["$defs"]) {
1554+
resolveRefs(params, params["$defs"])
1555+
delete params["$defs"]
1556+
delete params["defs"]
1557+
changed = true
1558+
}
1559+
}
1560+
}
1561+
1562+
if (changed) opts.body = JSON.stringify(body)
1563+
}
1564+
14831565
const res = await fetchFn(input, {
14841566
...opts,
14851567
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682
@@ -1570,7 +1652,7 @@ const layer: Layer.Layer<
15701652
...provider.options,
15711653
...model.options,
15721654
})
1573-
: sdk.languageModel(model.api.id)
1655+
: resolveLanguageModel(sdk, model.api.id, provider.options?.["apiMode"])
15741656
s.models.set(key, language)
15751657
return language
15761658
} catch (e) {

‎packages/opencode/test/provider/provider.test.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,3 +2712,133 @@ test("opencode loader keeps paid models when auth exists", async () => {
27122712
}
27132713
}
27142714
})
2715+
2716+
test("api: 'chat' sets apiMode in provider options and does not corrupt model URL", async () => {
2717+
await using tmp = await tmpdir({
2718+
init: async (dir) => {
2719+
await Bun.write(
2720+
path.join(dir, "opencode.json"),
2721+
JSON.stringify({
2722+
$schema: "https://opencode.ai/config.json",
2723+
provider: {
2724+
"my-gateway": {
2725+
name: "My Gateway",
2726+
npm: "@ai-sdk/openai",
2727+
api: "chat",
2728+
env: [],
2729+
models: {
2730+
"gpt-5": {
2731+
name: "GPT-5",
2732+
tool_call: true,
2733+
limit: { context: 128000, output: 16000 },
2734+
},
2735+
},
2736+
options: {
2737+
apiKey: "test-key",
2738+
baseURL: "https://my-gateway.example.com/v1",
2739+
},
2740+
},
2741+
},
2742+
}),
2743+
)
2744+
},
2745+
})
2746+
await Instance.provide({
2747+
directory: tmp.path,
2748+
fn: async () => {
2749+
const providers = await list()
2750+
const provider = providers[ProviderID.make("my-gateway")]
2751+
expect(provider).toBeDefined()
2752+
// apiMode should be stored in provider options
2753+
expect(provider.options.apiMode).toBe("chat")
2754+
// model api.url should NOT be "chat" — it should fall back to empty string
2755+
const model = provider.models["gpt-5"]
2756+
expect(model.api.url).not.toBe("chat")
2757+
// baseURL in options should be preserved
2758+
expect(provider.options.baseURL).toBe("https://my-gateway.example.com/v1")
2759+
},
2760+
})
2761+
})
2762+
2763+
test("api: 'responses' sets apiMode in provider options", async () => {
2764+
await using tmp = await tmpdir({
2765+
init: async (dir) => {
2766+
await Bun.write(
2767+
path.join(dir, "opencode.json"),
2768+
JSON.stringify({
2769+
$schema: "https://opencode.ai/config.json",
2770+
provider: {
2771+
"my-openai": {
2772+
name: "My OpenAI",
2773+
npm: "@ai-sdk/openai",
2774+
api: "responses",
2775+
env: [],
2776+
models: {
2777+
"gpt-5": {
2778+
name: "GPT-5",
2779+
tool_call: true,
2780+
limit: { context: 128000, output: 16000 },
2781+
},
2782+
},
2783+
options: {
2784+
apiKey: "test-key",
2785+
},
2786+
},
2787+
},
2788+
}),
2789+
)
2790+
},
2791+
})
2792+
await Instance.provide({
2793+
directory: tmp.path,
2794+
fn: async () => {
2795+
const providers = await list()
2796+
const provider = providers[ProviderID.make("my-openai")]
2797+
expect(provider).toBeDefined()
2798+
expect(provider.options.apiMode).toBe("responses")
2799+
},
2800+
})
2801+
})
2802+
2803+
test("api URL string (not a mode) still flows into model.api.url normally", async () => {
2804+
await using tmp = await tmpdir({
2805+
init: async (dir) => {
2806+
await Bun.write(
2807+
path.join(dir, "opencode.json"),
2808+
JSON.stringify({
2809+
$schema: "https://opencode.ai/config.json",
2810+
provider: {
2811+
"url-api": {
2812+
name: "URL API",
2813+
npm: "@ai-sdk/openai-compatible",
2814+
api: "https://api.custom-gateway.com/v1",
2815+
env: [],
2816+
models: {
2817+
"model-1": {
2818+
name: "Model 1",
2819+
tool_call: true,
2820+
limit: { context: 8000, output: 2000 },
2821+
},
2822+
},
2823+
options: {
2824+
apiKey: "test-key",
2825+
},
2826+
},
2827+
},
2828+
}),
2829+
)
2830+
},
2831+
})
2832+
await Instance.provide({
2833+
directory: tmp.path,
2834+
fn: async () => {
2835+
const providers = await list()
2836+
const provider = providers[ProviderID.make("url-api")]
2837+
expect(provider).toBeDefined()
2838+
// api field with a URL should NOT set apiMode
2839+
expect(provider.options.apiMode).toBeUndefined()
2840+
// URL should flow into model.api.url as before
2841+
expect(provider.models["model-1"].api.url).toBe("https://api.custom-gateway.com/v1")
2842+
},
2843+
})
2844+
})

0 commit comments

Comments
 (0)