Skip to content
Merged
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
28 changes: 27 additions & 1 deletion src/llm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! `get_api_key()` calls read the new values lock-free.

use crate::auth::OAuthCredentials;
use crate::config::{LlmConfig, ProviderConfig};
use crate::config::{ApiType, LlmConfig, ProviderConfig};
use crate::error::{LlmError, Result};

use anyhow::Context as _;
Expand Down Expand Up @@ -143,6 +143,32 @@ impl LlmManager {
}
}

/// Resolve the Anthropic provider config, preferring OAuth credentials.
///
/// If a static provider exists in config, returns it with the API key
/// overridden by the OAuth token when available. If no static provider
/// exists but OAuth credentials are present, builds a provider from
/// the OAuth token alone.
pub async fn get_anthropic_provider(&self) -> Result<ProviderConfig> {
let token = self.get_anthropic_token().await?;
let static_provider = self.get_provider("anthropic").ok();

match (static_provider, token) {
(Some(mut provider), Some(token)) => {
provider.api_key = token;
Ok(provider)
}
(Some(provider), None) => Ok(provider),
(None, Some(token)) => Ok(ProviderConfig {
api_type: ApiType::Anthropic,
base_url: "https://api.anthropic.com".to_string(),
api_key: token,
name: None,
}),
(None, None) => Err(LlmError::UnknownProvider("anthropic".to_string()).into()),
}
}

/// Get the appropriate API key for a provider.
pub fn get_api_key(&self, provider_id: &str) -> Result<String> {
let provider = self.get_provider(provider_id)?;
Expand Down
21 changes: 10 additions & 11 deletions src/llm/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,16 @@ impl SpacebotModel {
.map(|(provider, _)| provider)
.unwrap_or("anthropic");

let mut provider_config = self
.llm_manager
.get_provider(provider_id)
.map_err(|e| CompletionError::ProviderError(e.to_string()))?;

// For Anthropic, prefer OAuth token from auth.json over static config key
if provider_id == "anthropic"
&& let Ok(Some(token)) = self.llm_manager.get_anthropic_token().await
{
provider_config.api_key = token;
}
let provider_config = if provider_id == "anthropic" {
self.llm_manager
.get_anthropic_provider()
.await
.map_err(|e| CompletionError::ProviderError(e.to_string()))?
} else {
self.llm_manager
.get_provider(provider_id)
.map_err(|e| CompletionError::ProviderError(e.to_string()))?
};

if provider_id == "zai-coding-plan" || provider_id == "zhipu" {
let display_name = if provider_id == "zhipu" {
Expand Down
Loading