systemprompt_runtime/
context.rs1use crate::registry::ModuleApiRegistry;
2use anyhow::Result;
3use std::sync::Arc;
4use systemprompt_analytics::{AnalyticsService, FingerprintRepository, GeoIpReader};
5use systemprompt_database::{Database, DbPool};
6use systemprompt_extension::{Extension, ExtensionContext, ExtensionRegistry};
7use systemprompt_logging::CliService;
8use systemprompt_models::{
9 AppPaths, Config, ContentConfigRaw, ContentRouting, ProfileBootstrap, RouteClassifier,
10};
11use systemprompt_traits::{
12 AnalyticsProvider, AppContext as AppContextTrait, ConfigProvider, DatabaseHandle,
13 FingerprintProvider, UserProvider,
14};
15use systemprompt_users::UserService;
16
17#[derive(Clone)]
18pub struct AppContext {
19 config: Arc<Config>,
20 database: DbPool,
21 api_registry: Arc<ModuleApiRegistry>,
22 extension_registry: Arc<ExtensionRegistry>,
23 geoip_reader: Option<GeoIpReader>,
24 content_config: Option<Arc<ContentConfigRaw>>,
25 route_classifier: Arc<RouteClassifier>,
26 analytics_service: Arc<AnalyticsService>,
27 fingerprint_repo: Option<Arc<FingerprintRepository>>,
28 user_service: Option<Arc<UserService>>,
29}
30
31impl std::fmt::Debug for AppContext {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 f.debug_struct("AppContext")
34 .field("config", &"Config")
35 .field("database", &"DbPool")
36 .field("api_registry", &"ModuleApiRegistry")
37 .field("extension_registry", &self.extension_registry)
38 .field("geoip_reader", &self.geoip_reader.is_some())
39 .field("content_config", &self.content_config.is_some())
40 .field("route_classifier", &"RouteClassifier")
41 .field("analytics_service", &"AnalyticsService")
42 .field("fingerprint_repo", &self.fingerprint_repo.is_some())
43 .field("user_service", &self.user_service.is_some())
44 .finish()
45 }
46}
47
48impl AppContext {
49 pub async fn new() -> Result<Self> {
50 Self::builder().build().await
51 }
52
53 #[must_use]
54 pub fn builder() -> AppContextBuilder {
55 AppContextBuilder::new()
56 }
57
58 async fn new_internal(
59 extension_registry: Option<ExtensionRegistry>,
60 show_startup_warnings: bool,
61 ) -> Result<Self> {
62 let profile = ProfileBootstrap::get()?;
63 AppPaths::init(&profile.paths)?;
64 systemprompt_files::FilesConfig::init()?;
65 let config = Arc::new(Config::get()?.clone());
66 let database =
67 Arc::new(Database::from_config(&config.database_type, &config.database_url).await?);
68
69 let api_registry = Arc::new(ModuleApiRegistry::new());
70
71 let registry = extension_registry.unwrap_or_else(ExtensionRegistry::discover);
72 registry.validate()?;
73
74 let extension_registry = Arc::new(registry);
75
76 let geoip_reader = Self::load_geoip_database(&config, show_startup_warnings);
77 let content_config = Self::load_content_config(&config);
78
79 #[allow(trivial_casts)]
80 let content_routing: Option<Arc<dyn ContentRouting>> =
81 content_config.clone().map(|c| c as Arc<dyn ContentRouting>);
82
83 let route_classifier = Arc::new(RouteClassifier::new(content_routing.clone()));
84
85 let analytics_service = Arc::new(AnalyticsService::new(
86 Arc::clone(&database),
87 geoip_reader.clone(),
88 content_routing,
89 ));
90
91 let fingerprint_repo = FingerprintRepository::new(&database).ok().map(Arc::new);
92
93 let user_service = UserService::new(&database).ok().map(Arc::new);
94
95 systemprompt_logging::init_logging(Arc::clone(&database));
96
97 Ok(Self {
98 config,
99 database,
100 api_registry,
101 extension_registry,
102 geoip_reader,
103 content_config,
104 route_classifier,
105 analytics_service,
106 fingerprint_repo,
107 user_service,
108 })
109 }
110
111 fn load_geoip_database(config: &Config, show_warnings: bool) -> Option<GeoIpReader> {
112 let Some(geoip_path) = &config.geoip_database_path else {
113 if show_warnings {
114 CliService::warning(
115 "GeoIP database not configured - geographic data will not be available",
116 );
117 CliService::info(" To enable geographic data:");
118 CliService::info(" 1. Download MaxMind GeoLite2-City database from: https://dev.maxmind.com/geoip/geolite2-free-geolocation-data");
119 CliService::info(
120 " 2. Add paths.geoip_database to your profile pointing to the .mmdb file",
121 );
122 }
123 return None;
124 };
125
126 match maxminddb::Reader::open_readfile(geoip_path) {
127 Ok(reader) => Some(Arc::new(reader)),
128 Err(e) => {
129 if show_warnings {
130 CliService::warning(&format!(
131 "Could not load GeoIP database from {geoip_path}: {e}"
132 ));
133 CliService::info(
134 " Geographic data (country/region/city) will not be available.",
135 );
136 CliService::info(
137 " To fix: Ensure the path is correct and the file is a valid MaxMind \
138 .mmdb database",
139 );
140 }
141 None
142 },
143 }
144 }
145
146 fn load_content_config(config: &Config) -> Option<Arc<ContentConfigRaw>> {
147 let content_config_path = AppPaths::get()
148 .ok()?
149 .system()
150 .content_config()
151 .to_path_buf();
152
153 if !content_config_path.exists() {
154 CliService::warning(&format!(
155 "Content config not found at: {}",
156 content_config_path.display()
157 ));
158 CliService::info(" Landing page detection will not be available.");
159 return None;
160 }
161
162 let yaml_content = match std::fs::read_to_string(&content_config_path) {
163 Ok(c) => c,
164 Err(e) => {
165 CliService::warning(&format!(
166 "Could not read content config from {}: {}",
167 content_config_path.display(),
168 e
169 ));
170 CliService::info(" Landing page detection will not be available.");
171 return None;
172 },
173 };
174
175 match serde_yaml::from_str::<ContentConfigRaw>(&yaml_content) {
176 Ok(mut content_cfg) => {
177 let base_url = config.api_external_url.trim_end_matches('/');
178
179 content_cfg.metadata.structured_data.organization.url = base_url.to_string();
180
181 let logo = &content_cfg.metadata.structured_data.organization.logo;
182 if logo.starts_with('/') {
183 content_cfg.metadata.structured_data.organization.logo =
184 format!("{base_url}{logo}");
185 }
186
187 Some(Arc::new(content_cfg))
188 },
189 Err(e) => {
190 CliService::warning(&format!(
191 "Could not parse content config from {}: {}",
192 content_config_path.display(),
193 e
194 ));
195 CliService::info(" Landing page detection will not be available.");
196 None
197 },
198 }
199 }
200
201 pub fn config(&self) -> &Config {
202 &self.config
203 }
204
205 pub fn content_config(&self) -> Option<&ContentConfigRaw> {
206 self.content_config.as_ref().map(AsRef::as_ref)
207 }
208
209 pub const fn db_pool(&self) -> &DbPool {
210 &self.database
211 }
212
213 pub const fn database(&self) -> &DbPool {
214 &self.database
215 }
216
217 pub fn api_registry(&self) -> &ModuleApiRegistry {
218 &self.api_registry
219 }
220
221 pub fn extension_registry(&self) -> &ExtensionRegistry {
222 &self.extension_registry
223 }
224
225 pub fn server_address(&self) -> String {
226 format!("{}:{}", self.config.host, self.config.port)
227 }
228
229 pub fn get_provided_audiences() -> Vec<String> {
230 vec!["a2a".to_string(), "api".to_string(), "mcp".to_string()]
231 }
232
233 pub fn get_valid_audiences(_module_name: &str) -> Vec<String> {
234 Self::get_provided_audiences()
235 }
236
237 pub fn get_server_audiences(_server_name: &str, _port: u16) -> Vec<String> {
238 Self::get_provided_audiences()
239 }
240
241 pub const fn geoip_reader(&self) -> Option<&GeoIpReader> {
242 self.geoip_reader.as_ref()
243 }
244
245 pub const fn analytics_service(&self) -> &Arc<AnalyticsService> {
246 &self.analytics_service
247 }
248
249 pub const fn route_classifier(&self) -> &Arc<RouteClassifier> {
250 &self.route_classifier
251 }
252}
253
254#[allow(clippy::clone_on_ref_ptr)]
255impl AppContextTrait for AppContext {
256 fn config(&self) -> Arc<dyn ConfigProvider> {
257 self.config.clone()
258 }
259
260 fn database_handle(&self) -> Arc<dyn DatabaseHandle> {
261 self.database.clone()
262 }
263
264 fn analytics_provider(&self) -> Option<Arc<dyn AnalyticsProvider>> {
265 Some(self.analytics_service.clone())
266 }
267
268 fn fingerprint_provider(&self) -> Option<Arc<dyn FingerprintProvider>> {
269 let provider: Arc<dyn FingerprintProvider> = self.fingerprint_repo.clone()?;
270 Some(provider)
271 }
272
273 fn user_provider(&self) -> Option<Arc<dyn UserProvider>> {
274 let provider: Arc<dyn UserProvider> = self.user_service.clone()?;
275 Some(provider)
276 }
277}
278
279#[allow(clippy::clone_on_ref_ptr)]
280impl ExtensionContext for AppContext {
281 fn config(&self) -> Arc<dyn ConfigProvider> {
282 self.config.clone()
283 }
284
285 fn database(&self) -> Arc<dyn DatabaseHandle> {
286 self.database.clone()
287 }
288
289 fn get_extension(&self, id: &str) -> Option<Arc<dyn Extension>> {
290 self.extension_registry.get(id).cloned()
291 }
292}
293
294#[derive(Debug, Default)]
295pub struct AppContextBuilder {
296 extension_registry: Option<ExtensionRegistry>,
297 show_startup_warnings: bool,
298}
299
300impl AppContextBuilder {
301 #[must_use]
302 pub fn new() -> Self {
303 Self::default()
304 }
305
306 #[must_use]
307 pub fn with_extensions(mut self, registry: ExtensionRegistry) -> Self {
308 self.extension_registry = Some(registry);
309 self
310 }
311
312 #[must_use]
313 pub const fn with_startup_warnings(mut self, show: bool) -> Self {
314 self.show_startup_warnings = show;
315 self
316 }
317
318 pub async fn build(self) -> Result<AppContext> {
319 AppContext::new_internal(self.extension_registry, self.show_startup_warnings).await
320 }
321}