Skip to main content

systemprompt_cli/
env_overrides.rs

1//! Process-environment snapshot for the CLI.
2//!
3//! [`EnvOverrides`] captures every environment variable the CLI consults, read
4//! once at process start ([`EnvOverrides::from_process_env`]) and threaded
5//! through [`crate::context::CommandContext`]. Command code never calls
6//! `std::env::var` directly — tests construct the snapshot with
7//! [`EnvOverrides::from_vars`] instead of mutating process state.
8//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12use std::collections::HashMap;
13
14use systemprompt_identifiers::{ContextId, SessionId, UserId};
15
16/// `is_deployment_host` means the process runs on the host the active profile
17/// describes, so a command must run locally instead of routing to the
18/// deployment it is already inside.
19#[derive(Debug, Clone, Default)]
20pub struct EnvOverrides {
21    pub output_format: Option<String>,
22    pub log_level: Option<String>,
23    pub no_color: bool,
24    pub non_interactive: bool,
25    pub profile: Option<String>,
26    pub rust_log: Option<String>,
27    pub is_deployment_host: bool,
28    pub is_remote_cli: bool,
29    pub editor: Option<String>,
30    pub database_url: Option<String>,
31    pub services_path: Option<String>,
32    pub session: SessionEnv,
33}
34
35#[derive(Debug, Clone, Default)]
36pub struct SessionEnv {
37    pub user_id: Option<UserId>,
38    pub session_id: Option<SessionId>,
39    pub context_id: Option<ContextId>,
40    pub auth_token: Option<String>,
41}
42
43impl EnvOverrides {
44    #[must_use]
45    pub fn from_process_env() -> Self {
46        Self::from_lookup(|key| std::env::var(key).ok())
47    }
48
49    #[must_use]
50    pub fn from_vars<I, K, V>(vars: I) -> Self
51    where
52        I: IntoIterator<Item = (K, V)>,
53        K: Into<String>,
54        V: Into<String>,
55    {
56        let map: HashMap<String, String> = vars
57            .into_iter()
58            .map(|(k, v)| (k.into(), v.into()))
59            .collect();
60        Self::from_lookup(|key| map.get(key).cloned())
61    }
62
63    fn from_lookup(lookup: impl Fn(&str) -> Option<String>) -> Self {
64        Self {
65            output_format: lookup("SYSTEMPROMPT_OUTPUT_FORMAT"),
66            log_level: lookup("SYSTEMPROMPT_LOG_LEVEL"),
67            no_color: lookup("SYSTEMPROMPT_NO_COLOR").is_some() || lookup("NO_COLOR").is_some(),
68            non_interactive: lookup("SYSTEMPROMPT_NON_INTERACTIVE").is_some(),
69            profile: lookup("SYSTEMPROMPT_PROFILE"),
70            rust_log: lookup("RUST_LOG"),
71            is_deployment_host: systemprompt_models::subprocess::is_deployment_host(&lookup),
72            is_remote_cli: lookup("SYSTEMPROMPT_CLI_REMOTE").is_some(),
73            editor: lookup("VISUAL").or_else(|| lookup("EDITOR")),
74            database_url: lookup("DATABASE_URL"),
75            services_path: lookup("SYSTEMPROMPT_SERVICES_PATH"),
76            session: SessionEnv {
77                user_id: lookup("SYSTEMPROMPT_USER_ID").map(UserId::new),
78                session_id: lookup("SYSTEMPROMPT_SESSION_ID").map(SessionId::new),
79                context_id: lookup("SYSTEMPROMPT_CONTEXT_ID").and_then(|value| {
80                    ContextId::try_new(value)
81                        .inspect_err(|error| {
82                            tracing::warn!(
83                                error = %error,
84                                "ignoring malformed SYSTEMPROMPT_CONTEXT_ID; expected a UUID"
85                            );
86                        })
87                        .ok()
88                }),
89                auth_token: lookup("SYSTEMPROMPT_AUTH_TOKEN"),
90            },
91        }
92    }
93}