@privateclaw/privateclaw 0.1.6 → 0.1.8

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/src/provider.ts CHANGED
@@ -13,6 +13,8 @@ import {
13
13
  import QRCode from "qrcode";
14
14
  import {
15
15
  PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
16
+ PRIVATECLAW_QR_IMAGE_MARGIN,
17
+ PRIVATECLAW_QR_PNG_WIDTH,
16
18
  PRIVATECLAW_QR_SVG_MARGIN,
17
19
  PRIVATECLAW_QR_TERMINAL_MARGIN,
18
20
  } from "./qr-options.js";
@@ -23,6 +25,7 @@ import {
23
25
  formatBilingualText,
24
26
  PRIVATECLAW_MUTE_BOT_DESCRIPTION,
25
27
  PRIVATECLAW_RENEW_SESSION_DESCRIPTION,
28
+ PRIVATECLAW_SESSION_QR_DESCRIPTION,
26
29
  PRIVATECLAW_UNMUTE_BOT_DESCRIPTION,
27
30
  } from "./text.js";
28
31
  import type {
@@ -156,6 +159,27 @@ function buildRenewalReminderMessage(session: ProviderSessionState): string {
156
159
  );
157
160
  }
158
161
 
162
+ function buildSessionQrMessage(session: ProviderSessionState): string {
163
+ if (session.groupMode) {
164
+ return formatBilingualText(
165
+ `当前 PrivateClaw 群聊二维码已附上。请在 ${session.invite.expiresAt} 之前当面分享给需要加入此会话的设备。`,
166
+ `The current PrivateClaw group-session QR is attached. Share it in person with devices that should join before ${session.invite.expiresAt}.`,
167
+ );
168
+ }
169
+
170
+ return formatBilingualText(
171
+ `当前 PrivateClaw 会话二维码已附上。请在 ${session.invite.expiresAt} 之前当面分享给需要连接的设备。`,
172
+ `The current PrivateClaw session QR is attached. Share it in person with devices that should connect before ${session.invite.expiresAt}.`,
173
+ );
174
+ }
175
+
176
+ function buildSessionQrFailureMessage(details: string): string {
177
+ return formatBilingualInline(
178
+ `生成当前会话二维码失败:${details}`,
179
+ `Failed to render the current session QR: ${details}`,
180
+ );
181
+ }
182
+
159
183
  function normalizeBridgeText(response: BridgeResponse): string {
160
184
  if (typeof response === "string") {
161
185
  return response;
@@ -785,6 +809,12 @@ export class PrivateClawProvider {
785
809
  acceptsArgs: false,
786
810
  source: "privateclaw",
787
811
  },
812
+ {
813
+ slash: "/session-qr",
814
+ description: PRIVATECLAW_SESSION_QR_DESCRIPTION,
815
+ acceptsArgs: false,
816
+ source: "privateclaw",
817
+ },
788
818
  ];
789
819
 
790
820
  if (session.groupMode) {
@@ -1042,6 +1072,8 @@ export class PrivateClawProvider {
1042
1072
  const hasRenewCommandArgs =
1043
1073
  normalizedCommand.startsWith("/renew-session ") ||
1044
1074
  normalizedCommand.startsWith("/session_renew ");
1075
+ const isSessionQrCommand = normalizedCommand === "/session-qr";
1076
+ const hasSessionQrArgs = normalizedCommand.startsWith("/session-qr ");
1045
1077
  const isMuteBotCommand = normalizedCommand === "/mute-bot";
1046
1078
  const hasMuteBotArgs = normalizedCommand.startsWith("/mute-bot ");
1047
1079
  const isUnmuteBotCommand = normalizedCommand === "/unmute-bot";
@@ -1080,6 +1112,74 @@ export class PrivateClawProvider {
1080
1112
  return;
1081
1113
  }
1082
1114
 
1115
+ if (isSessionQrCommand) {
1116
+ if ((payload.attachments?.length ?? 0) > 0) {
1117
+ await this.sendSystemMessage(
1118
+ sessionId,
1119
+ buildCommandDoesNotAcceptAttachmentsMessage("/session-qr"),
1120
+ "error",
1121
+ payload.clientMessageId,
1122
+ participant
1123
+ ? {
1124
+ targetAppId: participant.appId,
1125
+ storeHistory: false,
1126
+ }
1127
+ : undefined,
1128
+ );
1129
+ return;
1130
+ }
1131
+ if (session.groupMode && !participant) {
1132
+ await this.sendSystemMessage(
1133
+ sessionId,
1134
+ buildMissingParticipantIdentityMessage(),
1135
+ "error",
1136
+ payload.clientMessageId,
1137
+ );
1138
+ return;
1139
+ }
1140
+ try {
1141
+ const qrAttachment = await this.buildSessionQrAttachment(sessionId);
1142
+ await this.sendAssistantMessage(sessionId, {
1143
+ text: buildSessionQrMessage(session),
1144
+ replyTo: payload.clientMessageId,
1145
+ attachments: [qrAttachment],
1146
+ ...(participant ? { targetAppId: participant.appId } : {}),
1147
+ storeHistory: false,
1148
+ });
1149
+ } catch (error) {
1150
+ await this.sendSystemMessage(
1151
+ sessionId,
1152
+ buildSessionQrFailureMessage(
1153
+ error instanceof Error ? error.message : String(error),
1154
+ ),
1155
+ "error",
1156
+ payload.clientMessageId,
1157
+ participant
1158
+ ? {
1159
+ targetAppId: participant.appId,
1160
+ storeHistory: false,
1161
+ }
1162
+ : undefined,
1163
+ );
1164
+ }
1165
+ return;
1166
+ }
1167
+ if (hasSessionQrArgs) {
1168
+ await this.sendSystemMessage(
1169
+ sessionId,
1170
+ buildCommandDoesNotAcceptArgumentsMessage("/session-qr"),
1171
+ "error",
1172
+ payload.clientMessageId,
1173
+ participant
1174
+ ? {
1175
+ targetAppId: participant.appId,
1176
+ storeHistory: false,
1177
+ }
1178
+ : undefined,
1179
+ );
1180
+ return;
1181
+ }
1182
+
1083
1183
  if (isMuteBotCommand || isUnmuteBotCommand) {
1084
1184
  const command = isMuteBotCommand ? "/mute-bot" : "/unmute-bot";
1085
1185
  if ((payload.attachments?.length ?? 0) > 0) {
@@ -1317,7 +1417,16 @@ export class PrivateClawProvider {
1317
1417
  state: "awaiting_hello",
1318
1418
  });
1319
1419
  this.scheduleRenewalReminder(sessionId);
1420
+ return this.buildInviteBundleForInvite(invite);
1421
+ }
1320
1422
 
1423
+ decodeInvite(uri: string): PrivateClawInvite {
1424
+ return decodeInviteString(uri);
1425
+ }
1426
+
1427
+ private async buildInviteBundleForInvite(
1428
+ invite: PrivateClawInvite,
1429
+ ): Promise<PrivateClawInviteBundle> {
1321
1430
  const inviteUri = encodeInviteToUri(invite);
1322
1431
  const qrSvg = await QRCode.toString(inviteUri, {
1323
1432
  type: "svg",
@@ -1336,14 +1445,29 @@ export class PrivateClawProvider {
1336
1445
  qrSvg,
1337
1446
  qrTerminal,
1338
1447
  announcementText: buildInviteAnnouncementText({
1339
- sessionId,
1340
- expiresAt,
1341
- groupMode,
1448
+ sessionId: invite.sessionId,
1449
+ expiresAt: invite.expiresAt,
1450
+ groupMode: invite.groupMode === true,
1342
1451
  }),
1343
1452
  };
1344
1453
  }
1345
1454
 
1346
- decodeInvite(uri: string): PrivateClawInvite {
1347
- return decodeInviteString(uri);
1455
+ private async buildSessionQrAttachment(sessionId: string): Promise<PrivateClawAttachment> {
1456
+ const session = this.requireSession(sessionId);
1457
+ const bundle = await this.buildInviteBundleForInvite(session.invite);
1458
+ const pngBuffer = await QRCode.toBuffer(bundle.inviteUri, {
1459
+ type: "png",
1460
+ errorCorrectionLevel: PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
1461
+ margin: PRIVATECLAW_QR_IMAGE_MARGIN,
1462
+ width: PRIVATECLAW_QR_PNG_WIDTH,
1463
+ });
1464
+
1465
+ return {
1466
+ id: buildMessageId("attachment"),
1467
+ name: `privateclaw-${bundle.invite.sessionId}.png`,
1468
+ mimeType: "image/png",
1469
+ sizeBytes: pngBuffer.length,
1470
+ dataBase64: pngBuffer.toString("base64"),
1471
+ };
1348
1472
  }
1349
1473
  }
package/src/text.ts CHANGED
@@ -107,6 +107,11 @@ export const PRIVATECLAW_RENEW_SESSION_DESCRIPTION = formatBilingualInline(
107
107
  "Rotate the current PrivateClaw session key and extend this session by 8 hours.",
108
108
  );
109
109
 
110
+ export const PRIVATECLAW_SESSION_QR_DESCRIPTION = formatBilingualInline(
111
+ "重新打印当前 PrivateClaw 会话二维码,便于当面分享。",
112
+ "Show the current PrivateClaw session QR again for in-person sharing.",
113
+ );
114
+
110
115
  export const PRIVATECLAW_MUTE_BOT_DESCRIPTION = formatBilingualInline(
111
116
  "在当前群聊中暂停机器人参与回复。",
112
117
  "Pause bot replies in this group chat.",
package/src/types.ts CHANGED
@@ -24,7 +24,12 @@ export type BridgeMessage =
24
24
  attachments?: PrivateClawAttachment[];
25
25
  };
26
26
 
27
- export type BridgeResponse = string | { messages: BridgeMessage[] };
27
+ export interface BridgeResponseEnvelope {
28
+ messages: BridgeMessage[];
29
+ data?: unknown;
30
+ }
31
+
32
+ export type BridgeResponse = string | BridgeResponseEnvelope;
28
33
 
29
34
  export interface PrivateClawAgentBridge {
30
35
  handleUserMessage(params: {