@privateclaw/privateclaw 0.1.26 → 0.1.27
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 +23 -4
- package/dist/bridges/openclaw-agent-bridge.js +10 -2
- package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
- package/dist/cli.js +69 -6
- package/dist/cli.js.map +1 -1
- package/dist/provider.js +0 -2
- package/dist/provider.js.map +1 -1
- package/dist/setup.d.ts +86 -0
- package/dist/setup.js +501 -0
- package/dist/setup.js.map +1 -0
- package/dist/text.d.ts +3 -0
- package/dist/text.js +3 -0
- package/dist/text.js.map +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/bridges/openclaw-agent-bridge.test.ts +59 -0
- package/src/bridges/openclaw-agent-bridge.ts +11 -2
- package/src/cli.ts +92 -4
- package/src/provider.test.ts +121 -0
- package/src/provider.ts +0 -2
- package/src/setup.test.ts +187 -0
- package/src/setup.ts +814 -0
- package/src/text.ts +16 -0
|
@@ -228,6 +228,65 @@ test("parseOpenClawAgentOutput resolves structured filePath attachments from the
|
|
|
228
228
|
assert.equal(attachment.dataBase64, Buffer.from("hello from filePath").toString("base64"));
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
+
test("parseOpenClawAgentOutput preserves top-level structured attachments as a follow-up message", async (t) => {
|
|
232
|
+
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-workspace-"));
|
|
233
|
+
t.after(async () => {
|
|
234
|
+
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const attachmentPath = path.join(workspaceDir, "tetris.jpg");
|
|
238
|
+
await fs.writeFile(attachmentPath, "top-level attachment bytes", "utf8");
|
|
239
|
+
|
|
240
|
+
const result = parseOpenClawAgentOutput(
|
|
241
|
+
JSON.stringify({
|
|
242
|
+
status: "ok",
|
|
243
|
+
result: {
|
|
244
|
+
payloads: [
|
|
245
|
+
{
|
|
246
|
+
text: [
|
|
247
|
+
"<privateclaw-response>",
|
|
248
|
+
JSON.stringify({
|
|
249
|
+
version: 1,
|
|
250
|
+
messages: [{ text: "搞定啦!截图如下~" }],
|
|
251
|
+
attachments: [
|
|
252
|
+
{
|
|
253
|
+
name: "tetris.jpg",
|
|
254
|
+
mimeType: "image/jpeg",
|
|
255
|
+
filePath: attachmentPath,
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
}),
|
|
259
|
+
"</privateclaw-response>",
|
|
260
|
+
].join("\n"),
|
|
261
|
+
},
|
|
262
|
+
],
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
{ workspaceDir },
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
if (typeof result === "string") {
|
|
269
|
+
assert.fail("Expected an attachment-bearing bridge response.");
|
|
270
|
+
}
|
|
271
|
+
assert.equal(result.messages.length, 2);
|
|
272
|
+
assert.deepEqual(result.messages[0], { text: "搞定啦!截图如下~" });
|
|
273
|
+
const attachmentMessage = result.messages[1];
|
|
274
|
+
assert(attachmentMessage && typeof attachmentMessage !== "string");
|
|
275
|
+
assert.equal(attachmentMessage.text, "");
|
|
276
|
+
const [attachment] = attachmentMessage.attachments ?? [];
|
|
277
|
+
assert(attachment);
|
|
278
|
+
assert.equal(attachment.name, "tetris.jpg");
|
|
279
|
+
assert.equal(attachment.mimeType, "image/jpeg");
|
|
280
|
+
assert.equal(
|
|
281
|
+
attachment.sizeBytes,
|
|
282
|
+
Buffer.byteLength("top-level attachment bytes"),
|
|
283
|
+
);
|
|
284
|
+
assert.equal(
|
|
285
|
+
attachment.dataBase64,
|
|
286
|
+
Buffer.from("top-level attachment bytes").toString("base64"),
|
|
287
|
+
);
|
|
288
|
+
});
|
|
289
|
+
|
|
231
290
|
test("parseOpenClawAgentOutput accepts structured absolute filePath attachments outside the workspace", async (t) => {
|
|
232
291
|
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-workspace-"));
|
|
233
292
|
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-outside-"));
|
|
@@ -87,6 +87,7 @@ interface PrivateClawStructuredMessage {
|
|
|
87
87
|
interface PrivateClawStructuredResponse {
|
|
88
88
|
version?: number;
|
|
89
89
|
messages?: PrivateClawStructuredMessage[];
|
|
90
|
+
attachments?: PrivateClawStructuredMessageAttachment[];
|
|
90
91
|
data?: unknown;
|
|
91
92
|
}
|
|
92
93
|
|
|
@@ -1355,6 +1356,7 @@ function buildStructuredResponseContractPrompt(): string {
|
|
|
1355
1356
|
"- Put user-visible text only in messages[].text.",
|
|
1356
1357
|
"- To send images/files, add an attachments array to the message: {\"text\":\"...\",\"attachments\":[{\"name\":\"file.png\",\"mimeType\":\"image/png\",\"filePath\":\"relative/path/in/current/workspace/or/absolute/local/path\"}]}",
|
|
1357
1358
|
"- Or use dataBase64 for inline binary: {\"name\":\"file.png\",\"mimeType\":\"image/png\",\"dataBase64\":\"...\"}",
|
|
1359
|
+
"- Never put attachments at the top level of the response object. Files must live inside messages[].attachments.",
|
|
1358
1360
|
"- Never put channel-specific markup such as <qqimg>...</qqimg>, markdown image syntax, or raw local file paths inside messages[].text. Put files only in messages[].attachments.",
|
|
1359
1361
|
"- Use data for optional machine-readable extraction results that PrivateClaw may consume in future file-processing flows.",
|
|
1360
1362
|
"- Do not wrap the JSON in markdown fences.",
|
|
@@ -1428,12 +1430,12 @@ function parseStructuredResponseResult(
|
|
|
1428
1430
|
},
|
|
1429
1431
|
): NormalizedBridgeResult | undefined {
|
|
1430
1432
|
const structured = parseStructuredResponseBlock(raw);
|
|
1431
|
-
if (!structured
|
|
1433
|
+
if (!structured) {
|
|
1432
1434
|
return undefined;
|
|
1433
1435
|
}
|
|
1434
1436
|
|
|
1435
1437
|
const messages: NormalizedBridgeMessage[] = [];
|
|
1436
|
-
for (const message of structured.messages) {
|
|
1438
|
+
for (const message of structured.messages ?? []) {
|
|
1437
1439
|
const text = typeof message.text === "string" ? message.text.trim() : "";
|
|
1438
1440
|
const qqimgResult = extractInlineQqimgAttachments(text, options);
|
|
1439
1441
|
const attachments = [
|
|
@@ -1447,6 +1449,13 @@ function parseStructuredResponseResult(
|
|
|
1447
1449
|
});
|
|
1448
1450
|
}
|
|
1449
1451
|
}
|
|
1452
|
+
const topLevelAttachments = resolveStructuredAttachments(structured.attachments, options);
|
|
1453
|
+
if (topLevelAttachments.length > 0) {
|
|
1454
|
+
messages.push({
|
|
1455
|
+
text: "",
|
|
1456
|
+
attachments: topLevelAttachments,
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1450
1459
|
if (messages.length === 0) {
|
|
1451
1460
|
return undefined;
|
|
1452
1461
|
}
|
package/src/cli.ts
CHANGED
|
@@ -40,15 +40,22 @@ import {
|
|
|
40
40
|
PrivateClawSessionControlServer,
|
|
41
41
|
resolvePrivateClawStateDir,
|
|
42
42
|
} from "./session-control.js";
|
|
43
|
+
import {
|
|
44
|
+
parsePrivateClawSessionDurationPreset,
|
|
45
|
+
runPrivateClawSetup,
|
|
46
|
+
} from "./setup.js";
|
|
43
47
|
import {
|
|
44
48
|
buildPrivateClawBackgroundDaemonReminder,
|
|
45
49
|
formatBilingualInline,
|
|
50
|
+
PRIVATECLAW_CLI_DURATION_OPTION_DESCRIPTION,
|
|
46
51
|
PRIVATECLAW_CLI_NOTIFY_OPTION_DESCRIPTION,
|
|
47
52
|
PRIVATECLAW_CLI_RELAY_OPTION_DESCRIPTION,
|
|
53
|
+
PRIVATECLAW_CLI_SETUP_DESCRIPTION,
|
|
48
54
|
PRIVATECLAW_CLI_SESSIONS_FOLLOW_DESCRIPTION,
|
|
49
55
|
PRIVATECLAW_CLI_SESSIONS_KILL_DESCRIPTION,
|
|
50
56
|
PRIVATECLAW_CLI_SESSIONS_KILLALL_DESCRIPTION,
|
|
51
57
|
PRIVATECLAW_CLI_SESSIONS_QR_DESCRIPTION,
|
|
58
|
+
PRIVATECLAW_CLI_SINGLE_OPTION_DESCRIPTION,
|
|
52
59
|
PRIVATECLAW_CLI_VERBOSE_OPTION_DESCRIPTION,
|
|
53
60
|
} from "./text.js";
|
|
54
61
|
import type {
|
|
@@ -311,8 +318,9 @@ async function readHandoffState(
|
|
|
311
318
|
}
|
|
312
319
|
|
|
313
320
|
function printHelp(): void {
|
|
314
|
-
console.log(`privateclaw-provider <pair|sessions|kick|killall>
|
|
321
|
+
console.log(`privateclaw-provider <setup|pair|sessions|kick|killall>
|
|
315
322
|
|
|
323
|
+
setup [--group|--single] [--duration <preset>] [--ttl-ms <ms>] [--label <label>] [--relay <url>] [--open] [--foreground] [--verbose]
|
|
316
324
|
pair [--ttl-ms <ms>] [--label <label>] [--relay <url>] [--group] [--print-only] [--open] [--foreground] [--verbose]
|
|
317
325
|
sessions
|
|
318
326
|
sessions follow <sessionId>
|
|
@@ -321,7 +329,10 @@ sessions kill <sessionId>
|
|
|
321
329
|
sessions killall
|
|
322
330
|
killall
|
|
323
331
|
kick <sessionId> <appId>`);
|
|
332
|
+
console.log(`\nsetup: ${PRIVATECLAW_CLI_SETUP_DESCRIPTION}`);
|
|
324
333
|
console.log(`\n--relay <url>: ${PRIVATECLAW_CLI_RELAY_OPTION_DESCRIPTION}`);
|
|
334
|
+
console.log(`--duration <preset>: ${PRIVATECLAW_CLI_DURATION_OPTION_DESCRIPTION}`);
|
|
335
|
+
console.log(`--single: ${PRIVATECLAW_CLI_SINGLE_OPTION_DESCRIPTION}`);
|
|
325
336
|
console.log(`--verbose: ${PRIVATECLAW_CLI_VERBOSE_OPTION_DESCRIPTION}`);
|
|
326
337
|
console.log(`--notify: ${PRIVATECLAW_CLI_NOTIFY_OPTION_DESCRIPTION}`);
|
|
327
338
|
console.log(`sessions follow: ${PRIVATECLAW_CLI_SESSIONS_FOLLOW_DESCRIPTION}`);
|
|
@@ -331,6 +342,79 @@ kick <sessionId> <appId>`);
|
|
|
331
342
|
console.log(`killall: ${PRIVATECLAW_CLI_SESSIONS_KILLALL_DESCRIPTION}`);
|
|
332
343
|
}
|
|
333
344
|
|
|
345
|
+
async function runSetupCommand(args: string[]): Promise<void> {
|
|
346
|
+
const parsed = parseArgs({
|
|
347
|
+
args,
|
|
348
|
+
allowPositionals: true,
|
|
349
|
+
options: {
|
|
350
|
+
group: { type: "boolean", default: false },
|
|
351
|
+
single: { type: "boolean", default: false },
|
|
352
|
+
duration: { type: "string" },
|
|
353
|
+
"ttl-ms": { type: "string" },
|
|
354
|
+
label: { type: "string" },
|
|
355
|
+
relay: { type: "string", short: "r" },
|
|
356
|
+
open: { type: "boolean", default: parseBooleanFlag(process.env.PRIVATECLAW_OPEN_QR) },
|
|
357
|
+
foreground: { type: "boolean", default: false },
|
|
358
|
+
verbose: { type: "boolean", default: parseBooleanFlag(process.env.PRIVATECLAW_VERBOSE) },
|
|
359
|
+
help: { type: "boolean", short: "h" },
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
if (parsed.values.help) {
|
|
364
|
+
printHelp();
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (parsed.positionals.length > 0) {
|
|
368
|
+
throw new Error(
|
|
369
|
+
formatBilingualInline(
|
|
370
|
+
"`setup` 不接受额外参数。",
|
|
371
|
+
"`setup` does not accept extra arguments.",
|
|
372
|
+
),
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
if (parsed.values.group && parsed.values.single) {
|
|
376
|
+
throw new Error(
|
|
377
|
+
formatBilingualInline(
|
|
378
|
+
"`setup` 不能同时使用 `--group` 和 `--single`。",
|
|
379
|
+
"`setup` cannot use both `--group` and `--single` at the same time.",
|
|
380
|
+
),
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
if (parsed.values.duration && parsed.values["ttl-ms"]) {
|
|
384
|
+
throw new Error(
|
|
385
|
+
formatBilingualInline(
|
|
386
|
+
"`setup` 不能同时使用 `--duration` 和 `--ttl-ms`。",
|
|
387
|
+
"`setup` cannot use both `--duration` and `--ttl-ms` at the same time.",
|
|
388
|
+
),
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const durationPreset = parsed.values.duration?.trim();
|
|
393
|
+
if (durationPreset) {
|
|
394
|
+
parsePrivateClawSessionDurationPreset(durationPreset);
|
|
395
|
+
}
|
|
396
|
+
const ttlMs = parsePositiveIntegerFlag(parsed.values["ttl-ms"], "--ttl-ms");
|
|
397
|
+
const groupMode =
|
|
398
|
+
parsed.values.group === true
|
|
399
|
+
? true
|
|
400
|
+
: parsed.values.single === true
|
|
401
|
+
? false
|
|
402
|
+
: undefined;
|
|
403
|
+
|
|
404
|
+
await runPrivateClawSetup({
|
|
405
|
+
...(typeof groupMode === "boolean" ? { groupMode } : {}),
|
|
406
|
+
...(typeof ttlMs === "number" ? { ttlMs } : {}),
|
|
407
|
+
...(durationPreset ? { durationPreset } : {}),
|
|
408
|
+
...(parsed.values.relay?.trim()
|
|
409
|
+
? { relayBaseUrl: parsed.values.relay.trim() }
|
|
410
|
+
: {}),
|
|
411
|
+
...(parsed.values.label?.trim() ? { label: parsed.values.label.trim() } : {}),
|
|
412
|
+
...(parsed.values.open ? { openInBrowser: true } : {}),
|
|
413
|
+
...(parsed.values.foreground ? { foreground: true } : {}),
|
|
414
|
+
...(parsed.values.verbose ? { verbose: true } : {}),
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
334
418
|
async function runPairCommand(args: string[]): Promise<void> {
|
|
335
419
|
const parsed = parseArgs({
|
|
336
420
|
args,
|
|
@@ -814,12 +898,16 @@ async function runKickCommand(args: string[]): Promise<void> {
|
|
|
814
898
|
|
|
815
899
|
const argv = process.argv.slice(2);
|
|
816
900
|
const [command, ...rest] =
|
|
817
|
-
argv.length === 0
|
|
818
|
-
? ["
|
|
901
|
+
argv.length === 0
|
|
902
|
+
? ["setup", ...argv]
|
|
903
|
+
: argv[0]!.startsWith("-")
|
|
904
|
+
? ["pair", ...argv]
|
|
819
905
|
: argv;
|
|
820
906
|
|
|
821
907
|
try {
|
|
822
|
-
if (command === "
|
|
908
|
+
if (command === "setup") {
|
|
909
|
+
await runSetupCommand(rest);
|
|
910
|
+
} else if (command === "pair") {
|
|
823
911
|
await runPairCommand(rest);
|
|
824
912
|
} else if (command === "sessions") {
|
|
825
913
|
await runSessionsCommand(rest);
|
package/src/provider.test.ts
CHANGED
|
@@ -531,6 +531,127 @@ test("provider creates one-time invites and replies through the encrypted relay"
|
|
|
531
531
|
appSocket.terminate();
|
|
532
532
|
});
|
|
533
533
|
|
|
534
|
+
test("provider keeps a non-group session alive after app initiated disconnect", async (t) => {
|
|
535
|
+
const relay = createRelayServer({
|
|
536
|
+
host: "127.0.0.1",
|
|
537
|
+
port: 0,
|
|
538
|
+
sessionTtlMs: 60_000,
|
|
539
|
+
frameCacheSize: 8,
|
|
540
|
+
});
|
|
541
|
+
const { port } = await relay.start();
|
|
542
|
+
t.after(async () => {
|
|
543
|
+
await relay.stop();
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
const provider = new PrivateClawProvider({
|
|
547
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
548
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
549
|
+
bridge: new EchoBridge("OpenClaw bridge"),
|
|
550
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
551
|
+
welcomeMessage: "欢迎来到 PrivateClaw",
|
|
552
|
+
});
|
|
553
|
+
await provider.connect();
|
|
554
|
+
t.after(async () => {
|
|
555
|
+
await provider.dispose();
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
const inviteBundle = await provider.createInviteBundle();
|
|
559
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
560
|
+
|
|
561
|
+
const appOneUrl = new URL(invite.appWsUrl);
|
|
562
|
+
appOneUrl.searchParams.set("appId", "app-one");
|
|
563
|
+
const appOne = new WebSocket(appOneUrl.toString());
|
|
564
|
+
const appOneAttached = nextMessage(appOne);
|
|
565
|
+
await waitForOpen(appOne);
|
|
566
|
+
assert.equal((await appOneAttached).type, "relay:attached");
|
|
567
|
+
|
|
568
|
+
appOne.send(
|
|
569
|
+
JSON.stringify({
|
|
570
|
+
type: "app:frame",
|
|
571
|
+
envelope: encryptPayload({
|
|
572
|
+
sessionId: invite.sessionId,
|
|
573
|
+
sessionKey: invite.sessionKey,
|
|
574
|
+
payload: {
|
|
575
|
+
kind: "client_hello",
|
|
576
|
+
appId: "app-one",
|
|
577
|
+
appVersion: "flutter-test",
|
|
578
|
+
deviceLabel: "Device One",
|
|
579
|
+
sentAt: new Date().toISOString(),
|
|
580
|
+
},
|
|
581
|
+
}),
|
|
582
|
+
}),
|
|
583
|
+
);
|
|
584
|
+
await nextMessages(appOne, 2);
|
|
585
|
+
|
|
586
|
+
appOne.send(
|
|
587
|
+
JSON.stringify({
|
|
588
|
+
type: "app:frame",
|
|
589
|
+
envelope: encryptPayload({
|
|
590
|
+
sessionId: invite.sessionId,
|
|
591
|
+
sessionKey: invite.sessionKey,
|
|
592
|
+
payload: {
|
|
593
|
+
kind: "session_close",
|
|
594
|
+
reason: "user_disconnect",
|
|
595
|
+
appId: "app-one",
|
|
596
|
+
sentAt: new Date().toISOString(),
|
|
597
|
+
},
|
|
598
|
+
}),
|
|
599
|
+
}),
|
|
600
|
+
);
|
|
601
|
+
await waitForNoMessage(appOne, 120);
|
|
602
|
+
appOne.close();
|
|
603
|
+
await waitForClose(appOne);
|
|
604
|
+
|
|
605
|
+
const appTwoUrl = new URL(invite.appWsUrl);
|
|
606
|
+
appTwoUrl.searchParams.set("appId", "app-two");
|
|
607
|
+
const appTwo = new WebSocket(appTwoUrl.toString());
|
|
608
|
+
const appTwoAttached = nextMessage(appTwo);
|
|
609
|
+
await waitForOpen(appTwo);
|
|
610
|
+
assert.equal((await appTwoAttached).type, "relay:attached");
|
|
611
|
+
|
|
612
|
+
appTwo.send(
|
|
613
|
+
JSON.stringify({
|
|
614
|
+
type: "app:frame",
|
|
615
|
+
envelope: encryptPayload({
|
|
616
|
+
sessionId: invite.sessionId,
|
|
617
|
+
sessionKey: invite.sessionKey,
|
|
618
|
+
payload: {
|
|
619
|
+
kind: "client_hello",
|
|
620
|
+
appId: "app-two",
|
|
621
|
+
appVersion: "flutter-test",
|
|
622
|
+
deviceLabel: "Device Two",
|
|
623
|
+
sentAt: new Date().toISOString(),
|
|
624
|
+
},
|
|
625
|
+
}),
|
|
626
|
+
}),
|
|
627
|
+
);
|
|
628
|
+
await nextMessages(appTwo, 1);
|
|
629
|
+
|
|
630
|
+
appTwo.send(
|
|
631
|
+
JSON.stringify({
|
|
632
|
+
type: "app:frame",
|
|
633
|
+
envelope: encryptPayload({
|
|
634
|
+
sessionId: invite.sessionId,
|
|
635
|
+
sessionKey: invite.sessionKey,
|
|
636
|
+
payload: {
|
|
637
|
+
kind: "user_message",
|
|
638
|
+
appId: "app-two",
|
|
639
|
+
text: "hello again",
|
|
640
|
+
clientMessageId: "client-message-reconnect-1",
|
|
641
|
+
sentAt: new Date().toISOString(),
|
|
642
|
+
},
|
|
643
|
+
}),
|
|
644
|
+
}),
|
|
645
|
+
);
|
|
646
|
+
|
|
647
|
+
const assistant = await nextRelayPayload(appTwo, invite);
|
|
648
|
+
assert.equal(assistant.kind, "assistant_message");
|
|
649
|
+
assert.match(assistant.text, /hello again/);
|
|
650
|
+
|
|
651
|
+
appTwo.close();
|
|
652
|
+
await waitForClose(appTwo);
|
|
653
|
+
});
|
|
654
|
+
|
|
534
655
|
test("provider turns a NO_REPLY bridge response into a fallback joke", async (t) => {
|
|
535
656
|
const relay = createRelayServer({
|
|
536
657
|
host: "127.0.0.1",
|
package/src/provider.ts
CHANGED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
buildPrivateClawSetupPlan,
|
|
5
|
+
detectLocalOpenClawStatus,
|
|
6
|
+
EFFECTIVELY_PERMANENT_SESSION_TTL_MS,
|
|
7
|
+
parsePrivateClawSessionDurationPreset,
|
|
8
|
+
renderPrivateClawSetupGuidance,
|
|
9
|
+
resolvePrivateClawSetupSelection,
|
|
10
|
+
runPrivateClawSetup,
|
|
11
|
+
} from "./setup.js";
|
|
12
|
+
|
|
13
|
+
test("detectLocalOpenClawStatus falls back to the privateclaw help probe when commands list is unavailable", async () => {
|
|
14
|
+
const status = await detectLocalOpenClawStatus(async (_command, args) => {
|
|
15
|
+
if (args.length === 1 && args[0] === "--version") {
|
|
16
|
+
return {
|
|
17
|
+
stdout: "OpenClaw 2026.3.22\n",
|
|
18
|
+
stderr: "",
|
|
19
|
+
combined: "OpenClaw 2026.3.22\n",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (args[0] === "commands" && args[1] === "list") {
|
|
23
|
+
throw new Error(
|
|
24
|
+
"Command `openclaw commands list` exited with code 1: error: unknown command 'commands'",
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if (
|
|
28
|
+
args[0] === "privateclaw" &&
|
|
29
|
+
args[1] === "pair" &&
|
|
30
|
+
args[2] === "--help"
|
|
31
|
+
) {
|
|
32
|
+
return {
|
|
33
|
+
stdout: "Usage: openclaw privateclaw pair [options]\n",
|
|
34
|
+
stderr: "",
|
|
35
|
+
combined: "Usage: openclaw privateclaw pair [options]\n",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (args[0] === "plugins" && args[1] === "info" && args[2] === "privateclaw") {
|
|
39
|
+
throw new Error("Plugin not found");
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Unexpected command args: ${args.join(" ")}`);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
assert.deepEqual(status, {
|
|
45
|
+
openClawAvailable: true,
|
|
46
|
+
privateClawCommandAvailable: true,
|
|
47
|
+
privateClawPluginPresent: false,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("parsePrivateClawSessionDurationPreset supports the permanent preset", () => {
|
|
52
|
+
const preset = parsePrivateClawSessionDurationPreset("permanent");
|
|
53
|
+
assert.equal(preset.ttlMs, EFFECTIVELY_PERMANENT_SESSION_TTL_MS);
|
|
54
|
+
assert.match(preset.label, /Permanent/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("resolvePrivateClawSetupSelection prompts for missing mode and duration", async () => {
|
|
58
|
+
const prompts: string[] = [];
|
|
59
|
+
const selection = await resolvePrivateClawSetupSelection({
|
|
60
|
+
promptForChoice: async (question, _choices, defaultValue) => {
|
|
61
|
+
prompts.push(question);
|
|
62
|
+
if (question.includes("session type")) {
|
|
63
|
+
return "group";
|
|
64
|
+
}
|
|
65
|
+
assert.equal(defaultValue, "24h");
|
|
66
|
+
return "1w";
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
assert.equal(selection.groupMode, true);
|
|
71
|
+
assert.equal(selection.ttlMs, 7 * 24 * 60 * 60 * 1000);
|
|
72
|
+
assert.match(selection.durationLabel, /1 week/);
|
|
73
|
+
assert.equal(prompts.length, 2);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("buildPrivateClawSetupPlan prints manual OpenClaw commands when openclaw is unavailable", () => {
|
|
77
|
+
const plan = buildPrivateClawSetupPlan({
|
|
78
|
+
packageSpec: "@privateclaw/privateclaw@0.1.26",
|
|
79
|
+
status: {
|
|
80
|
+
openClawAvailable: false,
|
|
81
|
+
privateClawCommandAvailable: false,
|
|
82
|
+
},
|
|
83
|
+
selection: {
|
|
84
|
+
groupMode: true,
|
|
85
|
+
ttlMs: 2 * 60 * 60 * 1000,
|
|
86
|
+
durationLabel: "2 小时 / 2 hours",
|
|
87
|
+
},
|
|
88
|
+
relayBaseUrl: "https://relay.example.com",
|
|
89
|
+
label: "hello",
|
|
90
|
+
openInBrowser: true,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
assert.equal(plan.localOpenClaw, false);
|
|
94
|
+
assert.equal(plan.manualSteps.length, 4);
|
|
95
|
+
assert.match(
|
|
96
|
+
renderPrivateClawSetupGuidance(plan),
|
|
97
|
+
/openclaw plugins install @privateclaw\/privateclaw@0\.1\.26/,
|
|
98
|
+
);
|
|
99
|
+
assert.equal(
|
|
100
|
+
plan.pairingCommand.display,
|
|
101
|
+
"openclaw privateclaw pair --ttl-ms 7200000 --group --label hello --relay https://relay.example.com --open",
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("runPrivateClawSetup installs, enables, restarts, then starts pairing", async () => {
|
|
106
|
+
const executed: string[] = [];
|
|
107
|
+
const pairings: string[] = [];
|
|
108
|
+
const logs: string[] = [];
|
|
109
|
+
let detectCalls = 0;
|
|
110
|
+
|
|
111
|
+
await runPrivateClawSetup({
|
|
112
|
+
packageSpec: "@privateclaw/privateclaw@0.1.26",
|
|
113
|
+
selection: {
|
|
114
|
+
groupMode: true,
|
|
115
|
+
ttlMs: 8 * 60 * 60 * 1000,
|
|
116
|
+
durationLabel: "8 小时 / 8 hours",
|
|
117
|
+
},
|
|
118
|
+
relayBaseUrl: "https://relay.example.com",
|
|
119
|
+
label: "demo",
|
|
120
|
+
verbose: true,
|
|
121
|
+
onLog: (line) => {
|
|
122
|
+
logs.push(line);
|
|
123
|
+
},
|
|
124
|
+
detectLocalOpenClawStatus: async () => {
|
|
125
|
+
detectCalls += 1;
|
|
126
|
+
if (detectCalls === 1) {
|
|
127
|
+
return {
|
|
128
|
+
openClawAvailable: true,
|
|
129
|
+
privateClawCommandAvailable: false,
|
|
130
|
+
privateClawPluginPresent: false,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
openClawAvailable: true,
|
|
135
|
+
privateClawCommandAvailable: true,
|
|
136
|
+
privateClawPluginPresent: true,
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
runStep: async (step) => {
|
|
140
|
+
executed.push(step.display);
|
|
141
|
+
},
|
|
142
|
+
runPairingCommand: async (step) => {
|
|
143
|
+
pairings.push(step.display);
|
|
144
|
+
return {
|
|
145
|
+
stdout: "",
|
|
146
|
+
stderr: "",
|
|
147
|
+
combined: "",
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
verificationTimeoutMs: 10,
|
|
151
|
+
verificationPollMs: 0,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
assert.deepEqual(executed, [
|
|
155
|
+
"openclaw plugins install @privateclaw/privateclaw@0.1.26",
|
|
156
|
+
"openclaw plugins enable privateclaw",
|
|
157
|
+
"openclaw gateway restart",
|
|
158
|
+
]);
|
|
159
|
+
assert.deepEqual(pairings, [
|
|
160
|
+
"openclaw privateclaw pair --ttl-ms 28800000 --group --label demo --relay https://relay.example.com --verbose",
|
|
161
|
+
]);
|
|
162
|
+
assert.match(logs.join("\n"), /Verified command availability/);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("runPrivateClawSetup prints guidance and fails when openclaw is unavailable locally", async () => {
|
|
166
|
+
const logs: string[] = [];
|
|
167
|
+
|
|
168
|
+
await assert.rejects(
|
|
169
|
+
runPrivateClawSetup({
|
|
170
|
+
selection: {
|
|
171
|
+
groupMode: false,
|
|
172
|
+
ttlMs: 30 * 60 * 1000,
|
|
173
|
+
durationLabel: "30 分钟 / 30 minutes",
|
|
174
|
+
},
|
|
175
|
+
onLog: (line) => {
|
|
176
|
+
logs.push(line);
|
|
177
|
+
},
|
|
178
|
+
detectLocalOpenClawStatus: async () => ({
|
|
179
|
+
openClawAvailable: false,
|
|
180
|
+
privateClawCommandAvailable: false,
|
|
181
|
+
}),
|
|
182
|
+
}),
|
|
183
|
+
/cannot be installed automatically/i,
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
assert.match(logs.join("\n"), /Run these commands on the machine where OpenClaw is installed/i);
|
|
187
|
+
});
|