Skip to main content

systemprompt_extension/
capabilities.rs

1use std::sync::Arc;
2
3use systemprompt_traits::{ConfigProvider, DatabaseHandle};
4
5use crate::types::ExtensionType;
6
7pub trait HasConfig: Send + Sync {
8    type Config: ConfigProvider;
9
10    fn config(&self) -> &Self::Config;
11}
12
13pub trait HasDatabase: Send + Sync {
14    type Database: DatabaseHandle;
15
16    fn database(&self) -> &Self::Database;
17}
18
19pub trait HasExtension<E: ExtensionType>: Send + Sync {
20    fn extension(&self) -> &E;
21}
22
23#[cfg(feature = "web")]
24pub trait HasHttpClient: Send + Sync {
25    fn http_client(&self) -> &reqwest::Client;
26}
27
28pub trait HasEventBus: Send + Sync {
29    type Publisher: systemprompt_traits::UserEventPublisher + Send + Sync;
30
31    fn event_bus(&self) -> &Self::Publisher;
32}
33
34pub trait FullContext: HasConfig + HasDatabase + HasEventBus {}
35
36impl<T: HasConfig + HasDatabase + HasEventBus> FullContext for T {}
37
38#[derive(Debug)]
39pub struct CapabilityContext<C, D, E> {
40    config: Arc<C>,
41    database: Arc<D>,
42    event_bus: Arc<E>,
43}
44
45impl<C, D, E> CapabilityContext<C, D, E>
46where
47    C: ConfigProvider,
48    D: DatabaseHandle,
49    E: systemprompt_traits::UserEventPublisher + Send + Sync,
50{
51    #[must_use]
52    pub const fn new(config: Arc<C>, database: Arc<D>, event_bus: Arc<E>) -> Self {
53        Self {
54            config,
55            database,
56            event_bus,
57        }
58    }
59}
60
61impl<C, D, E> HasConfig for CapabilityContext<C, D, E>
62where
63    C: ConfigProvider + Send + Sync,
64    D: Send + Sync,
65    E: Send + Sync,
66{
67    type Config = C;
68
69    fn config(&self) -> &Self::Config {
70        &self.config
71    }
72}
73
74impl<C, D, E> HasDatabase for CapabilityContext<C, D, E>
75where
76    C: Send + Sync,
77    D: DatabaseHandle + Send + Sync,
78    E: Send + Sync,
79{
80    type Database = D;
81
82    fn database(&self) -> &Self::Database {
83        &self.database
84    }
85}
86
87impl<C, D, E> HasEventBus for CapabilityContext<C, D, E>
88where
89    C: Send + Sync,
90    D: Send + Sync,
91    E: systemprompt_traits::UserEventPublisher + Send + Sync,
92{
93    type Publisher = E;
94
95    fn event_bus(&self) -> &Self::Publisher {
96        &self.event_bus
97    }
98}