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