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 installation;
29mod registry;
30mod span;
31mod startup_validation;
32mod validation;
33mod wellknown;
34
35pub use builder::AppContextBuilder;
36pub use context::{AppContext, AppContextParts};
37pub use database_context::DatabaseContext;
38pub use error::{RuntimeError, RuntimeResult};
39pub use installation::{install_module, install_module_with_db};
40pub use registry::{ModuleApiRegistration, ModuleApiRegistry, ModuleRuntime, WellKnownRoute};
41pub use span::create_request_span;
42pub use startup_validation::{
43    FilesConfigValidator, StartupValidator, display_validation_report, display_validation_warnings,
44};
45pub use validation::{validate_database_path, validate_system};
46pub use wellknown::{WellKnownMetadata, get_wellknown_metadata};
47
48pub use systemprompt_models::modules::{
49    ApiConfig, Module, ModuleDefinition, ModulePermission, ModuleSchema, ModuleSeed, ModuleType,
50    Modules, ServiceCategory,
51};
52
53#[macro_export]
54macro_rules! register_module_api {
55    ($module_name:literal, $category:expr, $router_fn:expr, $auth_required:expr, $module_type:expr) => {
56        inventory::submit! {
57            $crate::ModuleApiRegistration {
58                module_name: $module_name,
59                category: $category,
60                module_type: $module_type,
61                router_fn: $router_fn,
62                auth_required: $auth_required,
63            }
64        }
65    };
66    ($module_name:literal, $category:expr, $router_fn:expr, $auth_required:expr) => {
67        inventory::submit! {
68            $crate::ModuleApiRegistration {
69                module_name: $module_name,
70                category: $category,
71                module_type: $crate::ModuleType::Regular,
72                router_fn: $router_fn,
73                auth_required: $auth_required,
74            }
75        }
76    };
77}
78
79#[macro_export]
80macro_rules! register_wellknown_route {
81    ($path:literal, $handler:expr, $methods:expr, name: $name:literal, description: $desc:literal) => {
82        inventory::submit! {
83            $crate::WellKnownRoute {
84                path: $path,
85                handler_fn: $handler,
86                methods: $methods,
87            }
88        }
89
90        inventory::submit! {
91            $crate::WellKnownMetadata::new($path, $name, $desc)
92        }
93    };
94
95    ($path:literal, $handler:expr, $methods:expr) => {
96        inventory::submit! {
97            $crate::WellKnownRoute {
98                path: $path,
99                handler_fn: $handler,
100                methods: $methods,
101            }
102        }
103    };
104}