Skip to main content

systemprompt_cloud/cli_session/
mod.rs

1mod session;
2mod store;
3
4use serde::{Deserialize, Serialize};
5use systemprompt_identifiers::TenantId;
6
7pub use session::{CliSession, CliSessionBuilder};
8pub use store::SessionStore;
9
10pub const LOCAL_SESSION_KEY: &str = "local";
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[serde(tag = "type", content = "value")]
14pub enum SessionKey {
15    Local,
16    Tenant(TenantId),
17}
18
19impl SessionKey {
20    #[must_use]
21    pub fn from_tenant_id(tenant_id: Option<&str>) -> Self {
22        tenant_id.map_or(Self::Local, |id| Self::Tenant(TenantId::new(id)))
23    }
24
25    #[must_use]
26    pub fn as_storage_key(&self) -> String {
27        match self {
28            Self::Local => LOCAL_SESSION_KEY.to_string(),
29            Self::Tenant(id) => format!("tenant_{}", id),
30        }
31    }
32
33    #[must_use]
34    pub fn tenant_id(&self) -> Option<&TenantId> {
35        match self {
36            Self::Local => None,
37            Self::Tenant(id) => Some(id),
38        }
39    }
40
41    #[must_use]
42    pub fn tenant_id_str(&self) -> Option<&str> {
43        self.tenant_id().map(TenantId::as_str)
44    }
45
46    #[must_use]
47    pub const fn is_local(&self) -> bool {
48        matches!(self, Self::Local)
49    }
50}
51
52impl std::fmt::Display for SessionKey {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            Self::Local => write!(f, "local"),
56            Self::Tenant(id) => write!(f, "tenant:{}", id),
57        }
58    }
59}