Skip to main content

systemprompt_runtime/
lib.rs

1//! `systemprompt-runtime` — application runtime services.
2//!
3//! This crate hosts [`AppContext`], the lifecycle [`AppContextBuilder`],
4//! the inventory-driven module API and well-known route registries,
5//! per-module installation helpers, startup validation, and the typed
6//! [`RuntimeError`] / [`RuntimeResult`] error boundary used by all of
7//! the above.
8//!
9//! Public APIs return [`RuntimeResult<T>`]. [`RuntimeError`] composes
10//! upstream typed errors (`ConfigError`, `RepositoryError`,
11//! `FilesError`, `UserError`, `LoaderError`, `AnalyticsError`,
12//! `ProfileBootstrapError`, `PathError`) via `#[from]` and absorbs
13//! stringifies still-anyhow upstream calls into [`RuntimeError::Internal`].
14//!
15//! # Feature flags
16//!
17//! | Feature       | Effect                                                          |
18//! |---------------|------------------------------------------------------------------|
19//! | (default)     | Core context, builder, registries, validation                   |
20//! | `geolocation` | Enables MaxMind GeoIP2 loading via `maxminddb` and pulls in `systemprompt-analytics/geolocation` |
21
22mod builder;
23mod context;
24mod context_loaders;
25mod context_traits;
26mod database_context;
27mod error;
28mod registry;
29mod span;
30mod startup_validation;
31mod validation;
32mod wellknown;
33
34pub use builder::AppContextBuilder;
35pub use context::{AppContext, AppContextParts};
36pub use database_context::DatabaseContext;
37pub use error::{RuntimeError, RuntimeResult};
38pub use registry::{ModuleApiRegistration, ModuleApiRegistry, ModuleType, WellKnownRoute};
39pub use span::create_request_span;
40pub use startup_validation::{
41    FilesConfigValidator, StartupValidator, display_validation_report, display_validation_warnings,
42};
43pub use systemprompt_database::MigrationConfig;
44pub use validation::{validate_database_path, validate_system};
45pub use wellknown::{WellKnownMetadata, get_wellknown_metadata};
46
47pub use systemprompt_models::modules::ServiceCategory;
48
49#[macro_export]
50macro_rules! register_module_api {
51    ($module_name:literal, $category:expr, $router_fn:expr, $auth_required:expr, $module_type:expr) => {
52        inventory::submit! {
53            $crate::ModuleApiRegistration {
54                module_name: $module_name,
55                category: $category,
56                module_type: $module_type,
57                router_fn: $router_fn,
58                auth_required: $auth_required,
59            }
60        }
61    };
62    ($module_name:literal, $category:expr, $router_fn:expr, $auth_required:expr) => {
63        inventory::submit! {
64            $crate::ModuleApiRegistration {
65                module_name: $module_name,
66                category: $category,
67                module_type: $crate::ModuleType::Regular,
68                router_fn: $router_fn,
69                auth_required: $auth_required,
70            }
71        }
72    };
73}
74
75#[macro_export]
76macro_rules! register_wellknown_route {
77    ($path:literal, $handler:expr, $methods:expr, name: $name:literal, description: $desc:literal) => {
78        inventory::submit! {
79            $crate::WellKnownRoute {
80                path: $path,
81                handler_fn: $handler,
82                methods: $methods,
83            }
84        }
85
86        inventory::submit! {
87            $crate::WellKnownMetadata::new($path, $name, $desc)
88        }
89    };
90
91    ($path:literal, $handler:expr, $methods:expr) => {
92        inventory::submit! {
93            $crate::WellKnownRoute {
94                path: $path,
95                handler_fn: $handler,
96                methods: $methods,
97            }
98        }
99    };
100}