Skip to content

Commit c10b269

Browse files
committed
fix(provider): apply config headers to fetch requests
Fixes anomalyco#11789 The custom fetch function in getSDK was not merging options["headers"] (which contains provider/model headers from config) into the actual fetch request headers. Solution: Merge options["headers"] into opts.headers before calling fetch. Changes: - packages/opencode/src/provider/provider.ts: merge headers in custom fetch - packages/opencode/test/provider/provider.test.ts: add tests for header config
1 parent 94baf1f commit c10b269

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,11 @@ export namespace Provider {
10201020
}
10211021
}
10221022

1023+
opts.headers = {
1024+
...(typeof opts.headers === "object" ? opts.headers : {}),
1025+
...options["headers"],
1026+
}
1027+
10231028
return fetchFn(input, {
10241029
...opts,
10251030
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,3 +2148,78 @@ test("custom model with variants enabled and disabled", async () => {
21482148
},
21492149
})
21502150
})
2151+
2152+
test("provider headers from config are applied to fetch requests", async () => {
2153+
let capturedHeaders: Record<string, string> | undefined
2154+
2155+
await using tmp = await tmpdir({
2156+
init: async (dir) => {
2157+
await Bun.write(
2158+
path.join(dir, "opencode.json"),
2159+
JSON.stringify({
2160+
$schema: "https://opencode.ai/config.json",
2161+
provider: {
2162+
anthropic: {
2163+
options: {
2164+
headers: {
2165+
"X-Custom-Header": "custom-value",
2166+
"User-Agent": "MyCustomAgent/1.0",
2167+
},
2168+
},
2169+
},
2170+
},
2171+
}),
2172+
)
2173+
},
2174+
})
2175+
await Instance.provide({
2176+
directory: tmp.path,
2177+
init: async () => {
2178+
Env.set("ANTHROPIC_API_KEY", "test-api-key")
2179+
},
2180+
fn: async () => {
2181+
const providers = await Provider.list()
2182+
expect(providers["anthropic"]).toBeDefined()
2183+
// Verify headers are in provider options
2184+
expect(providers["anthropic"].options.headers).toBeDefined()
2185+
expect(providers["anthropic"].options.headers["X-Custom-Header"]).toBe("custom-value")
2186+
expect(providers["anthropic"].options.headers["User-Agent"]).toBe("MyCustomAgent/1.0")
2187+
},
2188+
})
2189+
})
2190+
2191+
test("model headers from config are merged with provider headers", async () => {
2192+
await using tmp = await tmpdir({
2193+
init: async (dir) => {
2194+
await Bun.write(
2195+
path.join(dir, "opencode.json"),
2196+
JSON.stringify({
2197+
$schema: "https://opencode.ai/config.json",
2198+
provider: {
2199+
anthropic: {
2200+
models: {
2201+
"claude-sonnet-4-20250514": {
2202+
headers: {
2203+
"X-Model-Header": "model-value",
2204+
},
2205+
},
2206+
},
2207+
},
2208+
},
2209+
}),
2210+
)
2211+
},
2212+
})
2213+
await Instance.provide({
2214+
directory: tmp.path,
2215+
init: async () => {
2216+
Env.set("ANTHROPIC_API_KEY", "test-api-key")
2217+
},
2218+
fn: async () => {
2219+
const providers = await Provider.list()
2220+
const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
2221+
expect(model.headers).toBeDefined()
2222+
expect(model.headers["X-Model-Header"]).toBe("model-value")
2223+
},
2224+
})
2225+
})

0 commit comments

Comments
 (0)