|
| 1 | +--- |
| 2 | +title: SQL Storage |
| 3 | +description: Using SQL databases (SQLite/Turso) for persistent conversation storage in the Multi-Agent Orchestrator System |
| 4 | +--- |
| 5 | + |
| 6 | +SQL storage provides a flexible and reliable solution for storing conversation history in the Multi-Agent Orchestrator System. This implementation supports both local SQLite databases and remote Turso databases, making it suitable for various deployment scenarios from development to production. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- Persistent storage across application restarts |
| 11 | +- Support for both local and remote databases |
| 12 | +- Built-in connection pooling and retry mechanisms |
| 13 | +- Compatible with edge and serverless deployments |
| 14 | +- Transaction support for data consistency |
| 15 | +- Efficient indexing for quick data retrieval |
| 16 | + |
| 17 | +## When to Use SQL Storage |
| 18 | + |
| 19 | +- When you need a balance between simplicity and scalability |
| 20 | +- For applications requiring persistent storage without complex infrastructure |
| 21 | +- In both development and production environments |
| 22 | +- When working with edge or serverless deployments |
| 23 | +- When you need local-first development with remote deployment options |
| 24 | + |
| 25 | +## Python Package Installation |
| 26 | + |
| 27 | +To use SQL storage in your Python application, make sure to install them: |
| 28 | + |
| 29 | +```bash |
| 30 | +pip install "multi-agent-orchestrator[sql]" |
| 31 | +``` |
| 32 | + |
| 33 | +This will install the `libsql-client` package required for SQL storage functionality. |
| 34 | + |
| 35 | +## Implementation |
| 36 | + |
| 37 | +To use SQL storage in your Multi-Agent Orchestrator: |
| 38 | + |
| 39 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 40 | + |
| 41 | +<Tabs syncKey="runtime"> |
| 42 | + <TabItem label="TypeScript" icon="seti:typescript" color="blue"> |
| 43 | + ```typescript |
| 44 | + import { SqlChatStorage, MultiAgentOrchestrator } from 'multi-agent-orchestrator'; |
| 45 | + |
| 46 | + // For local SQLite database |
| 47 | + const localStorage = new SqlChatStorage('file:local.db'); |
| 48 | + await localStorage.waitForInitialization(); |
| 49 | + |
| 50 | + // For remote database |
| 51 | + const remoteStorage = new SqlChatStorage( |
| 52 | + 'libsql://your-database-url.example.com', |
| 53 | + 'your-auth-token' |
| 54 | + ); |
| 55 | + await remoteStorage.waitForInitialization(); |
| 56 | + |
| 57 | + const orchestrator = new MultiAgentOrchestrator({ |
| 58 | + storage: localStorage // or remoteStorage |
| 59 | + }); |
| 60 | + |
| 61 | + |
| 62 | + // Close the database connections when done |
| 63 | + await localStorage.close(); |
| 64 | + await remoteStorage.close(); |
| 65 | + ``` |
| 66 | + </TabItem> |
| 67 | + <TabItem label="Python" icon="seti:python"> |
| 68 | + ```python |
| 69 | + from multi_agent_orchestrator.storage import SqlChatStorage |
| 70 | + from multi_agent_orchestrator.orchestrator import MultiAgentOrchestrator |
| 71 | + |
| 72 | + # For local SQLite database |
| 73 | + local_storage = SqlChatStorage('file:local.db') |
| 74 | + |
| 75 | + # For remote Turso database |
| 76 | + remote_storage = SqlChatStorage( |
| 77 | + url='libsql://your-database-url.turso.io', |
| 78 | + auth_token='your-auth-token' |
| 79 | + ) |
| 80 | + |
| 81 | + orchestrator = MultiAgentOrchestrator(storage=local_storage) # or remote_storage |
| 82 | + ``` |
| 83 | + </TabItem> |
| 84 | +</Tabs> |
| 85 | + |
| 86 | +## Configuration |
| 87 | + |
| 88 | +### Local DB |
| 89 | +For local development, simply provide a file URL: |
| 90 | +```typescript |
| 91 | +const storage = new SqlChatStorage('file:local.db'); |
| 92 | +``` |
| 93 | + |
| 94 | +### Remote DB |
| 95 | +For production with Turso: |
| 96 | +1. Create a Turso database through their platform |
| 97 | +2. Obtain your database URL and authentication token |
| 98 | +3. Configure your storage: |
| 99 | +```typescript |
| 100 | +const storage = new SqlChatStorage( |
| 101 | + 'libsql://your-database-url.com', |
| 102 | + 'your-auth-token' |
| 103 | +); |
| 104 | +``` |
| 105 | + |
| 106 | +## Database Schema |
| 107 | + |
| 108 | +The SQL storage implementation uses the following schema: |
| 109 | + |
| 110 | +```sql |
| 111 | +CREATE TABLE conversations ( |
| 112 | + user_id TEXT NOT NULL, |
| 113 | + session_id TEXT NOT NULL, |
| 114 | + agent_id TEXT NOT NULL, |
| 115 | + message_index INTEGER NOT NULL, |
| 116 | + role TEXT NOT NULL, |
| 117 | + content TEXT NOT NULL, |
| 118 | + timestamp INTEGER NOT NULL, |
| 119 | + PRIMARY KEY (user_id, session_id, agent_id, message_index) |
| 120 | +); |
| 121 | + |
| 122 | +CREATE INDEX idx_conversations_lookup |
| 123 | +ON conversations(user_id, session_id, agent_id); |
| 124 | +``` |
| 125 | + |
| 126 | +## Considerations |
| 127 | + |
| 128 | +- Automatic table and index creation on initialization |
| 129 | +- Built-in transaction support for data consistency |
| 130 | +- Efficient query performance through proper indexing |
| 131 | +- Support for message history size limits |
| 132 | +- Automatic JSON serialization/deserialization of message content |
| 133 | + |
| 134 | +## Best Practices |
| 135 | + |
| 136 | +- Use a single instance of SqlChatStorage throughout your application |
| 137 | +- Regularly backup your database |
| 138 | +- Implement data cleanup strategies for old conversations |
| 139 | +- Monitor database size and performance |
| 140 | +- Keep your authentication tokens secure |
| 141 | +- Implement proper access controls at the application level |
| 142 | +- Close the database connections when done |
| 143 | + |
| 144 | +SQL storage provides a robust and flexible solution for managing conversation history in the Multi-Agent Orchestrator System. It offers a good balance between simplicity and features, making it suitable for both development and production environments. |
0 commit comments