Skip to main content

systemprompt_cli/commands/admin/setup/secrets/
data.rs

1//! Secrets data model and default-provider resolution.
2//!
3//! [`SecretsData`] holds the generated OAuth at-rest pepper, database URL, and
4//! AI-provider keys. [`resolve_primary`] picks the default provider from an
5//! explicit flag or the first present key by [`PROVIDER_PRIORITY`].
6//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10use anyhow::{Result, bail};
11use serde::{Deserialize, Serialize};
12use systemprompt_identifiers::ProviderId;
13
14use super::super::SetupArgs;
15
16pub(super) const STANDARD_PROVIDERS: [&str; 3] = ["gemini", "anthropic", "openai"];
17
18const PROVIDER_PRIORITY: [&str; 3] = ["anthropic", "openai", "gemini"];
19
20#[derive(Debug, Clone, Default, Serialize, Deserialize)]
21pub struct SecretsData {
22    pub oauth_at_rest_pepper: String,
23
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub manifest_signing_secret_seed: Option<String>,
26
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub signing_key_pem: Option<String>,
29
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub database_url: Option<String>,
32
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub gemini: Option<String>,
35
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub anthropic: Option<String>,
38
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub openai: Option<String>,
41
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub github: Option<String>,
44}
45
46impl SecretsData {
47    pub(crate) const fn has_ai_provider(&self) -> bool {
48        self.gemini.is_some() || self.anthropic.is_some() || self.openai.is_some()
49    }
50
51    fn key_for(&self, provider: &str) -> Option<&String> {
52        match provider {
53            "gemini" => self.gemini.as_ref(),
54            "anthropic" => self.anthropic.as_ref(),
55            "openai" => self.openai.as_ref(),
56            _ => None,
57        }
58    }
59
60    pub(crate) fn present_providers(&self) -> Vec<&'static str> {
61        STANDARD_PROVIDERS
62            .into_iter()
63            .filter(|p| self.key_for(p).is_some())
64            .collect()
65    }
66
67    pub(crate) fn summary(&self) -> String {
68        let mut keys = Vec::new();
69        if self.gemini.is_some() {
70            keys.push("Gemini");
71        }
72        if self.anthropic.is_some() {
73            keys.push("Anthropic");
74        }
75        if self.openai.is_some() {
76            keys.push("OpenAI");
77        }
78        if self.github.is_some() {
79            keys.push("GitHub");
80        }
81
82        if keys.is_empty() {
83            "None".to_owned()
84        } else {
85            keys.join(", ")
86        }
87    }
88}
89
90fn first_present_by_priority(secrets: &SecretsData) -> Option<ProviderId> {
91    PROVIDER_PRIORITY
92        .into_iter()
93        .find(|p| secrets.key_for(p).is_some())
94        .map(ProviderId::new)
95}
96
97pub(super) fn resolve_primary(
98    args: &SetupArgs,
99    secrets: &SecretsData,
100) -> Result<Option<ProviderId>> {
101    let Some(name) = args.default_provider.as_deref().map(str::trim) else {
102        return Ok(first_present_by_priority(secrets));
103    };
104    if !STANDARD_PROVIDERS.contains(&name) {
105        bail!("--default-provider must be one of: gemini, anthropic, openai (got '{name}')");
106    }
107    if secrets.key_for(name).is_none() {
108        bail!(
109            "--default-provider '{name}' has no API key; pass --{name}-key or drop \
110             --default-provider"
111        );
112    }
113    Ok(Some(ProviderId::new(name)))
114}