@clawchatsai/connector 0.1.14 → 0.1.16

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/server/gateway.js CHANGED
@@ -3,6 +3,19 @@ import { WebSocket as WS } from 'ws';
3
3
  import { loadOrCreateDeviceIdentity, buildDeviceAuth } from './bootstrap/identity.js';
4
4
  import { parseSessionKey, extractContent, isSilentReplyExact, isSilentReplyPrefix, sanitizeAssistantContent, syncThreadUnreadCount, generateActivitySummary, writeActivityToDb } from './util/helpers.js';
5
5
 
6
+ /**
7
+ * Cumulative assistant text for a `chat` delta event.
8
+ * Protocol-v4 gateways (2026.9+) send appends in `deltaText`; only the first frame of a
9
+ * run (and any new baseline) carries the full `message` snapshot, which already includes
10
+ * the delta. `replace: true` makes `deltaText` the entire text. Older gateways send the
11
+ * full cumulative `message` on every delta, which the snapshot branch covers.
12
+ */
13
+ export function nextChatText(prev, params) {
14
+ if (params?.message) return extractContent(params.message);
15
+ if (typeof params?.deltaText !== 'string') return prev || '';
16
+ return params.replace ? params.deltaText : (prev || '') + params.deltaText;
17
+ }
18
+
6
19
  export class GatewayClient {
7
20
  constructor({ getDb, getWorkspaces, dataDir, debugLogger, gatewayWsUrl, authToken }) {
8
21
  this.getDb = getDb;
@@ -26,6 +39,9 @@ export class GatewayClient {
26
39
  this.browserClients = new Map();
27
40
  this._externalBroadcastTargets = [];
28
41
  this.streamState = new Map();
42
+ this._utilityText = new Map(); // sessionKey → accumulated delta text for utility sessions
43
+ this._rpc = new Map(); // request id → { resolve, reject, timer } for connector-originated RPCs
44
+ this._rpcSeq = 0;
29
45
  this.activityLogs = new Map();
30
46
  this._pendingTitleGens = new Map();
31
47
  // Runs we've already synthesized a streaming-end{reason:'error'} for
@@ -69,13 +85,18 @@ export class GatewayClient {
69
85
  }, 5 * 60 * 1000);
70
86
  }
71
87
 
88
+ /** parseSessionKey, but following threads that sync moved to another project. */
89
+ _locate(sessionKey) {
90
+ return this.sync ? this.sync.locate(sessionKey) : parseSessionKey(sessionKey);
91
+ }
92
+
72
93
  connect() {
73
94
  if (this.ws && (this.ws.readyState === WS.CONNECTING || this.ws.readyState === WS.OPEN)) return;
74
95
  console.log(`Connecting to gateway at ${this.gatewayWsUrl}...`);
75
96
  this.ws = new WS(this.gatewayWsUrl);
76
97
  this.ws.on('open', () => { console.log('Gateway WebSocket connected'); this.reconnectAttempts = 0; });
77
98
  this.ws.on('message', data => this.handleGatewayMessage(data.toString()));
78
- this.ws.on('close', () => { console.log('Gateway WebSocket closed'); this.connected = false; this.broadcastGatewayStatus(false); this.scheduleReconnect(); });
99
+ this.ws.on('close', () => { console.log('Gateway WebSocket closed'); this.connected = false; this._failPendingRpcs('gateway disconnected'); this.broadcastGatewayStatus(false); this.scheduleReconnect(); });
79
100
  this.ws.on('error', err => console.error('Gateway WebSocket error:', err.message));
80
101
  }
81
102
 
@@ -84,13 +105,29 @@ export class GatewayClient {
84
105
  let msg;
85
106
  try { msg = JSON.parse(data); } catch { console.error('Invalid JSON from gateway:', data); return; }
86
107
 
108
+ // Responses to the connector's own RPCs (request()) are not browser traffic.
109
+ if (msg.type === 'res' && this._rpc.has(msg.id)) {
110
+ const pending = this._rpc.get(msg.id);
111
+ this._rpc.delete(msg.id);
112
+ clearTimeout(pending.timer);
113
+ if (msg.ok) pending.resolve(msg.payload);
114
+ else pending.reject(new Error(msg.error?.message || `${pending.method} failed`));
115
+ return;
116
+ }
117
+
87
118
  if (msg.type === 'event' && msg.event === 'connect.challenge') {
88
119
  const identity = loadOrCreateDeviceIdentity(path.join(this.dataDir, 'device-identity.json'));
89
120
  const device = buildDeviceAuth(identity, { clientId: 'gateway-client', clientMode: 'backend', role: 'operator', scopes: ['operator.read', 'operator.write', 'operator.admin'], token: this.authToken, nonce: msg.payload?.nonce || '' });
90
121
  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'], device, auth: { token: this.authToken }, caps: ['tool-events'] } }));
91
122
  return;
92
123
  }
93
- if (msg.type === 'res' && msg.payload?.type === 'hello-ok') { console.log('Gateway handshake complete'); this.connected = true; this.broadcastGatewayStatus(true); }
124
+ if (msg.type === 'res' && msg.payload?.type === 'hello-ok') { console.log('Gateway handshake complete'); this.connected = true; this.broadcastGatewayStatus(true); this.sync?.onConnected(); }
125
+ // Session-list events exist for the connector's own subscription (gateway sync);
126
+ // the ClawChats frontend never subscribes, so don't spend tunnel bandwidth on them.
127
+ if (msg.type === 'event' && (msg.event === 'sessions.changed' || msg.event === 'session.message')) {
128
+ if (msg.event === 'sessions.changed') this.sync?.onSessionsChanged(msg.payload);
129
+ return;
130
+ }
94
131
  if (msg.type === 'event' && msg.event === 'chat' && msg.payload) {
95
132
  this.handleChatEvent(msg.payload, data);
96
133
  } else {
@@ -109,7 +146,8 @@ export class GatewayClient {
109
146
  const UTILITY_SESSIONS = { '__clawchats_summarizer': 'summarizer', '__clawchats_semantic': 'semantic', '__clawchats_intelligence': 'intelligence' };
110
147
  const utilityName = UTILITY_SESSIONS[bareSessionKey];
111
148
  if (utilityName) {
112
- const content = extractContent(message);
149
+ const content = state === 'delta' ? this._accumulateChatText(sessionKey, params) : extractContent(message);
150
+ if (state !== 'delta') this._utilityText.delete(sessionKey);
113
151
  if (state === 'delta' && content) {
114
152
  this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'utility-response', session: utilityName, state: 'delta', content }));
115
153
  } else if (state === 'final' || state === 'aborted') {
@@ -123,11 +161,12 @@ export class GatewayClient {
123
161
 
124
162
  // --- Delta path ---
125
163
  if (state === 'delta') {
126
- const parsed = parseSessionKey(sessionKey);
164
+ const parsed = this._locate(sessionKey);
127
165
  if (parsed) {
128
166
  const existing = this.streamState.get(sessionKey) || { buffer: '', threadId: parsed.threadId, state: 'streaming', held: [] };
129
167
  const prevLen = existing.buffer.length; // capture before update — used to advance thoughtStartOffset on first post-tool delta
130
- existing.buffer = extractContent(message); // gateway sends full cumulative content per delta, not chunks
168
+ const prevBuffer = existing.buffer;
169
+ existing.buffer = nextChatText(existing.buffer, params);
131
170
  if (isSilentReplyPrefix(existing.buffer, 'NO_REPLY') || isSilentReplyPrefix(existing.buffer, 'HEARTBEAT_OK')) {
132
171
  existing.held = existing.held || [];
133
172
  existing.held.push(rawData);
@@ -144,7 +183,9 @@ export class GatewayClient {
144
183
  // frontend to clear the bubble. Both happen here — right before the new-segment delta —
145
184
  // so the clear and fill are atomic from the browser's perspective.
146
185
  if (existing.pendingReset) {
147
- existing.thoughtStartOffset = prevLen;
186
+ // A v4 gateway may open the post-tool segment with a fresh snapshot instead of
187
+ // extending the run text; only skip the old text if it's still a prefix.
188
+ existing.thoughtStartOffset = existing.buffer.startsWith(prevBuffer) ? prevLen : 0;
148
189
  existing.pendingReset = false;
149
190
  this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-reset', threadId: parsed.threadId, workspace: parsed.workspace }));
150
191
  }
@@ -164,14 +205,19 @@ export class GatewayClient {
164
205
 
165
206
  // Title sessions are handled server-side — intercept and skip browser delivery
166
207
  if (sessionKey?.includes('__clawchats_title_')) {
167
- if (state === 'final') { const content = extractContent(message); if (content && this.handleTitleResponse(sessionKey, content)) return; }
208
+ if (state === 'final') {
209
+ const content = extractContent(message);
210
+ const handled = content && this.handleTitleResponse(sessionKey, content);
211
+ if (this.sync) this.sync.run(() => this.sync.deleteSession(sessionKey)).catch(e => console.warn(`[clawchats] title session cleanup: ${e.message}`));
212
+ if (handled) return;
213
+ }
168
214
  else if (state === 'error' || state === 'aborted') { for (const key of this._pendingTitleGens.keys()) { if (sessionKey === key || sessionKey.includes(key)) { this._pendingTitleGens.delete(key); break; } } return; }
169
215
  return;
170
216
  }
171
217
 
172
218
  if (state === 'final') {
173
219
  const rawContent = extractContent(message);
174
- const parsed = parseSessionKey(sessionKey);
220
+ const parsed = this._locate(sessionKey);
175
221
  if (isSilentReplyExact(rawContent, 'NO_REPLY') || isSilentReplyExact(rawContent, 'HEARTBEAT_OK')) {
176
222
  this._cleanupSilentPending(sessionKey);
177
223
  if (parsed) this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-end', threadId: parsed.threadId, workspace: parsed.workspace, reason: 'silent' }));
@@ -188,7 +234,7 @@ export class GatewayClient {
188
234
  return;
189
235
  }
190
236
  if (state === 'aborted') {
191
- const parsed = parseSessionKey(sessionKey);
237
+ const parsed = this._locate(sessionKey);
192
238
  // Clear pending flag from DB so a stale pending:true doesn't survive page reloads
193
239
  // and trigger phantom "thinking..." state on next visit to this thread.
194
240
  if (parsed) {
@@ -208,7 +254,7 @@ export class GatewayClient {
208
254
  console.log(`[clawchats] ignoring late error for run ${runId} — reply already delivered`);
209
255
  return;
210
256
  }
211
- const parsed = parseSessionKey(sessionKey);
257
+ const parsed = this._locate(sessionKey);
212
258
  this.saveErrorMarker(sessionKey, message);
213
259
  this.broadcastToBrowsers(rawData); // dual-emit
214
260
  if (parsed) {
@@ -218,6 +264,28 @@ export class GatewayClient {
218
264
  }
219
265
  }
220
266
 
267
+ // Gateway RPC from the connector itself; resolves with the response payload.
268
+ request(method, params = {}, timeoutMs = 15000) {
269
+ return new Promise((resolve, reject) => {
270
+ if (!this.connected || !this.ws || this.ws.readyState !== 1) return reject(new Error('gateway not connected'));
271
+ const id = `cc-rpc-${++this._rpcSeq}`;
272
+ const timer = setTimeout(() => { this._rpc.delete(id); reject(new Error(`${method} timed out`)); }, timeoutMs);
273
+ this._rpc.set(id, { resolve, reject, timer, method });
274
+ this.ws.send(JSON.stringify({ type: 'req', id, method, params }));
275
+ });
276
+ }
277
+
278
+ _failPendingRpcs(reason) {
279
+ for (const [id, p] of this._rpc) { clearTimeout(p.timer); p.reject(new Error(reason)); this._rpc.delete(id); }
280
+ }
281
+
282
+ // Utility sessions keep no streamState entry; accumulate their delta text separately.
283
+ _accumulateChatText(sessionKey, params) {
284
+ const text = nextChatText(this._utilityText.get(sessionKey) || '', params);
285
+ this._utilityText.set(sessionKey, text);
286
+ return text;
287
+ }
288
+
221
289
  // Removes metadata.pending flag from any pending assistant message for a thread.
222
290
  // Called on abort and by _cleanupSilentPending — keeps the SQL in one place.
223
291
  _clearPendingFlag(db, threadId) {
@@ -228,7 +296,7 @@ export class GatewayClient {
228
296
  }
229
297
 
230
298
  _cleanupSilentPending(sessionKey) {
231
- const parsed = parseSessionKey(sessionKey);
299
+ const parsed = this._locate(sessionKey);
232
300
  if (!parsed) return;
233
301
  const ws = this.getWorkspaces();
234
302
  if (!ws.workspaces[parsed.workspace]) return;
@@ -243,7 +311,7 @@ export class GatewayClient {
243
311
  // early-out. handleChatEvent uses the result to decide whether the run counts as
244
312
  // having delivered a visible reply.
245
313
  saveAssistantMessage(sessionKey, message, seq, thoughtStartOffset = 0) {
246
- const parsed = parseSessionKey(sessionKey);
314
+ const parsed = this._locate(sessionKey);
247
315
  if (!parsed) return false;
248
316
  const ws = this.getWorkspaces();
249
317
  if (!ws.workspaces[parsed.workspace]) { console.log(`Ignoring response for deleted workspace: ${parsed.workspace}`); return false; }
@@ -253,7 +321,10 @@ export class GatewayClient {
253
321
  // Trim to final-answer portion: only text after the last tool call offset.
254
322
  // thoughtStartOffset is passed in from handleChatEvent (captured before streamState.delete).
255
323
  // Intermediate narration lives in activityLog steps; message.content is the clean final answer.
256
- let content = sanitizeAssistantContent(extractContent(message).substring(thoughtStartOffset));
324
+ const fullContent = extractContent(message);
325
+ // Never let a stale segment offset blank the reply (v4 snapshots can be shorter).
326
+ if (thoughtStartOffset > fullContent.length) thoughtStartOffset = 0;
327
+ let content = sanitizeAssistantContent(fullContent.substring(thoughtStartOffset));
257
328
 
258
329
  // Attach media (MEDIA: paths extracted from exec tool args by handleAgentEvent).
259
330
  // Buffer is read before the empty-content guard — media-only responses (no text) must not be dropped.
@@ -312,7 +383,7 @@ export class GatewayClient {
312
383
  }
313
384
 
314
385
  saveErrorMarker(sessionKey, message) {
315
- const parsed = parseSessionKey(sessionKey);
386
+ const parsed = this._locate(sessionKey);
316
387
  if (!parsed) return;
317
388
  const ws = this.getWorkspaces();
318
389
  if (!ws.workspaces[parsed.workspace]) return;
@@ -336,6 +407,7 @@ export class GatewayClient {
336
407
  const heuristic = firstUserMsg.content.replace(/\n.*/s, '').slice(0, 40).trim() + (firstUserMsg.content.length > 40 ? '...' : '');
337
408
  db.prepare('UPDATE threads SET title = ? WHERE id = ?').run(heuristic, threadId);
338
409
  this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'thread-title-updated', threadId, workspace, title: heuristic }));
410
+ this._syncTitle(db, workspace, threadId, heuristic);
339
411
  }
340
412
  const messages = db.prepare('SELECT role, content FROM messages WHERE thread_id = ? ORDER BY created_at ASC LIMIT 6').all(threadId);
341
413
  if (messages.length < 2) return;
@@ -360,14 +432,24 @@ export class GatewayClient {
360
432
  db.prepare('UPDATE threads SET title = ? WHERE id = ?').run(title, pending.threadId);
361
433
  this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'thread-title-updated', threadId: pending.threadId, workspace: pending.workspace, title }));
362
434
  console.log(`AI title generated for ${pending.threadId}: "${title}"`);
435
+ this._syncTitle(db, pending.workspace, pending.threadId, title);
363
436
  return true;
364
437
  }
365
438
 
439
+ // Copy a generated title to the gateway session's custom name; the throwaway
440
+ // title-generation session is deleted once its answer is in.
441
+ _syncTitle(db, workspace, threadId, title) {
442
+ if (!this.sync) return;
443
+ const row = db.prepare('SELECT session_key FROM threads WHERE id = ?').get(threadId);
444
+ if (row?.session_key) this.sync.titleGenerated(workspace, threadId, row.session_key, title);
445
+ }
446
+
366
447
  handleAgentEvent(payload) {
367
448
  const { runId, stream, data, sessionKey } = payload;
368
449
  if (!runId) return;
369
450
  if (!this.activityLogs.has(runId)) this.activityLogs.set(runId, { sessionKey, steps: [], startTime: Date.now() });
370
451
  const log = this.activityLogs.get(runId);
452
+ if (!log._parsed && sessionKey) log._parsed = this._locate(sessionKey);
371
453
 
372
454
  if (stream === 'thinking') {
373
455
  let step = log.steps.find(s => s.type === 'thinking');
@@ -482,7 +564,7 @@ export class GatewayClient {
482
564
  // "never produced a reply" from "already delivered one".
483
565
  if (data?.phase === 'error' && !this.streamState.has(sessionKey) && !this._syntheticErrorRuns.has(runId) && !this._runsWithDeliveredReply.has(runId)) {
484
566
  this._syntheticErrorRuns.add(runId);
485
- const parsed = parseSessionKey(sessionKey);
567
+ const parsed = this._locate(sessionKey);
486
568
  if (parsed) {
487
569
  // Mirror the state:'error' path — writeActivityToDb just set metadata.pending=true,
488
570
  // and without this the flag survives: loadHistory re-derives has_pending on reconnect,
@@ -553,7 +635,7 @@ export class GatewayClient {
553
635
  const streams = [];
554
636
  for (const [sessionKey, state] of this.streamState.entries()) {
555
637
  if (state.state === 'streaming' && !(state.held?.length > 0)) {
556
- const parsed = parseSessionKey(sessionKey);
638
+ const parsed = this._locate(sessionKey);
557
639
  // Include both old shape (sessionKey/buffer) and new shape (workspace/content) for dual-emit compat
558
640
  streams.push({ sessionKey, threadId: state.threadId, buffer: state.buffer, ...(parsed ? { workspace: parsed.workspace, content: state.buffer.substring(state.thoughtStartOffset || 0) } : {}) });
559
641
  }
@@ -0,0 +1,203 @@
1
+ // `ocplatform clawchats export-history`: writes ClawChats chats that the gateway has no
2
+ // session for into the gateway's legacy session-store format (sessions.json + one JSONL
3
+ // transcript per chat) in each agent's sessions folder. The gateway's own importer
4
+ // (`ocplatform doctor --fix`, gateway stopped) then moves them into its database.
5
+ //
6
+ // Text only: ClawChats stored final reply text plus an activity summary, not full tool
7
+ // calls, so tool calls are not reconstructed (that would feed the model invented history).
8
+
9
+ import fs from 'node:fs';
10
+ import path from 'node:path';
11
+ import crypto from 'node:crypto';
12
+ import { DatabaseSync } from 'node:sqlite';
13
+ import { WebSocket } from 'ws';
14
+ import { loadOrCreateDeviceIdentity, buildDeviceAuth } from './bootstrap/identity.js';
15
+ import { parseSessionKey } from './util/helpers.js';
16
+ import { isPushableTitle, withSuffix } from './gateway-sync.js';
17
+
18
+ const SKIP_CONTENT = new Set(['[Response interrupted]']);
19
+
20
+ /** One-shot gateway connection (same handshake as the connector's GatewayClient). */
21
+ export async function withGateway({ url, token, identityPath }, fn) {
22
+ const ws = new WebSocket(url);
23
+ const pending = new Map();
24
+ let seq = 0;
25
+ const ready = new Promise((resolve, reject) => {
26
+ const timer = setTimeout(() => reject(new Error(`gateway at ${url} did not answer`)), 15000);
27
+ ws.on('error', e => { clearTimeout(timer); reject(new Error(`cannot reach the gateway at ${url} (${e.message}) — is it running?`)); });
28
+ ws.on('message', data => {
29
+ let msg; try { msg = JSON.parse(data.toString()); } catch { return; }
30
+ if (msg.type === 'event' && msg.event === 'connect.challenge') {
31
+ const identity = loadOrCreateDeviceIdentity(identityPath);
32
+ const scopes = ['operator.read'];
33
+ const device = buildDeviceAuth(identity, { clientId: 'gateway-client', clientMode: 'backend', role: 'operator', scopes, token, nonce: msg.payload?.nonce || '' });
34
+ ws.send(JSON.stringify({ type: 'req', id: 'connect', method: 'connect', params: { minProtocol: 3, maxProtocol: 4, client: { id: 'gateway-client', version: '0.1.0', platform: 'node', mode: 'backend' }, role: 'operator', scopes, device, auth: { token } } }));
35
+ return;
36
+ }
37
+ if (msg.type === 'res' && msg.id === 'connect') {
38
+ clearTimeout(timer);
39
+ return msg.ok ? resolve() : reject(new Error(`gateway refused the connection: ${msg.error?.message || 'unknown error'}`));
40
+ }
41
+ if (msg.type === 'res' && pending.has(msg.id)) {
42
+ const p = pending.get(msg.id); pending.delete(msg.id);
43
+ msg.ok ? p.resolve(msg.payload) : p.reject(new Error(msg.error?.message || 'request failed'));
44
+ }
45
+ });
46
+ });
47
+ const request = (method, params = {}) => new Promise((resolve, reject) => {
48
+ const id = `export-${++seq}`;
49
+ pending.set(id, { resolve, reject });
50
+ ws.send(JSON.stringify({ type: 'req', id, method, params }));
51
+ });
52
+ try { await ready; return await fn(request); }
53
+ finally { ws.close(); }
54
+ }
55
+
56
+ async function listAllSessions(request) {
57
+ const out = [];
58
+ let offset = 0;
59
+ for (let page = 0; page < 50; page++) {
60
+ const res = await request('sessions.list', { archived: 'all', limit: 1000, offset });
61
+ out.push(...(res?.sessions || []));
62
+ if (!res?.hasMore || res.nextOffset == null) break;
63
+ offset = res.nextOffset;
64
+ }
65
+ return out;
66
+ }
67
+
68
+ function readThreads(dataDir, project) {
69
+ const file = path.join(dataDir, `${project.name}.db`);
70
+ if (!fs.existsSync(file)) return [];
71
+ const db = new DatabaseSync(file, { readOnly: true });
72
+ try {
73
+ const cols = db.prepare('PRAGMA table_info(threads)').all().map(c => c.name);
74
+ if (!cols.includes('session_key')) return [];
75
+ const threads = db.prepare('SELECT id, title, session_key FROM threads').all();
76
+ const msgStmt = db.prepare(`SELECT role, content, timestamp FROM messages WHERE thread_id = ? AND role IN ('user','assistant') AND content IS NOT NULL AND content != '' ORDER BY timestamp, rowid`);
77
+ return threads.map(t => ({ ...t, project, messages: msgStmt.all(t.id).filter(m => !SKIP_CONTENT.has(m.content.trim())) }));
78
+ } finally { db.close(); }
79
+ }
80
+
81
+ function transcriptLines(sessionId, cwd, messages) {
82
+ const iso = ms => new Date(ms).toISOString();
83
+ const lines = [{ type: 'session', version: 3, id: sessionId, timestamp: iso(messages[0].timestamp), cwd }];
84
+ let parentId = null;
85
+ for (const m of messages) {
86
+ const id = crypto.randomBytes(4).toString('hex');
87
+ const message = m.role === 'user'
88
+ ? { role: 'user', content: m.content, timestamp: m.timestamp }
89
+ : { role: 'assistant', content: [{ type: 'text', text: m.content }], stopReason: 'stop', timestamp: m.timestamp };
90
+ lines.push({ type: 'message', id, parentId, timestamp: iso(m.timestamp), message });
91
+ parentId = id;
92
+ }
93
+ return lines.map(l => JSON.stringify(l)).join('\n') + '\n';
94
+ }
95
+
96
+ /**
97
+ * @param {object} o
98
+ * @param {string} o.dataDir ClawChats data dir (workspaces.json + <project>.db)
99
+ * @param {string} o.stateDir gateway state dir (contains agents/<id>/sessions)
100
+ * @param {string} o.gatewayUrl
101
+ * @param {string} o.token
102
+ * @param {boolean} [o.check] report only, write nothing
103
+ * @param {(line: string) => void} [o.print]
104
+ * @returns {Promise<{ exported: number, skippedExisting: number, skippedEmpty: number, written: string[] }>}
105
+ */
106
+ export async function exportHistory({ dataDir, stateDir, gatewayUrl, token, check = false, print = console.log }) {
107
+ const workspaces = JSON.parse(fs.readFileSync(path.join(dataDir, 'workspaces.json'), 'utf8'));
108
+ const projects = Object.values(workspaces.workspaces || {}).sort((a, b) => (a.order ?? 999) - (b.order ?? 999));
109
+
110
+ const sessions = await withGateway({ url: gatewayUrl, token, identityPath: path.join(dataDir, 'device-identity.json') }, listAllSessions);
111
+ const existing = new Set(sessions.map(s => s.key));
112
+
113
+ const perAgent = new Map(); // agentId -> [thread]
114
+ const report = [];
115
+ let skippedExisting = 0, skippedEmpty = 0;
116
+ const allThreads = [];
117
+ for (const project of projects) {
118
+ const threads = readThreads(dataDir, project);
119
+ allThreads.push(...threads);
120
+ let exp = 0, have = 0, empty = 0;
121
+ for (const t of threads) {
122
+ const parsed = parseSessionKey(t.session_key);
123
+ if (!parsed) continue;
124
+ if (existing.has(t.session_key)) { have++; continue; }
125
+ if (!t.messages.length) { empty++; continue; }
126
+ if (!perAgent.has(parsed.agent)) perAgent.set(parsed.agent, []);
127
+ perAgent.get(parsed.agent).push(t);
128
+ exp++;
129
+ }
130
+ skippedExisting += have; skippedEmpty += empty;
131
+ report.push({ project: project.label, threads: threads.length, export: exp, inGateway: have, empty });
132
+ }
133
+
134
+ // Titles that will get a " (n)" suffix once synced (gateway names are unique per agent).
135
+ const taken = new Map(sessions.filter(s => s.label).map(s => [s.label, s.key]));
136
+ const dupes = new Map();
137
+ for (const t of allThreads) {
138
+ if (!isPushableTitle(t.title)) continue;
139
+ const owner = taken.get(t.title);
140
+ if (owner === t.session_key) continue;
141
+ if (owner) {
142
+ let n = 2; while (taken.has(withSuffix(t.title, n))) n++;
143
+ taken.set(withSuffix(t.title, n), t.session_key);
144
+ dupes.set(t.title, (dupes.get(t.title) || 0) + 1);
145
+ } else taken.set(t.title, t.session_key);
146
+ }
147
+
148
+ // Refuse before writing anything if an agent already has a pending legacy store.
149
+ const targets = [...perAgent.keys()].map(agent => ({ agent, dir: path.join(stateDir, 'agents', agent, 'sessions') }));
150
+ for (const { agent, dir } of targets) {
151
+ const store = path.join(dir, 'sessions.json');
152
+ if (fs.existsSync(store)) {
153
+ throw new Error(`${store} already exists (an unfinished import for agent "${agent}"). Run \`ocplatform gateway stop && ocplatform doctor --fix && ocplatform gateway start\` first, then run export-history again.`);
154
+ }
155
+ }
156
+
157
+ print('ClawChats history export' + (check ? ' (check only — nothing written)' : ''));
158
+ print('');
159
+ for (const r of report) print(` ${r.project.padEnd(24)} ${String(r.export).padStart(4)} to import ${String(r.inGateway).padStart(4)} already in gateway ${r.empty ? `${r.empty} empty (skipped)` : ''}`);
160
+ const total = [...perAgent.values()].reduce((n, l) => n + l.length, 0);
161
+ print('');
162
+ print(` ${total} chats to import, ${skippedExisting} already in the gateway, ${skippedEmpty} empty.`);
163
+ if (dupes.size) {
164
+ print('');
165
+ print(' Duplicate titles (gateway chat names must be unique; these get " (2)", " (3)"… in ClawChats and the gateway):');
166
+ for (const [title, n] of [...dupes].sort((a, b) => b[1] - a[1])) print(` ${String(n).padStart(4)} × "${title}"`);
167
+ }
168
+
169
+ const written = [];
170
+ if (!check && total > 0) {
171
+ for (const { agent, dir } of targets) {
172
+ fs.mkdirSync(dir, { recursive: true });
173
+ const cwd = path.join(stateDir, agent === 'main' ? 'workspace' : `workspace-${agent}`);
174
+ const store = {};
175
+ for (const t of perAgent.get(agent)) {
176
+ const sessionId = crypto.randomUUID();
177
+ const sessionFile = path.join(dir, `${sessionId}.jsonl`);
178
+ fs.writeFileSync(sessionFile, transcriptLines(sessionId, cwd, t.messages));
179
+ const start = t.messages[0].timestamp, end = t.messages[t.messages.length - 1].timestamp;
180
+ store[t.session_key] = { sessionId, sessionFile, updatedAt: end, sessionStartedAt: start, lastInteractionAt: end, chatType: 'direct', lastChannel: 'webchat' };
181
+ written.push(sessionFile);
182
+ }
183
+ const storePath = path.join(dir, 'sessions.json');
184
+ fs.writeFileSync(storePath, JSON.stringify(store, null, 2));
185
+ written.push(storePath);
186
+ }
187
+ print('');
188
+ print(' Written. Finish the import now (the gateway is offline for a minute or two):');
189
+ print('');
190
+ print(' ocplatform gateway stop');
191
+ print(' ocplatform doctor --fix');
192
+ print(' ocplatform gateway start');
193
+ print('');
194
+ print(' Run these right away: until `doctor --fix` runs, the gateway refuses to restart.');
195
+ } else if (check) {
196
+ print('');
197
+ print(' Run `ocplatform clawchats export-history` (without "check") to write the files.');
198
+ } else {
199
+ print('');
200
+ print(' Nothing to import.');
201
+ }
202
+ return { exported: total, skippedExisting, skippedEmpty, written };
203
+ }
package/server/index.js CHANGED
@@ -9,6 +9,7 @@ import { Database, requestDbStore } from './bootstrap/native.js';
9
9
  import { GATEWAY_WS_URL, AUTH_TOKEN, getSessionsDirForAgent } from './config.js';
10
10
  import { DebugLogger } from './debug.js';
11
11
  import { GatewayClient } from './gateway.js';
12
+ import { GatewaySync } from './gateway-sync.js';
12
13
  import { discoverMemoryConfig } from './providers/memory-config.js';
13
14
  import { createMemoryProvider } from './providers/memory.js';
14
15
  import { WorkspaceController } from './controllers/workspaces.js';
@@ -22,7 +23,7 @@ import { handleStatic } from './controllers/static.js';
22
23
  import { handleAgents } from './controllers/agents.js';
23
24
  import { createSettingsHandlers } from './controllers/settings.js';
24
25
  import { createWorkspaceStore } from './store/workspace-store.js';
25
- import { cleanGatewaySession } from './gateway-cleanup.js';
26
+ import { cleanGatewaySession, setGatewayClient } from './gateway-cleanup.js';
26
27
  import { parseSessionKey } from './util/helpers.js';
27
28
  import { send, sendError, parseBody, uuid, matchRoute, setCors } from './util/http.js';
28
29
 
@@ -63,7 +64,7 @@ export function createApp(config = {}) {
63
64
  }
64
65
  function getActiveDb() { return requestDbStore.getStore() || getDb(getWorkspaces().active); }
65
66
  function closeDb(name) { const db = dbCache.get(name); if (db) { db.close(); dbCache.delete(name); } }
66
- function closeAll() { for (const db of dbCache.values()) db.close(); dbCache.clear(); globalDbCache.close?.(); }
67
+ function closeAll() { gatewayClient?.sync?.stop(); for (const db of dbCache.values()) db.close(); dbCache.clear(); globalDbCache.close?.(); }
67
68
 
68
69
  // Global DB (custom emojis, cross-workspace data)
69
70
  let _globalDb = null;
@@ -92,10 +93,13 @@ export function createApp(config = {}) {
92
93
  // Instantiate the gateway client with all dependencies injected
93
94
  const gatewayClient = new GatewayClient({ getDb, getWorkspaces, dataDir: DATA_DIR, debugLogger, gatewayWsUrl: gatewayUrl, authToken: gatewayToken });
94
95
  const broadcast = msg => gatewayClient.broadcastToBrowsers(msg);
96
+ setGatewayClient(gatewayClient);
97
+ const gatewaySync = new GatewaySync({ gateway: gatewayClient, getDb, closeDb, getWorkspaces, setWorkspaces, dataDir: DATA_DIR, uploadsDir: UPLOADS_DIR, broadcast });
98
+ gatewayClient.sync = gatewaySync;
95
99
 
96
100
  // Instantiate controllers
97
- const workspaces = new WorkspaceController({ getDb, closeDb, getWorkspaces, setWorkspaces, dataDir: DATA_DIR, broadcast });
98
- const threads = new ThreadController({ getActiveDb, getWorkspaces, uploadsDir: UPLOADS_DIR, broadcast });
101
+ const workspaces = new WorkspaceController({ getDb, closeDb, getWorkspaces, setWorkspaces, dataDir: DATA_DIR, broadcast, sync: gatewaySync });
102
+ const threads = new ThreadController({ getActiveDb, getWorkspaces, uploadsDir: UPLOADS_DIR, broadcast, sync: gatewaySync });
99
103
  const messages = new MessageController({ getActiveDb, getWorkspaces, broadcast });
100
104
  const files = new FileController({ getActiveDb, getWorkspaces, uploadsDir: UPLOADS_DIR, intelligenceDir: INTELLIGENCE_DIR });
101
105
  const memory = new MemoryController({ memoryProvider, memoryFilesDir: MEMORY_FILES_DIR, memoryConfig });
@@ -221,7 +225,7 @@ export function createApp(config = {}) {
221
225
  if (method === 'GET' && urlPath === '/api/workspaces') return workspaces.getAll(req, res);
222
226
  if (method === 'POST' && urlPath === '/api/workspaces') return await workspaces.create(req, res);
223
227
  if ((p = matchRoute(method, urlPath, 'PATCH /api/workspaces/:name'))) return await workspaces.update(req, res, p);
224
- if ((p = matchRoute(method, urlPath, 'DELETE /api/workspaces/:name'))) return workspaces.delete(req, res, p);
228
+ if ((p = matchRoute(method, urlPath, 'DELETE /api/workspaces/:name'))) return await workspaces.delete(req, res, p);
225
229
  if (method === 'POST' && urlPath === '/api/workspaces/reorder') return await workspaces.reorder(req, res);
226
230
  if ((p = matchRoute(method, urlPath, 'POST /api/workspaces/:name/activate'))) return workspaces.activate(req, res, p);
227
231
 
@@ -233,7 +237,6 @@ export function createApp(config = {}) {
233
237
  if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id/messages'))) return messages.getAll(req, res, p, query);
234
238
  if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/messages'))) return await messages.create(req, res, p);
235
239
  if ((p = matchRoute(method, urlPath, 'DELETE /api/threads/:id/messages/:messageId'))) return messages.delete(req, res, p);
236
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/context-fill'))) return messages.contextFill(req, res, p);
237
240
  if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/generate-title'))) {
238
241
  const db = getActiveDb();
239
242
  const thread = db.prepare('SELECT * FROM threads WHERE id = ?').get(p.id);
@@ -246,7 +249,7 @@ export function createApp(config = {}) {
246
249
  if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/intelligence'))) return await files.saveIntelligence(req, res, p);
247
250
  if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id'))) return threads.get(req, res, p);
248
251
  if ((p = matchRoute(method, urlPath, 'PATCH /api/threads/:id'))) return await threads.update(req, res, p);
249
- if ((p = matchRoute(method, urlPath, 'DELETE /api/threads/:id'))) return threads.delete(req, res, p);
252
+ if ((p = matchRoute(method, urlPath, 'DELETE /api/threads/:id'))) return await threads.delete(req, res, p);
250
253
 
251
254
  // Search / export / import
252
255
  if (method === 'GET' && urlPath === '/api/search') return messages.search(req, res, {}, query);
@@ -269,7 +272,8 @@ export function createApp(config = {}) {
269
272
  sendError(res, 404, `Not found: ${method} ${urlPath}`);
270
273
  } catch (err) {
271
274
  console.error(`Error handling ${method} ${urlPath}:`, err);
272
- if (err.message?.includes('UNIQUE constraint')) sendError(res, 409, 'Conflict: ' + err.message);
275
+ if (err.gatewayError) sendError(res, 502, `Gateway: ${err.message}`);
276
+ else if (err.message?.includes('UNIQUE constraint')) sendError(res, 409, 'Conflict: ' + err.message);
273
277
  else sendError(res, 500, err.message || 'Internal server error');
274
278
  }
275
279
  }
@@ -342,6 +346,7 @@ export function createApp(config = {}) {
342
346
  shutdown: closeAll,
343
347
  closeAllDbs: closeAll,
344
348
  gatewayClient,
349
+ gatewaySync,
345
350
  setupBrowserWs,
346
351
  debugLogger,
347
352
  dataDir: DATA_DIR,
@@ -367,6 +372,9 @@ function migrate(db) {
367
372
  try { db.exec('ALTER TABLE threads ADD COLUMN sort_order INTEGER DEFAULT 0'); } catch { /* exists */ }
368
373
  try { db.exec('ALTER TABLE threads ADD COLUMN unread_count INTEGER DEFAULT 0'); } catch { /* exists */ }
369
374
  try { db.prepare('ALTER TABLE threads ADD COLUMN metadata TEXT DEFAULT NULL').run(); } catch {}
375
+ // When the connector first saw this thread's gateway session (gateway sync). NULL = never
376
+ // synced: the thread's state is pushed to the gateway, never pulled or deleted from it.
377
+ try { db.exec('ALTER TABLE threads ADD COLUMN gateway_seen_at INTEGER DEFAULT NULL'); } catch { /* exists */ }
370
378
  db.exec(`CREATE TABLE IF NOT EXISTS unread_messages (thread_id TEXT NOT NULL, message_id TEXT NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY (thread_id, message_id), FOREIGN KEY (thread_id) REFERENCES threads(id) ON DELETE CASCADE)`);
371
379
  db.exec('CREATE INDEX IF NOT EXISTS idx_unread_thread ON unread_messages(thread_id)');
372
380
  ensureFts(db);
@@ -0,0 +1,47 @@
1
+ // Move a thread (row + messages + unread markers) between per-project SQLite files.
2
+ // Projects are separate databases, so a move is copy-then-delete: the target copy is
3
+ // committed before the source row is removed, so a failure never loses the thread.
4
+
5
+ const TABLES = ['threads', 'messages', 'unread_messages'];
6
+
7
+ function columns(db, table) {
8
+ return db.prepare(`PRAGMA table_info(${table})`).all().map(c => c.name);
9
+ }
10
+
11
+ function inTransaction(db, fn) {
12
+ db.exec('BEGIN IMMEDIATE');
13
+ try { const r = fn(); db.exec('COMMIT'); return r; }
14
+ catch (e) { try { db.exec('ROLLBACK'); } catch { /* already rolled back */ } throw e; }
15
+ }
16
+
17
+ /**
18
+ * @returns {boolean} true when the thread was moved, false when it wasn't in `from`.
19
+ */
20
+ export function moveThread(getDb, threadId, from, to) {
21
+ if (from === to) return false;
22
+ const src = getDb(from);
23
+ const dst = getDb(to);
24
+ const thread = src.prepare('SELECT * FROM threads WHERE id = ?').get(threadId);
25
+ if (!thread) return false;
26
+
27
+ const rows = {
28
+ threads: [thread],
29
+ messages: src.prepare('SELECT * FROM messages WHERE thread_id = ? ORDER BY timestamp, rowid').all(threadId),
30
+ unread_messages: src.prepare('SELECT * FROM unread_messages WHERE thread_id = ?').all(threadId),
31
+ };
32
+
33
+ inTransaction(dst, () => {
34
+ // A leftover copy from an interrupted earlier move is replaced (cascade clears its children).
35
+ dst.prepare('DELETE FROM threads WHERE id = ?').run(threadId);
36
+ for (const table of TABLES) {
37
+ if (!rows[table].length) continue;
38
+ const dstCols = new Set(columns(dst, table));
39
+ const cols = Object.keys(rows[table][0]).filter(c => dstCols.has(c));
40
+ const stmt = dst.prepare(`INSERT INTO ${table} (${cols.join(', ')}) VALUES (${cols.map(() => '?').join(', ')})`);
41
+ for (const row of rows[table]) stmt.run(...cols.map(c => row[c] ?? null));
42
+ }
43
+ });
44
+
45
+ inTransaction(src, () => { src.prepare('DELETE FROM threads WHERE id = ?').run(threadId); });
46
+ return true;
47
+ }