Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit 9da8992

Browse files
**[feature/openrouter-provider] Add support for custom headers in OpenAI client configuration**
- Introduced a new `extraHeaders` field in the `openaiOptions` struct to allow specifying additional HTTP headers. - Added logic in `newOpenAIClient` to apply `extraHeaders` to the OpenAI client configuration. - Implemented a new option function `WithOpenAIExtraHeaders` to set custom headers in `openaiOptions`. - Updated the OpenRouter provider configuration in `NewProvider` to include default headers (`HTTP-Referer` and `X-Title`) for OpenRouter API requests.
1 parent 0bb9c80 commit 9da8992

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

internal/llm/provider/openai.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type openaiOptions struct {
2121
baseURL string
2222
disableCache bool
2323
reasoningEffort string
24+
extraHeaders map[string]string
2425
}
2526

2627
type OpenAIOption func(*openaiOptions)
@@ -49,6 +50,12 @@ func newOpenAIClient(opts providerClientOptions) OpenAIClient {
4950
openaiClientOptions = append(openaiClientOptions, option.WithBaseURL(openaiOpts.baseURL))
5051
}
5152

53+
if openaiOpts.extraHeaders != nil {
54+
for key, value := range openaiOpts.extraHeaders {
55+
openaiClientOptions = append(openaiClientOptions, option.WithHeader(key, value))
56+
}
57+
}
58+
5259
client := openai.NewClient(openaiClientOptions...)
5360
return &openaiClient{
5461
providerOptions: opts,
@@ -392,6 +399,12 @@ func WithOpenAIBaseURL(baseURL string) OpenAIOption {
392399
}
393400
}
394401

402+
func WithOpenAIExtraHeaders(headers map[string]string) OpenAIOption {
403+
return func(options *openaiOptions) {
404+
options.extraHeaders = headers
405+
}
406+
}
407+
395408
func WithOpenAIDisableCache() OpenAIOption {
396409
return func(options *openaiOptions) {
397410
options.disableCache = true

internal/llm/provider/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func NewProvider(providerName models.ModelProvider, opts ...ProviderClientOption
118118
case models.ProviderOpenRouter:
119119
clientOptions.openaiOptions = append(clientOptions.openaiOptions,
120120
WithOpenAIBaseURL("https://openrouter.ai/api/v1"),
121+
WithOpenAIExtraHeaders(map[string]string{
122+
"HTTP-Referer": "opencode.ai",
123+
"X-Title": "OpenCode",
124+
}),
121125
)
122126
return &baseProvider[OpenAIClient]{
123127
options: clientOptions,

0 commit comments

Comments
 (0)