@clawchatsai/connector 0.1.15 → 0.1.17
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/README.md +1 -2
- package/dist/index.js +68 -133
- package/dist/plugin-config.d.ts +29 -0
- package/dist/plugin-config.js +1 -0
- package/package.json +2 -1
- package/server/bootstrap/native.js +0 -4
- package/server/config.js +9 -18
- package/server/controllers/filesystem.js +2 -2
- package/server/controllers/gateway-media.js +42 -0
- package/server/controllers/gateway-media.test.js +23 -0
- package/server/debug.js +14 -1
- package/server/debug.test.js +22 -0
- package/server/gateway.js +58 -535
- package/server/gateway.test.js +27 -0
- package/server/history-export.js +212 -0
- package/server/index.js +36 -200
- package/server/session-lens.js +280 -0
- package/server/session-lens.test.js +168 -0
- package/server/store/extras-store.js +77 -0
- package/server/util/helpers.js +2 -105
- package/dist/gateway-bridge.d.ts +0 -73
- package/dist/gateway-bridge.js +0 -255
- package/server/controllers/files.js +0 -64
- package/server/controllers/memory.js +0 -86
- package/server/controllers/messages.js +0 -126
- package/server/controllers/threads.js +0 -107
- package/server/controllers/workspaces.js +0 -103
- package/server/gateway-cleanup.js +0 -31
- package/server/providers/memory-config.js +0 -52
- package/server/providers/memory.js +0 -108
- package/server/util/context.js +0 -49
package/README.md
CHANGED
|
@@ -50,8 +50,7 @@ server/ # Local backend server (Node.js, plain ESM)
|
|
|
50
50
|
bootstrap/
|
|
51
51
|
native.js # node:sqlite initialisation (built into Node ≥22.5, no compilation)
|
|
52
52
|
identity.js # ed25519 device signing for OpenClaw ≥2.15
|
|
53
|
-
controllers/ # HTTP route handlers (
|
|
54
|
-
providers/ # Memory backends (Qdrant, Postgres)
|
|
53
|
+
controllers/ # HTTP route handlers (files, workspace, gateway media, transcribe, settings)
|
|
55
54
|
util/ # HTTP helpers, multipart parser, context builder, misc
|
|
56
55
|
src/ # OpenClaw plugin wrapper (TypeScript)
|
|
57
56
|
index.ts # Plugin entry point — registers with OpenClaw, manages lifecycle
|
package/dist/index.js
CHANGED
|
@@ -29,8 +29,14 @@ export const PLUGIN_ID = 'connector';
|
|
|
29
29
|
// Read version from package.json so it stays in sync with npm publishes
|
|
30
30
|
const _pkgJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf-8'));
|
|
31
31
|
export const PLUGIN_VERSION = _pkgJson.version;
|
|
32
|
-
/**
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Largest string sent as one DataChannel message. The SCTP limit (~256 KiB) is in bytes;
|
|
34
|
+
* string length is in UTF-16 units, so non-ASCII text and JSON escaping can push a
|
|
35
|
+
* "256K" string over it (libdatachannel then drops it: "Message size exceeds limit").
|
|
36
|
+
* 64K units stays under the limit even at 3 bytes per unit; matches the browser side.
|
|
37
|
+
*/
|
|
38
|
+
const MAX_DC_MESSAGE_SIZE = 64 * 1024;
|
|
39
|
+
const DC_CHUNK_SIZE = MAX_DC_MESSAGE_SIZE - 512; // room for the chunk envelope
|
|
34
40
|
/** Active DataChannel connections: connectionId → send function */
|
|
35
41
|
const connectedClients = new Map();
|
|
36
42
|
/** Reassembly buffers for chunked RPC requests from browser (large uploads). */
|
|
@@ -42,10 +48,6 @@ let signaling = null;
|
|
|
42
48
|
let webrtcPeer = null;
|
|
43
49
|
let healthServer = null;
|
|
44
50
|
let _stopRequested = false;
|
|
45
|
-
/** Model IDs that have 'input' explicitly set without 'image' support. */
|
|
46
|
-
let _imageRestrictedModels = [];
|
|
47
|
-
/** True if session.reset config risks wiping ClawChats history (daily reset or short idle). */
|
|
48
|
-
let _sessionResetWarning = false;
|
|
49
51
|
// ---------------------------------------------------------------------------
|
|
50
52
|
// Config helpers
|
|
51
53
|
// ---------------------------------------------------------------------------
|
|
@@ -168,40 +170,8 @@ async function ensureNativeModules(ctx) {
|
|
|
168
170
|
` cp ${prebuiltPath} ${targetPath}`);
|
|
169
171
|
}
|
|
170
172
|
}
|
|
171
|
-
const CLAWCHATS_MD_CONTENT = `# ClawChats — Inline File Delivery
|
|
172
|
-
|
|
173
|
-
To deliver a file inline in this chat, run an exec that outputs a MEDIA: line:
|
|
174
|
-
|
|
175
|
-
MEDIA:/absolute/path/to/file
|
|
176
|
-
|
|
177
|
-
**After creating any file** (via Write tool or exec), deliver it by running:
|
|
178
|
-
|
|
179
|
-
exec echo "MEDIA:/absolute/path/to/file"
|
|
180
|
-
|
|
181
|
-
For files created during an exec command, you can inline it at the end of the same command:
|
|
182
|
-
|
|
183
|
-
exec python3 generate.py --output /tmp/result.png && echo "MEDIA:/tmp/result.png"
|
|
184
|
-
|
|
185
|
-
Notes:
|
|
186
|
-
- Works for images (png, jpg, gif, webp, svg, etc.), documents, markdown, code files, and more
|
|
187
|
-
- Output each path once — duplicates are ignored automatically
|
|
188
|
-
`;
|
|
189
173
|
async function startClawChats(ctx, api) {
|
|
190
174
|
_stopRequested = false;
|
|
191
|
-
// Bootstrap CLAWCHATS.md in the agent workspace so the agent always knows the MEDIA: protocol.
|
|
192
|
-
// Written once on plugin start; never overwrites an existing file (user may have customised it).
|
|
193
|
-
try {
|
|
194
|
-
const workspaceDir = path.join(ctx.stateDir, 'workspace');
|
|
195
|
-
const clawchatsDoc = path.join(workspaceDir, 'CLAWCHATS.md');
|
|
196
|
-
if (!fs.existsSync(clawchatsDoc)) {
|
|
197
|
-
fs.mkdirSync(workspaceDir, { recursive: true });
|
|
198
|
-
fs.writeFileSync(clawchatsDoc, CLAWCHATS_MD_CONTENT, { encoding: 'utf8' });
|
|
199
|
-
ctx.logger.info('[clawchats] wrote CLAWCHATS.md to workspace');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
catch (e) {
|
|
203
|
-
ctx.logger.warn(`[clawchats] could not write CLAWCHATS.md: ${e.message}`);
|
|
204
|
-
}
|
|
205
175
|
let config = loadConfig();
|
|
206
176
|
if (!config) {
|
|
207
177
|
ctx.logger.info('ClawChats not configured. Waiting for setup...');
|
|
@@ -222,64 +192,15 @@ async function startClawChats(ctx, api) {
|
|
|
222
192
|
ctx.logger.error('No gateway token available. Re-run: openclaw clawchats setup <token>');
|
|
223
193
|
return;
|
|
224
194
|
}
|
|
225
|
-
// Check session.reset config — warn if daily reset or short idle could wipe ClawChats history.
|
|
226
|
-
_sessionResetWarning = false;
|
|
227
|
-
try {
|
|
228
|
-
const sessionReset = gwCfg?.['session']?.['reset'];
|
|
229
|
-
if (sessionReset) {
|
|
230
|
-
const mode = sessionReset['mode'];
|
|
231
|
-
const idleMinutes = sessionReset['idleMinutes'];
|
|
232
|
-
if (mode === 'daily' || (mode === 'idle' && typeof idleMinutes === 'number' && idleMinutes < 43200)) {
|
|
233
|
-
_sessionResetWarning = true;
|
|
234
|
-
ctx.logger.warn(`[clawchats] session.reset may wipe chat history (mode=${mode}, idleMinutes=${idleMinutes ?? 'unset'}) — set mode=idle + idleMinutes=999999`);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
catch {
|
|
239
|
-
// Non-fatal
|
|
240
|
-
}
|
|
241
|
-
// Check for model definitions with 'input' set but missing 'image' — they silently drop attachments.
|
|
242
|
-
_imageRestrictedModels = [];
|
|
243
|
-
try {
|
|
244
|
-
const providers = gwCfg?.['models']?.['providers'] ?? {};
|
|
245
|
-
for (const provider of Object.values(providers)) {
|
|
246
|
-
if (Array.isArray(provider.models)) {
|
|
247
|
-
for (const m of provider.models) {
|
|
248
|
-
if (Array.isArray(m.input) && !m.input.includes('image')) {
|
|
249
|
-
_imageRestrictedModels.push(m.id ?? '(unknown)');
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
if (_imageRestrictedModels.length > 0) {
|
|
255
|
-
ctx.logger.warn(`[clawchats] image-restricted models detected (missing "image" input): ${_imageRestrictedModels.join(', ')}`);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
catch {
|
|
259
|
-
// Non-fatal: config parse issue, just skip the check
|
|
260
|
-
}
|
|
261
195
|
// 3. Ensure native modules are built (OpenClaw installs with --ignore-scripts)
|
|
262
196
|
await ensureNativeModules(ctx);
|
|
263
197
|
// 4. Import server.js and create app instance with plugin paths
|
|
264
198
|
const dataDir = path.join(ctx.stateDir, 'clawchats', 'data');
|
|
265
|
-
const uploadsDir = path.join(ctx.stateDir, 'clawchats', 'uploads');
|
|
266
199
|
// Dynamic import of server.js (plain JS, no type declarations)
|
|
267
200
|
// @ts-expect-error — server/index.js is plain JS with no .d.ts
|
|
268
201
|
const serverModule = await import('../server/index.js');
|
|
269
|
-
// Read env vars here (plugin host) so server/ bundle stays process.env-free.
|
|
270
|
-
const memoryEnv = {
|
|
271
|
-
provider: process.env.MEMORY_PROVIDER,
|
|
272
|
-
host: process.env.MEMORY_HOST || process.env.QDRANT_HOST,
|
|
273
|
-
port: process.env.MEMORY_PORT || process.env.QDRANT_PORT,
|
|
274
|
-
collection: process.env.MEMORY_COLLECTION || process.env.QDRANT_COLLECTION,
|
|
275
|
-
pgUrl: process.env.MEMORY_PG_URL,
|
|
276
|
-
qdrantUrl: process.env.QDRANT_URL,
|
|
277
|
-
};
|
|
278
|
-
// Filter out undefined values so discoverMemoryConfig only overrides what's set.
|
|
279
|
-
const memoryEnvFiltered = Object.fromEntries(Object.entries(memoryEnv).filter(([, v]) => v !== undefined && v !== ''));
|
|
280
202
|
app = serverModule.createApp({
|
|
281
203
|
dataDir,
|
|
282
|
-
uploadsDir,
|
|
283
204
|
port: parseInt(process.env.PORT || '3001', 10),
|
|
284
205
|
gatewayUrl: process.env.GATEWAY_WS_URL || 'ws://localhost:18789',
|
|
285
206
|
authToken: process.env.CLAWCHATS_AUTH_TOKEN || '', // P2P: DataChannel is the auth boundary
|
|
@@ -295,15 +216,19 @@ async function startClawChats(ctx, api) {
|
|
|
295
216
|
catch { /* ok */ }
|
|
296
217
|
return process.env.OPENAI_API_KEY || null;
|
|
297
218
|
})(),
|
|
298
|
-
memoryEnv: memoryEnvFiltered,
|
|
299
219
|
});
|
|
300
220
|
// 4. Connect createApp's gateway client (handles persistence + event relay)
|
|
301
221
|
app.gatewayClient.connect();
|
|
302
|
-
// Wire DataChannel clients as broadcast targets so they receive gateway events
|
|
222
|
+
// Wire DataChannel clients as broadcast targets so they receive gateway events.
|
|
223
|
+
// Frames over the DataChannel limit (e.g. a complete chat.history snapshot) go as chunks.
|
|
303
224
|
app.gatewayClient.addBroadcastTarget((data) => {
|
|
225
|
+
const frame = JSON.stringify({ type: 'gateway-event', payload: data });
|
|
304
226
|
for (const [id, client] of connectedClients) {
|
|
305
227
|
try {
|
|
306
|
-
|
|
228
|
+
if (frame.length <= MAX_DC_MESSAGE_SIZE)
|
|
229
|
+
client.send(frame);
|
|
230
|
+
else
|
|
231
|
+
sendGatewayEventChunked(client, data);
|
|
307
232
|
}
|
|
308
233
|
catch {
|
|
309
234
|
connectedClients.delete(id);
|
|
@@ -482,20 +407,6 @@ function setupDataChannelHandler(dc, connectionId, ctx) {
|
|
|
482
407
|
// Auth succeeded — add to broadcast clients and inform gateway
|
|
483
408
|
connectedClients.set(connectionId, dc);
|
|
484
409
|
ctx.logger.info(`Browser authenticated: ${connectionId}`);
|
|
485
|
-
// Warn if any models have image support disabled in openclaw.json
|
|
486
|
-
if (_imageRestrictedModels.length > 0) {
|
|
487
|
-
dc.send(JSON.stringify({
|
|
488
|
-
type: 'gateway-event',
|
|
489
|
-
payload: JSON.stringify({ type: 'clawchats', event: 'image-capability-warning', models: _imageRestrictedModels }),
|
|
490
|
-
}));
|
|
491
|
-
}
|
|
492
|
-
// Warn if session.reset config risks wiping ClawChats history
|
|
493
|
-
if (_sessionResetWarning) {
|
|
494
|
-
dc.send(JSON.stringify({
|
|
495
|
-
type: 'gateway-event',
|
|
496
|
-
payload: JSON.stringify({ type: 'clawchats', event: 'session-reset-warning' }),
|
|
497
|
-
}));
|
|
498
|
-
}
|
|
499
410
|
// Persist backup code changes if any were consumed
|
|
500
411
|
if (authConfig.backupCodeHashes && config.backupCodeHashes) {
|
|
501
412
|
config.backupCodeHashes = authConfig.backupCodeHashes;
|
|
@@ -597,17 +508,7 @@ function processAuthenticatedMessage(dc, connectionId, msg, ctx) {
|
|
|
597
508
|
case 'gateway-msg':
|
|
598
509
|
if (app?.gatewayClient && typeof msg['payload'] === 'string') {
|
|
599
510
|
const _gwPayload = msg['payload'];
|
|
600
|
-
|
|
601
|
-
try {
|
|
602
|
-
const _gwMsg = JSON.parse(_gwPayload);
|
|
603
|
-
if (_gwMsg.method === 'sessions.patch' && _gwMsg.params?.model && _gwMsg.params?.key) {
|
|
604
|
-
const _db = app.getActiveDb();
|
|
605
|
-
_db.prepare('UPDATE threads SET model = ?, updated_at = ? WHERE session_key = ?')
|
|
606
|
-
.run(_gwMsg.params.model, Date.now(), _gwMsg.params.key);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
catch { /* ignore parse/db errors */ }
|
|
610
|
-
app.gatewayClient.sendToGateway(_gwPayload);
|
|
511
|
+
app.gatewayClient.forwardFromBrowser(_gwPayload);
|
|
611
512
|
}
|
|
612
513
|
break;
|
|
613
514
|
case 'gateway-msg-chunk': {
|
|
@@ -637,7 +538,7 @@ function processAuthenticatedMessage(dc, connectionId, msg, ctx) {
|
|
|
637
538
|
gatewayMsgChunkBuffers.delete(chunkId);
|
|
638
539
|
const fullPayload = buf.chunks.join('');
|
|
639
540
|
if (app?.gatewayClient) {
|
|
640
|
-
app.gatewayClient.
|
|
541
|
+
app.gatewayClient.forwardFromBrowser(fullPayload);
|
|
641
542
|
}
|
|
642
543
|
}
|
|
643
544
|
break;
|
|
@@ -748,8 +649,7 @@ async function handleRpcMessage(dc, msg, ctx) {
|
|
|
748
649
|
}
|
|
749
650
|
function sendChunked(dc, id, status, body) {
|
|
750
651
|
const data = typeof body === 'string' ? body : JSON.stringify(body);
|
|
751
|
-
const
|
|
752
|
-
const totalChunks = Math.ceil(data.length / chunkSize);
|
|
652
|
+
const totalChunks = Math.ceil(data.length / DC_CHUNK_SIZE);
|
|
753
653
|
for (let i = 0; i < totalChunks; i++) {
|
|
754
654
|
dc.send(JSON.stringify({
|
|
755
655
|
type: 'rpc-chunk',
|
|
@@ -757,7 +657,22 @@ function sendChunked(dc, id, status, body) {
|
|
|
757
657
|
status,
|
|
758
658
|
index: i,
|
|
759
659
|
total: totalChunks,
|
|
760
|
-
data: data.slice(i *
|
|
660
|
+
data: data.slice(i * DC_CHUNK_SIZE, (i + 1) * DC_CHUNK_SIZE),
|
|
661
|
+
}));
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
/** A gateway frame too large for one DataChannel message; the browser transport reassembles it. */
|
|
665
|
+
let gatewayChunkSeq = 0;
|
|
666
|
+
function sendGatewayEventChunked(client, payload) {
|
|
667
|
+
const id = `ge-${Date.now().toString(36)}-${(gatewayChunkSeq++).toString(36)}`;
|
|
668
|
+
const total = Math.ceil(payload.length / DC_CHUNK_SIZE);
|
|
669
|
+
for (let i = 0; i < total; i++) {
|
|
670
|
+
client.send(JSON.stringify({
|
|
671
|
+
type: 'gateway-event-chunk',
|
|
672
|
+
id,
|
|
673
|
+
index: i,
|
|
674
|
+
total,
|
|
675
|
+
data: payload.slice(i * DC_CHUNK_SIZE, (i + 1) * DC_CHUNK_SIZE),
|
|
761
676
|
}));
|
|
762
677
|
}
|
|
763
678
|
}
|
|
@@ -870,9 +785,7 @@ async function handleSetup(token, options = {}) {
|
|
|
870
785
|
saveConfig(config);
|
|
871
786
|
// Create data directories
|
|
872
787
|
const dataDir = path.join(CONFIG_DIR, 'data');
|
|
873
|
-
const uploadsDir = path.join(CONFIG_DIR, 'uploads');
|
|
874
788
|
fs.mkdirSync(dataDir, { recursive: true });
|
|
875
|
-
fs.mkdirSync(uploadsDir, { recursive: true });
|
|
876
789
|
ws.close();
|
|
877
790
|
if (options.skipTotp) {
|
|
878
791
|
// Agent-driven flow: skip interactive TOTP enrollment.
|
|
@@ -1148,6 +1061,37 @@ async function handleReauth() {
|
|
|
1148
1061
|
console.log(' Restart the gateway for changes to take effect: systemctl --user restart openclaw-gateway');
|
|
1149
1062
|
}
|
|
1150
1063
|
}
|
|
1064
|
+
async function handleExportHistory(api, mode) {
|
|
1065
|
+
if (mode !== undefined && mode !== 'check') {
|
|
1066
|
+
console.error(`Unknown option "${mode}". Use: ocplatform clawchats export-history [check]`);
|
|
1067
|
+
process.exitCode = 1;
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
const gwCfg = api.config;
|
|
1071
|
+
const gw = gwCfg?.['gateway'];
|
|
1072
|
+
const token = gw?.['auth']?.['token'] || loadConfig()?.gatewayToken || '';
|
|
1073
|
+
if (!token) {
|
|
1074
|
+
console.error('No gateway token found. Is ClawChats set up? (ocplatform clawchats setup <token>)');
|
|
1075
|
+
process.exitCode = 1;
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
const port = gw?.['port'] || 18789;
|
|
1079
|
+
// @ts-expect-error — server/ is plain JS with no .d.ts
|
|
1080
|
+
const serverModule = await import('../server/history-export.js');
|
|
1081
|
+
try {
|
|
1082
|
+
await serverModule.exportHistory({
|
|
1083
|
+
dataDir: path.join(CONFIG_DIR, 'data'),
|
|
1084
|
+
stateDir: path.dirname(CONFIG_DIR),
|
|
1085
|
+
gatewayUrl: process.env.GATEWAY_WS_URL || `ws://127.0.0.1:${port}`,
|
|
1086
|
+
token,
|
|
1087
|
+
check: mode === 'check',
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
catch (err) {
|
|
1091
|
+
console.error(`export-history failed: ${err.message}`);
|
|
1092
|
+
process.exitCode = 1;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1151
1095
|
async function handleStatus() {
|
|
1152
1096
|
// CLI runs in a separate process — module-level vars are null here.
|
|
1153
1097
|
// Query the live service via the health endpoint instead.
|
|
@@ -1267,18 +1211,6 @@ const plugin = {
|
|
|
1267
1211
|
name: 'ClawChats',
|
|
1268
1212
|
description: 'Connects your gateway to ClawChats via WebRTC P2P',
|
|
1269
1213
|
register(api) {
|
|
1270
|
-
// Inject MEDIA: capability note into the system prompt via before_prompt_build.
|
|
1271
|
-
// Appended to system context (trusted, not user-turn) so it's always present and
|
|
1272
|
-
// never flagged as prompt injection. Survives compaction automatically.
|
|
1273
|
-
//
|
|
1274
|
-
// Path parsing lives in server/gateway.js handleAgentEvent (stable singleton) —
|
|
1275
|
-
// do NOT hold state in this closure; OpenClaw may call register() multiple times
|
|
1276
|
-
// during the plugin lifecycle and each call creates a fresh closure.
|
|
1277
|
-
api.on('before_prompt_build', (_event, _ctx) => {
|
|
1278
|
-
return {
|
|
1279
|
-
appendSystemContext: 'ClawChats inline preview: after writing a file with the Write tool, run `echo "MEDIA:/absolute/path/to/file"` via the exec tool to display it inline in the chat UI.',
|
|
1280
|
-
};
|
|
1281
|
-
}, { name: 'clawchats-media-hint', description: 'Appends MEDIA: file preview capability note to system prompt' });
|
|
1282
1214
|
// Background service: signaling + gateway bridge + future WebRTC
|
|
1283
1215
|
api.registerService({
|
|
1284
1216
|
id: 'connector-service',
|
|
@@ -1312,6 +1244,9 @@ const plugin = {
|
|
|
1312
1244
|
cmd.command('reset')
|
|
1313
1245
|
.description('Disconnect and remove all ClawChats data')
|
|
1314
1246
|
.action(() => handleReset());
|
|
1247
|
+
cmd.command('export-history [check]')
|
|
1248
|
+
.description('Export ClawChats chats the gateway has no history for, for the gateway importer ("check" = report only)')
|
|
1249
|
+
.action((mode) => handleExportHistory(api, mode === undefined ? undefined : String(mode)));
|
|
1315
1250
|
cmd.command('import <path>')
|
|
1316
1251
|
.description('Import databases and config from a folder (e.g. migrate from old data directory)')
|
|
1317
1252
|
.action((srcPath) => handleImport(String(srcPath)));
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Persisted ClawChats plugin config (~/.ocplatform/clawchats/config.json). */
|
|
2
|
+
export interface PluginConfig {
|
|
3
|
+
userId: string;
|
|
4
|
+
serverUrl: string;
|
|
5
|
+
apiKey: string;
|
|
6
|
+
gatewayId?: string;
|
|
7
|
+
gatewayToken?: string;
|
|
8
|
+
devicePrivateKey?: string;
|
|
9
|
+
schemaVersion: number;
|
|
10
|
+
installedAt: string;
|
|
11
|
+
totp?: {
|
|
12
|
+
secret: string;
|
|
13
|
+
algorithm: string;
|
|
14
|
+
digits: number;
|
|
15
|
+
period: number;
|
|
16
|
+
enabledAt: string;
|
|
17
|
+
};
|
|
18
|
+
google?: {
|
|
19
|
+
clientId: string;
|
|
20
|
+
authorizedSub: string;
|
|
21
|
+
authorizedEmail: string;
|
|
22
|
+
};
|
|
23
|
+
sessionSecret?: string;
|
|
24
|
+
backupCodeHashes?: string[];
|
|
25
|
+
totpPending?: {
|
|
26
|
+
secret: string;
|
|
27
|
+
generatedAt: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clawchatsai/connector",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tsc",
|
|
22
22
|
"dev": "tsc --watch",
|
|
23
|
+
"test": "node --test 'server/**/*.test.js'",
|
|
23
24
|
"prepublishOnly": "npm run build"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { DatabaseSync as Database } from 'node:sqlite';
|
|
2
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
|
|
4
|
-
// Per-request workspace DB — isolates concurrent clients on different workspaces.
|
|
5
|
-
export const requestDbStore = new AsyncLocalStorage();
|
|
6
2
|
|
|
7
3
|
// node:sqlite is a built-in Node.js module (available since Node 22.5).
|
|
8
4
|
// No native compilation required — SQLite is bundled directly into Node.
|
package/server/config.js
CHANGED
|
@@ -4,7 +4,6 @@ import os from 'node:os';
|
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
5
|
|
|
6
6
|
export const HOME = os.homedir();
|
|
7
|
-
export const MAX_PREAMBLE_CHARS = 50000;
|
|
8
7
|
|
|
9
8
|
// Resolve __dirname for ESM (esbuild inlines this correctly)
|
|
10
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -43,21 +42,13 @@ export function discoverGatewayWsUrl() {
|
|
|
43
42
|
}
|
|
44
43
|
export const GATEWAY_WS_URL = discoverGatewayWsUrl();
|
|
45
44
|
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return path.join(HOME, '.openclaw', '
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function validateAgent(agentId) {
|
|
58
|
-
if (!agentId) return 'main';
|
|
59
|
-
if (!/^[a-zA-Z0-9_-]+$/.test(agentId)) throw new Error('Invalid agent ID');
|
|
60
|
-
const agentDir = path.join(HOME, '.openclaw', 'agents', agentId);
|
|
61
|
-
if (!fs.existsSync(agentDir)) throw new Error(`Agent not found: ${agentId}`);
|
|
62
|
-
return agentId;
|
|
45
|
+
// Agent workspace dir (relative /api/file paths resolve against it): openclaw config → default.
|
|
46
|
+
export function discoverWorkspaceDir() {
|
|
47
|
+
for (const cfgPath of [path.join(HOME, '.openclaw', 'openclaw.json'), '/etc/openclaw/openclaw.json']) {
|
|
48
|
+
try {
|
|
49
|
+
const ws = JSON.parse(fs.readFileSync(cfgPath, 'utf8')).agents?.defaults?.workspace;
|
|
50
|
+
if (ws) return ws;
|
|
51
|
+
} catch { /* try next */ }
|
|
52
|
+
}
|
|
53
|
+
return path.join(HOME, '.openclaw', 'workspace');
|
|
63
54
|
}
|
|
@@ -7,11 +7,11 @@ import { parseMultipart } from '../util/multipart.js';
|
|
|
7
7
|
const HOME = os.homedir();
|
|
8
8
|
const ALLOWED_FILE_DIRS = [HOME, '/tmp'];
|
|
9
9
|
|
|
10
|
-
export function handleServeFile(req, res, query,
|
|
10
|
+
export function handleServeFile(req, res, query, workspaceDir) {
|
|
11
11
|
const filePath = query.path;
|
|
12
12
|
if (!filePath) return sendError(res, 400, 'Missing path parameter');
|
|
13
13
|
const resolved = (filePath.startsWith('./') || filePath.startsWith('../'))
|
|
14
|
-
? path.resolve(
|
|
14
|
+
? path.resolve(workspaceDir, filePath)
|
|
15
15
|
: path.resolve(filePath);
|
|
16
16
|
if (!ALLOWED_FILE_DIRS.some(dir => resolved.startsWith(dir + '/') || resolved === dir)) return sendError(res, 403, 'Access denied: path not in allowed directories');
|
|
17
17
|
if (!fs.existsSync(resolved) || !fs.statSync(resolved).isFile()) return sendError(res, 404, 'File not found');
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { sendError } from '../util/http.js';
|
|
3
|
+
|
|
4
|
+
// Agent-delivered media (`MEDIA:` lines) lives in the gateway's outgoing-media store and
|
|
5
|
+
// is referenced from transcripts as /api/chat/media/outgoing/<session>/<id>/<variant>.
|
|
6
|
+
// That route needs the gateway token, which the browser doesn't have; the connector
|
|
7
|
+
// fetches it on the browser's behalf.
|
|
8
|
+
//
|
|
9
|
+
// User-sent attachments live in the gateway's inbound media store (`media://inbound/<file>`),
|
|
10
|
+
// served by the Control UI's authenticated assistant-media route:
|
|
11
|
+
// /__<platform>__/assistant-media?source=media://inbound/<file>&sessionKey=…&agentId=…
|
|
12
|
+
// The route is matched by shape (no brand string), and only plain inbound sources pass.
|
|
13
|
+
const ALLOWED_PREFIX = '/api/chat/media/outgoing/';
|
|
14
|
+
const INBOUND_ROUTE = /^\/__[a-z0-9-]+__\/assistant-media\?/;
|
|
15
|
+
const INBOUND_SOURCE = /^media:\/\/inbound\/[^/?#\\]+$/;
|
|
16
|
+
|
|
17
|
+
export function isAllowedMediaTarget(target) {
|
|
18
|
+
if (typeof target !== 'string' || target.includes('..')) return false;
|
|
19
|
+
if (target.startsWith(ALLOWED_PREFIX)) return true;
|
|
20
|
+
if (!INBOUND_ROUTE.test(target)) return false;
|
|
21
|
+
const q = new URLSearchParams(target.slice(target.indexOf('?') + 1));
|
|
22
|
+
return INBOUND_SOURCE.test(q.get('source') || '') && !q.has('meta') && !q.has('allow') && !q.has('mediaTicket');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createGatewayMediaHandler({ gatewayWsUrl, gatewayToken }) {
|
|
26
|
+
const base = new URL(gatewayWsUrl.replace(/^ws/, 'http'));
|
|
27
|
+
return function handleGatewayMedia(req, res, query) {
|
|
28
|
+
const target = query.u || '';
|
|
29
|
+
if (!isAllowedMediaTarget(target)) return sendError(res, 400, 'Unsupported media URL');
|
|
30
|
+
const upstream = http.request({
|
|
31
|
+
hostname: base.hostname, port: base.port, path: target, method: 'GET',
|
|
32
|
+
headers: { Authorization: `Bearer ${gatewayToken}` },
|
|
33
|
+
}, up => {
|
|
34
|
+
const headers = { 'Cache-Control': 'private, max-age=86400' };
|
|
35
|
+
for (const h of ['content-type', 'content-length', 'content-disposition']) if (up.headers[h]) headers[h] = up.headers[h];
|
|
36
|
+
res.writeHead(up.statusCode || 502, headers);
|
|
37
|
+
up.pipe(res);
|
|
38
|
+
});
|
|
39
|
+
upstream.on('error', e => { if (!res.headersSent) sendError(res, 502, `Gateway media: ${e.message}`); else res.destroy(); });
|
|
40
|
+
upstream.end();
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { isAllowedMediaTarget } from './gateway-media.js';
|
|
4
|
+
|
|
5
|
+
const inbound = (src, extra = '') => `/__gw__/assistant-media?source=${encodeURIComponent(src)}&sessionKey=agent%3Amain%3Adashboard%3Ax&agentId=main${extra}`;
|
|
6
|
+
|
|
7
|
+
test('outgoing agent media is allowed', () => {
|
|
8
|
+
assert.equal(isAllowedMediaTarget('/api/chat/media/outgoing/s/id/full'), true);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('inbound user media via the assistant-media route is allowed', () => {
|
|
12
|
+
assert.equal(isAllowedMediaTarget(inbound('media://inbound/red---9c45.png')), true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('anything else is rejected', () => {
|
|
16
|
+
assert.equal(isAllowedMediaTarget('/api/sessions'), false);
|
|
17
|
+
assert.equal(isAllowedMediaTarget(inbound('/etc/passwd')), false);
|
|
18
|
+
assert.equal(isAllowedMediaTarget(inbound('file:///etc/passwd')), false);
|
|
19
|
+
assert.equal(isAllowedMediaTarget(inbound('media://inbound/a/b.png')), false);
|
|
20
|
+
assert.equal(isAllowedMediaTarget(inbound('media://inbound/..png')), false);
|
|
21
|
+
assert.equal(isAllowedMediaTarget(inbound('media://inbound/x.png', '&meta=1&allow=1')), false);
|
|
22
|
+
assert.equal(isAllowedMediaTarget('/__gw__/other?source=media%3A%2F%2Finbound%2Fx.png'), false);
|
|
23
|
+
});
|
package/server/debug.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
|
|
4
|
+
/** Frames that may carry a user-entered secret (question.resolve answers, incl. secret-store values) are logged with the values masked. */
|
|
5
|
+
export function redactFrame(data) {
|
|
6
|
+
if (typeof data !== 'string' || !data.includes('question.resolve')) return data;
|
|
7
|
+
try {
|
|
8
|
+
const msg = JSON.parse(data);
|
|
9
|
+
if (msg?.type === 'req' && msg.method === 'question.resolve' && msg.params?.answers?.answers) {
|
|
10
|
+
const masked = Object.fromEntries(Object.entries(msg.params.answers.answers).map(([k, v]) => [k, Array.isArray(v) ? v.map(() => '[redacted]') : '[redacted]']));
|
|
11
|
+
return JSON.stringify({ ...msg, params: { ...msg.params, answers: { answers: masked } } });
|
|
12
|
+
}
|
|
13
|
+
} catch { /* not JSON: log nothing sensitive-looking */ return '[unparseable question.resolve frame omitted]'; }
|
|
14
|
+
return data;
|
|
15
|
+
}
|
|
16
|
+
|
|
4
17
|
export class DebugLogger {
|
|
5
18
|
constructor(baseDir) {
|
|
6
19
|
this.baseDir = path.join(baseDir, '..', 'debug');
|
|
@@ -22,7 +35,7 @@ export class DebugLogger {
|
|
|
22
35
|
}
|
|
23
36
|
|
|
24
37
|
logFrame(direction, data) {
|
|
25
|
-
if (this.active && this.wsStream) this.wsStream.write(`${new Date().toISOString()} ${direction} ${data}\n`);
|
|
38
|
+
if (this.active && this.wsStream) this.wsStream.write(`${new Date().toISOString()} ${direction} ${redactFrame(data)}\n`);
|
|
26
39
|
}
|
|
27
40
|
|
|
28
41
|
saveDump(payload) {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { redactFrame } from './debug.js';
|
|
4
|
+
|
|
5
|
+
test('question.resolve answers are masked in debug logs', () => {
|
|
6
|
+
const frame = JSON.stringify({ type: 'req', id: '1', method: 'question.resolve', params: { id: 'q', answers: { answers: { api_key: ['sk-live-SECRET'], other: ['a', 'b'] } }, secretStoreAllowedHosts: ['api.example.com'] } });
|
|
7
|
+
const out = redactFrame(frame);
|
|
8
|
+
assert.ok(!out.includes('sk-live-SECRET'));
|
|
9
|
+
const parsed = JSON.parse(out);
|
|
10
|
+
assert.deepEqual(parsed.params.answers.answers, { api_key: ['[redacted]'], other: ['[redacted]', '[redacted]'] });
|
|
11
|
+
assert.equal(parsed.params.id, 'q');
|
|
12
|
+
assert.deepEqual(parsed.params.secretStoreAllowedHosts, ['api.example.com']);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('other frames pass through untouched; unparseable question.resolve frames are dropped', () => {
|
|
16
|
+
const chat = JSON.stringify({ type: 'req', method: 'chat.send', params: { message: 'hi' } });
|
|
17
|
+
assert.equal(redactFrame(chat), chat);
|
|
18
|
+
assert.equal(redactFrame('{"type":"event"}'), '{"type":"event"}');
|
|
19
|
+
assert.ok(!redactFrame('question.resolve {not json secret').includes('secret'));
|
|
20
|
+
const cancel = JSON.stringify({ type: 'req', method: 'question.resolve', params: { id: 'q', cancel: true } });
|
|
21
|
+
assert.equal(redactFrame(cancel), cancel);
|
|
22
|
+
});
|