Author's bio: ππ Hi, I'm CryptoPatrick! I'm currently enrolled as an Undergraduate student in Mathematics, at Chalmers & the University of Gothenburg, Sweden. If you have any questions or need more info, then please join my Discord Channel: AiMath
π Important Notices
- This is a trait-based abstraction layer, not a complete database
- Requires concrete implementations (see agentsql)
- Designed for AI agent persistence needs
π€ What is AgentDB
agentdb is a unified database abstraction layer that provides a common interface for AI agent storage operations across multiple backend families (SQL, Key-Value, Graph databases). It serves as the middleware between high-level agent operations and concrete database implementations.
Built to enable database flexibility without code changes, AgentDB provides a single trait that works seamlessly across SQLite, PostgreSQL, MySQL, and future backends.
Use Cases
- Multi-Backend Support: Write agent code once, deploy on any supported backend
- Database Flexibility: Switch between SQLite, PostgreSQL, MySQL without code changes
- Type Safety: Rust's type system ensures correctness across database operations
- Future-Proof: Easy to add new database backends without changing agent code
- Testing: Mock implementations for unit testing agent logic
- Custom Backends: Implement your own backend for specialized storage needs
π· Features
agentdb provides a complete abstraction layer for database operations with type safety and flexibility:
π§ Core Traits
AgentDB Trait
- CRUD Operations: Put, get, delete, and scan key-value pairs
- Query Interface: Execute SQL queries with result sets
- Transaction Support: Optional transactional operations
- Capability Discovery: Runtime feature detection
- Async-First: All operations use async/await
Capabilities Trait
- Transaction Support: Check if transactions are available
- Index Support: Determine if indexes are supported
- Backend Family: Identify backend type (SQL, KV, Graph)
- Feature Flags: Query specific backend capabilities
Transaction Trait
- ACID Guarantees: Atomic commit and rollback operations
- Nested Transactions: Support for savepoints (backend-dependent)
- Error Handling: Comprehensive error types
πΎ Value System
- Type-Safe Values: Strong typing with
Valuewrapper - Byte-Level Storage: Efficient binary data handling
- Zero-Copy Operations: Minimize allocations where possible
- Conversion Traits: Easy conversion to/from Rust types
π Error Handling
- Unified Error Types: Single error type across all backends
- Context Preservation: Detailed error messages with context
- Result Type: Standard Rust
Result<T, AgentDbError> - Backend Errors: Wrapped backend-specific errors
π Architecture
- π Overall Architecture
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β AgentFS High-Level APIs β
β (FileSystem, KvStore, ToolRecorder) β
βββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ
β AgentDB Trait β
β β’ put(key, value) β Result<()> β
β β’ get(key) β Result<Option<Value>> β
β β’ delete(key) β Result<()> β
β β’ scan(prefix) β Result<ScanResult> β
β β’ query(sql, params) β Result<QueryResult> β
β β’ capabilities() β &dyn Capabilities β
βββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β β β
βββββββββΌβββββββ ββββββββΌβββββββ βββββββΌβββββββ
β AgentSQL β β AgentKV β β AgentGraph β
β (SQLx) β β (Future) β β (Future) β
βββββββββ¬βββββββ βββββββββββββββ ββββββββββββββ
β
βββββββββΌβββββββββ¬βββββββββββββ
β β β β
βΌ βΌ βΌ βΌ
SQLite Postgres MySQL MariaDB
- π Data Flow
ββββββββββββββββββββββββββββββββββββββββββββββββ
β Application calls AgentDB::put() β
ββββββββββββββββββββββ¬ββββββββββββββββββββββββββ
β
ββββββββββΌβββββββββ
β AgentDB Trait β
β Dispatches to β
β Implementationβ
ββββββββββ¬βββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β β β
ββββββΌββββββ ββββββββΌβββββββ ββββββΌβββββββ
β SQLite β β PostgreSQL β β MySQL β
β Backend β β Backend β β Backend β
ββββββ¬ββββββ ββββββββ¬βββββββ ββββββ¬βββββββ
β β β
βββββββββββββββββΌββββββββββββββββ
β
ββββββββββΌβββββββββ
β Actual Storage β
β (Database) β
βββββββββββββββββββ
- πΎ Trait System
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β AgentDB Trait β
β β’ Core database operations β
β β’ Required for all implementations β
ββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βββββββββββββββββ΄ββββββββββββββββ
β β
ββββββΌβββββββββββββββ ββββββββββββββΌβββββββββββ
β Capabilities β β Transaction β
β β’ Runtime flags β β β’ commit() β
β β’ Feature detect β β β’ rollback() β
βββββββββββββββββββββ βββββββββββββββββββββββββ
π How to Use
Installation
Add agentdb to your Cargo.toml:
[]
= "0.1"
# Also add a concrete implementation:
= "0.1"
Or install with cargo:
Basic Example
use ;
async
Advanced: Implementing a Custom Backend
use ;
use async_trait;
π§ͺ Examples
See the agentsql and agentfs crates for complete examples of using AgentDB in practice.
π§ͺ Testing
Run the test suite:
# Run all tests
# Run with output
π Documentation
Comprehensive documentation is available at docs.rs/agentdb, including:
- Complete API reference for all traits and types
- Guide to implementing custom backends
- Error handling best practices
- Performance optimization tips
π Author
CryptoPatrick
Keybase Verification: https://keybase.io/cryptopatrick/sigs/8epNh5h2FtIX1UNNmf8YQ-k33M8J-Md4LnAN
π£ Support
Leave a β if you think this project is cool.
π License
This project is licensed under MIT. See LICENSE for details.