Interface: IStorageAdapter
Defined in: packages/agentos/src/core/storage/IStorageAdapter.ts:217
Core storage adapter interface for AgentOS persistence.
Implementations of this interface provide the actual storage mechanism (SQL database, NoSQL, in-memory, etc.) while AgentOS remains storage-agnostic.
Design Principles:
- All methods are async for non-blocking I/O
- Returns null when entities don't exist (not throwing errors)
- Uses strong typing for compile-time safety
- Supports transaction-like batch operations
- Designed for multi-user scenarios (userId scoping)
Lifecycle:
- Instantiate adapter with configuration
- Call
initialize()to set up database/schema - Use CRUD operations during runtime
- Call
close()when shutting down (optional cleanup)
Interface
IStorageAdapter
Example
// Example implementation instantiation
const storage: IStorageAdapter = new SqlStorageAdapter({
type: 'better-sqlite3',
database: './agentos.db'
});
await storage.initialize();
// Create conversation
const conversation = await storage.createConversation({
id: 'conv-123',
userId: 'user-456',
createdAt: Date.now(),
lastActivity: Date.now()
});
// Store message
await storage.storeMessage({
id: 'msg-001',
conversationId: 'conv-123',
role: 'user',
content: 'Hello!',
timestamp: Date.now()
});
// Query messages
const messages = await storage.getMessages('conv-123', { limit: 10 });
Methods
close()
close():
Promise<void>
Defined in: packages/agentos/src/core/storage/IStorageAdapter.ts:260
Closes the storage adapter and releases resources.
This should:
- Close database connections
- Flush any pending writes
- Clean up temporary resources
Call during application shutdown for graceful cleanup.
Returns
Promise<void>
Resolves when cleanup is complete
Example
process.on('SIGTERM', async () => {
await storage.close();
process.exit(0);
});
createConversation()
createConversation(
conversation):Promise<IConversation>
Defined in: packages/agentos/src/core/storage/IStorageAdapter.ts:283
Creates a new conversation record.
Parameters
conversation
The conversation object to create
Returns
Promise<IConversation>
The created conversation (may include defaults)
Throws
If conversation with same ID already exists or validation fails
Example
const conversation = await storage.createConversation({
id: uuidv4(),
userId: 'user-123',
agentId: 'agent-coding',
createdAt: Date.now(),
lastActivity: Date.now(),
title: 'New coding project'
});
deleteConversation()
deleteConversation(
conversationId):Promise<boolean>
Defined in: packages/agentos/src/core/storage/IStorageAdapter.ts:339
Deletes a conversation and all its messages.
Warning: This is a destructive operation that cannot be undone. Consider implementing soft deletes in production.
Parameters
conversationId
string
The conversation to delete
Returns
Promise<boolean>
True if deleted, false if not found
Example
const deleted = await storage.deleteConversation('conv-123');
if (deleted) {
console.log('Conversation deleted successfully');
}
deleteMessage()
deleteMessage(
messageId):Promise<boolean>
Defined in: packages/agentos/src/core/storage/IStorageAdapter.ts:453
Deletes a specific message.
Note: This does NOT update the conversation's lastActivity timestamp.
Parameters
messageId
string
The message to delete
Returns
Promise<boolean>
True if deleted, false if not found
Example
const deleted = await storage.deleteMessage('msg-456');
deleteMessagesForConversation()
deleteMessagesForConversation(
conversationId):Promise<number>
Defined in: packages/agentos/src/core/storage/IStorageAdapter.ts:469
Deletes all messages in a conversation.
Warning: Destructive operation. Consider soft deletes in production.
Parameters
conversationId
string
The conversation whose messages to delete
Returns
Promise<number>
Number of messages deleted
Example
const deletedCount = await storage.deleteMessagesForConversation('conv-123');
console.log(`Deleted ${deletedCount} messages`);