@clawchatsai/connector 0.1.15 → 0.1.17

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
@@ -1,97 +1,34 @@
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 { parseSessionKey, extractContent, isSilentReplyExact, isSilentReplyPrefix, sanitizeAssistantContent, syncThreadUnreadCount, generateActivitySummary, writeActivityToDb } from './util/helpers.js';
5
4
 
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
- }
5
+ // Per-session run/transcript events; hidden sessions' ones are dropped by the lens.
6
+ const RUN_EVENTS = new Set(['chat', 'agent', 'session.tool', 'session.operation', 'session.observer']);
18
7
 
19
8
  export class GatewayClient {
20
- constructor({ getDb, getWorkspaces, dataDir, debugLogger, gatewayWsUrl, authToken }) {
21
- this.getDb = getDb;
22
- this.getWorkspaces = getWorkspaces;
9
+ constructor({ dataDir, debugLogger, gatewayWsUrl, authToken }) {
23
10
  this.dataDir = dataDir;
24
11
  this.debugLogger = debugLogger;
25
12
  this.gatewayWsUrl = gatewayWsUrl;
26
13
  this.authToken = authToken;
27
- // Per-session buffer of attachment paths captured during a run. Drained by saveAssistantMessage.
28
- // Sources: (1) MEDIA: paths from exec tool's echo args, (2) Write/Edit tool args.path.
29
- // Lives on the stable singleton — safe from plugin re-registration that broke the old closure-based stash.
30
- this._runMediaPaths = new Map();
31
- // Per-toolCallId staging for Write/Edit: path captured at phase:start, promoted to _runMediaPaths
32
- // at phase:result if the call succeeded. Keyed by toolCallId; value: { sessionKey, path }.
33
- this._runWriteBuffers = new Map();
34
-
35
14
  this.ws = null;
36
15
  this.connected = false;
37
16
  this.reconnectAttempts = 0;
38
17
  this.maxReconnectDelay = 30000;
39
18
  this.browserClients = new Map();
40
19
  this._externalBroadcastTargets = [];
41
- this.streamState = new Map();
42
- this._utilityText = new Map(); // sessionKey → accumulated delta text for utility sessions
43
20
  this._rpc = new Map(); // request id → { resolve, reject, timer } for connector-originated RPCs
44
21
  this._rpcSeq = 0;
45
- this.activityLogs = new Map();
46
- this._pendingTitleGens = new Map();
47
- // Runs we've already synthesized a streaming-end{reason:'error'} for
48
- // (agent lifecycle:error emitted but no chat state:error ever will). Prevents
49
- // double-fires across retries and cross-path dedup with handleChatEvent.
50
- this._syntheticErrorRuns = new Set();
51
-
52
- // Runs that already delivered a reply the user can see (persisted + broadcast).
53
- // The gateway may report a run-level fault AFTER the reply is delivered — a retried
54
- // tool call, or a post_turn/gateway_draining fault (see OpenClaw's
55
- // AGENT_RUN_TERMINAL_RETRY_GRACE_MS deferral). Those describe the run, not the reply,
56
- // and must never overwrite a message the user already read. Keyed by runId because
57
- // sessionKey is stable for the whole thread — a late error from turn N must not be
58
- // confused with a live error from turn N+1. Mirrors the OCPlatform TUI's
59
- // finalizedRunsWithDisplay guard.
60
- this._runsWithDeliveredReply = new Set();
61
-
62
- // On startup: mark any pending activity log messages from previous crashed/restarted sessions
63
- // as interrupted. Without this, stale pending=true rows survive gateway restarts and cause
64
- // phantom "thinking..." indicators in the browser on reconnect.
65
- try {
66
- const workspaces = this.getWorkspaces();
67
- for (const wsName of Object.keys(workspaces.workspaces || {})) {
68
- const db = this.getDb(wsName);
69
- if (db) db.prepare(`UPDATE messages SET content = '[Response interrupted]', metadata = json_remove(metadata, '$.pending') WHERE content = '' AND json_extract(metadata, '$.pending') = 1`).run();
70
- }
71
- } catch (e) { console.log('[clawchats] startup pending cleanup skipped:', e.message); }
72
-
73
- // Periodically clean up stale activity logs (>10 min old)
74
- setInterval(() => {
75
- const cutoff = Date.now() - 10 * 60 * 1000;
76
- for (const [runId, log] of this.activityLogs) {
77
- if (log.startTime < cutoff) {
78
- if (log._messageId) {
79
- const db = this.getDb(log._parsed?.workspace);
80
- if (db) db.prepare(`UPDATE messages SET content = '[Response interrupted]', metadata = json_remove(metadata, '$.pending') WHERE id = ? AND content = ''`).run(log._messageId);
81
- }
82
- this.activityLogs.delete(runId);
83
- }
84
- }
85
- }, 5 * 60 * 1000);
86
22
  }
87
23
 
88
24
  connect() {
25
+ if (this._closed) return;
89
26
  if (this.ws && (this.ws.readyState === WS.CONNECTING || this.ws.readyState === WS.OPEN)) return;
90
27
  console.log(`Connecting to gateway at ${this.gatewayWsUrl}...`);
91
28
  this.ws = new WS(this.gatewayWsUrl);
92
29
  this.ws.on('open', () => { console.log('Gateway WebSocket connected'); this.reconnectAttempts = 0; });
93
30
  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(); });
31
+ this.ws.on('close', () => { console.log('Gateway WebSocket closed'); this.connected = false; this._failPendingRpcs('gateway disconnected'); this.lens?.reset(); this.broadcastGatewayStatus(false); if (!this._closed) this.scheduleReconnect(); });
95
32
  this.ws.on('error', err => console.error('Gateway WebSocket error:', err.message));
96
33
  }
97
34
 
@@ -110,142 +47,34 @@ export class GatewayClient {
110
47
  return;
111
48
  }
112
49
 
113
- if (msg.type === 'event' && msg.event === 'connect.challenge') {
114
- const identity = loadOrCreateDeviceIdentity(path.join(this.dataDir, 'device-identity.json'));
115
- 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 || '' });
116
- 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'] } }));
50
+ // Responses to browser session requests the lens filters (roster, search, groups).
51
+ if (msg.type === 'res' && this.lens?.ownsResponse(msg.id)) {
52
+ this.broadcastToBrowsers(JSON.stringify(this.lens.response(msg)));
117
53
  return;
118
54
  }
119
- if (msg.type === 'res' && msg.payload?.type === 'hello-ok') { console.log('Gateway handshake complete'); this.connected = true; this.broadcastGatewayStatus(true); }
120
- if (msg.type === 'event' && msg.event === 'chat' && msg.payload) {
121
- this.handleChatEvent(msg.payload, data);
122
- } else {
123
- this.broadcastToBrowsers(data);
124
- }
125
- if (msg.type === 'event' && msg.event === 'agent' && msg.payload) this.handleAgentEvent(msg.payload);
126
- }
127
-
128
- handleChatEvent(params, rawData) {
129
- const { sessionKey, state, message, seq, runId, errorMessage: gwErrorMessage } = params;
130
- const bareSessionKey = sessionKey.replace(/^agent:[^:]+:/, '');
131
55
 
132
- // --- Utility session routing (connector-side) ---
133
- // Emits semantic utility-response events. Also forwards rawData for dual-emit compatibility
134
- // during transition — browser handleChatEvent guards these and defers to utility-response handler.
135
- const UTILITY_SESSIONS = { '__clawchats_summarizer': 'summarizer', '__clawchats_semantic': 'semantic', '__clawchats_intelligence': 'intelligence' };
136
- const utilityName = UTILITY_SESSIONS[bareSessionKey];
137
- if (utilityName) {
138
- const content = state === 'delta' ? this._accumulateChatText(sessionKey, params) : extractContent(message);
139
- if (state !== 'delta') this._utilityText.delete(sessionKey);
140
- if (state === 'delta' && content) {
141
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'utility-response', session: utilityName, state: 'delta', content }));
142
- } else if (state === 'final' || state === 'aborted') {
143
- if (content) this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'utility-response', session: utilityName, state: state === 'final' ? 'final' : 'aborted', content }));
144
- } else if (state === 'error') {
145
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'utility-response', session: utilityName, state: 'error', errorMessage: gwErrorMessage || message?.error || message?.content || 'Unknown error' }));
146
- }
147
- this.broadcastToBrowsers(rawData); // dual-emit: browser guard skips raw handling for utility sessions
56
+ 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'] } }));
148
60
  return;
149
61
  }
150
-
151
- // --- Delta path ---
152
- if (state === 'delta') {
153
- const parsed = parseSessionKey(sessionKey);
154
- if (parsed) {
155
- const existing = this.streamState.get(sessionKey) || { buffer: '', threadId: parsed.threadId, state: 'streaming', held: [] };
156
- const prevLen = existing.buffer.length; // capture before update — used to advance thoughtStartOffset on first post-tool delta
157
- const prevBuffer = existing.buffer;
158
- existing.buffer = nextChatText(existing.buffer, params);
159
- if (isSilentReplyPrefix(existing.buffer, 'NO_REPLY') || isSilentReplyPrefix(existing.buffer, 'HEARTBEAT_OK')) {
160
- existing.held = existing.held || [];
161
- existing.held.push(rawData);
162
- this.streamState.set(sessionKey, existing);
163
- return;
164
- }
165
- if (existing.held?.length > 0) {
166
- for (const h of existing.held) this.broadcastToBrowsers(h);
167
- existing.held = [];
168
- }
169
- this.streamState.set(sessionKey, existing);
170
- this.broadcastToBrowsers(rawData); // dual-emit: raw chat event (old protocol)
171
- // On the first delta after a tool result, advance the segment offset and signal the
172
- // frontend to clear the bubble. Both happen here — right before the new-segment delta —
173
- // so the clear and fill are atomic from the browser's perspective.
174
- if (existing.pendingReset) {
175
- // A v4 gateway may open the post-tool segment with a fresh snapshot instead of
176
- // extending the run text; only skip the old text if it's still a prefix.
177
- existing.thoughtStartOffset = existing.buffer.startsWith(prevBuffer) ? prevLen : 0;
178
- existing.pendingReset = false;
179
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-reset', threadId: parsed.threadId, workspace: parsed.workspace }));
180
- }
181
- // Broadcast only the current-segment portion (text after the last tool call offset).
182
- // thoughtStartOffset advances on first post-tool delta; 0 means no tools yet → full buffer.
183
- const visibleContent = existing.buffer.substring(existing.thoughtStartOffset || 0);
184
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-delta', threadId: parsed.threadId, workspace: parsed.workspace, content: visibleContent }));
185
- } else {
186
- this.broadcastToBrowsers(rawData); // non-clawchats session (discord, telegram, etc.)
187
- }
62
+ 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}`)); }
63
+ // Session events arrive through the connector's own subscription; the lens filters them
64
+ // for the browser (session.message only for chats a browser subscribed to).
65
+ if (msg.type === 'event' && (msg.event === 'sessions.changed' || msg.event === 'session.message')) {
66
+ if (msg.event === 'sessions.changed') this.sync?.onSessionsChanged(msg.payload);
67
+ const frame = msg.event === 'sessions.changed' ? this.lens?.sessionsChanged(msg.payload) : this.lens?.sessionMessage(msg.payload);
68
+ if (frame) this.broadcastToBrowsers(JSON.stringify(frame));
188
69
  return;
189
70
  }
190
-
191
- // --- End states ---
192
- const streamEntry = this.streamState.get(sessionKey);
193
- if (state === 'final' || state === 'aborted' || state === 'error') this.streamState.delete(sessionKey);
194
-
195
- // Title sessions are handled server-side — intercept and skip browser delivery
196
- if (sessionKey?.includes('__clawchats_title_')) {
197
- if (state === 'final') { const content = extractContent(message); if (content && this.handleTitleResponse(sessionKey, content)) return; }
198
- 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; }
71
+ // Run events of sessions ClawChats doesn't show (channels, cron, subagents) never
72
+ // leave the connector.
73
+ if (msg.type === 'event' && RUN_EVENTS.has(msg.event) && this.lens && msg.payload?.sessionKey && !this.lens.forwardsRunEvent(msg.payload.sessionKey)) {
199
74
  return;
200
75
  }
201
-
202
- if (state === 'final') {
203
- const rawContent = extractContent(message);
204
- const parsed = parseSessionKey(sessionKey);
205
- if (isSilentReplyExact(rawContent, 'NO_REPLY') || isSilentReplyExact(rawContent, 'HEARTBEAT_OK')) {
206
- this._cleanupSilentPending(sessionKey);
207
- if (parsed) this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-end', threadId: parsed.threadId, workspace: parsed.workspace, reason: 'silent' }));
208
- return;
209
- }
210
- if (streamEntry?.held?.length > 0) for (const h of streamEntry.held) this.broadcastToBrowsers(h);
211
- const delivered = this.saveAssistantMessage(sessionKey, message, seq, streamEntry?.thoughtStartOffset); // persist + broadcast message-saved first
212
- if (delivered && runId) this._runsWithDeliveredReply.add(runId);
213
- this.broadcastToBrowsers(rawData); // dual-emit: raw final event
214
- if (parsed) {
215
- const activity = this._popActivityLogForSession(sessionKey);
216
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-end', threadId: parsed.threadId, workspace: parsed.workspace, reason: 'complete', ...(activity || {}) }));
217
- }
218
- return;
219
- }
220
- if (state === 'aborted') {
221
- const parsed = parseSessionKey(sessionKey);
222
- // Clear pending flag from DB so a stale pending:true doesn't survive page reloads
223
- // and trigger phantom "thinking..." state on next visit to this thread.
224
- if (parsed) {
225
- const db = this.getDb(parsed.workspace);
226
- if (db) this._clearPendingFlag(db, parsed.threadId);
227
- }
228
- this.broadcastToBrowsers(rawData); // dual-emit
229
- if (parsed) {
230
- const activity = this._popActivityLogForSession(sessionKey);
231
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-end', threadId: parsed.threadId, workspace: parsed.workspace, reason: 'aborted', ...(activity || {}) }));
232
- }
233
- } else if (state === 'error') {
234
- // Late run-level fault for a run that already delivered its reply — drop it.
235
- // Emitting streaming-end{reason:'error'} here makes the browser replace the
236
- // rendered answer with an error bubble, destroying a message the user already read.
237
- if (runId && this._runsWithDeliveredReply.has(runId)) {
238
- console.log(`[clawchats] ignoring late error for run ${runId} — reply already delivered`);
239
- return;
240
- }
241
- const parsed = parseSessionKey(sessionKey);
242
- this.saveErrorMarker(sessionKey, message);
243
- this.broadcastToBrowsers(rawData); // dual-emit
244
- if (parsed) {
245
- const activity = this._popActivityLogForSession(sessionKey);
246
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'streaming-end', threadId: parsed.threadId, workspace: parsed.workspace, reason: 'error', errorMessage: gwErrorMessage || message?.error || message?.content || 'Unknown error', ...(activity || {}) }));
247
- }
248
- }
76
+ // Raw chat/agent events pass through; the browser builds live runs (frontend/live-run.js).
77
+ this.broadcastToBrowsers(data);
249
78
  }
250
79
 
251
80
  // Gateway RPC from the connector itself; resolves with the response payload.
@@ -259,319 +88,8 @@ export class GatewayClient {
259
88
  });
260
89
  }
261
90
 
262
- // Utility sessions keep no streamState entry; accumulate their delta text separately.
263
- _accumulateChatText(sessionKey, params) {
264
- const text = nextChatText(this._utilityText.get(sessionKey) || '', params);
265
- this._utilityText.set(sessionKey, text);
266
- return text;
267
- }
268
-
269
- // Removes metadata.pending flag from any pending assistant message for a thread.
270
- // Called on abort and by _cleanupSilentPending — keeps the SQL in one place.
271
- _clearPendingFlag(db, threadId) {
272
- db.prepare(
273
- "UPDATE messages SET metadata = json_remove(metadata, '$.pending') " +
274
- "WHERE thread_id = ? AND role = 'assistant' AND json_extract(metadata, '$.pending') = 1"
275
- ).run(threadId);
276
- }
277
-
278
- _cleanupSilentPending(sessionKey) {
279
- const parsed = parseSessionKey(sessionKey);
280
- if (!parsed) return;
281
- const ws = this.getWorkspaces();
282
- if (!ws.workspaces[parsed.workspace]) return;
283
- const db = this.getDb(parsed.workspace);
284
- if (!db) return;
285
- const result = db.prepare(`DELETE FROM messages WHERE thread_id = ? AND role = 'assistant' AND json_extract(metadata, '$.pending') = 1`).run(parsed.threadId);
286
- if (result.changes > 0) console.log(`[clawchats] silent-reply cleanup: removed ${result.changes} pending message(s) for ${parsed.threadId}`);
287
- // Caller broadcasts streaming-end { reason: 'silent' } — no event emitted here.
288
- }
289
-
290
- // Returns true when a reply was actually persisted for this run, false on every
291
- // early-out. handleChatEvent uses the result to decide whether the run counts as
292
- // having delivered a visible reply.
293
- saveAssistantMessage(sessionKey, message, seq, thoughtStartOffset = 0) {
294
- const parsed = parseSessionKey(sessionKey);
295
- if (!parsed) return false;
296
- const ws = this.getWorkspaces();
297
- if (!ws.workspaces[parsed.workspace]) { console.log(`Ignoring response for deleted workspace: ${parsed.workspace}`); return false; }
298
- const db = this.getDb(parsed.workspace);
299
- if (!db.prepare('SELECT id FROM threads WHERE id = ?').get(parsed.threadId)) { console.log(`Ignoring response for deleted thread: ${parsed.threadId}`); return false; }
300
-
301
- // Trim to final-answer portion: only text after the last tool call offset.
302
- // thoughtStartOffset is passed in from handleChatEvent (captured before streamState.delete).
303
- // Intermediate narration lives in activityLog steps; message.content is the clean final answer.
304
- const fullContent = extractContent(message);
305
- // Never let a stale segment offset blank the reply (v4 snapshots can be shorter).
306
- if (thoughtStartOffset > fullContent.length) thoughtStartOffset = 0;
307
- let content = sanitizeAssistantContent(fullContent.substring(thoughtStartOffset));
308
-
309
- // Attach media (MEDIA: paths extracted from exec tool args by handleAgentEvent).
310
- // Buffer is read before the empty-content guard — media-only responses (no text) must not be dropped.
311
- const pendingPaths = this._runMediaPaths.get(sessionKey) ?? [];
312
- this._runMediaPaths.delete(sessionKey);
313
- const IMAGE_EXTS = new Set(['png','jpg','jpeg','gif','webp','bmp','svg','ico','avif','tiff']);
314
- const AUDIO_EXTS = new Set(['mp3','wav','ogg','m4a','flac','aac','opus','wma']);
315
- const imagePaths = [], pendingAttachments = [];
316
- for (const p of pendingPaths) {
317
- const ext = (p.split('.').pop() || '').toLowerCase();
318
- if (IMAGE_EXTS.has(ext)) imagePaths.push(p);
319
- else pendingAttachments.push({ path: p, name: p.split('/').pop(), type: AUDIO_EXTS.has(ext) ? 'audio' : 'file' });
320
- }
321
- if (imagePaths.length > 0) content = (content?.trimEnd() || '') + '\n\n' + imagePaths.map(p => `![image](${p})`).join('\n');
322
- if (pendingPaths.length > 0) console.log(`[clawchats] media-attach: ${imagePaths.length} image(s), ${pendingAttachments.length} attachment(s) for ${sessionKey}`);
323
-
324
- const now = Date.now();
325
- const pendingMsg = db.prepare(`SELECT id, metadata FROM messages WHERE thread_id = ? AND role = 'assistant' AND json_extract(metadata, '$.pending') = 1 ORDER BY timestamp DESC LIMIT 1`).get(parsed.threadId);
326
-
327
- // Skip only if there is truly nothing to save AND no pending row to resolve.
328
- // If a pending row exists, always proceed to update it (even with empty content) so the
329
- // pending flag is cleared. Without this, tool-only responses (no post-tool text) leave
330
- // pending=true forever, and the startup cleanup marks them '[Response interrupted]'.
331
- if (!content?.trim() && pendingPaths.length === 0 && !pendingMsg) { console.log(`Skipping empty assistant response for thread ${parsed.threadId}`); return false; }
332
- let messageId;
333
-
334
- if (pendingMsg) {
335
- const metadata = pendingMsg.metadata ? JSON.parse(pendingMsg.metadata) : {};
336
- delete metadata.pending;
337
- if (metadata.activityLog) { const idx = metadata.activityLog.findLastIndex(s => s.type === 'assistant'); if (idx >= 0) metadata.activityLog.splice(idx, 1); metadata.activitySummary = generateActivitySummary(metadata.activityLog); }
338
- if (pendingAttachments.length > 0) metadata.attachments = [...(metadata.attachments || []), ...pendingAttachments];
339
- db.prepare('UPDATE messages SET content = ?, metadata = ?, timestamp = ? WHERE id = ?').run(content, JSON.stringify(metadata), now, pendingMsg.id);
340
- messageId = pendingMsg.id;
341
- } else {
342
- messageId = seq != null ? `gw-${parsed.threadId}-${seq}` : `gw-${parsed.threadId}-${now}`;
343
- const newMeta = pendingAttachments.length > 0 ? JSON.stringify({ attachments: pendingAttachments }) : null;
344
- db.prepare(`INSERT INTO messages (id, thread_id, role, content, status, metadata, timestamp, created_at) VALUES (?, ?, 'assistant', ?, 'sent', ?, ?, ?) ON CONFLICT(id) DO UPDATE SET content = excluded.content, metadata = COALESCE(excluded.metadata, metadata), timestamp = excluded.timestamp`).run(messageId, parsed.threadId, content, newMeta, now, now);
345
- }
346
-
347
- try {
348
- db.prepare('UPDATE threads SET updated_at = ? WHERE id = ?').run(now, parsed.threadId);
349
- db.prepare('INSERT OR IGNORE INTO unread_messages (thread_id, message_id, created_at) VALUES (?, ?, ?)').run(parsed.threadId, messageId, now);
350
- syncThreadUnreadCount(db, parsed.threadId);
351
- const threadInfo = db.prepare('SELECT title FROM threads WHERE id = ?').get(parsed.threadId);
352
- const unreadCount = db.prepare('SELECT COUNT(*) as c FROM unread_messages WHERE thread_id = ?').get(parsed.threadId).c;
353
- const preview = content.length > 120 ? content.substring(0, 120) + '...' : content;
354
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'message-saved', threadId: parsed.threadId, workspace: parsed.workspace, messageId, timestamp: now, title: threadInfo?.title, preview, unreadCount, updatedContent: imagePaths.length > 0 ? content : undefined, updatedAttachments: pendingAttachments.length > 0 ? pendingAttachments : undefined }));
355
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'unread-update', workspace: parsed.workspace, threadId: parsed.threadId, messageId, action: 'new', unreadCount, workspaceUnreadTotal: db.prepare('SELECT COALESCE(SUM(unread_count), 0) as total FROM threads').get().total, title: threadInfo?.title, preview, timestamp: now }));
356
- console.log(`Saved assistant message to ${parsed.workspace}/${parsed.threadId} (${pendingMsg ? 'merged into pending' : 'seq: ' + seq})`);
357
- const msgCount = db.prepare('SELECT COUNT(*) as c FROM messages WHERE thread_id = ?').get(parsed.threadId).c;
358
- if (msgCount === 2 || threadInfo?.title === 'New chat') this.generateThreadTitle(db, parsed.threadId, parsed.workspace, true);
359
- } catch (e) { console.error('Failed to save assistant message:', e.message); }
360
- // The message row is written above the try block, so it is persisted even if the
361
- // follow-up bookkeeping/broadcast throws — the reply was delivered either way.
362
- return true;
363
- }
364
-
365
- saveErrorMarker(sessionKey, message) {
366
- const parsed = parseSessionKey(sessionKey);
367
- if (!parsed) return;
368
- const ws = this.getWorkspaces();
369
- if (!ws.workspaces[parsed.workspace]) return;
370
- const db = this.getDb(parsed.workspace);
371
- if (!db.prepare('SELECT id FROM threads WHERE id = ?').get(parsed.threadId)) return;
372
- const now = Date.now();
373
- try {
374
- db.prepare('INSERT INTO messages (id, thread_id, role, content, status, metadata, timestamp, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)').run(`gw-error-${parsed.threadId}-${now}`, parsed.threadId, 'system', `[error] ${message?.error || message?.content || 'Unknown error'}`, 'sent', '{"transient":true}', now, now);
375
- // Clear stale pending flag so browsers reloading the chat don't re-derive "thinking..." state.
376
- db.prepare("UPDATE messages SET metadata = json_remove(metadata, '$.pending') WHERE thread_id = ? AND role = 'assistant' AND json_extract(metadata, '$.pending') = 1").run(parsed.threadId);
377
- } catch (e) { console.error('Failed to save error marker:', e.message); }
378
- }
379
-
380
- generateThreadTitle(db, threadId, workspace, skipHeuristic = false) {
381
- if (!db.prepare('SELECT title FROM threads WHERE id = ?').get(threadId)) return;
382
- const titleKey = `__clawchats_title_${threadId}`;
383
- if (this._pendingTitleGens.has(titleKey)) return;
384
- const firstUserMsg = db.prepare("SELECT content FROM messages WHERE thread_id = ? AND role = 'user' ORDER BY created_at ASC LIMIT 1").get(threadId);
385
- if (!firstUserMsg?.content) return;
386
- if (!skipHeuristic) {
387
- const heuristic = firstUserMsg.content.replace(/\n.*/s, '').slice(0, 40).trim() + (firstUserMsg.content.length > 40 ? '...' : '');
388
- db.prepare('UPDATE threads SET title = ? WHERE id = ?').run(heuristic, threadId);
389
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'thread-title-updated', threadId, workspace, title: heuristic }));
390
- }
391
- const messages = db.prepare('SELECT role, content FROM messages WHERE thread_id = ? ORDER BY created_at ASC LIMIT 6').all(threadId);
392
- if (messages.length < 2) return;
393
- const conversation = messages.map(m => `${m.role === 'user' ? 'User' : 'Assistant'}: ${m.content.length > 300 ? m.content.slice(0, 300) + '...' : m.content}`).join('\n\n');
394
- const reqId = `title-${threadId}-${Date.now()}`;
395
- this._pendingTitleGens.set(titleKey, { threadId, workspace, reqId });
396
- setTimeout(() => { if (this._pendingTitleGens.has(titleKey)) { this._pendingTitleGens.delete(titleKey); console.log(`Title gen timeout for ${threadId}`); } }, 30000);
397
- this.sendToGateway(JSON.stringify({ type: 'req', id: reqId, method: 'chat.send', params: { sessionKey: titleKey, message: `Based on this conversation, generate a concise 3-5 word title. Return ONLY the title text, no quotes, no explanation:\n\n${conversation}\n\nTitle:`, deliver: false, idempotencyKey: reqId } }));
398
- }
399
-
400
- handleTitleResponse(sessionKey, content) {
401
- let matchKey = null, pending = null;
402
- for (const [key, val] of this._pendingTitleGens) {
403
- if (sessionKey === key || sessionKey.includes(key)) { matchKey = key; pending = val; break; }
404
- }
405
- if (!pending) return false;
406
- this._pendingTitleGens.delete(matchKey);
407
- let title = content.trim().replace(/^["']|["']$/g, '').replace(/^Title:\s*/i, '').replace(/\n.*/s, '').trim();
408
- if (title.length > 50) title = title.substring(0, 47) + '...';
409
- if (!title || title.length >= 100) return true;
410
- const db = this.getDb(pending.workspace);
411
- db.prepare('UPDATE threads SET title = ? WHERE id = ?').run(title, pending.threadId);
412
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'thread-title-updated', threadId: pending.threadId, workspace: pending.workspace, title }));
413
- console.log(`AI title generated for ${pending.threadId}: "${title}"`);
414
- return true;
415
- }
416
-
417
- handleAgentEvent(payload) {
418
- const { runId, stream, data, sessionKey } = payload;
419
- if (!runId) return;
420
- if (!this.activityLogs.has(runId)) this.activityLogs.set(runId, { sessionKey, steps: [], startTime: Date.now() });
421
- const log = this.activityLogs.get(runId);
422
-
423
- if (stream === 'thinking') {
424
- let step = log.steps.find(s => s.type === 'thinking');
425
- if (step) step.text = data?.text || '';
426
- else log.steps.push({ type: 'thinking', timestamp: Date.now(), text: data?.text || '' });
427
- writeActivityToDb(this.getDb, this.broadcastToBrowsers.bind(this), runId, log);
428
- const now = Date.now();
429
- if (!log._lastThinkingBroadcast || now - log._lastThinkingBroadcast >= 300) { log._lastThinkingBroadcast = now; this._broadcastActivityUpdate(runId, log); }
430
- }
431
- if (stream === 'tool') {
432
- // At phase:start the gateway has already flushed the text buffer, so the buffer is
433
- // complete. Capture the full inter-tool narration now — no race, no stale snapshots.
434
- // For result/update phases, just defensively seal any still-open segment.
435
- if (data?.phase !== 'result' && data?.phase !== 'update') {
436
- const streamEntry = this.streamState.get(sessionKey);
437
- const narrationStart = log._lastNarrationStart ?? 0;
438
- const narrationText = streamEntry ? streamEntry.buffer.substring(narrationStart) : '';
439
- if (log._currentAssistantSegment && !log._currentAssistantSegment._sealed) {
440
- if (narrationText.trim()) log._currentAssistantSegment.text = narrationText;
441
- log._currentAssistantSegment._sealed = true;
442
- } else if (narrationText.trim()) {
443
- const seg = { type: 'assistant', timestamp: Date.now(), text: narrationText, _sealed: true };
444
- log._currentAssistantSegment = seg;
445
- log.steps.push(seg);
446
- }
447
- } else if (log._currentAssistantSegment && !log._currentAssistantSegment._sealed) {
448
- log._currentAssistantSegment._sealed = true;
449
- }
450
- const argsMeta = data?.args ? (data.args.command || data.args.path || data.args.query || data.args.url || Object.values(data.args).find(v => typeof v === 'string') || '') : '';
451
- // Extract MEDIA: paths from exec tool's echo command at phase:start.
452
- // The agent signals inline media with `echo "MEDIA:/absolute/path"` (see plugin's before_prompt_build hint).
453
- // Parsing the tool args here keeps state on this stable singleton instead of the plugin's re-registered closure.
454
- if (sessionKey && (data?.name === 'exec' || data?.name === 'process') && data?.phase === 'start' && typeof data?.args?.command === 'string') {
455
- const paths = [...data.args.command.matchAll(/MEDIA:([^\s"'`]+)/g)].map(m => m[1]).filter(Boolean);
456
- if (paths.length > 0) {
457
- const existing = this._runMediaPaths.get(sessionKey) ?? [];
458
- this._runMediaPaths.set(sessionKey, [...new Set([...existing, ...paths])]);
459
- }
460
- }
461
- // Capture Write/Edit tool paths. The split pane (and existing attachment chip render) handles
462
- // any text-ish or PDF file, so allow that broad set; image/audio extensions also flow through
463
- // — saveAssistantMessage routes them by ext (image → markdown, audio → audio chip).
464
- // Buffer at phase:start when args.path is present; promote to _runMediaPaths at phase:result
465
- // only if the call succeeded — failed writes shouldn't surface a chip for a file that may not exist.
466
- const VIEWABLE_EXTS = new Set([
467
- 'pdf','md','mdx','rst','txt','log',
468
- 'csv','tsv','json','xml','yaml','yml','toml','ini','env','conf',
469
- 'js','jsx','mjs','cjs','ts','tsx','py','rb','go','rs','java','c','cpp','h','hpp','cs','php',
470
- 'html','htm','css','scss','sass','less','sh','bash','zsh','sql','pl','lua','r','swift','kt','dart',
471
- 'png','jpg','jpeg','gif','webp','bmp','svg','ico','avif','tiff',
472
- 'mp3','wav','ogg','m4a','flac','aac','opus','wma'
473
- ]);
474
- if (sessionKey && data?.toolCallId && /^(write|edit|multiedit|notebookedit)$/i.test(data?.name || '') && data?.phase === 'start' && typeof data?.args?.path === 'string' && data.args.path.length > 0) {
475
- const ext = (data.args.path.split('.').pop() || '').toLowerCase();
476
- if (VIEWABLE_EXTS.has(ext)) this._runWriteBuffers.set(data.toolCallId, { sessionKey, path: data.args.path });
477
- }
478
- if (data?.toolCallId && data?.phase === 'result' && this._runWriteBuffers.has(data.toolCallId)) {
479
- const buf = this._runWriteBuffers.get(data.toolCallId);
480
- this._runWriteBuffers.delete(data.toolCallId);
481
- if (!data.isError && buf.sessionKey === sessionKey) {
482
- const existing = this._runMediaPaths.get(buf.sessionKey) ?? [];
483
- this._runMediaPaths.set(buf.sessionKey, [...new Set([...existing, buf.path])]);
484
- }
485
- }
486
- const step = { type: 'tool', timestamp: Date.now(), name: data?.name || 'unknown', phase: data?.phase || 'start', toolCallId: data?.toolCallId, meta: data?.meta || (argsMeta ? String(argsMeta) : undefined), isError: data?.isError || false };
487
- if (data?.phase === 'result') {
488
- const existing = log.steps.findLast(s => s.toolCallId === data.toolCallId && (s.phase === 'start' || s.phase === 'running'));
489
- if (existing) { existing.phase = 'done'; existing.resultMeta = data?.meta; existing.isError = data?.isError || false; existing.durationMs = Date.now() - existing.timestamp; }
490
- else { step.phase = 'done'; log.steps.push(step); }
491
- // Advance the visible content offset to the current buffer end.
492
- // Future streaming-delta broadcasts and the saved message content start from here,
493
- // so the response area always shows only the most recent thought / final answer.
494
- const streamEntry = this.streamState.get(sessionKey);
495
- if (streamEntry && log._parsed) {
496
- // Don't advance thoughtStartOffset or broadcast streaming-reset yet.
497
- // Wait for the first post-tool delta before doing either:
498
- // (a) No post-tool text: thoughtStartOffset stays at the current segment start,
499
- // bubble retains the last segment text as the final answer.
500
- // (b) Post-tool text arrives: delta handler advances the offset and broadcasts
501
- // streaming-reset immediately before the first new-segment delta, so the
502
- // bubble clears at exactly the right moment.
503
- streamEntry.pendingReset = true;
504
- log._lastNarrationStart = streamEntry.buffer.length; // align activity log segments with buffer
505
- }
506
- } else if (data?.phase === 'update') {
507
- const existing = log.steps.findLast(s => s.toolCallId === data.toolCallId);
508
- if (existing) { if (data?.meta) existing.resultMeta = data.meta; if (data?.isError) existing.isError = true; existing.phase = 'running'; }
509
- } else log.steps.push(step);
510
- writeActivityToDb(this.getDb, this.broadcastToBrowsers.bind(this), runId, log);
511
- this._broadcastActivityUpdate(runId, log);
512
- }
513
- if (stream === 'lifecycle' && (data?.phase === 'end' || data?.phase === 'error')) {
514
- if (log._currentAssistantSegment && !log._currentAssistantSegment._sealed) {
515
- log._currentAssistantSegment._sealed = true;
516
- }
517
- writeActivityToDb(this.getDb, this.broadcastToBrowsers.bind(this), runId, log);
518
- // Store finalized state — streaming-end (handleChatEvent) will pick it up and carry it
519
- // as one atomic payload. Do NOT broadcast activity-updated here anymore.
520
- const cleanSteps = log.steps.map(s => { const c = { ...s }; delete c._sealed; return c; });
521
- log.finalized = true;
522
- log.finalSteps = cleanSteps;
523
- log.finalSummary = generateActivitySummary(log.steps);
524
- // Note: activityLogs entry is kept until _popActivityLogForSession cleans it up
525
- //
526
- // Fallback: if the gateway never emitted any chat.* events for this session
527
- // (e.g. pre-reply provider error on a non-webchat surface where the chat
528
- // broadcast is gated off upstream), handleChatEvent won't fire a
529
- // streaming-end and the UI stays locked on "thinking…" forever. Synthesize
530
- // one here so the frontend's existing error path (`reason: 'error'`) runs.
531
- // `!streamState.has(sessionKey)` is also true for a run that finished cleanly (the entry
532
- // is deleted on every terminal state), so the delivered-reply check is what distinguishes
533
- // "never produced a reply" from "already delivered one".
534
- if (data?.phase === 'error' && !this.streamState.has(sessionKey) && !this._syntheticErrorRuns.has(runId) && !this._runsWithDeliveredReply.has(runId)) {
535
- this._syntheticErrorRuns.add(runId);
536
- const parsed = parseSessionKey(sessionKey);
537
- if (parsed) {
538
- // Mirror the state:'error' path — writeActivityToDb just set metadata.pending=true,
539
- // and without this the flag survives: loadHistory re-derives has_pending on reconnect,
540
- // "thinking…" sticks, and sendMessage's isStreaming() guard queues future sends into
541
- // _pendingSend forever.
542
- this.saveErrorMarker(sessionKey, { error: data?.error || 'Agent failed before reply' });
543
- this.broadcastToBrowsers(JSON.stringify({
544
- type: 'clawchats',
545
- event: 'streaming-end',
546
- threadId: parsed.threadId,
547
- workspace: parsed.workspace,
548
- reason: 'error',
549
- errorMessage: data?.error || 'Agent failed before reply',
550
- }));
551
- }
552
- // Bound the set — auto-evict after the retry window so long-lived
553
- // processes don't leak. 5 min is far longer than any retry chain.
554
- setTimeout(() => this._syntheticErrorRuns.delete(runId), 5 * 60 * 1000);
555
- }
556
- }
557
- }
558
-
559
- // Returns and removes the finalized activity log for a given sessionKey, or null if none.
560
- // Called by handleChatEvent so streaming-end carries the final activity state atomically.
561
- _popActivityLogForSession(sessionKey) {
562
- for (const [runId, log] of this.activityLogs) {
563
- if (log.sessionKey === sessionKey && log.finalized) {
564
- this.activityLogs.delete(runId);
565
- return { activityLog: log.finalSteps, activitySummary: log.finalSummary };
566
- }
567
- }
568
- return null;
569
- }
570
-
571
- _broadcastActivityUpdate(runId, log) {
572
- if (!log._parsed || !log._messageId) return;
573
- const cleanSteps = log.steps.map(s => { const c = { ...s }; delete c._sealed; return c; });
574
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'activity-updated', workspace: log._parsed.workspace, threadId: log._parsed.threadId, messageId: log._messageId, activityLog: cleanSteps, activitySummary: generateActivitySummary(log.steps) }));
91
+ _failPendingRpcs(reason) {
92
+ for (const [id, p] of this._rpc) { clearTimeout(p.timer); p.reject(new Error(reason)); this._rpc.delete(id); }
575
93
  }
576
94
 
577
95
  broadcastToBrowsers(data) {
@@ -584,6 +102,12 @@ export class GatewayClient {
584
102
  this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'gateway-status', connected }));
585
103
  }
586
104
 
105
+ /** A frame from a browser: the lens may rewrite it or answer it itself. */
106
+ forwardFromBrowser(data) {
107
+ const out = this.lens ? this.lens.outbound(data) : data;
108
+ if (out) this.sendToGateway(out);
109
+ }
110
+
587
111
  sendToGateway(data) {
588
112
  this.debugLogger.logFrame('SRV→GW', data);
589
113
  if (this.ws?.readyState === WS.OPEN) this.ws.send(data);
@@ -594,43 +118,42 @@ export class GatewayClient {
594
118
  const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), this.maxReconnectDelay);
595
119
  this.reconnectAttempts++;
596
120
  console.log(`Reconnecting to gateway in ${delay}ms (attempt ${this.reconnectAttempts})...`);
597
- setTimeout(() => this.connect(), delay);
121
+ clearTimeout(this._reconnectTimer);
122
+ this._reconnectTimer = setTimeout(() => { this._reconnectTimer = null; this.connect(); }, delay);
123
+ }
124
+
125
+ /**
126
+ * Permanent shutdown (plugin stop/reload): no reconnects, pending RPCs fail, socket closed.
127
+ * Without this the gateway's plugin drain waits forever on the open WebSocket.
128
+ */
129
+ close() {
130
+ this._closed = true;
131
+ clearTimeout(this._reconnectTimer);
132
+ this._reconnectTimer = null;
133
+ this._failPendingRpcs('connector stopping');
134
+ this.connected = false;
135
+ const ws = this.ws;
136
+ this.ws = null;
137
+ if (ws) {
138
+ ws.removeAllListeners('message');
139
+ ws.on('error', () => {});
140
+ try { ws.readyState === WS.CONNECTING ? ws.terminate() : ws.close(1001, 'connector stopping'); } catch { /* already closed */ }
141
+ // Don't let a half-closed socket hold the drain open.
142
+ setTimeout(() => { try { ws.terminate(); } catch { /* gone */ } }, 2000).unref?.();
143
+ }
144
+ this.browserClients.clear();
145
+ this._externalBroadcastTargets = [];
598
146
  }
599
147
 
600
148
  addBrowserClient(ws) {
601
- this.browserClients.set(ws, { activeWorkspace: null, activeThreadId: null });
149
+ this.browserClients.set(ws, {});
602
150
  if (ws.readyState === WS.OPEN) {
603
151
  ws.send(JSON.stringify({ type: 'clawchats', event: 'gateway-status', connected: this.connected }));
604
- const streams = [];
605
- for (const [sessionKey, state] of this.streamState.entries()) {
606
- if (state.state === 'streaming' && !(state.held?.length > 0)) {
607
- const parsed = parseSessionKey(sessionKey);
608
- // Include both old shape (sessionKey/buffer) and new shape (workspace/content) for dual-emit compat
609
- streams.push({ sessionKey, threadId: state.threadId, buffer: state.buffer, ...(parsed ? { workspace: parsed.workspace, content: state.buffer.substring(state.thoughtStartOffset || 0) } : {}) });
610
- }
611
- }
612
- if (streams.length > 0) ws.send(JSON.stringify({ type: 'clawchats', event: 'stream-sync', streams }));
613
152
  }
614
153
  }
615
154
 
616
155
  removeBrowserClient(ws) { this.browserClients.delete(ws); }
617
156
 
618
- setActiveThread(ws, workspace, threadId) {
619
- const client = ws ? this.browserClients.get(ws) : null;
620
- if (client) { client.activeWorkspace = workspace; client.activeThreadId = threadId; }
621
- if (!workspace || !threadId) return;
622
- try {
623
- const wsData = this.getWorkspaces();
624
- if (!wsData.workspaces[workspace]) return;
625
- const db = this.getDb(workspace);
626
- if (!db.prepare('SELECT id FROM threads WHERE id = ?').get(threadId)) return;
627
- const deleted = db.prepare('DELETE FROM unread_messages WHERE thread_id = ?').run(threadId);
628
- if (deleted.changes > 0) {
629
- syncThreadUnreadCount(db, threadId);
630
- this.broadcastToBrowsers(JSON.stringify({ type: 'clawchats', event: 'unread-update', workspace, threadId, action: 'clear', unreadCount: 0, workspaceUnreadTotal: db.prepare('SELECT COALESCE(SUM(unread_count), 0) as total FROM threads').get().total, timestamp: Date.now() }));
631
- }
632
- } catch (e) { console.error('Failed to auto-clear unreads on active-thread:', e.message); }
633
- }
634
157
 
635
158
  addBroadcastTarget(fn) { this._externalBroadcastTargets.push(fn); }
636
159
  removeBroadcastTarget(fn) { this._externalBroadcastTargets = this._externalBroadcastTargets.filter(f => f !== fn); }