Skip to main content

systemprompt_models/api/
contexts.rs

1//! `ContextKind` classification for conversation contexts.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use 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}
21
22impl ContextKind {
23    pub const fn as_str(self) -> &'static str {
24        match self {
25            Self::User => "user",
26            Self::CliSession => "cli_session",
27        }
28    }
29}
30
31impl fmt::Display for ContextKind {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.write_str(self.as_str())
34    }
35}
36
37#[derive(Debug, Clone, thiserror::Error)]
38#[error("unknown context kind: {0}")]
39pub struct ParseContextKindError(String);
40
41impl FromStr for ContextKind {
42    type Err = ParseContextKindError;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s {
46            "user" => Ok(Self::User),
47            "cli_session" => Ok(Self::CliSession),
48            other => Err(ParseContextKindError(other.to_owned())),
49        }
50    }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct UserContext {
55    pub context_id: ContextId,
56    pub user_id: UserId,
57    pub name: String,
58    pub kind: ContextKind,
59    pub created_at: DateTime<Utc>,
60    pub updated_at: DateTime<Utc>,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct UserContextWithStats {
65    pub context_id: ContextId,
66    pub user_id: UserId,
67    pub name: String,
68    pub kind: ContextKind,
69    pub created_at: DateTime<Utc>,
70    pub updated_at: DateTime<Utc>,
71    pub task_count: i64,
72    pub message_count: i64,
73    pub last_message_at: Option<DateTime<Utc>>,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct CreateContextRequest {
78    pub name: Option<String>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct UpdateContextRequest {
83    pub name: String,
84}