@privateclaw/privateclaw 0.1.1 → 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 +18 -5
- 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-command-discovery.d.ts +1 -0
- package/dist/openclaw-command-discovery.js +67 -14
- package/dist/openclaw-command-discovery.js.map +1 -1
- 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 +18 -9
- 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/openclaw.plugin.json +1 -1
- 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-command-discovery.test.ts +108 -0
- package/src/openclaw-command-discovery.ts +119 -19
- 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.test.ts +10 -0
- package/src/provider.ts +26 -11
- 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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import {
|
|
7
|
+
loadAvailableOpenClawCommands,
|
|
8
|
+
resetOpenClawCommandDiscoveryForTests,
|
|
9
|
+
} from "./openclaw-command-discovery.js";
|
|
10
|
+
|
|
11
|
+
test("loadAvailableOpenClawCommands discovers built-in, skill, and plugin commands from hashed OpenClaw chunks", async (t) => {
|
|
12
|
+
const openClawRoot = await mkdtemp(
|
|
13
|
+
path.join(os.tmpdir(), "privateclaw-openclaw-root-"),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
t.after(async () => {
|
|
17
|
+
delete process.env.OPENCLAW_PACKAGE_ROOT;
|
|
18
|
+
resetOpenClawCommandDiscoveryForTests();
|
|
19
|
+
await rm(openClawRoot, { recursive: true, force: true });
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
await mkdir(path.join(openClawRoot, "dist"), { recursive: true });
|
|
23
|
+
await writeFile(
|
|
24
|
+
path.join(openClawRoot, "package.json"),
|
|
25
|
+
JSON.stringify({
|
|
26
|
+
name: "openclaw",
|
|
27
|
+
type: "module",
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
await writeFile(
|
|
31
|
+
path.join(openClawRoot, "dist", "index.js"),
|
|
32
|
+
'import "./reply-test.js";\nimport "./registry-test.js";\n',
|
|
33
|
+
);
|
|
34
|
+
await writeFile(
|
|
35
|
+
path.join(openClawRoot, "dist", "reply-test.js"),
|
|
36
|
+
[
|
|
37
|
+
'const BASE_COMMANDS = [',
|
|
38
|
+
' {',
|
|
39
|
+
' key: "help",',
|
|
40
|
+
' description: "Show help",',
|
|
41
|
+
' textAliases: ["/help"],',
|
|
42
|
+
' acceptsArgs: false,',
|
|
43
|
+
' },',
|
|
44
|
+
' {',
|
|
45
|
+
' key: "bash",',
|
|
46
|
+
' description: "Run bash",',
|
|
47
|
+
' textAliases: ["/bash"],',
|
|
48
|
+
' acceptsArgs: true,',
|
|
49
|
+
' },',
|
|
50
|
+
"];",
|
|
51
|
+
"function buildCommands(params) {",
|
|
52
|
+
" return [...BASE_COMMANDS, ...(params?.skillCommands ?? []).map((spec) => ({",
|
|
53
|
+
' key: `skill:${spec.skillName ?? spec.name}`,',
|
|
54
|
+
" description: spec.description,",
|
|
55
|
+
' textAliases: [`/${spec.name}`],',
|
|
56
|
+
" acceptsArgs: true,",
|
|
57
|
+
" }))];",
|
|
58
|
+
"}",
|
|
59
|
+
"export const cfg = function loadConfig() {",
|
|
60
|
+
" return { disableBash: true };",
|
|
61
|
+
"};",
|
|
62
|
+
"export const skills = function listSkillCommandsForAgents() {",
|
|
63
|
+
' return [{ name: "diagram", description: "Render a diagram", skillName: "diagram" }];',
|
|
64
|
+
"};",
|
|
65
|
+
"export const list = function listChatCommands(params) {",
|
|
66
|
+
" return buildCommands(params);",
|
|
67
|
+
"};",
|
|
68
|
+
"export const filtered = function listChatCommandsForConfig(config, params) {",
|
|
69
|
+
" const commands = buildCommands(params);",
|
|
70
|
+
" return config?.disableBash",
|
|
71
|
+
' ? commands.filter((command) => command.key !== "bash")',
|
|
72
|
+
" : commands;",
|
|
73
|
+
"};",
|
|
74
|
+
"",
|
|
75
|
+
].join("\n"),
|
|
76
|
+
);
|
|
77
|
+
await writeFile(
|
|
78
|
+
path.join(openClawRoot, "dist", "registry-test.js"),
|
|
79
|
+
[
|
|
80
|
+
"export const pluginSpecs = function getPluginCommandSpecs() {",
|
|
81
|
+
" return [",
|
|
82
|
+
' { name: "memory", description: "Search memory", acceptsArgs: true },',
|
|
83
|
+
" ];",
|
|
84
|
+
"};",
|
|
85
|
+
"",
|
|
86
|
+
].join("\n"),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
process.env.OPENCLAW_PACKAGE_ROOT = openClawRoot;
|
|
90
|
+
resetOpenClawCommandDiscoveryForTests();
|
|
91
|
+
|
|
92
|
+
const commands = await loadAvailableOpenClawCommands();
|
|
93
|
+
const slashes = commands.map((command) => command.slash);
|
|
94
|
+
|
|
95
|
+
assert.deepEqual(slashes, ["/diagram", "/help", "/memory"]);
|
|
96
|
+
assert.equal(
|
|
97
|
+
commands.find((command) => command.slash === "/memory")?.source,
|
|
98
|
+
"plugin",
|
|
99
|
+
);
|
|
100
|
+
assert.equal(
|
|
101
|
+
commands.find((command) => command.slash === "/diagram")?.acceptsArgs,
|
|
102
|
+
true,
|
|
103
|
+
);
|
|
104
|
+
assert.ok(
|
|
105
|
+
commands.every((command) => command.slash !== "/bash"),
|
|
106
|
+
"config-aware discovery should respect disabled commands",
|
|
107
|
+
);
|
|
108
|
+
});
|
|
@@ -21,9 +21,25 @@ interface OpenClawPluginCommandSpecLike {
|
|
|
21
21
|
acceptsArgs?: unknown;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
interface OpenClawSkillCommandSpecLike {
|
|
25
|
+
name?: unknown;
|
|
26
|
+
description?: unknown;
|
|
27
|
+
skillName?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
24
30
|
interface OpenClawCommandRuntime {
|
|
25
|
-
listChatCommands: (
|
|
26
|
-
|
|
31
|
+
listChatCommands: (
|
|
32
|
+
params?: { skillCommands?: OpenClawSkillCommandSpecLike[] },
|
|
33
|
+
) => OpenClawCommandDefinitionLike[];
|
|
34
|
+
listChatCommandsForConfig?: (
|
|
35
|
+
config: unknown,
|
|
36
|
+
params?: { skillCommands?: OpenClawSkillCommandSpecLike[] },
|
|
37
|
+
) => OpenClawCommandDefinitionLike[];
|
|
38
|
+
loadConfig?: () => unknown;
|
|
39
|
+
listSkillCommandsForAgents?: (
|
|
40
|
+
config: unknown,
|
|
41
|
+
) => OpenClawSkillCommandSpecLike[];
|
|
42
|
+
getPluginCommandSpecs: (provider?: string) => OpenClawPluginCommandSpecLike[];
|
|
27
43
|
}
|
|
28
44
|
|
|
29
45
|
let cachedRuntimePromise: Promise<OpenClawCommandRuntime | null> | undefined;
|
|
@@ -211,6 +227,18 @@ async function resolveHashedModulePath(params: {
|
|
|
211
227
|
}
|
|
212
228
|
}
|
|
213
229
|
|
|
230
|
+
function findExportedFunction<T extends (...args: never[]) => unknown>(
|
|
231
|
+
module: Record<string, unknown>,
|
|
232
|
+
functionName: string,
|
|
233
|
+
): T | null {
|
|
234
|
+
for (const exported of Object.values(module)) {
|
|
235
|
+
if (typeof exported === "function" && exported.name === functionName) {
|
|
236
|
+
return exported as T;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
214
242
|
async function loadRuntime(): Promise<OpenClawCommandRuntime | null> {
|
|
215
243
|
if (!cachedRuntimePromise) {
|
|
216
244
|
cachedRuntimePromise = (async () => {
|
|
@@ -220,12 +248,11 @@ async function loadRuntime(): Promise<OpenClawCommandRuntime | null> {
|
|
|
220
248
|
}
|
|
221
249
|
|
|
222
250
|
const distDir = path.join(openClawRoot, "dist");
|
|
223
|
-
const
|
|
224
|
-
const [commandsRegistryPath, registryPath] = await Promise.all([
|
|
251
|
+
const [replyPath, registryPath] = await Promise.all([
|
|
225
252
|
resolveHashedModulePath({
|
|
226
|
-
distDir
|
|
253
|
+
distDir,
|
|
227
254
|
entryFile: "index.js",
|
|
228
|
-
prefix: "
|
|
255
|
+
prefix: "reply-",
|
|
229
256
|
}),
|
|
230
257
|
resolveHashedModulePath({
|
|
231
258
|
distDir,
|
|
@@ -234,28 +261,70 @@ async function loadRuntime(): Promise<OpenClawCommandRuntime | null> {
|
|
|
234
261
|
}),
|
|
235
262
|
]);
|
|
236
263
|
|
|
237
|
-
if (!
|
|
264
|
+
if (!replyPath || !registryPath) {
|
|
238
265
|
return null;
|
|
239
266
|
}
|
|
240
267
|
|
|
241
|
-
const [
|
|
242
|
-
import(pathToFileURL(
|
|
268
|
+
const [replyModule, registryModule] = await Promise.all([
|
|
269
|
+
import(pathToFileURL(replyPath).href) as Promise<Record<string, unknown>>,
|
|
243
270
|
import(pathToFileURL(registryPath).href) as Promise<Record<string, unknown>>,
|
|
244
271
|
]);
|
|
245
272
|
|
|
246
|
-
const listChatCommands =
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
273
|
+
const listChatCommands = findExportedFunction<
|
|
274
|
+
(params?: {
|
|
275
|
+
skillCommands?: OpenClawSkillCommandSpecLike[];
|
|
276
|
+
}) => OpenClawCommandDefinitionLike[]
|
|
277
|
+
>(replyModule, "listChatCommands");
|
|
278
|
+
const listChatCommandsForConfig = findExportedFunction<
|
|
279
|
+
(
|
|
280
|
+
config: unknown,
|
|
281
|
+
params?: { skillCommands?: OpenClawSkillCommandSpecLike[] },
|
|
282
|
+
) => OpenClawCommandDefinitionLike[]
|
|
283
|
+
>(replyModule, "listChatCommandsForConfig");
|
|
284
|
+
const loadConfig = findExportedFunction<() => unknown>(
|
|
285
|
+
replyModule,
|
|
286
|
+
"loadConfig",
|
|
287
|
+
);
|
|
288
|
+
const listSkillCommandsForAgents = findExportedFunction<
|
|
289
|
+
(config: unknown) => OpenClawSkillCommandSpecLike[]
|
|
290
|
+
>(replyModule, "listSkillCommandsForAgents");
|
|
291
|
+
const getPluginCommandSpecs = findExportedFunction<
|
|
292
|
+
(provider?: string) => OpenClawPluginCommandSpecLike[]
|
|
293
|
+
>(registryModule, "getPluginCommandSpecs");
|
|
294
|
+
|
|
295
|
+
if (!listChatCommands || !getPluginCommandSpecs) {
|
|
252
296
|
return null;
|
|
253
297
|
}
|
|
254
298
|
|
|
255
299
|
return {
|
|
256
|
-
listChatCommands: () =>
|
|
257
|
-
|
|
258
|
-
|
|
300
|
+
listChatCommands: (params) =>
|
|
301
|
+
listChatCommands(params) as OpenClawCommandDefinitionLike[],
|
|
302
|
+
...(listChatCommandsForConfig
|
|
303
|
+
? {
|
|
304
|
+
listChatCommandsForConfig: (config: unknown, params?: {
|
|
305
|
+
skillCommands?: OpenClawSkillCommandSpecLike[];
|
|
306
|
+
}) =>
|
|
307
|
+
listChatCommandsForConfig(
|
|
308
|
+
config,
|
|
309
|
+
params,
|
|
310
|
+
) as OpenClawCommandDefinitionLike[],
|
|
311
|
+
}
|
|
312
|
+
: {}),
|
|
313
|
+
...(loadConfig
|
|
314
|
+
? {
|
|
315
|
+
loadConfig: () => loadConfig(),
|
|
316
|
+
}
|
|
317
|
+
: {}),
|
|
318
|
+
...(listSkillCommandsForAgents
|
|
319
|
+
? {
|
|
320
|
+
listSkillCommandsForAgents: (config: unknown) =>
|
|
321
|
+
listSkillCommandsForAgents(
|
|
322
|
+
config,
|
|
323
|
+
) as OpenClawSkillCommandSpecLike[],
|
|
324
|
+
}
|
|
325
|
+
: {}),
|
|
326
|
+
getPluginCommandSpecs: (provider?: string) =>
|
|
327
|
+
getPluginCommandSpecs(provider) as OpenClawPluginCommandSpecLike[],
|
|
259
328
|
};
|
|
260
329
|
})();
|
|
261
330
|
}
|
|
@@ -269,8 +338,39 @@ export async function loadAvailableOpenClawCommands(): Promise<PrivateClawSlashC
|
|
|
269
338
|
return [];
|
|
270
339
|
}
|
|
271
340
|
|
|
341
|
+
let config: unknown;
|
|
342
|
+
let skillCommands: OpenClawSkillCommandSpecLike[] | undefined;
|
|
343
|
+
|
|
344
|
+
if (runtime.loadConfig) {
|
|
345
|
+
try {
|
|
346
|
+
config = runtime.loadConfig();
|
|
347
|
+
} catch {
|
|
348
|
+
config = undefined;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (config !== undefined && runtime.listSkillCommandsForAgents) {
|
|
353
|
+
try {
|
|
354
|
+
skillCommands = runtime.listSkillCommandsForAgents(config);
|
|
355
|
+
} catch {
|
|
356
|
+
skillCommands = undefined;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const listParams = skillCommands?.length ? { skillCommands } : undefined;
|
|
361
|
+
let builtInCommands: OpenClawCommandDefinitionLike[];
|
|
362
|
+
if (config !== undefined && runtime.listChatCommandsForConfig) {
|
|
363
|
+
builtInCommands = runtime.listChatCommandsForConfig(config, listParams);
|
|
364
|
+
} else {
|
|
365
|
+
builtInCommands = runtime.listChatCommands(listParams);
|
|
366
|
+
}
|
|
367
|
+
|
|
272
368
|
return dedupeCommands([
|
|
273
|
-
...mapBuiltInCommands(
|
|
369
|
+
...mapBuiltInCommands(builtInCommands),
|
|
274
370
|
...mapPluginCommands(runtime.getPluginCommandSpecs()),
|
|
275
371
|
]);
|
|
276
372
|
}
|
|
373
|
+
|
|
374
|
+
export function resetOpenClawCommandDiscoveryForTests(): void {
|
|
375
|
+
cachedRuntimePromise = undefined;
|
|
376
|
+
}
|
|
@@ -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
|
},
|