@privateclaw/privateclaw 0.1.2 → 0.1.4
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 -1
- package/dist/bridges/openclaw-agent-bridge.d.ts +15 -0
- package/dist/bridges/openclaw-agent-bridge.js +113 -4
- package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
- package/dist/cli.js +2 -0
- package/dist/cli.js.map +1 -1
- package/dist/invite-qr-files.d.ts +12 -0
- package/dist/invite-qr-files.js +124 -0
- package/dist/invite-qr-files.js.map +1 -0
- package/dist/openclaw-plugin.js +14 -21
- package/dist/openclaw-plugin.js.map +1 -1
- package/dist/pair-session.d.ts +3 -1
- package/dist/pair-session.js +39 -3
- package/dist/pair-session.js.map +1 -1
- package/dist/qr-terminal-render.d.ts +7 -0
- package/dist/qr-terminal-render.js +44 -0
- package/dist/qr-terminal-render.js.map +1 -0
- package/dist/text.d.ts +2 -0
- package/dist/text.js +2 -0
- package/dist/text.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/bridges/openclaw-agent-bridge.test.ts +46 -0
- package/src/bridges/openclaw-agent-bridge.ts +155 -5
- package/src/cli.ts +2 -0
- package/src/invite-qr-files.ts +156 -0
- package/src/openclaw-plugin.test.ts +25 -3
- package/src/openclaw-plugin.ts +19 -32
- package/src/pair-session.ts +65 -1
- package/src/text.ts +10 -0
- package/src/types.ts +1 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import QRCode from "qrcode";
|
|
6
|
+
import {
|
|
7
|
+
PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
8
|
+
PRIVATECLAW_QR_IMAGE_MARGIN,
|
|
9
|
+
PRIVATECLAW_QR_PNG_WIDTH,
|
|
10
|
+
} from "./qr-options.js";
|
|
11
|
+
import type { PrivateClawInviteBundle } from "./types.js";
|
|
12
|
+
|
|
13
|
+
export interface PrivateClawQrPngArtifact {
|
|
14
|
+
pngPath: string;
|
|
15
|
+
pngFileUrl: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface PrivateClawQrPreviewArtifact {
|
|
19
|
+
previewPath: string;
|
|
20
|
+
previewFileUrl: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function resolvePrivateClawMediaDir(
|
|
24
|
+
baseStateDir: string = process.env.OPENCLAW_STATE_DIR ?? path.join(os.homedir(), ".openclaw"),
|
|
25
|
+
): string {
|
|
26
|
+
return path.join(baseStateDir, "media", "privateclaw");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function writeInviteQrPng(
|
|
30
|
+
bundle: PrivateClawInviteBundle,
|
|
31
|
+
mediaDir: string,
|
|
32
|
+
): Promise<PrivateClawQrPngArtifact> {
|
|
33
|
+
const pngBuffer = await QRCode.toBuffer(bundle.inviteUri, {
|
|
34
|
+
type: "png",
|
|
35
|
+
errorCorrectionLevel: PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
36
|
+
margin: PRIVATECLAW_QR_IMAGE_MARGIN,
|
|
37
|
+
width: PRIVATECLAW_QR_PNG_WIDTH,
|
|
38
|
+
});
|
|
39
|
+
await mkdir(mediaDir, { recursive: true });
|
|
40
|
+
const pngPath = path.join(mediaDir, `privateclaw-${bundle.invite.sessionId}.png`);
|
|
41
|
+
await writeFile(pngPath, pngBuffer);
|
|
42
|
+
return {
|
|
43
|
+
pngPath,
|
|
44
|
+
pngFileUrl: pathToFileURL(pngPath).href,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function writeInviteQrPreviewHtml(
|
|
49
|
+
bundle: PrivateClawInviteBundle,
|
|
50
|
+
mediaDir: string,
|
|
51
|
+
pngPath: string,
|
|
52
|
+
): Promise<PrivateClawQrPreviewArtifact> {
|
|
53
|
+
await mkdir(mediaDir, { recursive: true });
|
|
54
|
+
const previewPath = path.join(
|
|
55
|
+
mediaDir,
|
|
56
|
+
`privateclaw-${bundle.invite.sessionId}.html`,
|
|
57
|
+
);
|
|
58
|
+
const html = buildPreviewHtml(bundle, path.basename(pngPath));
|
|
59
|
+
await writeFile(previewPath, html, "utf8");
|
|
60
|
+
return {
|
|
61
|
+
previewPath,
|
|
62
|
+
previewFileUrl: pathToFileURL(previewPath).href,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function buildPreviewHtml(bundle: PrivateClawInviteBundle, pngFilename: string): string {
|
|
67
|
+
const title = escapeHtml(`PrivateClaw QR • ${bundle.invite.sessionId}`);
|
|
68
|
+
const announcementHtml = escapeHtml(bundle.announcementText).replace(/\r?\n/gu, "<br />");
|
|
69
|
+
const inviteHtml = escapeHtml(bundle.inviteUri);
|
|
70
|
+
const qrSrc = escapeHtml(pngFilename);
|
|
71
|
+
|
|
72
|
+
return `<!doctype html>
|
|
73
|
+
<html lang="en">
|
|
74
|
+
<head>
|
|
75
|
+
<meta charset="utf-8" />
|
|
76
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
77
|
+
<title>${title}</title>
|
|
78
|
+
<style>
|
|
79
|
+
:root {
|
|
80
|
+
color-scheme: dark;
|
|
81
|
+
font-family: Inter, "SF Pro Display", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
|
82
|
+
}
|
|
83
|
+
body {
|
|
84
|
+
margin: 0;
|
|
85
|
+
min-height: 100vh;
|
|
86
|
+
display: grid;
|
|
87
|
+
place-items: center;
|
|
88
|
+
background:
|
|
89
|
+
radial-gradient(circle at top, rgba(122, 164, 255, 0.24), transparent 46%),
|
|
90
|
+
linear-gradient(160deg, #060816 0%, #0d1730 48%, #05070e 100%);
|
|
91
|
+
color: #f4f7ff;
|
|
92
|
+
}
|
|
93
|
+
main {
|
|
94
|
+
width: min(92vw, 560px);
|
|
95
|
+
padding: 28px;
|
|
96
|
+
border-radius: 28px;
|
|
97
|
+
border: 1px solid rgba(255, 255, 255, 0.14);
|
|
98
|
+
background: rgba(11, 18, 35, 0.82);
|
|
99
|
+
box-shadow: 0 22px 64px rgba(0, 0, 0, 0.38);
|
|
100
|
+
backdrop-filter: blur(18px);
|
|
101
|
+
}
|
|
102
|
+
h1 {
|
|
103
|
+
margin: 0 0 14px;
|
|
104
|
+
font-size: 1.35rem;
|
|
105
|
+
}
|
|
106
|
+
p {
|
|
107
|
+
margin: 0;
|
|
108
|
+
line-height: 1.6;
|
|
109
|
+
color: rgba(234, 240, 255, 0.86);
|
|
110
|
+
}
|
|
111
|
+
img {
|
|
112
|
+
display: block;
|
|
113
|
+
width: min(100%, 320px);
|
|
114
|
+
margin: 22px auto;
|
|
115
|
+
border-radius: 20px;
|
|
116
|
+
background: white;
|
|
117
|
+
padding: 12px;
|
|
118
|
+
}
|
|
119
|
+
pre {
|
|
120
|
+
margin: 0;
|
|
121
|
+
padding: 14px;
|
|
122
|
+
overflow: auto;
|
|
123
|
+
border-radius: 18px;
|
|
124
|
+
background: rgba(255, 255, 255, 0.06);
|
|
125
|
+
color: #cfe2ff;
|
|
126
|
+
white-space: pre-wrap;
|
|
127
|
+
word-break: break-word;
|
|
128
|
+
}
|
|
129
|
+
.hint {
|
|
130
|
+
margin-top: 16px;
|
|
131
|
+
font-size: 0.92rem;
|
|
132
|
+
color: rgba(207, 226, 255, 0.76);
|
|
133
|
+
}
|
|
134
|
+
</style>
|
|
135
|
+
</head>
|
|
136
|
+
<body>
|
|
137
|
+
<main>
|
|
138
|
+
<h1>${title}</h1>
|
|
139
|
+
<p>${announcementHtml}</p>
|
|
140
|
+
<img src="./${qrSrc}" alt="PrivateClaw QR code" />
|
|
141
|
+
<pre>${inviteHtml}</pre>
|
|
142
|
+
<p class="hint">Scan this code with PrivateClaw, or copy the invite URI if your device is already ready to join.</p>
|
|
143
|
+
</main>
|
|
144
|
+
</body>
|
|
145
|
+
</html>
|
|
146
|
+
`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function escapeHtml(value: string): string {
|
|
150
|
+
return value
|
|
151
|
+
.replaceAll("&", "&")
|
|
152
|
+
.replaceAll("<", "<")
|
|
153
|
+
.replaceAll(">", ">")
|
|
154
|
+
.replaceAll('"', """)
|
|
155
|
+
.replaceAll("'", "'");
|
|
156
|
+
}
|
|
@@ -3,6 +3,7 @@ import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import test from "node:test";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
6
7
|
import { decodeInviteString } from "@privateclaw/protocol";
|
|
7
8
|
import { createRelayServer } from "../../../services/relay-server/src/relay-server.js";
|
|
8
9
|
import { EchoBridge } from "./bridges/echo-bridge.js";
|
|
@@ -167,10 +168,12 @@ test("privateclaw command writes QR media into the OpenClaw state media director
|
|
|
167
168
|
"new sessions should default to roughly 8 hours even when relay default TTL is shorter",
|
|
168
169
|
);
|
|
169
170
|
const expectedMediaDir = path.join(stateDir, "media", "privateclaw");
|
|
170
|
-
|
|
171
|
-
|
|
171
|
+
const mediaPath =
|
|
172
|
+
process.platform === "win32" ? fileURLToPath(reply.mediaUrl) : reply.mediaUrl;
|
|
173
|
+
assert.equal(path.dirname(mediaPath), expectedMediaDir);
|
|
174
|
+
assert.equal(path.basename(mediaPath), `privateclaw-${invite.sessionId}.png`);
|
|
172
175
|
|
|
173
|
-
const qrPng = await readFile(
|
|
176
|
+
const qrPng = await readFile(mediaPath);
|
|
174
177
|
assert.deepEqual(qrPng.subarray(0, PNG_SIGNATURE.length), PNG_SIGNATURE);
|
|
175
178
|
});
|
|
176
179
|
|
|
@@ -211,6 +214,18 @@ test("privateclaw plugin local pair CLI defaults to the long session TTL", async
|
|
|
211
214
|
const pair = privateclaw.children.get("pair");
|
|
212
215
|
assert.ok(pair?.actionHandler, "plugin should register the privateclaw pair subcommand");
|
|
213
216
|
|
|
217
|
+
const stateDir = await mkdtemp(path.join(os.tmpdir(), "privateclaw-cli-state-"));
|
|
218
|
+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
|
|
219
|
+
process.env.OPENCLAW_STATE_DIR = stateDir;
|
|
220
|
+
t.after(async () => {
|
|
221
|
+
if (previousStateDir == null) {
|
|
222
|
+
delete process.env.OPENCLAW_STATE_DIR;
|
|
223
|
+
} else {
|
|
224
|
+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
|
|
225
|
+
}
|
|
226
|
+
await rm(stateDir, { recursive: true, force: true });
|
|
227
|
+
});
|
|
228
|
+
|
|
214
229
|
const printed: string[] = [];
|
|
215
230
|
const originalLog = console.log;
|
|
216
231
|
console.log = (message?: unknown, ...optionalParams: unknown[]) => {
|
|
@@ -242,6 +257,10 @@ test("privateclaw plugin local pair CLI defaults to the long session TTL", async
|
|
|
242
257
|
line.startsWith("邀请链接 / Invite URI: privateclaw://connect?payload="),
|
|
243
258
|
);
|
|
244
259
|
assert.ok(inviteUri, "pair command should print a PrivateClaw invite URI");
|
|
260
|
+
const qrPathLine = printed.find((line) =>
|
|
261
|
+
line.startsWith("二维码 PNG 路径 / QR PNG path: "),
|
|
262
|
+
);
|
|
263
|
+
assert.ok(qrPathLine, "pair command should print the saved QR PNG path");
|
|
245
264
|
const invite = decodeInviteString(
|
|
246
265
|
inviteUri.replace("邀请链接 / Invite URI: ", ""),
|
|
247
266
|
);
|
|
@@ -250,4 +269,7 @@ test("privateclaw plugin local pair CLI defaults to the long session TTL", async
|
|
|
250
269
|
remainingMs > DEFAULT_SESSION_TTL_MS - 60_000,
|
|
251
270
|
"local pair sessions should default to roughly 8 hours even when relay default TTL is shorter",
|
|
252
271
|
);
|
|
272
|
+
const qrPath = qrPathLine.replace("二维码 PNG 路径 / QR PNG path: ", "");
|
|
273
|
+
const qrPng = await readFile(qrPath);
|
|
274
|
+
assert.deepEqual(qrPng.subarray(0, PNG_SIGNATURE.length), PNG_SIGNATURE);
|
|
253
275
|
});
|
package/src/openclaw-plugin.ts
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import QRCode from "qrcode";
|
|
5
1
|
import { EchoBridge } from "./bridges/echo-bridge.js";
|
|
6
2
|
import {
|
|
7
3
|
OpenClawAgentBridge,
|
|
8
4
|
type OpenClawAgentBridgeOptions,
|
|
9
5
|
} from "./bridges/openclaw-agent-bridge.js";
|
|
6
|
+
import {
|
|
7
|
+
resolvePrivateClawMediaDir,
|
|
8
|
+
writeInviteQrPng,
|
|
9
|
+
} from "./invite-qr-files.js";
|
|
10
10
|
import { loadAvailableOpenClawCommands } from "./openclaw-command-discovery.js";
|
|
11
11
|
import { parsePositiveIntegerFlag, runPairSession } from "./pair-session.js";
|
|
12
|
-
import {
|
|
13
|
-
PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
14
|
-
PRIVATECLAW_QR_IMAGE_MARGIN,
|
|
15
|
-
PRIVATECLAW_QR_PNG_WIDTH,
|
|
16
|
-
} from "./qr-options.js";
|
|
17
12
|
import { WebhookBridge } from "./bridges/webhook-bridge.js";
|
|
18
13
|
import {
|
|
19
14
|
type OpenClawExtensionPluginCompat,
|
|
@@ -29,6 +24,7 @@ import {
|
|
|
29
24
|
buildPrivateClawCommandErrorMessage,
|
|
30
25
|
PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION,
|
|
31
26
|
PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION,
|
|
27
|
+
PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION,
|
|
32
28
|
PRIVATECLAW_CLI_PAIR_DESCRIPTION,
|
|
33
29
|
PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION,
|
|
34
30
|
PRIVATECLAW_CLI_ROOT_DESCRIPTION,
|
|
@@ -91,6 +87,7 @@ interface PrivateClawPairCliOptions {
|
|
|
91
87
|
label?: string;
|
|
92
88
|
printOnly?: boolean;
|
|
93
89
|
group?: boolean;
|
|
90
|
+
open?: boolean;
|
|
94
91
|
}
|
|
95
92
|
|
|
96
93
|
const DEFAULT_PROVIDER_LABEL = "PrivateClaw";
|
|
@@ -298,6 +295,7 @@ function normalizePairCliOptions(raw: unknown): PrivateClawPairCliOptions {
|
|
|
298
295
|
...(label ? { label } : {}),
|
|
299
296
|
...(typeof options.printOnly === "boolean" ? { printOnly: options.printOnly } : {}),
|
|
300
297
|
...(typeof options.group === "boolean" ? { group: options.group } : {}),
|
|
298
|
+
...(typeof options.open === "boolean" ? { open: options.open } : {}),
|
|
301
299
|
};
|
|
302
300
|
}
|
|
303
301
|
|
|
@@ -359,11 +357,7 @@ class PrivateClawPluginRuntime {
|
|
|
359
357
|
}
|
|
360
358
|
|
|
361
359
|
private getMediaDir(): string {
|
|
362
|
-
return
|
|
363
|
-
this.stateDir ?? process.env.OPENCLAW_STATE_DIR ?? path.join(os.homedir(), ".openclaw"),
|
|
364
|
-
"media",
|
|
365
|
-
"privateclaw",
|
|
366
|
-
);
|
|
360
|
+
return resolvePrivateClawMediaDir(this.stateDir);
|
|
367
361
|
}
|
|
368
362
|
|
|
369
363
|
private getProvider(): PrivateClawProvider {
|
|
@@ -394,10 +388,10 @@ class PrivateClawPluginRuntime {
|
|
|
394
388
|
|
|
395
389
|
async buildCommandReply(bundle: PrivateClawInviteBundle): Promise<ReplyPayloadCompat> {
|
|
396
390
|
const text = `${bundle.announcementText}\n\n${PRIVATECLAW_INVITE_URI_LABEL}:\n${bundle.inviteUri}`;
|
|
397
|
-
const
|
|
391
|
+
const qrPng = await writeInviteQrPng(bundle, this.getMediaDir());
|
|
398
392
|
return {
|
|
399
393
|
text,
|
|
400
|
-
mediaUrl:
|
|
394
|
+
mediaUrl: process.platform === "win32" ? qrPng.pngFileUrl : qrPng.pngPath,
|
|
401
395
|
};
|
|
402
396
|
}
|
|
403
397
|
|
|
@@ -406,6 +400,7 @@ class PrivateClawPluginRuntime {
|
|
|
406
400
|
label?: string;
|
|
407
401
|
printOnly?: boolean;
|
|
408
402
|
groupMode?: boolean;
|
|
403
|
+
openInBrowser?: boolean;
|
|
409
404
|
writeLine?: (line: string) => void;
|
|
410
405
|
}): Promise<PrivateClawInviteBundle> {
|
|
411
406
|
return runPairSession({
|
|
@@ -416,6 +411,10 @@ class PrivateClawPluginRuntime {
|
|
|
416
411
|
...(params?.label ? { label: params.label } : {}),
|
|
417
412
|
...(typeof params?.printOnly === "boolean" ? { printOnly: params.printOnly } : {}),
|
|
418
413
|
...(params?.groupMode === true ? { groupMode: true } : {}),
|
|
414
|
+
...(typeof params?.openInBrowser === "boolean"
|
|
415
|
+
? { openInBrowser: params.openInBrowser }
|
|
416
|
+
: {}),
|
|
417
|
+
qrMediaDir: this.getMediaDir(),
|
|
419
418
|
...(params?.writeLine ? { writeLine: params.writeLine } : {}),
|
|
420
419
|
});
|
|
421
420
|
}
|
|
@@ -430,22 +429,6 @@ class PrivateClawPluginRuntime {
|
|
|
430
429
|
}
|
|
431
430
|
}
|
|
432
431
|
|
|
433
|
-
async function writeInviteQrPng(
|
|
434
|
-
bundle: PrivateClawInviteBundle,
|
|
435
|
-
mediaDir: string,
|
|
436
|
-
): Promise<string> {
|
|
437
|
-
const pngBuffer = await QRCode.toBuffer(bundle.inviteUri, {
|
|
438
|
-
type: "png",
|
|
439
|
-
errorCorrectionLevel: PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
440
|
-
margin: PRIVATECLAW_QR_IMAGE_MARGIN,
|
|
441
|
-
width: PRIVATECLAW_QR_PNG_WIDTH,
|
|
442
|
-
});
|
|
443
|
-
await mkdir(mediaDir, { recursive: true });
|
|
444
|
-
const qrPath = path.join(mediaDir, `privateclaw-${bundle.invite.sessionId}.png`);
|
|
445
|
-
await writeFile(qrPath, pngBuffer);
|
|
446
|
-
return qrPath;
|
|
447
|
-
}
|
|
448
|
-
|
|
449
432
|
function formatCommandError(error: unknown): string {
|
|
450
433
|
return error instanceof Error ? error.message : String(error);
|
|
451
434
|
}
|
|
@@ -517,6 +500,7 @@ function createPluginDefinition(
|
|
|
517
500
|
.option("--label <label>", PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION)
|
|
518
501
|
.option("--group", PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION)
|
|
519
502
|
.option("--print-only", PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION)
|
|
503
|
+
.option("--open", PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION)
|
|
520
504
|
.action(async (rawOptions: unknown) => {
|
|
521
505
|
const options = normalizePairCliOptions(rawOptions);
|
|
522
506
|
const ttlMs = parsePositiveIntegerFlag(options.ttlMs, "--ttl-ms");
|
|
@@ -528,6 +512,9 @@ function createPluginDefinition(
|
|
|
528
512
|
...(typeof options.printOnly === "boolean"
|
|
529
513
|
? { printOnly: options.printOnly }
|
|
530
514
|
: {}),
|
|
515
|
+
...(typeof options.open === "boolean"
|
|
516
|
+
? { openInBrowser: options.open }
|
|
517
|
+
: {}),
|
|
531
518
|
writeLine: (line) => {
|
|
532
519
|
console.log(line);
|
|
533
520
|
},
|
package/src/pair-session.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
1
2
|
import type { PrivateClawProvider } from "./provider.js";
|
|
3
|
+
import {
|
|
4
|
+
resolvePrivateClawMediaDir,
|
|
5
|
+
writeInviteQrPng,
|
|
6
|
+
writeInviteQrPreviewHtml,
|
|
7
|
+
} from "./invite-qr-files.js";
|
|
2
8
|
import {
|
|
3
9
|
buildPrivateClawShutdownMessage,
|
|
4
10
|
formatBilingualInline,
|
|
5
11
|
PRIVATECLAW_INVITE_URI_LABEL,
|
|
12
|
+
PRIVATECLAW_QR_PNG_PATH_LABEL,
|
|
6
13
|
PRIVATECLAW_WAITING_FOR_APP_MESSAGE,
|
|
7
14
|
} from "./text.js";
|
|
8
15
|
import type { PrivateClawInviteBundle } from "./types.js";
|
|
@@ -13,6 +20,8 @@ export interface PairSessionOptions {
|
|
|
13
20
|
label?: string;
|
|
14
21
|
groupMode?: boolean;
|
|
15
22
|
printOnly?: boolean;
|
|
23
|
+
openInBrowser?: boolean;
|
|
24
|
+
qrMediaDir?: string;
|
|
16
25
|
writeLine?: (line: string) => void;
|
|
17
26
|
}
|
|
18
27
|
|
|
@@ -63,21 +72,41 @@ export async function runPairSession({
|
|
|
63
72
|
label,
|
|
64
73
|
groupMode = false,
|
|
65
74
|
printOnly = false,
|
|
75
|
+
openInBrowser = false,
|
|
76
|
+
qrMediaDir,
|
|
66
77
|
writeLine = (line) => {
|
|
67
78
|
console.log(line);
|
|
68
79
|
},
|
|
69
80
|
}: PairSessionOptions): Promise<PrivateClawInviteBundle> {
|
|
70
81
|
try {
|
|
71
|
-
const
|
|
82
|
+
const inviteBundle = await provider.createInviteBundle({
|
|
72
83
|
...(typeof ttlMs === "number" ? { ttlMs } : {}),
|
|
73
84
|
...(label ? { label: label.trim() } : {}),
|
|
74
85
|
...(groupMode ? { groupMode: true } : {}),
|
|
75
86
|
});
|
|
87
|
+
const qrPng = await writeInviteQrPng(
|
|
88
|
+
inviteBundle,
|
|
89
|
+
qrMediaDir ?? resolvePrivateClawMediaDir(),
|
|
90
|
+
);
|
|
91
|
+
const bundle: PrivateClawInviteBundle = {
|
|
92
|
+
...inviteBundle,
|
|
93
|
+
qrPngPath: qrPng.pngPath,
|
|
94
|
+
};
|
|
76
95
|
|
|
77
96
|
writeLine(bundle.announcementText);
|
|
78
97
|
writeLine(`${PRIVATECLAW_INVITE_URI_LABEL}: ${bundle.inviteUri}`);
|
|
98
|
+
writeLine(`${PRIVATECLAW_QR_PNG_PATH_LABEL}: ${qrPng.pngPath}`);
|
|
79
99
|
writeLine(bundle.qrTerminal);
|
|
80
100
|
|
|
101
|
+
if (openInBrowser) {
|
|
102
|
+
const preview = await writeInviteQrPreviewHtml(
|
|
103
|
+
bundle,
|
|
104
|
+
qrMediaDir ?? resolvePrivateClawMediaDir(),
|
|
105
|
+
qrPng.pngPath,
|
|
106
|
+
);
|
|
107
|
+
await openInBrowserPreview(preview.previewFileUrl);
|
|
108
|
+
}
|
|
109
|
+
|
|
81
110
|
if (printOnly) {
|
|
82
111
|
await provider.dispose();
|
|
83
112
|
return bundle;
|
|
@@ -98,3 +127,38 @@ export async function runPairSession({
|
|
|
98
127
|
throw error;
|
|
99
128
|
}
|
|
100
129
|
}
|
|
130
|
+
|
|
131
|
+
async function openInBrowserPreview(target: string): Promise<void> {
|
|
132
|
+
const command =
|
|
133
|
+
process.platform === "win32"
|
|
134
|
+
? {
|
|
135
|
+
file: "cmd.exe",
|
|
136
|
+
args: ["/d", "/s", "/c", `start "" "${target.replace(/"/gu, '""')}"`],
|
|
137
|
+
}
|
|
138
|
+
: process.platform === "darwin"
|
|
139
|
+
? { file: "open", args: [target] }
|
|
140
|
+
: { file: "xdg-open", args: [target] };
|
|
141
|
+
|
|
142
|
+
await new Promise<void>((resolve, reject) => {
|
|
143
|
+
const child = spawn(command.file, command.args, {
|
|
144
|
+
stdio: "ignore",
|
|
145
|
+
...(process.platform === "win32" ? { windowsHide: true } : {}),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
child.once("error", reject);
|
|
149
|
+
child.once("close", (code) => {
|
|
150
|
+
if (code === 0) {
|
|
151
|
+
resolve();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
reject(
|
|
155
|
+
new Error(
|
|
156
|
+
formatBilingualInline(
|
|
157
|
+
`无法打开二维码预览页(退出码 ${code ?? "unknown"})。`,
|
|
158
|
+
`Failed to open the QR preview page (exit code ${code ?? "unknown"}).`,
|
|
159
|
+
),
|
|
160
|
+
),
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
package/src/text.ts
CHANGED
|
@@ -68,11 +68,21 @@ export const PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION =
|
|
|
68
68
|
"Print the invite and QR code, then exit immediately.",
|
|
69
69
|
);
|
|
70
70
|
|
|
71
|
+
export const PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION = formatBilingualInline(
|
|
72
|
+
"生成二维码后在浏览器中打开本地预览页。",
|
|
73
|
+
"Open a local browser preview after generating the QR code.",
|
|
74
|
+
);
|
|
75
|
+
|
|
71
76
|
export const PRIVATECLAW_INVITE_URI_LABEL = formatBilingualInline(
|
|
72
77
|
"邀请链接",
|
|
73
78
|
"Invite URI",
|
|
74
79
|
);
|
|
75
80
|
|
|
81
|
+
export const PRIVATECLAW_QR_PNG_PATH_LABEL = formatBilingualInline(
|
|
82
|
+
"二维码 PNG 路径",
|
|
83
|
+
"QR PNG path",
|
|
84
|
+
);
|
|
85
|
+
|
|
76
86
|
export const PRIVATECLAW_WAITING_FOR_APP_MESSAGE = formatBilingualInline(
|
|
77
87
|
"等待 PrivateClaw App 连接,按 Ctrl+C 停止。",
|
|
78
88
|
"Waiting for the PrivateClaw app to connect. Press Ctrl+C to stop.",
|