From 1c5443358e99bb196bdd812969109126cb99937f Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Fri, 19 Dec 2025 13:22:05 +0100 Subject: [PATCH] fix: initial api key load (#1672) --- internal/agent/coordinator.go | 10 ++++------ internal/config/config.go | 4 +--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/internal/agent/coordinator.go b/internal/agent/coordinator.go index ea214a5bcfb8e65e6d5dee826854345168a2eb86..7ce37758897da42cd8515c94c26b9e24add6ea1e 100644 --- a/internal/agent/coordinator.go +++ b/internal/agent/coordinator.go @@ -516,14 +516,13 @@ func (c *coordinator) buildAgentModels(ctx context.Context) (Model, Model, error }, nil } -func (c *coordinator) buildAnthropicProvider(baseURL, apiKey string, headers map[string]string) (fantasy.Provider, error) { +func (c *coordinator) buildAnthropicProvider(baseURL, apiKey string, headers map[string]string, isOauth bool) (fantasy.Provider, error) { var opts []anthropic.Option - if strings.HasPrefix(apiKey, "Bearer ") { + if isOauth { // NOTE: Prevent the SDK from picking up the API key from env. os.Setenv("ANTHROPIC_API_KEY", "") - - headers["Authorization"] = apiKey + headers["Authorization"] = fmt.Sprintf("Bearer %s", apiKey) } else if apiKey != "" { // X-Api-Key header opts = append(opts, anthropic.WithAPIKey(apiKey)) @@ -541,7 +540,6 @@ func (c *coordinator) buildAnthropicProvider(baseURL, apiKey string, headers map httpClient := log.NewHTTPClient() opts = append(opts, anthropic.WithHTTPClient(httpClient)) } - return anthropic.New(opts...) } @@ -722,7 +720,7 @@ func (c *coordinator) buildProvider(providerCfg config.ProviderConfig, model con case openai.Name: return c.buildOpenaiProvider(baseURL, apiKey, headers) case anthropic.Name: - return c.buildAnthropicProvider(baseURL, apiKey, headers) + return c.buildAnthropicProvider(baseURL, apiKey, headers, providerCfg.OAuthToken != nil) case openrouter.Name: return c.buildOpenrouterProvider(baseURL, apiKey, headers) case azure.Name: diff --git a/internal/config/config.go b/internal/config/config.go index e5878a5d0c999c612e3b5e2c5dd61ec4c4dc324d..887e58b66d92c860c5d7fa9bc7a512b3853be4f4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -550,13 +550,12 @@ func (c *Config) RefreshOAuthToken(ctx context.Context, providerID string) error slog.Info("Successfully refreshed OAuth token", "provider", providerID) providerConfig.OAuthToken = newToken + providerConfig.APIKey = newToken.AccessToken switch providerID { case string(catwalk.InferenceProviderAnthropic): - providerConfig.APIKey = fmt.Sprintf("Bearer %s", newToken.AccessToken) providerConfig.SetupClaudeCode() case string(catwalk.InferenceProviderCopilot): - providerConfig.APIKey = newToken.AccessToken providerConfig.SetupGitHubCopilot() } @@ -595,7 +594,6 @@ func (c *Config) SetProviderAPIKey(providerID string, apiKey any) error { providerConfig.OAuthToken = v switch providerID { case string(catwalk.InferenceProviderAnthropic): - providerConfig.APIKey = fmt.Sprintf("Bearer %s", v.AccessToken) providerConfig.SetupClaudeCode() case string(catwalk.InferenceProviderCopilot): providerConfig.SetupGitHubCopilot()