@clawchatsai/connector 0.1.46 → 0.1.48

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.46",
3
+ "version": "0.1.48",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,56 @@
1
+ import http from 'node:http';
2
+ import { parseBody, send, sendError } from '../util/http.js';
3
+
4
+ // Realtime Talk over WebRTC: `talk.client.create` returns an `offerUrl` that is a route of the
5
+ // gateway's own HTTP server (e.g. /plugins/openai/realtime/calls). The Control UI reaches it
6
+ // directly; a ClawChats browser has no HTTP path to the gateway, only this DataChannel. The
7
+ // browser posts its SDP offer here and the connector relays it to that gateway route.
8
+ //
9
+ // The only credential involved is the one-time `clientSecret` the gateway minted for this call
10
+ // (sent as the route's Bearer token, as the Control UI does); the gateway token never leaves
11
+ // the connector. Only the gateway's realtime broker routes are reachable.
12
+ const OFFER_PATH = /^\/plugins\/[a-z0-9][a-z0-9-]*\/realtime\/calls$/;
13
+ const MAX_SDP_BYTES = 64 * 1024;
14
+ const MAX_ANSWER_BYTES = 256 * 1024;
15
+ const OFFER_TIMEOUT_MS = 30_000;
16
+ // Provider-specific extras (`offerHeaders`) may only be X-* headers.
17
+ const EXTRA_HEADER = /^x-[a-z0-9-]+$/i;
18
+
19
+ export function isAllowedOfferPath(p) {
20
+ return typeof p === 'string' && OFFER_PATH.test(p);
21
+ }
22
+
23
+ export function createTalkOfferHandler({ gatewayWsUrl }) {
24
+ const base = new URL(gatewayWsUrl.replace(/^ws/, 'http'));
25
+ return async function handleTalkOffer(req, res) {
26
+ let body;
27
+ try { body = await parseBody(req); } catch { return sendError(res, 400, 'Invalid JSON'); }
28
+ const { path, sdp, clientSecret, headers } = body || {};
29
+ if (!isAllowedOfferPath(path)) return sendError(res, 400, 'Unsupported realtime offer route');
30
+ if (typeof sdp !== 'string' || !sdp.trim() || Buffer.byteLength(sdp) > MAX_SDP_BYTES) return sendError(res, 400, 'SDP offer required');
31
+ if (typeof clientSecret !== 'string' || !clientSecret || /\s/.test(clientSecret)) return sendError(res, 400, 'Realtime session token required');
32
+ const outHeaders = { 'Content-Type': 'application/sdp', 'Content-Length': String(Buffer.byteLength(sdp)), Authorization: `Bearer ${clientSecret}` };
33
+ if (headers && typeof headers === 'object') {
34
+ for (const [k, v] of Object.entries(headers)) if (EXTRA_HEADER.test(k) && typeof v === 'string') outHeaders[k] = v;
35
+ }
36
+ await new Promise(resolve => {
37
+ const upstream = http.request({ hostname: base.hostname, port: base.port, path, method: 'POST', headers: outHeaders, timeout: OFFER_TIMEOUT_MS }, up => {
38
+ const chunks = [];
39
+ let size = 0;
40
+ up.on('data', c => {
41
+ size += c.length;
42
+ if (size > MAX_ANSWER_BYTES) { up.destroy(); if (!res.headersSent) sendError(res, 502, 'Realtime answer too large'); return resolve(); }
43
+ chunks.push(c);
44
+ });
45
+ up.on('end', () => {
46
+ if (!res.headersSent) send(res, 200, { status: up.statusCode || 502, body: Buffer.concat(chunks).toString('utf8') });
47
+ resolve();
48
+ });
49
+ up.on('error', e => { if (!res.headersSent) sendError(res, 502, `Gateway realtime offer: ${e.message}`); resolve(); });
50
+ });
51
+ upstream.on('timeout', () => upstream.destroy(new Error('timed out')));
52
+ upstream.on('error', e => { if (!res.headersSent) sendError(res, 502, `Gateway realtime offer: ${e.message}`); resolve(); });
53
+ upstream.end(sdp);
54
+ });
55
+ };
56
+ }
@@ -0,0 +1,48 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import http from 'node:http';
4
+ import { Readable } from 'node:stream';
5
+ import { isAllowedOfferPath, createTalkOfferHandler } from './talk-offer.js';
6
+
7
+ test('only realtime broker routes are reachable', () => {
8
+ assert.equal(isAllowedOfferPath('/plugins/openai/realtime/calls'), true);
9
+ assert.equal(isAllowedOfferPath('/plugins/google/realtime/calls'), true);
10
+ for (const p of ['/api/sessions', '/plugins/openai/realtime/calls/x', '/plugins/../realtime/calls', '/plugins/a/b/realtime/calls', 'http://evil/plugins/openai/realtime/calls', '', null]) {
11
+ assert.equal(isAllowedOfferPath(p), false, String(p));
12
+ }
13
+ });
14
+
15
+ function fakeRes() {
16
+ const r = { status: 0, body: '', headersSent: false, writeHead(s) { r.status = s; r.headersSent = true; }, end(b) { r.body = b; r.done?.(); } };
17
+ return r;
18
+ }
19
+ const fakeReq = body => Object.assign(Readable.from([Buffer.from(JSON.stringify(body))]), { headers: {} });
20
+
21
+ test('relays the SDP offer with the call token as Bearer, never the gateway token', async () => {
22
+ let seen;
23
+ const gw = http.createServer((req, res) => {
24
+ const chunks = [];
25
+ req.on('data', c => chunks.push(c));
26
+ req.on('end', () => { seen = { url: req.url, auth: req.headers.authorization, type: req.headers['content-type'], x: req.headers['x-extra'], cookie: req.headers.cookie, body: Buffer.concat(chunks).toString() }; res.writeHead(201); res.end('v=0 answer'); });
27
+ });
28
+ await new Promise(r => gw.listen(0, '127.0.0.1', r));
29
+ try {
30
+ const handler = createTalkOfferHandler({ gatewayWsUrl: `ws://127.0.0.1:${gw.address().port}` });
31
+ const res = fakeRes();
32
+ const done = new Promise(r => { res.done = r; });
33
+ await handler(fakeReq({ path: '/plugins/openai/realtime/calls', sdp: 'v=0 offer', clientSecret: 'tok123', headers: { 'X-Extra': '1', Cookie: 'no' } }), res);
34
+ await done;
35
+ assert.equal(res.status, 200);
36
+ assert.deepEqual(JSON.parse(res.body), { status: 201, body: 'v=0 answer' });
37
+ assert.deepEqual(seen, { url: '/plugins/openai/realtime/calls', auth: 'Bearer tok123', type: 'application/sdp', x: '1', cookie: undefined, body: 'v=0 offer' });
38
+ } finally { gw.close(); }
39
+ });
40
+
41
+ test('rejects other routes and missing fields without calling the gateway', async () => {
42
+ const handler = createTalkOfferHandler({ gatewayWsUrl: 'ws://127.0.0.1:1' });
43
+ for (const body of [{ path: '/api/sessions', sdp: 'x', clientSecret: 't' }, { path: '/plugins/openai/realtime/calls', clientSecret: 't' }, { path: '/plugins/openai/realtime/calls', sdp: 'x' }]) {
44
+ const res = fakeRes();
45
+ await handler(fakeReq(body), res);
46
+ assert.equal(res.status, 400);
47
+ }
48
+ });
@@ -54,7 +54,17 @@ export function createTitleHandler(llm) {
54
54
  if (typeof llm?.complete !== 'function') return sendError(res, 501, 'This OpenClaw version has no plugin model completion');
55
55
  const { message } = await parseBody(req);
56
56
  if (typeof message !== 'string' || !message.trim()) return sendError(res, 400, 'Missing message');
57
- const result = await complete(Array.from(message.trim()).slice(0, SOURCE_MAX_CHARS).join(''));
58
- return send(res, 200, { title: normalizeTitle(result?.text), model: result?.provider && result?.model ? `${result.provider}/${result.model}` : undefined });
57
+ const source = Array.from(message.trim()).slice(0, SOURCE_MAX_CHARS).join('');
58
+ let result;
59
+ try {
60
+ result = await complete(source);
61
+ } catch (err) {
62
+ console.warn(`[title] completion failed (source ${source.length} chars): ${err?.code || ''} ${err?.stack || err}`);
63
+ throw err;
64
+ }
65
+ const title = normalizeTitle(result?.text);
66
+ // The reason a retitle comes back empty: which model answered, and what it actually returned.
67
+ if (!title) console.warn(`[title] no title from ${result?.provider}/${result?.model} (source ${source.length} chars, reply ${JSON.stringify(String(result?.text ?? '').slice(0, 200))}, keys ${Object.keys(result || {}).join(',')})`);
68
+ return send(res, 200, { title, model: result?.provider && result?.model ? `${result.provider}/${result.model}` : undefined });
59
69
  };
60
70
  }
package/server/index.js CHANGED
@@ -11,6 +11,7 @@ import { GatewayClient } from './gateway.js';
11
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
+ import { createTalkOfferHandler } from './controllers/talk-offer.js';
14
15
  import { handleStatic } from './controllers/static.js';
15
16
  import { handleAgents } from './controllers/agents.js';
16
17
  import { createTitleHandler, createTitler } from './controllers/title.js';
@@ -117,6 +118,7 @@ export function createApp(config = {}) {
117
118
 
118
119
  // Instantiate controllers
119
120
  const handleGatewayMedia = createGatewayMediaHandler({ gatewayWsUrl: gatewayUrl, gatewayToken });
121
+ const handleTalkOffer = createTalkOfferHandler({ gatewayWsUrl: gatewayUrl });
120
122
 
121
123
  // Settings — file I/O lives in settings.js
122
124
  const { handleGetSettings, handleSaveSettings } = createSettingsHandlers(SETTINGS_FILE);
@@ -195,6 +197,7 @@ export function createApp(config = {}) {
195
197
  // File serving & workspace browser
196
198
  if (method === 'GET' && urlPath === '/api/file') return handleServeFile(req, res, query, workspaceDir);
197
199
  if (method === 'GET' && urlPath === '/api/gw-media') return handleGatewayMedia(req, res, query);
200
+ if (method === 'POST' && urlPath === '/api/talk/offer') return await handleTalkOffer(req, res);
198
201
  if (method === 'GET' && urlPath === '/api/workspace') return handleWorkspaceList(req, res, query);
199
202
  if (method === 'GET' && urlPath === '/api/workspace/file') return handleWorkspaceFileRead(req, res, query, workspaceDir, query.session ? await sessionRoot(query.session) : null);
200
203
  if (method === 'PUT' && urlPath === '/api/workspace/file') return await handleWorkspaceFileWrite(req, res, query);