Skip to main content

briefcase_core/
lib.rs

1//! # Briefcase AI Core Library
2//!
3//! High-performance AI observability, replay, and decision tracking for Rust applications.
4//!
5//! ## Features
6//!
7//! - **AI Decision Tracking**: Capture inputs, outputs, and context for every AI decision
8//! - **Deterministic Replay**: Reproduce AI decisions exactly with full context preservation
9//! - **Comprehensive Observability**: Monitor model performance, drift, and behavior
10//! - **Enterprise Security**: Built-in data sanitization and privacy controls
11//! - **Flexible Storage**: SQLite, cloud storage, and custom backends
12//! - **Cost Management**: Track and optimize AI model usage costs
13//! - **Cross-Platform**: Works on Linux, macOS, Windows, and WebAssembly
14//!
15//! ## Quick Start
16//!
17//! ```rust
18//! use briefcase_core::*;
19//! use briefcase_core::storage::StorageBackend;
20//! use serde_json::json;
21//!
22//! # #[tokio::main]
23//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//! // Create a decision snapshot
25//! let decision = DecisionSnapshot::new("ai_function")
26//!     .add_input(Input::new("user_query", json!("Hello world"), "string"))
27//!     .add_output(Output::new("response", json!("Hello back!"), "string").with_confidence(0.95))
28//!     .with_execution_time(120.5);
29//!
30//! // Save to storage (requires storage feature)
31//! #[cfg(feature = "sqlite-storage")]
32//! {
33//!     let storage = storage::SqliteBackend::in_memory()?;
34//!     let decision_id = storage.save_decision(&decision).await?;
35//!     println!("Saved decision: {}", decision_id);
36//! }
37//! # Ok(())
38//! # }
39//! ```
40//!
41//! ## Feature Flags
42//!
43//! - `recording` - Baseline decision capture pipeline (enabled by default)
44//! - `async` - Enable async/await runtime support (enabled by default)
45//! - `storage` - Enable persistent storage APIs (enabled by default)
46//! - `sqlite-storage` - SQLite storage backend (implied by `storage`)
47//! - `chain` - Hash-chained, append-only SQLite store with signed Merkle
48//!   checkpoints (implies `sqlite-storage`)
49//! - `vcs-storage` - Version-control storage abstractions (implies `async`)
50//! - `replay` - Deterministic replay engine
51//! - `drift` - Drift monitoring utilities
52//! - `sanitize` - Built-in PII sanitization
53//! - `otel` - OpenTelemetry instrumentation helpers
54//! - `tokens` - Token counting utilities
55//! - `networking` - HTTP client support for remote services
56//! - `compression` - Reserved; currently enables nothing
57
58/// Cost calculation and management functionality
59pub mod cost;
60
61/// Drift detection and model performance monitoring
62#[cfg(feature = "drift")]
63pub mod drift;
64
65#[cfg(feature = "embedding-drift")]
66pub mod embedding_drift;
67
68/// Core data models and structures
69pub mod models;
70
71/// RFC 8785 canonical JSON, Merkle proofs and Ed25519 signing (`chain`)
72pub mod integrity;
73
74/// Token counting and estimation
75#[cfg(feature = "tokens")]
76pub mod tokens;
77
78/// Telemetry helpers for reporting usage
79#[cfg(feature = "otel")]
80pub mod telemetry;
81
82/// The default base URL for the Briefcase AI API.
83pub const DEFAULT_API_URL: &str = "http://localhost:8080";
84
85/// Decision replay and validation
86#[cfg(feature = "replay")]
87pub mod replay;
88
89/// Data sanitization and privacy controls
90#[cfg(feature = "sanitize")]
91pub mod sanitization;
92
93// Storage module only available with storage features
94#[cfg(any(feature = "sqlite-storage", feature = "vcs-storage"))]
95/// Storage backends for persisting decisions and snapshots
96pub mod storage;
97
98// Control plane abstraction for auth/authz backends (remote, local, etc.)
99#[cfg(feature = "async")]
100/// Control plane trait and implementations for client lookup
101pub mod control;
102
103// Client authentication module (requires networking for server validation)
104#[cfg(feature = "networking")]
105/// Unified client for authenticated access to the Briefcase AI platform
106pub mod client;
107
108// Re-export all public types for convenience
109pub use cost::*;
110#[cfg(feature = "drift")]
111pub use drift::*;
112pub use models::*;
113#[cfg(feature = "replay")]
114pub use replay::*;
115#[cfg(feature = "sanitize")]
116pub use sanitization::*;
117#[cfg(feature = "tokens")]
118pub use tokens::*;
119
120use thiserror::Error;
121
122#[derive(Error, Debug)]
123pub enum BriefcaseError {
124    #[cfg(any(feature = "sqlite-storage", feature = "vcs-storage"))]
125    #[error("Storage error: {0}")]
126    Storage(#[from] storage::StorageError),
127
128    #[cfg(feature = "replay")]
129    #[error("Replay error: {0}")]
130    Replay(#[from] replay::ReplayError),
131
132    #[error("Cost calculation error: {0}")]
133    Cost(#[from] cost::CostError),
134
135    #[cfg(feature = "sanitize")]
136    #[error("Sanitization error: {0}")]
137    Sanitization(#[from] sanitization::SanitizationError),
138
139    #[error("Serialization error: {0}")]
140    Serialization(#[from] serde_json::Error),
141
142    #[error("Invalid input: {0}")]
143    InvalidInput(String),
144
145    #[cfg(feature = "networking")]
146    #[error("Client error: {0}")]
147    Client(#[from] client::ClientError),
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    // Compiles in every feature set: `cost` is always built, so its error
155    // must always convert.
156    #[test]
157    fn cost_errors_convert_into_briefcase_error() {
158        let err: BriefcaseError = cost::CostError::InvalidTokenCount.into();
159        assert!(matches!(err, BriefcaseError::Cost(_)));
160    }
161}