|
| 1 | +use std::{future::Future, ops::Deref, pin::Pin, sync::Arc}; |
| 2 | + |
| 3 | +use genai::{ |
| 4 | + adapter::AdapterKind, |
| 5 | + resolver::{AuthData, Endpoint, ServiceTargetResolver}, |
| 6 | + ClientConfig, ModelIden, ServiceTarget, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::secret_cache::{SecretCache, SecretCacheError}; |
| 10 | + |
| 11 | +#[derive(Debug, thiserror::Error)] |
| 12 | +pub enum AtuinAIClientError { |
| 13 | + #[error("Failed to get credential: {0}")] |
| 14 | + CredentialError(#[from] SecretCacheError), |
| 15 | +} |
| 16 | + |
| 17 | +/// A wrapper around a genai::Client that includes Atuin's custom service target resolver |
| 18 | +pub struct AtuinAIClient { |
| 19 | + client: genai::Client, |
| 20 | + #[allow(dead_code)] // this will be used to fetch provider API keys in the future |
| 21 | + secret_cache: Arc<SecretCache>, |
| 22 | +} |
| 23 | + |
| 24 | +impl AtuinAIClient { |
| 25 | + pub fn new(secret_cache: Arc<SecretCache>) -> Self { |
| 26 | + let secret_cache_clone = secret_cache.clone(); |
| 27 | + let target_resolver = |
| 28 | + ServiceTargetResolver::from_resolver_async_fn(move |service_target| { |
| 29 | + resolve_service_target(service_target, secret_cache_clone.clone()) |
| 30 | + }); |
| 31 | + let client = genai::Client::builder() |
| 32 | + .with_config(ClientConfig::default().with_service_target_resolver(target_resolver)) |
| 33 | + .build(); |
| 34 | + |
| 35 | + Self { |
| 36 | + client, |
| 37 | + secret_cache, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl Deref for AtuinAIClient { |
| 43 | + type Target = genai::Client; |
| 44 | + |
| 45 | + fn deref(&self) -> &genai::Client { |
| 46 | + &self.client |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +fn resolve_service_target( |
| 51 | + mut service_target: ServiceTarget, |
| 52 | + secret_cache: Arc<SecretCache>, |
| 53 | +) -> Pin<Box<dyn Future<Output = Result<ServiceTarget, genai::resolver::Error>> + Send>> { |
| 54 | + Box::pin(async move { |
| 55 | + let model_name = service_target.model.model_name.to_string(); |
| 56 | + let parts = model_name.splitn(3, "::").collect::<Vec<&str>>(); |
| 57 | + |
| 58 | + if parts.len() != 3 { |
| 59 | + return Err(genai::resolver::Error::Custom(format!( |
| 60 | + "Invalid Atuin Desktop model identifier format: {}", |
| 61 | + model_name |
| 62 | + ))); |
| 63 | + } |
| 64 | + |
| 65 | + // Set the adapter kind based on the provider |
| 66 | + let adapter_kind = match parts[0] { |
| 67 | + "atuinhub" => AdapterKind::Anthropic, |
| 68 | + "claude" => AdapterKind::Anthropic, |
| 69 | + "openai" => AdapterKind::OpenAI, |
| 70 | + "ollama" => AdapterKind::Ollama, |
| 71 | + _ => { |
| 72 | + return Err(genai::resolver::Error::Custom(format!( |
| 73 | + "Invalid provider identifier: {}", |
| 74 | + parts[0] |
| 75 | + ))) |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + // Set the API key, if any, for the provider |
| 80 | + let key = get_api_key(&secret_cache, adapter_kind, parts[0] == "atuinhub") |
| 81 | + .await |
| 82 | + .map_err(|e| genai::resolver::Error::Custom(e.to_string()))?; |
| 83 | + |
| 84 | + if let Some(key) = key { |
| 85 | + let auth = AuthData::Key(key); |
| 86 | + service_target.auth = auth; |
| 87 | + } else { |
| 88 | + service_target.auth = AuthData::Key("".to_string()); |
| 89 | + } |
| 90 | + |
| 91 | + // Set the specific model |
| 92 | + let model_id = ModelIden::new(adapter_kind, parts[1]); |
| 93 | + service_target.model = model_id; |
| 94 | + |
| 95 | + // Set the endpoint, if any, for the model |
| 96 | + if parts[2] != "default" { |
| 97 | + service_target.endpoint = Endpoint::from_owned(parts[2].to_string()); |
| 98 | + } |
| 99 | + |
| 100 | + Ok(service_target) |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +async fn get_api_key( |
| 105 | + _secret_cache: &SecretCache, |
| 106 | + _adapter_kind: AdapterKind, |
| 107 | + is_hub: bool, |
| 108 | +) -> Result<Option<String>, AtuinAIClientError> { |
| 109 | + // todo |
| 110 | + if is_hub { |
| 111 | + return Ok(None); |
| 112 | + } |
| 113 | + |
| 114 | + Ok(None) |
| 115 | +} |
0 commit comments