Cotton code
131 lines · 5.4 KB · RustRaw
//! The provider keys — the ONE declaration of which API keys Cotton knows//! about, and of what each one is called everywhere it is written down.//!//! **A key is a vault secret** (Cotton note "OAuth and the provider keys — what the server holds"). It used to be a//! line in `~/CottonDB/env` on one Mac, which meant a second Mac joined the//! same database and could route nothing; the vault is synced and device-bound,//! so every Mac that can open the passwords can also pay for a run.//!//! **One entry per key, and a "no" is a written reason rather than a missing//! line.** Four names are declared here and nothing else may invent a fifth on//! the side: the Keys page draws this list, the server resolves this list, and//! the one-release env fallback migrates this list. A provider Cotton does not//! call yet has no entry because Cotton does not call it — when it does, the//! entry comes first and [`PROVIDER_KEYS`] is what every seam re-reads.//!//! Three spellings per key and they are deliberately different from each other://!//! * [`ProviderKey::name`] — `api.anthropic`. What the `cotton_secrets` row is//!   CALLED, so a person scrolling their own passwords sees a name rather than//!   a shouted environment variable.//! * [`ProviderKey::service`] — `anthropic`. What the row is FILED under, which//!   is the column `api_key_for` matches on and the column the partial unique//!   index holds one row per. It was already `anthropic` before this module//!   existed and is left exactly as it was, because changing it would orphan//!   the key the owner has already stored.//! * [`ProviderKey::env_name`] — `ANTHROPIC_API_KEY`. The environment variable//!   the server has always read, kept for ONE release as a fallback and as what//!   the migration reads out of the env file./// One API key Cotton knows how to spend.#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]pub enum ProviderKey {    /// Routing, documents, summaries, meetings, the assistant runs.    Anthropic,    /// Voice-note transcription and the meaning lane.    OpenAi,    /// Meeting transcription.    AssemblyAi,    /// The news harvest's web search.    Brave,}/// Every provider key, in the order the Keys page draws them.////// The order is the order a person meets them: without the Anthropic key/// nothing is routed at all, and the other three each turn one lane on.pub const PROVIDER_KEYS: [ProviderKey; 4] = [    ProviderKey::Anthropic,    ProviderKey::OpenAi,    ProviderKey::AssemblyAi,    ProviderKey::Brave,];impl ProviderKey {    /// What the `cotton_secrets` row is called — `api.anthropic`.    ///    /// The `api.` prefix is what keeps these four apart from a login somebody    /// happens to call "Anthropic" in the same list.    pub const fn name(self) -> &'static str {        match self {            ProviderKey::Anthropic => "api.anthropic",            ProviderKey::OpenAi => "api.openai",            ProviderKey::AssemblyAi => "api.assemblyai",            ProviderKey::Brave => "api.brave",        }    }    /// The `service` column — what a reader matches on.    ///    /// **Never renamed.** `anthropic` is the string the owner's existing row    /// already carries (`assistant::secrets::ANTHROPIC_SERVICE` was its one    /// declaration and now delegates here), and a rename would be a key that is    /// silently not found.    pub const fn service(self) -> &'static str {        match self {            ProviderKey::Anthropic => "anthropic",            ProviderKey::OpenAi => "openai",            ProviderKey::AssemblyAi => "assemblyai",            ProviderKey::Brave => "brave",        }    }    /// The environment variable the server read before the vault held these,    /// and reads for one more release when the vault has no row.    pub const fn env_name(self) -> &'static str {        match self {            ProviderKey::Anthropic => "ANTHROPIC_API_KEY",            ProviderKey::OpenAi => "OPENAI_API_KEY",            ProviderKey::AssemblyAi => "ASSEMBLYAI_API_KEY",            ProviderKey::Brave => "BRAVE_SEARCH_API_KEY",        }    }    /// What the Keys page calls it, in the person's words.    pub const fn label(self) -> &'static str {        match self {            ProviderKey::Anthropic => "Anthropic",            ProviderKey::OpenAi => "OpenAI",            ProviderKey::AssemblyAi => "AssemblyAI",            ProviderKey::Brave => "Brave Search",        }    }    /// One sentence saying what stops without it — the line under the field.    pub const fn rests(self) -> &'static str {        match self {            ProviderKey::Anthropic => {                "Routing, documents, summaries and the assistant wait without this."            }            ProviderKey::OpenAi => "Voice notes wait to be transcribed without this.",            ProviderKey::AssemblyAi => "Meetings wait to be transcribed without this.",            ProviderKey::Brave => "The news harvest searches less widely without this.",        }    }    /// The key this name means, or nothing.    pub fn from_name(name: &str) -> Option<ProviderKey> {        PROVIDER_KEYS.into_iter().find(|key| key.name() == name)    }    /// The key this `service` column means, or nothing.    pub fn from_service(service: &str) -> Option<ProviderKey> {        PROVIDER_KEYS            .into_iter()            .find(|key| key.service() == service)    }}#[cfg(test)]#[path = "keys_tests.rs"]mod keys_tests;