@privateclaw/privateclaw 0.1.2 → 0.1.3
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/provider.js +3 -6
- package/dist/provider.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/provider.ts +2 -6
- package/src/qr-terminal-render.test.ts +26 -0
- package/src/qr-terminal-render.ts +66 -0
- package/src/text.ts +10 -0
- package/src/types.ts +1 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PrivateClawAttachment } from "@privateclaw/protocol";
|
|
2
|
-
import {
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import fs from "node:fs/promises";
|
|
5
5
|
import os from "node:os";
|
|
@@ -12,6 +12,8 @@ type OpenClawThinkingLevel = "off" | "minimal" | "low" | "medium" | "high";
|
|
|
12
12
|
|
|
13
13
|
interface OpenClawAgentBridgeExecOptions {
|
|
14
14
|
maxBuffer: number;
|
|
15
|
+
shell?: boolean;
|
|
16
|
+
windowsHide?: boolean;
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
type OpenClawAgentExecFile = (
|
|
@@ -91,6 +93,12 @@ export interface OpenClawAgentBridgeOptions {
|
|
|
91
93
|
execFileImpl?: OpenClawAgentExecFile;
|
|
92
94
|
}
|
|
93
95
|
|
|
96
|
+
interface OpenClawLaunchCommand {
|
|
97
|
+
readonly file: string;
|
|
98
|
+
readonly args: string[];
|
|
99
|
+
readonly shell: boolean;
|
|
100
|
+
}
|
|
101
|
+
|
|
94
102
|
const DEFAULT_MAX_BUFFER = 10 * 1024 * 1024;
|
|
95
103
|
const DEFAULT_STATE_DIR = process.env.OPENCLAW_STATE_DIR ?? path.join(os.homedir(), ".openclaw");
|
|
96
104
|
const DEFAULT_WORKSPACE_DIR = path.join(DEFAULT_STATE_DIR, "workspace");
|
|
@@ -117,6 +125,39 @@ export function parseOpenClawAgentOutput(stdout: string): BridgeResponse {
|
|
|
117
125
|
return parseOpenClawAgentResult(parsed);
|
|
118
126
|
}
|
|
119
127
|
|
|
128
|
+
export function resolveOpenClawLaunchCommand(options: {
|
|
129
|
+
executable?: string;
|
|
130
|
+
platform?: NodeJS.Platform;
|
|
131
|
+
nodeExecutable?: string;
|
|
132
|
+
processArgv?: string[];
|
|
133
|
+
} = {}): OpenClawLaunchCommand {
|
|
134
|
+
const platform = options.platform ?? process.platform;
|
|
135
|
+
const executable = options.executable?.trim();
|
|
136
|
+
if (executable) {
|
|
137
|
+
return {
|
|
138
|
+
file: executable,
|
|
139
|
+
args: [],
|
|
140
|
+
shell: shouldUseShellForExecutable(executable, platform),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const processArgv = options.processArgv ?? process.argv;
|
|
145
|
+
const currentCliScript = processArgv[1];
|
|
146
|
+
if (looksLikeOpenClawCliScript(currentCliScript)) {
|
|
147
|
+
return {
|
|
148
|
+
file: options.nodeExecutable ?? process.execPath,
|
|
149
|
+
args: [currentCliScript],
|
|
150
|
+
shell: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
file: "openclaw",
|
|
156
|
+
args: [],
|
|
157
|
+
shell: platform === "win32",
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
120
161
|
function parseOpenClawAgentJson(stdout: string): OpenClawAgentJsonResult {
|
|
121
162
|
return JSON.parse(stdout) as OpenClawAgentJsonResult;
|
|
122
163
|
}
|
|
@@ -152,13 +193,20 @@ function extractDisplayMessages(parsed: OpenClawAgentJsonResult): NormalizedBrid
|
|
|
152
193
|
|
|
153
194
|
export class OpenClawAgentBridge implements PrivateClawAgentBridge {
|
|
154
195
|
private readonly executable: string;
|
|
196
|
+
private readonly executableArgs: string[];
|
|
197
|
+
private readonly executableUsesShell: boolean;
|
|
155
198
|
private readonly execFileImpl: OpenClawAgentExecFile;
|
|
156
199
|
private readonly stateDir: string;
|
|
157
200
|
private readonly workspaceDir: string;
|
|
158
201
|
|
|
159
202
|
constructor(private readonly options: OpenClawAgentBridgeOptions = {}) {
|
|
160
|
-
|
|
161
|
-
|
|
203
|
+
const launchCommand = resolveOpenClawLaunchCommand({
|
|
204
|
+
...(options.executable ? { executable: options.executable } : {}),
|
|
205
|
+
});
|
|
206
|
+
this.executable = launchCommand.file;
|
|
207
|
+
this.executableArgs = launchCommand.args;
|
|
208
|
+
this.executableUsesShell = launchCommand.shell;
|
|
209
|
+
this.execFileImpl = options.execFileImpl ?? defaultExecFileImpl;
|
|
162
210
|
this.stateDir = options.stateDir ?? DEFAULT_STATE_DIR;
|
|
163
211
|
this.workspaceDir = options.workspaceDir ?? path.join(this.stateDir, "workspace");
|
|
164
212
|
}
|
|
@@ -406,10 +454,22 @@ export class OpenClawAgentBridge implements PrivateClawAgentBridge {
|
|
|
406
454
|
return new Promise((resolve, reject) => {
|
|
407
455
|
this.execFileImpl(
|
|
408
456
|
this.executable,
|
|
409
|
-
args,
|
|
410
|
-
{
|
|
457
|
+
[...this.executableArgs, ...args],
|
|
458
|
+
{
|
|
459
|
+
maxBuffer: DEFAULT_MAX_BUFFER,
|
|
460
|
+
...(this.executableUsesShell ? { shell: true } : {}),
|
|
461
|
+
...(process.platform === "win32" ? { windowsHide: true } : {}),
|
|
462
|
+
},
|
|
411
463
|
(error, stdout, stderr) => {
|
|
412
464
|
if (error) {
|
|
465
|
+
if (isNodeError(error) && error.code === "ENOENT") {
|
|
466
|
+
reject(
|
|
467
|
+
new Error(
|
|
468
|
+
`Failed to run OpenClaw agent bridge: OpenClaw CLI was not found (${this.executable}). Set openclawAgentExecutable or PRIVATECLAW_OPENCLAW_AGENT_BIN if OpenClaw is not on PATH.`,
|
|
469
|
+
),
|
|
470
|
+
);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
413
473
|
const suffix = stderr.trim() !== "" ? ` ${stderr.trim()}` : "";
|
|
414
474
|
reject(new Error(`Failed to run OpenClaw agent bridge:${suffix || ` ${error.message}`}`));
|
|
415
475
|
return;
|
|
@@ -422,6 +482,96 @@ export class OpenClawAgentBridge implements PrivateClawAgentBridge {
|
|
|
422
482
|
}
|
|
423
483
|
}
|
|
424
484
|
|
|
485
|
+
function defaultExecFileImpl(
|
|
486
|
+
file: string,
|
|
487
|
+
args: string[],
|
|
488
|
+
options: OpenClawAgentBridgeExecOptions,
|
|
489
|
+
callback: (error: Error | null, stdout: string, stderr: string) => void,
|
|
490
|
+
): void {
|
|
491
|
+
let finished = false;
|
|
492
|
+
let stdout = "";
|
|
493
|
+
let stderr = "";
|
|
494
|
+
let bufferedBytes = 0;
|
|
495
|
+
|
|
496
|
+
const finish = (error: Error | null) => {
|
|
497
|
+
if (finished) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
finished = true;
|
|
501
|
+
callback(error, stdout, stderr);
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const child = spawn(file, args, {
|
|
505
|
+
shell: options.shell,
|
|
506
|
+
...(typeof options.windowsHide === "boolean" ? { windowsHide: options.windowsHide } : {}),
|
|
507
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
const append = (chunk: Buffer | string, target: "stdout" | "stderr") => {
|
|
511
|
+
const text = typeof chunk === "string" ? chunk : chunk.toString("utf8");
|
|
512
|
+
bufferedBytes += Buffer.byteLength(text);
|
|
513
|
+
if (bufferedBytes > options.maxBuffer) {
|
|
514
|
+
child.kill();
|
|
515
|
+
finish(new Error(`OpenClaw agent bridge output exceeded ${options.maxBuffer} bytes.`));
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (target === "stdout") {
|
|
519
|
+
stdout += text;
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
stderr += text;
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
child.stdout?.on("data", (chunk) => {
|
|
526
|
+
append(chunk, "stdout");
|
|
527
|
+
});
|
|
528
|
+
child.stderr?.on("data", (chunk) => {
|
|
529
|
+
append(chunk, "stderr");
|
|
530
|
+
});
|
|
531
|
+
child.once("error", (error) => {
|
|
532
|
+
finish(error instanceof Error ? error : new Error(String(error)));
|
|
533
|
+
});
|
|
534
|
+
child.once("close", (code, signal) => {
|
|
535
|
+
if (finished) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (code === 0) {
|
|
539
|
+
finish(null);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
finish(
|
|
543
|
+
new Error(
|
|
544
|
+
signal
|
|
545
|
+
? `OpenClaw agent bridge exited via ${signal}.`
|
|
546
|
+
: `OpenClaw agent bridge exited with code ${code ?? "unknown"}.`,
|
|
547
|
+
),
|
|
548
|
+
);
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function looksLikeOpenClawCliScript(value: string | undefined): value is string {
|
|
553
|
+
if (!value) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
const normalized = value.replace(/\\/gu, "/").toLowerCase();
|
|
557
|
+
return (
|
|
558
|
+
normalized.includes("/node_modules/openclaw/") ||
|
|
559
|
+
normalized.endsWith("/openclaw") ||
|
|
560
|
+
normalized.endsWith("/openclaw.js") ||
|
|
561
|
+
normalized.endsWith("/openclaw.cjs")
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function shouldUseShellForExecutable(
|
|
566
|
+
executable: string,
|
|
567
|
+
platform: NodeJS.Platform,
|
|
568
|
+
): boolean {
|
|
569
|
+
if (platform !== "win32") {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
return !/\.(?:exe|com)$/iu.test(path.basename(executable));
|
|
573
|
+
}
|
|
574
|
+
|
|
425
575
|
function buildNoDisplayPayloadNotice(
|
|
426
576
|
message: string,
|
|
427
577
|
parsed: OpenClawAgentJsonResult,
|
package/src/cli.ts
CHANGED
|
@@ -62,6 +62,7 @@ const ttlMs = parsePositiveInteger(
|
|
|
62
62
|
process.env.PRIVATECLAW_SESSION_TTL_MS,
|
|
63
63
|
DEFAULT_SESSION_TTL_MS,
|
|
64
64
|
);
|
|
65
|
+
const openQr = parseBooleanFlag(process.env.PRIVATECLAW_OPEN_QR);
|
|
65
66
|
const providerLabel = process.env.PRIVATECLAW_PROVIDER_LABEL?.trim() || "PrivateClaw";
|
|
66
67
|
const welcomeMessage = process.env.PRIVATECLAW_WELCOME_MESSAGE?.trim();
|
|
67
68
|
|
|
@@ -101,6 +102,7 @@ const provider = new PrivateClawProvider({
|
|
|
101
102
|
await runPairSession({
|
|
102
103
|
provider,
|
|
103
104
|
ttlMs,
|
|
105
|
+
...(openQr ? { openInBrowser: true } : {}),
|
|
104
106
|
writeLine: (line) => {
|
|
105
107
|
console.log(line);
|
|
106
108
|
},
|
|
@@ -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
|
+
}
|