@clawchatsai/connector 0.1.30 → 0.1.32

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
@@ -62,6 +62,11 @@ class BrowserClientMap extends Map {
62
62
  }
63
63
  }
64
64
  const connectedClients = new BrowserClientMap();
65
+ /** The local gateway's WebSocket URL (the app's gateway client and Desktop tunnels). */
66
+ const GATEWAY_WS_URL = process.env.GATEWAY_WS_URL || 'ws://localhost:18789';
67
+ /** Open Desktop tunnels per browser connection (server/desktop-tunnel.js); a viewer uses one or two. */
68
+ const desktopTunnels = new Map();
69
+ const MAX_TUNNELS_PER_CONNECTION = 8;
65
70
  /** Reassembly buffers for chunked RPC requests from browser (large uploads). */
66
71
  const rpcReqChunkBuffers = new Map();
67
72
  /** Reassembly buffers for chunked gateway-msg from browser (large payloads like image attachments). */
@@ -253,7 +258,7 @@ async function startClawChats(ctx, api) {
253
258
  app = serverModule.createApp({
254
259
  dataDir,
255
260
  port: parseInt(process.env.PORT || '3001', 10),
256
- gatewayUrl: process.env.GATEWAY_WS_URL || 'ws://localhost:18789',
261
+ gatewayUrl: GATEWAY_WS_URL,
257
262
  authToken: process.env.CLAWCHATS_AUTH_TOKEN || '', // P2P: DataChannel is the auth boundary
258
263
  gatewayToken, // For WS auth to local OpenClaw gateway
259
264
  llm: api.runtime?.llm, // host-run completions (chat-title fallback, /api/title)
@@ -329,6 +334,31 @@ async function startClawChats(ctx, api) {
329
334
  setupDataChannelHandler(dc, connectionId, ctx);
330
335
  signaling?.reportConnectionCount(webrtcPeer?.activeCount ?? 0);
331
336
  });
337
+ // A Desktop view's tunnel to the gateway's desktop stream: only on an authenticated browser
338
+ // connection (webrtc-peer.ts already refuses gateway-sharing peers).
339
+ // @ts-expect-error — server/ is plain JS with no .d.ts
340
+ const { openDesktopTunnel } = await import('../server/desktop-tunnel.js');
341
+ webrtcPeer.on('tunnel-channel', (channel, connectionId) => {
342
+ const open = desktopTunnels.get(connectionId) ?? new Set();
343
+ if (!connectedClients.has(connectionId) || open.size >= MAX_TUNNELS_PER_CONNECTION) {
344
+ ctx.logger.warn(`Desktop tunnel refused for ${connectionId} (${connectedClients.has(connectionId) ? 'too many tunnels' : 'not signed in'})`);
345
+ try {
346
+ channel.close();
347
+ }
348
+ catch { /* gone */ }
349
+ return;
350
+ }
351
+ const tunnel = openDesktopTunnel(channel, { gatewayUrl: GATEWAY_WS_URL, log: ctx.logger });
352
+ if (!tunnel)
353
+ return;
354
+ open.add(tunnel);
355
+ desktopTunnels.set(connectionId, open);
356
+ channel.addEventListener('close', () => {
357
+ open.delete(tunnel);
358
+ if (!open.size && desktopTunnels.get(connectionId) === open)
359
+ desktopTunnels.delete(connectionId);
360
+ });
361
+ });
332
362
  // Gateway sharing: a DataChannel with another gateway. Never on the browser path.
333
363
  webrtcPeer.on('peer-datachannel', (dc, info) => {
334
364
  if (info.role === 'owner' && info.peer) {
@@ -362,6 +392,9 @@ async function startClawChats(ctx, api) {
362
392
  webrtcPeer.on('datachannel-closed', (connectionId) => {
363
393
  ctx.logger.info(`Browser disconnected: ${connectionId}`);
364
394
  connectedClients.delete(connectionId);
395
+ for (const tunnel of desktopTunnels.get(connectionId) ?? [])
396
+ tunnel.close();
397
+ desktopTunnels.delete(connectionId);
365
398
  signaling?.reportConnectionCount(webrtcPeer?.activeCount ?? 0);
366
399
  });
367
400
  // Wire signaling ICE events to peer manager
@@ -13,6 +13,8 @@ import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate, } from 'node
13
13
  // Public interfaces
14
14
  // ---------------------------------------------------------------------------
15
15
  const MAX_PENDING_ICE = 200;
16
+ /** Label prefix of a Desktop tunnel channel (matches server/desktop-tunnel.js). */
17
+ const TUNNEL_LABEL_PREFIX = 'tunnel:';
16
18
  const MAX_PEER_CONNECTIONS_PER_SHARE = 3;
17
19
  const PEER_OPEN_TIMEOUT_MS = 30_000;
18
20
  /** sha-256 DTLS fingerprint from an SDP ("" when absent). */
@@ -132,6 +134,20 @@ export class WebRTCPeerManager extends EventEmitter {
132
134
  // W3C-standard ondatachannel
133
135
  pc.ondatachannel = (event) => {
134
136
  const channel = event.channel;
137
+ // A Desktop view's tunnel (server/desktop-tunnel.js): an extra channel on a browser
138
+ // connection. It isn't the connection's main channel, so closing it must not close the
139
+ // peer connection. Gateway-sharing peers never get one.
140
+ if (String(channel.label || '').startsWith(TUNNEL_LABEL_PREFIX)) {
141
+ if (this.peerInfo.has(connectionId)) {
142
+ try {
143
+ channel.close();
144
+ }
145
+ catch { /* gone */ }
146
+ return;
147
+ }
148
+ this.emit('tunnel-channel', channel, connectionId);
149
+ return;
150
+ }
135
151
  this._handleDataChannel(channel, connectionId);
136
152
  };
137
153
  // Forward local ICE candidates to browser via signaling
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -252,12 +252,16 @@ export async function handleWorkspaceUpload(req, res, query) {
252
252
  send(res, 200, { ok: true, uploaded });
253
253
  }
254
254
 
255
+ function isBadName(name) {
256
+ return name === '.' || name === '..' || /[/\\\0]/.test(name) || name.trim() !== name;
257
+ }
258
+
255
259
  // Creates one empty file or folder named `name` inside the directory `path`.
256
260
  export function handleWorkspaceCreate(req, res, query) {
257
261
  const { path: parentPath, name, type } = query;
258
262
  if (!parentPath || !name) return sendError(res, 400, 'Missing path or name parameter');
259
263
  if (type !== 'file' && type !== 'dir') return sendError(res, 400, 'type must be file or dir');
260
- if (name === '.' || name === '..' || /[/\\\0]/.test(name) || name.trim() !== name) return sendError(res, 400, 'Invalid name');
264
+ if (isBadName(name)) return sendError(res, 400, 'Invalid name');
261
265
  const parent = resolveInHome(parentPath);
262
266
  if (!parent) return sendError(res, 403, 'Access denied');
263
267
  if (!fs.existsSync(parent) || !fs.statSync(parent).isDirectory()) return sendError(res, 404, 'Target directory not found');
@@ -271,3 +275,55 @@ export function handleWorkspaceCreate(req, res, query) {
271
275
  }
272
276
  send(res, 200, { ok: true, path: target + (type === 'dir' ? '/' : ''), type });
273
277
  }
278
+
279
+ // `name` inside `dir`, or with " (2)", " (3)"... before the extension when that is taken.
280
+ function freeName(dir, name) {
281
+ if (!exists(path.join(dir, name))) return name;
282
+ const ext = path.extname(name);
283
+ const stem = path.basename(name, ext);
284
+ for (let n = 2; ; n++) {
285
+ const candidate = `${stem} (${n})${ext}`;
286
+ if (!exists(path.join(dir, candidate))) return candidate;
287
+ }
288
+ }
289
+
290
+ // Moves (op 'move') or copies (op 'copy') `path` into directory `dir` as `name` (default: its
291
+ // current name). An existing target is a 409 unless `unique=1`, which picks a free "name (2)".
292
+ // Folders copy recursively; a folder can't go inside itself.
293
+ function transfer(res, query, op) {
294
+ const { path: srcPath, dir: dirPath, unique } = query;
295
+ if (!srcPath || !dirPath) return sendError(res, 400, 'Missing path or dir parameter');
296
+ const src = resolveInHome(srcPath);
297
+ const dir = resolveInHome(dirPath);
298
+ if (!src || !dir || src === HOME) return sendError(res, 403, 'Access denied');
299
+ if (!exists(src)) return sendError(res, 404, 'Path not found');
300
+ if (!exists(dir) || !fs.statSync(dir).isDirectory()) return sendError(res, 404, 'Target directory not found');
301
+ let name = query.name || path.basename(src);
302
+ if (isBadName(name)) return sendError(res, 400, 'Invalid name');
303
+ const isDir = fs.statSync(src).isDirectory();
304
+ if (isDir && (dir === src || dir.startsWith(src + path.sep))) return sendError(res, 400, 'Can\'t put a folder inside itself');
305
+ let target = path.join(dir, name);
306
+ if (target === src && op === 'move') return send(res, 200, { ok: true, path: target + (isDir ? '/' : '') });
307
+ if (exists(target)) {
308
+ if (unique !== '1') return sendError(res, 409, `"${name}" already exists`);
309
+ name = freeName(dir, name);
310
+ target = path.join(dir, name);
311
+ }
312
+ try {
313
+ if (op === 'copy') fs.cpSync(src, target, { recursive: true, errorOnExist: true, force: false });
314
+ else {
315
+ try { fs.renameSync(src, target); }
316
+ catch (err) {
317
+ if (err.code !== 'EXDEV') throw err;
318
+ fs.cpSync(src, target, { recursive: true, errorOnExist: true, force: false });
319
+ fs.rmSync(src, { recursive: true, force: true });
320
+ }
321
+ }
322
+ } catch (err) {
323
+ return sendError(res, 500, `${op === 'copy' ? 'Copy' : 'Move'} failed: ${err.message}`);
324
+ }
325
+ send(res, 200, { ok: true, path: target + (isDir ? '/' : ''), type: isDir ? 'dir' : 'file' });
326
+ }
327
+
328
+ export function handleWorkspaceMove(req, res, query) { return transfer(res, query, 'move'); }
329
+ export function handleWorkspaceCopy(req, res, query) { return transfer(res, query, 'copy'); }
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
3
3
  import fs from 'node:fs';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
- import { handleWorkspaceFileRead, handleWorkspaceCreate, resolveReadPath } from './filesystem.js';
6
+ import { handleWorkspaceFileRead, handleWorkspaceCreate, handleWorkspaceMove, handleWorkspaceCopy, resolveReadPath } from './filesystem.js';
7
7
 
8
8
  function read(filePath, workspaceDir) {
9
9
  const out = { status: 0, body: '' };
@@ -162,3 +162,64 @@ test('create: makes an empty file or folder inside home, rejects bad names and e
162
162
  fs.rmSync(dir, { recursive: true, force: true });
163
163
  }
164
164
  });
165
+
166
+ function call(handler, query) {
167
+ const out = { status: 0, body: null };
168
+ const res = {
169
+ writeHead(status) { out.status = status; },
170
+ setHeader() {},
171
+ end(body) { out.body = JSON.parse(String(body ?? 'null')); },
172
+ };
173
+ handler({}, res, query);
174
+ return out;
175
+ }
176
+
177
+ test('move: renames in place, moves into another folder, refuses clashes unless unique', () => {
178
+ const dir = fs.mkdtempSync(path.join(os.homedir(), '.cc-move-test-'));
179
+ try {
180
+ fs.writeFileSync(path.join(dir, 'a.md'), 'A');
181
+ fs.writeFileSync(path.join(dir, 'b.md'), 'B');
182
+ fs.mkdirSync(path.join(dir, 'sub'));
183
+ const renamed = call(handleWorkspaceMove, { path: path.join(dir, 'a.md'), dir, name: 'c.md' });
184
+ assert.equal(renamed.status, 200);
185
+ assert.equal(renamed.body.path, path.join(dir, 'c.md'));
186
+ assert.equal(fs.readFileSync(path.join(dir, 'c.md'), 'utf8'), 'A');
187
+ assert.ok(!fs.existsSync(path.join(dir, 'a.md')));
188
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir, name: 'b.md' }).status, 409);
189
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir, name: 'x/y' }).status, 400);
190
+ // Moving onto itself is a no-op, not a clash.
191
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir }).status, 200);
192
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir: path.join(dir, 'sub') }).status, 200);
193
+ assert.equal(fs.readFileSync(path.join(dir, 'sub', 'c.md'), 'utf8'), 'A');
194
+ fs.writeFileSync(path.join(dir, 'c.md'), 'A2');
195
+ const clash = call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir: path.join(dir, 'sub'), unique: '1' });
196
+ assert.equal(clash.body.path, path.join(dir, 'sub', 'c (2).md'));
197
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'sub'), dir: path.join(dir, 'sub') }).status, 400);
198
+ assert.equal(call(handleWorkspaceMove, { path: dir, dir: path.join(dir, 'sub') }).status, 400);
199
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'missing'), dir }).status, 404);
200
+ assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'b.md'), dir: '/tmp' }).status, 403);
201
+ assert.equal(call(handleWorkspaceMove, { path: os.homedir(), dir }).status, 403);
202
+ } finally {
203
+ fs.rmSync(dir, { recursive: true, force: true });
204
+ }
205
+ });
206
+
207
+ test('copy: copies files and folders, numbering clashes with unique', () => {
208
+ const dir = fs.mkdtempSync(path.join(os.homedir(), '.cc-copy-test-'));
209
+ try {
210
+ fs.mkdirSync(path.join(dir, 'src', 'deep'), { recursive: true });
211
+ fs.writeFileSync(path.join(dir, 'src', 'deep', 'f.txt'), 'F');
212
+ fs.writeFileSync(path.join(dir, 'a.md'), 'A');
213
+ const dup = call(handleWorkspaceCopy, { path: path.join(dir, 'a.md'), dir, name: 'a copy.md', unique: '1' });
214
+ assert.equal(dup.body.path, path.join(dir, 'a copy.md'));
215
+ assert.equal(call(handleWorkspaceCopy, { path: path.join(dir, 'a.md'), dir, name: 'a copy.md', unique: '1' }).body.path, path.join(dir, 'a copy (2).md'));
216
+ assert.equal(call(handleWorkspaceCopy, { path: path.join(dir, 'a.md'), dir }).status, 409);
217
+ assert.equal(fs.readFileSync(path.join(dir, 'a.md'), 'utf8'), 'A');
218
+ const folder = call(handleWorkspaceCopy, { path: path.join(dir, 'src'), dir, unique: '1' });
219
+ assert.equal(folder.body.path, path.join(dir, 'src (2)') + '/');
220
+ assert.equal(fs.readFileSync(path.join(dir, 'src (2)', 'deep', 'f.txt'), 'utf8'), 'F');
221
+ assert.equal(call(handleWorkspaceCopy, { path: path.join(dir, 'src'), dir: path.join(dir, 'src', 'deep') }).status, 400);
222
+ } finally {
223
+ fs.rmSync(dir, { recursive: true, force: true });
224
+ }
225
+ });
@@ -0,0 +1,154 @@
1
+ // Desktop tunnel: one extra DataChannel per Desktop view, piped to the gateway's desktop WebSocket.
2
+ //
3
+ // The Control UI's Desktop panel opens a raw binary WebSocket straight to the gateway
4
+ // (/desktop/observe?token=…, minted by the desktop.observe RPC). A ClawChats browser can't reach
5
+ // the gateway directly, so it opens a DataChannel labelled "tunnel:<that path>" on its existing
6
+ // WebRTC connection; this module connects it to the gateway on loopback and copies bytes both ways.
7
+ //
8
+ // Rules:
9
+ // - Only the desktop stream paths, with only a token parameter: the tunnel can't reach any other
10
+ // gateway URL. The gateway checks the one-time token itself; nothing here holds credentials.
11
+ // - Callers only pass channels from an authenticated browser connection (never a share peer).
12
+ // - Gateway → browser data goes in pieces of CHUNK bytes, so chat on the main channel interleaves.
13
+ // - Backpressure: when the channel has more than HIGH_WATER queued, stop reading the gateway
14
+ // socket until it drains below LOW_WATER. Memory stays bounded on a slow or relayed link.
15
+ // - Text messages on the channel are control frames: the connector sends one
16
+ // {"type":"close","code","reason"} before closing, so the viewer can show why (WebSocket close
17
+ // codes don't survive a DataChannel close).
18
+
19
+ import { WebSocket as WS } from 'ws';
20
+
21
+ export const TUNNEL_LABEL_PREFIX = 'tunnel:';
22
+ const ALLOWED_PATHS = new Set(['/desktop/observe', '/desktop/audio']);
23
+ export const CHUNK = 16 * 1024;
24
+ export const HIGH_WATER = 1024 * 1024;
25
+ export const LOW_WATER = 256 * 1024;
26
+ // Browser → gateway bytes buffered while the gateway socket is still connecting.
27
+ const MAX_PENDING_BYTES = 1024 * 1024;
28
+ const MAX_LABEL_LENGTH = 2048;
29
+
30
+ /** "tunnel:/desktop/observe?token=…" → "/desktop/observe?token=…", or null when not allowed. */
31
+ export function parseTunnelLabel(label) {
32
+ if (typeof label !== 'string' || !label.startsWith(TUNNEL_LABEL_PREFIX) || label.length > MAX_LABEL_LENGTH) return null;
33
+ const raw = label.slice(TUNNEL_LABEL_PREFIX.length);
34
+ if (!raw.startsWith('/') || raw.startsWith('//')) return null;
35
+ let url;
36
+ try { url = new URL(raw, 'http://127.0.0.1'); } catch { return null; }
37
+ if (url.origin !== 'http://127.0.0.1' || !ALLOWED_PATHS.has(url.pathname) || url.hash) return null;
38
+ const keys = [...url.searchParams.keys()];
39
+ const token = url.searchParams.get('token');
40
+ if (keys.length !== 1 || keys[0] !== 'token' || !token) return null;
41
+ return `${url.pathname}?token=${encodeURIComponent(token)}`;
42
+ }
43
+
44
+ /** ws://host:port (the gateway client's URL) + path → the gateway desktop socket URL. */
45
+ export function gatewaySocketUrl(gatewayUrl, path) {
46
+ const base = new URL(gatewayUrl);
47
+ return new URL(path, `${base.protocol}//${base.host}`).toString();
48
+ }
49
+
50
+ const toBuffer = data => Buffer.isBuffer(data) ? data
51
+ : data instanceof ArrayBuffer ? Buffer.from(data)
52
+ : ArrayBuffer.isView(data) ? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
53
+ : null;
54
+
55
+ /**
56
+ * Pipe a browser DataChannel (W3C RTCDataChannel API: send, close, readyState, bufferedAmount,
57
+ * onmessage, onclose, onbufferedamountlow) to the gateway desktop socket its label names.
58
+ * Returns a handle with close(), or null when the label isn't an allowed tunnel (channel closed).
59
+ */
60
+ export function openDesktopTunnel(dc, { gatewayUrl, WebSocketImpl = WS, log = console } = {}) {
61
+ const path = parseTunnelLabel(dc.label);
62
+ if (!path) {
63
+ try { dc.close(); } catch { /* gone */ }
64
+ return null;
65
+ }
66
+ try { dc.binaryType = 'arraybuffer'; } catch { /* read-only in some polyfills */ }
67
+ dc.bufferedAmountLowThreshold = LOW_WATER;
68
+ // A channel can arrive before it's open; the gateway speaks first (RFB), so connect once it is.
69
+ if (dc.readyState === 'connecting') {
70
+ const handle = { closed: false, inner: null, close() { this.closed = true; this.inner?.close(); try { dc.close(); } catch { /* gone */ } } };
71
+ dc.onopen = () => { if (!handle.closed) handle.inner = pipe(dc, path, { gatewayUrl, WebSocketImpl, log }); };
72
+ dc.onclose = () => { handle.closed = true; };
73
+ return handle;
74
+ }
75
+ return pipe(dc, path, { gatewayUrl, WebSocketImpl, log });
76
+ }
77
+
78
+ function pipe(dc, path, { gatewayUrl, WebSocketImpl, log }) {
79
+ let closed = false;
80
+ const sendControl = (code, reason) => {
81
+ try { if (dc.readyState === 'open') dc.send(JSON.stringify({ type: 'close', code, reason: String(reason || '').slice(0, 200) })); }
82
+ catch { /* channel already gone */ }
83
+ };
84
+ const ws = new WebSocketImpl(gatewaySocketUrl(gatewayUrl, path), { perMessageDeflate: false });
85
+ let pending = [];
86
+ let pendingBytes = 0;
87
+ let paused = false;
88
+
89
+ const finish = (code, reason, { fromChannel = false } = {}) => {
90
+ if (closed) return;
91
+ closed = true;
92
+ pending = [];
93
+ if (!fromChannel) {
94
+ sendControl(code, reason);
95
+ try { dc.close(); } catch { /* gone */ }
96
+ }
97
+ try {
98
+ if (ws.readyState === WebSocketImpl.OPEN) ws.close(1000);
99
+ else if (ws.readyState === WebSocketImpl.CONNECTING) ws.terminate();
100
+ } catch { /* gone */ }
101
+ };
102
+
103
+ const resume = () => {
104
+ if (!paused || closed) return;
105
+ paused = false;
106
+ try { ws.resume(); } catch { /* closed */ }
107
+ };
108
+ dc.onbufferedamountlow = resume;
109
+
110
+ ws.on('open', () => {
111
+ for (const buf of pending) ws.send(buf);
112
+ pending = [];
113
+ pendingBytes = 0;
114
+ });
115
+ ws.on('message', (data) => {
116
+ if (closed || dc.readyState !== 'open') return;
117
+ const buf = Array.isArray(data) ? Buffer.concat(data) : toBuffer(data) ?? Buffer.from(String(data));
118
+ try {
119
+ for (let i = 0; i < buf.length; i += CHUNK) dc.send(buf.subarray(i, Math.min(i + CHUNK, buf.length)));
120
+ } catch (e) {
121
+ finish(1011, `tunnel send failed: ${e.message}`);
122
+ return;
123
+ }
124
+ if (!paused && dc.bufferedAmount > HIGH_WATER) {
125
+ paused = true;
126
+ ws.pause();
127
+ // The low-water event can fire before the pause took effect; check once more.
128
+ if (dc.bufferedAmount <= LOW_WATER) resume();
129
+ }
130
+ });
131
+ ws.on('unexpected-response', (_req, res) => {
132
+ finish(1006, `the gateway refused the desktop stream (HTTP ${res.statusCode})`);
133
+ res.resume?.();
134
+ });
135
+ ws.on('close', (code, reason) => finish(code || 1006, reason?.toString?.() || ''));
136
+ ws.on('error', (e) => {
137
+ log.warn?.(`[desktop-tunnel] gateway socket error: ${e.message}`);
138
+ finish(1006, '');
139
+ });
140
+
141
+ dc.onmessage = (event) => {
142
+ if (closed) return;
143
+ if (typeof event.data === 'string') return; // the browser sends no control frames yet
144
+ const buf = toBuffer(event.data);
145
+ if (!buf) return;
146
+ if (ws.readyState === WebSocketImpl.OPEN) { ws.send(buf); return; }
147
+ pendingBytes += buf.length;
148
+ if (pendingBytes > MAX_PENDING_BYTES) { finish(1009, 'tunnel buffer full'); return; }
149
+ pending.push(Buffer.from(buf)); // copy: the channel may reuse its buffer
150
+ };
151
+ dc.onclose = () => finish(1000, '', { fromChannel: true });
152
+
153
+ return { close: () => finish(1000, 'tunnel closed'), get closed() { return closed; } };
154
+ }
@@ -0,0 +1,133 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { WebSocketServer } from 'ws';
4
+ import { parseTunnelLabel, gatewaySocketUrl, openDesktopTunnel, CHUNK, HIGH_WATER } from './desktop-tunnel.js';
5
+
6
+ test('parseTunnelLabel allows only the desktop stream paths with a token', () => {
7
+ assert.equal(parseTunnelLabel('tunnel:/desktop/observe?token=abc'), '/desktop/observe?token=abc');
8
+ assert.equal(parseTunnelLabel('tunnel:/desktop/audio?token=a%2Fb'), '/desktop/audio?token=a%2Fb');
9
+ for (const bad of [
10
+ 'clawchats', 'tunnel:', 'tunnel:/desktop/observe', 'tunnel:/desktop/observe?token=',
11
+ 'tunnel:/desktop/observe?token=a&x=1', 'tunnel:/ws?token=a', 'tunnel://evil.com/desktop/observe?token=a',
12
+ 'tunnel:http://127.0.0.1/desktop/observe?token=a', 'tunnel:/desktop/observe/../../x?token=a',
13
+ 'tunnel:/desktop/observe?token=a#x', `tunnel:/desktop/observe?token=${'a'.repeat(3000)}`, null,
14
+ ]) assert.equal(parseTunnelLabel(bad), null, String(bad).slice(0, 60));
15
+ });
16
+
17
+ test('gatewaySocketUrl keeps the gateway host and replaces the path', () => {
18
+ assert.equal(gatewaySocketUrl('ws://localhost:18789', '/desktop/observe?token=t'), 'ws://localhost:18789/desktop/observe?token=t');
19
+ assert.equal(gatewaySocketUrl('ws://127.0.0.1:1/some/path', '/desktop/audio?token=t'), 'ws://127.0.0.1:1/desktop/audio?token=t');
20
+ });
21
+
22
+ /** A W3C-style DataChannel double: records sends, exposes bufferedAmount, and can be closed. */
23
+ function fakeChannel(label, { readyState = 'open' } = {}) {
24
+ const dc = {
25
+ label, readyState, binaryType: 'blob', bufferedAmount: 0, bufferedAmountLowThreshold: 0,
26
+ sent: [], closed: false, onmessage: null, onclose: null, onopen: null, onbufferedamountlow: null,
27
+ send(data) { if (this.readyState !== 'open') throw new Error('not open'); this.sent.push(data); },
28
+ close() { if (this.closed) return; this.closed = true; this.readyState = 'closed'; this.onclose?.(); },
29
+ fromBrowser(bytes) { this.onmessage?.({ data: new Uint8Array(bytes).buffer }); },
30
+ binary() { return Buffer.concat(this.sent.filter(d => typeof d !== 'string').map(d => Buffer.from(d))); },
31
+ control() { return this.sent.filter(d => typeof d === 'string').map(d => JSON.parse(d)); },
32
+ };
33
+ return dc;
34
+ }
35
+
36
+ async function gateway(onConnection, { verifyClient } = {}) {
37
+ const wss = new WebSocketServer({ port: 0, host: '127.0.0.1', ...(verifyClient ? { verifyClient } : {}) });
38
+ await new Promise(r => wss.on('listening', r));
39
+ wss.on('connection', onConnection);
40
+ return { wss, url: `ws://127.0.0.1:${wss.address().port}`, close: () => new Promise(r => { for (const c of wss.clients) c.terminate(); wss.close(r); }) };
41
+ }
42
+ const until = async (fn, ms = 2000) => {
43
+ const end = Date.now() + ms;
44
+ while (!fn()) { if (Date.now() > end) throw new Error('timed out'); await new Promise(r => setTimeout(r, 10)); }
45
+ };
46
+
47
+ test('pipes bytes both ways, in chunks toward the browser', async () => {
48
+ let gwSocket, gwPath, received = [];
49
+ const gw = await gateway((ws, req) => {
50
+ gwSocket = ws; gwPath = req.url;
51
+ ws.on('message', d => received.push(Buffer.from(d)));
52
+ ws.send(Buffer.alloc(CHUNK * 2 + 5, 7)); // the gateway speaks first (RFB version)
53
+ });
54
+ const dc = fakeChannel('tunnel:/desktop/observe?token=tok');
55
+ openDesktopTunnel(dc, { gatewayUrl: gw.url, log: { warn() {} } });
56
+ dc.fromBrowser([1, 2, 3]); // queued until the gateway socket opens
57
+ await until(() => dc.binary().length === CHUNK * 2 + 5);
58
+ assert.equal(gwPath, '/desktop/observe?token=tok');
59
+ assert.deepEqual(dc.sent.map(d => d.byteLength ?? d.length), [CHUNK, CHUNK, 5]);
60
+ await until(() => received.length === 1);
61
+ assert.deepEqual([...received[0]], [1, 2, 3]);
62
+ dc.fromBrowser([4]);
63
+ await until(() => received.length === 2);
64
+ gwSocket.close(4000, 'control-taken:Ada');
65
+ await until(() => dc.closed);
66
+ assert.deepEqual(dc.control(), [{ type: 'close', code: 4000, reason: 'control-taken:Ada' }]);
67
+ await gw.close();
68
+ });
69
+
70
+ test('closing the channel closes the gateway socket', async () => {
71
+ let closedCode = null;
72
+ const gw = await gateway(ws => ws.on('close', code => { closedCode = code; }));
73
+ const dc = fakeChannel('tunnel:/desktop/observe?token=t');
74
+ openDesktopTunnel(dc, { gatewayUrl: gw.url });
75
+ await until(() => gw.wss.clients.size === 1);
76
+ dc.close();
77
+ await until(() => closedCode !== null);
78
+ assert.equal(closedCode, 1000);
79
+ await gw.close();
80
+ });
81
+
82
+ test('a refused upgrade reaches the browser as a close reason', async () => {
83
+ const gw = await gateway(() => {}, { verifyClient: (_info, done) => done(false, 401) });
84
+ const dc = fakeChannel('tunnel:/desktop/observe?token=used');
85
+ openDesktopTunnel(dc, { gatewayUrl: gw.url, log: { warn() {} } });
86
+ await until(() => dc.closed);
87
+ assert.deepEqual(dc.control(), [{ type: 'close', code: 1006, reason: 'the gateway refused the desktop stream (HTTP 401)' }]);
88
+ await gw.close();
89
+ });
90
+
91
+ test('a disallowed label closes the channel without connecting', async () => {
92
+ let connections = 0;
93
+ const gw = await gateway(() => { connections++; });
94
+ const dc = fakeChannel('tunnel:/ws?token=x');
95
+ assert.equal(openDesktopTunnel(dc, { gatewayUrl: gw.url }), null);
96
+ assert.equal(dc.closed, true);
97
+ await new Promise(r => setTimeout(r, 100));
98
+ assert.equal(connections, 0);
99
+ await gw.close();
100
+ });
101
+
102
+ test('stops reading the gateway while the channel is backed up, resumes when it drains', async () => {
103
+ let gwSocket;
104
+ const gw = await gateway(ws => { gwSocket = ws; });
105
+ const dc = fakeChannel('tunnel:/desktop/observe?token=t');
106
+ openDesktopTunnel(dc, { gatewayUrl: gw.url });
107
+ await until(() => gwSocket);
108
+ dc.bufferedAmount = HIGH_WATER + 1; // the link is slow: lots still queued
109
+ gwSocket.send(Buffer.alloc(10, 1));
110
+ await until(() => dc.sent.length === 1);
111
+ gwSocket.send(Buffer.alloc(10, 2)); // arrives at the connector but isn't read while paused
112
+ await new Promise(r => setTimeout(r, 150));
113
+ assert.equal(dc.sent.length, 1);
114
+ dc.bufferedAmount = 0;
115
+ dc.onbufferedamountlow();
116
+ await until(() => dc.sent.length === 2);
117
+ dc.close();
118
+ await gw.close();
119
+ });
120
+
121
+ test('a channel that is still connecting waits for open before dialling the gateway', async () => {
122
+ let connections = 0;
123
+ const gw = await gateway(ws => { connections++; ws.send(Buffer.from([9])); });
124
+ const dc = fakeChannel('tunnel:/desktop/observe?token=t', { readyState: 'connecting' });
125
+ openDesktopTunnel(dc, { gatewayUrl: gw.url });
126
+ await new Promise(r => setTimeout(r, 100));
127
+ assert.equal(connections, 0);
128
+ dc.readyState = 'open';
129
+ dc.onopen();
130
+ await until(() => dc.binary().length === 1);
131
+ dc.close();
132
+ await gw.close();
133
+ });
package/server/index.js CHANGED
@@ -8,7 +8,7 @@ import { Database } from './bootstrap/native.js';
8
8
  import { GATEWAY_WS_URL, AUTH_TOKEN, discoverWorkspaceDir } from './config.js';
9
9
  import { DebugLogger } from './debug.js';
10
10
  import { GatewayClient } from './gateway.js';
11
- import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload, handleWorkspaceCreate } from './controllers/filesystem.js';
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
14
  import { handleStatic } from './controllers/static.js';
@@ -192,6 +192,8 @@ export function createApp(config = {}) {
192
192
  if (method === 'DELETE' && urlPath === '/api/workspace/file') return handleWorkspaceFileDelete(req, res, query);
193
193
  if (method === 'POST' && urlPath === '/api/workspace/upload') return await handleWorkspaceUpload(req, res, query);
194
194
  if (method === 'POST' && urlPath === '/api/workspace/create') return handleWorkspaceCreate(req, res, query);
195
+ if (method === 'POST' && urlPath === '/api/workspace/move') return handleWorkspaceMove(req, res, query);
196
+ if (method === 'POST' && urlPath === '/api/workspace/copy') return handleWorkspaceCopy(req, res, query);
195
197
 
196
198
  // Settings & misc
197
199
  if (method === 'GET' && urlPath === '/api/settings') return handleGetSettings(req, res);
@@ -79,7 +79,8 @@ export class SharingManager {
79
79
  // Requester side: shares this side removed. The server can't bring them back by replaying an old grant.
80
80
  g.exec(`CREATE TABLE IF NOT EXISTS peer_removed (share_id TEXT PRIMARY KEY, removed_at INTEGER NOT NULL)`);
81
81
  // Owner side: agents created as guest versions here (their restrictions are checked on every turn).
82
- // Keys the user compared out of band and marked as verified (per share; a different key isn't).
82
+ // Keys the user compared out of band and marked as verified (a row per share where it was marked; the key
83
+ // counts as verified on every share with it, a different key doesn't).
83
84
  g.exec(`CREATE TABLE IF NOT EXISTS peer_key_verified (share_id TEXT PRIMARY KEY, pubkey TEXT NOT NULL, verified_at INTEGER NOT NULL)`);
84
85
  g.exec(`CREATE TABLE IF NOT EXISTS peer_guest_agents (agent_id TEXT PRIMARY KEY, created_at INTEGER NOT NULL)`);
85
86
  this._ready = true;
@@ -140,8 +141,9 @@ export class SharingManager {
140
141
  return this._db().prepare('SELECT owner_pubkey FROM peer_pins WHERE share_id = ?').get(s?.id)?.owner_pubkey || null;
141
142
  }
142
143
 
144
+ /** A key compared once is verified on every share with that gateway (a share's own row records where it was done). */
143
145
  _keyVerified(shareId, pubKey) {
144
- return this._db().prepare('SELECT pubkey FROM peer_key_verified WHERE share_id = ?').get(shareId)?.pubkey === pubKey;
146
+ return !!this._db().prepare('SELECT 1 FROM peer_key_verified WHERE pubkey = ? LIMIT 1').get(pubKey);
145
147
  }
146
148
 
147
149
  /** The user compared "Their key" with the other person (by phone, chat…) and it matched. */
@@ -280,6 +282,15 @@ export class SharingManager {
280
282
  this._db().prepare('INSERT INTO peer_usage (share_id, day, turns) VALUES (?, ?, 1) ON CONFLICT(share_id, day) DO UPDATE SET turns = turns + 1').run(shareId, today());
281
283
  this._changed(); // the owner's Sharing view shows today's count
282
284
  try { return await this._runGuestTurn(shareId, grant, agent, roomId, p, emit, turnKey); }
285
+ catch (e) {
286
+ // A reply that failed without saying anything doesn't use up the day's replies. A timeout still
287
+ // counts: it ran for the full turn timeout.
288
+ if (!e.partial && !/timed out/i.test(e.message)) {
289
+ this._db().prepare('UPDATE peer_usage SET turns = MAX(0, turns - 1) WHERE share_id = ? AND day = ?').run(shareId, today());
290
+ this._changed();
291
+ }
292
+ throw e;
293
+ }
283
294
  finally { this._turns.delete(turnKey); }
284
295
  }
285
296
 
@@ -333,6 +333,25 @@ test('keys start unverified; marking verified holds only for that exact key', ()
333
333
  assert.equal(r().theirKey, s.kOwner.fingerprint);
334
334
  });
335
335
 
336
+ test('a key verified on one share is verified on every share with that gateway, both directions', () => {
337
+ const s = setup();
338
+ s.owner.approve('sh1', { agents: [{ id: 'jarvis', name: 'Jarvis' }] });
339
+ const active = s.activate();
340
+ s.req.remoteAgents(); // pins Kamil's key
341
+ // Kamil also asks Houman: on Houman's gateway, sh2 is an owner share whose requester key is Kamil's.
342
+ const back = { id: 'sh2', status: 'pending', as: 'owner', requester: s.people.owner, owner: s.people.requester, agents: [] };
343
+ s.req.setShares([{ ...active, as: 'requester' }, back]);
344
+ const byId = id => s.req.list().find(x => x.id === id);
345
+ assert.equal(byId('sh2').theirKey, byId('sh1').theirKey);
346
+ assert.equal(byId('sh2').keyVerified, false);
347
+ s.req.markKeyVerified('sh1');
348
+ assert.equal(byId('sh1').keyVerified, true);
349
+ assert.equal(byId('sh2').keyVerified, true);
350
+ // Someone else's key stays unverified.
351
+ s.req.setShares([{ ...active, as: 'requester' }, { ...back, id: 'sh3', requester: { ...s.people.owner, pubKey: keyIn().publicKey } }]);
352
+ assert.equal(byId('sh3').keyVerified, false);
353
+ });
354
+
336
355
  test('a pending request can be verified, and stays verified once approved', () => {
337
356
  const s = setup();
338
357
  const o = () => s.owner.list()[0];
@@ -342,3 +361,22 @@ test('a pending request can be verified, and stays verified once approved', () =
342
361
  s.owner.approve('sh1', { agents: [{ id: 'jarvis', name: 'Jarvis' }] });
343
362
  assert.equal(o().keyVerified, true);
344
363
  });
364
+
365
+ test('a reply that fails without text is not counted against the daily cap; one with partial text is', async () => {
366
+ const s = setup({ reply: () => null }); // runs stay open; the test ends them
367
+ s.owner.approve('sh1', { agents: [{ id: 'jarvis', name: 'Jarvis' }], dailyCap: 5 });
368
+ s.activate();
369
+ // A guest version whose restrictions were removed: refused before it runs, nothing said.
370
+ s.owner.markGuestAgent('jarvis');
371
+ await assert.rejects(s.req.turn('peer:sh1:jarvis', { turnId: 'f1', roomId: 'room1', message: 'hi' }), e => e.code === 'unavailable');
372
+ assert.equal(s.owner.list()[0].usedToday, 0);
373
+ s.owner._db().prepare('DELETE FROM peer_guest_agents').run();
374
+ // Fails after streaming some text ("jar"): counted.
375
+ const p = s.req.turn('peer:sh1:jarvis', { turnId: 'f2', roomId: 'room1', message: 'hi' });
376
+ for (let i = 0; i < 50 && !s.ownerCalls.some(c => c.method === 'chat.send'); i++) await wait(2);
377
+ await wait(5);
378
+ const runId = s.ownerCalls.filter(c => c.method === 'chat.send').at(-1).params.idempotencyKey;
379
+ s.owner.onGatewayEvent({ event: 'chat', payload: { runId, state: 'error', errorMessage: 'provider down' } });
380
+ await assert.rejects(p);
381
+ assert.equal(s.owner.list()[0].usedToday, 1);
382
+ });
package/server/team.js CHANGED
@@ -16,6 +16,8 @@
16
16
  import crypto from 'node:crypto';
17
17
 
18
18
  export const SILENT = /^\s*NO_REPLY\s*$/i;
19
+ /** Room entry for an agent asked directly whose run was stopped before it said anything. */
20
+ const STOPPED_EARLY = '*[stopped before replying]*';
19
21
  const LABEL_PREFIX = /^\[([^\]\n]{1,100})\]\n\n/;
20
22
  const TURN_TIMEOUT_MS = 15 * 60_000;
21
23
  const HISTORY_LIMIT = 80;
@@ -401,7 +403,7 @@ export class TeamCoordinator {
401
403
  if (result.state === 'aborted') {
402
404
  if (partial && !SILENT.test(partial)) await this._post(roomKey, self, `${partial}\n\n*[stopped]*`);
403
405
  // Some runtimes (claude-cli) finish the reply after the abort was acknowledged: post it once it lands.
404
- else this._recoverAfterAbort(roomKey, self, workKey);
406
+ else this._recoverAfterAbort(roomKey, self, workKey, { marker: mode === 'addressed' });
405
407
  return { agentId, text: null };
406
408
  }
407
409
  throw Object.assign(new Error(result.errorMessage || 'run failed'), { partial });
@@ -440,6 +442,7 @@ export class TeamCoordinator {
440
442
  const text = String(result?.text || '').trim();
441
443
  if (result?.state === 'aborted') {
442
444
  if (text && !SILENT.test(text)) await this._post(roomKey, self, `${text}\n\n*[stopped]*`);
445
+ else if (mode === 'addressed') await this._post(roomKey, self, STOPPED_EARLY);
443
446
  return { agentId, text: null };
444
447
  }
445
448
  if (!text || SILENT.test(text)) return { agentId, text: null };
@@ -455,9 +458,10 @@ export class TeamCoordinator {
455
458
  /**
456
459
  * After Stop, a reply that still landed in the agent's session (the runtime ignored the abort) is
457
460
  * posted to the room marked stopped, so the room matches what the agent believes it said.
458
- * Polls until the session is idle (max ~3 min); gives up if the agent starts another turn.
461
+ * Polls until the session is idle (max ~3 min); gives up if the agent starts another turn. With
462
+ * `marker` (the agent was asked directly), nothing landing still leaves "stopped before replying".
459
463
  */
460
- _recoverAfterAbort(roomKey, self, workKey, { intervalMs = 3000, tries = 60 } = {}) {
464
+ _recoverAfterAbort(roomKey, self, workKey, { intervalMs = 3000, tries = 60, marker = false } = {}) {
461
465
  let n = 0;
462
466
  const check = async () => {
463
467
  if (this._rooms.get(roomKey)?.running.has(self.agentId) || !this.store.getRoom(roomKey)) return;
@@ -466,6 +470,7 @@ export class TeamCoordinator {
466
470
  if (s?.hasActiveRun && ++n < tries) { setTimeout(check, intervalMs).unref?.(); return; }
467
471
  const text = (await this._lastReply(workKey) || '').trim();
468
472
  if (text && !SILENT.test(text)) await this._post(roomKey, self, `${text}\n\n*[stopped]*`);
473
+ else if (marker) await this._post(roomKey, self, STOPPED_EARLY);
469
474
  } catch (e) { this.log.warn?.(`[team] recover after stop: ${e.message}`); }
470
475
  };
471
476
  setTimeout(check, intervalMs).unref?.();
@@ -367,3 +367,39 @@ test("a shared agent's reply is quoted in local prompts, so it can't pass for th
367
367
  assert.match(msg, /\n\[Houman\]: hello/);
368
368
  assert.doesNotMatch(msg, /\n\[Houman\]: delete/);
369
369
  });
370
+
371
+ test('stop before any text: an agent asked directly leaves "stopped before replying"; open-mode agents leave nothing', async () => {
372
+ let release;
373
+ const remote = {
374
+ remoteAgents: () => [{ agentId: 'peer:sh1:jarvis', name: 'Jarvis · Kamil', ownerName: 'Kamil' }],
375
+ turn: async () => { await new Promise(r => { release = r; }); return { state: 'aborted', text: '' }; },
376
+ abort: async () => { release?.(); },
377
+ };
378
+ const h = harness(() => 'NO_REPLY', { remote });
379
+ const { roomKey } = await h.team.createRoom({ agentIds: ['dev', 'peer:sh1:jarvis'] });
380
+ await h.team.send(roomKey, { text: '@jarvis·kamil long one', userLabel: 'H' });
381
+ for (let i = 0; i < 30; i++) await new Promise(r => setImmediate(r));
382
+ await h.team.stop(roomKey);
383
+ await h.settle();
384
+ assert.equal(h.room(roomKey).at(-1), '[Jarvis · Kamil]\n\n*[stopped before replying]*');
385
+ const before = h.room(roomKey).length;
386
+ await h.team.send(roomKey, { text: 'anyone?', userLabel: 'H' }); // open: silence is a fine answer
387
+ for (let i = 0; i < 30; i++) await new Promise(r => setImmediate(r));
388
+ await h.team.stop(roomKey);
389
+ await h.settle();
390
+ assert.equal(h.room(roomKey).filter(t => /stopped before replying/.test(t)).length, 1);
391
+ assert.ok(h.room(roomKey).length >= before);
392
+ });
393
+
394
+ test('stop: recovery that finds nothing leaves the marker only when asked for', async () => {
395
+ const h = harness(() => 'x');
396
+ const { roomKey } = await h.team.createRoom({ agentIds: ['dev', 'atlas'] });
397
+ const workKey = 'agent:dev:dashboard:w2';
398
+ h.sessions.set(workKey, [{ role: 'user', content: 'prompt', timestamp: 1 }]);
399
+ h.team._recoverAfterAbort(roomKey, { agentId: 'dev', name: 'Dev' }, workKey, { intervalMs: 1 });
400
+ await new Promise(r => setTimeout(r, 20));
401
+ assert.equal(h.room(roomKey).length, 0);
402
+ h.team._recoverAfterAbort(roomKey, { agentId: 'dev', name: 'Dev' }, workKey, { intervalMs: 1, marker: true });
403
+ await new Promise(r => setTimeout(r, 20));
404
+ assert.deepEqual(h.room(roomKey), ['[Dev]\n\n*[stopped before replying]*']);
405
+ });