@privateclaw/privateclaw 0.1.25 → 0.1.26
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 +10 -1
- package/dist/bot-mode-topics.d.ts +7 -0
- package/dist/bot-mode-topics.js +236 -0
- package/dist/bot-mode-topics.js.map +1 -0
- package/dist/provider.d.ts +4 -1
- package/dist/provider.js +328 -216
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/bot-mode-topics.test.ts +39 -0
- package/src/bot-mode-topics.ts +248 -0
- package/src/provider.test.ts +368 -1
- package/src/provider.ts +451 -269
- package/src/types.ts +2 -0
package/src/provider.test.ts
CHANGED
|
@@ -8,11 +8,35 @@ import {
|
|
|
8
8
|
} from "@privateclaw/protocol";
|
|
9
9
|
import WebSocket from "ws";
|
|
10
10
|
import { createRelayServer } from "../../../services/relay-server/src/relay-server.js";
|
|
11
|
+
import { BOT_MODE_IDLE_TOPICS } from "./bot-mode-topics.js";
|
|
11
12
|
import { EchoBridge } from "./bridges/echo-bridge.js";
|
|
12
13
|
import { DEFAULT_SESSION_TTL_MS, PrivateClawProvider } from "./provider.js";
|
|
13
14
|
|
|
14
15
|
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
15
16
|
|
|
17
|
+
function createDeferred<T>() {
|
|
18
|
+
let resolve!: (value: T | PromiseLike<T>) => void;
|
|
19
|
+
let reject!: (reason?: unknown) => void;
|
|
20
|
+
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
|
|
21
|
+
resolve = resolvePromise;
|
|
22
|
+
reject = rejectPromise;
|
|
23
|
+
});
|
|
24
|
+
return { promise, resolve, reject };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function waitForCondition(
|
|
28
|
+
predicate: () => boolean,
|
|
29
|
+
timeoutMs = 500,
|
|
30
|
+
): Promise<void> {
|
|
31
|
+
const startedAt = Date.now();
|
|
32
|
+
while (!predicate()) {
|
|
33
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
34
|
+
throw new Error(`condition not met within ${timeoutMs}ms`);
|
|
35
|
+
}
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
16
40
|
function waitForOpen(socket: WebSocket): Promise<void> {
|
|
17
41
|
return new Promise((resolve, reject) => {
|
|
18
42
|
socket.once("open", () => resolve());
|
|
@@ -248,6 +272,52 @@ class BotModeBridge {
|
|
|
248
272
|
}
|
|
249
273
|
}
|
|
250
274
|
|
|
275
|
+
class NoReplyThenJokeBridge {
|
|
276
|
+
readonly prompts: string[] = [];
|
|
277
|
+
|
|
278
|
+
async handleUserMessage(params: {
|
|
279
|
+
message: string;
|
|
280
|
+
}): Promise<string> {
|
|
281
|
+
this.prompts.push(params.message);
|
|
282
|
+
return params.message.includes("\"NO_REPLY\"") ? "Fallback joke" : "NO_REPLY";
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
class ControlledQueueBridge {
|
|
287
|
+
readonly calls: Array<{
|
|
288
|
+
message: string;
|
|
289
|
+
deferred: ReturnType<typeof createDeferred<string>>;
|
|
290
|
+
}> = [];
|
|
291
|
+
maxConcurrentCalls = 0;
|
|
292
|
+
private activeCalls = 0;
|
|
293
|
+
|
|
294
|
+
async handleUserMessage(params: {
|
|
295
|
+
message: string;
|
|
296
|
+
}): Promise<string> {
|
|
297
|
+
const deferred = createDeferred<string>();
|
|
298
|
+
this.calls.push({
|
|
299
|
+
message: params.message,
|
|
300
|
+
deferred,
|
|
301
|
+
});
|
|
302
|
+
this.activeCalls += 1;
|
|
303
|
+
this.maxConcurrentCalls = Math.max(
|
|
304
|
+
this.maxConcurrentCalls,
|
|
305
|
+
this.activeCalls,
|
|
306
|
+
);
|
|
307
|
+
try {
|
|
308
|
+
return await deferred.promise;
|
|
309
|
+
} finally {
|
|
310
|
+
this.activeCalls -= 1;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
resolveCall(index: number, response: string): void {
|
|
315
|
+
const call = this.calls[index];
|
|
316
|
+
assert.ok(call, `expected bridge call ${index}`);
|
|
317
|
+
call.deferred.resolve(response);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
251
321
|
class ThinkingTraceBridge {
|
|
252
322
|
readonly supportsThinkingTrace = true;
|
|
253
323
|
readonly conversationMessages: string[] = [];
|
|
@@ -461,6 +531,198 @@ test("provider creates one-time invites and replies through the encrypted relay"
|
|
|
461
531
|
appSocket.terminate();
|
|
462
532
|
});
|
|
463
533
|
|
|
534
|
+
test("provider turns a NO_REPLY bridge response into a fallback joke", async (t) => {
|
|
535
|
+
const relay = createRelayServer({
|
|
536
|
+
host: "127.0.0.1",
|
|
537
|
+
port: 0,
|
|
538
|
+
sessionTtlMs: 60_000,
|
|
539
|
+
frameCacheSize: 8,
|
|
540
|
+
});
|
|
541
|
+
const { port } = await relay.start();
|
|
542
|
+
t.after(async () => {
|
|
543
|
+
await relay.stop();
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
const bridge = new NoReplyThenJokeBridge();
|
|
547
|
+
const provider = new PrivateClawProvider({
|
|
548
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
549
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
550
|
+
bridge,
|
|
551
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
552
|
+
welcomeMessage: "欢迎来到 PrivateClaw",
|
|
553
|
+
});
|
|
554
|
+
await provider.connect();
|
|
555
|
+
t.after(async () => {
|
|
556
|
+
await provider.dispose();
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
const inviteBundle = await provider.createInviteBundle();
|
|
560
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
561
|
+
|
|
562
|
+
const appSocket = new WebSocket(invite.appWsUrl);
|
|
563
|
+
const attachedPromise = nextMessage(appSocket);
|
|
564
|
+
await waitForOpen(appSocket);
|
|
565
|
+
assert.equal((await attachedPromise).type, "relay:attached");
|
|
566
|
+
|
|
567
|
+
appSocket.send(
|
|
568
|
+
JSON.stringify({
|
|
569
|
+
type: "app:frame",
|
|
570
|
+
envelope: encryptPayload({
|
|
571
|
+
sessionId: invite.sessionId,
|
|
572
|
+
sessionKey: invite.sessionKey,
|
|
573
|
+
payload: {
|
|
574
|
+
kind: "client_hello",
|
|
575
|
+
appVersion: "flutter-test",
|
|
576
|
+
deviceLabel: "Simulator",
|
|
577
|
+
sentAt: new Date().toISOString(),
|
|
578
|
+
},
|
|
579
|
+
}),
|
|
580
|
+
}),
|
|
581
|
+
);
|
|
582
|
+
await nextMessages(appSocket, 2);
|
|
583
|
+
|
|
584
|
+
appSocket.send(
|
|
585
|
+
JSON.stringify({
|
|
586
|
+
type: "app:frame",
|
|
587
|
+
envelope: encryptPayload({
|
|
588
|
+
sessionId: invite.sessionId,
|
|
589
|
+
sessionKey: invite.sessionKey,
|
|
590
|
+
payload: {
|
|
591
|
+
kind: "user_message",
|
|
592
|
+
text: "你好",
|
|
593
|
+
clientMessageId: "no-reply-user-1",
|
|
594
|
+
sentAt: new Date().toISOString(),
|
|
595
|
+
},
|
|
596
|
+
}),
|
|
597
|
+
}),
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
const assistant = await nextRelayPayload(appSocket, invite);
|
|
601
|
+
assert.equal(assistant.kind, "assistant_message");
|
|
602
|
+
assert.equal(assistant.replyTo, "no-reply-user-1");
|
|
603
|
+
assert.equal(assistant.text, "Fallback joke");
|
|
604
|
+
assert.equal(bridge.prompts[0], "你好");
|
|
605
|
+
assert.equal(bridge.prompts.length, 2);
|
|
606
|
+
assert.match(
|
|
607
|
+
bridge.prompts[1] ?? "",
|
|
608
|
+
/Your previous assistant reply for this turn was exactly "NO_REPLY"\./u,
|
|
609
|
+
);
|
|
610
|
+
assert.match(
|
|
611
|
+
bridge.prompts[1] ?? "",
|
|
612
|
+
/same language as the recent user\/assistant conversation already in this session/u,
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
appSocket.terminate();
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
test("provider serializes bridge turns per session", async (t) => {
|
|
619
|
+
const relay = createRelayServer({
|
|
620
|
+
host: "127.0.0.1",
|
|
621
|
+
port: 0,
|
|
622
|
+
sessionTtlMs: 60_000,
|
|
623
|
+
frameCacheSize: 8,
|
|
624
|
+
});
|
|
625
|
+
const { port } = await relay.start();
|
|
626
|
+
t.after(async () => {
|
|
627
|
+
await relay.stop();
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
const bridge = new ControlledQueueBridge();
|
|
631
|
+
const provider = new PrivateClawProvider({
|
|
632
|
+
providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
|
|
633
|
+
appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
|
|
634
|
+
bridge,
|
|
635
|
+
defaultTtlMs: DEFAULT_SESSION_TTL_MS,
|
|
636
|
+
welcomeMessage: "欢迎来到 PrivateClaw",
|
|
637
|
+
});
|
|
638
|
+
await provider.connect();
|
|
639
|
+
t.after(async () => {
|
|
640
|
+
await provider.dispose();
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
const inviteBundle = await provider.createInviteBundle();
|
|
644
|
+
const invite = decodeInviteString(inviteBundle.inviteUri);
|
|
645
|
+
|
|
646
|
+
const appSocket = new WebSocket(invite.appWsUrl);
|
|
647
|
+
const attachedPromise = nextMessage(appSocket);
|
|
648
|
+
await waitForOpen(appSocket);
|
|
649
|
+
assert.equal((await attachedPromise).type, "relay:attached");
|
|
650
|
+
|
|
651
|
+
appSocket.send(
|
|
652
|
+
JSON.stringify({
|
|
653
|
+
type: "app:frame",
|
|
654
|
+
envelope: encryptPayload({
|
|
655
|
+
sessionId: invite.sessionId,
|
|
656
|
+
sessionKey: invite.sessionKey,
|
|
657
|
+
payload: {
|
|
658
|
+
kind: "client_hello",
|
|
659
|
+
appVersion: "flutter-test",
|
|
660
|
+
deviceLabel: "Simulator",
|
|
661
|
+
sentAt: new Date().toISOString(),
|
|
662
|
+
},
|
|
663
|
+
}),
|
|
664
|
+
}),
|
|
665
|
+
);
|
|
666
|
+
await nextMessages(appSocket, 2);
|
|
667
|
+
|
|
668
|
+
appSocket.send(
|
|
669
|
+
JSON.stringify({
|
|
670
|
+
type: "app:frame",
|
|
671
|
+
envelope: encryptPayload({
|
|
672
|
+
sessionId: invite.sessionId,
|
|
673
|
+
sessionKey: invite.sessionKey,
|
|
674
|
+
payload: {
|
|
675
|
+
kind: "user_message",
|
|
676
|
+
text: "first queued message",
|
|
677
|
+
clientMessageId: "queued-message-1",
|
|
678
|
+
sentAt: new Date().toISOString(),
|
|
679
|
+
},
|
|
680
|
+
}),
|
|
681
|
+
}),
|
|
682
|
+
);
|
|
683
|
+
await waitForCondition(() => bridge.calls.length === 1);
|
|
684
|
+
|
|
685
|
+
appSocket.send(
|
|
686
|
+
JSON.stringify({
|
|
687
|
+
type: "app:frame",
|
|
688
|
+
envelope: encryptPayload({
|
|
689
|
+
sessionId: invite.sessionId,
|
|
690
|
+
sessionKey: invite.sessionKey,
|
|
691
|
+
payload: {
|
|
692
|
+
kind: "user_message",
|
|
693
|
+
text: "second queued message",
|
|
694
|
+
clientMessageId: "queued-message-2",
|
|
695
|
+
sentAt: new Date().toISOString(),
|
|
696
|
+
},
|
|
697
|
+
}),
|
|
698
|
+
}),
|
|
699
|
+
);
|
|
700
|
+
await new Promise((resolve) => setTimeout(resolve, 80));
|
|
701
|
+
assert.equal(bridge.calls.length, 1);
|
|
702
|
+
assert.equal(bridge.maxConcurrentCalls, 1);
|
|
703
|
+
assert.equal(bridge.calls[0]?.message, "first queued message");
|
|
704
|
+
|
|
705
|
+
const firstAssistantPromise = nextRelayPayload(appSocket, invite);
|
|
706
|
+
bridge.resolveCall(0, "First queued reply");
|
|
707
|
+
const firstAssistant = await firstAssistantPromise;
|
|
708
|
+
assert.equal(firstAssistant.kind, "assistant_message");
|
|
709
|
+
assert.equal(firstAssistant.replyTo, "queued-message-1");
|
|
710
|
+
assert.equal(firstAssistant.text, "First queued reply");
|
|
711
|
+
|
|
712
|
+
await waitForCondition(() => bridge.calls.length === 2);
|
|
713
|
+
assert.equal(bridge.calls[1]?.message, "second queued message");
|
|
714
|
+
assert.equal(bridge.maxConcurrentCalls, 1);
|
|
715
|
+
|
|
716
|
+
const secondAssistantPromise = nextRelayPayload(appSocket, invite);
|
|
717
|
+
bridge.resolveCall(1, "Second queued reply");
|
|
718
|
+
const secondAssistant = await secondAssistantPromise;
|
|
719
|
+
assert.equal(secondAssistant.kind, "assistant_message");
|
|
720
|
+
assert.equal(secondAssistant.replyTo, "queued-message-2");
|
|
721
|
+
assert.equal(secondAssistant.text, "Second queued reply");
|
|
722
|
+
|
|
723
|
+
appSocket.terminate();
|
|
724
|
+
});
|
|
725
|
+
|
|
464
726
|
test("provider emits verbose session logs when enabled", async (t) => {
|
|
465
727
|
const relay = createRelayServer({
|
|
466
728
|
host: "127.0.0.1",
|
|
@@ -2297,6 +2559,34 @@ test("group bot mode greets a silent participant after the configured join delay
|
|
|
2297
2559
|
await waitForNoMessage(app, 120);
|
|
2298
2560
|
});
|
|
2299
2561
|
|
|
2562
|
+
test("group bot mode turns a NO_REPLY proactive prompt into a fallback joke", async (t) => {
|
|
2563
|
+
const bridge = new NoReplyThenJokeBridge();
|
|
2564
|
+
const { invite } = await createGroupBotModeHarness(t, bridge, {
|
|
2565
|
+
botMode: true,
|
|
2566
|
+
botModeSilentJoinDelayMs: 40,
|
|
2567
|
+
botModeIdleDelayMs: 250,
|
|
2568
|
+
});
|
|
2569
|
+
const app = await connectGroupApp({
|
|
2570
|
+
invite,
|
|
2571
|
+
appId: "app-one",
|
|
2572
|
+
displayName: "SolarFox",
|
|
2573
|
+
});
|
|
2574
|
+
t.after(async () => {
|
|
2575
|
+
app.close();
|
|
2576
|
+
await waitForClose(app);
|
|
2577
|
+
});
|
|
2578
|
+
|
|
2579
|
+
const proactive = await nextRelayPayload(app, invite);
|
|
2580
|
+
assert.equal(proactive.kind, "assistant_message");
|
|
2581
|
+
assert.equal(proactive.text, "Fallback joke");
|
|
2582
|
+
assert.equal(bridge.prompts.length, 2);
|
|
2583
|
+
assert.match(bridge.prompts[0] ?? "", /joined this PrivateClaw group chat/u);
|
|
2584
|
+
assert.match(
|
|
2585
|
+
bridge.prompts[1] ?? "",
|
|
2586
|
+
/Your previous assistant reply for this turn was exactly "NO_REPLY"\./u,
|
|
2587
|
+
);
|
|
2588
|
+
});
|
|
2589
|
+
|
|
2300
2590
|
test("group bot mode cancels the silent-join greeting after the participant speaks", async (t) => {
|
|
2301
2591
|
const bridge = new BotModeBridge();
|
|
2302
2592
|
const { invite } = await createGroupBotModeHarness(t, bridge, {
|
|
@@ -2343,7 +2633,7 @@ test("group bot mode cancels the silent-join greeting after the participant spea
|
|
|
2343
2633
|
|
|
2344
2634
|
test("group bot mode sends an idle follow-up after the room stays quiet", async (t) => {
|
|
2345
2635
|
const bridge = new BotModeBridge();
|
|
2346
|
-
const { invite } = await createGroupBotModeHarness(t, bridge, {
|
|
2636
|
+
const { invite, provider } = await createGroupBotModeHarness(t, bridge, {
|
|
2347
2637
|
botMode: true,
|
|
2348
2638
|
botModeSilentJoinDelayMs: 250,
|
|
2349
2639
|
botModeIdleDelayMs: 60,
|
|
@@ -2384,10 +2674,87 @@ test("group bot mode sends an idle follow-up after the room stays quiet", async
|
|
|
2384
2674
|
assert.equal(idleFollowUp.text, "Bot mode joke");
|
|
2385
2675
|
assert.deepEqual(bridge.promptKinds, ["user", "idle_group"]);
|
|
2386
2676
|
assert.match(bridge.prompts[1]!, /has had no new message/u);
|
|
2677
|
+
assert.match(bridge.prompts[1]!, /Idle topic:/u);
|
|
2678
|
+
assert.match(bridge.prompts[1]!, /Topic prompt:/u);
|
|
2387
2679
|
assert.match(
|
|
2388
2680
|
bridge.prompts[1]!,
|
|
2389
2681
|
/Write the proactive message in the same language as the recent user\/assistant conversation already in this session\./u,
|
|
2390
2682
|
);
|
|
2683
|
+
const sessionState = (provider as unknown as { sessions: Map<string, { botModeLastIdleTopicId?: string }> }).sessions.get(
|
|
2684
|
+
invite.sessionId,
|
|
2685
|
+
);
|
|
2686
|
+
assert.ok(sessionState?.botModeLastIdleTopicId);
|
|
2687
|
+
assert.ok(
|
|
2688
|
+
BOT_MODE_IDLE_TOPICS.some(
|
|
2689
|
+
(topic) => topic.id === sessionState.botModeLastIdleTopicId,
|
|
2690
|
+
),
|
|
2691
|
+
);
|
|
2692
|
+
});
|
|
2693
|
+
|
|
2694
|
+
test("group bot mode proactive turns wait for the session bridge queue", async (t) => {
|
|
2695
|
+
const bridge = new ControlledQueueBridge();
|
|
2696
|
+
const { invite } = await createGroupBotModeHarness(t, bridge, {
|
|
2697
|
+
botMode: true,
|
|
2698
|
+
botModeSilentJoinDelayMs: 250,
|
|
2699
|
+
botModeIdleDelayMs: 60,
|
|
2700
|
+
});
|
|
2701
|
+
const app = await connectGroupApp({
|
|
2702
|
+
invite,
|
|
2703
|
+
appId: "app-one",
|
|
2704
|
+
displayName: "SolarFox",
|
|
2705
|
+
});
|
|
2706
|
+
t.after(async () => {
|
|
2707
|
+
app.close();
|
|
2708
|
+
await waitForClose(app);
|
|
2709
|
+
});
|
|
2710
|
+
|
|
2711
|
+
const participantEchoPromise = nextRelayPayload(app, invite);
|
|
2712
|
+
app.send(
|
|
2713
|
+
JSON.stringify({
|
|
2714
|
+
type: "app:frame",
|
|
2715
|
+
envelope: encryptPayload({
|
|
2716
|
+
sessionId: invite.sessionId,
|
|
2717
|
+
sessionKey: invite.sessionKey,
|
|
2718
|
+
payload: {
|
|
2719
|
+
kind: "user_message",
|
|
2720
|
+
appId: "app-one",
|
|
2721
|
+
text: "queue this turn first",
|
|
2722
|
+
clientMessageId: "queued-bot-mode-user-1",
|
|
2723
|
+
sentAt: new Date().toISOString(),
|
|
2724
|
+
},
|
|
2725
|
+
}),
|
|
2726
|
+
}),
|
|
2727
|
+
);
|
|
2728
|
+
|
|
2729
|
+
const participantEcho = await participantEchoPromise;
|
|
2730
|
+
assert.equal(participantEcho.kind, "participant_message");
|
|
2731
|
+
await waitForCondition(() => bridge.calls.length === 1);
|
|
2732
|
+
assert.equal(bridge.calls[0]?.message, "SolarFox: queue this turn first");
|
|
2733
|
+
|
|
2734
|
+
await new Promise((resolve) => setTimeout(resolve, 90));
|
|
2735
|
+
assert.equal(
|
|
2736
|
+
bridge.calls.length,
|
|
2737
|
+
1,
|
|
2738
|
+
"idle bot-mode prompt should wait behind the queued user turn",
|
|
2739
|
+
);
|
|
2740
|
+
assert.equal(bridge.maxConcurrentCalls, 1);
|
|
2741
|
+
|
|
2742
|
+
const assistantReplyPromise = nextRelayPayload(app, invite);
|
|
2743
|
+
bridge.resolveCall(0, "Queued user reply");
|
|
2744
|
+
const assistantReply = await assistantReplyPromise;
|
|
2745
|
+
assert.equal(assistantReply.kind, "assistant_message");
|
|
2746
|
+
assert.equal(assistantReply.replyTo, "queued-bot-mode-user-1");
|
|
2747
|
+
assert.equal(assistantReply.text, "Queued user reply");
|
|
2748
|
+
|
|
2749
|
+
await waitForCondition(() => bridge.calls.length === 2);
|
|
2750
|
+
assert.match(bridge.calls[1]?.message ?? "", /has had no new message/u);
|
|
2751
|
+
assert.equal(bridge.maxConcurrentCalls, 1);
|
|
2752
|
+
|
|
2753
|
+
const proactiveReplyPromise = nextRelayPayload(app, invite);
|
|
2754
|
+
bridge.resolveCall(1, "Queued bot joke");
|
|
2755
|
+
const proactiveReply = await proactiveReplyPromise;
|
|
2756
|
+
assert.equal(proactiveReply.kind, "assistant_message");
|
|
2757
|
+
assert.equal(proactiveReply.text, "Queued bot joke");
|
|
2391
2758
|
});
|
|
2392
2759
|
|
|
2393
2760
|
test("group bot mode idle timing ignores non-human assistant broadcasts", async (t) => {
|