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