Skip to main content

systemprompt_cloud/cli_session/
mod.rs

1//! Persisted CLI authentication sessions, keyed per local or per tenant.
2//!
3//! Exposes [`SessionKey`] (the local-or-tenant discriminator used as a storage
4//! key), the [`CliSession`] record and its [`CliSessionBuilder`], and the
5//! [`SessionStore`] that loads and saves sessions on disk.
6//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10mod session;
11mod store;
12
13use serde::{Deserialize, Serialize};
14use systemprompt_identifiers::TenantId;
15
16pub use session::{CliSession, CliSessionBuilder, SessionIdentity};
17pub use store::SessionStore;
18
19pub const LOCAL_SESSION_KEY: &str = "local";
20
21#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
22#[serde(tag = "type", content = "value")]
23pub enum SessionKey {
24    Local,
25    Tenant(TenantId),
26}
27
28impl SessionKey {
29    #[must_use]
30    pub fn from_tenant_id(tenant_id: Option<&TenantId>) -> Self {
31        tenant_id.map_or(Self::Local, |id| Self::Tenant(id.clone()))
32    }
33
34    #[must_use]
35    pub fn as_storage_key(&self) -> String {
36        match self {
37            Self::Local => LOCAL_SESSION_KEY.to_owned(),
38            Self::Tenant(id) => format!("tenant_{}", id),
39        }
40    }
41
42    #[must_use]
43    pub const fn tenant_id(&self) -> Option<&TenantId> {
44        match self {
45            Self::Local => None,
46            Self::Tenant(id) => Some(id),
47        }
48    }
49
50    #[must_use]
51    pub fn tenant_id_str(&self) -> Option<&str> {
52        self.tenant_id().map(TenantId::as_str)
53    }
54
55    #[must_use]
56    pub const fn is_local(&self) -> bool {
57        matches!(self, Self::Local)
58    }
59}
60
61impl std::fmt::Display for SessionKey {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        match self {
64            Self::Local => write!(f, "local"),
65            Self::Tenant(id) => write!(f, "tenant:{}", id),
66        }
67    }
68}