@privateclaw/privateclaw 0.1.0 → 0.1.2
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 +103 -8
- package/dist/bridges/openclaw-agent-bridge.d.ts +5 -0
- package/dist/bridges/openclaw-agent-bridge.js +228 -15
- package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
- package/dist/cli.js +11 -19
- package/dist/cli.js.map +1 -1
- package/dist/compat/openclaw.d.ts +18 -1
- package/dist/compat/openclaw.js +3 -1
- package/dist/compat/openclaw.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- 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 +90 -15
- package/dist/openclaw-plugin.js.map +1 -1
- package/dist/pair-session.d.ts +12 -0
- package/dist/pair-session.js +63 -0
- package/dist/pair-session.js.map +1 -0
- package/dist/provider.d.ts +22 -1
- package/dist/provider.js +625 -60
- package/dist/provider.js.map +1 -1
- package/dist/qr-options.d.ts +5 -0
- package/dist/qr-options.js +6 -0
- package/dist/qr-options.js.map +1 -0
- package/dist/relay-defaults.d.ts +1 -0
- package/dist/relay-defaults.js +2 -0
- package/dist/relay-defaults.js.map +1 -0
- package/dist/relay-provider-client.d.ts +2 -2
- package/dist/relay-provider-client.js +9 -3
- package/dist/relay-provider-client.js.map +1 -1
- package/dist/text.d.ts +22 -0
- package/dist/text.js +32 -0
- package/dist/text.js.map +1 -0
- package/dist/types.d.ts +14 -1
- package/openclaw.plugin.json +3 -2
- package/package.json +1 -1
- package/src/bridges/openclaw-agent-bridge.test.ts +141 -0
- package/src/bridges/openclaw-agent-bridge.ts +302 -16
- package/src/cli.ts +14 -22
- package/src/compat/openclaw.ts +30 -1
- package/src/index.ts +1 -0
- package/src/openclaw-command-discovery.test.ts +108 -0
- package/src/openclaw-command-discovery.ts +119 -19
- package/src/openclaw-plugin.test.ts +139 -0
- package/src/openclaw-plugin.ts +137 -15
- package/src/pair-session.ts +100 -0
- package/src/provider.test.ts +714 -1
- package/src/provider.ts +952 -60
- package/src/qr-options.ts +5 -0
- package/src/relay-defaults.ts +1 -0
- package/src/relay-provider-client.ts +13 -2
- package/src/text.ts +108 -0
- package/src/types.ts +15 -0
package/src/provider.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
1
2
|
import {
|
|
2
3
|
decodeInviteString,
|
|
3
4
|
decryptPayload,
|
|
@@ -6,20 +7,58 @@ import {
|
|
|
6
7
|
generateSessionKey,
|
|
7
8
|
type PrivateClawAttachment,
|
|
8
9
|
type PrivateClawInvite,
|
|
10
|
+
type PrivateClawParticipant,
|
|
9
11
|
type PrivateClawSlashCommand,
|
|
10
12
|
} from "@privateclaw/protocol";
|
|
11
13
|
import QRCode from "qrcode";
|
|
14
|
+
import {
|
|
15
|
+
PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
16
|
+
PRIVATECLAW_QR_SVG_MARGIN,
|
|
17
|
+
PRIVATECLAW_QR_TERMINAL_MARGIN,
|
|
18
|
+
} from "./qr-options.js";
|
|
12
19
|
import { RelayProviderClient } from "./relay-provider-client.js";
|
|
20
|
+
import {
|
|
21
|
+
buildInviteAnnouncementText,
|
|
22
|
+
formatBilingualInline,
|
|
23
|
+
formatBilingualText,
|
|
24
|
+
PRIVATECLAW_MUTE_BOT_DESCRIPTION,
|
|
25
|
+
PRIVATECLAW_RENEW_SESSION_DESCRIPTION,
|
|
26
|
+
PRIVATECLAW_UNMUTE_BOT_DESCRIPTION,
|
|
27
|
+
} from "./text.js";
|
|
13
28
|
import type {
|
|
14
29
|
BridgeMessage,
|
|
15
30
|
BridgeResponse,
|
|
16
31
|
PrivateClawConversationTurn,
|
|
17
32
|
PrivateClawInviteBundle,
|
|
18
33
|
PrivateClawProviderOptions,
|
|
34
|
+
ProviderParticipantState,
|
|
19
35
|
ProviderSessionState,
|
|
20
36
|
} from "./types.js";
|
|
21
37
|
|
|
22
|
-
const
|
|
38
|
+
export const DEFAULT_SESSION_TTL_MS = 8 * 60 * 60 * 1000;
|
|
39
|
+
const RENEW_SESSION_TTL_MS = DEFAULT_SESSION_TTL_MS;
|
|
40
|
+
const SESSION_RENEWAL_REMINDER_WINDOW_MS = 30 * 60 * 1000;
|
|
41
|
+
const PARTICIPANT_LABEL_MAX_CHARS = 12;
|
|
42
|
+
const PARTICIPANT_FALLBACK_PREFIXES = [
|
|
43
|
+
"流萤",
|
|
44
|
+
"星河",
|
|
45
|
+
"青柠",
|
|
46
|
+
"雾岚",
|
|
47
|
+
"松果",
|
|
48
|
+
"电光",
|
|
49
|
+
"夜航",
|
|
50
|
+
"珊瑚",
|
|
51
|
+
] as const;
|
|
52
|
+
const PARTICIPANT_FALLBACK_SUFFIXES = [
|
|
53
|
+
"狐",
|
|
54
|
+
"猫",
|
|
55
|
+
"鲸",
|
|
56
|
+
"鹿",
|
|
57
|
+
"狸",
|
|
58
|
+
"鹭",
|
|
59
|
+
"狼",
|
|
60
|
+
"鸮",
|
|
61
|
+
] as const;
|
|
23
62
|
|
|
24
63
|
function normalizeBridgeMessages(
|
|
25
64
|
response: BridgeResponse,
|
|
@@ -39,6 +78,273 @@ function nowIso(): string {
|
|
|
39
78
|
return new Date().toISOString();
|
|
40
79
|
}
|
|
41
80
|
|
|
81
|
+
function buildMessageId(prefix: string): string {
|
|
82
|
+
return `${prefix}-${randomUUID()}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function sanitizeParticipantLabel(raw: string | undefined): string | undefined {
|
|
86
|
+
if (!raw) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const normalized = raw
|
|
91
|
+
.replace(/\s+/g, " ")
|
|
92
|
+
.trim()
|
|
93
|
+
.replace(/^["'`]+|["'`]+$/g, "");
|
|
94
|
+
if (normalized === "") {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const cleaned = normalized
|
|
99
|
+
.replace(/[^\p{Letter}\p{Number}\s_-]/gu, "")
|
|
100
|
+
.trim();
|
|
101
|
+
if (cleaned === "") {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return [...cleaned].slice(0, PARTICIPANT_LABEL_MAX_CHARS).join("");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function deterministicHash(input: string): number {
|
|
109
|
+
let hash = 0;
|
|
110
|
+
for (const char of input) {
|
|
111
|
+
hash = (hash * 31 + char.codePointAt(0)!) >>> 0;
|
|
112
|
+
}
|
|
113
|
+
return hash;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function buildFallbackParticipantLabel(appId: string): string {
|
|
117
|
+
const prefix =
|
|
118
|
+
PARTICIPANT_FALLBACK_PREFIXES[
|
|
119
|
+
deterministicHash(`prefix:${appId}`) % PARTICIPANT_FALLBACK_PREFIXES.length
|
|
120
|
+
];
|
|
121
|
+
const suffix =
|
|
122
|
+
PARTICIPANT_FALLBACK_SUFFIXES[
|
|
123
|
+
deterministicHash(`suffix:${appId}`) % PARTICIPANT_FALLBACK_SUFFIXES.length
|
|
124
|
+
];
|
|
125
|
+
return `${prefix}${suffix}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function buildDerivedBridgeSessionId(
|
|
129
|
+
sessionId: string,
|
|
130
|
+
scope: string,
|
|
131
|
+
): string {
|
|
132
|
+
const hex = createHash("sha256")
|
|
133
|
+
.update(`${sessionId}:${scope}`)
|
|
134
|
+
.digest("hex")
|
|
135
|
+
.slice(0, 32)
|
|
136
|
+
.split("");
|
|
137
|
+
|
|
138
|
+
hex[12] = "5";
|
|
139
|
+
const variant = Number.parseInt(hex[16]!, 16);
|
|
140
|
+
hex[16] = ((variant & 0x3) | 0x8).toString(16);
|
|
141
|
+
|
|
142
|
+
return `${hex.slice(0, 8).join("")}-${hex.slice(8, 12).join("")}-${hex.slice(12, 16).join("")}-${hex.slice(16, 20).join("")}-${hex.slice(20, 32).join("")}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function buildRenewalReminderMessage(session: ProviderSessionState): string {
|
|
146
|
+
const command = "/renew-session";
|
|
147
|
+
if (session.groupMode) {
|
|
148
|
+
return formatBilingualText(
|
|
149
|
+
`这个 PrivateClaw 群聊会话将在 30 分钟内过期(${session.invite.expiresAt})。任意参与者都可以发送 ${command} 将它延长 8 小时。`,
|
|
150
|
+
`This PrivateClaw group session will expire in less than 30 minutes (at ${session.invite.expiresAt}). Any participant can send ${command} to extend it by 8 hours.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return formatBilingualText(
|
|
154
|
+
`这个 PrivateClaw 会话将在 30 分钟内过期(${session.invite.expiresAt})。发送 ${command} 可将它延长 8 小时。`,
|
|
155
|
+
`This PrivateClaw session will expire in less than 30 minutes (at ${session.invite.expiresAt}). Send ${command} to extend it by 8 hours.`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function normalizeBridgeText(response: BridgeResponse): string {
|
|
160
|
+
if (typeof response === "string") {
|
|
161
|
+
return response;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return response.messages
|
|
165
|
+
.map((message: BridgeMessage) =>
|
|
166
|
+
typeof message === "string" ? message : message.text,
|
|
167
|
+
)
|
|
168
|
+
.find((message) => message.trim() !== "") ?? "";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function formatBridgeHistoryTurn(
|
|
172
|
+
turn: PrivateClawConversationTurn,
|
|
173
|
+
): PrivateClawConversationTurn {
|
|
174
|
+
if (turn.role !== "user" || !turn.participantLabel) {
|
|
175
|
+
return turn;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
...turn,
|
|
180
|
+
text: `${turn.participantLabel}: ${turn.text}`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function toParticipantSnapshot(
|
|
185
|
+
participant: ProviderParticipantState,
|
|
186
|
+
): PrivateClawParticipant {
|
|
187
|
+
return {
|
|
188
|
+
appId: participant.appId,
|
|
189
|
+
displayName: participant.displayName,
|
|
190
|
+
...(participant.deviceLabel ? { deviceLabel: participant.deviceLabel } : {}),
|
|
191
|
+
joinedAt: participant.joinedAt,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function buildParticipantNamingPrompt(deviceLabel?: string): string {
|
|
196
|
+
return [
|
|
197
|
+
"Generate one playful nickname for a PrivateClaw group participant.",
|
|
198
|
+
"Rules:",
|
|
199
|
+
"- return the nickname only",
|
|
200
|
+
"- no quotes, emoji, punctuation, or explanation",
|
|
201
|
+
"- maximum 6 Chinese characters or 12 ASCII characters",
|
|
202
|
+
"- make it short, memorable, and cute",
|
|
203
|
+
...(deviceLabel
|
|
204
|
+
? [`The participant's device label is: ${deviceLabel}`]
|
|
205
|
+
: []),
|
|
206
|
+
].join("\n");
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function buildWelcomeMessage(): string {
|
|
210
|
+
return formatBilingualText(
|
|
211
|
+
"PrivateClaw 已连接。从现在起,消息都会通过这个一次性的端到端加密会话进行保护。",
|
|
212
|
+
"PrivateClaw connected. Messages from now on are protected by this one-time end-to-end encrypted session.",
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function buildCommandDoesNotAcceptAttachmentsMessage(command: string): string {
|
|
217
|
+
return formatBilingualInline(
|
|
218
|
+
`${command} 命令不接受附件。`,
|
|
219
|
+
`The ${command} command does not accept attachments.`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function buildCommandDoesNotAcceptArgumentsMessage(command: string): string {
|
|
224
|
+
return formatBilingualInline(
|
|
225
|
+
`${command} 命令不接受参数。`,
|
|
226
|
+
`The ${command} command does not accept arguments.`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function buildRenewalAlreadyInProgressMessage(): string {
|
|
231
|
+
return formatBilingualInline(
|
|
232
|
+
"会话续期已经在进行中。请等待重新握手完成后再重试。",
|
|
233
|
+
"A session renewal is already in progress. Wait for the reconnect handshake to finish and try again.",
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function buildRenewalPayloadMessage(): string {
|
|
238
|
+
return formatBilingualInline("会话已续期。", "Session renewed.");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function buildRenewalInitiatedMessage(expiresAt: string): string {
|
|
242
|
+
return formatBilingualInline(
|
|
243
|
+
`PrivateClaw 会话续期已开始,新过期时间为 ${expiresAt}。`,
|
|
244
|
+
`PrivateClaw session renewal initiated until ${expiresAt}.`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function buildSessionKeyRotationCompletedMessage(
|
|
249
|
+
participantLabel: string,
|
|
250
|
+
): string {
|
|
251
|
+
return formatBilingualInline(
|
|
252
|
+
`${participantLabel} 已完成会话密钥轮换。`,
|
|
253
|
+
`${participantLabel} completed session key rotation.`,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function buildSingleSessionConnectedMessage(participantLabel: string): string {
|
|
258
|
+
return formatBilingualInline(
|
|
259
|
+
`${participantLabel} 已连接。`,
|
|
260
|
+
`${participantLabel} connected.`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function buildParticipantJoinedMessage(participantLabel: string): string {
|
|
265
|
+
return formatBilingualInline(
|
|
266
|
+
`${participantLabel} 加入了群聊。`,
|
|
267
|
+
`${participantLabel} joined the group chat.`,
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function buildParticipantLeftMessage(participantLabel: string): string {
|
|
272
|
+
return formatBilingualInline(
|
|
273
|
+
`${participantLabel} 离开了群聊。`,
|
|
274
|
+
`${participantLabel} left the group chat.`,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function buildHandshakeIncompleteMessage(): string {
|
|
279
|
+
return formatBilingualInline(
|
|
280
|
+
"PrivateClaw 握手尚未完成。请重新扫码或重试连接。",
|
|
281
|
+
"The PrivateClaw handshake is not complete yet. Scan the QR code again or retry the connection.",
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function buildMissingParticipantIdentityMessage(): string {
|
|
286
|
+
return formatBilingualInline(
|
|
287
|
+
"当前群成员缺少稳定的应用身份。请从 PrivateClaw App 重新加入会话。",
|
|
288
|
+
"This group participant is missing a stable app identity. Rejoin the session from the PrivateClaw app.",
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function buildRenewSessionFailureMessage(details: string): string {
|
|
293
|
+
return formatBilingualInline(
|
|
294
|
+
`PrivateClaw 会话续期失败:${details}`,
|
|
295
|
+
`Failed to renew the PrivateClaw session: ${details}`,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function buildBridgeErrorMessage(details: string): string {
|
|
300
|
+
return formatBilingualInline(
|
|
301
|
+
`OpenClaw bridge 错误:${details}`,
|
|
302
|
+
`OpenClaw bridge error: ${details}`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function buildUnsupportedPayloadMessage(kind: string): string {
|
|
307
|
+
return formatBilingualInline(
|
|
308
|
+
`不支持的 PrivateClaw 负载类型:${kind}`,
|
|
309
|
+
`Unsupported PrivateClaw payload: ${kind}`,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function buildGroupOnlyCommandMessage(command: string): string {
|
|
314
|
+
return formatBilingualInline(
|
|
315
|
+
`${command} 只能在群聊会话中使用。`,
|
|
316
|
+
`${command} is only available in group sessions.`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function buildBotMutedMessage(participantLabel: string): string {
|
|
321
|
+
return formatBilingualInline(
|
|
322
|
+
`${participantLabel} 已暂停机器人参与群聊。`,
|
|
323
|
+
`${participantLabel} paused bot replies for this group chat.`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function buildBotUnmutedMessage(participantLabel: string): string {
|
|
328
|
+
return formatBilingualInline(
|
|
329
|
+
`${participantLabel} 已恢复机器人参与群聊。`,
|
|
330
|
+
`${participantLabel} resumed bot replies for this group chat.`,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function buildBotAlreadyMutedMessage(): string {
|
|
335
|
+
return formatBilingualInline(
|
|
336
|
+
"机器人目前已经处于静默状态。",
|
|
337
|
+
"Bot replies are already muted.",
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function buildBotAlreadyUnmutedMessage(): string {
|
|
342
|
+
return formatBilingualInline(
|
|
343
|
+
"机器人目前已经处于可回复状态。",
|
|
344
|
+
"Bot replies are already enabled.",
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
42
348
|
function dedupeCommands(
|
|
43
349
|
commands: ReadonlyArray<PrivateClawSlashCommand>,
|
|
44
350
|
): PrivateClawSlashCommand[] {
|
|
@@ -62,7 +368,7 @@ export class PrivateClawProvider {
|
|
|
62
368
|
await this.handleRelayFrame(sessionId, envelope);
|
|
63
369
|
},
|
|
64
370
|
onSessionClosed: async (sessionId, reason) => {
|
|
65
|
-
this.
|
|
371
|
+
this.deleteSession(sessionId);
|
|
66
372
|
this.options.onLog?.(`Session ${sessionId} closed by relay: ${reason}`);
|
|
67
373
|
},
|
|
68
374
|
onError: (message) => {
|
|
@@ -82,11 +388,12 @@ export class PrivateClawProvider {
|
|
|
82
388
|
await this.relayClient.closeSession(sessionId, "provider_shutdown");
|
|
83
389
|
} catch (error) {
|
|
84
390
|
this.options.onLog?.(
|
|
85
|
-
|
|
86
|
-
|
|
391
|
+
`[provider] failed to close session ${sessionId} during shutdown: ${error instanceof Error ? error.message : String(error)}`,
|
|
392
|
+
);
|
|
393
|
+
} finally {
|
|
394
|
+
this.deleteSession(sessionId);
|
|
87
395
|
}
|
|
88
396
|
}
|
|
89
|
-
this.sessions.clear();
|
|
90
397
|
await this.relayClient.dispose();
|
|
91
398
|
}
|
|
92
399
|
|
|
@@ -108,10 +415,170 @@ export class PrivateClawProvider {
|
|
|
108
415
|
return session;
|
|
109
416
|
}
|
|
110
417
|
|
|
418
|
+
private deleteSession(sessionId: string): ProviderSessionState | undefined {
|
|
419
|
+
const session = this.sessions.get(sessionId);
|
|
420
|
+
if (!session) {
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
if (session.renewalReminderTimer) {
|
|
424
|
+
clearTimeout(session.renewalReminderTimer);
|
|
425
|
+
delete session.renewalReminderTimer;
|
|
426
|
+
}
|
|
427
|
+
this.sessions.delete(sessionId);
|
|
428
|
+
return session;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private buildBridgeHistory(
|
|
432
|
+
session: ProviderSessionState,
|
|
433
|
+
): PrivateClawConversationTurn[] {
|
|
434
|
+
return session.history.map(formatBridgeHistoryTurn);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
private listParticipants(
|
|
438
|
+
session: ProviderSessionState,
|
|
439
|
+
): PrivateClawParticipant[] {
|
|
440
|
+
return [...session.participants.values()]
|
|
441
|
+
.sort((left, right) => left.joinedAt.localeCompare(right.joinedAt))
|
|
442
|
+
.map(toParticipantSnapshot);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
private scheduleRenewalReminder(sessionId: string): void {
|
|
446
|
+
const session = this.sessions.get(sessionId);
|
|
447
|
+
if (!session) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (session.renewalReminderTimer) {
|
|
452
|
+
clearTimeout(session.renewalReminderTimer);
|
|
453
|
+
}
|
|
454
|
+
delete session.renewalReminderTimer;
|
|
455
|
+
delete session.renewalReminderSentAt;
|
|
456
|
+
|
|
457
|
+
const renewAtMs =
|
|
458
|
+
new Date(session.invite.expiresAt).getTime() -
|
|
459
|
+
SESSION_RENEWAL_REMINDER_WINDOW_MS;
|
|
460
|
+
const delayMs = Math.max(0, renewAtMs - Date.now());
|
|
461
|
+
|
|
462
|
+
session.renewalReminderTimer = setTimeout(() => {
|
|
463
|
+
void this.sendRenewalReminder(sessionId).catch((error) => {
|
|
464
|
+
this.options.onLog?.(
|
|
465
|
+
`[provider] failed to send renewal reminder for ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
466
|
+
);
|
|
467
|
+
});
|
|
468
|
+
}, delayMs);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
private async sendRenewalReminder(sessionId: string): Promise<void> {
|
|
472
|
+
const session = this.sessions.get(sessionId);
|
|
473
|
+
if (!session) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
delete session.renewalReminderTimer;
|
|
478
|
+
|
|
479
|
+
if (session.pendingRenewal || session.renewalReminderSentAt) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const remainingMs = new Date(session.invite.expiresAt).getTime() - Date.now();
|
|
484
|
+
if (remainingMs > SESSION_RENEWAL_REMINDER_WINDOW_MS) {
|
|
485
|
+
this.scheduleRenewalReminder(sessionId);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const sentAt = nowIso();
|
|
490
|
+
session.renewalReminderSentAt = sentAt;
|
|
491
|
+
await this.sendSystemMessage(
|
|
492
|
+
sessionId,
|
|
493
|
+
buildRenewalReminderMessage(session),
|
|
494
|
+
"info",
|
|
495
|
+
undefined,
|
|
496
|
+
{ sentAt },
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
private async generateParticipantLabel(
|
|
501
|
+
sessionId: string,
|
|
502
|
+
appId: string,
|
|
503
|
+
deviceLabel?: string,
|
|
504
|
+
): Promise<string> {
|
|
505
|
+
const session = this.requireSession(sessionId);
|
|
506
|
+
const bridgeSessionId = buildDerivedBridgeSessionId(
|
|
507
|
+
sessionId,
|
|
508
|
+
`participant:${appId}`,
|
|
509
|
+
);
|
|
510
|
+
try {
|
|
511
|
+
const bridgeResponse = await this.options.bridge.handleUserMessage({
|
|
512
|
+
sessionId: bridgeSessionId,
|
|
513
|
+
invite: {
|
|
514
|
+
...session.invite,
|
|
515
|
+
sessionId: bridgeSessionId,
|
|
516
|
+
},
|
|
517
|
+
message: buildParticipantNamingPrompt(deviceLabel),
|
|
518
|
+
history: [],
|
|
519
|
+
});
|
|
520
|
+
const generated = sanitizeParticipantLabel(
|
|
521
|
+
normalizeBridgeText(bridgeResponse),
|
|
522
|
+
);
|
|
523
|
+
if (generated) {
|
|
524
|
+
return generated;
|
|
525
|
+
}
|
|
526
|
+
throw new Error("nickname generation returned an empty label");
|
|
527
|
+
} catch (error) {
|
|
528
|
+
this.options.onLog?.(
|
|
529
|
+
`[provider] participant nickname fallback for ${appId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
530
|
+
);
|
|
531
|
+
return buildFallbackParticipantLabel(appId);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
private async upsertParticipant(
|
|
536
|
+
sessionId: string,
|
|
537
|
+
params: {
|
|
538
|
+
appId?: string;
|
|
539
|
+
deviceLabel?: string;
|
|
540
|
+
displayName?: string;
|
|
541
|
+
sentAt: string;
|
|
542
|
+
},
|
|
543
|
+
): Promise<{ participant: ProviderParticipantState; isNew: boolean }> {
|
|
544
|
+
const session = this.requireSession(sessionId);
|
|
545
|
+
const normalizedAppId = params.appId?.trim() || "legacy-app";
|
|
546
|
+
const existing = session.participants.get(normalizedAppId);
|
|
547
|
+
const requestedDisplayName = sanitizeParticipantLabel(params.displayName);
|
|
548
|
+
|
|
549
|
+
if (existing) {
|
|
550
|
+
const participant: ProviderParticipantState = {
|
|
551
|
+
...existing,
|
|
552
|
+
...(params.deviceLabel ? { deviceLabel: params.deviceLabel } : {}),
|
|
553
|
+
...(requestedDisplayName ? { displayName: requestedDisplayName } : {}),
|
|
554
|
+
lastSeenAt: params.sentAt,
|
|
555
|
+
};
|
|
556
|
+
session.participants.set(normalizedAppId, participant);
|
|
557
|
+
return { participant, isNew: false };
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const participant: ProviderParticipantState = {
|
|
561
|
+
appId: normalizedAppId,
|
|
562
|
+
displayName:
|
|
563
|
+
requestedDisplayName ??
|
|
564
|
+
(await this.generateParticipantLabel(
|
|
565
|
+
sessionId,
|
|
566
|
+
normalizedAppId,
|
|
567
|
+
params.deviceLabel,
|
|
568
|
+
)),
|
|
569
|
+
...(params.deviceLabel ? { deviceLabel: params.deviceLabel } : {}),
|
|
570
|
+
joinedAt: params.sentAt,
|
|
571
|
+
lastSeenAt: params.sentAt,
|
|
572
|
+
};
|
|
573
|
+
session.participants.set(normalizedAppId, participant);
|
|
574
|
+
return { participant, isNew: true };
|
|
575
|
+
}
|
|
576
|
+
|
|
111
577
|
private async sendPayloadWithSessionKey(
|
|
112
578
|
sessionId: string,
|
|
113
579
|
sessionKey: string,
|
|
114
580
|
payload: Parameters<typeof encryptPayload>[0]["payload"],
|
|
581
|
+
targetAppId?: string,
|
|
115
582
|
): Promise<void> {
|
|
116
583
|
await this.relayClient.sendFrame(
|
|
117
584
|
sessionId,
|
|
@@ -120,18 +587,21 @@ export class PrivateClawProvider {
|
|
|
120
587
|
sessionKey,
|
|
121
588
|
payload,
|
|
122
589
|
}),
|
|
590
|
+
targetAppId,
|
|
123
591
|
);
|
|
124
592
|
}
|
|
125
593
|
|
|
126
594
|
private async sendPayload(
|
|
127
595
|
sessionId: string,
|
|
128
596
|
payload: Parameters<typeof encryptPayload>[0]["payload"],
|
|
597
|
+
targetAppId?: string,
|
|
129
598
|
): Promise<void> {
|
|
130
599
|
const session = this.requireSession(sessionId);
|
|
131
600
|
await this.sendPayloadWithSessionKey(
|
|
132
601
|
sessionId,
|
|
133
602
|
session.invite.sessionKey,
|
|
134
603
|
payload,
|
|
604
|
+
targetAppId,
|
|
135
605
|
);
|
|
136
606
|
}
|
|
137
607
|
|
|
@@ -141,23 +611,33 @@ export class PrivateClawProvider {
|
|
|
141
611
|
text: string;
|
|
142
612
|
replyTo?: string;
|
|
143
613
|
attachments?: PrivateClawAttachment[];
|
|
614
|
+
messageId?: string;
|
|
615
|
+
sentAt?: string;
|
|
616
|
+
targetAppId?: string;
|
|
617
|
+
storeHistory?: boolean;
|
|
144
618
|
},
|
|
145
619
|
): Promise<void> {
|
|
146
|
-
const sentAt = nowIso();
|
|
620
|
+
const sentAt = params.sentAt ?? nowIso();
|
|
621
|
+
const messageId = params.messageId ?? buildMessageId("assistant");
|
|
147
622
|
const session = this.requireSession(sessionId);
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
623
|
+
if (params.storeHistory !== false) {
|
|
624
|
+
session.history.push({
|
|
625
|
+
messageId,
|
|
626
|
+
role: "assistant",
|
|
627
|
+
text: params.text,
|
|
628
|
+
sentAt,
|
|
629
|
+
...(params.replyTo ? { replyTo: params.replyTo } : {}),
|
|
630
|
+
...(params.attachments ? { attachments: params.attachments } : {}),
|
|
631
|
+
});
|
|
632
|
+
}
|
|
154
633
|
await this.sendPayload(sessionId, {
|
|
155
634
|
kind: "assistant_message",
|
|
635
|
+
messageId,
|
|
156
636
|
text: params.text,
|
|
157
637
|
sentAt,
|
|
158
638
|
...(params.replyTo ? { replyTo: params.replyTo } : {}),
|
|
159
639
|
...(params.attachments ? { attachments: params.attachments } : {}),
|
|
160
|
-
});
|
|
640
|
+
}, params.targetAppId);
|
|
161
641
|
}
|
|
162
642
|
|
|
163
643
|
async sendSystemMessage(
|
|
@@ -165,56 +645,244 @@ export class PrivateClawProvider {
|
|
|
165
645
|
message: string,
|
|
166
646
|
severity: "info" | "error" = "info",
|
|
167
647
|
replyTo?: string,
|
|
648
|
+
params?: {
|
|
649
|
+
messageId?: string;
|
|
650
|
+
sentAt?: string;
|
|
651
|
+
targetAppId?: string;
|
|
652
|
+
storeHistory?: boolean;
|
|
653
|
+
},
|
|
168
654
|
): Promise<void> {
|
|
169
|
-
const sentAt = nowIso();
|
|
655
|
+
const sentAt = params?.sentAt ?? nowIso();
|
|
656
|
+
const messageId = params?.messageId ?? buildMessageId("system");
|
|
170
657
|
const session = this.requireSession(sessionId);
|
|
171
|
-
|
|
658
|
+
if (params?.storeHistory !== false) {
|
|
659
|
+
session.history.push({
|
|
660
|
+
messageId,
|
|
661
|
+
role: "system",
|
|
662
|
+
text: message,
|
|
663
|
+
sentAt,
|
|
664
|
+
...(replyTo ? { replyTo } : {}),
|
|
665
|
+
severity,
|
|
666
|
+
});
|
|
667
|
+
}
|
|
172
668
|
await this.sendPayload(sessionId, {
|
|
173
669
|
kind: "system_message",
|
|
670
|
+
messageId,
|
|
174
671
|
message,
|
|
175
672
|
severity,
|
|
176
673
|
sentAt,
|
|
177
674
|
...(replyTo ? { replyTo } : {}),
|
|
675
|
+
}, params?.targetAppId);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
private async sendSystemMessageToGroupParticipants(
|
|
679
|
+
sessionId: string,
|
|
680
|
+
message: string,
|
|
681
|
+
params?: {
|
|
682
|
+
excludeAppId?: string;
|
|
683
|
+
severity?: "info" | "error";
|
|
684
|
+
sentAt?: string;
|
|
685
|
+
messageId?: string;
|
|
686
|
+
},
|
|
687
|
+
): Promise<void> {
|
|
688
|
+
const session = this.requireSession(sessionId);
|
|
689
|
+
const sentAt = params?.sentAt ?? nowIso();
|
|
690
|
+
const severity = params?.severity ?? "info";
|
|
691
|
+
const messageId = params?.messageId ?? buildMessageId("system");
|
|
692
|
+
const targetAppIds = [...session.participants.keys()].filter(
|
|
693
|
+
(appId) => appId !== params?.excludeAppId,
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
if (targetAppIds.length === 0) {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
session.history.push({
|
|
701
|
+
messageId,
|
|
702
|
+
role: "system",
|
|
703
|
+
text: message,
|
|
704
|
+
sentAt,
|
|
705
|
+
severity,
|
|
178
706
|
});
|
|
707
|
+
|
|
708
|
+
await Promise.all(
|
|
709
|
+
targetAppIds.map((targetAppId) =>
|
|
710
|
+
this.sendPayload(
|
|
711
|
+
sessionId,
|
|
712
|
+
{
|
|
713
|
+
kind: "system_message",
|
|
714
|
+
messageId,
|
|
715
|
+
message,
|
|
716
|
+
severity,
|
|
717
|
+
sentAt,
|
|
718
|
+
},
|
|
719
|
+
targetAppId,
|
|
720
|
+
),
|
|
721
|
+
),
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
private async sendParticipantMessage(
|
|
726
|
+
sessionId: string,
|
|
727
|
+
params: {
|
|
728
|
+
text: string;
|
|
729
|
+
senderAppId: string;
|
|
730
|
+
senderDisplayName: string;
|
|
731
|
+
clientMessageId: string;
|
|
732
|
+
sentAt?: string;
|
|
733
|
+
attachments?: PrivateClawAttachment[];
|
|
734
|
+
targetAppId?: string;
|
|
735
|
+
storeHistory?: boolean;
|
|
736
|
+
},
|
|
737
|
+
): Promise<void> {
|
|
738
|
+
const sentAt = params.sentAt ?? nowIso();
|
|
739
|
+
const session = this.requireSession(sessionId);
|
|
740
|
+
if (params.storeHistory !== false) {
|
|
741
|
+
session.history.push({
|
|
742
|
+
messageId: params.clientMessageId,
|
|
743
|
+
role: "user",
|
|
744
|
+
text: params.text,
|
|
745
|
+
sentAt,
|
|
746
|
+
appId: params.senderAppId,
|
|
747
|
+
participantLabel: params.senderDisplayName,
|
|
748
|
+
...(params.attachments ? { attachments: params.attachments } : {}),
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
await this.sendPayload(sessionId, {
|
|
752
|
+
kind: "participant_message",
|
|
753
|
+
messageId: params.clientMessageId,
|
|
754
|
+
senderAppId: params.senderAppId,
|
|
755
|
+
senderDisplayName: params.senderDisplayName,
|
|
756
|
+
text: params.text,
|
|
757
|
+
clientMessageId: params.clientMessageId,
|
|
758
|
+
sentAt,
|
|
759
|
+
...(params.attachments ? { attachments: params.attachments } : {}),
|
|
760
|
+
}, params.targetAppId);
|
|
179
761
|
}
|
|
180
762
|
|
|
181
|
-
private async sendWelcomeMessage(
|
|
763
|
+
private async sendWelcomeMessage(
|
|
764
|
+
sessionId: string,
|
|
765
|
+
targetAppId?: string,
|
|
766
|
+
): Promise<void> {
|
|
182
767
|
const sentAt = nowIso();
|
|
183
|
-
const message =
|
|
184
|
-
this.options.welcomeMessage ??
|
|
185
|
-
"PrivateClaw connected. Messages from now on are protected by this one-time end-to-end encrypted session.";
|
|
768
|
+
const message = this.options.welcomeMessage ?? buildWelcomeMessage();
|
|
186
769
|
await this.sendPayload(sessionId, {
|
|
187
770
|
kind: "server_welcome",
|
|
188
771
|
message,
|
|
189
772
|
sentAt,
|
|
190
|
-
});
|
|
773
|
+
}, targetAppId);
|
|
191
774
|
}
|
|
192
775
|
|
|
193
|
-
private async listAvailableCommands(
|
|
776
|
+
private async listAvailableCommands(
|
|
777
|
+
session: ProviderSessionState,
|
|
778
|
+
): Promise<PrivateClawSlashCommand[]> {
|
|
194
779
|
const discovered = (await this.options.commandsProvider?.()) ?? [];
|
|
195
|
-
|
|
780
|
+
const commands: PrivateClawSlashCommand[] = [
|
|
196
781
|
...discovered,
|
|
197
782
|
{
|
|
198
783
|
slash: "/renew-session",
|
|
199
|
-
description:
|
|
200
|
-
"Rotate the current PrivateClaw session key and extend this session by 8 hours.",
|
|
784
|
+
description: PRIVATECLAW_RENEW_SESSION_DESCRIPTION,
|
|
201
785
|
acceptsArgs: false,
|
|
202
786
|
source: "privateclaw",
|
|
203
787
|
},
|
|
204
|
-
]
|
|
788
|
+
];
|
|
789
|
+
|
|
790
|
+
if (session.groupMode) {
|
|
791
|
+
commands.push({
|
|
792
|
+
slash: session.botMuted ? "/unmute-bot" : "/mute-bot",
|
|
793
|
+
description: session.botMuted
|
|
794
|
+
? PRIVATECLAW_UNMUTE_BOT_DESCRIPTION
|
|
795
|
+
: PRIVATECLAW_MUTE_BOT_DESCRIPTION,
|
|
796
|
+
acceptsArgs: false,
|
|
797
|
+
source: "privateclaw",
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
return dedupeCommands(commands);
|
|
205
802
|
}
|
|
206
803
|
|
|
207
|
-
private async sendCapabilities(
|
|
804
|
+
private async sendCapabilities(
|
|
805
|
+
sessionId: string,
|
|
806
|
+
params?: {
|
|
807
|
+
targetAppId?: string;
|
|
808
|
+
includeCurrentIdentity?: boolean;
|
|
809
|
+
},
|
|
810
|
+
): Promise<void> {
|
|
208
811
|
const session = this.requireSession(sessionId);
|
|
812
|
+
const currentParticipant = params?.targetAppId
|
|
813
|
+
? session.participants.get(params.targetAppId)
|
|
814
|
+
: undefined;
|
|
209
815
|
await this.sendPayload(sessionId, {
|
|
210
816
|
kind: "provider_capabilities",
|
|
211
817
|
sentAt: nowIso(),
|
|
212
818
|
expiresAt: session.invite.expiresAt,
|
|
213
|
-
|
|
819
|
+
...(session.groupMode ? { groupMode: true } : {}),
|
|
820
|
+
...(session.groupMode ? { botMuted: session.botMuted } : {}),
|
|
821
|
+
commands: await this.listAvailableCommands(session),
|
|
822
|
+
...(session.groupMode
|
|
823
|
+
? { participants: this.listParticipants(session) }
|
|
824
|
+
: {}),
|
|
214
825
|
...(session.invite.providerLabel
|
|
215
826
|
? { providerLabel: session.invite.providerLabel }
|
|
216
827
|
: {}),
|
|
217
|
-
|
|
828
|
+
...(params?.includeCurrentIdentity && currentParticipant
|
|
829
|
+
? {
|
|
830
|
+
currentAppId: currentParticipant.appId,
|
|
831
|
+
currentDisplayName: currentParticipant.displayName,
|
|
832
|
+
}
|
|
833
|
+
: {}),
|
|
834
|
+
}, params?.targetAppId);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
private async sendHistorySnapshot(
|
|
838
|
+
sessionId: string,
|
|
839
|
+
targetAppId: string,
|
|
840
|
+
): Promise<void> {
|
|
841
|
+
const session = this.requireSession(sessionId);
|
|
842
|
+
for (const turn of session.history) {
|
|
843
|
+
switch (turn.role) {
|
|
844
|
+
case "assistant":
|
|
845
|
+
await this.sendAssistantMessage(sessionId, {
|
|
846
|
+
messageId: turn.messageId,
|
|
847
|
+
text: turn.text,
|
|
848
|
+
sentAt: turn.sentAt,
|
|
849
|
+
targetAppId,
|
|
850
|
+
storeHistory: false,
|
|
851
|
+
...(turn.replyTo ? { replyTo: turn.replyTo } : {}),
|
|
852
|
+
...(turn.attachments ? { attachments: turn.attachments } : {}),
|
|
853
|
+
});
|
|
854
|
+
break;
|
|
855
|
+
case "system":
|
|
856
|
+
await this.sendSystemMessage(
|
|
857
|
+
sessionId,
|
|
858
|
+
turn.text,
|
|
859
|
+
turn.severity ?? "info",
|
|
860
|
+
turn.replyTo,
|
|
861
|
+
{
|
|
862
|
+
messageId: turn.messageId,
|
|
863
|
+
sentAt: turn.sentAt,
|
|
864
|
+
targetAppId,
|
|
865
|
+
storeHistory: false,
|
|
866
|
+
},
|
|
867
|
+
);
|
|
868
|
+
break;
|
|
869
|
+
case "user":
|
|
870
|
+
if (!session.groupMode || !turn.appId || !turn.participantLabel) {
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
await this.sendParticipantMessage(sessionId, {
|
|
874
|
+
text: turn.text,
|
|
875
|
+
senderAppId: turn.appId,
|
|
876
|
+
senderDisplayName: turn.participantLabel,
|
|
877
|
+
clientMessageId: turn.messageId,
|
|
878
|
+
sentAt: turn.sentAt,
|
|
879
|
+
targetAppId,
|
|
880
|
+
storeHistory: false,
|
|
881
|
+
...(turn.attachments ? { attachments: turn.attachments } : {}),
|
|
882
|
+
});
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
218
886
|
}
|
|
219
887
|
|
|
220
888
|
private async renewSession(sessionId: string, replyTo?: string): Promise<void> {
|
|
@@ -222,7 +890,7 @@ export class PrivateClawProvider {
|
|
|
222
890
|
if (session.pendingRenewal) {
|
|
223
891
|
await this.sendSystemMessage(
|
|
224
892
|
sessionId,
|
|
225
|
-
|
|
893
|
+
buildRenewalAlreadyInProgressMessage(),
|
|
226
894
|
"error",
|
|
227
895
|
replyTo,
|
|
228
896
|
);
|
|
@@ -239,7 +907,7 @@ export class PrivateClawProvider {
|
|
|
239
907
|
|
|
240
908
|
await this.sendPayloadWithSessionKey(sessionId, previousSessionKey, {
|
|
241
909
|
kind: "session_renewed",
|
|
242
|
-
message:
|
|
910
|
+
message: buildRenewalPayloadMessage(),
|
|
243
911
|
newSessionKey: nextSessionKey,
|
|
244
912
|
expiresAt,
|
|
245
913
|
sentAt,
|
|
@@ -252,11 +920,14 @@ export class PrivateClawProvider {
|
|
|
252
920
|
expiresAt,
|
|
253
921
|
};
|
|
254
922
|
session.pendingRenewal = { expiresAt, sentAt };
|
|
923
|
+
this.scheduleRenewalReminder(sessionId);
|
|
255
924
|
session.state = "awaiting_hello";
|
|
256
925
|
session.history.push({
|
|
926
|
+
messageId: buildMessageId("system"),
|
|
257
927
|
role: "system",
|
|
258
|
-
text:
|
|
928
|
+
text: buildRenewalInitiatedMessage(expiresAt),
|
|
259
929
|
sentAt,
|
|
930
|
+
severity: "info",
|
|
260
931
|
});
|
|
261
932
|
}
|
|
262
933
|
|
|
@@ -273,54 +944,108 @@ export class PrivateClawProvider {
|
|
|
273
944
|
|
|
274
945
|
switch (payload.kind) {
|
|
275
946
|
case "client_hello": {
|
|
947
|
+
const participantState = await this.upsertParticipant(sessionId, {
|
|
948
|
+
sentAt: payload.sentAt,
|
|
949
|
+
...(payload.appId ? { appId: payload.appId } : {}),
|
|
950
|
+
...(payload.deviceLabel
|
|
951
|
+
? { deviceLabel: payload.deviceLabel }
|
|
952
|
+
: {}),
|
|
953
|
+
...(payload.displayName
|
|
954
|
+
? { displayName: payload.displayName }
|
|
955
|
+
: {}),
|
|
956
|
+
});
|
|
957
|
+
const participant = participantState.participant;
|
|
276
958
|
const wasAwaitingHello = session.state !== "active";
|
|
277
959
|
const wasRenewing = Boolean(session.pendingRenewal);
|
|
278
960
|
session.state = "active";
|
|
279
961
|
if (wasRenewing) {
|
|
280
962
|
session.history.push({
|
|
963
|
+
messageId: buildMessageId("system"),
|
|
281
964
|
role: "system",
|
|
282
|
-
text:
|
|
965
|
+
text: buildSessionKeyRotationCompletedMessage(
|
|
966
|
+
participant.displayName,
|
|
967
|
+
),
|
|
283
968
|
sentAt: payload.sentAt,
|
|
969
|
+
severity: "info",
|
|
284
970
|
});
|
|
285
971
|
delete session.pendingRenewal;
|
|
286
|
-
} else if (wasAwaitingHello) {
|
|
972
|
+
} else if (!session.groupMode && wasAwaitingHello) {
|
|
287
973
|
session.history.push({
|
|
974
|
+
messageId: buildMessageId("system"),
|
|
288
975
|
role: "system",
|
|
289
|
-
text:
|
|
976
|
+
text: buildSingleSessionConnectedMessage(participant.displayName),
|
|
290
977
|
sentAt: payload.sentAt,
|
|
978
|
+
severity: "info",
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
if (session.groupMode) {
|
|
983
|
+
await this.sendHistorySnapshot(sessionId, participant.appId);
|
|
984
|
+
await this.sendWelcomeMessage(sessionId, participant.appId);
|
|
985
|
+
await this.sendCapabilities(sessionId, {
|
|
986
|
+
targetAppId: participant.appId,
|
|
987
|
+
includeCurrentIdentity: true,
|
|
291
988
|
});
|
|
989
|
+
if (participantState.isNew && session.participants.size > 1) {
|
|
990
|
+
await this.sendSystemMessageToGroupParticipants(
|
|
991
|
+
sessionId,
|
|
992
|
+
buildParticipantJoinedMessage(participant.displayName),
|
|
993
|
+
{
|
|
994
|
+
excludeAppId: participant.appId,
|
|
995
|
+
sentAt: payload.sentAt,
|
|
996
|
+
},
|
|
997
|
+
);
|
|
998
|
+
}
|
|
999
|
+
await this.sendCapabilities(sessionId);
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
if (wasAwaitingHello && !wasRenewing) {
|
|
292
1004
|
await this.sendWelcomeMessage(sessionId);
|
|
293
1005
|
}
|
|
294
|
-
await this.sendCapabilities(sessionId
|
|
1006
|
+
await this.sendCapabilities(sessionId, {
|
|
1007
|
+
targetAppId: participant.appId,
|
|
1008
|
+
includeCurrentIdentity: true,
|
|
1009
|
+
});
|
|
295
1010
|
return;
|
|
296
1011
|
}
|
|
297
1012
|
case "user_message": {
|
|
298
1013
|
if (session.state !== "active") {
|
|
299
1014
|
await this.sendSystemMessage(
|
|
300
1015
|
sessionId,
|
|
301
|
-
|
|
1016
|
+
buildHandshakeIncompleteMessage(),
|
|
302
1017
|
"error",
|
|
303
1018
|
payload.clientMessageId,
|
|
304
1019
|
);
|
|
305
1020
|
return;
|
|
306
1021
|
}
|
|
307
1022
|
|
|
308
|
-
session.history.push({
|
|
309
|
-
role: "user",
|
|
310
|
-
text: payload.text,
|
|
311
|
-
sentAt: payload.sentAt,
|
|
312
|
-
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
313
|
-
});
|
|
314
1023
|
this.options.onLog?.(
|
|
315
1024
|
`[provider] user_message session=${sessionId} textChars=${payload.text.length} attachments=${payload.attachments?.length ?? 0}`,
|
|
316
1025
|
);
|
|
317
1026
|
|
|
1027
|
+
const participant =
|
|
1028
|
+
(payload.appId ? session.participants.get(payload.appId) : undefined) ??
|
|
1029
|
+
(session.participants.size === 1
|
|
1030
|
+
? [...session.participants.values()][0]
|
|
1031
|
+
: undefined);
|
|
1032
|
+
|
|
318
1033
|
const normalizedCommand = payload.text.trim().toLowerCase();
|
|
319
|
-
|
|
1034
|
+
const isRenewCommand =
|
|
1035
|
+
normalizedCommand === "/renew-session" ||
|
|
1036
|
+
normalizedCommand === "/session_renew";
|
|
1037
|
+
const hasRenewCommandArgs =
|
|
1038
|
+
normalizedCommand.startsWith("/renew-session ") ||
|
|
1039
|
+
normalizedCommand.startsWith("/session_renew ");
|
|
1040
|
+
const isMuteBotCommand = normalizedCommand === "/mute-bot";
|
|
1041
|
+
const hasMuteBotArgs = normalizedCommand.startsWith("/mute-bot ");
|
|
1042
|
+
const isUnmuteBotCommand = normalizedCommand === "/unmute-bot";
|
|
1043
|
+
const hasUnmuteBotArgs = normalizedCommand.startsWith("/unmute-bot ");
|
|
1044
|
+
if (isRenewCommand) {
|
|
320
1045
|
if ((payload.attachments?.length ?? 0) > 0) {
|
|
321
1046
|
await this.sendSystemMessage(
|
|
322
1047
|
sessionId,
|
|
323
|
-
"
|
|
1048
|
+
buildCommandDoesNotAcceptAttachmentsMessage("/renew-session"),
|
|
324
1049
|
"error",
|
|
325
1050
|
payload.clientMessageId,
|
|
326
1051
|
);
|
|
@@ -331,30 +1056,152 @@ export class PrivateClawProvider {
|
|
|
331
1056
|
} catch (error) {
|
|
332
1057
|
await this.sendSystemMessage(
|
|
333
1058
|
sessionId,
|
|
334
|
-
|
|
1059
|
+
buildRenewSessionFailureMessage(
|
|
1060
|
+
error instanceof Error ? error.message : String(error),
|
|
1061
|
+
),
|
|
335
1062
|
"error",
|
|
336
1063
|
payload.clientMessageId,
|
|
337
1064
|
);
|
|
338
1065
|
}
|
|
339
1066
|
return;
|
|
340
1067
|
}
|
|
341
|
-
if (
|
|
1068
|
+
if (hasRenewCommandArgs) {
|
|
342
1069
|
await this.sendSystemMessage(
|
|
343
1070
|
sessionId,
|
|
344
|
-
"
|
|
1071
|
+
buildCommandDoesNotAcceptArgumentsMessage("/renew-session"),
|
|
345
1072
|
"error",
|
|
346
1073
|
payload.clientMessageId,
|
|
347
1074
|
);
|
|
348
1075
|
return;
|
|
349
1076
|
}
|
|
350
1077
|
|
|
1078
|
+
if (isMuteBotCommand || isUnmuteBotCommand) {
|
|
1079
|
+
const command = isMuteBotCommand ? "/mute-bot" : "/unmute-bot";
|
|
1080
|
+
if ((payload.attachments?.length ?? 0) > 0) {
|
|
1081
|
+
await this.sendSystemMessage(
|
|
1082
|
+
sessionId,
|
|
1083
|
+
buildCommandDoesNotAcceptAttachmentsMessage(command),
|
|
1084
|
+
"error",
|
|
1085
|
+
payload.clientMessageId,
|
|
1086
|
+
);
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
if (!session.groupMode) {
|
|
1090
|
+
await this.sendSystemMessage(
|
|
1091
|
+
sessionId,
|
|
1092
|
+
buildGroupOnlyCommandMessage(command),
|
|
1093
|
+
"error",
|
|
1094
|
+
payload.clientMessageId,
|
|
1095
|
+
);
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
if (!participant) {
|
|
1099
|
+
await this.sendSystemMessage(
|
|
1100
|
+
sessionId,
|
|
1101
|
+
buildMissingParticipantIdentityMessage(),
|
|
1102
|
+
"error",
|
|
1103
|
+
payload.clientMessageId,
|
|
1104
|
+
);
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
const shouldMute = isMuteBotCommand;
|
|
1109
|
+
if (session.botMuted === shouldMute) {
|
|
1110
|
+
await this.sendSystemMessage(
|
|
1111
|
+
sessionId,
|
|
1112
|
+
shouldMute
|
|
1113
|
+
? buildBotAlreadyMutedMessage()
|
|
1114
|
+
: buildBotAlreadyUnmutedMessage(),
|
|
1115
|
+
"info",
|
|
1116
|
+
payload.clientMessageId,
|
|
1117
|
+
);
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
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
|
+
);
|
|
1138
|
+
await this.sendSystemMessageToGroupParticipants(
|
|
1139
|
+
sessionId,
|
|
1140
|
+
message,
|
|
1141
|
+
{
|
|
1142
|
+
excludeAppId: participant.appId,
|
|
1143
|
+
sentAt: payload.sentAt,
|
|
1144
|
+
messageId,
|
|
1145
|
+
},
|
|
1146
|
+
);
|
|
1147
|
+
await this.sendCapabilities(sessionId);
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
if (hasMuteBotArgs || hasUnmuteBotArgs) {
|
|
1152
|
+
const command = hasMuteBotArgs ? "/mute-bot" : "/unmute-bot";
|
|
1153
|
+
await this.sendSystemMessage(
|
|
1154
|
+
sessionId,
|
|
1155
|
+
buildCommandDoesNotAcceptArgumentsMessage(command),
|
|
1156
|
+
"error",
|
|
1157
|
+
payload.clientMessageId,
|
|
1158
|
+
);
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
if (session.groupMode) {
|
|
1163
|
+
if (!participant) {
|
|
1164
|
+
await this.sendSystemMessage(
|
|
1165
|
+
sessionId,
|
|
1166
|
+
buildMissingParticipantIdentityMessage(),
|
|
1167
|
+
"error",
|
|
1168
|
+
payload.clientMessageId,
|
|
1169
|
+
);
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
await this.sendParticipantMessage(sessionId, {
|
|
1173
|
+
text: payload.text,
|
|
1174
|
+
senderAppId: participant.appId,
|
|
1175
|
+
senderDisplayName: participant.displayName,
|
|
1176
|
+
clientMessageId: payload.clientMessageId,
|
|
1177
|
+
sentAt: payload.sentAt,
|
|
1178
|
+
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
1179
|
+
});
|
|
1180
|
+
} else {
|
|
1181
|
+
session.history.push({
|
|
1182
|
+
messageId: payload.clientMessageId,
|
|
1183
|
+
role: "user",
|
|
1184
|
+
text: payload.text,
|
|
1185
|
+
sentAt: payload.sentAt,
|
|
1186
|
+
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
if (session.groupMode && session.botMuted) {
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
351
1194
|
try {
|
|
1195
|
+
const bridgeMessage =
|
|
1196
|
+
session.groupMode && participant
|
|
1197
|
+
? `${participant.displayName}: ${payload.text}`
|
|
1198
|
+
: payload.text;
|
|
352
1199
|
const bridgeResponse = await this.options.bridge.handleUserMessage({
|
|
353
1200
|
sessionId,
|
|
354
1201
|
invite: session.invite,
|
|
355
|
-
message:
|
|
1202
|
+
message: bridgeMessage,
|
|
356
1203
|
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
357
|
-
history:
|
|
1204
|
+
history: this.buildBridgeHistory(session),
|
|
358
1205
|
});
|
|
359
1206
|
|
|
360
1207
|
for (const message of normalizeBridgeMessages(bridgeResponse)) {
|
|
@@ -367,18 +1214,47 @@ export class PrivateClawProvider {
|
|
|
367
1214
|
} catch (error) {
|
|
368
1215
|
await this.sendSystemMessage(
|
|
369
1216
|
sessionId,
|
|
370
|
-
|
|
1217
|
+
buildBridgeErrorMessage(
|
|
1218
|
+
error instanceof Error ? error.message : String(error),
|
|
1219
|
+
),
|
|
371
1220
|
"error",
|
|
372
1221
|
payload.clientMessageId,
|
|
373
1222
|
);
|
|
374
1223
|
}
|
|
375
1224
|
return;
|
|
376
1225
|
}
|
|
377
|
-
case "session_close":
|
|
378
|
-
|
|
379
|
-
|
|
1226
|
+
case "session_close": {
|
|
1227
|
+
if (!session.groupMode) {
|
|
1228
|
+
this.deleteSession(sessionId);
|
|
1229
|
+
await this.relayClient.closeSession(sessionId, payload.reason);
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
const participantAppId =
|
|
1234
|
+
payload.appId?.trim() ||
|
|
1235
|
+
(session.participants.size === 1
|
|
1236
|
+
? [...session.participants.keys()][0]
|
|
1237
|
+
: undefined);
|
|
1238
|
+
if (!participantAppId) {
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
const participant = session.participants.get(participantAppId);
|
|
1243
|
+
session.participants.delete(participantAppId);
|
|
1244
|
+
if (participant && session.participants.size > 0) {
|
|
1245
|
+
await this.sendSystemMessageToGroupParticipants(
|
|
1246
|
+
sessionId,
|
|
1247
|
+
buildParticipantLeftMessage(participant.displayName),
|
|
1248
|
+
{ sentAt: payload.sentAt },
|
|
1249
|
+
);
|
|
1250
|
+
}
|
|
1251
|
+
if (session.participants.size > 0) {
|
|
1252
|
+
await this.sendCapabilities(sessionId);
|
|
1253
|
+
}
|
|
380
1254
|
return;
|
|
1255
|
+
}
|
|
381
1256
|
case "assistant_message":
|
|
1257
|
+
case "participant_message":
|
|
382
1258
|
case "server_welcome":
|
|
383
1259
|
case "provider_capabilities":
|
|
384
1260
|
case "session_renewed":
|
|
@@ -387,18 +1263,26 @@ export class PrivateClawProvider {
|
|
|
387
1263
|
default:
|
|
388
1264
|
await this.sendSystemMessage(
|
|
389
1265
|
sessionId,
|
|
390
|
-
|
|
1266
|
+
buildUnsupportedPayloadMessage(
|
|
1267
|
+
(payload as { kind?: string }).kind ?? "unknown",
|
|
1268
|
+
),
|
|
391
1269
|
"error",
|
|
392
1270
|
);
|
|
393
1271
|
}
|
|
394
1272
|
}
|
|
395
1273
|
|
|
396
|
-
async createInviteBundle(params?: {
|
|
1274
|
+
async createInviteBundle(params?: {
|
|
1275
|
+
ttlMs?: number;
|
|
1276
|
+
label?: string;
|
|
1277
|
+
groupMode?: boolean;
|
|
1278
|
+
}): Promise<PrivateClawInviteBundle> {
|
|
397
1279
|
await this.connect();
|
|
398
1280
|
|
|
1281
|
+
const groupMode = params?.groupMode === true;
|
|
399
1282
|
const { sessionId, expiresAt } = await this.relayClient.createSession(
|
|
400
1283
|
params?.ttlMs ?? this.options.defaultTtlMs,
|
|
401
1284
|
params?.label,
|
|
1285
|
+
groupMode,
|
|
402
1286
|
);
|
|
403
1287
|
|
|
404
1288
|
const invite: PrivateClawInvite = {
|
|
@@ -407,23 +1291,29 @@ export class PrivateClawProvider {
|
|
|
407
1291
|
sessionKey: generateSessionKey(),
|
|
408
1292
|
appWsUrl: this.buildSessionAppUrl(sessionId),
|
|
409
1293
|
expiresAt,
|
|
1294
|
+
...(groupMode ? { groupMode: true } : {}),
|
|
410
1295
|
...(this.options.providerLabel ? { providerLabel: this.options.providerLabel } : {}),
|
|
411
1296
|
};
|
|
412
1297
|
|
|
413
1298
|
this.sessions.set(sessionId, {
|
|
414
1299
|
invite,
|
|
415
1300
|
history: [],
|
|
1301
|
+
groupMode,
|
|
1302
|
+
botMuted: false,
|
|
1303
|
+
participants: new Map<string, ProviderParticipantState>(),
|
|
416
1304
|
state: "awaiting_hello",
|
|
417
1305
|
});
|
|
1306
|
+
this.scheduleRenewalReminder(sessionId);
|
|
418
1307
|
|
|
419
1308
|
const inviteUri = encodeInviteToUri(invite);
|
|
420
1309
|
const qrSvg = await QRCode.toString(inviteUri, {
|
|
421
1310
|
type: "svg",
|
|
422
|
-
errorCorrectionLevel:
|
|
423
|
-
margin:
|
|
1311
|
+
errorCorrectionLevel: PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
1312
|
+
margin: PRIVATECLAW_QR_SVG_MARGIN,
|
|
424
1313
|
});
|
|
425
1314
|
const qrTerminal = await QRCode.toString(inviteUri, {
|
|
426
1315
|
type: "terminal",
|
|
1316
|
+
margin: PRIVATECLAW_QR_TERMINAL_MARGIN,
|
|
427
1317
|
small: true,
|
|
428
1318
|
});
|
|
429
1319
|
|
|
@@ -432,9 +1322,11 @@ export class PrivateClawProvider {
|
|
|
432
1322
|
inviteUri,
|
|
433
1323
|
qrSvg,
|
|
434
1324
|
qrTerminal,
|
|
435
|
-
announcementText:
|
|
436
|
-
|
|
437
|
-
|
|
1325
|
+
announcementText: buildInviteAnnouncementText({
|
|
1326
|
+
sessionId,
|
|
1327
|
+
expiresAt,
|
|
1328
|
+
groupMode,
|
|
1329
|
+
}),
|
|
438
1330
|
};
|
|
439
1331
|
}
|
|
440
1332
|
|