@clawchatsai/connector 0.1.15 → 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/dist/index.js +34 -0
- package/dist/plugin-config.d.ts +29 -0
- package/dist/plugin-config.js +1 -0
- package/package.json +1 -1
- package/server/config.js +0 -1
- package/server/controllers/messages.js +0 -8
- package/server/controllers/threads.js +17 -5
- package/server/controllers/workspaces.js +20 -7
- package/server/gateway-sync.js +571 -0
- package/server/gateway.js +43 -12
- package/server/history-export.js +203 -0
- package/server/index.js +14 -7
- package/server/thread-move.js +47 -0
- package/dist/gateway-bridge.d.ts +0 -73
- package/dist/gateway-bridge.js +0 -255
- package/server/util/context.js +0 -49
package/server/gateway.js
CHANGED
|
@@ -85,13 +85,18 @@ export class GatewayClient {
|
|
|
85
85
|
}, 5 * 60 * 1000);
|
|
86
86
|
}
|
|
87
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
|
+
|
|
88
93
|
connect() {
|
|
89
94
|
if (this.ws && (this.ws.readyState === WS.CONNECTING || this.ws.readyState === WS.OPEN)) return;
|
|
90
95
|
console.log(`Connecting to gateway at ${this.gatewayWsUrl}...`);
|
|
91
96
|
this.ws = new WS(this.gatewayWsUrl);
|
|
92
97
|
this.ws.on('open', () => { console.log('Gateway WebSocket connected'); this.reconnectAttempts = 0; });
|
|
93
98
|
this.ws.on('message', data => this.handleGatewayMessage(data.toString()));
|
|
94
|
-
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(); });
|
|
95
100
|
this.ws.on('error', err => console.error('Gateway WebSocket error:', err.message));
|
|
96
101
|
}
|
|
97
102
|
|
|
@@ -116,7 +121,13 @@ export class GatewayClient {
|
|
|
116
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'] } }));
|
|
117
122
|
return;
|
|
118
123
|
}
|
|
119
|
-
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
|
+
}
|
|
120
131
|
if (msg.type === 'event' && msg.event === 'chat' && msg.payload) {
|
|
121
132
|
this.handleChatEvent(msg.payload, data);
|
|
122
133
|
} else {
|
|
@@ -150,7 +161,7 @@ export class GatewayClient {
|
|
|
150
161
|
|
|
151
162
|
// --- Delta path ---
|
|
152
163
|
if (state === 'delta') {
|
|
153
|
-
const parsed =
|
|
164
|
+
const parsed = this._locate(sessionKey);
|
|
154
165
|
if (parsed) {
|
|
155
166
|
const existing = this.streamState.get(sessionKey) || { buffer: '', threadId: parsed.threadId, state: 'streaming', held: [] };
|
|
156
167
|
const prevLen = existing.buffer.length; // capture before update — used to advance thoughtStartOffset on first post-tool delta
|
|
@@ -194,14 +205,19 @@ export class GatewayClient {
|
|
|
194
205
|
|
|
195
206
|
// Title sessions are handled server-side — intercept and skip browser delivery
|
|
196
207
|
if (sessionKey?.includes('__clawchats_title_')) {
|
|
197
|
-
if (state === 'final') {
|
|
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
|
+
}
|
|
198
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; }
|
|
199
215
|
return;
|
|
200
216
|
}
|
|
201
217
|
|
|
202
218
|
if (state === 'final') {
|
|
203
219
|
const rawContent = extractContent(message);
|
|
204
|
-
const parsed =
|
|
220
|
+
const parsed = this._locate(sessionKey);
|
|
205
221
|
if (isSilentReplyExact(rawContent, 'NO_REPLY') || isSilentReplyExact(rawContent, 'HEARTBEAT_OK')) {
|
|
206
222
|
this._cleanupSilentPending(sessionKey);
|
|
207
223
|
if (parsed) this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-end', threadId: parsed.threadId, workspace: parsed.workspace, reason: 'silent' }));
|
|
@@ -218,7 +234,7 @@ export class GatewayClient {
|
|
|
218
234
|
return;
|
|
219
235
|
}
|
|
220
236
|
if (state === 'aborted') {
|
|
221
|
-
const parsed =
|
|
237
|
+
const parsed = this._locate(sessionKey);
|
|
222
238
|
// Clear pending flag from DB so a stale pending:true doesn't survive page reloads
|
|
223
239
|
// and trigger phantom "thinking..." state on next visit to this thread.
|
|
224
240
|
if (parsed) {
|
|
@@ -238,7 +254,7 @@ export class GatewayClient {
|
|
|
238
254
|
console.log(`[clawchats] ignoring late error for run ${runId} — reply already delivered`);
|
|
239
255
|
return;
|
|
240
256
|
}
|
|
241
|
-
const parsed =
|
|
257
|
+
const parsed = this._locate(sessionKey);
|
|
242
258
|
this.saveErrorMarker(sessionKey, message);
|
|
243
259
|
this.broadcastToBrowsers(rawData); // dual-emit
|
|
244
260
|
if (parsed) {
|
|
@@ -259,6 +275,10 @@ export class GatewayClient {
|
|
|
259
275
|
});
|
|
260
276
|
}
|
|
261
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
|
+
|
|
262
282
|
// Utility sessions keep no streamState entry; accumulate their delta text separately.
|
|
263
283
|
_accumulateChatText(sessionKey, params) {
|
|
264
284
|
const text = nextChatText(this._utilityText.get(sessionKey) || '', params);
|
|
@@ -276,7 +296,7 @@ export class GatewayClient {
|
|
|
276
296
|
}
|
|
277
297
|
|
|
278
298
|
_cleanupSilentPending(sessionKey) {
|
|
279
|
-
const parsed =
|
|
299
|
+
const parsed = this._locate(sessionKey);
|
|
280
300
|
if (!parsed) return;
|
|
281
301
|
const ws = this.getWorkspaces();
|
|
282
302
|
if (!ws.workspaces[parsed.workspace]) return;
|
|
@@ -291,7 +311,7 @@ export class GatewayClient {
|
|
|
291
311
|
// early-out. handleChatEvent uses the result to decide whether the run counts as
|
|
292
312
|
// having delivered a visible reply.
|
|
293
313
|
saveAssistantMessage(sessionKey, message, seq, thoughtStartOffset = 0) {
|
|
294
|
-
const parsed =
|
|
314
|
+
const parsed = this._locate(sessionKey);
|
|
295
315
|
if (!parsed) return false;
|
|
296
316
|
const ws = this.getWorkspaces();
|
|
297
317
|
if (!ws.workspaces[parsed.workspace]) { console.log(`Ignoring response for deleted workspace: ${parsed.workspace}`); return false; }
|
|
@@ -363,7 +383,7 @@ export class GatewayClient {
|
|
|
363
383
|
}
|
|
364
384
|
|
|
365
385
|
saveErrorMarker(sessionKey, message) {
|
|
366
|
-
const parsed =
|
|
386
|
+
const parsed = this._locate(sessionKey);
|
|
367
387
|
if (!parsed) return;
|
|
368
388
|
const ws = this.getWorkspaces();
|
|
369
389
|
if (!ws.workspaces[parsed.workspace]) return;
|
|
@@ -387,6 +407,7 @@ export class GatewayClient {
|
|
|
387
407
|
const heuristic = firstUserMsg.content.replace(/\n.*/s, '').slice(0, 40).trim() + (firstUserMsg.content.length > 40 ? '...' : '');
|
|
388
408
|
db.prepare('UPDATE threads SET title = ? WHERE id = ?').run(heuristic, threadId);
|
|
389
409
|
this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'thread-title-updated', threadId, workspace, title: heuristic }));
|
|
410
|
+
this._syncTitle(db, workspace, threadId, heuristic);
|
|
390
411
|
}
|
|
391
412
|
const messages = db.prepare('SELECT role, content FROM messages WHERE thread_id = ? ORDER BY created_at ASC LIMIT 6').all(threadId);
|
|
392
413
|
if (messages.length < 2) return;
|
|
@@ -411,14 +432,24 @@ export class GatewayClient {
|
|
|
411
432
|
db.prepare('UPDATE threads SET title = ? WHERE id = ?').run(title, pending.threadId);
|
|
412
433
|
this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'thread-title-updated', threadId: pending.threadId, workspace: pending.workspace, title }));
|
|
413
434
|
console.log(`AI title generated for ${pending.threadId}: "${title}"`);
|
|
435
|
+
this._syncTitle(db, pending.workspace, pending.threadId, title);
|
|
414
436
|
return true;
|
|
415
437
|
}
|
|
416
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
|
+
|
|
417
447
|
handleAgentEvent(payload) {
|
|
418
448
|
const { runId, stream, data, sessionKey } = payload;
|
|
419
449
|
if (!runId) return;
|
|
420
450
|
if (!this.activityLogs.has(runId)) this.activityLogs.set(runId, { sessionKey, steps: [], startTime: Date.now() });
|
|
421
451
|
const log = this.activityLogs.get(runId);
|
|
452
|
+
if (!log._parsed && sessionKey) log._parsed = this._locate(sessionKey);
|
|
422
453
|
|
|
423
454
|
if (stream === 'thinking') {
|
|
424
455
|
let step = log.steps.find(s => s.type === 'thinking');
|
|
@@ -533,7 +564,7 @@ export class GatewayClient {
|
|
|
533
564
|
// "never produced a reply" from "already delivered one".
|
|
534
565
|
if (data?.phase === 'error' && !this.streamState.has(sessionKey) && !this._syntheticErrorRuns.has(runId) && !this._runsWithDeliveredReply.has(runId)) {
|
|
535
566
|
this._syntheticErrorRuns.add(runId);
|
|
536
|
-
const parsed =
|
|
567
|
+
const parsed = this._locate(sessionKey);
|
|
537
568
|
if (parsed) {
|
|
538
569
|
// Mirror the state:'error' path — writeActivityToDb just set metadata.pending=true,
|
|
539
570
|
// and without this the flag survives: loadHistory re-derives has_pending on reconnect,
|
|
@@ -604,7 +635,7 @@ export class GatewayClient {
|
|
|
604
635
|
const streams = [];
|
|
605
636
|
for (const [sessionKey, state] of this.streamState.entries()) {
|
|
606
637
|
if (state.state === 'streaming' && !(state.held?.length > 0)) {
|
|
607
|
-
const parsed =
|
|
638
|
+
const parsed = this._locate(sessionKey);
|
|
608
639
|
// Include both old shape (sessionKey/buffer) and new shape (workspace/content) for dual-emit compat
|
|
609
640
|
streams.push({ sessionKey, threadId: state.threadId, buffer: state.buffer, ...(parsed ? { workspace: parsed.workspace, content: state.buffer.substring(state.thoughtStartOffset || 0) } : {}) });
|
|
610
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';
|
|
@@ -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;
|
|
@@ -93,10 +94,12 @@ export function createApp(config = {}) {
|
|
|
93
94
|
const gatewayClient = new GatewayClient({ getDb, getWorkspaces, dataDir: DATA_DIR, debugLogger, gatewayWsUrl: gatewayUrl, authToken: gatewayToken });
|
|
94
95
|
const broadcast = msg => gatewayClient.broadcastToBrowsers(msg);
|
|
95
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;
|
|
96
99
|
|
|
97
100
|
// Instantiate controllers
|
|
98
|
-
const workspaces = new WorkspaceController({ getDb, closeDb, getWorkspaces, setWorkspaces, dataDir: DATA_DIR, broadcast });
|
|
99
|
-
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 });
|
|
100
103
|
const messages = new MessageController({ getActiveDb, getWorkspaces, broadcast });
|
|
101
104
|
const files = new FileController({ getActiveDb, getWorkspaces, uploadsDir: UPLOADS_DIR, intelligenceDir: INTELLIGENCE_DIR });
|
|
102
105
|
const memory = new MemoryController({ memoryProvider, memoryFilesDir: MEMORY_FILES_DIR, memoryConfig });
|
|
@@ -222,7 +225,7 @@ export function createApp(config = {}) {
|
|
|
222
225
|
if (method === 'GET' && urlPath === '/api/workspaces') return workspaces.getAll(req, res);
|
|
223
226
|
if (method === 'POST' && urlPath === '/api/workspaces') return await workspaces.create(req, res);
|
|
224
227
|
if ((p = matchRoute(method, urlPath, 'PATCH /api/workspaces/:name'))) return await workspaces.update(req, res, p);
|
|
225
|
-
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);
|
|
226
229
|
if (method === 'POST' && urlPath === '/api/workspaces/reorder') return await workspaces.reorder(req, res);
|
|
227
230
|
if ((p = matchRoute(method, urlPath, 'POST /api/workspaces/:name/activate'))) return workspaces.activate(req, res, p);
|
|
228
231
|
|
|
@@ -234,7 +237,6 @@ export function createApp(config = {}) {
|
|
|
234
237
|
if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id/messages'))) return messages.getAll(req, res, p, query);
|
|
235
238
|
if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/messages'))) return await messages.create(req, res, p);
|
|
236
239
|
if ((p = matchRoute(method, urlPath, 'DELETE /api/threads/:id/messages/:messageId'))) return messages.delete(req, res, p);
|
|
237
|
-
if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/context-fill'))) return messages.contextFill(req, res, p);
|
|
238
240
|
if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/generate-title'))) {
|
|
239
241
|
const db = getActiveDb();
|
|
240
242
|
const thread = db.prepare('SELECT * FROM threads WHERE id = ?').get(p.id);
|
|
@@ -247,7 +249,7 @@ export function createApp(config = {}) {
|
|
|
247
249
|
if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/intelligence'))) return await files.saveIntelligence(req, res, p);
|
|
248
250
|
if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id'))) return threads.get(req, res, p);
|
|
249
251
|
if ((p = matchRoute(method, urlPath, 'PATCH /api/threads/:id'))) return await threads.update(req, res, p);
|
|
250
|
-
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);
|
|
251
253
|
|
|
252
254
|
// Search / export / import
|
|
253
255
|
if (method === 'GET' && urlPath === '/api/search') return messages.search(req, res, {}, query);
|
|
@@ -270,7 +272,8 @@ export function createApp(config = {}) {
|
|
|
270
272
|
sendError(res, 404, `Not found: ${method} ${urlPath}`);
|
|
271
273
|
} catch (err) {
|
|
272
274
|
console.error(`Error handling ${method} ${urlPath}:`, err);
|
|
273
|
-
if (err.
|
|
275
|
+
if (err.gatewayError) sendError(res, 502, `Gateway: ${err.message}`);
|
|
276
|
+
else if (err.message?.includes('UNIQUE constraint')) sendError(res, 409, 'Conflict: ' + err.message);
|
|
274
277
|
else sendError(res, 500, err.message || 'Internal server error');
|
|
275
278
|
}
|
|
276
279
|
}
|
|
@@ -343,6 +346,7 @@ export function createApp(config = {}) {
|
|
|
343
346
|
shutdown: closeAll,
|
|
344
347
|
closeAllDbs: closeAll,
|
|
345
348
|
gatewayClient,
|
|
349
|
+
gatewaySync,
|
|
346
350
|
setupBrowserWs,
|
|
347
351
|
debugLogger,
|
|
348
352
|
dataDir: DATA_DIR,
|
|
@@ -368,6 +372,9 @@ function migrate(db) {
|
|
|
368
372
|
try { db.exec('ALTER TABLE threads ADD COLUMN sort_order INTEGER DEFAULT 0'); } catch { /* exists */ }
|
|
369
373
|
try { db.exec('ALTER TABLE threads ADD COLUMN unread_count INTEGER DEFAULT 0'); } catch { /* exists */ }
|
|
370
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 */ }
|
|
371
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)`);
|
|
372
379
|
db.exec('CREATE INDEX IF NOT EXISTS idx_unread_thread ON unread_messages(thread_id)');
|
|
373
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
|
+
}
|
package/dist/gateway-bridge.d.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GatewayBridge — persistent WebSocket connection to the local OpenClaw gateway.
|
|
3
|
-
*
|
|
4
|
-
* Responsibilities:
|
|
5
|
-
* 1. Connect to the local gateway using token-only auth (loopback connections
|
|
6
|
-
* with valid token skip device identity entirely).
|
|
7
|
-
* 2. Broadcast incoming gateway events to all connected DataChannel clients
|
|
8
|
-
* via the 'gateway-event' EventEmitter event.
|
|
9
|
-
* 3. Reconnect with exponential backoff on disconnect.
|
|
10
|
-
*/
|
|
11
|
-
import { EventEmitter } from 'node:events';
|
|
12
|
-
export interface PluginConfig {
|
|
13
|
-
userId: string;
|
|
14
|
-
serverUrl: string;
|
|
15
|
-
apiKey: string;
|
|
16
|
-
gatewayId?: string;
|
|
17
|
-
gatewayToken?: string;
|
|
18
|
-
devicePrivateKey?: string;
|
|
19
|
-
schemaVersion: number;
|
|
20
|
-
installedAt: string;
|
|
21
|
-
totp?: {
|
|
22
|
-
secret: string;
|
|
23
|
-
algorithm: string;
|
|
24
|
-
digits: number;
|
|
25
|
-
period: number;
|
|
26
|
-
enabledAt: string;
|
|
27
|
-
};
|
|
28
|
-
google?: {
|
|
29
|
-
clientId: string;
|
|
30
|
-
authorizedSub: string;
|
|
31
|
-
authorizedEmail: string;
|
|
32
|
-
};
|
|
33
|
-
sessionSecret?: string;
|
|
34
|
-
backupCodeHashes?: string[];
|
|
35
|
-
totpPending?: {
|
|
36
|
-
secret: string;
|
|
37
|
-
generatedAt: string;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
export interface BridgeConfig {
|
|
41
|
-
gatewayToken: string;
|
|
42
|
-
dataDir: string;
|
|
43
|
-
}
|
|
44
|
-
export declare class GatewayBridge extends EventEmitter {
|
|
45
|
-
private readonly gatewayUrl;
|
|
46
|
-
private readonly config;
|
|
47
|
-
private ws;
|
|
48
|
-
private _isConnected;
|
|
49
|
-
private destroyed;
|
|
50
|
-
private reconnectTimer;
|
|
51
|
-
private backoffMs;
|
|
52
|
-
constructor(gatewayUrl: string, config: BridgeConfig);
|
|
53
|
-
/**
|
|
54
|
-
* Initiate the connection. Resolves once the WebSocket has been opened
|
|
55
|
-
* (not necessarily after the gateway handshake completes — listen for
|
|
56
|
-
* the 'connected' event for that).
|
|
57
|
-
*/
|
|
58
|
-
connect(): Promise<void>;
|
|
59
|
-
/**
|
|
60
|
-
* Forward a message to the gateway (transparent proxy).
|
|
61
|
-
*/
|
|
62
|
-
send(data: string): void;
|
|
63
|
-
/**
|
|
64
|
-
* Permanently close the connection and cancel any pending reconnect.
|
|
65
|
-
*/
|
|
66
|
-
disconnect(): void;
|
|
67
|
-
get isConnected(): boolean;
|
|
68
|
-
private _openSocket;
|
|
69
|
-
private _handleMessage;
|
|
70
|
-
private _sendConnect;
|
|
71
|
-
private _scheduleReconnect;
|
|
72
|
-
private _cancelReconnect;
|
|
73
|
-
}
|