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/// Core data models and structures
66pub mod models;
67
68/// RFC 8785 canonical JSON, Merkle proofs and Ed25519 signing (`chain`)
69pub mod integrity;
70
71/// Token counting and estimation
72#[cfg(feature = "tokens")]
73pub mod tokens;
74
75/// Telemetry helpers for reporting usage
76#[cfg(feature = "otel")]
77pub mod telemetry;
78
79/// The default base URL for the Briefcase AI API.
80pub const DEFAULT_API_URL: &str = "http://localhost:8080";
81
82/// Decision replay and validation
83#[cfg(feature = "replay")]
84pub mod replay;
85
86/// Data sanitization and privacy controls
87#[cfg(feature = "sanitize")]
88pub mod sanitization;
89
90// Storage module only available with storage features
91#[cfg(any(feature = "sqlite-storage", feature = "vcs-storage"))]
92/// Storage backends for persisting decisions and snapshots
93pub mod storage;
94
95// Control plane abstraction for auth/authz backends (remote, local, etc.)
96#[cfg(feature = "async")]
97/// Control plane trait and implementations for client lookup
98pub mod control;
99
100// Client authentication module (requires networking for server validation)
101#[cfg(feature = "networking")]
102/// Unified client for authenticated access to the Briefcase AI platform
103pub mod client;
104
105// Re-export all public types for convenience
106pub use cost::*;
107#[cfg(feature = "drift")]
108pub use drift::*;
109pub use models::*;
110#[cfg(feature = "replay")]
111pub use replay::*;
112#[cfg(feature = "sanitize")]
113pub use sanitization::*;
114#[cfg(feature = "tokens")]
115pub use tokens::*;
116
117use thiserror::Error;
118
119#[derive(Error, Debug)]
120pub enum BriefcaseError {
121    #[cfg(any(feature = "sqlite-storage", feature = "vcs-storage"))]
122    #[error("Storage error: {0}")]
123    Storage(#[from] storage::StorageError),
124
125    #[cfg(feature = "replay")]
126    #[error("Replay error: {0}")]
127    Replay(#[from] replay::ReplayError),
128
129    #[error("Cost calculation error: {0}")]
130    Cost(#[from] cost::CostError),
131
132    #[cfg(feature = "sanitize")]
133    #[error("Sanitization error: {0}")]
134    Sanitization(#[from] sanitization::SanitizationError),
135
136    #[error("Serialization error: {0}")]
137    Serialization(#[from] serde_json::Error),
138
139    #[error("Invalid input: {0}")]
140    InvalidInput(String),
141
142    #[cfg(feature = "networking")]
143    #[error("Client error: {0}")]
144    Client(#[from] client::ClientError),
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150
151    // Compiles in every feature set: `cost` is always built, so its error
152    // must always convert.
153    #[test]
154    fn cost_errors_convert_into_briefcase_error() {
155        let err: BriefcaseError = cost::CostError::InvalidTokenCount.into();
156        assert!(matches!(err, BriefcaseError::Cost(_)));
157    }
158}