@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.
Files changed (55) hide show
  1. package/README.md +103 -8
  2. package/dist/bridges/openclaw-agent-bridge.d.ts +5 -0
  3. package/dist/bridges/openclaw-agent-bridge.js +228 -15
  4. package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
  5. package/dist/cli.js +11 -19
  6. package/dist/cli.js.map +1 -1
  7. package/dist/compat/openclaw.d.ts +18 -1
  8. package/dist/compat/openclaw.js +3 -1
  9. package/dist/compat/openclaw.js.map +1 -1
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/openclaw-command-discovery.d.ts +1 -0
  14. package/dist/openclaw-command-discovery.js +67 -14
  15. package/dist/openclaw-command-discovery.js.map +1 -1
  16. package/dist/openclaw-plugin.js +90 -15
  17. package/dist/openclaw-plugin.js.map +1 -1
  18. package/dist/pair-session.d.ts +12 -0
  19. package/dist/pair-session.js +63 -0
  20. package/dist/pair-session.js.map +1 -0
  21. package/dist/provider.d.ts +22 -1
  22. package/dist/provider.js +625 -60
  23. package/dist/provider.js.map +1 -1
  24. package/dist/qr-options.d.ts +5 -0
  25. package/dist/qr-options.js +6 -0
  26. package/dist/qr-options.js.map +1 -0
  27. package/dist/relay-defaults.d.ts +1 -0
  28. package/dist/relay-defaults.js +2 -0
  29. package/dist/relay-defaults.js.map +1 -0
  30. package/dist/relay-provider-client.d.ts +2 -2
  31. package/dist/relay-provider-client.js +9 -3
  32. package/dist/relay-provider-client.js.map +1 -1
  33. package/dist/text.d.ts +22 -0
  34. package/dist/text.js +32 -0
  35. package/dist/text.js.map +1 -0
  36. package/dist/types.d.ts +14 -1
  37. package/openclaw.plugin.json +3 -2
  38. package/package.json +1 -1
  39. package/src/bridges/openclaw-agent-bridge.test.ts +141 -0
  40. package/src/bridges/openclaw-agent-bridge.ts +302 -16
  41. package/src/cli.ts +14 -22
  42. package/src/compat/openclaw.ts +30 -1
  43. package/src/index.ts +1 -0
  44. package/src/openclaw-command-discovery.test.ts +108 -0
  45. package/src/openclaw-command-discovery.ts +119 -19
  46. package/src/openclaw-plugin.test.ts +139 -0
  47. package/src/openclaw-plugin.ts +137 -15
  48. package/src/pair-session.ts +100 -0
  49. package/src/provider.test.ts +714 -1
  50. package/src/provider.ts +952 -60
  51. package/src/qr-options.ts +5 -0
  52. package/src/relay-defaults.ts +1 -0
  53. package/src/relay-provider-client.ts +13 -2
  54. package/src/text.ts +108 -0
  55. package/src/types.ts +15 -0
@@ -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,669 @@ 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.equal(muteNotice.replyTo, "mute-bot-1");
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);
619
+ const muteCapabilities = decryptRelayPayload(muteAppOneFrames[1]!, invite);
620
+ assert.equal(muteCapabilities.kind, "provider_capabilities");
621
+ assert.equal(muteCapabilities.botMuted, true);
622
+ assert.ok(
623
+ muteCapabilities.commands.some((command) => command.slash === "/unmute-bot"),
624
+ );
625
+ assert.ok(
626
+ muteCapabilities.commands.every((command) => command.slash !== "/mute-bot"),
627
+ );
628
+ const peerMuteCapabilities = decryptRelayPayload(muteAppTwoFrames[1]!, invite);
629
+ assert.equal(peerMuteCapabilities.kind, "provider_capabilities");
630
+ assert.equal(peerMuteCapabilities.botMuted, true);
631
+
632
+ const bridgeCallsBeforeMutedMessage = bridge.conversationMessages.length;
633
+ const mutedAppOneFramesPromise = nextMessages(appOne, 1);
634
+ const mutedAppTwoFramesPromise = nextMessages(appTwo, 1);
635
+ appTwo.send(
636
+ JSON.stringify({
637
+ type: "app:frame",
638
+ envelope: encryptPayload({
639
+ sessionId: invite.sessionId,
640
+ sessionKey: invite.sessionKey,
641
+ payload: {
642
+ kind: "user_message",
643
+ appId: "app-two",
644
+ text: "please stay quiet",
645
+ clientMessageId: "muted-message-1",
646
+ sentAt: new Date().toISOString(),
647
+ },
648
+ }),
649
+ }),
650
+ );
651
+
652
+ const [mutedAppOneFrames, mutedAppTwoFrames] = await Promise.all([
653
+ mutedAppOneFramesPromise,
654
+ mutedAppTwoFramesPromise,
655
+ ]);
656
+ const mutedParticipantForAppOne = decryptRelayPayload(
657
+ mutedAppOneFrames[0]!,
658
+ invite,
659
+ );
660
+ const mutedParticipantForAppTwo = decryptRelayPayload(
661
+ mutedAppTwoFrames[0]!,
662
+ invite,
663
+ );
664
+ assert.equal(mutedParticipantForAppOne.kind, "participant_message");
665
+ assert.equal(mutedParticipantForAppTwo.kind, "participant_message");
666
+ assert.equal(bridge.conversationMessages.length, bridgeCallsBeforeMutedMessage);
667
+ await Promise.all([waitForNoMessage(appOne), waitForNoMessage(appTwo)]);
668
+
669
+ const unmuteAppOneFramesPromise = nextMessages(appOne, 2);
670
+ const unmuteAppTwoFramesPromise = nextMessages(appTwo, 2);
671
+ appTwo.send(
672
+ JSON.stringify({
673
+ type: "app:frame",
674
+ envelope: encryptPayload({
675
+ sessionId: invite.sessionId,
676
+ sessionKey: invite.sessionKey,
677
+ payload: {
678
+ kind: "user_message",
679
+ appId: "app-two",
680
+ text: "/unmute-bot",
681
+ clientMessageId: "unmute-bot-1",
682
+ sentAt: new Date().toISOString(),
683
+ },
684
+ }),
685
+ }),
686
+ );
687
+
688
+ const [unmuteAppOneFrames, unmuteAppTwoFrames] = await Promise.all([
689
+ unmuteAppOneFramesPromise,
690
+ unmuteAppTwoFramesPromise,
691
+ ]);
692
+ const peerUnmuteNotice = decryptRelayPayload(unmuteAppOneFrames[0]!, invite);
693
+ assert.equal(peerUnmuteNotice.kind, "system_message");
694
+ assert.equal(peerUnmuteNotice.replyTo, undefined);
695
+ const unmuteNotice = decryptRelayPayload(unmuteAppTwoFrames[0]!, invite);
696
+ assert.equal(unmuteNotice.kind, "system_message");
697
+ assert.equal(unmuteNotice.replyTo, "unmute-bot-1");
698
+ assert.equal(unmuteNotice.messageId, peerUnmuteNotice.messageId);
699
+ assert.match(unmuteNotice.message, /恢复机器人|resume(?:d)? bot replies/i);
700
+ const unmuteCapabilities = decryptRelayPayload(unmuteAppOneFrames[1]!, invite);
701
+ assert.equal(unmuteCapabilities.kind, "provider_capabilities");
702
+ assert.equal(unmuteCapabilities.botMuted, false);
703
+ assert.ok(
704
+ unmuteCapabilities.commands.some((command) => command.slash === "/mute-bot"),
705
+ );
706
+ assert.ok(
707
+ unmuteCapabilities.commands.every((command) => command.slash !== "/unmute-bot"),
708
+ );
709
+
710
+ const bridgeCallsBeforeResumedMessage = bridge.conversationMessages.length;
711
+ const resumedAppOneFramesPromise = nextMessages(appOne, 2);
712
+ const resumedAppTwoFramesPromise = nextMessages(appTwo, 2);
713
+ appOne.send(
714
+ JSON.stringify({
715
+ type: "app:frame",
716
+ envelope: encryptPayload({
717
+ sessionId: invite.sessionId,
718
+ sessionKey: invite.sessionKey,
719
+ payload: {
720
+ kind: "user_message",
721
+ appId: "app-one",
722
+ text: "bot come back",
723
+ clientMessageId: "resumed-message-1",
724
+ sentAt: new Date().toISOString(),
725
+ },
726
+ }),
727
+ }),
728
+ );
729
+
730
+ const [resumedAppOneFrames, resumedAppTwoFrames] = await Promise.all([
731
+ resumedAppOneFramesPromise,
732
+ resumedAppTwoFramesPromise,
733
+ ]);
734
+ const resumedAssistantForAppOne = decryptRelayPayload(
735
+ resumedAppOneFrames[1]!,
736
+ invite,
737
+ );
738
+ const resumedAssistantForAppTwo = decryptRelayPayload(
739
+ resumedAppTwoFrames[1]!,
740
+ invite,
741
+ );
742
+ assert.equal(resumedAssistantForAppOne.kind, "assistant_message");
743
+ assert.equal(resumedAssistantForAppTwo.kind, "assistant_message");
744
+ assert.match(resumedAssistantForAppTwo.text, /SolarFox: bot come back/);
745
+ assert.equal(
746
+ bridge.conversationMessages.length,
747
+ bridgeCallsBeforeResumedMessage + 1,
748
+ );
749
+
750
+ appOne.close();
751
+ appTwo.close();
752
+ await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
753
+ });
754
+
755
+ test("group sessions keep running when one participant leaves and later rejoins", async (t) => {
756
+ const relay = createRelayServer({
757
+ host: "127.0.0.1",
758
+ port: 0,
759
+ sessionTtlMs: 60_000,
760
+ frameCacheSize: 8,
761
+ });
762
+ const { port } = await relay.start();
763
+ t.after(async () => {
764
+ await relay.stop();
765
+ });
766
+
767
+ const provider = new PrivateClawProvider({
768
+ providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
769
+ appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
770
+ bridge: new GroupBridge(),
771
+ defaultTtlMs: DEFAULT_SESSION_TTL_MS,
772
+ });
773
+ await provider.connect();
774
+ t.after(async () => {
775
+ await provider.dispose();
776
+ });
777
+
778
+ const inviteBundle = await provider.createInviteBundle({ groupMode: true });
779
+ const invite = decodeInviteString(inviteBundle.inviteUri);
780
+
781
+ const appOneUrl = new URL(invite.appWsUrl);
782
+ appOneUrl.searchParams.set("appId", "app-one");
783
+ const appOne = new WebSocket(appOneUrl.toString());
784
+ const appOneAttached = nextMessage(appOne);
785
+ await waitForOpen(appOne);
786
+ assert.equal((await appOneAttached).type, "relay:attached");
787
+ const appOneInitialFrames = nextMessages(appOne, 3);
788
+ appOne.send(
789
+ JSON.stringify({
790
+ type: "app:frame",
791
+ envelope: encryptPayload({
792
+ sessionId: invite.sessionId,
793
+ sessionKey: invite.sessionKey,
794
+ payload: {
795
+ kind: "client_hello",
796
+ appId: "app-one",
797
+ displayName: "SolarFox",
798
+ appVersion: "flutter-test",
799
+ deviceLabel: "Tester One",
800
+ sentAt: new Date().toISOString(),
801
+ },
802
+ }),
803
+ }),
804
+ );
805
+ await appOneInitialFrames;
806
+
807
+ const appTwoUrl = new URL(invite.appWsUrl);
808
+ appTwoUrl.searchParams.set("appId", "app-two");
809
+ const appTwo = new WebSocket(appTwoUrl.toString());
810
+ const appTwoAttached = nextMessage(appTwo);
811
+ await waitForOpen(appTwo);
812
+ assert.equal((await appTwoAttached).type, "relay:attached");
813
+ const appTwoInitialFrames = nextMessages(appTwo, 3);
814
+ const appOneJoinFrames = nextMessages(appOne, 2);
815
+ appTwo.send(
816
+ JSON.stringify({
817
+ type: "app:frame",
818
+ envelope: encryptPayload({
819
+ sessionId: invite.sessionId,
820
+ sessionKey: invite.sessionKey,
821
+ payload: {
822
+ kind: "client_hello",
823
+ appId: "app-two",
824
+ displayName: "RiverCat",
825
+ appVersion: "flutter-test",
826
+ deviceLabel: "Tester Two",
827
+ sentAt: new Date().toISOString(),
828
+ },
829
+ }),
830
+ }),
831
+ );
832
+ await appTwoInitialFrames;
833
+ const initialJoinNotice = decryptRelayPayload((await appOneJoinFrames)[0]!, invite);
834
+ assert.equal(initialJoinNotice.kind, "system_message");
835
+ assert.match(initialJoinNotice.message, /RiverCat joined the group chat/i);
836
+
837
+ const appOneLeaveFramesPromise = nextMessages(appOne, 2);
838
+ appTwo.send(
839
+ JSON.stringify({
840
+ type: "app:frame",
841
+ envelope: encryptPayload({
842
+ sessionId: invite.sessionId,
843
+ sessionKey: invite.sessionKey,
844
+ payload: {
845
+ kind: "session_close",
846
+ reason: "user_disconnect",
847
+ appId: "app-two",
848
+ sentAt: new Date().toISOString(),
849
+ },
850
+ }),
851
+ }),
852
+ );
853
+
854
+ const appOneLeaveFrames = await appOneLeaveFramesPromise;
855
+ const leaveNotice = decryptRelayPayload(appOneLeaveFrames[0]!, invite);
856
+ assert.equal(leaveNotice.kind, "system_message");
857
+ assert.match(leaveNotice.message, /RiverCat left the group chat/i);
858
+ const leaveCapabilities = decryptRelayPayload(appOneLeaveFrames[1]!, invite);
859
+ assert.equal(leaveCapabilities.kind, "provider_capabilities");
860
+ assert.deepEqual(
861
+ leaveCapabilities.participants?.map((participant) => participant.displayName),
862
+ ["SolarFox"],
863
+ );
864
+
865
+ appTwo.close();
866
+ await waitForClose(appTwo);
867
+
868
+ const appTwoReconnect = new WebSocket(appTwoUrl.toString());
869
+ const appTwoReconnectAttached = nextMessage(appTwoReconnect);
870
+ await waitForOpen(appTwoReconnect);
871
+ assert.equal((await appTwoReconnectAttached).type, "relay:attached");
872
+ const appTwoReconnectFramesPromise = nextMessages(appTwoReconnect, 5);
873
+ const appOneRejoinFramesPromise = nextMessages(appOne, 2);
874
+ appTwoReconnect.send(
875
+ JSON.stringify({
876
+ type: "app:frame",
877
+ envelope: encryptPayload({
878
+ sessionId: invite.sessionId,
879
+ sessionKey: invite.sessionKey,
880
+ payload: {
881
+ kind: "client_hello",
882
+ appId: "app-two",
883
+ displayName: "RiverCat",
884
+ appVersion: "flutter-test",
885
+ deviceLabel: "Tester Two",
886
+ sentAt: new Date().toISOString(),
887
+ },
888
+ }),
889
+ }),
890
+ );
891
+
892
+ const [appTwoReconnectFrames, appOneRejoinFrames] = await Promise.all([
893
+ appTwoReconnectFramesPromise,
894
+ appOneRejoinFramesPromise,
895
+ ]);
896
+ const rejoinNotice = decryptRelayPayload(appOneRejoinFrames[0]!, invite);
897
+ assert.equal(rejoinNotice.kind, "system_message");
898
+ assert.match(rejoinNotice.message, /RiverCat joined the group chat/i);
899
+
900
+ const reconnectPayloads = appTwoReconnectFrames.map((frame) =>
901
+ decryptRelayPayload(frame, invite),
902
+ );
903
+ const reconnectCapabilities = reconnectPayloads.find(
904
+ (payload) =>
905
+ payload.kind === "provider_capabilities" &&
906
+ payload.currentAppId === "app-two",
907
+ );
908
+ assert.ok(reconnectCapabilities);
909
+ assert.equal(reconnectCapabilities.kind, "provider_capabilities");
910
+ assert.equal(reconnectCapabilities.currentDisplayName, "RiverCat");
911
+
912
+ const appOnePayloadFramesPromise = nextMessages(appOne, 2);
913
+ const appTwoPayloadFramesPromise = nextMessages(appTwoReconnect, 2);
914
+ appTwoReconnect.send(
915
+ JSON.stringify({
916
+ type: "app:frame",
917
+ envelope: encryptPayload({
918
+ sessionId: invite.sessionId,
919
+ sessionKey: invite.sessionKey,
920
+ payload: {
921
+ kind: "user_message",
922
+ appId: "app-two",
923
+ text: "back again",
924
+ clientMessageId: "group-rejoin-message",
925
+ sentAt: new Date().toISOString(),
926
+ },
927
+ }),
928
+ }),
929
+ );
930
+
931
+ const [appOnePayloadFrames, appTwoPayloadFrames] = await Promise.all([
932
+ appOnePayloadFramesPromise,
933
+ appTwoPayloadFramesPromise,
934
+ ]);
935
+ const appOneParticipantFrame = decryptRelayPayload(
936
+ appOnePayloadFrames[0]!,
937
+ invite,
938
+ );
939
+ const appTwoParticipantFrame = decryptRelayPayload(
940
+ appTwoPayloadFrames[0]!,
941
+ invite,
942
+ );
943
+ assert.equal(appOneParticipantFrame.kind, "participant_message");
944
+ assert.equal(appTwoParticipantFrame.kind, "participant_message");
945
+ assert.equal(appOneParticipantFrame.senderDisplayName, "RiverCat");
946
+ assert.equal(appTwoParticipantFrame.senderDisplayName, "RiverCat");
947
+
948
+ const appOneAssistant = decryptRelayPayload(appOnePayloadFrames[1]!, invite);
949
+ const appTwoAssistant = decryptRelayPayload(appTwoPayloadFrames[1]!, invite);
950
+ assert.equal(appOneAssistant.kind, "assistant_message");
951
+ assert.equal(appTwoAssistant.kind, "assistant_message");
952
+ assert.match(appOneAssistant.text, /RiverCat: back again/);
953
+
954
+ appOne.close();
955
+ appTwoReconnect.close();
956
+ await Promise.all([waitForClose(appOne), waitForClose(appTwoReconnect)]);
957
+ });
958
+
959
+ test("provider reminds users to renew when a session has less than 30 minutes left", async (t) => {
960
+ const relay = createRelayServer({
961
+ host: "127.0.0.1",
962
+ port: 0,
963
+ sessionTtlMs: 60_000,
964
+ frameCacheSize: 8,
965
+ });
966
+ const { port } = await relay.start();
967
+ t.after(async () => {
968
+ await relay.stop();
969
+ });
970
+
971
+ const provider = new PrivateClawProvider({
972
+ providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
973
+ appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
974
+ bridge: new EchoBridge("OpenClaw bridge"),
975
+ defaultTtlMs: 10 * 60 * 1000,
976
+ });
977
+ await provider.connect();
978
+ t.after(async () => {
979
+ await provider.dispose();
980
+ });
981
+
982
+ const inviteBundle = await provider.createInviteBundle();
983
+ const invite = decodeInviteString(inviteBundle.inviteUri);
984
+ await new Promise((resolve) => setTimeout(resolve, 20));
985
+
986
+ const appSocket = new WebSocket(invite.appWsUrl);
987
+ const incomingPromise = nextMessages(appSocket, 4);
988
+ await waitForOpen(appSocket);
989
+ appSocket.send(
990
+ JSON.stringify({
991
+ type: "app:frame",
992
+ envelope: encryptPayload({
993
+ sessionId: invite.sessionId,
994
+ sessionKey: invite.sessionKey,
995
+ payload: {
996
+ kind: "client_hello",
997
+ appVersion: "flutter-test",
998
+ deviceLabel: "Simulator",
999
+ sentAt: new Date().toISOString(),
1000
+ },
1001
+ }),
1002
+ }),
1003
+ );
1004
+
1005
+ const incoming = await incomingPromise;
1006
+ assert.equal(incoming[0]?.type, "relay:attached");
1007
+ const framePayloads = incoming
1008
+ .slice(1)
1009
+ .map((frame) => decryptRelayPayload(frame, invite));
1010
+ assert.ok(
1011
+ framePayloads.some(
1012
+ (payload) =>
1013
+ payload.kind === "system_message" &&
1014
+ /less than 30 minutes/i.test(payload.message) &&
1015
+ /renew-session/i.test(payload.message),
1016
+ ),
1017
+ );
1018
+ assert.ok(
1019
+ framePayloads.some((payload) => payload.kind === "provider_capabilities"),
1020
+ );
1021
+
1022
+ appSocket.close();
1023
+ await waitForClose(appSocket);
1024
+ });