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
7use std::sync::{Arc, OnceLock};
8
9use tokio::task::JoinHandle;
10
11use systemprompt_analytics::{AnalyticsService, FingerprintRepository, GeoIpReader};
12use systemprompt_database::DbPool;
13use systemprompt_extension::ExtensionRegistry;
14use systemprompt_marketplace::MarketplaceFilter;
15use systemprompt_mcp::services::registry::RegistryService;
16use systemprompt_models::services::SystemAdmin;
17use systemprompt_models::{AppPaths, Config, ContentConfigRaw, ContentRouting, RouteClassifier};
18use systemprompt_security::authz::SharedAuthzHook;
19use systemprompt_users::UserService;
20
21mod context_loaders;
22
23use crate::builder::AppContextBuilder;
24use crate::error::RuntimeResult;
25use crate::registry::ModuleApiRegistry;
26
27/// Application-wide runtime container shared across the HTTP server, the
28/// scheduler, and CLI commands.
29///
30/// Every field is an [`Arc`] (or an `Arc`-internal handle such as [`DbPool`]),
31/// so `clone` is a reference-count bump rather than a deep copy; the type is
32/// designed to be cloned freely into request handlers, jobs, and spawned
33/// tasks. Construct it via [`AppContext::builder`] (or [`AppContext::new`] for
34/// the default build); [`AppContext::from_parts`] bypasses the bootstrap and
35/// is intended for tests and embedders that assemble the parts themselves.
36///
37/// Some handles are optional: [`geoip_reader`](Self::geoip_reader),
38/// `content_config`, `fingerprint_repo`, and `user_service` are `None` when
39/// the corresponding resource is absent or failed to initialise, and callers
40/// must degrade gracefully rather than assume presence.
41#[derive(Clone)]
42pub struct AppContext {
43    pub(crate) config: Arc<Config>,
44    pub(crate) database: DbPool,
45    pub(crate) api_registry: Arc<ModuleApiRegistry>,
46    pub(crate) extension_registry: Arc<ExtensionRegistry>,
47    pub(crate) geoip_reader: Option<GeoIpReader>,
48    pub(crate) content_config: Option<Arc<ContentConfigRaw>>,
49    pub(crate) route_classifier: Arc<RouteClassifier>,
50    pub(crate) analytics_service: Arc<AnalyticsService>,
51    pub(crate) fingerprint_repo: Option<Arc<FingerprintRepository>>,
52    pub(crate) user_service: Option<Arc<UserService>>,
53    pub(crate) app_paths: Arc<AppPaths>,
54    pub(crate) marketplace_filter: Arc<dyn MarketplaceFilter>,
55    pub(crate) event_bridge: Arc<OnceLock<JoinHandle<()>>>,
56    pub(crate) system_admin: Arc<SystemAdmin>,
57    pub(crate) mcp_registry: RegistryService,
58    pub(crate) authz_hook: SharedAuthzHook,
59}
60
61impl std::fmt::Debug for AppContext {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.debug_struct("AppContext")
64            .field("config", &"Config")
65            .field("database", &"DbPool")
66            .field("api_registry", &"ModuleApiRegistry")
67            .field("extension_registry", &self.extension_registry)
68            .field("geoip_reader", &self.geoip_reader.is_some())
69            .field("content_config", &self.content_config.is_some())
70            .field("route_classifier", &"RouteClassifier")
71            .field("analytics_service", &"AnalyticsService")
72            .field("fingerprint_repo", &self.fingerprint_repo.is_some())
73            .field("user_service", &self.user_service.is_some())
74            .field("app_paths", &"AppPaths")
75            .field("marketplace_filter", &self.marketplace_filter)
76            .field("event_bridge", &self.event_bridge.get().is_some())
77            .field("system_admin", &self.system_admin.username())
78            .field("mcp_registry", &"RegistryService")
79            .field("authz_hook", &"SharedAuthzHook")
80            .finish()
81    }
82}
83
84/// Owned constructor inputs for [`AppContext::from_parts`].
85///
86/// Exposes every field of [`AppContext`] as a public, movable value so an
87/// embedder or test can assemble a context without running the full
88/// [`AppContextBuilder`] bootstrap.
89#[derive(Debug)]
90pub struct AppContextParts {
91    pub config: Arc<Config>,
92    pub database: DbPool,
93    pub api_registry: Arc<ModuleApiRegistry>,
94    pub extension_registry: Arc<ExtensionRegistry>,
95    pub geoip_reader: Option<GeoIpReader>,
96    pub content_config: Option<Arc<ContentConfigRaw>>,
97    pub route_classifier: Arc<RouteClassifier>,
98    pub analytics_service: Arc<AnalyticsService>,
99    pub fingerprint_repo: Option<Arc<FingerprintRepository>>,
100    pub user_service: Option<Arc<UserService>>,
101    pub app_paths: Arc<AppPaths>,
102    pub marketplace_filter: Arc<dyn MarketplaceFilter>,
103    pub event_bridge: Arc<OnceLock<JoinHandle<()>>>,
104    pub system_admin: Arc<SystemAdmin>,
105    pub mcp_registry: RegistryService,
106    pub authz_hook: SharedAuthzHook,
107}
108
109impl AppContext {
110    /// Builds a context with default settings: schema installation off,
111    /// extensions discovered via inventory, and the inventory-registered
112    /// marketplace filter. Equivalent to `Self::builder().build().await`.
113    pub async fn new() -> RuntimeResult<Self> {
114        Self::builder().build().await
115    }
116
117    #[must_use]
118    pub fn builder() -> AppContextBuilder {
119        AppContextBuilder::new()
120    }
121
122    /// Assembles a context directly from pre-built parts, bypassing the
123    /// [`AppContextBuilder`] bootstrap. Intended for tests and embedders that
124    /// own the construction of the individual handles.
125    pub fn from_parts(parts: AppContextParts) -> Self {
126        Self {
127            config: parts.config,
128            database: parts.database,
129            api_registry: parts.api_registry,
130            extension_registry: parts.extension_registry,
131            geoip_reader: parts.geoip_reader,
132            content_config: parts.content_config,
133            route_classifier: parts.route_classifier,
134            analytics_service: parts.analytics_service,
135            fingerprint_repo: parts.fingerprint_repo,
136            user_service: parts.user_service,
137            app_paths: parts.app_paths,
138            marketplace_filter: parts.marketplace_filter,
139            event_bridge: parts.event_bridge,
140            system_admin: parts.system_admin,
141            mcp_registry: parts.mcp_registry,
142            authz_hook: parts.authz_hook,
143        }
144    }
145
146    pub fn load_geoip_database(config: &Config, show_warnings: bool) -> Option<GeoIpReader> {
147        context_loaders::load_geoip_database(config, show_warnings)
148    }
149
150    pub fn load_content_config(
151        config: &Config,
152        app_paths: &AppPaths,
153    ) -> Option<Arc<ContentConfigRaw>> {
154        context_loaders::load_content_config(config, app_paths)
155    }
156
157    pub fn config(&self) -> &Config {
158        &self.config
159    }
160
161    pub fn content_config(&self) -> Option<&ContentConfigRaw> {
162        self.content_config.as_ref().map(AsRef::as_ref)
163    }
164
165    pub fn content_routing(&self) -> Option<Arc<dyn ContentRouting>> {
166        let concrete = Arc::clone(self.content_config.as_ref()?);
167        let routing: Arc<dyn ContentRouting> = concrete;
168        Some(routing)
169    }
170
171    pub const fn db_pool(&self) -> &DbPool {
172        &self.database
173    }
174
175    pub fn api_registry(&self) -> &ModuleApiRegistry {
176        &self.api_registry
177    }
178
179    pub fn extension_registry(&self) -> &ExtensionRegistry {
180        &self.extension_registry
181    }
182
183    pub fn server_address(&self) -> String {
184        format!("{}:{}", self.config.host, self.config.port)
185    }
186
187    pub const fn geoip_reader(&self) -> Option<&GeoIpReader> {
188        self.geoip_reader.as_ref()
189    }
190
191    pub const fn analytics_service(&self) -> &Arc<AnalyticsService> {
192        &self.analytics_service
193    }
194
195    pub const fn route_classifier(&self) -> &Arc<RouteClassifier> {
196        &self.route_classifier
197    }
198
199    pub fn app_paths(&self) -> &AppPaths {
200        &self.app_paths
201    }
202
203    pub const fn app_paths_arc(&self) -> &Arc<AppPaths> {
204        &self.app_paths
205    }
206
207    pub fn marketplace_filter(&self) -> &Arc<dyn MarketplaceFilter> {
208        &self.marketplace_filter
209    }
210
211    pub const fn event_bridge(&self) -> &Arc<OnceLock<JoinHandle<()>>> {
212        &self.event_bridge
213    }
214
215    pub fn system_admin(&self) -> &SystemAdmin {
216        &self.system_admin
217    }
218
219    pub const fn mcp_registry(&self) -> &RegistryService {
220        &self.mcp_registry
221    }
222
223    pub const fn authz_hook(&self) -> &SharedAuthzHook {
224        &self.authz_hook
225    }
226}