Skip to main content

systemprompt_identifiers/
session.rs

1//! Session identifier (`sess_<uuid>`) and its originating-source enum.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6crate::define_id!(SessionId, schema);
7
8impl SessionId {
9    pub fn generate() -> Self {
10        Self(format!("sess_{}", uuid::Uuid::new_v4()))
11    }
12
13    pub fn system() -> Self {
14        Self("system".to_owned())
15    }
16}
17
18#[derive(
19    Debug, Clone, Copy, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
20)]
21#[serde(rename_all = "lowercase")]
22pub enum SessionSource {
23    Web,
24    Api,
25    Cli,
26    Oauth,
27    Mcp,
28    Bridge,
29    #[default]
30    Unknown,
31}
32
33impl SessionSource {
34    pub const fn as_str(self) -> &'static str {
35        match self {
36            Self::Web => "web",
37            Self::Api => "api",
38            Self::Cli => "cli",
39            Self::Oauth => "oauth",
40            Self::Mcp => "mcp",
41            Self::Bridge => "bridge",
42            Self::Unknown => "unknown",
43        }
44    }
45
46    pub fn from_client_id(client_id: &crate::ClientId) -> Self {
47        match client_id.as_str() {
48            "sp_web" => Self::Web,
49            "sp_cli" => Self::Cli,
50            "sp_bridge" => Self::Bridge,
51            _ => Self::Unknown,
52        }
53    }
54}
55
56impl std::fmt::Display for SessionSource {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        write!(f, "{}", self.as_str())
59    }
60}
61
62impl std::str::FromStr for SessionSource {
63    type Err = std::convert::Infallible;
64
65    fn from_str(s: &str) -> Result<Self, Self::Err> {
66        Ok(match s.to_lowercase().as_str() {
67            "web" => Self::Web,
68            "api" => Self::Api,
69            "cli" => Self::Cli,
70            "oauth" => Self::Oauth,
71            "mcp" => Self::Mcp,
72            "bridge" => Self::Bridge,
73            _ => Self::Unknown,
74        })
75    }
76}