()
| 126 | } |
| 127 | |
| 128 | private getDb(): Database.Database { |
| 129 | if (this.db) return this.db |
| 130 | |
| 131 | this.ensureDir(this.aiDataDir) |
| 132 | const dbPath = path.join(this.aiDataDir, 'conversations.db') |
| 133 | this.db = this.nativeBinding ? new Database(dbPath, { nativeBinding: this.nativeBinding }) : new Database(dbPath) |
| 134 | this.db.pragma('journal_mode = WAL') |
| 135 | |
| 136 | this.db.exec(` |
| 137 | CREATE TABLE IF NOT EXISTS ai_chat ( |
| 138 | id TEXT PRIMARY KEY, |
| 139 | session_id TEXT NOT NULL, |
| 140 | title TEXT, |
| 141 | assistant_id TEXT DEFAULT '${DEFAULT_GENERAL_ID}', |
| 142 | active_message_id TEXT, |
| 143 | created_at INTEGER NOT NULL, |
| 144 | updated_at INTEGER NOT NULL |
| 145 | ); |
| 146 | |
| 147 | CREATE TABLE IF NOT EXISTS ai_message ( |
| 148 | id TEXT PRIMARY KEY, |
| 149 | ai_chat_id TEXT NOT NULL, |
| 150 | role TEXT NOT NULL, |
| 151 | content TEXT NOT NULL, |
| 152 | timestamp INTEGER NOT NULL, |
| 153 | data_keywords TEXT, |
| 154 | data_message_count INTEGER, |
| 155 | content_blocks TEXT, |
| 156 | parent_id TEXT, |
| 157 | sibling_group_id TEXT, |
| 158 | branch_index INTEGER DEFAULT 0, |
| 159 | debug_context TEXT, |
| 160 | token_usage TEXT, |
| 161 | FOREIGN KEY(ai_chat_id) REFERENCES ai_chat(id) ON DELETE CASCADE |
| 162 | ); |
| 163 | |
| 164 | `) |
| 165 | |
| 166 | this.migrateDatabase(this.db) |
| 167 | return this.db |
| 168 | } |
| 169 | |
| 170 | private tableExists(db: Database.Database, tableName: string): boolean { |
| 171 | const row = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(tableName) as |
no test coverage detected