systemprompt_runtime/context/
mod.rs1use std::sync::{Arc, OnceLock};
11
12
13use systemprompt_agent::repository::A2ARepositories;
14use systemprompt_ai::repository::AiRepositories;
15use systemprompt_analytics::repository::AnalyticsRepositories;
16use systemprompt_analytics::{AnalyticsService, FingerprintRepository, GeoIpReader};
17use systemprompt_config::paths::AppPaths;
18use systemprompt_content::repository::ContentRepositories;
19use systemprompt_database::{DbPool, SchemaInstallReport, ServiceRepository};
20use systemprompt_events::EventBridgeHandle;
21use systemprompt_extension::ExtensionRegistry;
22use systemprompt_files::FileRepository;
23use systemprompt_marketplace::inventory::PublishGuard;
24use systemprompt_marketplace::managed::ManagedRepository;
25use systemprompt_marketplace::{MarketplaceCache, MarketplaceFilter};
26use systemprompt_mcp::repository::McpSessionRepository;
27use systemprompt_mcp::services::registry::RegistryService;
28use systemprompt_models::services::SystemAdmin;
29use systemprompt_models::{Config, ContentConfigRaw, ContentRouting, RouteClassifier};
30use systemprompt_oauth::repository::OAuthRepositories;
31use systemprompt_security::authz::SharedAuthzHook;
32use systemprompt_security::policy::GovernanceEngine;
33use systemprompt_traits::FileStorage;
34use systemprompt_users::{UserRepository, UserService};
35
36mod context_loaders;
37mod debug_impls;
38mod repositories;
39mod services;
40mod shutdown;
41
42pub use shutdown::ShutdownRequest;
43
44use crate::builder::AppContextBuilder;
45use crate::error::RuntimeResult;
46use crate::registry::ModuleApiRegistry;
47
48#[derive(Clone)]
53pub struct DataPlane {
54 pub database: DbPool,
55 pub analytics_service: Arc<AnalyticsService>,
56 pub fingerprint_repo: Option<Arc<FingerprintRepository>>,
57 pub user_service: Option<Arc<UserService>>,
58 pub a2a_repositories: Arc<A2ARepositories>,
59 pub content_repositories: Arc<ContentRepositories>,
60 pub oauth_repositories: Arc<OAuthRepositories>,
61 pub user_repository: Arc<UserRepository>,
62 pub service_repository: Arc<ServiceRepository>,
63 pub ai_repositories: Arc<AiRepositories>,
64 pub analytics_repositories: Arc<AnalyticsRepositories>,
65 pub file_repository: Arc<FileRepository>,
66 pub mcp_session_repository: Arc<McpSessionRepository>,
67 pub managed_repository: Arc<ManagedRepository>,
68}
69
70#[derive(Clone)]
71pub struct ConfigPlane {
72 pub config: Arc<Config>,
73 pub app_paths: Arc<AppPaths>,
74 pub content_config: Option<Arc<ContentConfigRaw>>,
75 pub route_classifier: Arc<RouteClassifier>,
76}
77
78#[derive(Clone)]
79pub struct Plugins {
80 pub extension_registry: Arc<ExtensionRegistry>,
81 pub api_registry: Arc<ModuleApiRegistry>,
82 pub mcp_registry: RegistryService,
83 pub marketplace_filter: Arc<dyn MarketplaceFilter>,
84 pub marketplace_cache: Arc<MarketplaceCache>,
85}
86
87#[derive(Clone)]
88pub struct Subsystems {
89 pub system_admin: Arc<SystemAdmin>,
90 pub authz_hook: SharedAuthzHook,
91 pub governance: Arc<GovernanceEngine>,
92 pub ai_service: Option<Arc<systemprompt_ai::AiService>>,
93 pub artifact_ingest: Arc<systemprompt_mcp::ArtifactIngest>,
94 pub schema_install: Arc<SchemaInstallReport>,
95 pub event_bridge: Arc<OnceLock<EventBridgeHandle>>,
96 pub geoip_reader: Option<GeoIpReader>,
97 pub file_storage: Arc<dyn FileStorage>,
98 pub shutdown: ShutdownRequest,
99 pub publish_guard: Arc<tokio::sync::Mutex<PublishGuard>>,
100}
101
102#[derive(Clone)]
114pub struct AppContext {
115 pub(crate) data: DataPlane,
116 pub(crate) cfg: ConfigPlane,
117 pub(crate) plugins: Plugins,
118 pub(crate) subsystems: Subsystems,
119}
120
121impl AppContext {
122 pub async fn new() -> RuntimeResult<Self> {
123 Self::builder().build().await
124 }
125
126 #[must_use]
127 pub fn builder() -> AppContextBuilder {
128 AppContextBuilder::new()
129 }
130
131 #[must_use]
132 pub const fn from_parts(
133 data: DataPlane,
134 cfg: ConfigPlane,
135 plugins: Plugins,
136 subsystems: Subsystems,
137 ) -> Self {
138 Self {
139 data,
140 cfg,
141 plugins,
142 subsystems,
143 }
144 }
145
146 pub fn load_geoip_database(
147 config: &Config,
148 show_warnings: bool,
149 ) -> Result<Option<GeoIpReader>, crate::error::RuntimeError> {
150 context_loaders::load_geoip_database(config, show_warnings)
151 }
152
153 pub fn load_content_config(
154 config: &Config,
155 app_paths: &AppPaths,
156 ) -> Option<Arc<ContentConfigRaw>> {
157 context_loaders::load_content_config(config, app_paths)
158 }
159
160 pub fn config(&self) -> &Config {
161 &self.cfg.config
162 }
163
164 pub fn content_config(&self) -> Option<&ContentConfigRaw> {
165 self.cfg.content_config.as_ref().map(AsRef::as_ref)
166 }
167
168 pub fn content_routing(&self) -> Option<Arc<dyn ContentRouting>> {
169 let concrete = Arc::clone(self.cfg.content_config.as_ref()?);
170 let routing: Arc<dyn ContentRouting> = concrete;
171 Some(routing)
172 }
173
174 pub const fn db_pool(&self) -> &DbPool {
175 &self.data.database
176 }
177
178 pub fn api_registry(&self) -> &ModuleApiRegistry {
179 &self.plugins.api_registry
180 }
181
182 pub fn extension_registry(&self) -> &ExtensionRegistry {
183 &self.plugins.extension_registry
184 }
185
186 pub fn server_address(&self) -> String {
187 format!("{}:{}", self.cfg.config.host, self.cfg.config.port)
188 }
189
190 pub const fn geoip_reader(&self) -> Option<&GeoIpReader> {
191 self.subsystems.geoip_reader.as_ref()
192 }
193
194 pub const fn file_storage(&self) -> &Arc<dyn FileStorage> {
195 &self.subsystems.file_storage
196 }
197
198 pub const fn analytics_service(&self) -> &Arc<AnalyticsService> {
199 &self.data.analytics_service
200 }
201
202 #[must_use]
203 pub fn session_usage(&self) -> systemprompt_traits::DynSessionUsageCounters {
204 self.data.analytics_repositories.sessions.owner()
205 }
206
207 #[must_use]
208 pub fn session_store(&self) -> systemprompt_traits::DynSessionStore {
209 self.data.analytics_repositories.sessions.owner()
210 }
211
212 pub fn context_materializer(&self) -> systemprompt_traits::DynContextMaterializer {
213 Arc::new(systemprompt_agent::services::ContextProviderService::new(
214 self.data.a2a_repositories.contexts.clone(),
215 ))
216 }
217
218 pub const fn route_classifier(&self) -> &Arc<RouteClassifier> {
219 &self.cfg.route_classifier
220 }
221
222 pub fn app_paths(&self) -> &AppPaths {
223 &self.cfg.app_paths
224 }
225
226 pub const fn app_paths_arc(&self) -> &Arc<AppPaths> {
227 &self.cfg.app_paths
228 }
229
230 pub fn marketplace_filter(&self) -> &Arc<dyn MarketplaceFilter> {
231 &self.plugins.marketplace_filter
232 }
233
234 pub const fn marketplace_cache(&self) -> &Arc<MarketplaceCache> {
235 &self.plugins.marketplace_cache
236 }
237
238 pub const fn event_bridge(&self) -> &Arc<OnceLock<EventBridgeHandle>> {
239 &self.subsystems.event_bridge
240 }
241
242 pub const fn publish_guard(&self) -> &Arc<tokio::sync::Mutex<PublishGuard>> {
246 &self.subsystems.publish_guard
247 }
248
249 pub fn system_admin(&self) -> &SystemAdmin {
250 &self.subsystems.system_admin
251 }
252
253 pub const fn mcp_registry(&self) -> &RegistryService {
254 &self.plugins.mcp_registry
255 }
256
257 pub const fn authz_hook(&self) -> &SharedAuthzHook {
258 &self.subsystems.authz_hook
259 }
260
261 #[must_use]
262 pub fn schema_install(&self) -> &SchemaInstallReport {
263 &self.subsystems.schema_install
264 }
265
266 pub const fn shutdown_request(&self) -> &ShutdownRequest {
267 &self.subsystems.shutdown
268 }
269
270 pub fn request_restart(&self, reason: &str) {
271 self.subsystems.shutdown.request(reason);
272 }
273}