pulsedb/lib.rs
1//! # PulseDB
2//!
3//! Distributed database for agentic AI systems - collective memory for multi-agent coordination.
4//!
5//! PulseDB provides persistent storage for AI agent experiences, enabling semantic
6//! search, context retrieval, and knowledge sharing between agents. Supports native
7//! sync between instances for multi-device and client-server deployments.
8//!
9//! ## Quick Start
10//!
11//! ```rust
12//! # fn main() -> pulsedb::Result<()> {
13//! # let dir = tempfile::tempdir().unwrap();
14//! use pulsedb::{PulseDB, Config, NewExperience};
15//!
16//! // Open or create a database
17//! let db = PulseDB::open(dir.path().join("test.db"), Config::default())?;
18//!
19//! // Create a collective (isolated namespace)
20//! let collective = db.create_collective("my-project")?;
21//!
22//! // Record an experience
23//! db.record_experience(NewExperience {
24//! collective_id: collective,
25//! content: "Always validate user input before processing".to_string(),
26//! importance: 0.8,
27//! embedding: Some(vec![0.1f32; 384]),
28//! ..Default::default()
29//! })?;
30//!
31//! // Search for relevant experiences
32//! let query_embedding = vec![0.1f32; 384];
33//! let results = db.search_similar(collective, &query_embedding, 10)?;
34//!
35//! // Clean up
36//! db.close()?;
37//! # Ok(())
38//! # }
39//! ```
40//!
41//! ## Key Concepts
42//!
43//! ### Collective
44//!
45//! A **collective** is an isolated namespace for experiences, typically one per project.
46//! Each collective has its own vector index and can have different embedding dimensions.
47//!
48//! ### Experience
49//!
50//! An **experience** is a unit of learned knowledge. It contains:
51//! - Content (text description of the experience)
52//! - Embedding (vector representation for semantic search)
53//! - Metadata (type, importance, confidence, tags)
54//!
55//! ### Embedding Providers
56//!
57//! PulseDB supports two modes for embeddings:
58//!
59//! - **External** (default): You provide pre-computed embeddings from your own service
60//! (OpenAI, Cohere, etc.)
61//! - **Builtin**: PulseDB generates embeddings using a bundled ONNX model
62//! (requires `builtin-embeddings` feature)
63//!
64//! ## Distributed Sync
65//!
66//! With the `sync` feature, PulseDB instances can synchronize data across a
67//! network. See the [`sync`] module for full documentation.
68//!
69//! Key components:
70//! - `SyncManager` — Orchestrates sync lifecycle (start/stop/sync_once)
71//! - `SyncTransport` — Pluggable transport trait (HTTP, in-memory, custom)
72//! - `SyncServer` — Server-side handler for Axum consumers (`sync-http`)
73//! - `PulseDB::compact_wal()` — WAL compaction for disk space reclamation
74//!
75//! ## Thread Safety
76//!
77//! `PulseDB` is `Send + Sync` and can be shared across threads using `Arc`.
78//! The database uses MVCC for concurrent reads with exclusive write locking.
79//!
80//! ## Feature Flags
81//!
82//! | Feature | Description |
83//! |---------|-------------|
84//! | `builtin-embeddings` | Bundles ONNX runtime with all-MiniLM-L6-v2 for local embedding generation. Without this feature, you must supply pre-computed embeddings. |
85//! | `sync` | Core sync protocol: types, transport trait, in-memory transport, echo prevention guard. |
86//! | `sync-http` | HTTP sync transport via reqwest (implies `sync`). |
87//! | `sync-websocket` | WebSocket sync transport via tokio-tungstenite (implies `sync`). |
88
89#![cfg_attr(docsrs, feature(doc_cfg))]
90#![warn(missing_docs)]
91#![warn(rustdoc::missing_crate_level_docs)]
92#![deny(unsafe_op_in_unsafe_fn)]
93
94// ============================================================================
95// Module declarations
96// ============================================================================
97
98mod config;
99mod db;
100mod error;
101mod types;
102
103pub mod embedding;
104pub mod storage;
105
106// Domain modules
107mod activity;
108mod collective;
109mod experience;
110mod insight;
111mod relation;
112mod search;
113mod watch;
114
115/// SubstrateProvider async trait for agent framework integration.
116pub mod substrate;
117
118/// Native sync protocol for distributed PulseDB instances.
119///
120/// Requires the `sync` feature flag. Provides types, transport trait,
121/// and echo prevention for synchronizing data between PulseDB instances.
122#[cfg(feature = "sync")]
123#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
124pub mod sync;
125
126/// Vector index module for HNSW-based approximate nearest neighbor search.
127pub mod vector;
128
129// ============================================================================
130// Public API re-exports
131// ============================================================================
132
133// Main database interface
134pub use db::PulseDB;
135
136// Configuration
137pub use config::{
138 ActivityConfig, Config, EmbeddingDimension, EmbeddingProvider, HnswConfig, SyncMode,
139 WatchConfig,
140};
141
142// Error handling
143pub use error::{NotFoundError, PulseDBError, Result, StorageError, ValidationError};
144
145// Core types
146pub use types::{
147 AgentId, CollectiveId, Embedding, ExperienceId, InsightId, RelationId, TaskId, Timestamp,
148 UserId,
149};
150
151// Domain types
152pub use collective::{Collective, CollectiveStats};
153pub use experience::{Experience, ExperienceType, ExperienceUpdate, NewExperience, Severity};
154
155// Relations
156pub use relation::{ExperienceRelation, NewExperienceRelation, RelationDirection, RelationType};
157
158// Insights
159pub use insight::{DerivedInsight, InsightType, NewDerivedInsight};
160
161// Activities
162pub use activity::{Activity, NewActivity};
163
164// Search & Context
165pub use search::{ContextCandidates, ContextRequest, SearchFilter, SearchResult};
166
167// Watch (real-time notifications + cross-process change detection)
168pub use watch::{ChangePoller, WatchEvent, WatchEventType, WatchFilter, WatchLock, WatchStream};
169
170// Substrate (async agent framework integration)
171pub use substrate::{PulseDBSubstrate, SubstrateProvider};
172
173// Storage (for advanced users)
174pub use storage::DatabaseMetadata;
175
176// ============================================================================
177// Prelude module for convenient imports
178// ============================================================================
179
180/// Convenient imports for common PulseDB usage.
181///
182/// ```rust
183/// use pulsedb::prelude::*;
184/// ```
185pub mod prelude {
186 pub use crate::config::{Config, EmbeddingDimension, SyncMode};
187 pub use crate::db::PulseDB;
188 pub use crate::error::{PulseDBError, Result};
189 pub use crate::experience::{Experience, ExperienceType, NewExperience};
190 pub use crate::search::{ContextCandidates, ContextRequest, SearchFilter, SearchResult};
191 pub use crate::substrate::{PulseDBSubstrate, SubstrateProvider};
192 pub use crate::types::{CollectiveId, ExperienceId, Timestamp};
193 pub use crate::watch::{ChangePoller, WatchEvent, WatchEventType, WatchFilter, WatchLock};
194}