Skip to main content

systemprompt_identifiers/
session.rs

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