agentsql 0.2.0

Interface for agent sql persistence
Documentation

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

  • Supports SQLite, PostgreSQL, and MySQL
  • Implements the AgentDB trait from agentdb
  • Includes database migration system for schema management
  • Powered by SQLx for type-safe SQL

๐Ÿค” What is AgentSQL

agentsql is a production-ready SQL backend implementation for AI agent persistence. It provides a unified interface for SQLite, PostgreSQL, and MySQL databases through the AgentDB trait, enabling agents to store filesystems, key-value data, and tool call audit logs with a single API.

Built with SQLx for type-safe SQL, AgentSQL handles schema migrations automatically and provides seamless switching between database backends without code changes.

Use Cases

  • Development: Use SQLite for local development with zero configuration
  • Production: Deploy on PostgreSQL or MySQL for multi-agent systems
  • Cloud: Seamlessly migrate between managed database services (AWS RDS, Google Cloud SQL, Azure)
  • Testing: Fast in-memory SQLite databases for unit tests
  • Edge: Embedded SQLite for resource-constrained environments
  • Enterprise: PostgreSQL/MySQL for high-availability deployments

๐Ÿ“ท Features

agentsql provides complete SQL backend support for agent persistence with production-grade features:

๐Ÿ’พ Multi-Database Support

SQLite

  • Zero Configuration: File-based or in-memory databases
  • Single File: Entire database in one portable file
  • Fast: Ideal for development and embedded systems
  • ACID Compliant: Full transaction support
  • In-Memory Mode: Perfect for testing

PostgreSQL

  • Production Grade: Battle-tested for high-load scenarios
  • Advanced Features: JSONB, full-text search, concurrent access
  • Scalability: Handles millions of records efficiently
  • Cloud Ready: Works with AWS RDS, Google Cloud SQL, Azure Database
  • Replication: Built-in streaming replication

MySQL

  • Wide Adoption: Industry-standard database
  • Compatibility: Works with MySQL, MariaDB, and cloud variants
  • Replication: Built-in master-slave replication
  • Cloud Services: Compatible with AWS Aurora, Google Cloud SQL
  • Performance: Optimized for read-heavy workloads

๐Ÿ”ง Schema Management

  • Automatic Migrations: Schema applied on first connection
  • Multi-Statement Support: Complex migration scripts
  • Inode/Dentry Design: Unix-like filesystem structure
  • Indexes: Optimized for filesystem operations
  • Tool Call Auditing: Built-in audit trail table
  • Version Control: Schema versioning support

๐Ÿš€ Production Ready

  • Connection Pooling: Efficient connection management via SQLx
  • Async Operations: Full async/await support with Tokio
  • Error Handling: Detailed error messages with context
  • Type Safety: Compile-time SQL verification (SQLx)
  • NULL Handling: Proper handling of optional fields
  • Performance: Optimized queries with prepared statements

๐Ÿ“ Architecture

  1. ๐Ÿ› Overall Architecture
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚            AgentFS High-Level APIs              โ”‚
โ”‚   (FileSystem, KvStore, ToolRecorder)           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚             AgentDB Trait Interface             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              AgentSQL (SQLx)                    โ”‚
โ”‚  โ€ข Connection pooling                           โ”‚
โ”‚  โ€ข Query builder                                โ”‚
โ”‚  โ€ข Migration system                             โ”‚
โ”‚  โ€ข Type conversions                             โ”‚
โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚                 โ”‚                โ”‚
โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  SQLite  โ”‚   โ”‚ PostgreSQL  โ”‚  โ”‚   MySQL    โ”‚
โ”‚  Local   โ”‚   โ”‚  Production โ”‚  โ”‚   Cloud    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  1. ๐Ÿ’พ Database Schema
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           SQLite / PostgreSQL / MySQL          โ”‚
โ”‚                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚          fs_inode (File Metadata)        โ”‚ โ”‚
โ”‚  โ”‚  - ino (PK, AUTO_INCREMENT)              โ”‚ โ”‚
โ”‚  โ”‚  - mode (permissions)                    โ”‚ โ”‚
โ”‚  โ”‚  - uid, gid                              โ”‚ โ”‚
โ”‚  โ”‚  - size, atime, mtime, ctime             โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚      fs_dentry (Directory Entries)       โ”‚ โ”‚
โ”‚  โ”‚  - id (PK)                               โ”‚ โ”‚
โ”‚  โ”‚  - name                                  โ”‚ โ”‚
โ”‚  โ”‚  - parent_ino (FK โ†’ fs_inode)            โ”‚ โ”‚
โ”‚  โ”‚  - ino (FK โ†’ fs_inode)                   โ”‚ โ”‚
โ”‚  โ”‚  UNIQUE(parent_ino, name)                โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚         fs_data (File Content)           โ”‚ โ”‚
โ”‚  โ”‚  - id (PK)                               โ”‚ โ”‚
โ”‚  โ”‚  - ino (FK โ†’ fs_inode)                   โ”‚ โ”‚
โ”‚  โ”‚  - offset                                โ”‚ โ”‚
โ”‚  โ”‚  - size                                  โ”‚ โ”‚
โ”‚  โ”‚  - data (BLOB/BYTEA)                     โ”‚ โ”‚
โ”‚  โ”‚  INDEX(ino, offset)                      โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚           kv_store (Key-Value)           โ”‚ โ”‚
โ”‚  โ”‚  - key (PK)                              โ”‚ โ”‚
โ”‚  โ”‚  - value (TEXT)                          โ”‚ โ”‚
โ”‚  โ”‚  - created_at, updated_at                โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚      tool_calls (Audit Trail)            โ”‚ โ”‚
โ”‚  โ”‚  - id (PK)                               โ”‚ โ”‚
โ”‚  โ”‚  - name                                  โ”‚ โ”‚
โ”‚  โ”‚  - parameters (JSON)                     โ”‚ โ”‚
โ”‚  โ”‚  - result (JSON)                         โ”‚ โ”‚
โ”‚  โ”‚  - error                                 โ”‚ โ”‚
โ”‚  โ”‚  - status (pending/success/error)        โ”‚ โ”‚
โ”‚  โ”‚  - started_at, completed_at              โ”‚ โ”‚
โ”‚  โ”‚  - duration_ms                           โ”‚ โ”‚
โ”‚  โ”‚  INDEX(name), INDEX(started_at)          โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  1. ๐Ÿ”„ Migration Flow
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚     SqlBackend::new(config)            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Detect Backend   โ”‚
         โ”‚  SQLite/PG/MySQL  โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Load Migration   โ”‚
         โ”‚  SQL for backend  โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Execute Schema   โ”‚
         โ”‚  Multi-statement  โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Initialize Root  โ”‚
         โ”‚  inode (ino=1)    โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Ready for Use!   โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš™ How to Use

Installation

Add to your Cargo.toml:

[dependencies]
agentdb = "0.1"
agentsql = "0.1"

# Enable the backend(s) you need:
agentsql = { version = "0.1", features = ["sqlite"] }
# agentsql = { version = "0.1", features = ["postgres"] }
# agentsql = { version = "0.1", features = ["mysql"] }
# agentsql = { version = "0.1", features = ["sqlite", "postgres", "mysql"] }

Or install with cargo:

cargo add agentsql --features sqlite

Example: SQLite (Local Development)

use agentsql::SqlBackend;
use agentdb::AgentDB;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create SQLite database (file-based)
    let db = SqlBackend::sqlite("agent.db").await?;

    // Or use in-memory for testing
    let db = SqlBackend::sqlite(":memory:").await?;

    // Key-value operations
    db.put("config:theme", b"dark".to_vec().into()).await?;

    let theme = db.get("config:theme").await?.unwrap();
    println!("Theme: {}", String::from_utf8_lossy(theme.as_bytes()));

    // SQL queries
    let result = db.query(
        "SELECT * FROM fs_inode WHERE ino = 1",
        vec![]
    ).await?;

    println!("Root inode: {:?}", result.rows.first());

    Ok(())
}

Example: PostgreSQL (Production)

use agentsql::SqlBackend;
use agentdb::AgentDB;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to PostgreSQL
    let database_url = env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgres://user:pass@localhost/agentfs".to_string());

    let db = SqlBackend::postgres(database_url).await?;

    // Same API as SQLite!
    db.put("agent:status", b"running".to_vec().into()).await?;

    // Execute queries
    let result = db.query(
        "SELECT COUNT(*) as count FROM tool_calls WHERE status = 'success'",
        vec![]
    ).await?;

    Ok(())
}

Example: MySQL (Cloud Deployment)

use agentsql::SqlBackend;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to MySQL (e.g., AWS Aurora)
    let db = SqlBackend::mysql(
        "mysql://user:pass@aurora-cluster.region.rds.amazonaws.com/agentfs"
    ).await?;

    // Scan with prefix
    let sessions = db.scan("session:").await?;
    println!("Found {} active sessions", sessions.keys.len());

    Ok(())
}

๐Ÿงช Examples

See the agentfs crate for complete examples demonstrating:

  • Basic SQLite usage
  • PostgreSQL multi-agent systems
  • MySQL cloud deployments

๐Ÿงช Testing

Run the test suite:

# Run all tests (SQLite)
cargo test

# Test with PostgreSQL
cargo test --features postgres

# Test with MySQL
cargo test --features mysql

# Run with output
cargo test -- --nocapture

๐Ÿ“š Documentation

Comprehensive documentation is available at docs.rs/agentsql, including:

  • API reference for SqlBackend
  • Migration system details
  • Database-specific configuration
  • Performance tuning guides
  • Connection pooling best practices

๐Ÿ–Š 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.