Creates a new SQL storage adapter instance.
Storage configuration
const storage = new SqlStorageAdapter({
filePath: './agentos.db',
priority: ['better-sqlite3']
});
Initializes the storage adapter and creates the database schema.
Schema created (unless enableAutoMigration is false):
conversations table with indexes on userId, agentId and lastActivitymessages table with indexes on conversationId, timestamp and roleWith messageRetentionDays set, it then deletes the messages older than that.
Must be called before any other operations.
If database connection or schema creation fails
await storage.initialize();
console.log('Storage ready!');
Creates a new conversation record.
Conversation to create
The created conversation
If conversation with same ID exists or validation fails
Retrieves a conversation by ID.
The conversation ID
The conversation or null if not found
Updates a conversation's fields.
Conversation to update
Fields to update
Updated conversation
If conversation doesn't exist
Deletes a conversation and all its messages, in one transaction.
The messages are deleted by their own statement. The table's
ON DELETE CASCADE acts only where foreign keys are enforced, and SQLite
leaves them off on a connection that does not turn them on
(https://www.sqlite.org/foreignkeys.html), as the sql.js, IndexedDB and
Capacitor adapters do.
Conversation to delete
True if deleted, false if not found
Lists conversations for a user with optional filtering.
User whose conversations to list
Optional options: { Query options
Optional limit?: numberOptional offset?: numberOptional agentArray of conversations
Stores a message and updates conversation's lastActivity.
Message to store
The stored message
If conversation doesn't exist
Retrieves a message by ID.
Message ID
The message or null
Retrieves messages for a conversation with filtering.
Conversation ID
Optional options: IMessageQueryOptionsQuery options
Array of messages
Calculates total token usage for a conversation.
Conversation to analyze
Aggregated token usage
SQL storage adapter implementation for AgentOS.
Provides full persistence for conversations and messages using a SQL database. Wraps @framers/sql-storage-adapter to provide AgentOS-specific schema and operations.
Features:
Database Schema:
conversationstable: Stores conversation metadatamessagestable: Stores individual messages with foreign key to conversationsImplements
Example