@clawchatsai/connector 0.1.45 → 0.1.46

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.46",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
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')) {
@@ -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
  });