@clawchatsai/connector 0.1.35 → 0.1.36

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.35",
3
+ "version": "0.1.36",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -16,11 +16,18 @@
16
16
  // `agent:<id>:<project>:chat:<id>` (legacy ClawChats). Everything else is hidden.
17
17
  // Team chat working sessions (server/team.js) are hidden too, but their run events are
18
18
  // forwarded: the room shows each agent's reply streaming.
19
+ //
20
+ // Spawned children (sessions_spawn: hidden `agent:<id>:subagent:<id>` runs and `visible: true`
21
+ // dashboard sessions) are visible when their parent chain reaches a visible chat; the browser
22
+ // nests them under it (Control UI session tree). Their rows carry `ccParentKey`, the parent the
23
+ // lens resolved, so the browser never re-derives the placement rule.
19
24
 
20
25
  const DASHBOARD_KEY = /^agent:[^:]+:dashboard:[^:]+$/;
21
26
  const LEGACY_CHAT_KEY = /^agent:[^:]+:[^:]+:chat:[^:]+$/;
22
27
  const UTILITY_MARK = '__clawchats_';
23
28
  const MAIN_SESSION_KEY = /^agent:[^:]+:main$/;
29
+ const SUBAGENT_KEY = /^agent:[^:]+:subagent:[^:]+$/;
30
+ const MAX_CHILD_DEPTH = 8;
24
31
 
25
32
  // Session row fields the browser needs. Anything else stays on the connector side.
26
33
  export const ROW_FIELDS = new Set([
@@ -32,12 +39,14 @@ export const ROW_FIELDS = new Set([
32
39
  'status', 'hasActiveRun', 'activeRunIds', 'lastRunId', 'lastRunError', 'endedAt', 'agentStatus',
33
40
  'model', 'modelProvider', 'thinkingLevel', 'contextTokens', 'totalTokens', 'totalTokensFresh',
34
41
  'inputTokens', 'outputTokens', 'permissionMode', 'workspaceDir', 'contextBudgetStatus', 'fastMode', 'effectiveFastMode',
42
+ 'startedAt', 'runtimeMs', 'ccParentKey',
35
43
  ]);
36
44
 
37
45
  // `sessions.changed` envelope fields (snapshot fields are spread next to these).
38
46
  const EVENT_ENVELOPE = new Set(['sessionKey', 'agentId', 'reason', 'phase', 'ts', 'messageId', 'catalogChanged']);
39
47
 
40
- const LIST_DEFAULTS = { excludeSubagents: true, excludeCron: true, excludeSystem: true };
48
+ // Subagent runs are listed: the lens keeps the ones nested under a visible chat.
49
+ const LIST_DEFAULTS = { excludeSubagents: false, excludeCron: true, excludeSystem: true };
41
50
 
42
51
  export function isUtilityKey(key) {
43
52
  return typeof key === 'string' && key.includes(UTILITY_MARK);
@@ -47,6 +56,18 @@ export function isCandidateKey(key) {
47
56
  return typeof key === 'string' && !isUtilityKey(key) && (DASHBOARD_KEY.test(key) || LEGACY_CHAT_KEY.test(key));
48
57
  }
49
58
 
59
+ /** Keys that can be a nested child: chat-shaped sessions and subagent runs. */
60
+ function isChildCandidateKey(key) {
61
+ return isCandidateKey(key) || (typeof key === 'string' && SUBAGENT_KEY.test(key));
62
+ }
63
+
64
+ /** The parent a hidden row nests under (Control UI resolveUiSessionNavigationParentKey), if any. */
65
+ function nestingParent(row) {
66
+ if (row.kind === 'global' || row.kind === 'unknown' || row.kind === 'group') return null;
67
+ const parent = row.parentSessionKey || row.spawnedBy;
68
+ return typeof parent === 'string' && parent && !MAIN_SESSION_KEY.test(parent) ? parent : null;
69
+ }
70
+
50
71
  // ── Visibility: port of the Control UI sidebar rule ──────────────────
51
72
  // ui/src/components/app-sidebar-session-parent.ts resolveSidebarSessionParentKey
52
73
  // ui/src/lib/sessions/navigation.ts sessionMatchesVisibleSessionScope
@@ -127,6 +148,7 @@ export class SessionLens {
127
148
  this.team = team;
128
149
  this.log = logger;
129
150
  this.hidden = new Set(); // candidate-shaped keys a row revealed as not a user chat
151
+ this.parents = new Map(); // child key -> parent key (nested children; visible when the chain reaches a chat)
130
152
  this.pending = new Map(); // browser req id -> { method, params }
131
153
  // Chats a browser opened with sessions.messages.subscribe (Control UI: one per selected
132
154
  // chat). session.message is forwarded only for these. subscriptionId -> session key.
@@ -137,17 +159,48 @@ export class SessionLens {
137
159
 
138
160
  /** Learn from a (possibly partial) row; returns whether the session is visible. */
139
161
  observe(key, row) {
140
- if (!isCandidateKey(key)) return false;
141
- const verdict = rowVerdict(row);
142
- if (verdict === true) this.hidden.add(key);
143
- else if (verdict === false) this.hidden.delete(key);
162
+ if (!isChildCandidateKey(key)) return false;
163
+ const verdict = SUBAGENT_KEY.test(key) ? true : rowVerdict(row);
164
+ if (verdict === true) {
165
+ this.hidden.add(key);
166
+ const parent = row && typeof row === 'object' && 'kind' in row ? nestingParent(row) : undefined;
167
+ if (parent) this.parents.set(key, parent);
168
+ else if (parent === null) this.parents.delete(key);
169
+ } else if (verdict === false) {
170
+ this.hidden.delete(key);
171
+ this.parents.delete(key);
172
+ }
144
173
  return this.isVisible(key);
145
174
  }
146
175
 
147
176
  isVisible(key) {
177
+ return this._isTopLevel(key) || !!this.parentOf(key);
178
+ }
179
+
180
+ _isTopLevel(key) {
148
181
  return isCandidateKey(key) && !this.hidden.has(key) && !this.team?.isWorkKey(key);
149
182
  }
150
183
 
184
+ /** The parent a visible nested child shows under, or null (not a child, or its chain never reaches a chat). */
185
+ parentOf(key) {
186
+ const parent = this.parents.get(key);
187
+ if (!parent || this.team?.isWorkKey(key)) return null;
188
+ let cur = parent;
189
+ for (let depth = 0; depth < MAX_CHILD_DEPTH; depth++) {
190
+ if (this._isTopLevel(cur)) return parent;
191
+ cur = this.parents.get(cur);
192
+ if (!cur || cur === key) return null;
193
+ }
194
+ return null;
195
+ }
196
+
197
+ /** Row/event fields plus the resolved parent link of a nested child. */
198
+ _withParent(key, out) {
199
+ const parent = this.parentOf(key);
200
+ if (parent) out.ccParentKey = parent;
201
+ return out;
202
+ }
203
+
151
204
  /** chat/agent run events: visible chats, team chat working sessions, utility sessions. */
152
205
  forwardsRunEvent(key) {
153
206
  return isUtilityKey(key) || this.isVisible(key) || !!this.team?.isWorkKey(key);
@@ -178,7 +231,9 @@ export class SessionLens {
178
231
  }
179
232
  if (!this.observe(key, payload.session || payload)) return null;
180
233
  if (payload.reason === 'delete' && this.extras) this.extras.deleteThreadExtras?.(key);
181
- return { type: 'event', event: 'sessions.changed', payload: trimEvent(payload) };
234
+ const out = trimEvent(payload);
235
+ if (payload.reason !== 'delete') this._withParent(key, out.session || out);
236
+ return { type: 'event', event: 'sessions.changed', payload: out };
182
237
  }
183
238
 
184
239
  /** `session.message` (live transcript entry). Forwarded only for watched visible chats. */
@@ -271,9 +326,11 @@ export class SessionLens {
271
326
  let legacy = null;
272
327
  try { legacy = this.extras?.getLegacyCreatedAt?.(); } catch (e) { this.log.warn?.(`[lens] legacy dates: ${e.message}`); }
273
328
  const out = [];
329
+ // Learn the whole page first: a child may be listed before its parent.
330
+ for (const row of rows || []) if (row) this.observe(row.key, row);
274
331
  for (const row of rows || []) {
275
- if (!row || !this.observe(row.key, row)) continue;
276
- const trimmed = trimRow(row);
332
+ if (!row || !this.isVisible(row.key)) continue;
333
+ const trimmed = this._withParent(row.key, trimRow(row));
277
334
  if (!trimmed.createdAt && legacy?.has(row.key)) trimmed.createdAt = legacy.get(row.key);
278
335
  out.push(trimmed);
279
336
  }
@@ -20,7 +20,7 @@ test('key shapes', () => {
20
20
  assert.equal(isCandidateKey(k), false, k);
21
21
  });
22
22
 
23
- test('child sessions stay hidden once a row revealed them, even for partial events', () => {
23
+ test('a child of an unresolved parent stays hidden, even for partial events', () => {
24
24
  const { l } = lens();
25
25
  assert.equal(l.sessionsChanged({ sessionKey: CHILD, reason: 'create', parentSessionKey: CHAT, label: 'x' }), null);
26
26
  assert.equal(l.sessionsChanged({ sessionKey: CHILD, phase: 'message', ts: 1 }), null);
@@ -47,7 +47,7 @@ test('run events: visible chats and utility sessions only', () => {
47
47
  test('browser sessions.list gets defaults and a filtered, trimmed response', () => {
48
48
  const { l } = lens();
49
49
  const out = JSON.parse(l.outbound(JSON.stringify({ type: 'req', id: 'r1', method: 'sessions.list', params: { limit: 50 } })));
50
- assert.deepEqual(out.params, { excludeSubagents: true, excludeCron: true, excludeSystem: true, limit: 50 });
50
+ assert.deepEqual(out.params, { excludeSubagents: false, excludeCron: true, excludeSystem: true, limit: 50 });
51
51
  assert.ok(l.ownsResponse('r1'));
52
52
  const res = l.response({ type: 'res', id: 'r1', ok: true, payload: {
53
53
  ts: 1, count: 4, totalCount: 4, hasMore: false, nextOffset: null, owners: [{}],
@@ -140,8 +140,8 @@ test('forks of chats are chats (edit/regen branches)', () => {
140
140
  assert.ok(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:f1', reason: 'create', kind: 'direct', forkSource: { sessionKey: LEGACY, sessionId: 's' } }));
141
141
  // fork of a dashboard chat: inherits the main link
142
142
  assert.ok(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:f2', reason: 'create', kind: 'direct', createdVia: 'operator', parentSessionKey: 'agent:main:main', spawnDepth: 0, forkSource: { sessionKey: CHAT, sessionId: 's' } }));
143
- // a fork nested under another chat is still a child
144
- assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:f3', reason: 'create', kind: 'direct', parentSessionKey: CHAT, forkSource: { sessionKey: LEGACY, sessionId: 's' } }), null); // parent is not the fork source
143
+ // a fork nested under another chat is still a child: shown nested under that chat
144
+ assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:f3', reason: 'create', kind: 'direct', parentSessionKey: CHAT, forkSource: { sessionKey: LEGACY, sessionId: 's' } }).payload.ccParentKey, CHAT); // parent is not the fork source
145
145
  });
146
146
 
147
147
  test('sessions.fork branches (parent = source chat) are top-level chats', () => {
@@ -150,7 +150,8 @@ test('sessions.fork branches (parent = source chat) are top-level chats', () =>
150
150
  assert.ok(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:br', reason: 'create', kind: 'direct', createdVia: 'operator', parentSessionKey: SRC, forkedFromParent: true, forkSource: { sessionKey: SRC, sessionId: 's', entryId: 'e' } }));
151
151
  assert.ok(l.forwardsRunEvent('agent:main:dashboard:br'));
152
152
  // a child whose parent is a chat but that is not a fork of it stays nested
153
- assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:kid', reason: 'create', kind: 'direct', parentSessionKey: SRC }), null);
153
+ assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:kid', reason: 'create', kind: 'direct', parentSessionKey: SRC }).payload.ccParentKey, SRC);
154
+ assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:br', reason: 'patch', label: 'x' }).payload.ccParentKey, undefined); // the branch itself is top level
154
155
  // fork of a channel session is not a chat
155
156
  assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:dashboard:br2', reason: 'create', kind: 'direct', parentSessionKey: DISCORD, forkSource: { sessionKey: DISCORD } }), null);
156
157
  });
@@ -177,3 +178,51 @@ test('listed rows without createdAt get the legacy creation time; gateway values
177
178
  ] } });
178
179
  assert.deepEqual(res.payload.sessions.map(s => s.createdAt), [111, 999]);
179
180
  });
181
+
182
+ const RUN = 'agent:main:subagent:run1';
183
+
184
+ test('spawned children of a visible chat are visible, nested via ccParentKey', () => {
185
+ const { l } = lens();
186
+ l.observe(CHAT, { key: CHAT, kind: 'direct', createdVia: 'operator', parentSessionKey: 'agent:main:main', spawnDepth: 0 });
187
+ // hidden run (subagent key)
188
+ const f = l.sessionsChanged({ sessionKey: RUN, reason: 'create', session: { key: RUN, kind: 'direct', spawnedBy: CHAT, parentSessionKey: CHAT, spawnDepth: 1, label: 'Review', owner: {} } });
189
+ assert.deepEqual(f.payload.session, { key: RUN, kind: 'direct', label: 'Review', ccParentKey: CHAT });
190
+ // later partial events keep the link
191
+ assert.equal(l.sessionsChanged({ sessionKey: RUN, phase: 'message', ts: 2 }).payload.ccParentKey, CHAT);
192
+ assert.ok(l.forwardsRunEvent(RUN));
193
+ // visible spawn (dashboard key)
194
+ const V = 'agent:main:dashboard:vis';
195
+ assert.ok(l.sessionsChanged({ sessionKey: V, reason: 'create', kind: 'direct', createdVia: 'spawn', spawnedBy: CHAT, parentSessionKey: CHAT, spawnDepth: 1 }));
196
+ assert.equal(l.parentOf(V), CHAT);
197
+ // grandchild through a run
198
+ const G = 'agent:main:subagent:run2';
199
+ assert.equal(l.sessionsChanged({ sessionKey: G, reason: 'create', kind: 'direct', spawnedBy: RUN, spawnDepth: 2 }).payload.ccParentKey, RUN);
200
+ });
201
+
202
+ test('children outside ClawChats chats stay hidden', () => {
203
+ const { l } = lens();
204
+ assert.equal(l.sessionsChanged({ sessionKey: RUN, reason: 'create', kind: 'direct', spawnedBy: 'agent:main:main', parentSessionKey: 'agent:main:main', spawnDepth: 1 }), null);
205
+ assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:subagent:d', reason: 'create', kind: 'direct', spawnedBy: DISCORD, spawnDepth: 1 }), null);
206
+ assert.equal(l.forwardsRunEvent(RUN), false);
207
+ // parent chat hidden (e.g. group kind) -> its children too
208
+ l.observe(CHAT, { key: CHAT, kind: 'group' });
209
+ assert.equal(l.sessionsChanged({ sessionKey: 'agent:main:subagent:e', reason: 'create', kind: 'direct', spawnedBy: CHAT, spawnDepth: 1 }), null);
210
+ // a cycle never resolves
211
+ l.observe('agent:main:subagent:x', { kind: 'direct', spawnedBy: 'agent:main:subagent:y' });
212
+ l.observe('agent:main:subagent:y', { kind: 'direct', spawnedBy: 'agent:main:subagent:x' });
213
+ assert.equal(l.parentOf('agent:main:subagent:x'), null);
214
+ });
215
+
216
+ test('list responses keep children whose parent is listed later on the page', () => {
217
+ const { l } = lens();
218
+ l.outbound(JSON.stringify({ type: 'req', id: 'c1', method: 'sessions.list', params: {} }));
219
+ const res = l.response({ type: 'res', id: 'c1', ok: true, payload: { sessions: [
220
+ { key: RUN, kind: 'direct', spawnedBy: CHAT, parentSessionKey: CHAT, spawnDepth: 1, label: 'r', runtimeMs: 5, startedAt: 1 },
221
+ { key: CHAT, kind: 'direct', createdVia: 'operator', parentSessionKey: 'agent:main:main', spawnDepth: 0, label: 'c' },
222
+ { key: 'agent:main:subagent:other', kind: 'direct', spawnedBy: 'agent:main:main', spawnDepth: 1 },
223
+ ] } });
224
+ assert.deepEqual(res.payload.sessions, [
225
+ { key: RUN, kind: 'direct', label: 'r', runtimeMs: 5, startedAt: 1, ccParentKey: CHAT },
226
+ { key: CHAT, kind: 'direct', label: 'c' },
227
+ ]);
228
+ });