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

Commit fd487d5

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 75f6737 commit fd487d5

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
@@ -123,6 +123,10 @@ func NewProvider(providerName models.ModelProvider, opts ...ProviderClientOption
123123
case models.ProviderOpenRouter:
124124
clientOptions.openaiOptions = append(clientOptions.openaiOptions,
125125
WithOpenAIBaseURL("https://openrouter.ai/api/v1"),
126+
WithOpenAIExtraHeaders(map[string]string{
127+
"HTTP-Referer": "opencode.ai",
128+
"X-Title": "OpenCode",
129+
}),
126130
)
127131
return &baseProvider[OpenAIClient]{
128132
options: clientOptions,

0 commit comments

Comments
 (0)