Skip to main content

systemprompt_identifiers/
session.rs

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