Read-only mirror of github.com/ondeinference/ed.
Mirrored from https://github.com/ondeinference/ed.git
Synced Nov 23, 01:00 UTC
| 1 | //! [`ClaudeAuth`]: the Anthropic API key as an `ed-acp` [`AuthProvider`]. |
| 2 | |
| 3 | use std::path::PathBuf; |
| 4 | |
| 5 | use agent_client_protocol::schema::v1::{AuthMethod, AuthMethodAgent, ErrorCode}; |
| 6 | use async_trait::async_trait; |
| 7 | use ed_acp::{AuthCtx, AuthProvider, LlmConfig, Provider}; |
| 8 | |
| 9 | use crate::credentials; |
| 10 | |
| 11 | /// The id of the auth method, as the client sends it in `authenticate`. |
| 12 | pub const METHOD_ID: &str = "anthropic-api-key"; |
| 13 | |
| 14 | /// Anthropic's OpenAI-compatible endpoint. |
| 15 | pub const BASE_URL: &str = "https://api.anthropic.com/v1"; |
| 16 | |
| 17 | /// The model a session starts on when none is stored. |
| 18 | pub const DEFAULT_MODEL: &str = "claude-sonnet-5-5"; |
| 19 | |
| 20 | /// The models offered in the picker. Fixed here because Anthropic's `/models` may want an |
| 21 | /// `x-api-key` header the compatibility layer doesn't take from a bearer. |
| 22 | pub const MODELS: [&str; 3] = [DEFAULT_MODEL, "claude-opus-5-5", "claude-haiku-5-5"]; |
| 23 | |
| 24 | /// The message `authenticate` fails with when no key is stored. A host UI that sees it asks the |
| 25 | /// user for a key and stores it with [`credentials::store_api_key`]; `ed-acp-tauri` supports |
| 26 | /// only URL elicitation, so the key can't be collected through the protocol. |
| 27 | pub const API_KEY_NEEDED: &str = "Add your Anthropic API key to use Claude."; |
| 28 | |
| 29 | #[derive(Debug, Clone, Default)] |
| 30 | pub struct ClaudeAuth { |
| 31 | base_url: Option<String>, |
| 32 | dir: Option<PathBuf>, |
| 33 | } |
| 34 | |
| 35 | impl ClaudeAuth { |
| 36 | pub fn new() -> Self { |
| 37 | Self::default() |
| 38 | } |
| 39 | |
| 40 | /// Talk to another endpoint, e.g. a proxy or a test server. |
| 41 | pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self { |
| 42 | self.base_url = Some(base_url.into().trim_end_matches('/').to_string()); |
| 43 | self |
| 44 | } |
| 45 | |
| 46 | /// Read the key from `dir` instead of the process-wide [`credentials::dir`]. |
| 47 | pub fn with_dir(mut self, dir: PathBuf) -> Self { |
| 48 | self.dir = Some(dir); |
| 49 | self |
| 50 | } |
| 51 | |
| 52 | fn stored(&self) -> Option<credentials::Stored> { |
| 53 | match &self.dir { |
| 54 | Some(dir) => credentials::load_from(dir), |
| 55 | None => credentials::load(), |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[async_trait] |
| 61 | impl AuthProvider for ClaudeAuth { |
| 62 | fn method(&self) -> AuthMethod { |
| 63 | AuthMethod::Agent( |
| 64 | AuthMethodAgent::new(METHOD_ID, "Use an Anthropic API key") |
| 65 | .description("Claude through the Anthropic API, billed to your API key."), |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | fn llm_config(&self) -> Option<LlmConfig> { |
| 70 | let stored = self.stored()?; |
| 71 | let model = stored |
| 72 | .model |
| 73 | .filter(|m| MODELS.contains(&m.as_str())) |
| 74 | .unwrap_or_else(|| DEFAULT_MODEL.to_string()); |
| 75 | Some(LlmConfig { |
| 76 | provider: Provider::OpenAi, |
| 77 | base_url: self |
| 78 | .base_url |
| 79 | .clone() |
| 80 | .unwrap_or_else(|| BASE_URL.to_string()), |
| 81 | api_key: Some(stored.api_key), |
| 82 | model, |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | fn models(&self) -> Option<Vec<String>> { |
| 87 | Some(MODELS.iter().map(|m| m.to_string()).collect()) |
| 88 | } |
| 89 | |
| 90 | async fn authenticate(&self, _ctx: AuthCtx<'_>) -> agent_client_protocol::Result<()> { |
| 91 | if self.stored().is_some() { |
| 92 | return Ok(()); |
| 93 | } |
| 94 | Err(agent_client_protocol::Error::new( |
| 95 | ErrorCode::InternalError.into(), |
| 96 | API_KEY_NEEDED.to_string(), |
| 97 | )) |
| 98 | } |
| 99 | |
| 100 | async fn logout(&self) -> anyhow::Result<()> { |
| 101 | let removed = match &self.dir { |
| 102 | Some(dir) => credentials::clear_in(dir), |
| 103 | None => credentials::clear(), |
| 104 | }; |
| 105 | tracing::info!("logout: stored Anthropic key removed: {removed}"); |
| 106 | Ok(()) |
| 107 | } |
| 108 | } |