Skip to main content

systemprompt_runtime/context/
mod.rs

1//! [`AppContext`] — the application-wide runtime container.
2//!
3//! Holds shared handles (config, database pool, extension registry,
4//! analytics, route classifier, etc.) cloned cheaply via [`Arc`].
5//! Constructed via [`crate::AppContextBuilder`] or [`AppContext::new`].
6//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10use std::sync::{Arc, OnceLock};
11
12use tokio::task::JoinHandle;
13
14use systemprompt_analytics::{AnalyticsService, FingerprintRepository, GeoIpReader};
15use systemprompt_database::DbPool;
16use systemprompt_extension::ExtensionRegistry;
17use systemprompt_marketplace::MarketplaceFilter;
18use systemprompt_mcp::services::registry::RegistryService;
19use systemprompt_models::services::SystemAdmin;
20use systemprompt_models::{AppPaths, Config, ContentConfigRaw, ContentRouting, RouteClassifier};
21use systemprompt_security::authz::SharedAuthzHook;
22use systemprompt_users::UserService;
23
24mod context_loaders;
25
26use crate::builder::AppContextBuilder;
27use crate::error::RuntimeResult;
28use crate::registry::ModuleApiRegistry;
29
30/// Database pool and the data-access services layered on it.
31///
32/// `fingerprint_repo` and `user_service` are `None` when the corresponding
33/// resource failed to initialise; callers must degrade gracefully.
34#[derive(Clone)]
35pub struct DataPlane {
36    pub database: DbPool,
37    pub analytics_service: Arc<AnalyticsService>,
38    pub fingerprint_repo: Option<Arc<FingerprintRepository>>,
39    pub user_service: Option<Arc<UserService>>,
40}
41
42#[derive(Clone)]
43pub struct ConfigPlane {
44    pub config: Arc<Config>,
45    pub app_paths: Arc<AppPaths>,
46    pub content_config: Option<Arc<ContentConfigRaw>>,
47    pub route_classifier: Arc<RouteClassifier>,
48}
49
50#[derive(Clone)]
51pub struct Plugins {
52    pub extension_registry: Arc<ExtensionRegistry>,
53    pub api_registry: Arc<ModuleApiRegistry>,
54    pub mcp_registry: RegistryService,
55    pub marketplace_filter: Arc<dyn MarketplaceFilter>,
56}
57
58#[derive(Clone)]
59pub struct Subsystems {
60    pub system_admin: Arc<SystemAdmin>,
61    pub authz_hook: SharedAuthzHook,
62    pub event_bridge: Arc<OnceLock<JoinHandle<()>>>,
63    pub geoip_reader: Option<GeoIpReader>,
64}
65
66/// Application-wide runtime container shared across the HTTP server, the
67/// scheduler, and CLI commands.
68///
69/// Handles are grouped into four cohesive planes ([`DataPlane`],
70/// [`ConfigPlane`], [`Plugins`], [`Subsystems`]); each field is an [`Arc`] (or
71/// an `Arc`-internal handle such as [`DbPool`]), so `clone` is a
72/// reference-count bump rather than a deep copy. Construct it via
73/// [`AppContext::builder`] (or [`AppContext::new`] for the default build);
74/// [`AppContext::from_parts`] bypasses the bootstrap and is intended for tests
75/// and embedders that assemble the planes themselves. Read individual handles
76/// through the accessor methods.
77#[derive(Clone)]
78pub struct AppContext {
79    pub(crate) data: DataPlane,
80    pub(crate) cfg: ConfigPlane,
81    pub(crate) plugins: Plugins,
82    pub(crate) subsystems: Subsystems,
83}
84
85impl std::fmt::Debug for AppContext {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        f.debug_struct("AppContext")
88            .field("config", &"Config")
89            .field("database", &"DbPool")
90            .field("api_registry", &"ModuleApiRegistry")
91            .field("extension_registry", &self.plugins.extension_registry)
92            .field("geoip_reader", &self.subsystems.geoip_reader.is_some())
93            .field("content_config", &self.cfg.content_config.is_some())
94            .field("route_classifier", &"RouteClassifier")
95            .field("analytics_service", &"AnalyticsService")
96            .field("fingerprint_repo", &self.data.fingerprint_repo.is_some())
97            .field("user_service", &self.data.user_service.is_some())
98            .field("app_paths", &"AppPaths")
99            .field("marketplace_filter", &self.plugins.marketplace_filter)
100            .field(
101                "event_bridge",
102                &self.subsystems.event_bridge.get().is_some(),
103            )
104            .field("system_admin", &self.subsystems.system_admin.username())
105            .field("mcp_registry", &"RegistryService")
106            .field("authz_hook", &"SharedAuthzHook")
107            .finish()
108    }
109}
110
111impl std::fmt::Debug for DataPlane {
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        f.debug_struct("DataPlane")
114            .field("database", &"DbPool")
115            .field("analytics_service", &"AnalyticsService")
116            .field("fingerprint_repo", &self.fingerprint_repo.is_some())
117            .field("user_service", &self.user_service.is_some())
118            .finish()
119    }
120}
121
122impl std::fmt::Debug for ConfigPlane {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        f.debug_struct("ConfigPlane")
125            .field("config", &"Config")
126            .field("app_paths", &"AppPaths")
127            .field("content_config", &self.content_config.is_some())
128            .field("route_classifier", &"RouteClassifier")
129            .finish()
130    }
131}
132
133impl std::fmt::Debug for Plugins {
134    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135        f.debug_struct("Plugins")
136            .field("extension_registry", &self.extension_registry)
137            .field("api_registry", &"ModuleApiRegistry")
138            .field("mcp_registry", &"RegistryService")
139            .field("marketplace_filter", &self.marketplace_filter)
140            .finish()
141    }
142}
143
144impl std::fmt::Debug for Subsystems {
145    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146        f.debug_struct("Subsystems")
147            .field("system_admin", &self.system_admin.username())
148            .field("authz_hook", &"SharedAuthzHook")
149            .field("event_bridge", &self.event_bridge.get().is_some())
150            .field("geoip_reader", &self.geoip_reader.is_some())
151            .finish()
152    }
153}
154
155impl AppContext {
156    pub async fn new() -> RuntimeResult<Self> {
157        Self::builder().build().await
158    }
159
160    #[must_use]
161    pub fn builder() -> AppContextBuilder {
162        AppContextBuilder::new()
163    }
164
165    /// Assembles a context directly from pre-built planes, bypassing the
166    /// [`AppContextBuilder`] bootstrap. Intended for tests and embedders that
167    /// own the construction of the individual handles.
168    #[must_use]
169    pub const fn from_parts(
170        data: DataPlane,
171        cfg: ConfigPlane,
172        plugins: Plugins,
173        subsystems: Subsystems,
174    ) -> Self {
175        Self {
176            data,
177            cfg,
178            plugins,
179            subsystems,
180        }
181    }
182
183    pub fn load_geoip_database(config: &Config, show_warnings: bool) -> Option<GeoIpReader> {
184        context_loaders::load_geoip_database(config, show_warnings)
185    }
186
187    pub fn load_content_config(
188        config: &Config,
189        app_paths: &AppPaths,
190    ) -> Option<Arc<ContentConfigRaw>> {
191        context_loaders::load_content_config(config, app_paths)
192    }
193
194    pub fn config(&self) -> &Config {
195        &self.cfg.config
196    }
197
198    pub fn content_config(&self) -> Option<&ContentConfigRaw> {
199        self.cfg.content_config.as_ref().map(AsRef::as_ref)
200    }
201
202    pub fn content_routing(&self) -> Option<Arc<dyn ContentRouting>> {
203        let concrete = Arc::clone(self.cfg.content_config.as_ref()?);
204        let routing: Arc<dyn ContentRouting> = concrete;
205        Some(routing)
206    }
207
208    pub const fn db_pool(&self) -> &DbPool {
209        &self.data.database
210    }
211
212    pub fn api_registry(&self) -> &ModuleApiRegistry {
213        &self.plugins.api_registry
214    }
215
216    pub fn extension_registry(&self) -> &ExtensionRegistry {
217        &self.plugins.extension_registry
218    }
219
220    pub fn server_address(&self) -> String {
221        format!("{}:{}", self.cfg.config.host, self.cfg.config.port)
222    }
223
224    pub const fn geoip_reader(&self) -> Option<&GeoIpReader> {
225        self.subsystems.geoip_reader.as_ref()
226    }
227
228    pub const fn analytics_service(&self) -> &Arc<AnalyticsService> {
229        &self.data.analytics_service
230    }
231
232    pub const fn route_classifier(&self) -> &Arc<RouteClassifier> {
233        &self.cfg.route_classifier
234    }
235
236    pub fn app_paths(&self) -> &AppPaths {
237        &self.cfg.app_paths
238    }
239
240    pub const fn app_paths_arc(&self) -> &Arc<AppPaths> {
241        &self.cfg.app_paths
242    }
243
244    pub fn marketplace_filter(&self) -> &Arc<dyn MarketplaceFilter> {
245        &self.plugins.marketplace_filter
246    }
247
248    pub const fn event_bridge(&self) -> &Arc<OnceLock<JoinHandle<()>>> {
249        &self.subsystems.event_bridge
250    }
251
252    pub fn system_admin(&self) -> &SystemAdmin {
253        &self.subsystems.system_admin
254    }
255
256    pub const fn mcp_registry(&self) -> &RegistryService {
257        &self.plugins.mcp_registry
258    }
259
260    pub const fn authz_hook(&self) -> &SharedAuthzHook {
261        &self.subsystems.authz_hook
262    }
263}