@clawchatsai/connector 0.1.45 → 0.1.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,56 @@
1
+ import http from 'node:http';
2
+ import { parseBody, send, sendError } from '../util/http.js';
3
+
4
+ // Realtime Talk over WebRTC: `talk.client.create` returns an `offerUrl` that is a route of the
5
+ // gateway's own HTTP server (e.g. /plugins/openai/realtime/calls). The Control UI reaches it
6
+ // directly; a ClawChats browser has no HTTP path to the gateway, only this DataChannel. The
7
+ // browser posts its SDP offer here and the connector relays it to that gateway route.
8
+ //
9
+ // The only credential involved is the one-time `clientSecret` the gateway minted for this call
10
+ // (sent as the route's Bearer token, as the Control UI does); the gateway token never leaves
11
+ // the connector. Only the gateway's realtime broker routes are reachable.
12
+ const OFFER_PATH = /^\/plugins\/[a-z0-9][a-z0-9-]*\/realtime\/calls$/;
13
+ const MAX_SDP_BYTES = 64 * 1024;
14
+ const MAX_ANSWER_BYTES = 256 * 1024;
15
+ const OFFER_TIMEOUT_MS = 30_000;
16
+ // Provider-specific extras (`offerHeaders`) may only be X-* headers.
17
+ const EXTRA_HEADER = /^x-[a-z0-9-]+$/i;
18
+
19
+ export function isAllowedOfferPath(p) {
20
+ return typeof p === 'string' && OFFER_PATH.test(p);
21
+ }
22
+
23
+ export function createTalkOfferHandler({ gatewayWsUrl }) {
24
+ const base = new URL(gatewayWsUrl.replace(/^ws/, 'http'));
25
+ return async function handleTalkOffer(req, res) {
26
+ let body;
27
+ try { body = await parseBody(req); } catch { return sendError(res, 400, 'Invalid JSON'); }
28
+ const { path, sdp, clientSecret, headers } = body || {};
29
+ if (!isAllowedOfferPath(path)) return sendError(res, 400, 'Unsupported realtime offer route');
30
+ if (typeof sdp !== 'string' || !sdp.trim() || Buffer.byteLength(sdp) > MAX_SDP_BYTES) return sendError(res, 400, 'SDP offer required');
31
+ if (typeof clientSecret !== 'string' || !clientSecret || /\s/.test(clientSecret)) return sendError(res, 400, 'Realtime session token required');
32
+ const outHeaders = { 'Content-Type': 'application/sdp', 'Content-Length': String(Buffer.byteLength(sdp)), Authorization: `Bearer ${clientSecret}` };
33
+ if (headers && typeof headers === 'object') {
34
+ for (const [k, v] of Object.entries(headers)) if (EXTRA_HEADER.test(k) && typeof v === 'string') outHeaders[k] = v;
35
+ }
36
+ await new Promise(resolve => {
37
+ const upstream = http.request({ hostname: base.hostname, port: base.port, path, method: 'POST', headers: outHeaders, timeout: OFFER_TIMEOUT_MS }, up => {
38
+ const chunks = [];
39
+ let size = 0;
40
+ up.on('data', c => {
41
+ size += c.length;
42
+ if (size > MAX_ANSWER_BYTES) { up.destroy(); if (!res.headersSent) sendError(res, 502, 'Realtime answer too large'); return resolve(); }
43
+ chunks.push(c);
44
+ });
45
+ up.on('end', () => {
46
+ if (!res.headersSent) send(res, 200, { status: up.statusCode || 502, body: Buffer.concat(chunks).toString('utf8') });
47
+ resolve();
48
+ });
49
+ up.on('error', e => { if (!res.headersSent) sendError(res, 502, `Gateway realtime offer: ${e.message}`); resolve(); });
50
+ });
51
+ upstream.on('timeout', () => upstream.destroy(new Error('timed out')));
52
+ upstream.on('error', e => { if (!res.headersSent) sendError(res, 502, `Gateway realtime offer: ${e.message}`); resolve(); });
53
+ upstream.end(sdp);
54
+ });
55
+ };
56
+ }
@@ -0,0 +1,48 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import http from 'node:http';
4
+ import { Readable } from 'node:stream';
5
+ import { isAllowedOfferPath, createTalkOfferHandler } from './talk-offer.js';
6
+
7
+ test('only realtime broker routes are reachable', () => {
8
+ assert.equal(isAllowedOfferPath('/plugins/openai/realtime/calls'), true);
9
+ assert.equal(isAllowedOfferPath('/plugins/google/realtime/calls'), true);
10
+ for (const p of ['/api/sessions', '/plugins/openai/realtime/calls/x', '/plugins/../realtime/calls', '/plugins/a/b/realtime/calls', 'http://evil/plugins/openai/realtime/calls', '', null]) {
11
+ assert.equal(isAllowedOfferPath(p), false, String(p));
12
+ }
13
+ });
14
+
15
+ function fakeRes() {
16
+ const r = { status: 0, body: '', headersSent: false, writeHead(s) { r.status = s; r.headersSent = true; }, end(b) { r.body = b; r.done?.(); } };
17
+ return r;
18
+ }
19
+ const fakeReq = body => Object.assign(Readable.from([Buffer.from(JSON.stringify(body))]), { headers: {} });
20
+
21
+ test('relays the SDP offer with the call token as Bearer, never the gateway token', async () => {
22
+ let seen;
23
+ const gw = http.createServer((req, res) => {
24
+ const chunks = [];
25
+ req.on('data', c => chunks.push(c));
26
+ req.on('end', () => { seen = { url: req.url, auth: req.headers.authorization, type: req.headers['content-type'], x: req.headers['x-extra'], cookie: req.headers.cookie, body: Buffer.concat(chunks).toString() }; res.writeHead(201); res.end('v=0 answer'); });
27
+ });
28
+ await new Promise(r => gw.listen(0, '127.0.0.1', r));
29
+ try {
30
+ const handler = createTalkOfferHandler({ gatewayWsUrl: `ws://127.0.0.1:${gw.address().port}` });
31
+ const res = fakeRes();
32
+ const done = new Promise(r => { res.done = r; });
33
+ await handler(fakeReq({ path: '/plugins/openai/realtime/calls', sdp: 'v=0 offer', clientSecret: 'tok123', headers: { 'X-Extra': '1', Cookie: 'no' } }), res);
34
+ await done;
35
+ assert.equal(res.status, 200);
36
+ assert.deepEqual(JSON.parse(res.body), { status: 201, body: 'v=0 answer' });
37
+ assert.deepEqual(seen, { url: '/plugins/openai/realtime/calls', auth: 'Bearer tok123', type: 'application/sdp', x: '1', cookie: undefined, body: 'v=0 offer' });
38
+ } finally { gw.close(); }
39
+ });
40
+
41
+ test('rejects other routes and missing fields without calling the gateway', async () => {
42
+ const handler = createTalkOfferHandler({ gatewayWsUrl: 'ws://127.0.0.1:1' });
43
+ for (const body of [{ path: '/api/sessions', sdp: 'x', clientSecret: 't' }, { path: '/plugins/openai/realtime/calls', clientSecret: 't' }, { path: '/plugins/openai/realtime/calls', sdp: 'x' }]) {
44
+ const res = fakeRes();
45
+ await handler(fakeReq(body), res);
46
+ assert.equal(res.status, 400);
47
+ }
48
+ });
package/server/gateway.js CHANGED
@@ -102,7 +102,7 @@ export class GatewayClient {
102
102
  this.ws.send(JSON.stringify({ type: 'req', id: 'gw-connect-1', method: 'connect', params: this._connectParams(msg.payload?.nonce || '', ['tool-events']) }));
103
103
  return;
104
104
  }
105
- if (msg.type === 'res' && msg.payload?.type === 'hello-ok') { console.log('Gateway handshake complete'); this.connected = true; this.broadcastGatewayStatus(true); this.sync?.onConnected(); this.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}`)); }
105
+ if (msg.type === 'res' && msg.payload?.type === 'hello-ok') { console.log('Gateway handshake complete'); this.connected = true; this.broadcastGatewayStatus(true); this.sync?.onConnected(); this.onConnected?.(); this.request('sessions.subscribe', {}).catch(e => console.warn(`[lens] sessions.subscribe failed: ${e.message}`)); this.lens?.seedWithRetry(); }
106
106
  // Session events arrive through the connector's own subscription; the lens filters them
107
107
  // for the browser (session.message only for chats a browser subscribed to).
108
108
  if (msg.type === 'event' && (msg.event === 'sessions.changed' || msg.event === 'session.message')) {
package/server/index.js CHANGED
@@ -11,6 +11,7 @@ import { GatewayClient } from './gateway.js';
11
11
  import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload, handleWorkspaceCreate, handleWorkspaceMove, handleWorkspaceCopy } from './controllers/filesystem.js';
12
12
  import { handleTranscribe } from './controllers/transcribe.js';
13
13
  import { createGatewayMediaHandler } from './controllers/gateway-media.js';
14
+ import { createTalkOfferHandler } from './controllers/talk-offer.js';
14
15
  import { handleStatic } from './controllers/static.js';
15
16
  import { handleAgents } from './controllers/agents.js';
16
17
  import { createTitleHandler, createTitler } from './controllers/title.js';
@@ -117,6 +118,7 @@ export function createApp(config = {}) {
117
118
 
118
119
  // Instantiate controllers
119
120
  const handleGatewayMedia = createGatewayMediaHandler({ gatewayWsUrl: gatewayUrl, gatewayToken });
121
+ const handleTalkOffer = createTalkOfferHandler({ gatewayWsUrl: gatewayUrl });
120
122
 
121
123
  // Settings — file I/O lives in settings.js
122
124
  const { handleGetSettings, handleSaveSettings } = createSettingsHandlers(SETTINGS_FILE);
@@ -195,6 +197,7 @@ export function createApp(config = {}) {
195
197
  // File serving & workspace browser
196
198
  if (method === 'GET' && urlPath === '/api/file') return handleServeFile(req, res, query, workspaceDir);
197
199
  if (method === 'GET' && urlPath === '/api/gw-media') return handleGatewayMedia(req, res, query);
200
+ if (method === 'POST' && urlPath === '/api/talk/offer') return await handleTalkOffer(req, res);
198
201
  if (method === 'GET' && urlPath === '/api/workspace') return handleWorkspaceList(req, res, query);
199
202
  if (method === 'GET' && urlPath === '/api/workspace/file') return handleWorkspaceFileRead(req, res, query, workspaceDir, query.session ? await sessionRoot(query.session) : null);
200
203
  if (method === 'PUT' && urlPath === '/api/workspace/file') return await handleWorkspaceFileWrite(req, res, query);
@@ -206,17 +206,39 @@ export class SessionLens {
206
206
  return isUtilityKey(key) || this.isVisible(key) || !!this.team?.isWorkKey(key);
207
207
  }
208
208
 
209
- /** Seed `hidden` from a full roster so partial events of child sessions stay hidden. */
209
+ /** Seed `hidden` from a full roster so partial events of child sessions stay hidden. With the
210
+ * whole roster in hand, also drop ClawChats data (bookmarks etc.) of sessions deleted while
211
+ * the connector wasn't listening for `sessions.changed`. */
210
212
  async seed() {
211
213
  if (!this.request) return;
212
- let offset = 0, rows = 0;
214
+ let offset = 0, rows = 0, complete = false;
215
+ const live = new Set();
213
216
  for (let page = 0; page < 50; page++) {
214
217
  const res = await this.request('sessions.list', { archived: 'all', limit: 1000, offset }, 60000);
215
- for (const row of res?.sessions || []) { this.observe(row.key, row); rows++; }
216
- if (!res?.hasMore || res.nextOffset == null) break;
218
+ for (const row of res?.sessions || []) { this.observe(row.key, row); live.add(row.key); rows++; }
219
+ if (!res?.hasMore || res.nextOffset == null) { complete = true; break; }
217
220
  offset = res.nextOffset;
218
221
  }
219
222
  this.log.info?.(`[lens] seeded from ${rows} sessions (${this.hidden.size} hidden chat-shaped)`);
223
+ if (complete && live.size && this.extras?.pruneThreadExtras) {
224
+ const dropped = this.extras.pruneThreadExtras(live);
225
+ if (dropped) {
226
+ this.log.info?.(`[lens] dropped bookmarks/extras of ${dropped} deleted sessions`);
227
+ this.broadcast(JSON.stringify({ type: 'clawchats', event: 'bookmarks-changed' }));
228
+ }
229
+ }
230
+ }
231
+
232
+ /** seed(), retried: right after a plugin reload the gateway can briefly fail sessions.list
233
+ * (PluginInstanceUnavailableError), and a missed seed also skips the extras prune. */
234
+ async seedWithRetry(tries = 4, delayMs = 5000) {
235
+ for (let i = 1; ; i++) {
236
+ try { return await this.seed(); } catch (e) {
237
+ this.log.warn?.(`[lens] seed failed (${i}/${tries}): ${e.message}`);
238
+ if (i >= tries) return;
239
+ await new Promise(r => setTimeout(r, delayMs));
240
+ }
241
+ }
220
242
  }
221
243
 
222
244
  // ── Gateway → browser ───────────────────────────────────────────────
@@ -232,8 +254,9 @@ export class SessionLens {
232
254
  if (payload.reason === 'patch' && this.team?.onSessionPatched) {
233
255
  this.team.onSessionPatched(key).catch(e => this.log.warn?.(`[lens] team patch: ${e.message}`));
234
256
  }
235
- if (!this.observe(key, payload.session || payload)) return null;
257
+ // Before the visibility check: a deleted session's bookmarks go even if the lens had it hidden.
236
258
  if (payload.reason === 'delete' && this.extras) this.extras.deleteThreadExtras?.(key);
259
+ if (!this.observe(key, payload.session || payload)) return null;
237
260
  const out = trimEvent(payload);
238
261
  if (payload.reason !== 'delete') this._withParent(key, out.session || out);
239
262
  return { type: 'event', event: 'sessions.changed', payload: out };
@@ -101,6 +101,48 @@ test('group rename/delete move project styles only on success', () => {
101
101
  assert.deepEqual(calls, [['rename', 'A', 'B'], ['delete', 'C'], ['thread', CHAT]]);
102
102
  });
103
103
 
104
+ test('delete drops extras even for a hidden session', () => {
105
+ const calls = [];
106
+ const { l } = lens({ deleteThreadExtras: k => calls.push(k) });
107
+ l.sessionsChanged({ sessionKey: CHILD, reason: 'create', parentSessionKey: CHAT });
108
+ assert.equal(l.sessionsChanged({ sessionKey: CHILD, reason: 'delete' }), null);
109
+ assert.deepEqual(calls, [CHILD]);
110
+ });
111
+
112
+ test('seed prunes extras of sessions missing from a complete roster', async () => {
113
+ const pruned = [];
114
+ const sent = [];
115
+ const pages = [{ sessions: [{ key: CHAT }], hasMore: true, nextOffset: 1 }, { sessions: [{ key: DISCORD }], hasMore: false }];
116
+ const l = new SessionLens({ broadcast: d => sent.push(JSON.parse(d)), request: async () => pages.shift(), logger: { info() {} },
117
+ extras: { pruneThreadExtras: keys => { pruned.push([...keys]); return 1; } } });
118
+ await l.seed();
119
+ assert.deepEqual(pruned, [[CHAT, DISCORD]]);
120
+ assert.deepEqual(sent, [{ type: 'clawchats', event: 'bookmarks-changed' }]);
121
+
122
+ // A failed or empty roster prunes nothing.
123
+ const l2 = new SessionLens({ broadcast() {}, request: async () => ({ sessions: [] }), logger: { info() {} },
124
+ extras: { pruneThreadExtras: () => { throw new Error('must not prune'); } } });
125
+ await l2.seed();
126
+ const l3 = new SessionLens({ broadcast() {}, request: async () => { throw new Error('down'); }, logger: { info() {} },
127
+ extras: { pruneThreadExtras: () => { throw new Error('must not prune'); } } });
128
+ await assert.rejects(l3.seed());
129
+ });
130
+
131
+ test('seedWithRetry retries a failed roster', async () => {
132
+ let calls = 0;
133
+ const pruned = [];
134
+ const l = new SessionLens({ broadcast() {}, logger: { info() {}, warn() {} },
135
+ request: async () => { if (++calls < 3) throw new Error('PluginInstanceUnavailableError'); return { sessions: [{ key: CHAT }] }; },
136
+ extras: { pruneThreadExtras: keys => { pruned.push([...keys]); return 0; } } });
137
+ await l.seedWithRetry(4, 1);
138
+ assert.equal(calls, 3);
139
+ assert.deepEqual(pruned, [[CHAT]]);
140
+ let n = 0;
141
+ const l2 = new SessionLens({ broadcast() {}, logger: { info() {}, warn() {} }, request: async () => { n++; throw new Error('down'); } });
142
+ await l2.seedWithRetry(2, 1);
143
+ assert.equal(n, 2);
144
+ });
145
+
104
146
  test('trimRow keeps only allow-listed fields', () => {
105
147
  assert.deepEqual(trimRow({ key: 'k', unread: true, participants: [], thinkingOptions: [] }), { key: 'k', unread: true });
106
148
  });
@@ -130,6 +130,15 @@ export function createExtrasStore(getGlobalDb) {
130
130
  db().prepare('DELETE FROM bookmarks WHERE session_key = ?').run(sessionKey);
131
131
  },
132
132
 
133
+ /** Drop per-session data of sessions not in `liveKeys` (deleted while the connector wasn't
134
+ * listening). Returns how many sessions were dropped. */
135
+ pruneThreadExtras(liveKeys) {
136
+ const keys = db().prepare('SELECT session_key FROM thread_extras UNION SELECT session_key FROM bookmarks').all().map(r => r.session_key);
137
+ const gone = keys.filter(k => !liveKeys.has(k));
138
+ for (const k of gone) this.deleteThreadExtras(k);
139
+ return gone.length;
140
+ },
141
+
133
142
  /** All bookmarks, newest first. */
134
143
  listBookmarks() {
135
144
  return db().prepare('SELECT * FROM bookmarks ORDER BY created_at DESC').all().map(bookmarkRow);
@@ -66,4 +66,12 @@ test('bookmarks: add is idempotent per message, rename, delete, dropped with the
66
66
  assert.equal(extras.deleteBookmark('b4'), false);
67
67
  extras.deleteThreadExtras('agent:main:x');
68
68
  assert.deepEqual(extras.listBookmarks(), []);
69
+
70
+ // Sessions deleted while the connector wasn't listening: dropped against the live roster.
71
+ extras.addBookmark('b5', { sessionKey: 'agent:main:live', messageId: 'm1' });
72
+ extras.addBookmark('b6', { sessionKey: 'agent:main:gone', messageId: 'm1' });
73
+ extras.setThreadExtra('agent:main:gone2', 'intel', { a: 1 });
74
+ assert.equal(extras.pruneThreadExtras(new Set(['agent:main:live'])), 2);
75
+ assert.deepEqual(extras.listBookmarks().map(b => b.id), ['b5']);
76
+ assert.equal(extras.pruneThreadExtras(new Set(['agent:main:live'])), 0);
69
77
  });