@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.
Files changed (43) hide show
  1. package/README.md +18 -5
  2. package/dist/bridges/openclaw-agent-bridge.d.ts +15 -0
  3. package/dist/bridges/openclaw-agent-bridge.js +113 -4
  4. package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
  5. package/dist/cli.js +2 -0
  6. package/dist/cli.js.map +1 -1
  7. package/dist/invite-qr-files.d.ts +12 -0
  8. package/dist/invite-qr-files.js +124 -0
  9. package/dist/invite-qr-files.js.map +1 -0
  10. package/dist/openclaw-command-discovery.d.ts +1 -0
  11. package/dist/openclaw-command-discovery.js +67 -14
  12. package/dist/openclaw-command-discovery.js.map +1 -1
  13. package/dist/openclaw-plugin.js +14 -21
  14. package/dist/openclaw-plugin.js.map +1 -1
  15. package/dist/pair-session.d.ts +3 -1
  16. package/dist/pair-session.js +39 -3
  17. package/dist/pair-session.js.map +1 -1
  18. package/dist/provider.js +18 -9
  19. package/dist/provider.js.map +1 -1
  20. package/dist/qr-terminal-render.d.ts +7 -0
  21. package/dist/qr-terminal-render.js +44 -0
  22. package/dist/qr-terminal-render.js.map +1 -0
  23. package/dist/text.d.ts +2 -0
  24. package/dist/text.js +2 -0
  25. package/dist/text.js.map +1 -1
  26. package/dist/types.d.ts +1 -0
  27. package/openclaw.plugin.json +1 -1
  28. package/package.json +1 -1
  29. package/src/bridges/openclaw-agent-bridge.test.ts +46 -0
  30. package/src/bridges/openclaw-agent-bridge.ts +155 -5
  31. package/src/cli.ts +2 -0
  32. package/src/invite-qr-files.ts +156 -0
  33. package/src/openclaw-command-discovery.test.ts +108 -0
  34. package/src/openclaw-command-discovery.ts +119 -19
  35. package/src/openclaw-plugin.test.ts +25 -3
  36. package/src/openclaw-plugin.ts +19 -32
  37. package/src/pair-session.ts +65 -1
  38. package/src/provider.test.ts +10 -0
  39. package/src/provider.ts +26 -11
  40. package/src/qr-terminal-render.test.ts +26 -0
  41. package/src/qr-terminal-render.ts +66 -0
  42. package/src/text.ts +10 -0
  43. package/src/types.ts +1 -0
@@ -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 bundle = await provider.createInviteBundle({
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
+ }
@@ -610,7 +610,12 @@ test("group sessions can mute and unmute bot replies without stopping participan
610
610
  ]);
611
611
  const muteNotice = decryptRelayPayload(muteAppOneFrames[0]!, invite);
612
612
  assert.equal(muteNotice.kind, "system_message");
613
+ assert.equal(muteNotice.replyTo, "mute-bot-1");
613
614
  assert.match(muteNotice.message, /暂停机器人|pause(?:d)? bot replies/i);
615
+ const peerMuteNotice = decryptRelayPayload(muteAppTwoFrames[0]!, invite);
616
+ assert.equal(peerMuteNotice.kind, "system_message");
617
+ assert.equal(peerMuteNotice.replyTo, undefined);
618
+ assert.equal(peerMuteNotice.messageId, muteNotice.messageId);
614
619
  const muteCapabilities = decryptRelayPayload(muteAppOneFrames[1]!, invite);
615
620
  assert.equal(muteCapabilities.kind, "provider_capabilities");
616
621
  assert.equal(muteCapabilities.botMuted, true);
@@ -684,8 +689,13 @@ test("group sessions can mute and unmute bot replies without stopping participan
684
689
  unmuteAppOneFramesPromise,
685
690
  unmuteAppTwoFramesPromise,
686
691
  ]);
692
+ const peerUnmuteNotice = decryptRelayPayload(unmuteAppOneFrames[0]!, invite);
693
+ assert.equal(peerUnmuteNotice.kind, "system_message");
694
+ assert.equal(peerUnmuteNotice.replyTo, undefined);
687
695
  const unmuteNotice = decryptRelayPayload(unmuteAppTwoFrames[0]!, invite);
688
696
  assert.equal(unmuteNotice.kind, "system_message");
697
+ assert.equal(unmuteNotice.replyTo, "unmute-bot-1");
698
+ assert.equal(unmuteNotice.messageId, peerUnmuteNotice.messageId);
689
699
  assert.match(unmuteNotice.message, /恢复机器人|resume(?:d)? bot replies/i);
690
700
  const unmuteCapabilities = decryptRelayPayload(unmuteAppOneFrames[1]!, invite);
691
701
  assert.equal(unmuteCapabilities.kind, "provider_capabilities");
package/src/provider.ts CHANGED
@@ -14,8 +14,8 @@ import QRCode from "qrcode";
14
14
  import {
15
15
  PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
16
16
  PRIVATECLAW_QR_SVG_MARGIN,
17
- PRIVATECLAW_QR_TERMINAL_MARGIN,
18
17
  } from "./qr-options.js";
18
+ import { renderTerminalQr } from "./qr-terminal-render.js";
19
19
  import { RelayProviderClient } from "./relay-provider-client.js";
20
20
  import {
21
21
  buildInviteAnnouncementText,
@@ -682,12 +682,13 @@ export class PrivateClawProvider {
682
682
  excludeAppId?: string;
683
683
  severity?: "info" | "error";
684
684
  sentAt?: string;
685
+ messageId?: string;
685
686
  },
686
687
  ): Promise<void> {
687
688
  const session = this.requireSession(sessionId);
688
689
  const sentAt = params?.sentAt ?? nowIso();
689
690
  const severity = params?.severity ?? "info";
690
- const messageId = buildMessageId("system");
691
+ const messageId = params?.messageId ?? buildMessageId("system");
691
692
  const targetAppIds = [...session.participants.keys()].filter(
692
693
  (appId) => appId !== params?.excludeAppId,
693
694
  );
@@ -1118,12 +1119,30 @@ export class PrivateClawProvider {
1118
1119
  }
1119
1120
 
1120
1121
  session.botMuted = shouldMute;
1122
+ const messageId = buildMessageId("system");
1123
+ const message = shouldMute
1124
+ ? buildBotMutedMessage(participant.displayName)
1125
+ : buildBotUnmutedMessage(participant.displayName);
1126
+ await this.sendSystemMessage(
1127
+ sessionId,
1128
+ message,
1129
+ "info",
1130
+ payload.clientMessageId,
1131
+ {
1132
+ messageId,
1133
+ sentAt: payload.sentAt,
1134
+ targetAppId: participant.appId,
1135
+ storeHistory: false,
1136
+ },
1137
+ );
1121
1138
  await this.sendSystemMessageToGroupParticipants(
1122
1139
  sessionId,
1123
- shouldMute
1124
- ? buildBotMutedMessage(participant.displayName)
1125
- : buildBotUnmutedMessage(participant.displayName),
1126
- { sentAt: payload.sentAt },
1140
+ message,
1141
+ {
1142
+ excludeAppId: participant.appId,
1143
+ sentAt: payload.sentAt,
1144
+ messageId,
1145
+ },
1127
1146
  );
1128
1147
  await this.sendCapabilities(sessionId);
1129
1148
  return;
@@ -1292,11 +1311,7 @@ export class PrivateClawProvider {
1292
1311
  errorCorrectionLevel: PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
1293
1312
  margin: PRIVATECLAW_QR_SVG_MARGIN,
1294
1313
  });
1295
- const qrTerminal = await QRCode.toString(inviteUri, {
1296
- type: "terminal",
1297
- margin: PRIVATECLAW_QR_TERMINAL_MARGIN,
1298
- small: true,
1299
- });
1314
+ const qrTerminal = renderTerminalQr(inviteUri);
1300
1315
 
1301
1316
  return {
1302
1317
  invite,
@@ -0,0 +1,26 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { renderQrModulesAsBraille } from "./qr-terminal-render.js";
4
+
5
+ test("renderQrModulesAsBraille packs QR modules into dense braille cells", () => {
6
+ const modules = {
7
+ size: 4,
8
+ get(x: number, y: number): boolean {
9
+ const pattern = [
10
+ [true, false, true, false],
11
+ [false, true, false, true],
12
+ [true, false, true, false],
13
+ [false, true, false, true],
14
+ ];
15
+ return pattern[y]?.[x] ?? false;
16
+ },
17
+ };
18
+
19
+ const rendered = renderQrModulesAsBraille(modules, 0);
20
+ const lines = rendered.split("\n");
21
+
22
+ assert.equal(lines.length, 1);
23
+ assert.match(lines[0], /\u001b\[47m\u001b\[30m/u);
24
+ assert.match(lines[0], /[⢕⡪]/u);
25
+ assert.ok(lines[0].length < 20, "braille rendering should stay compact");
26
+ });
@@ -0,0 +1,66 @@
1
+ import QRCode from "qrcode";
2
+ import {
3
+ PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
4
+ PRIVATECLAW_QR_TERMINAL_MARGIN,
5
+ } from "./qr-options.js";
6
+
7
+ const ANSI_QR_PREFIX = "\u001b[47m\u001b[30m";
8
+ const ANSI_QR_SUFFIX = "\u001b[0m";
9
+ const BRAILLE_BASE = 0x2800;
10
+
11
+ interface QrModules {
12
+ readonly size: number;
13
+ get(x: number, y: number): boolean | number;
14
+ }
15
+
16
+ export function renderTerminalQr(value: string): string {
17
+ const qr = QRCode.create(value, {
18
+ errorCorrectionLevel: PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
19
+ });
20
+ return renderQrModulesAsBraille(qr.modules, PRIVATECLAW_QR_TERMINAL_MARGIN);
21
+ }
22
+
23
+ export function renderQrModulesAsBraille(modules: QrModules, margin = 0): string {
24
+ const width = modules.size + margin * 2;
25
+ const height = modules.size + margin * 2;
26
+ const lines: string[] = [];
27
+
28
+ for (let y = 0; y < height; y += 4) {
29
+ let line = "";
30
+ for (let x = 0; x < width; x += 2) {
31
+ const bits =
32
+ (readModule(modules, x, y, margin) ? 0x01 : 0) |
33
+ (readModule(modules, x, y + 1, margin) ? 0x02 : 0) |
34
+ (readModule(modules, x, y + 2, margin) ? 0x04 : 0) |
35
+ (readModule(modules, x + 1, y, margin) ? 0x08 : 0) |
36
+ (readModule(modules, x + 1, y + 1, margin) ? 0x10 : 0) |
37
+ (readModule(modules, x + 1, y + 2, margin) ? 0x20 : 0) |
38
+ (readModule(modules, x, y + 3, margin) ? 0x40 : 0) |
39
+ (readModule(modules, x + 1, y + 3, margin) ? 0x80 : 0);
40
+
41
+ line += bits === 0 ? " " : String.fromCodePoint(BRAILLE_BASE + bits);
42
+ }
43
+ lines.push(`${ANSI_QR_PREFIX}${line}${ANSI_QR_SUFFIX}`);
44
+ }
45
+
46
+ return lines.join("\n");
47
+ }
48
+
49
+ function readModule(
50
+ modules: QrModules,
51
+ x: number,
52
+ y: number,
53
+ margin: number,
54
+ ): boolean {
55
+ const moduleX = x - margin;
56
+ const moduleY = y - margin;
57
+ if (
58
+ moduleX < 0 ||
59
+ moduleY < 0 ||
60
+ moduleX >= modules.size ||
61
+ moduleY >= modules.size
62
+ ) {
63
+ return false;
64
+ }
65
+ return Boolean(modules.get(moduleX, moduleY));
66
+ }
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.",
package/src/types.ts CHANGED
@@ -57,6 +57,7 @@ export interface PrivateClawInviteBundle {
57
57
  qrSvg: string;
58
58
  qrTerminal: string;
59
59
  announcementText: string;
60
+ qrPngPath?: string;
60
61
  }
61
62
 
62
63
  export interface ProviderSessionState {