agix-base 0.4.4

Agix — foundation types, traits, and context
Documentation
//! `ModelFactory` — how a wire protocol gets built from configuration.
//!
//! [`Model`] is open: anyone can implement another protocol. Constructing one
//! was not. The composition root matched on the protocol name and knew exactly two
//! answers, so a third protocol could be written but never reached — the only
//! way to configure it was to edit the match, in the kernel, in a release.
//!
//! # What is registered, and by what key
//!
//! The key is a provider's `api` **string** from `settings.json`, not the
//! [`ApiType`] enum. That enum names the protocols the engine itself
//! implements and is what a [`Model`] reports about itself; the registry is
//! about what a *host* can add, and a host adding a protocol has no way to
//! add a variant to an enum in this crate. A third-party model reports
//! whichever `ApiType` describes its wire shape most closely, which affects
//! how it is labeled in telemetry and nothing else.
//!
//! # Unknown means unknown at startup
//!
//! [`ModelFactoryRegistry::build`] fails for an `api` nobody claimed,
//! and it fails where provider config is read — at startup — rather than on
//! the first call that happens to route there. A typo in a provider name
//! should cost a clear error at launch, not a mid-conversation failure in
//! whichever session first uses that task type.

use std::collections::HashMap;
use std::sync::Arc;

use crate::interface::credentials::{ConfigCredentials, CredentialSource, Secret};
use crate::interface::exec::Network;
use crate::interface::model::Model;
use crate::provider::ProviderConfig;

/// Builds a live model from one provider's configuration.
pub trait ModelFactory: Send + Sync {
    /// The `api` value in `settings.json` this factory answers to.
    fn api_type(&self) -> &str;

    /// Construct the model, or say what is wrong with the config.
    ///
    /// `provider_id` is only for the error message — it is the name the user
    /// wrote, and an error that does not name it makes them guess which of
    /// their providers is broken.
    ///
    /// The credential comes from `credentials`, never from `config.api_key_env`
    /// directly: a factory that reads the field itself silently opts its
    /// protocol out of whatever the host arranged, which is exactly the bug
    /// the source exists to prevent.
    ///
    /// `network`, when there is one, is the egress the model's requests go
    /// out on — it carries the deployment's `User-Agent`; `None` leaves the
    /// factory's own.
    fn build(
        &self,
        provider_id: &str,
        config: &ProviderConfig,
        credentials: &dyn CredentialSource,
        network: Option<&Arc<dyn Network>>,
    ) -> Result<Arc<dyn Model>, String>;
}

/// Every wire protocol this process can construct.
#[derive(Clone)]
pub struct ModelFactoryRegistry {
    by_api_type: HashMap<String, Arc<dyn ModelFactory>>,
    credentials: Arc<dyn CredentialSource>,
    network: Option<Arc<dyn Network>>,
}

impl Default for ModelFactoryRegistry {
    fn default() -> Self {
        Self {
            by_api_type: HashMap::new(),
            credentials: Arc::new(ConfigCredentials),
            network: None,
        }
    }
}

impl ModelFactoryRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a factory, replacing any previous one for the same `api` and
    /// returning it.
    ///
    /// Replacing is allowed on purpose: a host that wants its own client
    /// behind `anthropic` — a proxy, a recording layer, a different retry
    /// policy — should not have to invent a new `api` its users then
    /// have to write in their settings.
    pub fn register(&mut self, factory: Arc<dyn ModelFactory>) -> Option<Arc<dyn ModelFactory>> {
        self.by_api_type
            .insert(factory.api_type().to_string(), factory)
    }

    /// Ask `source` for every credential instead of reading `settings.json`.
    ///
    /// One source for the whole registry, not one per protocol: "where do
    /// this deployment's credentials come from" is a property of the
    /// deployment, and letting it vary per protocol would mean a host could
    /// secure one and forget the other.
    pub fn with_credentials(mut self, source: Arc<dyn CredentialSource>) -> Self {
        self.credentials = source;
        self
    }

    /// Send every model's requests through `network`. One for the whole
    /// registry, for the reason credentials are.
    pub fn with_network(mut self, network: Arc<dyn Network>) -> Self {
        self.network = Some(network);
        self
    }

    pub fn get(&self, api_type: &str) -> Option<&Arc<dyn ModelFactory>> {
        self.by_api_type.get(api_type)
    }

    /// The registered `api` values, sorted — for error messages, so what
    /// the user is told they may write is generated from what actually works.
    pub fn api_types(&self) -> Vec<&str> {
        let mut out: Vec<&str> = self.by_api_type.keys().map(String::as_str).collect();
        out.sort_unstable();
        out
    }

    pub fn is_empty(&self) -> bool {
        self.by_api_type.is_empty()
    }

    /// Why the credential source has no usable key for this provider, when
    /// it has none.
    pub fn missing_credential(&self, provider_id: &str, config: &ProviderConfig) -> Option<String> {
        NonEmpty(self.credentials.as_ref())
            .api_key(provider_id, config)
            .err()
    }

    /// Build the model one provider config describes.
    ///
    /// An absent `api` means `anthropic`. An empty key from the source
    /// is no key: the factory is not asked to build a client that would send
    /// requests with it.
    pub fn build(
        &self,
        provider_id: &str,
        config: &ProviderConfig,
    ) -> Result<Arc<dyn Model>, String> {
        let api_type = config.api();
        match self.by_api_type.get(api_type) {
            Some(f) => f.build(
                provider_id,
                config,
                &NonEmpty(self.credentials.as_ref()),
                self.network.as_ref(),
            ),
            None => Err(format!(
                "provider '{provider_id}': unrecognized api '{api_type}' (expected {})",
                self.api_types()
                    .iter()
                    .map(|t| format!("'{t}'"))
                    .collect::<Vec<_>>()
                    .join(" or ")
            )),
        }
    }
}

struct NonEmpty<'a>(&'a dyn CredentialSource);

impl CredentialSource for NonEmpty<'_> {
    fn api_key(&self, provider_id: &str, config: &ProviderConfig) -> Result<Secret, String> {
        let key = self.0.api_key(provider_id, config)?;
        if key.is_empty() {
            return Err("its API key is empty".to_string());
        }
        Ok(key)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct Stub(&'static str);

    impl ModelFactory for Stub {
        fn api_type(&self) -> &str {
            self.0
        }
        fn build(
            &self,
            _id: &str,
            _cfg: &ProviderConfig,
            _credentials: &dyn CredentialSource,
            _network: Option<&Arc<dyn Network>>,
        ) -> Result<Arc<dyn Model>, String> {
            Err("stub".into())
        }
    }

    #[test]
    fn an_unknown_api_type_names_the_provider_and_what_is_available() {
        let mut registry = ModelFactoryRegistry::new();
        registry.register(Arc::new(Stub("anthropic")));
        registry.register(Arc::new(Stub("openai_chat")));

        let Err(err) = registry.build(
            "my-provider",
            &ProviderConfig {
                api: Some("gemini".into()),
                ..Default::default()
            },
        ) else {
            panic!("an unregistered api must not build");
        };
        assert!(err.contains("my-provider"), "{err}");
        assert!(err.contains("'gemini'"), "{err}");
        assert!(
            err.contains("'anthropic' or 'openai_chat'"),
            "the list of what works must come from what is registered: {err}"
        );
    }

    #[test]
    fn no_api_type_means_anthropic() {
        let mut registry = ModelFactoryRegistry::new();
        registry.register(Arc::new(Stub("anthropic")));
        let Err(err) = registry.build("p", &ProviderConfig::default()) else {
            panic!("the stub factory never succeeds");
        };
        assert_eq!(
            err, "stub",
            "an absent api must reach the anthropic factory"
        );
    }

    #[test]
    fn a_host_can_take_over_a_built_in_api_type() {
        let mut registry = ModelFactoryRegistry::new();
        registry.register(Arc::new(Stub("anthropic")));
        let replaced = registry.register(Arc::new(Stub("anthropic")));
        assert!(
            replaced.is_some(),
            "replacing must hand back what it replaced, not silently stack"
        );
        assert_eq!(registry.api_types(), ["anthropic"]);
    }
}