systemprompt_models/api/
contexts.rs1use std::fmt;
7use std::str::FromStr;
8
9use chrono::{DateTime, Utc};
10use serde::{Deserialize, Serialize};
11use systemprompt_identifiers::{ContextId, UserId};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
15#[cfg_attr(feature = "sqlx", sqlx(type_name = "TEXT", rename_all = "snake_case"))]
16#[serde(rename_all = "snake_case")]
17pub enum ContextKind {
18 User,
19 CliSession,
20 Session,
21 Evaluation,
22 McpValidation,
23 CliProbe,
24 Derived,
25 Legacy,
26}
27
28impl ContextKind {
29 pub const fn as_str(self) -> &'static str {
30 match self {
31 Self::User => "user",
32 Self::CliSession => "cli_session",
33 Self::Session => "session",
34 Self::Evaluation => "evaluation",
35 Self::McpValidation => "mcp_validation",
36 Self::CliProbe => "cli_probe",
37 Self::Derived => "derived",
38 Self::Legacy => "legacy",
39 }
40 }
41}
42
43impl fmt::Display for ContextKind {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 f.write_str(self.as_str())
46 }
47}
48
49#[derive(Debug, Clone, thiserror::Error)]
50#[error("unknown context kind: {0}")]
51pub struct ParseContextKindError(String);
52
53impl FromStr for ContextKind {
54 type Err = ParseContextKindError;
55
56 fn from_str(s: &str) -> Result<Self, Self::Err> {
57 match s {
58 "user" => Ok(Self::User),
59 "cli_session" => Ok(Self::CliSession),
60 "session" => Ok(Self::Session),
61 "evaluation" => Ok(Self::Evaluation),
62 "mcp_validation" => Ok(Self::McpValidation),
63 "cli_probe" => Ok(Self::CliProbe),
64 "derived" => Ok(Self::Derived),
65 "legacy" => Ok(Self::Legacy),
66 other => Err(ParseContextKindError(other.to_owned())),
67 }
68 }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct UserContext {
73 pub context_id: ContextId,
74 pub user_id: UserId,
75 pub name: String,
76 pub kind: ContextKind,
77 pub created_at: DateTime<Utc>,
78 pub updated_at: DateTime<Utc>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct UserContextWithStats {
83 pub context_id: ContextId,
84 pub user_id: UserId,
85 pub name: String,
86 pub kind: ContextKind,
87 pub created_at: DateTime<Utc>,
88 pub updated_at: DateTime<Utc>,
89 pub task_count: i64,
90 pub message_count: i64,
91 pub last_message_at: Option<DateTime<Utc>>,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct CreateContextRequest {
96 pub name: Option<String>,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct UpdateContextRequest {
101 pub name: String,
102}