MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / getDb

Method getDb

packages/node-runtime/src/ai/chats.ts:128–168  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers 15

getMessageRowMethod · 0.95
getActiveMessageIdMethod · 0.95
getAllMessageRowsMethod · 0.95
getAiSchemaMethod · 0.95
executeAiSQLMethod · 0.95
createAIChatMethod · 0.95
getAIChatsMethod · 0.95
getAIChatMethod · 0.95
updateAIChatTitleMethod · 0.95
deleteAIChatMethod · 0.95
addMessageMethod · 0.95

Calls 4

ensureDirMethod · 0.95
migrateDatabaseMethod · 0.95
pragmaMethod · 0.65
execMethod · 0.65

Tested by

no test coverage detected