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//! - `vcs-storage` - Version-control storage abstractions
48//! - `replay` - Deterministic replay engine
49//! - `drift` - Drift monitoring utilities
50//! - `sanitize` - Built-in PII sanitization
51//! - `otel` - OpenTelemetry instrumentation helpers
52//! - `tokens` - Token counting utilities
53//! - `networking` - HTTP client support for remote services
54//! - `compression` - Compression helpers for storage backends
55
56/// Cost calculation and management functionality
57#[cfg(feature = "drift")]
58pub mod cost;
59
60/// Drift detection and model performance monitoring
61#[cfg(feature = "drift")]
62pub mod drift;
63
64/// Core data models and structures
65pub mod models;
66
67/// Token counting and estimation
68#[cfg(feature = "tokens")]
69pub mod tokens;
70
71/// Telemetry helpers for reporting usage
72#[cfg(feature = "otel")]
73pub mod telemetry;
74
75/// The default base URL for the Briefcase AI API.
76pub const DEFAULT_API_URL: &str = "http://localhost:8080";
77
78/// Decision replay and validation
79#[cfg(feature = "replay")]
80pub mod replay;
81
82/// Data sanitization and privacy controls
83#[cfg(feature = "sanitize")]
84pub mod sanitization;
85
86// Storage module only available with storage features
87#[cfg(any(
88    feature = "sqlite-storage",
89    feature = "vcs-storage"
90))]
91/// Storage backends for persisting decisions and snapshots
92pub mod storage;
93
94// Control plane abstraction for auth/authz backends (remote, local, etc.)
95#[cfg(feature = "async")]
96/// Control plane trait and implementations for client lookup
97pub mod control;
98
99// Client authentication module (requires networking for server validation)
100#[cfg(feature = "networking")]
101/// Unified client for authenticated access to the Briefcase AI platform
102pub mod client;
103
104// Re-export all public types for convenience
105#[cfg(feature = "drift")]
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(
122        feature = "sqlite-storage",
123        feature = "vcs-storage"
124    ))]
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    #[cfg(feature = "drift")]
133    #[error("Cost calculation error: {0}")]
134    Cost(#[from] cost::CostError),
135
136    #[cfg(feature = "sanitize")]
137    #[error("Sanitization error: {0}")]
138    Sanitization(#[from] sanitization::SanitizationError),
139
140    #[error("Serialization error: {0}")]
141    Serialization(#[from] serde_json::Error),
142
143    #[error("Invalid input: {0}")]
144    InvalidInput(String),
145
146    #[cfg(feature = "networking")]
147    #[error("Client error: {0}")]
148    Client(#[from] client::ClientError),
149}