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