@clawchatsai/connector 0.1.17 → 0.1.19

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
@@ -38,7 +38,30 @@ export const PLUGIN_VERSION = _pkgJson.version;
38
38
  const MAX_DC_MESSAGE_SIZE = 64 * 1024;
39
39
  const DC_CHUNK_SIZE = MAX_DC_MESSAGE_SIZE - 512; // room for the chunk envelope
40
40
  /** Active DataChannel connections: connectionId → send function */
41
- const connectedClients = new Map();
41
+ // Authenticated DataChannel browsers. Adding or removing one tells the gateway client, which holds the
42
+ // approval-client gateway connection only while a browser is attached (server/approval-client.js).
43
+ class BrowserClientMap extends Map {
44
+ set(key, value) {
45
+ const added = !this.has(key);
46
+ super.set(key, value);
47
+ if (added)
48
+ app?.gatewayClient.browsersChanged();
49
+ return this;
50
+ }
51
+ delete(key) {
52
+ const removed = super.delete(key);
53
+ if (removed)
54
+ app?.gatewayClient.browsersChanged();
55
+ return removed;
56
+ }
57
+ clear() {
58
+ const had = this.size > 0;
59
+ super.clear();
60
+ if (had)
61
+ app?.gatewayClient.browsersChanged();
62
+ }
63
+ }
64
+ const connectedClients = new BrowserClientMap();
42
65
  /** Reassembly buffers for chunked RPC requests from browser (large uploads). */
43
66
  const rpcReqChunkBuffers = new Map();
44
67
  /** Reassembly buffers for chunked gateway-msg from browser (large payloads like image attachments). */
@@ -219,6 +242,7 @@ async function startClawChats(ctx, api) {
219
242
  });
220
243
  // 4. Connect createApp's gateway client (handles persistence + event relay)
221
244
  app.gatewayClient.connect();
245
+ app.gatewayClient.setExternalBrowserCount(() => connectedClients.size);
222
246
  // Wire DataChannel clients as broadcast targets so they receive gateway events.
223
247
  // Frames over the DataChannel limit (e.g. a complete chat.history snapshot) go as chunks.
224
248
  app.gatewayClient.addBroadcastTarget((data) => {
@@ -1092,6 +1116,18 @@ async function handleExportHistory(api, mode) {
1092
1116
  process.exitCode = 1;
1093
1117
  }
1094
1118
  }
1119
+ async function handleImportDates() {
1120
+ // @ts-expect-error — server/ is plain JS with no .d.ts
1121
+ const mod = await import('../server/store/legacy-threads.js');
1122
+ try {
1123
+ const { found, imported } = mod.importLegacyThreadDates(path.join(CONFIG_DIR, 'data'));
1124
+ console.log(`Chat creation dates: ${found} found in the old ClawChats databases, ${imported} new imported.`);
1125
+ }
1126
+ catch (err) {
1127
+ console.error(`import-dates failed: ${err.message}`);
1128
+ process.exitCode = 1;
1129
+ }
1130
+ }
1095
1131
  async function handleStatus() {
1096
1132
  // CLI runs in a separate process — module-level vars are null here.
1097
1133
  // Query the live service via the health endpoint instead.
@@ -1247,6 +1283,9 @@ const plugin = {
1247
1283
  cmd.command('export-history [check]')
1248
1284
  .description('Export ClawChats chats the gateway has no history for, for the gateway importer ("check" = report only)')
1249
1285
  .action((mode) => handleExportHistory(api, mode === undefined ? undefined : String(mode)));
1286
+ cmd.command('import-dates')
1287
+ .description('Copy chat creation dates from the old ClawChats databases (one-off migration; safe to re-run)')
1288
+ .action(() => handleImportDates());
1250
1289
  cmd.command('import <path>')
1251
1290
  .description('Import databases and config from a folder (e.g. migrate from old data directory)')
1252
1291
  .action((srcPath) => handleImport(String(srcPath)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,96 @@
1
+ import { WebSocket as WS } from 'ws';
2
+
3
+ // The gateway sends *.approval.requested / *.approval.resolved only to "approval clients"
4
+ // (src/gateway/server-request-context.ts canDeliverApprovals: the Control UI's client id, or a
5
+ // connection declaring the `approvals` cap). Being one also changes routing: while any approval
6
+ // client is connected, an approval waits for a decision instead of expiring at once with
7
+ // "no-approval-route". The connector's main connection is always up, so it must not be one.
8
+ // Like an open Control UI tab, this second connection declares the cap only while a browser is
9
+ // attached (GatewayClient.browsersChanged). Only approval events leave it; everything else the
10
+ // gateway broadcasts to it is dropped (the main connection already delivers those).
11
+ export const APPROVAL_EVENT_RE = /^(exec|plugin|openclaw)\.approval\.(requested|resolved)$/;
12
+
13
+ export class ApprovalClient {
14
+ /**
15
+ * @param {object} o
16
+ * @param {string} o.gatewayWsUrl
17
+ * @param {(nonce: string) => object} o.connectParams connect params for this socket (caps: ['approvals'])
18
+ * @param {(data: string) => void} o.onEvent an approval event frame, as received
19
+ * @param {{ logFrame(direction: string, data: string): void }} o.debugLogger
20
+ */
21
+ constructor({ gatewayWsUrl, connectParams, onEvent, debugLogger }) {
22
+ this.gatewayWsUrl = gatewayWsUrl;
23
+ this.connectParams = connectParams;
24
+ this.onEvent = onEvent;
25
+ this.debugLogger = debugLogger;
26
+ this.ws = null;
27
+ this.running = false;
28
+ this.connected = false;
29
+ this.reconnectAttempts = 0;
30
+ this._reconnectTimer = null;
31
+ }
32
+
33
+ start() {
34
+ if (this.running) return;
35
+ this.running = true;
36
+ this.reconnectAttempts = 0;
37
+ this._connect();
38
+ }
39
+
40
+ stop() {
41
+ this.running = false;
42
+ this.connected = false;
43
+ clearTimeout(this._reconnectTimer);
44
+ this._reconnectTimer = null;
45
+ const ws = this.ws;
46
+ this.ws = null;
47
+ if (ws) {
48
+ ws.removeAllListeners('message');
49
+ ws.removeAllListeners('close');
50
+ ws.on('error', () => {});
51
+ try { ws.readyState === WS.CONNECTING ? ws.terminate() : ws.close(1000, 'no browser attached'); } catch { /* already closed */ }
52
+ setTimeout(() => { try { ws.terminate(); } catch { /* gone */ } }, 2000).unref?.();
53
+ }
54
+ }
55
+
56
+ _connect() {
57
+ if (!this.running) return;
58
+ const ws = new WS(this.gatewayWsUrl);
59
+ this.ws = ws;
60
+ ws.on('open', () => { this.reconnectAttempts = 0; });
61
+ ws.on('message', data => this._onMessage(ws, data.toString()));
62
+ ws.on('close', () => {
63
+ if (this.ws !== ws) return;
64
+ this.connected = false;
65
+ this.ws = null;
66
+ if (this.running) this._scheduleReconnect();
67
+ });
68
+ ws.on('error', err => console.error('[approvals] gateway WebSocket error:', err.message));
69
+ }
70
+
71
+ _onMessage(ws, data) {
72
+ let msg;
73
+ try { msg = JSON.parse(data); } catch { return; }
74
+ if (msg.type === 'event' && msg.event === 'connect.challenge') {
75
+ ws.send(JSON.stringify({ type: 'req', id: 'gw-approvals-connect', method: 'connect', params: this.connectParams(msg.payload?.nonce || '') }));
76
+ return;
77
+ }
78
+ if (msg.type === 'res' && msg.id === 'gw-approvals-connect') {
79
+ this.connected = !!msg.ok;
80
+ if (msg.ok) console.log('[approvals] approval-client connection ready');
81
+ else console.warn(`[approvals] connect failed: ${msg.error?.message || 'unknown error'}`);
82
+ return;
83
+ }
84
+ if (msg.type === 'event' && APPROVAL_EVENT_RE.test(msg.event || '')) {
85
+ this.debugLogger?.logFrame('GW→SRV', data);
86
+ this.onEvent(data);
87
+ }
88
+ }
89
+
90
+ _scheduleReconnect() {
91
+ const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
92
+ this.reconnectAttempts++;
93
+ clearTimeout(this._reconnectTimer);
94
+ this._reconnectTimer = setTimeout(() => { this._reconnectTimer = null; this._connect(); }, delay);
95
+ }
96
+ }
@@ -0,0 +1,106 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { WebSocketServer } from 'ws';
4
+ import { GatewayClient } from './gateway.js';
5
+ import { APPROVAL_EVENT_RE } from './approval-client.js';
6
+
7
+ const logger = { logFrame() {} };
8
+ const wait = ms => new Promise(r => setTimeout(r, ms));
9
+
10
+ /** A fake gateway: challenges each connection, records connect params, can push events. */
11
+ async function fakeGateway() {
12
+ const wss = new WebSocketServer({ port: 0 });
13
+ const conns = [];
14
+ wss.on('connection', ws => {
15
+ const c = { ws, params: null, closed: false };
16
+ conns.push(c);
17
+ ws.on('close', () => { c.closed = true; });
18
+ ws.on('message', raw => {
19
+ const msg = JSON.parse(raw.toString());
20
+ if (msg.method === 'connect') { c.params = msg.params; ws.send(JSON.stringify({ type: 'res', id: msg.id, ok: true, payload: { type: 'hello-ok' } })); }
21
+ });
22
+ ws.send(JSON.stringify({ type: 'event', event: 'connect.challenge', payload: { nonce: 'n' } }));
23
+ });
24
+ await new Promise(r => wss.once('listening', r));
25
+ return { wss, conns, url: `ws://127.0.0.1:${wss.address().port}`, approvalConns: () => conns.filter(c => c.params?.caps?.includes('approvals')) };
26
+ }
27
+
28
+ function client(url, graceMs = 50) {
29
+ const dataDir = '/tmp/cc-approval-client-test';
30
+ return new GatewayClient({ dataDir, debugLogger: logger, gatewayWsUrl: url, authToken: '', approvalClientGraceMs: graceMs });
31
+ }
32
+
33
+ test('approval events only: requested/resolved for exec, plugin and system-agent', () => {
34
+ for (const e of ['exec.approval.requested', 'plugin.approval.resolved', 'openclaw.approval.requested']) assert.ok(APPROVAL_EVENT_RE.test(e), e);
35
+ for (const e of ['session.approval', 'exec.approval.list', 'chat', 'sessions.changed']) assert.ok(!APPROVAL_EVENT_RE.test(e), e);
36
+ });
37
+
38
+ test('no browser: no approval-client connection; main connection never declares approvals', async () => {
39
+ const gw = await fakeGateway();
40
+ const c = client(gw.url);
41
+ c.connect();
42
+ await wait(200);
43
+ assert.equal(gw.conns.length, 1);
44
+ assert.deepEqual(gw.conns[0].params.caps, ['tool-events']);
45
+ assert.equal(gw.approvalConns().length, 0);
46
+ c.close();
47
+ await new Promise(r => gw.wss.close(r));
48
+ });
49
+
50
+ test('a browser attaches: approval-client connection with the approvals cap; its approval events reach browsers, others are dropped', async () => {
51
+ const gw = await fakeGateway();
52
+ const c = client(gw.url);
53
+ const received = [];
54
+ c.addBroadcastTarget(d => received.push(JSON.parse(d)));
55
+ let n = 0;
56
+ c.setExternalBrowserCount(() => n);
57
+ n = 1; c.browsersChanged();
58
+ await wait(200);
59
+ const [ac] = gw.approvalConns();
60
+ assert.ok(ac, 'approval-client connection opened');
61
+ assert.deepEqual(ac.params.scopes.includes('operator.approvals'), true);
62
+ ac.ws.send(JSON.stringify({ type: 'event', event: 'plugin.approval.requested', payload: { id: 'plugin:1' } }));
63
+ ac.ws.send(JSON.stringify({ type: 'event', event: 'sessions.changed', payload: {} }));
64
+ ac.ws.send(JSON.stringify({ type: 'event', event: 'plugin.approval.resolved', payload: { id: 'plugin:1' } }));
65
+ await wait(100);
66
+ assert.deepEqual(received.map(m => m.event), ['plugin.approval.requested', 'plugin.approval.resolved']);
67
+ c.close();
68
+ await new Promise(r => gw.wss.close(r));
69
+ });
70
+
71
+ test('last browser leaves: closed after the grace period; a browser back within it keeps the same connection', async () => {
72
+ const gw = await fakeGateway();
73
+ const c = client(gw.url, 150);
74
+ let n = 1;
75
+ c.setExternalBrowserCount(() => n);
76
+ await wait(200);
77
+ assert.equal(gw.approvalConns().length, 1);
78
+ n = 0; c.browsersChanged();
79
+ await wait(50);
80
+ n = 1; c.browsersChanged(); // reconnect within grace
81
+ await wait(250);
82
+ assert.equal(gw.approvalConns().length, 1);
83
+ assert.equal(gw.approvalConns()[0].closed, false);
84
+ n = 0; c.browsersChanged();
85
+ await wait(300);
86
+ assert.equal(gw.approvalConns()[0].closed, true);
87
+ assert.equal(c.approvalClient.running, false);
88
+ c.close();
89
+ await new Promise(r => gw.wss.close(r));
90
+ });
91
+
92
+ test('local WebSocket browsers count too; close() stops the approval-client connection', async () => {
93
+ const gw = await fakeGateway();
94
+ const c = client(gw.url);
95
+ const fakeWs = { readyState: 3, send() {} };
96
+ c.addBrowserClient(fakeWs);
97
+ await wait(200);
98
+ assert.equal(gw.approvalConns().length, 1);
99
+ c.close();
100
+ await wait(100);
101
+ assert.equal(gw.approvalConns()[0].closed, true);
102
+ c.browsersChanged(); // ignored after close
103
+ await wait(100);
104
+ assert.equal(gw.approvalConns().length, 1);
105
+ await new Promise(r => gw.wss.close(r));
106
+ });
package/server/gateway.js CHANGED
@@ -1,12 +1,16 @@
1
1
  import path from 'node:path';
2
2
  import { WebSocket as WS } from 'ws';
3
3
  import { loadOrCreateDeviceIdentity, buildDeviceAuth } from './bootstrap/identity.js';
4
+ import { ApprovalClient } from './approval-client.js';
4
5
 
5
6
  // Per-session run/transcript events; hidden sessions' ones are dropped by the lens.
6
7
  const RUN_EVENTS = new Set(['chat', 'agent', 'session.tool', 'session.operation', 'session.observer']);
8
+ const SCOPES = ['operator.read', 'operator.write', 'operator.admin', 'operator.approvals', 'operator.questions'];
9
+ // After the last browser leaves, the approval-client connection stays this long (P2P reconnects, reloads).
10
+ const APPROVAL_CLIENT_GRACE_MS = 10_000;
7
11
 
8
12
  export class GatewayClient {
9
- constructor({ dataDir, debugLogger, gatewayWsUrl, authToken }) {
13
+ constructor({ dataDir, debugLogger, gatewayWsUrl, authToken, approvalClientGraceMs = APPROVAL_CLIENT_GRACE_MS }) {
10
14
  this.dataDir = dataDir;
11
15
  this.debugLogger = debugLogger;
12
16
  this.gatewayWsUrl = gatewayWsUrl;
@@ -19,6 +23,40 @@ export class GatewayClient {
19
23
  this._externalBroadcastTargets = [];
20
24
  this._rpc = new Map(); // request id → { resolve, reject, timer } for connector-originated RPCs
21
25
  this._rpcSeq = 0;
26
+ // Approval events only while a browser is attached (server/approval-client.js).
27
+ this._externalBrowserCount = () => 0;
28
+ this._approvalGraceMs = approvalClientGraceMs;
29
+ this._approvalStopTimer = null;
30
+ this.approvalClient = new ApprovalClient({
31
+ gatewayWsUrl,
32
+ debugLogger,
33
+ connectParams: nonce => this._connectParams(nonce, ['approvals']),
34
+ onEvent: data => this.broadcastToBrowsers(data),
35
+ });
36
+ }
37
+
38
+ /** Gateway connect params; `caps` differ between the main and the approval-client connection. */
39
+ _connectParams(nonce, caps) {
40
+ const identity = loadOrCreateDeviceIdentity(path.join(this.dataDir, 'device-identity.json'));
41
+ const device = buildDeviceAuth(identity, { clientId: 'gateway-client', clientMode: 'backend', role: 'operator', scopes: SCOPES, token: this.authToken, nonce });
42
+ return { minProtocol: 3, maxProtocol: 4, client: { id: 'gateway-client', version: '0.1.0', platform: 'node', mode: 'backend' }, role: 'operator', scopes: SCOPES, device, auth: { token: this.authToken }, caps };
43
+ }
44
+
45
+ /** Browsers attached over the DataChannel (src/index.ts); local WebSocket browsers are counted here. */
46
+ setExternalBrowserCount(fn) { this._externalBrowserCount = fn; this.browsersChanged(); }
47
+
48
+ /** A browser attached or left: hold the approval-client connection while any is attached. */
49
+ browsersChanged() {
50
+ if (this._closed) return;
51
+ const count = this.browserClients.size + this._externalBrowserCount();
52
+ if (count > 0) {
53
+ clearTimeout(this._approvalStopTimer);
54
+ this._approvalStopTimer = null;
55
+ this.approvalClient.start();
56
+ } else if (this.approvalClient.running && !this._approvalStopTimer) {
57
+ this._approvalStopTimer = setTimeout(() => { this._approvalStopTimer = null; this.approvalClient.stop(); }, this._approvalGraceMs);
58
+ this._approvalStopTimer.unref?.();
59
+ }
22
60
  }
23
61
 
24
62
  connect() {
@@ -54,9 +92,7 @@ export class GatewayClient {
54
92
  }
55
93
 
56
94
  if (msg.type === 'event' && msg.event === 'connect.challenge') {
57
- const identity = loadOrCreateDeviceIdentity(path.join(this.dataDir, 'device-identity.json'));
58
- const device = buildDeviceAuth(identity, { clientId: 'gateway-client', clientMode: 'backend', role: 'operator', scopes: ['operator.read', 'operator.write', 'operator.admin', 'operator.approvals', 'operator.questions'], token: this.authToken, nonce: msg.payload?.nonce || '' });
59
- this.ws.send(JSON.stringify({ type: 'req', id: 'gw-connect-1', method: 'connect', params: { minProtocol: 3, maxProtocol: 4, client: { id: 'gateway-client', version: '0.1.0', platform: 'node', mode: 'backend' }, role: 'operator', scopes: ['operator.read', 'operator.write', 'operator.admin', 'operator.approvals', 'operator.questions'], device, auth: { token: this.authToken }, caps: ['tool-events'] } }));
95
+ this.ws.send(JSON.stringify({ type: 'req', id: 'gw-connect-1', method: 'connect', params: this._connectParams(msg.payload?.nonce || '', ['tool-events']) }));
60
96
  return;
61
97
  }
62
98
  if (msg.type === 'res' && msg.payload?.type === 'hello-ok') { console.log('Gateway handshake complete'); this.connected = true; this.broadcastGatewayStatus(true); this.sync?.onConnected(); this.request('sessions.subscribe', {}).catch(e => console.warn(`[lens] sessions.subscribe failed: ${e.message}`)); this.lens?.seed().catch(e => console.warn(`[lens] seed failed: ${e.message}`)); }
@@ -132,6 +168,9 @@ export class GatewayClient {
132
168
  this._reconnectTimer = null;
133
169
  this._failPendingRpcs('connector stopping');
134
170
  this.connected = false;
171
+ clearTimeout(this._approvalStopTimer);
172
+ this._approvalStopTimer = null;
173
+ this.approvalClient.stop();
135
174
  const ws = this.ws;
136
175
  this.ws = null;
137
176
  if (ws) {
@@ -147,12 +186,13 @@ export class GatewayClient {
147
186
 
148
187
  addBrowserClient(ws) {
149
188
  this.browserClients.set(ws, {});
189
+ this.browsersChanged();
150
190
  if (ws.readyState === WS.OPEN) {
151
191
  ws.send(JSON.stringify({ type: 'clawchats', event: 'gateway-status', connected: this.connected }));
152
192
  }
153
193
  }
154
194
 
155
- removeBrowserClient(ws) { this.browserClients.delete(ws); }
195
+ removeBrowserClient(ws) { this.browserClients.delete(ws); this.browsersChanged(); }
156
196
 
157
197
 
158
198
  addBroadcastTarget(fn) { this._externalBroadcastTargets.push(fn); }
@@ -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 || []) if (row && this.observe(row.key, row)) out.push(trimRow(row));
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
+ });