@privateclaw/privateclaw 0.1.14 → 0.1.16

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.
@@ -138,6 +138,39 @@ class GroupBridge {
138
138
  }
139
139
  }
140
140
 
141
+ class VoiceTranscribingBridge {
142
+ readonly conversationMessages: string[] = [];
143
+ readonly transcriptionRequests: Array<{ sessionId: string; requestId: string }> = [];
144
+
145
+ async transcribeAudioAttachments(params: {
146
+ sessionId: string;
147
+ requestId: string;
148
+ }): Promise<string> {
149
+ this.transcriptionRequests.push({
150
+ sessionId: params.sessionId,
151
+ requestId: params.requestId,
152
+ });
153
+ return "你好世界";
154
+ }
155
+
156
+ async handleUserMessage(params: {
157
+ message: string;
158
+ }): Promise<string> {
159
+ this.conversationMessages.push(params.message);
160
+ return `OpenClaw bridge: ${params.message}`;
161
+ }
162
+ }
163
+
164
+ function buildVoiceAttachment() {
165
+ return {
166
+ id: "voice-attachment-1",
167
+ name: "voice.m4a",
168
+ mimeType: "audio/mp4",
169
+ sizeBytes: 16,
170
+ dataBase64: Buffer.from("voice-bytes").toString("base64"),
171
+ };
172
+ }
173
+
141
174
  test("provider creates one-time invites and replies through the encrypted relay", async (t) => {
142
175
  const relay = createRelayServer({
143
176
  host: "127.0.0.1",
@@ -227,8 +260,7 @@ test("provider creates one-time invites and replies through the encrypted relay"
227
260
  assert.equal(assistant.kind, "assistant_message");
228
261
  assert.match(assistant.text, /你好/);
229
262
 
230
- appSocket.close();
231
- await waitForClose(appSocket);
263
+ appSocket.terminate();
232
264
  });
233
265
 
234
266
  test("provider can hand off an active session to a successor provider", async (t) => {
@@ -337,6 +369,113 @@ test("provider can hand off an active session to a successor provider", async (t
337
369
  await waitForClose(appSocket);
338
370
  });
339
371
 
372
+ test("provider acknowledges voice uploads before forwarding transcript-derived replies", async (t) => {
373
+ const relay = createRelayServer({
374
+ host: "127.0.0.1",
375
+ port: 0,
376
+ sessionTtlMs: 60_000,
377
+ frameCacheSize: 8,
378
+ });
379
+ const { port } = await relay.start();
380
+ t.after(async () => {
381
+ await relay.stop();
382
+ });
383
+
384
+ const bridge = new VoiceTranscribingBridge();
385
+ const provider = new PrivateClawProvider({
386
+ providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
387
+ appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
388
+ bridge,
389
+ defaultTtlMs: DEFAULT_SESSION_TTL_MS,
390
+ welcomeMessage: "欢迎来到 PrivateClaw",
391
+ });
392
+ await provider.connect();
393
+ t.after(async () => {
394
+ await provider.dispose();
395
+ });
396
+
397
+ const inviteBundle = await provider.createInviteBundle();
398
+ const invite = decodeInviteString(inviteBundle.inviteUri);
399
+
400
+ const appUrl = new URL(invite.appWsUrl);
401
+ appUrl.searchParams.set("appId", "voice-app");
402
+ const appSocket = new WebSocket(appUrl.toString());
403
+ const attachedPromise = nextMessage(appSocket);
404
+ await waitForOpen(appSocket);
405
+ assert.equal((await attachedPromise).type, "relay:attached");
406
+
407
+ appSocket.send(
408
+ JSON.stringify({
409
+ type: "app:frame",
410
+ envelope: encryptPayload({
411
+ sessionId: invite.sessionId,
412
+ sessionKey: invite.sessionKey,
413
+ payload: {
414
+ kind: "client_hello",
415
+ appId: "voice-app",
416
+ displayName: "小明",
417
+ appVersion: "flutter-test",
418
+ deviceLabel: "Simulator",
419
+ sentAt: new Date().toISOString(),
420
+ },
421
+ }),
422
+ }),
423
+ );
424
+ await nextMessages(appSocket, 2);
425
+
426
+ appSocket.send(
427
+ JSON.stringify({
428
+ type: "app:frame",
429
+ envelope: encryptPayload({
430
+ sessionId: invite.sessionId,
431
+ sessionKey: invite.sessionKey,
432
+ payload: {
433
+ kind: "user_message",
434
+ appId: "voice-app",
435
+ displayName: "小明",
436
+ text: "",
437
+ attachments: [buildVoiceAttachment()],
438
+ clientMessageId: "voice-message-1",
439
+ sentAt: new Date().toISOString(),
440
+ },
441
+ }),
442
+ }),
443
+ );
444
+
445
+ const voiceFrames = await nextMessages(appSocket, 3);
446
+ const voiceReceipt = decryptRelayPayload(voiceFrames[0]!, invite);
447
+ assert.equal(voiceReceipt.kind, "system_message");
448
+ assert.match(voiceReceipt.message, /已经收到你的语音/u);
449
+ assert.match(voiceReceipt.message, /working on understanding/i);
450
+ assert.equal(voiceReceipt.replyTo, undefined);
451
+
452
+ const pendingReply = decryptRelayPayload(voiceFrames[1]!, invite);
453
+ assert.equal(pendingReply.kind, "assistant_message");
454
+ assert.equal(pendingReply.replyTo, "voice-message-1");
455
+ assert.equal(pendingReply.pending, true);
456
+ assert.equal(pendingReply.messageId, "pending-voice-message-1");
457
+ assert.equal(pendingReply.text, "");
458
+
459
+ const assistant = decryptRelayPayload(voiceFrames[2]!, invite);
460
+ assert.equal(assistant.kind, "assistant_message");
461
+ assert.notEqual(assistant.pending, true);
462
+ assert.match(assistant.text, /小明说:你好世界/u);
463
+ assert.deepEqual(bridge.transcriptionRequests, [
464
+ {
465
+ sessionId: invite.sessionId,
466
+ requestId: "voice-message-1",
467
+ },
468
+ ]);
469
+ assert.equal(bridge.conversationMessages.length, 1);
470
+ assert.match(
471
+ bridge.conversationMessages[0] ?? "",
472
+ /speech-to-text transcript of a user's voice message/u,
473
+ );
474
+ assert.match(bridge.conversationMessages[0] ?? "", /小明说:你好世界/u);
475
+
476
+ appSocket.terminate();
477
+ });
478
+
340
479
  test("provider returns the current session QR only to the requesting participant", async (t) => {
341
480
  const relay = createRelayServer({
342
481
  host: "127.0.0.1",
@@ -456,9 +595,8 @@ test("provider returns the current session QR only to the requesting participant
456
595
  PNG_SIGNATURE,
457
596
  );
458
597
 
459
- appOne.close();
460
- appTwo.close();
461
- await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
598
+ appOne.terminate();
599
+ appTwo.terminate();
462
600
  });
463
601
 
464
602
  test("provider can remove a group participant and reject the same app id on rejoin", async (t) => {
@@ -914,6 +1052,156 @@ test("provider group sessions broadcast participant messages and assign unique l
914
1052
  await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
915
1053
  });
916
1054
 
1055
+ test("provider group sessions broadcast raw voice first and only reply with the final assistant answer", async (t) => {
1056
+ const relay = createRelayServer({
1057
+ host: "127.0.0.1",
1058
+ port: 0,
1059
+ sessionTtlMs: 60_000,
1060
+ frameCacheSize: 8,
1061
+ });
1062
+ const { port } = await relay.start();
1063
+ t.after(async () => {
1064
+ await relay.stop();
1065
+ });
1066
+
1067
+ const bridge = new VoiceTranscribingBridge();
1068
+ const provider = new PrivateClawProvider({
1069
+ providerWsUrl: `ws://127.0.0.1:${port}/ws/provider`,
1070
+ appWsUrl: `ws://127.0.0.1:${port}/ws/app`,
1071
+ bridge,
1072
+ defaultTtlMs: DEFAULT_SESSION_TTL_MS,
1073
+ });
1074
+ await provider.connect();
1075
+ t.after(async () => {
1076
+ await provider.dispose();
1077
+ });
1078
+
1079
+ const inviteBundle = await provider.createInviteBundle({ groupMode: true });
1080
+ const invite = decodeInviteString(inviteBundle.inviteUri);
1081
+
1082
+ const appOneUrl = new URL(invite.appWsUrl);
1083
+ appOneUrl.searchParams.set("appId", "app-one");
1084
+ const appOne = new WebSocket(appOneUrl.toString());
1085
+ const appOneAttached = nextMessage(appOne);
1086
+ await waitForOpen(appOne);
1087
+ assert.equal((await appOneAttached).type, "relay:attached");
1088
+ const appOneInitialFramesPromise = nextMessages(appOne, 3);
1089
+ appOne.send(
1090
+ JSON.stringify({
1091
+ type: "app:frame",
1092
+ envelope: encryptPayload({
1093
+ sessionId: invite.sessionId,
1094
+ sessionKey: invite.sessionKey,
1095
+ payload: {
1096
+ kind: "client_hello",
1097
+ appId: "app-one",
1098
+ displayName: "小明",
1099
+ appVersion: "flutter-test",
1100
+ deviceLabel: "Tester One",
1101
+ sentAt: new Date().toISOString(),
1102
+ },
1103
+ }),
1104
+ }),
1105
+ );
1106
+ await appOneInitialFramesPromise;
1107
+
1108
+ const appTwoUrl = new URL(invite.appWsUrl);
1109
+ appTwoUrl.searchParams.set("appId", "app-two");
1110
+ const appTwo = new WebSocket(appTwoUrl.toString());
1111
+ const appTwoAttached = nextMessage(appTwo);
1112
+ await waitForOpen(appTwo);
1113
+ assert.equal((await appTwoAttached).type, "relay:attached");
1114
+ const appTwoFramesPromise = nextMessages(appTwo, 3);
1115
+ const appOneJoinFramesPromise = nextMessages(appOne, 2);
1116
+ appTwo.send(
1117
+ JSON.stringify({
1118
+ type: "app:frame",
1119
+ envelope: encryptPayload({
1120
+ sessionId: invite.sessionId,
1121
+ sessionKey: invite.sessionKey,
1122
+ payload: {
1123
+ kind: "client_hello",
1124
+ appId: "app-two",
1125
+ displayName: "小红",
1126
+ appVersion: "flutter-test",
1127
+ deviceLabel: "Tester Two",
1128
+ sentAt: new Date().toISOString(),
1129
+ },
1130
+ }),
1131
+ }),
1132
+ );
1133
+ await Promise.all([appTwoFramesPromise, appOneJoinFramesPromise]);
1134
+
1135
+ const appOnePayloadFramesPromise = nextMessages(appOne, 3);
1136
+ const appTwoPayloadFramesPromise = nextMessages(appTwo, 2);
1137
+ appOne.send(
1138
+ JSON.stringify({
1139
+ type: "app:frame",
1140
+ envelope: encryptPayload({
1141
+ sessionId: invite.sessionId,
1142
+ sessionKey: invite.sessionKey,
1143
+ payload: {
1144
+ kind: "user_message",
1145
+ appId: "app-one",
1146
+ displayName: "小明",
1147
+ text: "",
1148
+ attachments: [buildVoiceAttachment()],
1149
+ clientMessageId: "group-voice-1",
1150
+ sentAt: new Date().toISOString(),
1151
+ },
1152
+ }),
1153
+ }),
1154
+ );
1155
+
1156
+ const [appOnePayloadFrames, appTwoPayloadFrames] = await Promise.all([
1157
+ appOnePayloadFramesPromise,
1158
+ appTwoPayloadFramesPromise,
1159
+ ]);
1160
+ const appOneParticipantFrame = decryptRelayPayload(
1161
+ appOnePayloadFrames[0]!,
1162
+ invite,
1163
+ );
1164
+ const appTwoParticipantFrame = decryptRelayPayload(
1165
+ appTwoPayloadFrames[0]!,
1166
+ invite,
1167
+ );
1168
+ assert.equal(appOneParticipantFrame.kind, "system_message");
1169
+ assert.match(appOneParticipantFrame.message, /已经收到你的语音/u);
1170
+ assert.match(appOneParticipantFrame.message, /working on understanding/i);
1171
+ assert.equal(appOneParticipantFrame.replyTo, undefined);
1172
+
1173
+ const appOnePendingFrame = decryptRelayPayload(appOnePayloadFrames[1]!, invite);
1174
+ assert.equal(appOnePendingFrame.kind, "assistant_message");
1175
+ assert.equal(appOnePendingFrame.pending, true);
1176
+ assert.equal(appOnePendingFrame.replyTo, "group-voice-1");
1177
+ assert.equal(appOnePendingFrame.messageId, "pending-group-voice-1");
1178
+ assert.equal(appOnePendingFrame.text, "");
1179
+
1180
+ assert.equal(appTwoParticipantFrame.kind, "participant_message");
1181
+ assert.equal(appTwoParticipantFrame.text, "");
1182
+ assert.equal(appTwoParticipantFrame.attachments?.length, 1);
1183
+ assert.equal(appTwoParticipantFrame.attachments?.[0]?.mimeType, "audio/mp4");
1184
+
1185
+ const appOneAssistant = decryptRelayPayload(appOnePayloadFrames[2]!, invite);
1186
+ assert.equal(appOneAssistant.kind, "assistant_message");
1187
+ assert.notEqual(appOneAssistant.pending, true);
1188
+ const appTwoAssistant = decryptRelayPayload(appTwoPayloadFrames[1]!, invite);
1189
+ assert.equal(appTwoAssistant.kind, "assistant_message");
1190
+ assert.notEqual(appTwoAssistant.pending, true);
1191
+ assert.match(appTwoAssistant.text, /小明说:你好世界/u);
1192
+ assert.equal(appOneAssistant.text, appTwoAssistant.text);
1193
+ assert.equal(bridge.conversationMessages.length, 1);
1194
+ assert.match(
1195
+ bridge.conversationMessages[0] ?? "",
1196
+ /speech-to-text transcript of a user's voice message/u,
1197
+ );
1198
+ assert.match(bridge.conversationMessages[0] ?? "", /小明说:你好世界/u);
1199
+
1200
+ appOne.close();
1201
+ appTwo.close();
1202
+ await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
1203
+ });
1204
+
917
1205
  test("provider keeps generated participant labels stable across reconnects", async (t) => {
918
1206
  const relay = createRelayServer({
919
1207
  host: "127.0.0.1",