Skip to main content

briefcase_ai/
lib.rs

1//! # Briefcase AI Python Bindings
2//!
3//! Python FFI bindings for the Briefcase AI core library, providing high-performance
4//! AI observability, replay, and decision tracking for Python applications.
5//!
6//! ## Features
7//!
8//! - **AI Decision Tracking**: Capture inputs, outputs, and context for every AI decision
9//! - **Deterministic Replay**: Reproduce AI decisions exactly with full context preservation
10//! - **Cost Management**: Track and optimize AI model usage costs
11//! - **Drift Detection**: Monitor model performance and behavior changes
12//! - **Data Sanitization**: Built-in privacy controls for sensitive data
13//! - **Multiple Storage Backends**: SQLite, cloud storage, and custom backends
14//!
15//! ## Installation
16//!
17//! ```bash
18//! pip install briefcase-ai
19//! ```
20//!
21//! ## Usage
22//!
23//! ```python
24//! import briefcase_ai
25//!
26//! # Initialize the library
27//! briefcase_ai.init()
28//!
29//! # Create a decision snapshot
30//! decision = briefcase_ai.DecisionSnapshot("ai_function")
31//! decision.add_input(briefcase_ai.Input("query", "Hello world", "string"))
32//! decision.add_output(briefcase_ai.Output("response", "Hello back!", "string"))
33//!
34//! # Save to storage
35//! storage = briefcase_ai.SqliteBackend.in_memory()
36//! decision_id = storage.save_decision(decision)
37//! ```
38
39use pyo3::prelude::*;
40use std::ffi::CString;
41
42// Re-export core types with Python wrappers
43mod client;
44mod cost;
45mod drift;
46mod models;
47mod replay;
48mod runtime;
49mod sanitization;
50mod storage;
51
52use cost::*;
53use drift::*;
54use models::*;
55use replay::*;
56use runtime::{init_runtime, RuntimeConfig};
57use sanitization::*;
58use storage::*;
59
60/// Initialize the Briefcase AI library with default configuration
61#[pyfunction]
62fn init(py: Python) -> PyResult<()> {
63    let config = RuntimeConfig::default();
64    init_runtime(config)?;
65    let import_code = CString::new("import atexit")?;
66    let register_code = CString::new("atexit.register(_shutdown_runtime)")?;
67    py.run(&import_code, None, None)?;
68    py.run(&register_code, None, None)?;
69    Ok(())
70}
71
72/// Initialize with custom configuration
73#[pyfunction]
74#[pyo3(signature = (worker_threads = 2))]
75fn init_with_config(worker_threads: usize) -> PyResult<()> {
76    let config = RuntimeConfig { worker_threads };
77    init_runtime(config)
78}
79
80/// Check if the runtime is initialized
81#[pyfunction]
82fn is_initialized() -> bool {
83    runtime::is_initialized()
84}
85
86/// Internal function to shutdown the runtime (called by atexit)
87#[pyfunction]
88fn _shutdown_runtime() -> PyResult<()> {
89    runtime::shutdown_runtime()
90}
91
92/// Briefcase AI Python module
93#[pymodule]
94fn _core(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
95    // Core data models
96    m.add_class::<PyInput>()?;
97    m.add_class::<PyOutput>()?;
98    m.add_class::<PyModelParameters>()?;
99    m.add_class::<PyExecutionContext>()?;
100    m.add_class::<PyDecisionSnapshot>()?;
101    m.add_class::<PySnapshot>()?;
102
103    // Storage backends
104    m.add_class::<PySqliteBackend>()?;
105    m.add_class::<PyLakeFSBackend>()?;
106
107    // Core functionality
108    m.add_class::<PyDriftCalculator>()?;
109    m.add_class::<PyCostCalculator>()?;
110    m.add_class::<PySanitizer>()?;
111    m.add_class::<PyReplayEngine>()?;
112
113    // Query types
114    m.add_class::<PySnapshotQuery>()?;
115
116    // Authenticated client
117    m.add_class::<client::PyBriefcaseClient>()?;
118    m.add_class::<client::PyValidatedClient>()?;
119
120    // Runtime initialization functions
121    m.add_function(wrap_pyfunction!(init, m)?)?;
122    m.add_function(wrap_pyfunction!(init_with_config, m)?)?;
123    m.add_function(wrap_pyfunction!(is_initialized, m)?)?;
124    m.add_function(wrap_pyfunction!(_shutdown_runtime, m)?)?;
125
126    // Add module metadata
127    m.add("__version__", "2.1.27")?;
128    m.add("__author__", "Briefcase AI Team")?;
129
130    Ok(())
131}