fix: initial api key load (#1672)

Kujtim Hoxha created

Change summary

internal/agent/coordinator.go | 10 ++++------
internal/config/config.go     |  4 +---
2 files changed, 5 insertions(+), 9 deletions(-)

Detailed changes

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:

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()