Skip to main content

systemprompt_traits/
context_provider.rs

1//! Context provider trait for accessing user contexts.
2
3use async_trait::async_trait;
4use chrono::{DateTime, Utc};
5use std::sync::Arc;
6
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum ContextProviderError {
10    #[error("Context not found: {0}")]
11    NotFound(String),
12
13    #[error("Access denied: {0}")]
14    AccessDenied(String),
15
16    #[error("Database error: {0}")]
17    Database(String),
18
19    #[error("Internal error: {0}")]
20    Internal(String),
21}
22
23#[derive(Debug, Clone)]
24pub struct ContextWithStats {
25    pub context_id: String,
26    pub user_id: String,
27    pub name: String,
28    pub created_at: DateTime<Utc>,
29    pub updated_at: DateTime<Utc>,
30    pub task_count: i64,
31    pub message_count: i64,
32    pub last_message_at: Option<DateTime<Utc>>,
33}
34
35#[async_trait]
36pub trait ContextProvider: Send + Sync {
37    async fn list_contexts_with_stats(
38        &self,
39        user_id: &str,
40    ) -> Result<Vec<ContextWithStats>, ContextProviderError>;
41
42    async fn get_context(
43        &self,
44        context_id: &str,
45        user_id: &str,
46    ) -> Result<ContextWithStats, ContextProviderError>;
47
48    async fn create_context(
49        &self,
50        user_id: &str,
51        session_id: Option<&str>,
52        name: &str,
53    ) -> Result<String, ContextProviderError>;
54
55    async fn update_context_name(
56        &self,
57        context_id: &str,
58        user_id: &str,
59        name: &str,
60    ) -> Result<(), ContextProviderError>;
61
62    async fn delete_context(
63        &self,
64        context_id: &str,
65        user_id: &str,
66    ) -> Result<(), ContextProviderError>;
67}
68
69pub type DynContextProvider = Arc<dyn ContextProvider>;