| 46 | const ACTIVE_CONNECTIONS = new Map<string, ActiveConnectionContext>(); |
| 47 | |
| 48 | export class SqliteAgentRunner extends AgentRunner { |
| 49 | private db: any; |
| 50 | |
| 51 | constructor(options: SqliteAgentRunnerOptions = {}) { |
| 52 | super(); |
| 53 | const dbPath = options.dbPath ?? ":memory:"; |
| 54 | |
| 55 | if (!Database) { |
| 56 | throw new Error( |
| 57 | "better-sqlite3 is required for SqliteAgentRunner but was not found.\n" + |
| 58 | "Please install it in your project:\n" + |
| 59 | " npm install better-sqlite3\n" + |
| 60 | " or\n" + |
| 61 | " pnpm add better-sqlite3\n" + |
| 62 | " or\n" + |
| 63 | " yarn add better-sqlite3\n\n" + |
| 64 | "If you don't need persistence, use InMemoryAgentRunner instead.", |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | this.db = new Database(dbPath); |
| 69 | this.initializeSchema(); |
| 70 | } |
| 71 | |
| 72 | private initializeSchema(): void { |
| 73 | // Create the agent_runs table |
| 74 | this.db.exec(` |
| 75 | CREATE TABLE IF NOT EXISTS agent_runs ( |
| 76 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 77 | thread_id TEXT NOT NULL, |
| 78 | run_id TEXT NOT NULL UNIQUE, |
| 79 | parent_run_id TEXT, |
| 80 | events TEXT NOT NULL, |
| 81 | input TEXT NOT NULL, |
| 82 | created_at INTEGER NOT NULL, |
| 83 | version INTEGER NOT NULL |
| 84 | ) |
| 85 | `); |
| 86 | |
| 87 | // Create run_state table to track active runs |
| 88 | this.db.exec(` |
| 89 | CREATE TABLE IF NOT EXISTS run_state ( |
| 90 | thread_id TEXT PRIMARY KEY, |
| 91 | is_running INTEGER DEFAULT 0, |
| 92 | current_run_id TEXT, |
| 93 | updated_at INTEGER NOT NULL |
| 94 | ) |
| 95 | `); |
| 96 | |
| 97 | // Create indexes for efficient queries |
| 98 | this.db.exec(` |
| 99 | CREATE INDEX IF NOT EXISTS idx_thread_id ON agent_runs(thread_id); |
| 100 | CREATE INDEX IF NOT EXISTS idx_parent_run_id ON agent_runs(parent_run_id); |
| 101 | `); |
| 102 | |
| 103 | // Create schema version table |
| 104 | this.db.exec(` |
| 105 | CREATE TABLE IF NOT EXISTS schema_version ( |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…