-
Notifications
You must be signed in to change notification settings - Fork 794
Expand file tree
/
Copy pathsqliteStore.ts
More file actions
513 lines (454 loc) · 16.4 KB
/
sqliteStore.ts
File metadata and controls
513 lines (454 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import { app } from 'electron';
import { EventEmitter } from 'events';
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import initSqlJs, { Database, SqlJsStatic } from 'sql.js';
import { DB_FILENAME } from './appConstants';
type ChangePayload<T = unknown> = {
key: string;
newValue: T | undefined;
oldValue: T | undefined;
};
const USER_MEMORIES_MIGRATION_KEY = 'userMemories.migration.v1.completed';
// Pre-read the sql.js WASM binary from disk.
// Using fs.readFileSync (which handles non-ASCII paths via Windows wide-char APIs)
// and passing the buffer directly to initSqlJs bypasses Emscripten's file loading,
// which can fail or hang when the install path contains Chinese characters on Windows.
function loadWasmBinary(): ArrayBuffer {
const wasmPath = app.isPackaged
? path.join(
process.resourcesPath,
'app.asar.unpacked/node_modules/sql.js/dist/sql-wasm.wasm'
)
: path.join(app.getAppPath(), 'node_modules/sql.js/dist/sql-wasm.wasm');
const buf = fs.readFileSync(wasmPath);
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}
export class SqliteStore {
private db: Database;
private dbPath: string;
private emitter = new EventEmitter();
private static sqlPromise: Promise<SqlJsStatic> | null = null;
private constructor(db: Database, dbPath: string) {
this.db = db;
this.dbPath = dbPath;
}
static async create(userDataPath?: string): Promise<SqliteStore> {
const basePath = userDataPath ?? app.getPath('userData');
const dbPath = path.join(basePath, DB_FILENAME);
// Initialize SQL.js with WASM file path (cached promise for reuse)
if (!SqliteStore.sqlPromise) {
const wasmBinary = loadWasmBinary();
SqliteStore.sqlPromise = initSqlJs({
wasmBinary,
});
}
const SQL = await SqliteStore.sqlPromise;
// Load existing database or create new one
let db: Database;
if (fs.existsSync(dbPath)) {
const buffer = fs.readFileSync(dbPath);
db = new SQL.Database(buffer);
} else {
db = new SQL.Database();
}
const store = new SqliteStore(db, dbPath);
store.initializeTables(basePath);
return store;
}
private initializeTables(basePath: string) {
this.db.run(`
CREATE TABLE IF NOT EXISTS kv (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
`);
// Create cowork tables
this.db.run(`
CREATE TABLE IF NOT EXISTS cowork_sessions (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
claude_session_id TEXT,
status TEXT NOT NULL DEFAULT 'idle',
pinned INTEGER NOT NULL DEFAULT 0,
cwd TEXT NOT NULL,
system_prompt TEXT NOT NULL DEFAULT '',
execution_mode TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
`);
this.db.run(`
CREATE TABLE IF NOT EXISTS cowork_messages (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
type TEXT NOT NULL,
content TEXT NOT NULL,
metadata TEXT,
created_at INTEGER NOT NULL,
sequence INTEGER,
FOREIGN KEY (session_id) REFERENCES cowork_sessions(id) ON DELETE CASCADE
);
`);
this.db.run(`
CREATE INDEX IF NOT EXISTS idx_cowork_messages_session_id ON cowork_messages(session_id);
`);
this.db.run(`
CREATE TABLE IF NOT EXISTS cowork_config (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
`);
this.db.run(`
CREATE TABLE IF NOT EXISTS user_memories (
id TEXT PRIMARY KEY,
text TEXT NOT NULL,
fingerprint TEXT NOT NULL,
confidence REAL NOT NULL DEFAULT 0.75,
is_explicit INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'created',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
last_used_at INTEGER
);
`);
this.db.run(`
CREATE TABLE IF NOT EXISTS user_memory_sources (
id TEXT PRIMARY KEY,
memory_id TEXT NOT NULL,
session_id TEXT,
message_id TEXT,
role TEXT NOT NULL DEFAULT 'system',
is_active INTEGER NOT NULL DEFAULT 1,
created_at INTEGER NOT NULL,
FOREIGN KEY (memory_id) REFERENCES user_memories(id) ON DELETE CASCADE
);
`);
this.db.run(`
CREATE INDEX IF NOT EXISTS idx_user_memories_status_updated_at
ON user_memories(status, updated_at DESC);
`);
this.db.run(`
CREATE INDEX IF NOT EXISTS idx_user_memories_fingerprint
ON user_memories(fingerprint);
`);
this.db.run(`
CREATE INDEX IF NOT EXISTS idx_user_memory_sources_session_id
ON user_memory_sources(session_id, is_active);
`);
this.db.run(`
CREATE INDEX IF NOT EXISTS idx_user_memory_sources_memory_id
ON user_memory_sources(memory_id, is_active);
`);
// Create agents table
this.db.run(`
CREATE TABLE IF NOT EXISTS agents (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
system_prompt TEXT NOT NULL DEFAULT '',
identity TEXT NOT NULL DEFAULT '',
model TEXT NOT NULL DEFAULT '',
icon TEXT NOT NULL DEFAULT '',
skill_ids TEXT NOT NULL DEFAULT '[]',
enabled INTEGER NOT NULL DEFAULT 1,
is_default INTEGER NOT NULL DEFAULT 0,
source TEXT NOT NULL DEFAULT 'custom',
preset_id TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
`);
// Create MCP servers table
this.db.run(`
CREATE TABLE IF NOT EXISTS mcp_servers (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
description TEXT NOT NULL DEFAULT '',
enabled INTEGER NOT NULL DEFAULT 1,
transport_type TEXT NOT NULL DEFAULT 'stdio',
config_json TEXT NOT NULL DEFAULT '{}',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
`);
// Migrations - safely add columns if they don't exist
try {
// Check if execution_mode column exists
const colsResult = this.db.exec("PRAGMA table_info(cowork_sessions);");
const columns = colsResult[0]?.values.map((row) => row[1]) || [];
if (!columns.includes('execution_mode')) {
this.db.run('ALTER TABLE cowork_sessions ADD COLUMN execution_mode TEXT;');
this.save();
}
if (!columns.includes('pinned')) {
this.db.run('ALTER TABLE cowork_sessions ADD COLUMN pinned INTEGER NOT NULL DEFAULT 0;');
this.save();
}
if (!columns.includes('active_skill_ids')) {
this.db.run('ALTER TABLE cowork_sessions ADD COLUMN active_skill_ids TEXT;');
this.save();
}
// Migration: Add sequence column to cowork_messages
const msgColsResult = this.db.exec("PRAGMA table_info(cowork_messages);");
const msgColumns = msgColsResult[0]?.values.map((row) => row[1]) || [];
if (!msgColumns.includes('sequence')) {
this.db.run('ALTER TABLE cowork_messages ADD COLUMN sequence INTEGER');
// 为现有消息按 created_at 和 ROWID 分配序列号
this.db.run(`
WITH numbered AS (
SELECT id, ROW_NUMBER() OVER (
PARTITION BY session_id
ORDER BY created_at ASC, ROWID ASC
) as seq
FROM cowork_messages
)
UPDATE cowork_messages
SET sequence = (SELECT seq FROM numbered WHERE numbered.id = cowork_messages.id)
`);
this.save();
}
} catch {
// Column already exists or migration not needed.
}
try {
this.db.run('UPDATE cowork_sessions SET pinned = 0 WHERE pinned IS NULL;');
} catch {
// Column might not exist yet.
}
// Migration: Add agent_id column to cowork_sessions
try {
const sessionCols = this.db.exec("PRAGMA table_info(cowork_sessions);");
const sessionColNames = sessionCols[0]?.values.map((row) => row[1]) || [];
if (!sessionColNames.includes('agent_id')) {
this.db.run("ALTER TABLE cowork_sessions ADD COLUMN agent_id TEXT NOT NULL DEFAULT 'main';");
this.save();
}
} catch {
// Column already exists or migration not needed.
}
// Migration: Add hidden column to cowork_sessions
try {
const sessionCols2 = this.db.exec("PRAGMA table_info(cowork_sessions);");
const sessionColNames2 = sessionCols2[0]?.values.map((row) => row[1]) || [];
if (!sessionColNames2.includes('hidden')) {
this.db.run('ALTER TABLE cowork_sessions ADD COLUMN hidden INTEGER NOT NULL DEFAULT 0;');
this.db.run("UPDATE cowork_sessions SET hidden = 1 WHERE title = '[OpenClaw]';");
this.save();
}
} catch {
// Column already exists or migration not needed.
}
// Migration: Ensure default 'main' agent exists
try {
const mainAgent = this.db.exec("SELECT id FROM agents WHERE id = 'main'");
if (!mainAgent[0]?.values?.length) {
const now = Date.now();
// Read existing systemPrompt from cowork_config to inherit into main agent
let existingSystemPrompt = '';
try {
const spRow = this.db.exec("SELECT value FROM cowork_config WHERE key = 'systemPrompt'");
if (spRow[0]?.values?.[0]?.[0]) {
existingSystemPrompt = String(spRow[0].values[0][0]);
}
} catch {
// No existing systemPrompt
}
this.db.run(`
INSERT INTO agents (id, name, description, system_prompt, identity, model, icon, skill_ids, enabled, is_default, source, preset_id, created_at, updated_at)
VALUES ('main', 'main', '', ?, '', '', '', '[]', 1, 1, 'custom', '', ?, ?)
`, [existingSystemPrompt, now, now]);
this.save();
}
} catch (error) {
console.warn('Failed to ensure main agent:', error);
}
try {
this.db.run(`UPDATE cowork_sessions SET execution_mode = 'local' WHERE execution_mode = 'container';`);
this.db.run(`
UPDATE cowork_config
SET value = 'local'
WHERE key = 'executionMode' AND value = 'container';
`);
} catch (error) {
console.warn('Failed to migrate cowork execution mode:', error);
}
this.migrateLegacyMemoryFileToUserMemories();
this.migrateFromElectronStore(basePath);
this.save();
}
save() {
const data = this.db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(this.dbPath, buffer);
}
onDidChange<T = unknown>(key: string, callback: (newValue: T | undefined, oldValue: T | undefined) => void) {
const handler = (payload: ChangePayload<T>) => {
if (payload.key !== key) return;
callback(payload.newValue, payload.oldValue);
};
this.emitter.on('change', handler);
return () => this.emitter.off('change', handler);
}
get<T = unknown>(key: string): T | undefined {
const result = this.db.exec('SELECT value FROM kv WHERE key = ?', [key]);
if (!result[0]?.values[0]) return undefined;
const value = result[0].values[0][0] as string;
try {
return JSON.parse(value) as T;
} catch (error) {
console.warn(`Failed to parse store value for ${key}`, error);
return undefined;
}
}
set<T = unknown>(key: string, value: T): void {
const oldValue = this.get<T>(key);
const now = Date.now();
this.db.run(`
INSERT INTO kv (key, value, updated_at)
VALUES (?, ?, ?)
ON CONFLICT(key) DO UPDATE SET
value = excluded.value,
updated_at = excluded.updated_at
`, [key, JSON.stringify(value), now]);
this.save();
this.emitter.emit('change', { key, newValue: value, oldValue } as ChangePayload<T>);
}
delete(key: string): void {
const oldValue = this.get(key);
this.db.run('DELETE FROM kv WHERE key = ?', [key]);
this.save();
this.emitter.emit('change', { key, newValue: undefined, oldValue } as ChangePayload);
}
// Expose database for cowork operations
getDatabase(): Database {
return this.db;
}
// Expose save method for external use (e.g., CoworkStore)
getSaveFunction(): () => void {
return () => this.save();
}
private tryReadLegacyMemoryText(): string {
const candidates = [
path.join(process.cwd(), 'MEMORY.md'),
path.join(app.getAppPath(), 'MEMORY.md'),
path.join(process.cwd(), 'memory.md'),
path.join(app.getAppPath(), 'memory.md'),
];
for (const candidate of candidates) {
try {
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
return fs.readFileSync(candidate, 'utf8');
}
} catch {
// Skip unreadable candidates.
}
}
return '';
}
private parseLegacyMemoryEntries(raw: string): string[] {
const normalized = raw.replace(/```[\s\S]*?```/g, ' ');
const lines = normalized.split(/\r?\n/);
const entries: string[] = [];
const seen = new Set<string>();
for (const line of lines) {
const match = line.trim().match(/^-+\s*(?:\[[^\]]+\]\s*)?(.+)$/);
if (!match?.[1]) continue;
const text = match[1].replace(/\s+/g, ' ').trim();
if (!text || text.length < 6) continue;
if (/^\(empty\)$/i.test(text)) continue;
const key = text.toLowerCase();
if (seen.has(key)) continue;
seen.add(key);
entries.push(text.length > 360 ? `${text.slice(0, 359)}…` : text);
}
return entries.slice(0, 200);
}
private memoryFingerprint(text: string): string {
const normalized = text
.toLowerCase()
.replace(/[^\p{L}\p{N}\s]/gu, ' ')
.replace(/\s+/g, ' ')
.trim();
return crypto.createHash('sha1').update(normalized).digest('hex');
}
private migrateLegacyMemoryFileToUserMemories(): void {
if (this.get<string>(USER_MEMORIES_MIGRATION_KEY) === '1') {
return;
}
const content = this.tryReadLegacyMemoryText();
if (!content.trim()) {
this.set(USER_MEMORIES_MIGRATION_KEY, '1');
return;
}
const entries = this.parseLegacyMemoryEntries(content);
if (entries.length === 0) {
this.set(USER_MEMORIES_MIGRATION_KEY, '1');
return;
}
const now = Date.now();
this.db.run('BEGIN TRANSACTION;');
try {
for (const text of entries) {
const fingerprint = this.memoryFingerprint(text);
const existing = this.db.exec(
`SELECT id FROM user_memories WHERE fingerprint = ? AND status != 'deleted' LIMIT 1`,
[fingerprint]
);
if (existing[0]?.values?.[0]?.[0]) {
continue;
}
const memoryId = crypto.randomUUID();
this.db.run(`
INSERT INTO user_memories (
id, text, fingerprint, confidence, is_explicit, status, created_at, updated_at, last_used_at
) VALUES (?, ?, ?, ?, 1, 'created', ?, ?, NULL)
`, [memoryId, text, fingerprint, 0.9, now, now]);
this.db.run(`
INSERT INTO user_memory_sources (id, memory_id, session_id, message_id, role, is_active, created_at)
VALUES (?, ?, NULL, NULL, 'system', 1, ?)
`, [crypto.randomUUID(), memoryId, now]);
}
this.db.run('COMMIT;');
} catch (error) {
this.db.run('ROLLBACK;');
console.warn('Failed to migrate legacy MEMORY.md entries:', error);
}
this.set(USER_MEMORIES_MIGRATION_KEY, '1');
}
private migrateFromElectronStore(userDataPath: string) {
const result = this.db.exec('SELECT COUNT(*) as count FROM kv');
const count = result[0]?.values[0]?.[0] as number;
if (count > 0) return;
const legacyPath = path.join(userDataPath, 'config.json');
if (!fs.existsSync(legacyPath)) return;
try {
const raw = fs.readFileSync(legacyPath, 'utf8');
const data = JSON.parse(raw) as Record<string, unknown>;
if (!data || typeof data !== 'object') return;
const entries = Object.entries(data);
if (!entries.length) return;
const now = Date.now();
this.db.run('BEGIN TRANSACTION;');
try {
entries.forEach(([key, value]) => {
this.db.run(`
INSERT INTO kv (key, value, updated_at)
VALUES (?, ?, ?)
`, [key, JSON.stringify(value), now]);
});
this.db.run('COMMIT;');
this.save();
console.info(`Migrated ${entries.length} entries from electron-store.`);
} catch (error) {
this.db.run('ROLLBACK;');
throw error;
}
} catch (error) {
console.warn('Failed to migrate electron-store data:', error);
}
}
}