@clawchatsai/connector 0.1.39 → 0.1.41

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.39",
3
+ "version": "0.1.41",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
package/server/index.js CHANGED
@@ -255,7 +255,7 @@ export function createApp(config = {}) {
255
255
  if (method === 'GET' && urlPath === '/api/sharing') {
256
256
  // Enabled once the signal server has sharing on for this account (it then sends share lists).
257
257
  if (!sharing?.serverEnabled) return send(res, 200, { enabled: false, shares: [], remoteAgents: [] });
258
- return send(res, 200, { enabled: true, fingerprint: peerKey.fingerprint, shares: sharing.list(), remoteAgents: sharing.remoteAgents(), contacts: sharing.contacts(), me: sharing.gatewayId?.() || null });
258
+ return send(res, 200, { enabled: true, fingerprint: peerKey.fingerprint, shares: sharing.list(), remoteAgents: sharing.remoteAgents(), contacts: sharing.contacts(), me: sharing.gatewayId?.() || null, guestAgentIds: [...sharing.guestAgentIds()] });
259
259
  }
260
260
  // The project a person's chats go to (peer/sharing.js funnel). Keyed by email.
261
261
  if (method === 'GET' && urlPath === '/api/sharing/funnel') return send(res, 200, { funnel: sharing?.funnels() || {} });
@@ -278,7 +278,7 @@ export function createApp(config = {}) {
278
278
  // Owner asks for a guest version of one of its agents (peer/guest-agent.js); changes this gateway's config.
279
279
  const { agentId } = await parseBody(req);
280
280
  if (typeof agentId !== 'string' || !agentId) return sendError(res, 400, 'agentId is required');
281
- try { const r = await createGuestAgent(gwRequest, agentId); sharing.markGuestAgent(r.agentId); return send(res, 200, r); }
281
+ try { const r = await createGuestAgent(gwRequest, agentId, { known: sharing.guestAgentIds() }); sharing.markGuestAgent(r.agentId); return send(res, 200, r); }
282
282
  catch (e) { return sendError(res, 400, e.message); }
283
283
  }
284
284
  if ((p = matchRoute(method, urlPath, 'POST /api/sharing/:id/verify-key'))) {
@@ -67,7 +67,7 @@ export async function ensureGuestRestrictions(request, agentId) {
67
67
  }
68
68
 
69
69
  /** A new agent is only usable once the gateway's config reload picked it up. */
70
- async function waitForAgent(request, agentId, { tries = 40, ms = 250 } = {}) {
70
+ async function waitForAgent(request, agentId, { tries = 120, ms = 250 } = {}) {
71
71
  for (let i = 0; i < tries; i++) {
72
72
  if (((await request('agents.list', {}))?.agents || []).some(a => a.id === agentId)) return true;
73
73
  await wait(ms);
@@ -75,29 +75,37 @@ async function waitForAgent(request, agentId, { tries = 40, ms = 250 } = {}) {
75
75
  return false;
76
76
  }
77
77
 
78
- /** The guest version of `agentId` if it already exists (by name). */
79
- export function findGuestAgent(agents, agentId) {
78
+ /** Whether an agent is a guest version: called "<name> (guest)", or one this connector made (`known`: their ids). */
79
+ export function isGuestAgent(a, known = new Set()) {
80
+ return /\(guest\)$/.test(agentName(a)) || known.has(a?.id);
81
+ }
82
+
83
+ /**
84
+ * The guest version of `agentId` if it already exists. By name ("Jarvis (guest)"), or by the id it was made with
85
+ * ("main-guest"): renaming the original ("main" -> "homiabot") changes the name it looks for, not the guest that
86
+ * already exists. A guest the gateway shows under another name is still found when this connector made it (`known`).
87
+ */
88
+ export function findGuestAgent(agents, agentId, known = new Set()) {
80
89
  const src = agents.find(a => a.id === agentId);
81
90
  if (!src) return null;
82
- // By name ("Jarvis (guest)"), or by the id it was made with ("main-guest"): renaming the original
83
- // ("main" -> "homiabot") changes the name it looks for, not the guest that already exists.
84
91
  return agents.find(a => agentName(a) === guestName(agentName(src)))
85
- || agents.find(a => a.id === `${agentId}-guest` && /\(guest\)$/.test(agentName(a))) || null;
92
+ || agents.find(a => a.id === `${agentId}-guest` && isGuestAgent(a, known))
93
+ || agents.find(a => a.id.startsWith(`${agentId}-guest-`) && known.has(a.id)) || null;
86
94
  }
87
95
 
88
96
  /**
89
97
  * Create (or reuse) the guest version of an agent. Returns { agentId, name, created }.
90
98
  * @param {(m: string, p: object, t?: number) => Promise<any>} request gateway RPC (operator.admin)
91
99
  */
92
- export async function createGuestAgent(request, agentId) {
100
+ export async function createGuestAgent(request, agentId, { known = new Set() } = {}) {
93
101
  const agents = (await request('agents.list', {}))?.agents || [];
94
102
  const src = agents.find(a => a.id === agentId);
95
103
  if (!src) throw new Error(`unknown agent: ${agentId}`);
96
- if (/\(guest\)$/.test(agentName(src))) {
104
+ if (isGuestAgent(src, known)) {
97
105
  await ensureGuestRestrictions(request, src.id); // repairs a guest agent left without them
98
106
  return { agentId: src.id, name: agentName(src), created: false };
99
107
  }
100
- const existing = findGuestAgent(agents, agentId);
108
+ const existing = findGuestAgent(agents, agentId, known);
101
109
  if (existing) {
102
110
  // Repair a guest half-made by an earlier run: restore its restrictions, then the personality a
103
111
  // crash between create and copy would have skipped (only missing files; never clobber edits).
@@ -93,3 +93,12 @@ test('findGuestAgent: the guest made as "main (guest)" is still found after the
93
93
  // An agent that merely has the id shape isn't taken for a guest.
94
94
  assert.equal(findGuestAgent([{ id: 'x', name: 'x' }, { id: 'x-guest', name: 'something else' }], 'x'), null);
95
95
  });
96
+
97
+ test('a guest the gateway shows under the original\'s name is still found (and hidden) when this connector made it', async () => {
98
+ const { findGuestAgent, isGuestAgent } = await import('./guest-agent.js');
99
+ const agents = [{ id: 'capy', identity: { name: 'Capy' } }, { id: 'capy-guest', identity: { name: 'Capy' } }, { id: 'capy-guest-2', identity: { name: 'Capy' } }];
100
+ assert.equal(findGuestAgent(agents, 'capy'), null, 'without the connector\'s record it can\'t tell');
101
+ assert.equal(findGuestAgent(agents, 'capy', new Set(['capy-guest']))?.id, 'capy-guest');
102
+ assert.equal(findGuestAgent([agents[0], agents[2]], 'capy', new Set(['capy-guest-2']))?.id, 'capy-guest-2');
103
+ assert.ok(isGuestAgent(agents[1], new Set(['capy-guest'])) && !isGuestAgent(agents[0], new Set(['capy-guest'])));
104
+ });
@@ -16,7 +16,7 @@
16
16
  import crypto from 'node:crypto';
17
17
  import { PeerLink } from './link.js';
18
18
  import { canonicalJson, verifySignature, fingerprint } from './keys.js';
19
- import { hasGuestRestrictions, ensureGuestRestrictions, findGuestAgent } from './guest-agent.js';
19
+ import { hasGuestRestrictions, ensureGuestRestrictions, findGuestAgent, isGuestAgent } from './guest-agent.js';
20
20
 
21
21
  const TURN_TIMEOUT_MS = 10 * 60_000;
22
22
  const DEFAULT_DAILY_CAP = 50;
@@ -210,6 +210,11 @@ export class SharingManager {
210
210
  }
211
211
 
212
212
  /** Remember an agent created here as a guest version (ClawChats → Sharing). */
213
+ /** Ids of the guest agents this connector made (the gateway may show them under another name). */
214
+ guestAgentIds() {
215
+ return new Set(this._db().prepare('SELECT agent_id FROM peer_guest_agents').all().map(r => r.agent_id));
216
+ }
217
+
213
218
  markGuestAgent(agentId) {
214
219
  this._db().prepare('INSERT OR IGNORE INTO peer_guest_agents (agent_id, created_at) VALUES (?, ?)').run(agentId, Date.now());
215
220
  }
@@ -669,8 +674,9 @@ export class SharingManager {
669
674
  const shown = id => {
670
675
  const a = agents.find(x => x.id === id);
671
676
  if (!a) return null;
672
- if (!/\(guest\)$/.test(label(a))) return label(a);
673
- const src = agents.find(o => o.id !== id && findGuestAgent(agents, o.id)?.id === id);
677
+ const known = this.guestAgentIds();
678
+ if (!isGuestAgent(a, known)) return label(a);
679
+ const src = agents.find(o => o.id !== id && findGuestAgent(agents, o.id, known)?.id === id);
674
680
  return src ? label(src) : null;
675
681
  };
676
682
  const reissued = [];
@@ -759,7 +765,7 @@ export class SharingManager {
759
765
  const g = this._grantToOwnerOf(remoteAgentId);
760
766
  if (!g) return null;
761
767
  const agents = (await this.request('agents.list', {}))?.agents || [];
762
- const guestId = findGuestAgent(agents, localAgentId)?.id;
768
+ const guestId = findGuestAgent(agents, localAgentId, this.guestAgentIds())?.id;
763
769
  const shared = g.grant.agents.find(a => a.id === guestId) || g.grant.agents.find(a => a.id === localAgentId);
764
770
  if (!shared) return null;
765
771
  return this._guestTurn(g.shareId, { turnId, agentId: shared.id, roomId, roomTitle, message, inOwnerRoom: true, byPerson: String(remoteAgentId).startsWith('person:') },