@privateclaw/privateclaw 0.1.0 → 0.1.1
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 +88 -6
- 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-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 +613 -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-plugin.test.ts +139 -0
- package/src/openclaw-plugin.ts +137 -15
- package/src/pair-session.ts +100 -0
- package/src/provider.test.ts +704 -1
- package/src/provider.ts +933 -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.test.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import WebSocket from "ws";
|
|
10
10
|
import { createRelayServer } from "../../../services/relay-server/src/relay-server.js";
|
|
11
11
|
import { EchoBridge } from "./bridges/echo-bridge.js";
|
|
12
|
-
import { PrivateClawProvider } from "./provider.js";
|
|
12
|
+
import { DEFAULT_SESSION_TTL_MS, PrivateClawProvider } from "./provider.js";
|
|
13
13
|
|
|
14
14
|
function waitForOpen(socket: WebSocket): Promise<void> {
|
|
15
15
|
return new Promise((resolve, reject) => {
|
|
@@ -64,6 +64,34 @@ function waitForClose(socket: WebSocket): Promise<void> {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
function waitForNoMessage(socket: WebSocket, delayMs = 60): Promise<void> {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const timer = setTimeout(() => {
|
|
70
|
+
cleanup();
|
|
71
|
+
resolve();
|
|
72
|
+
}, delayMs);
|
|
73
|
+
|
|
74
|
+
const handleMessage = (data: WebSocket.RawData) => {
|
|
75
|
+
cleanup();
|
|
76
|
+
reject(new Error(`unexpected message: ${data.toString()}`));
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const handleError = (error: Error) => {
|
|
80
|
+
cleanup();
|
|
81
|
+
reject(error);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const cleanup = () => {
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
socket.off("message", handleMessage);
|
|
87
|
+
socket.off("error", handleError);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
socket.on("message", handleMessage);
|
|
91
|
+
socket.on("error", handleError);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
67
95
|
async function nextRelayFrame(socket: WebSocket): Promise<Record<string, unknown>> {
|
|
68
96
|
while (true) {
|
|
69
97
|
const message = await nextMessage(socket);
|
|
@@ -96,6 +124,23 @@ function decryptRelayPayload(
|
|
|
96
124
|
});
|
|
97
125
|
}
|
|
98
126
|
|
|
127
|
+
class GroupBridge {
|
|
128
|
+
readonly nicknameSessionIds: string[] = [];
|
|
129
|
+
readonly conversationMessages: string[] = [];
|
|
130
|
+
|
|
131
|
+
async handleUserMessage(params: {
|
|
132
|
+
sessionId: string;
|
|
133
|
+
message: string;
|
|
134
|
+
}): Promise<string> {
|
|
135
|
+
if (params.message.includes("Generate one playful nickname")) {
|
|
136
|
+
this.nicknameSessionIds.push(params.sessionId);
|
|
137
|
+
return "流萤狐";
|
|
138
|
+
}
|
|
139
|
+
this.conversationMessages.push(params.message);
|
|
140
|
+
return `OpenClaw bridge: ${params.message}`;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
99
144
|
test("provider creates one-time invites and replies through the encrypted relay", async (t) => {
|
|
100
145
|
const relay = createRelayServer({
|
|
101
146
|
host: "127.0.0.1",
|
|
@@ -112,6 +157,7 @@ test("provider creates one-time invites and replies through the encrypted relay"
|
|
|
112
157
|
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
113
158
|
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
114
159
|
bridge: new EchoBridge("OpenClaw bridge"),
|
|
160
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
115
161
|
welcomeMessage: "欢迎来到 PrivateClaw",
|
|
116
162
|
});
|
|
117
163
|
await provider.connect();
|
|
@@ -199,6 +245,7 @@ test("provider renews the session key and keeps the chat usable after re-handsha
|
|
|
199
245
|
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
200
246
|
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
201
247
|
bridge: new EchoBridge("OpenClaw bridge"),
|
|
248
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
202
249
|
});
|
|
203
250
|
await provider.connect();
|
|
204
251
|
t.after(async () => {
|
|
@@ -309,3 +356,659 @@ test("provider renews the session key and keeps the chat usable after re-handsha
|
|
|
309
356
|
appSocket.close();
|
|
310
357
|
await waitForClose(appSocket);
|
|
311
358
|
});
|
|
359
|
+
|
|
360
|
+
test("provider group sessions broadcast participant messages and assign stable participant labels", async (t) => {
|
|
361
|
+
const relay = createRelayServer({
|
|
362
|
+
host: "127.0.0.1",
|
|
363
|
+
port: 0,
|
|
364
|
+
sessionTtlMs: 60_000,
|
|
365
|
+
frameCacheSize: 8,
|
|
366
|
+
});
|
|
367
|
+
const { port } = await relay.start();
|
|
368
|
+
t.after(async () => {
|
|
369
|
+
await relay.stop();
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
const bridge = new GroupBridge();
|
|
373
|
+
const provider = new PrivateClawProvider({
|
|
374
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
375
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
376
|
+
bridge,
|
|
377
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
378
|
+
});
|
|
379
|
+
await provider.connect();
|
|
380
|
+
t.after(async () => {
|
|
381
|
+
await provider.dispose();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const inviteBundle = await provider.createInviteBundle({ groupMode: true });
|
|
385
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
386
|
+
|
|
387
|
+
const appOneUrl = new URL(invite.appWsUrl);
|
|
388
|
+
appOneUrl.searchParams.set("appId", "app-one");
|
|
389
|
+
const appOne = new WebSocket(appOneUrl.toString());
|
|
390
|
+
const appOneAttached = nextMessage(appOne);
|
|
391
|
+
await waitForOpen(appOne);
|
|
392
|
+
assert.equal((await appOneAttached).type, "relay:attached");
|
|
393
|
+
const firstFramesPromise = nextMessages(appOne, 3);
|
|
394
|
+
appOne.send(
|
|
395
|
+
JSON.stringify({
|
|
396
|
+
type: "app:frame",
|
|
397
|
+
envelope: encryptPayload({
|
|
398
|
+
sessionId: invite.sessionId,
|
|
399
|
+
sessionKey: invite.sessionKey,
|
|
400
|
+
payload: {
|
|
401
|
+
kind: "client_hello",
|
|
402
|
+
appId: "app-one",
|
|
403
|
+
appVersion: "flutter-test",
|
|
404
|
+
deviceLabel: "Tester One",
|
|
405
|
+
sentAt: new Date().toISOString(),
|
|
406
|
+
},
|
|
407
|
+
}),
|
|
408
|
+
}),
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
const firstFrames = await firstFramesPromise;
|
|
412
|
+
const firstCapabilities = decryptRelayPayload(firstFrames[1]!, invite);
|
|
413
|
+
assert.equal(firstCapabilities.kind, "provider_capabilities");
|
|
414
|
+
assert.equal(firstCapabilities.groupMode, true);
|
|
415
|
+
assert.equal(firstCapabilities.currentDisplayName, "流萤狐");
|
|
416
|
+
assert.equal(bridge.nicknameSessionIds.length, 1);
|
|
417
|
+
assert.match(
|
|
418
|
+
bridge.nicknameSessionIds[0]!,
|
|
419
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
420
|
+
);
|
|
421
|
+
assert.doesNotMatch(bridge.nicknameSessionIds[0]!, /:participant:/);
|
|
422
|
+
assert.notEqual(bridge.nicknameSessionIds[0], invite.sessionId);
|
|
423
|
+
|
|
424
|
+
const appTwoUrl = new URL(invite.appWsUrl);
|
|
425
|
+
appTwoUrl.searchParams.set("appId", "app-two");
|
|
426
|
+
const appTwo = new WebSocket(appTwoUrl.toString());
|
|
427
|
+
const appTwoAttached = nextMessage(appTwo);
|
|
428
|
+
await waitForOpen(appTwo);
|
|
429
|
+
assert.equal((await appTwoAttached).type, "relay:attached");
|
|
430
|
+
const appTwoFramesPromise = nextMessages(appTwo, 3);
|
|
431
|
+
const appOneJoinFramesPromise = nextMessages(appOne, 2);
|
|
432
|
+
appTwo.send(
|
|
433
|
+
JSON.stringify({
|
|
434
|
+
type: "app:frame",
|
|
435
|
+
envelope: encryptPayload({
|
|
436
|
+
sessionId: invite.sessionId,
|
|
437
|
+
sessionKey: invite.sessionKey,
|
|
438
|
+
payload: {
|
|
439
|
+
kind: "client_hello",
|
|
440
|
+
appId: "app-two",
|
|
441
|
+
appVersion: "flutter-test",
|
|
442
|
+
deviceLabel: "Tester Two",
|
|
443
|
+
sentAt: new Date().toISOString(),
|
|
444
|
+
},
|
|
445
|
+
}),
|
|
446
|
+
}),
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
const [appTwoFrames, appOneJoinFrames] = await Promise.all([
|
|
450
|
+
appTwoFramesPromise,
|
|
451
|
+
appOneJoinFramesPromise,
|
|
452
|
+
]);
|
|
453
|
+
const appOneJoinNotice = decryptRelayPayload(appOneJoinFrames[0]!, invite);
|
|
454
|
+
assert.equal(appOneJoinNotice.kind, "system_message");
|
|
455
|
+
assert.match(appOneJoinNotice.message, /joined the group chat/i);
|
|
456
|
+
|
|
457
|
+
const appTwoCapabilities = decryptRelayPayload(appTwoFrames[1]!, invite);
|
|
458
|
+
assert.equal(appTwoCapabilities.kind, "provider_capabilities");
|
|
459
|
+
assert.equal(appTwoCapabilities.groupMode, true);
|
|
460
|
+
|
|
461
|
+
const appOnePayloadFramesPromise = nextMessages(appOne, 2);
|
|
462
|
+
const appTwoPayloadFramesPromise = nextMessages(appTwo, 2);
|
|
463
|
+
appOne.send(
|
|
464
|
+
JSON.stringify({
|
|
465
|
+
type: "app:frame",
|
|
466
|
+
envelope: encryptPayload({
|
|
467
|
+
sessionId: invite.sessionId,
|
|
468
|
+
sessionKey: invite.sessionKey,
|
|
469
|
+
payload: {
|
|
470
|
+
kind: "user_message",
|
|
471
|
+
appId: "app-one",
|
|
472
|
+
text: "hello team",
|
|
473
|
+
clientMessageId: "group-message-1",
|
|
474
|
+
sentAt: new Date().toISOString(),
|
|
475
|
+
},
|
|
476
|
+
}),
|
|
477
|
+
}),
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
const [appOnePayloadFrames, appTwoPayloadFrames] = await Promise.all([
|
|
481
|
+
appOnePayloadFramesPromise,
|
|
482
|
+
appTwoPayloadFramesPromise,
|
|
483
|
+
]);
|
|
484
|
+
const appOneParticipantFrame = decryptRelayPayload(
|
|
485
|
+
appOnePayloadFrames[0]!,
|
|
486
|
+
invite,
|
|
487
|
+
);
|
|
488
|
+
const appTwoParticipantFrame = decryptRelayPayload(
|
|
489
|
+
appTwoPayloadFrames[0]!,
|
|
490
|
+
invite,
|
|
491
|
+
);
|
|
492
|
+
assert.equal(appOneParticipantFrame.kind, "participant_message");
|
|
493
|
+
assert.equal(appTwoParticipantFrame.kind, "participant_message");
|
|
494
|
+
assert.equal(appTwoParticipantFrame.senderDisplayName, "流萤狐");
|
|
495
|
+
assert.equal(appTwoParticipantFrame.text, "hello team");
|
|
496
|
+
|
|
497
|
+
const appOneAssistant = decryptRelayPayload(appOnePayloadFrames[1]!, invite);
|
|
498
|
+
const appTwoAssistant = decryptRelayPayload(appTwoPayloadFrames[1]!, invite);
|
|
499
|
+
assert.equal(appOneAssistant.kind, "assistant_message");
|
|
500
|
+
assert.equal(appTwoAssistant.kind, "assistant_message");
|
|
501
|
+
assert.match(appTwoAssistant.text, /流萤狐: hello team/);
|
|
502
|
+
|
|
503
|
+
appOne.close();
|
|
504
|
+
appTwo.close();
|
|
505
|
+
await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
test("group sessions can mute and unmute bot replies without stopping participant chat", async (t) => {
|
|
509
|
+
const relay = createRelayServer({
|
|
510
|
+
host: "127.0.0.1",
|
|
511
|
+
port: 0,
|
|
512
|
+
sessionTtlMs: 60_000,
|
|
513
|
+
frameCacheSize: 8,
|
|
514
|
+
});
|
|
515
|
+
const { port } = await relay.start();
|
|
516
|
+
t.after(async () => {
|
|
517
|
+
await relay.stop();
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const bridge = new GroupBridge();
|
|
521
|
+
const provider = new PrivateClawProvider({
|
|
522
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
523
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
524
|
+
bridge,
|
|
525
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
526
|
+
});
|
|
527
|
+
await provider.connect();
|
|
528
|
+
t.after(async () => {
|
|
529
|
+
await provider.dispose();
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
const inviteBundle = await provider.createInviteBundle({ groupMode: true });
|
|
533
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
534
|
+
|
|
535
|
+
const appOneUrl = new URL(invite.appWsUrl);
|
|
536
|
+
appOneUrl.searchParams.set("appId", "app-one");
|
|
537
|
+
const appOne = new WebSocket(appOneUrl.toString());
|
|
538
|
+
const appOneAttached = nextMessage(appOne);
|
|
539
|
+
await waitForOpen(appOne);
|
|
540
|
+
assert.equal((await appOneAttached).type, "relay:attached");
|
|
541
|
+
const appOneInitialFrames = nextMessages(appOne, 3);
|
|
542
|
+
appOne.send(
|
|
543
|
+
JSON.stringify({
|
|
544
|
+
type: "app:frame",
|
|
545
|
+
envelope: encryptPayload({
|
|
546
|
+
sessionId: invite.sessionId,
|
|
547
|
+
sessionKey: invite.sessionKey,
|
|
548
|
+
payload: {
|
|
549
|
+
kind: "client_hello",
|
|
550
|
+
appId: "app-one",
|
|
551
|
+
displayName: "SolarFox",
|
|
552
|
+
appVersion: "flutter-test",
|
|
553
|
+
deviceLabel: "Tester One",
|
|
554
|
+
sentAt: new Date().toISOString(),
|
|
555
|
+
},
|
|
556
|
+
}),
|
|
557
|
+
}),
|
|
558
|
+
);
|
|
559
|
+
await appOneInitialFrames;
|
|
560
|
+
|
|
561
|
+
const appTwoUrl = new URL(invite.appWsUrl);
|
|
562
|
+
appTwoUrl.searchParams.set("appId", "app-two");
|
|
563
|
+
const appTwo = new WebSocket(appTwoUrl.toString());
|
|
564
|
+
const appTwoAttached = nextMessage(appTwo);
|
|
565
|
+
await waitForOpen(appTwo);
|
|
566
|
+
assert.equal((await appTwoAttached).type, "relay:attached");
|
|
567
|
+
const appTwoInitialFrames = nextMessages(appTwo, 3);
|
|
568
|
+
const appOneJoinFrames = nextMessages(appOne, 2);
|
|
569
|
+
appTwo.send(
|
|
570
|
+
JSON.stringify({
|
|
571
|
+
type: "app:frame",
|
|
572
|
+
envelope: encryptPayload({
|
|
573
|
+
sessionId: invite.sessionId,
|
|
574
|
+
sessionKey: invite.sessionKey,
|
|
575
|
+
payload: {
|
|
576
|
+
kind: "client_hello",
|
|
577
|
+
appId: "app-two",
|
|
578
|
+
displayName: "RiverCat",
|
|
579
|
+
appVersion: "flutter-test",
|
|
580
|
+
deviceLabel: "Tester Two",
|
|
581
|
+
sentAt: new Date().toISOString(),
|
|
582
|
+
},
|
|
583
|
+
}),
|
|
584
|
+
}),
|
|
585
|
+
);
|
|
586
|
+
await Promise.all([appTwoInitialFrames, appOneJoinFrames]);
|
|
587
|
+
|
|
588
|
+
const muteAppOneFramesPromise = nextMessages(appOne, 2);
|
|
589
|
+
const muteAppTwoFramesPromise = nextMessages(appTwo, 2);
|
|
590
|
+
appOne.send(
|
|
591
|
+
JSON.stringify({
|
|
592
|
+
type: "app:frame",
|
|
593
|
+
envelope: encryptPayload({
|
|
594
|
+
sessionId: invite.sessionId,
|
|
595
|
+
sessionKey: invite.sessionKey,
|
|
596
|
+
payload: {
|
|
597
|
+
kind: "user_message",
|
|
598
|
+
appId: "app-one",
|
|
599
|
+
text: "/mute-bot",
|
|
600
|
+
clientMessageId: "mute-bot-1",
|
|
601
|
+
sentAt: new Date().toISOString(),
|
|
602
|
+
},
|
|
603
|
+
}),
|
|
604
|
+
}),
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
const [muteAppOneFrames, muteAppTwoFrames] = await Promise.all([
|
|
608
|
+
muteAppOneFramesPromise,
|
|
609
|
+
muteAppTwoFramesPromise,
|
|
610
|
+
]);
|
|
611
|
+
const muteNotice = decryptRelayPayload(muteAppOneFrames[0]!, invite);
|
|
612
|
+
assert.equal(muteNotice.kind, "system_message");
|
|
613
|
+
assert.match(muteNotice.message, /暂停机器人|pause(?:d)? bot replies/i);
|
|
614
|
+
const muteCapabilities = decryptRelayPayload(muteAppOneFrames[1]!, invite);
|
|
615
|
+
assert.equal(muteCapabilities.kind, "provider_capabilities");
|
|
616
|
+
assert.equal(muteCapabilities.botMuted, true);
|
|
617
|
+
assert.ok(
|
|
618
|
+
muteCapabilities.commands.some((command) => command.slash === "/unmute-bot"),
|
|
619
|
+
);
|
|
620
|
+
assert.ok(
|
|
621
|
+
muteCapabilities.commands.every((command) => command.slash !== "/mute-bot"),
|
|
622
|
+
);
|
|
623
|
+
const peerMuteCapabilities = decryptRelayPayload(muteAppTwoFrames[1]!, invite);
|
|
624
|
+
assert.equal(peerMuteCapabilities.kind, "provider_capabilities");
|
|
625
|
+
assert.equal(peerMuteCapabilities.botMuted, true);
|
|
626
|
+
|
|
627
|
+
const bridgeCallsBeforeMutedMessage = bridge.conversationMessages.length;
|
|
628
|
+
const mutedAppOneFramesPromise = nextMessages(appOne, 1);
|
|
629
|
+
const mutedAppTwoFramesPromise = nextMessages(appTwo, 1);
|
|
630
|
+
appTwo.send(
|
|
631
|
+
JSON.stringify({
|
|
632
|
+
type: "app:frame",
|
|
633
|
+
envelope: encryptPayload({
|
|
634
|
+
sessionId: invite.sessionId,
|
|
635
|
+
sessionKey: invite.sessionKey,
|
|
636
|
+
payload: {
|
|
637
|
+
kind: "user_message",
|
|
638
|
+
appId: "app-two",
|
|
639
|
+
text: "please stay quiet",
|
|
640
|
+
clientMessageId: "muted-message-1",
|
|
641
|
+
sentAt: new Date().toISOString(),
|
|
642
|
+
},
|
|
643
|
+
}),
|
|
644
|
+
}),
|
|
645
|
+
);
|
|
646
|
+
|
|
647
|
+
const [mutedAppOneFrames, mutedAppTwoFrames] = await Promise.all([
|
|
648
|
+
mutedAppOneFramesPromise,
|
|
649
|
+
mutedAppTwoFramesPromise,
|
|
650
|
+
]);
|
|
651
|
+
const mutedParticipantForAppOne = decryptRelayPayload(
|
|
652
|
+
mutedAppOneFrames[0]!,
|
|
653
|
+
invite,
|
|
654
|
+
);
|
|
655
|
+
const mutedParticipantForAppTwo = decryptRelayPayload(
|
|
656
|
+
mutedAppTwoFrames[0]!,
|
|
657
|
+
invite,
|
|
658
|
+
);
|
|
659
|
+
assert.equal(mutedParticipantForAppOne.kind, "participant_message");
|
|
660
|
+
assert.equal(mutedParticipantForAppTwo.kind, "participant_message");
|
|
661
|
+
assert.equal(bridge.conversationMessages.length, bridgeCallsBeforeMutedMessage);
|
|
662
|
+
await Promise.all([waitForNoMessage(appOne), waitForNoMessage(appTwo)]);
|
|
663
|
+
|
|
664
|
+
const unmuteAppOneFramesPromise = nextMessages(appOne, 2);
|
|
665
|
+
const unmuteAppTwoFramesPromise = nextMessages(appTwo, 2);
|
|
666
|
+
appTwo.send(
|
|
667
|
+
JSON.stringify({
|
|
668
|
+
type: "app:frame",
|
|
669
|
+
envelope: encryptPayload({
|
|
670
|
+
sessionId: invite.sessionId,
|
|
671
|
+
sessionKey: invite.sessionKey,
|
|
672
|
+
payload: {
|
|
673
|
+
kind: "user_message",
|
|
674
|
+
appId: "app-two",
|
|
675
|
+
text: "/unmute-bot",
|
|
676
|
+
clientMessageId: "unmute-bot-1",
|
|
677
|
+
sentAt: new Date().toISOString(),
|
|
678
|
+
},
|
|
679
|
+
}),
|
|
680
|
+
}),
|
|
681
|
+
);
|
|
682
|
+
|
|
683
|
+
const [unmuteAppOneFrames, unmuteAppTwoFrames] = await Promise.all([
|
|
684
|
+
unmuteAppOneFramesPromise,
|
|
685
|
+
unmuteAppTwoFramesPromise,
|
|
686
|
+
]);
|
|
687
|
+
const unmuteNotice = decryptRelayPayload(unmuteAppTwoFrames[0]!, invite);
|
|
688
|
+
assert.equal(unmuteNotice.kind, "system_message");
|
|
689
|
+
assert.match(unmuteNotice.message, /恢复机器人|resume(?:d)? bot replies/i);
|
|
690
|
+
const unmuteCapabilities = decryptRelayPayload(unmuteAppOneFrames[1]!, invite);
|
|
691
|
+
assert.equal(unmuteCapabilities.kind, "provider_capabilities");
|
|
692
|
+
assert.equal(unmuteCapabilities.botMuted, false);
|
|
693
|
+
assert.ok(
|
|
694
|
+
unmuteCapabilities.commands.some((command) => command.slash === "/mute-bot"),
|
|
695
|
+
);
|
|
696
|
+
assert.ok(
|
|
697
|
+
unmuteCapabilities.commands.every((command) => command.slash !== "/unmute-bot"),
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
const bridgeCallsBeforeResumedMessage = bridge.conversationMessages.length;
|
|
701
|
+
const resumedAppOneFramesPromise = nextMessages(appOne, 2);
|
|
702
|
+
const resumedAppTwoFramesPromise = nextMessages(appTwo, 2);
|
|
703
|
+
appOne.send(
|
|
704
|
+
JSON.stringify({
|
|
705
|
+
type: "app:frame",
|
|
706
|
+
envelope: encryptPayload({
|
|
707
|
+
sessionId: invite.sessionId,
|
|
708
|
+
sessionKey: invite.sessionKey,
|
|
709
|
+
payload: {
|
|
710
|
+
kind: "user_message",
|
|
711
|
+
appId: "app-one",
|
|
712
|
+
text: "bot come back",
|
|
713
|
+
clientMessageId: "resumed-message-1",
|
|
714
|
+
sentAt: new Date().toISOString(),
|
|
715
|
+
},
|
|
716
|
+
}),
|
|
717
|
+
}),
|
|
718
|
+
);
|
|
719
|
+
|
|
720
|
+
const [resumedAppOneFrames, resumedAppTwoFrames] = await Promise.all([
|
|
721
|
+
resumedAppOneFramesPromise,
|
|
722
|
+
resumedAppTwoFramesPromise,
|
|
723
|
+
]);
|
|
724
|
+
const resumedAssistantForAppOne = decryptRelayPayload(
|
|
725
|
+
resumedAppOneFrames[1]!,
|
|
726
|
+
invite,
|
|
727
|
+
);
|
|
728
|
+
const resumedAssistantForAppTwo = decryptRelayPayload(
|
|
729
|
+
resumedAppTwoFrames[1]!,
|
|
730
|
+
invite,
|
|
731
|
+
);
|
|
732
|
+
assert.equal(resumedAssistantForAppOne.kind, "assistant_message");
|
|
733
|
+
assert.equal(resumedAssistantForAppTwo.kind, "assistant_message");
|
|
734
|
+
assert.match(resumedAssistantForAppTwo.text, /SolarFox: bot come back/);
|
|
735
|
+
assert.equal(
|
|
736
|
+
bridge.conversationMessages.length,
|
|
737
|
+
bridgeCallsBeforeResumedMessage + 1,
|
|
738
|
+
);
|
|
739
|
+
|
|
740
|
+
appOne.close();
|
|
741
|
+
appTwo.close();
|
|
742
|
+
await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
test("group sessions keep running when one participant leaves and later rejoins", async (t) => {
|
|
746
|
+
const relay = createRelayServer({
|
|
747
|
+
host: "127.0.0.1",
|
|
748
|
+
port: 0,
|
|
749
|
+
sessionTtlMs: 60_000,
|
|
750
|
+
frameCacheSize: 8,
|
|
751
|
+
});
|
|
752
|
+
const { port } = await relay.start();
|
|
753
|
+
t.after(async () => {
|
|
754
|
+
await relay.stop();
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
const provider = new PrivateClawProvider({
|
|
758
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
759
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
760
|
+
bridge: new GroupBridge(),
|
|
761
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
762
|
+
});
|
|
763
|
+
await provider.connect();
|
|
764
|
+
t.after(async () => {
|
|
765
|
+
await provider.dispose();
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
const inviteBundle = await provider.createInviteBundle({ groupMode: true });
|
|
769
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
770
|
+
|
|
771
|
+
const appOneUrl = new URL(invite.appWsUrl);
|
|
772
|
+
appOneUrl.searchParams.set("appId", "app-one");
|
|
773
|
+
const appOne = new WebSocket(appOneUrl.toString());
|
|
774
|
+
const appOneAttached = nextMessage(appOne);
|
|
775
|
+
await waitForOpen(appOne);
|
|
776
|
+
assert.equal((await appOneAttached).type, "relay:attached");
|
|
777
|
+
const appOneInitialFrames = nextMessages(appOne, 3);
|
|
778
|
+
appOne.send(
|
|
779
|
+
JSON.stringify({
|
|
780
|
+
type: "app:frame",
|
|
781
|
+
envelope: encryptPayload({
|
|
782
|
+
sessionId: invite.sessionId,
|
|
783
|
+
sessionKey: invite.sessionKey,
|
|
784
|
+
payload: {
|
|
785
|
+
kind: "client_hello",
|
|
786
|
+
appId: "app-one",
|
|
787
|
+
displayName: "SolarFox",
|
|
788
|
+
appVersion: "flutter-test",
|
|
789
|
+
deviceLabel: "Tester One",
|
|
790
|
+
sentAt: new Date().toISOString(),
|
|
791
|
+
},
|
|
792
|
+
}),
|
|
793
|
+
}),
|
|
794
|
+
);
|
|
795
|
+
await appOneInitialFrames;
|
|
796
|
+
|
|
797
|
+
const appTwoUrl = new URL(invite.appWsUrl);
|
|
798
|
+
appTwoUrl.searchParams.set("appId", "app-two");
|
|
799
|
+
const appTwo = new WebSocket(appTwoUrl.toString());
|
|
800
|
+
const appTwoAttached = nextMessage(appTwo);
|
|
801
|
+
await waitForOpen(appTwo);
|
|
802
|
+
assert.equal((await appTwoAttached).type, "relay:attached");
|
|
803
|
+
const appTwoInitialFrames = nextMessages(appTwo, 3);
|
|
804
|
+
const appOneJoinFrames = nextMessages(appOne, 2);
|
|
805
|
+
appTwo.send(
|
|
806
|
+
JSON.stringify({
|
|
807
|
+
type: "app:frame",
|
|
808
|
+
envelope: encryptPayload({
|
|
809
|
+
sessionId: invite.sessionId,
|
|
810
|
+
sessionKey: invite.sessionKey,
|
|
811
|
+
payload: {
|
|
812
|
+
kind: "client_hello",
|
|
813
|
+
appId: "app-two",
|
|
814
|
+
displayName: "RiverCat",
|
|
815
|
+
appVersion: "flutter-test",
|
|
816
|
+
deviceLabel: "Tester Two",
|
|
817
|
+
sentAt: new Date().toISOString(),
|
|
818
|
+
},
|
|
819
|
+
}),
|
|
820
|
+
}),
|
|
821
|
+
);
|
|
822
|
+
await appTwoInitialFrames;
|
|
823
|
+
const initialJoinNotice = decryptRelayPayload((await appOneJoinFrames)[0]!, invite);
|
|
824
|
+
assert.equal(initialJoinNotice.kind, "system_message");
|
|
825
|
+
assert.match(initialJoinNotice.message, /RiverCat joined the group chat/i);
|
|
826
|
+
|
|
827
|
+
const appOneLeaveFramesPromise = nextMessages(appOne, 2);
|
|
828
|
+
appTwo.send(
|
|
829
|
+
JSON.stringify({
|
|
830
|
+
type: "app:frame",
|
|
831
|
+
envelope: encryptPayload({
|
|
832
|
+
sessionId: invite.sessionId,
|
|
833
|
+
sessionKey: invite.sessionKey,
|
|
834
|
+
payload: {
|
|
835
|
+
kind: "session_close",
|
|
836
|
+
reason: "user_disconnect",
|
|
837
|
+
appId: "app-two",
|
|
838
|
+
sentAt: new Date().toISOString(),
|
|
839
|
+
},
|
|
840
|
+
}),
|
|
841
|
+
}),
|
|
842
|
+
);
|
|
843
|
+
|
|
844
|
+
const appOneLeaveFrames = await appOneLeaveFramesPromise;
|
|
845
|
+
const leaveNotice = decryptRelayPayload(appOneLeaveFrames[0]!, invite);
|
|
846
|
+
assert.equal(leaveNotice.kind, "system_message");
|
|
847
|
+
assert.match(leaveNotice.message, /RiverCat left the group chat/i);
|
|
848
|
+
const leaveCapabilities = decryptRelayPayload(appOneLeaveFrames[1]!, invite);
|
|
849
|
+
assert.equal(leaveCapabilities.kind, "provider_capabilities");
|
|
850
|
+
assert.deepEqual(
|
|
851
|
+
leaveCapabilities.participants?.map((participant) => participant.displayName),
|
|
852
|
+
["SolarFox"],
|
|
853
|
+
);
|
|
854
|
+
|
|
855
|
+
appTwo.close();
|
|
856
|
+
await waitForClose(appTwo);
|
|
857
|
+
|
|
858
|
+
const appTwoReconnect = new WebSocket(appTwoUrl.toString());
|
|
859
|
+
const appTwoReconnectAttached = nextMessage(appTwoReconnect);
|
|
860
|
+
await waitForOpen(appTwoReconnect);
|
|
861
|
+
assert.equal((await appTwoReconnectAttached).type, "relay:attached");
|
|
862
|
+
const appTwoReconnectFramesPromise = nextMessages(appTwoReconnect, 5);
|
|
863
|
+
const appOneRejoinFramesPromise = nextMessages(appOne, 2);
|
|
864
|
+
appTwoReconnect.send(
|
|
865
|
+
JSON.stringify({
|
|
866
|
+
type: "app:frame",
|
|
867
|
+
envelope: encryptPayload({
|
|
868
|
+
sessionId: invite.sessionId,
|
|
869
|
+
sessionKey: invite.sessionKey,
|
|
870
|
+
payload: {
|
|
871
|
+
kind: "client_hello",
|
|
872
|
+
appId: "app-two",
|
|
873
|
+
displayName: "RiverCat",
|
|
874
|
+
appVersion: "flutter-test",
|
|
875
|
+
deviceLabel: "Tester Two",
|
|
876
|
+
sentAt: new Date().toISOString(),
|
|
877
|
+
},
|
|
878
|
+
}),
|
|
879
|
+
}),
|
|
880
|
+
);
|
|
881
|
+
|
|
882
|
+
const [appTwoReconnectFrames, appOneRejoinFrames] = await Promise.all([
|
|
883
|
+
appTwoReconnectFramesPromise,
|
|
884
|
+
appOneRejoinFramesPromise,
|
|
885
|
+
]);
|
|
886
|
+
const rejoinNotice = decryptRelayPayload(appOneRejoinFrames[0]!, invite);
|
|
887
|
+
assert.equal(rejoinNotice.kind, "system_message");
|
|
888
|
+
assert.match(rejoinNotice.message, /RiverCat joined the group chat/i);
|
|
889
|
+
|
|
890
|
+
const reconnectPayloads = appTwoReconnectFrames.map((frame) =>
|
|
891
|
+
decryptRelayPayload(frame, invite),
|
|
892
|
+
);
|
|
893
|
+
const reconnectCapabilities = reconnectPayloads.find(
|
|
894
|
+
(payload) =>
|
|
895
|
+
payload.kind === "provider_capabilities" &&
|
|
896
|
+
payload.currentAppId === "app-two",
|
|
897
|
+
);
|
|
898
|
+
assert.ok(reconnectCapabilities);
|
|
899
|
+
assert.equal(reconnectCapabilities.kind, "provider_capabilities");
|
|
900
|
+
assert.equal(reconnectCapabilities.currentDisplayName, "RiverCat");
|
|
901
|
+
|
|
902
|
+
const appOnePayloadFramesPromise = nextMessages(appOne, 2);
|
|
903
|
+
const appTwoPayloadFramesPromise = nextMessages(appTwoReconnect, 2);
|
|
904
|
+
appTwoReconnect.send(
|
|
905
|
+
JSON.stringify({
|
|
906
|
+
type: "app:frame",
|
|
907
|
+
envelope: encryptPayload({
|
|
908
|
+
sessionId: invite.sessionId,
|
|
909
|
+
sessionKey: invite.sessionKey,
|
|
910
|
+
payload: {
|
|
911
|
+
kind: "user_message",
|
|
912
|
+
appId: "app-two",
|
|
913
|
+
text: "back again",
|
|
914
|
+
clientMessageId: "group-rejoin-message",
|
|
915
|
+
sentAt: new Date().toISOString(),
|
|
916
|
+
},
|
|
917
|
+
}),
|
|
918
|
+
}),
|
|
919
|
+
);
|
|
920
|
+
|
|
921
|
+
const [appOnePayloadFrames, appTwoPayloadFrames] = await Promise.all([
|
|
922
|
+
appOnePayloadFramesPromise,
|
|
923
|
+
appTwoPayloadFramesPromise,
|
|
924
|
+
]);
|
|
925
|
+
const appOneParticipantFrame = decryptRelayPayload(
|
|
926
|
+
appOnePayloadFrames[0]!,
|
|
927
|
+
invite,
|
|
928
|
+
);
|
|
929
|
+
const appTwoParticipantFrame = decryptRelayPayload(
|
|
930
|
+
appTwoPayloadFrames[0]!,
|
|
931
|
+
invite,
|
|
932
|
+
);
|
|
933
|
+
assert.equal(appOneParticipantFrame.kind, "participant_message");
|
|
934
|
+
assert.equal(appTwoParticipantFrame.kind, "participant_message");
|
|
935
|
+
assert.equal(appOneParticipantFrame.senderDisplayName, "RiverCat");
|
|
936
|
+
assert.equal(appTwoParticipantFrame.senderDisplayName, "RiverCat");
|
|
937
|
+
|
|
938
|
+
const appOneAssistant = decryptRelayPayload(appOnePayloadFrames[1]!, invite);
|
|
939
|
+
const appTwoAssistant = decryptRelayPayload(appTwoPayloadFrames[1]!, invite);
|
|
940
|
+
assert.equal(appOneAssistant.kind, "assistant_message");
|
|
941
|
+
assert.equal(appTwoAssistant.kind, "assistant_message");
|
|
942
|
+
assert.match(appOneAssistant.text, /RiverCat: back again/);
|
|
943
|
+
|
|
944
|
+
appOne.close();
|
|
945
|
+
appTwoReconnect.close();
|
|
946
|
+
await Promise.all([waitForClose(appOne), waitForClose(appTwoReconnect)]);
|
|
947
|
+
});
|
|
948
|
+
|
|
949
|
+
test("provider reminds users to renew when a session has less than 30 minutes left", async (t) => {
|
|
950
|
+
const relay = createRelayServer({
|
|
951
|
+
host: "127.0.0.1",
|
|
952
|
+
port: 0,
|
|
953
|
+
sessionTtlMs: 60_000,
|
|
954
|
+
frameCacheSize: 8,
|
|
955
|
+
});
|
|
956
|
+
const { port } = await relay.start();
|
|
957
|
+
t.after(async () => {
|
|
958
|
+
await relay.stop();
|
|
959
|
+
});
|
|
960
|
+
|
|
961
|
+
const provider = new PrivateClawProvider({
|
|
962
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
963
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
964
|
+
bridge: new EchoBridge("OpenClaw bridge"),
|
|
965
|
+
defaultTtlMs: 10 * 60 * 1000,
|
|
966
|
+
});
|
|
967
|
+
await provider.connect();
|
|
968
|
+
t.after(async () => {
|
|
969
|
+
await provider.dispose();
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
const inviteBundle = await provider.createInviteBundle();
|
|
973
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
974
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
975
|
+
|
|
976
|
+
const appSocket = new WebSocket(invite.appWsUrl);
|
|
977
|
+
const incomingPromise = nextMessages(appSocket, 4);
|
|
978
|
+
await waitForOpen(appSocket);
|
|
979
|
+
appSocket.send(
|
|
980
|
+
JSON.stringify({
|
|
981
|
+
type: "app:frame",
|
|
982
|
+
envelope: encryptPayload({
|
|
983
|
+
sessionId: invite.sessionId,
|
|
984
|
+
sessionKey: invite.sessionKey,
|
|
985
|
+
payload: {
|
|
986
|
+
kind: "client_hello",
|
|
987
|
+
appVersion: "flutter-test",
|
|
988
|
+
deviceLabel: "Simulator",
|
|
989
|
+
sentAt: new Date().toISOString(),
|
|
990
|
+
},
|
|
991
|
+
}),
|
|
992
|
+
}),
|
|
993
|
+
);
|
|
994
|
+
|
|
995
|
+
const incoming = await incomingPromise;
|
|
996
|
+
assert.equal(incoming[0]?.type, "relay:attached");
|
|
997
|
+
const framePayloads = incoming
|
|
998
|
+
.slice(1)
|
|
999
|
+
.map((frame) => decryptRelayPayload(frame, invite));
|
|
1000
|
+
assert.ok(
|
|
1001
|
+
framePayloads.some(
|
|
1002
|
+
(payload) =>
|
|
1003
|
+
payload.kind === "system_message" &&
|
|
1004
|
+
/less than 30 minutes/i.test(payload.message) &&
|
|
1005
|
+
/renew-session/i.test(payload.message),
|
|
1006
|
+
),
|
|
1007
|
+
);
|
|
1008
|
+
assert.ok(
|
|
1009
|
+
framePayloads.some((payload) => payload.kind === "provider_capabilities"),
|
|
1010
|
+
);
|
|
1011
|
+
|
|
1012
|
+
appSocket.close();
|
|
1013
|
+
await waitForClose(appSocket);
|
|
1014
|
+
});
|