@clawchatsai/connector 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -1092,6 +1092,18 @@ async function handleExportHistory(api, mode) {
|
|
|
1092
1092
|
process.exitCode = 1;
|
|
1093
1093
|
}
|
|
1094
1094
|
}
|
|
1095
|
+
async function handleImportDates() {
|
|
1096
|
+
// @ts-expect-error — server/ is plain JS with no .d.ts
|
|
1097
|
+
const mod = await import('../server/store/legacy-threads.js');
|
|
1098
|
+
try {
|
|
1099
|
+
const { found, imported } = mod.importLegacyThreadDates(path.join(CONFIG_DIR, 'data'));
|
|
1100
|
+
console.log(`Chat creation dates: ${found} found in the old ClawChats databases, ${imported} new imported.`);
|
|
1101
|
+
}
|
|
1102
|
+
catch (err) {
|
|
1103
|
+
console.error(`import-dates failed: ${err.message}`);
|
|
1104
|
+
process.exitCode = 1;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1095
1107
|
async function handleStatus() {
|
|
1096
1108
|
// CLI runs in a separate process — module-level vars are null here.
|
|
1097
1109
|
// Query the live service via the health endpoint instead.
|
|
@@ -1247,6 +1259,9 @@ const plugin = {
|
|
|
1247
1259
|
cmd.command('export-history [check]')
|
|
1248
1260
|
.description('Export ClawChats chats the gateway has no history for, for the gateway importer ("check" = report only)')
|
|
1249
1261
|
.action((mode) => handleExportHistory(api, mode === undefined ? undefined : String(mode)));
|
|
1262
|
+
cmd.command('import-dates')
|
|
1263
|
+
.description('Copy chat creation dates from the old ClawChats databases (one-off migration; safe to re-run)')
|
|
1264
|
+
.action(() => handleImportDates());
|
|
1250
1265
|
cmd.command('import <path>')
|
|
1251
1266
|
.description('Import databases and config from a folder (e.g. migrate from old data directory)')
|
|
1252
1267
|
.action((srcPath) => handleImport(String(srcPath)));
|
package/package.json
CHANGED
package/server/session-lens.js
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
// - drops `chat`/`agent` run events of sessions ClawChats doesn't show;
|
|
10
10
|
// - filters/trims the responses to browser-issued `sessions.list|subscribe|search`;
|
|
11
11
|
// - keeps the browser from tearing down the connector's own session subscription;
|
|
12
|
-
// - applies project-style side effects of browser-issued group renames/deletes
|
|
12
|
+
// - applies project-style side effects of browser-issued group renames/deletes;
|
|
13
|
+
// - fills `createdAt` on listed rows from `clawchats import-dates` when the gateway has none.
|
|
13
14
|
//
|
|
14
15
|
// Visible = a root, direct, user chat: `agent:<id>:dashboard:<id>` (gateway-created) or
|
|
15
16
|
// `agent:<id>:<project>:chat:<id>` (legacy ClawChats). Everything else is hidden.
|
|
@@ -257,8 +258,18 @@ export class SessionLens {
|
|
|
257
258
|
}
|
|
258
259
|
|
|
259
260
|
_visibleRows(rows) {
|
|
261
|
+
// Creation times of pre-gateway chats (their gateway row has no createdAt). Read per list so
|
|
262
|
+
// `import-dates` applies without a restart; the browser store merges rows, so filling list
|
|
263
|
+
// responses is enough.
|
|
264
|
+
let legacy = null;
|
|
265
|
+
try { legacy = this.extras?.getLegacyCreatedAt?.(); } catch (e) { this.log.warn?.(`[lens] legacy dates: ${e.message}`); }
|
|
260
266
|
const out = [];
|
|
261
|
-
for (const row of rows || [])
|
|
267
|
+
for (const row of rows || []) {
|
|
268
|
+
if (!row || !this.observe(row.key, row)) continue;
|
|
269
|
+
const trimmed = trimRow(row);
|
|
270
|
+
if (!trimmed.createdAt && legacy?.has(row.key)) trimmed.createdAt = legacy.get(row.key);
|
|
271
|
+
out.push(trimmed);
|
|
272
|
+
}
|
|
262
273
|
return out;
|
|
263
274
|
}
|
|
264
275
|
|
|
@@ -166,3 +166,14 @@ test('session.message only for chats a browser subscribed to', () => {
|
|
|
166
166
|
l.outbound(JSON.stringify({ type: 'req', id: 'u', method: 'sessions.messages.unsubscribe', params: { key: CHAT, subscriptionId: 'tab1' } }));
|
|
167
167
|
assert.equal(l.sessionMessage(msg), null);
|
|
168
168
|
});
|
|
169
|
+
|
|
170
|
+
test('listed rows without createdAt get the legacy creation time; gateway values win', () => {
|
|
171
|
+
const extras = { getLegacyCreatedAt: () => new Map([[LEGACY, 111], [CHAT, 222]]) };
|
|
172
|
+
const { l } = lens(extras);
|
|
173
|
+
l.outbound(JSON.stringify({ type: 'req', id: 'c1', method: 'sessions.list', params: {} }));
|
|
174
|
+
const res = l.response({ type: 'res', id: 'c1', ok: true, payload: { sessions: [
|
|
175
|
+
{ key: LEGACY, label: 'old', kind: 'direct' },
|
|
176
|
+
{ key: CHAT, label: 'new', kind: 'direct', createdAt: 999 },
|
|
177
|
+
] } });
|
|
178
|
+
assert.deepEqual(res.payload.sessions.map(s => s.createdAt), [111, 999]);
|
|
179
|
+
});
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
// project_styles color/icon per gateway session group (groups are just {name, position})
|
|
5
5
|
// thread_extras per-session ClawChats data (Intelligence panel etc.), keyed by session key
|
|
6
|
+
// legacy_created creation time of chats from before the gateway recorded createdAt
|
|
6
7
|
|
|
7
8
|
export function createExtrasStore(getGlobalDb) {
|
|
8
9
|
let ready = false;
|
|
@@ -10,6 +11,7 @@ export function createExtrasStore(getGlobalDb) {
|
|
|
10
11
|
const g = getGlobalDb();
|
|
11
12
|
if (!ready) {
|
|
12
13
|
g.exec(`CREATE TABLE IF NOT EXISTS project_styles (name TEXT PRIMARY KEY, color TEXT, icon TEXT, updated_at INTEGER NOT NULL)`);
|
|
14
|
+
g.exec(`CREATE TABLE IF NOT EXISTS legacy_created (session_key TEXT PRIMARY KEY, created_at INTEGER NOT NULL)`);
|
|
13
15
|
g.exec(`CREATE TABLE IF NOT EXISTS thread_extras (session_key TEXT NOT NULL, kind TEXT NOT NULL, data TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (session_key, kind))`);
|
|
14
16
|
ready = true;
|
|
15
17
|
}
|
|
@@ -60,6 +62,19 @@ export function createExtrasStore(getGlobalDb) {
|
|
|
60
62
|
return n;
|
|
61
63
|
},
|
|
62
64
|
|
|
65
|
+
/** Store legacy creation times ([{ sessionKey, createdAt }]); never overwrites. */
|
|
66
|
+
seedLegacyCreatedAt(rows) {
|
|
67
|
+
const ins = db().prepare('INSERT OR IGNORE INTO legacy_created (session_key, created_at) VALUES (?, ?)');
|
|
68
|
+
let n = 0;
|
|
69
|
+
for (const r of rows || []) if (r?.sessionKey && r.createdAt > 0) n += ins.run(r.sessionKey, r.createdAt).changes;
|
|
70
|
+
return n;
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
/** Map of session key -> legacy creation time (ms). */
|
|
74
|
+
getLegacyCreatedAt() {
|
|
75
|
+
return new Map(db().prepare('SELECT session_key, created_at FROM legacy_created').all().map(r => [r.session_key, r.created_at]));
|
|
76
|
+
},
|
|
77
|
+
|
|
63
78
|
getThreadExtra(sessionKey, kind) {
|
|
64
79
|
const r = db().prepare('SELECT data FROM thread_extras WHERE session_key = ? AND kind = ?').get(sessionKey, kind);
|
|
65
80
|
return r ? JSON.parse(r.data) : null;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// `ocplatform clawchats import-dates`: one-off migration from the pre-gateway ClawChats databases
|
|
2
|
+
// (one <project>.db per project in the data dir, each with a `threads` table). The gateway has no
|
|
3
|
+
// creation time for sessions created before it recorded `createdAt`, and `sessions.patch` can't
|
|
4
|
+
// set one, so the connector keeps them (ExtrasStore `legacy_created`). Safe to re-run. Once every
|
|
5
|
+
// install has run it, this file and the command can go; the stored dates stay.
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { Database } from '../bootstrap/native.js';
|
|
10
|
+
import { createExtrasStore } from './extras-store.js';
|
|
11
|
+
|
|
12
|
+
/** [{ sessionKey, createdAt }] from every legacy project DB in `dataDir`; unreadable files are skipped. */
|
|
13
|
+
export function readLegacyThreadDates(dataDir, logger = console) {
|
|
14
|
+
const out = [];
|
|
15
|
+
let files = [];
|
|
16
|
+
try { files = fs.readdirSync(dataDir).filter(f => f.endsWith('.db') && f !== 'global.db'); } catch { return out; }
|
|
17
|
+
for (const f of files) {
|
|
18
|
+
let db;
|
|
19
|
+
try {
|
|
20
|
+
db = new Database(path.join(dataDir, f), { readOnly: true });
|
|
21
|
+
const hasThreads = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'threads'").get();
|
|
22
|
+
if (!hasThreads) continue;
|
|
23
|
+
for (const r of db.prepare('SELECT session_key, created_at FROM threads WHERE session_key IS NOT NULL AND created_at > 0').all()) {
|
|
24
|
+
out.push({ sessionKey: r.session_key, createdAt: Number(r.created_at) });
|
|
25
|
+
}
|
|
26
|
+
} catch (e) {
|
|
27
|
+
logger.warn?.(`[legacy] skipped ${f}: ${e.message}`);
|
|
28
|
+
} finally {
|
|
29
|
+
db?.close();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Copy legacy creation times into global.db. Returns { found, imported } (imported = new rows). */
|
|
36
|
+
export function importLegacyThreadDates(dataDir, logger = console) {
|
|
37
|
+
const rows = readLegacyThreadDates(dataDir, logger);
|
|
38
|
+
const db = new Database(path.join(dataDir, 'global.db'));
|
|
39
|
+
try {
|
|
40
|
+
return { found: rows.length, imported: createExtrasStore(() => db).seedLegacyCreatedAt(rows) };
|
|
41
|
+
} finally { db.close(); }
|
|
42
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { Database } from '../bootstrap/native.js';
|
|
7
|
+
import { readLegacyThreadDates, importLegacyThreadDates } from './legacy-threads.js';
|
|
8
|
+
import { createExtrasStore } from './extras-store.js';
|
|
9
|
+
|
|
10
|
+
function legacyDb(file, rows) {
|
|
11
|
+
const db = new Database(file);
|
|
12
|
+
db.exec('CREATE TABLE threads (id TEXT, session_key TEXT, created_at INTEGER)');
|
|
13
|
+
const ins = db.prepare('INSERT INTO threads VALUES (?, ?, ?)');
|
|
14
|
+
for (const r of rows) ins.run(...r);
|
|
15
|
+
db.close();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
test('reads creation times from every legacy project DB; skips global.db, non-thread DBs and junk', () => {
|
|
19
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cc-legacy-'));
|
|
20
|
+
try {
|
|
21
|
+
legacyDb(path.join(dir, 'default.db'), [['a', 'agent:main:default:chat:a', 100], ['b', null, 200], ['c', 'agent:main:default:chat:c', 0]]);
|
|
22
|
+
legacyDb(path.join(dir, 'proxmox.db'), [['p', 'agent:main:proxmox:chat:p', 300]]);
|
|
23
|
+
legacyDb(path.join(dir, 'global.db'), [['g', 'agent:main:x:chat:g', 400]]);
|
|
24
|
+
new Database(path.join(dir, 'alerts.db')).close(); // no threads table
|
|
25
|
+
fs.writeFileSync(path.join(dir, 'broken.db'), 'not sqlite');
|
|
26
|
+
const rows = readLegacyThreadDates(dir, { warn() {} }).sort((x, y) => x.createdAt - y.createdAt);
|
|
27
|
+
assert.deepEqual(rows, [{ sessionKey: 'agent:main:default:chat:a', createdAt: 100 }, { sessionKey: 'agent:main:proxmox:chat:p', createdAt: 300 }]);
|
|
28
|
+
assert.deepEqual(readLegacyThreadDates(path.join(dir, 'missing')), []);
|
|
29
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('seeding is insert-or-ignore and readable as a map', () => {
|
|
33
|
+
const g = new Database(':memory:');
|
|
34
|
+
const extras = createExtrasStore(() => g);
|
|
35
|
+
assert.equal(extras.seedLegacyCreatedAt([{ sessionKey: 'k1', createdAt: 5 }, { sessionKey: 'k2', createdAt: 6 }, { sessionKey: '', createdAt: 7 }]), 2);
|
|
36
|
+
assert.equal(extras.seedLegacyCreatedAt([{ sessionKey: 'k1', createdAt: 99 }]), 0);
|
|
37
|
+
assert.deepEqual([...extras.getLegacyCreatedAt()], [['k1', 5], ['k2', 6]]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('import-dates: writes into global.db, re-running imports nothing new', () => {
|
|
41
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cc-legacy-'));
|
|
42
|
+
try {
|
|
43
|
+
legacyDb(path.join(dir, 'default.db'), [['a', 'agent:main:default:chat:a', 100]]);
|
|
44
|
+
assert.deepEqual(importLegacyThreadDates(dir), { found: 1, imported: 1 });
|
|
45
|
+
assert.deepEqual(importLegacyThreadDates(dir), { found: 1, imported: 0 });
|
|
46
|
+
const g = new Database(path.join(dir, 'global.db'));
|
|
47
|
+
assert.deepEqual([...createExtrasStore(() => g).getLegacyCreatedAt()], [['agent:main:default:chat:a', 100]]);
|
|
48
|
+
g.close();
|
|
49
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
50
|
+
});
|