Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,11 @@ export namespace Provider {
}
}

opts.headers = {
...(typeof opts.headers === "object" ? opts.headers : {}),
...options["headers"],
}

return fetchFn(input, {
...opts,
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682
Expand Down
75 changes: 75 additions & 0 deletions packages/opencode/test/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2148,3 +2148,78 @@ test("custom model with variants enabled and disabled", async () => {
},
})
})

test("provider headers from config are applied to fetch requests", async () => {
let capturedHeaders: Record<string, string> | undefined

await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
anthropic: {
options: {
headers: {
"X-Custom-Header": "custom-value",
"User-Agent": "MyCustomAgent/1.0",
},
},
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
init: async () => {
Env.set("ANTHROPIC_API_KEY", "test-api-key")
},
fn: async () => {
const providers = await Provider.list()
expect(providers["anthropic"]).toBeDefined()
// Verify headers are in provider options
expect(providers["anthropic"].options.headers).toBeDefined()
expect(providers["anthropic"].options.headers["X-Custom-Header"]).toBe("custom-value")
expect(providers["anthropic"].options.headers["User-Agent"]).toBe("MyCustomAgent/1.0")
},
})
})

test("model headers from config are merged with provider headers", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
anthropic: {
models: {
"claude-sonnet-4-20250514": {
headers: {
"X-Model-Header": "model-value",
},
},
},
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
init: async () => {
Env.set("ANTHROPIC_API_KEY", "test-api-key")
},
fn: async () => {
const providers = await Provider.list()
const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
expect(model.headers).toBeDefined()
expect(model.headers["X-Model-Header"]).toBe("model-value")
},
})
})
Loading