@clawchatsai/connector 0.1.41 → 0.1.42

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/dist/index.js CHANGED
@@ -265,7 +265,7 @@ async function startClawChats(ctx, api) {
265
265
  // Gateway sharing: the server side signs grants and runs guest turns; the host provides the
266
266
  // signal server link and the WebRTC transport.
267
267
  peer: {
268
- gatewayId: () => loadConfig()?.gatewayId || '',
268
+ gatewayId: () => loadConfig()?.gatewayId || signaling?.currentGatewayId || '', // legacy installs have none saved: the session's
269
269
  signal: (msg) => signaling?.send(msg),
270
270
  openPeer: (shareId) => openPeerLink(shareId),
271
271
  },
@@ -314,7 +314,20 @@ async function startClawChats(ctx, api) {
314
314
  if (connectionId)
315
315
  webrtcPeer?.closePeer(connectionId);
316
316
  });
317
- signaling.on('share-grant-rejected', (shareId) => ctx.logger.warn(`[sharing] signal server rejected the grant for ${shareId}`));
317
+ signaling.on('share-grant-rejected', (shareId) => {
318
+ ctx.logger.warn(`[sharing] signal server rejected the grant for ${shareId}`);
319
+ app?.sharing?.onGrantRejected(shareId); // the owner sees it instead of "Saved"
320
+ });
321
+ // Installed before the setup flow saved the gateway id: log in by userId, then keep the id the server answers with,
322
+ // so the next login uses it and grants carry it. Nothing for the user to do.
323
+ signaling.on('gateway-id', (id) => {
324
+ const cfg = loadConfig();
325
+ if (cfg && !cfg.gatewayId) {
326
+ cfg.gatewayId = id;
327
+ saveConfig(cfg);
328
+ ctx.logger.info(`[clawchats] saved gateway id ${id} (this install had none; it logged in by user id)`);
329
+ }
330
+ });
318
331
  signaling.on('connected', () => {
319
332
  ctx.logger.info('Connected to signaling server');
320
333
  });
@@ -17,11 +17,13 @@ export declare class SignalingClient extends EventEmitter {
17
17
  private readonly serverUrl;
18
18
  private readonly userId;
19
19
  private readonly apiKey;
20
- private readonly gatewayId?;
20
+ private gatewayId?;
21
21
  private readonly hostname?;
22
22
  /** This connector's Ed25519 public key for gateway sharing (base64 SPKI). */
23
23
  private readonly peerPubKey?;
24
24
  private ws;
25
+ /** The gateway id this connection logged in as (its config's, else the one the server assigned). */
26
+ get currentGatewayId(): string;
25
27
  /** True only after gateway-auth-ok has been received. */
26
28
  private _connected;
27
29
  /**
@@ -38,6 +38,10 @@ export class SignalingClient extends EventEmitter {
38
38
  /** This connector's Ed25519 public key for gateway sharing (base64 SPKI). */
39
39
  peerPubKey;
40
40
  ws = null;
41
+ /** The gateway id this connection logged in as (its config's, else the one the server assigned). */
42
+ get currentGatewayId() {
43
+ return this.gatewayId ?? '';
44
+ }
41
45
  /** True only after gateway-auth-ok has been received. */
42
46
  _connected = false;
43
47
  /**
@@ -197,6 +201,13 @@ export class SignalingClient extends EventEmitter {
197
201
  const type = msg['type'];
198
202
  switch (type) {
199
203
  case 'gateway-auth-ok': {
204
+ // A gateway that logs in the legacy way (userId only) is told which gateway row it is; keep it for this
205
+ // session and tell the host so it can save it (index.ts), or sharing would sign grants for owner "".
206
+ const assigned = msg['gatewayId'];
207
+ if (typeof assigned === 'string' && assigned) {
208
+ this.gatewayId ||= assigned;
209
+ this.emit('gateway-id', assigned);
210
+ }
200
211
  this._connected = true;
201
212
  this.reconnectAttempts = 0;
202
213
  this._resetPingWatchdog();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.41",
3
+ "version": "0.1.42",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -210,6 +210,21 @@ export class SharingManager {
210
210
  }
211
211
 
212
212
  /** Remember an agent created here as a guest version (ClawChats → Sharing). */
213
+ /**
214
+ * The signal server refused a grant (it never activated the share). The owner is told, and a grant that was only
215
+ * pending is dropped, so both sides agree the share isn't there. A grant for a share that was already active
216
+ * (an edit the server turned down) stays: the share still works as it did.
217
+ */
218
+ onGrantRejected(shareId) {
219
+ const live = this._shares.find(x => x.id === shareId && x.as === 'owner');
220
+ if (live?.status !== 'active') this._db().prepare('DELETE FROM peer_grants WHERE share_id = ?').run(shareId);
221
+ this.broadcast?.(JSON.stringify({
222
+ type: 'clawchats', event: 'sharing-error', shareId,
223
+ message: live?.status === 'active' ? "The server didn't accept that change. Your sharing is as it was; try again." : "The server didn't accept it. Try again.",
224
+ }));
225
+ this._changed();
226
+ }
227
+
213
228
  /** Ids of the guest agents this connector made (the gateway may show them under another name). */
214
229
  guestAgentIds() {
215
230
  return new Set(this._db().prepare('SELECT agent_id FROM peer_guest_agents').all().map(r => r.agent_id));
@@ -250,8 +265,11 @@ export class SharingManager {
250
265
  if (prev && JSON.parse(prev.grant_json).requesterPubKey !== s.requester.pubKey) {
251
266
  throw new Error("The requester's gateway key changed since you approved them. Stop sharing and ask them to request again.");
252
267
  }
268
+ // The signal server only activates a grant signed for this gateway's own id; without one it would say no, silently.
269
+ const ownerGatewayId = this.gatewayId();
270
+ if (!ownerGatewayId) throw new Error("This gateway isn't connected to the signal server yet. Try again in a moment.");
253
271
  const grant = {
254
- v: 1, shareId, ownerGatewayId: this.gatewayId(), requesterGatewayId: s.requester.gatewayId, requesterPubKey: s.requester.pubKey,
272
+ v: 1, shareId, ownerGatewayId, requesterGatewayId: s.requester.gatewayId, requesterPubKey: s.requester.pubKey,
255
273
  requesterName: cleanName(s.requester.name || s.requester.email, 60) || null,
256
274
  agents: list, access, dailyCap: Math.max(1, Math.min(1000, Math.floor(Number(dailyCap) || DEFAULT_DAILY_CAP))), issuedAt: Date.now(),
257
275
  };
@@ -135,6 +135,31 @@ test("funnel: a person's chats go to the project chosen for them; renames follow
135
135
  assert.deepEqual(s.owner.funnels(), { 'h@x.dev': 'Team' });
136
136
  });
137
137
 
138
+ test('a gateway with no id can\'t sign a grant (the server would refuse it silently); a refused grant is dropped and the owner told', async () => {
139
+ const s = setup();
140
+ const frames = [];
141
+ const gid = s.owner.gatewayId;
142
+ s.owner.broadcast = f => frames.push(JSON.parse(f));
143
+ s.owner.gatewayId = () => ''; // a legacy install: logged in by user id, no id saved
144
+ assert.throws(() => s.owner.approve('sh1', { agents: [{ id: 'jarvis-guest', name: 'Jarvis' }] }), /isn't connected to the signal server yet/);
145
+ assert.equal(s.ownerSignals.filter(m => m.type === 'share-grant').length, 0, 'nothing was sent');
146
+
147
+ s.owner.gatewayId = gid;
148
+ s.owner.approve('sh1', { agents: [{ id: 'jarvis-guest', name: 'Jarvis' }] });
149
+ assert.equal(s.ownerSignals.filter(m => m.type === 'share-grant').at(-1).grant.ownerGatewayId, 'gwK');
150
+ // The server says no while the share is still pending: the local grant goes, and the browser is told.
151
+ s.owner.onGrantRejected('sh1');
152
+ assert.equal(s.owner._db().prepare('SELECT COUNT(*) n FROM peer_grants WHERE share_id = ?').get('sh1').n, 0);
153
+ assert.ok(frames.some(f => f.event === 'sharing-error' && f.shareId === 'sh1' && /Try again/.test(f.message)));
154
+
155
+ // An edit of a share that was already active: refused, but the share keeps working as it did.
156
+ s.owner.approve('sh1', { agents: [{ id: 'jarvis-guest', name: 'Jarvis' }] });
157
+ s.activate();
158
+ s.owner.onGrantRejected('sh1');
159
+ assert.equal(s.owner._db().prepare('SELECT COUNT(*) n FROM peer_grants WHERE share_id = ?').get('sh1').n, 1);
160
+ assert.ok(frames.at(-2).message.includes('as it was'));
161
+ });
162
+
138
163
  test('approve → verified on the requester → turn runs in a guest session on the owner, text streams back', async () => {
139
164
  const s = setup();
140
165
  assert.deepEqual(s.req.remoteAgents(), []);