@privateclaw/privateclaw 0.1.16 → 0.1.18

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 (42) hide show
  1. package/README.md +44 -1
  2. package/dist/audio-transcriber.d.ts +46 -0
  3. package/dist/audio-transcriber.js +433 -0
  4. package/dist/audio-transcriber.js.map +1 -0
  5. package/dist/bridges/openai-compatible-bridge.js +1 -0
  6. package/dist/bridges/openai-compatible-bridge.js.map +1 -1
  7. package/dist/bridges/openclaw-agent-bridge.d.ts +10 -2
  8. package/dist/bridges/openclaw-agent-bridge.js +632 -73
  9. package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
  10. package/dist/cli.js +57 -6
  11. package/dist/cli.js.map +1 -1
  12. package/dist/openclaw-plugin.js +150 -51
  13. package/dist/openclaw-plugin.js.map +1 -1
  14. package/dist/pair-daemon.d.ts +2 -0
  15. package/dist/pair-daemon.js +2 -0
  16. package/dist/pair-daemon.js.map +1 -1
  17. package/dist/provider.d.ts +14 -1
  18. package/dist/provider.js +262 -20
  19. package/dist/provider.js.map +1 -1
  20. package/dist/session-control.d.ts +7 -0
  21. package/dist/session-control.js +122 -0
  22. package/dist/session-control.js.map +1 -1
  23. package/dist/text.d.ts +2 -0
  24. package/dist/text.js +3 -1
  25. package/dist/text.js.map +1 -1
  26. package/dist/types.d.ts +21 -2
  27. package/package.json +2 -2
  28. package/src/audio-transcriber.test.ts +299 -0
  29. package/src/audio-transcriber.ts +615 -0
  30. package/src/bridges/openai-compatible-bridge.ts +1 -0
  31. package/src/bridges/openclaw-agent-bridge.test.ts +644 -7
  32. package/src/bridges/openclaw-agent-bridge.ts +817 -81
  33. package/src/cli.ts +73 -5
  34. package/src/openclaw-plugin.test.ts +12 -2
  35. package/src/openclaw-plugin.ts +191 -54
  36. package/src/pair-daemon.ts +4 -0
  37. package/src/provider.test.ts +679 -0
  38. package/src/provider.ts +349 -28
  39. package/src/session-control.test.ts +80 -0
  40. package/src/session-control.ts +151 -0
  41. package/src/text.ts +12 -2
  42. package/src/types.ts +29 -1
@@ -64,6 +64,22 @@ test("parseOpenClawAgentOutput accepts top-level payload arrays from alternate O
64
64
  assert.equal(result, "pong-from-top-level");
65
65
  });
66
66
 
67
+ test("parseOpenClawAgentOutput tolerates noisy stdout before the JSON payload", () => {
68
+ const result = parseOpenClawAgentOutput(
69
+ [
70
+ "WARNING: provider fallback emitted a banner before --json output",
71
+ JSON.stringify({
72
+ status: "ok",
73
+ result: {
74
+ payloads: [{ text: "pong-after-noise" }],
75
+ },
76
+ }),
77
+ ].join("\n"),
78
+ );
79
+
80
+ assert.equal(result, "pong-after-noise");
81
+ });
82
+
67
83
  test("parseOpenClawAgentOutput prefers tagged structured PrivateClaw responses", () => {
68
84
  const result = parseOpenClawAgentOutput(
69
85
  JSON.stringify({
@@ -93,6 +109,246 @@ test("parseOpenClawAgentOutput prefers tagged structured PrivateClaw responses",
93
109
  });
94
110
  });
95
111
 
112
+ test("parseOpenClawAgentOutput tolerates trailing commas inside structured responses", () => {
113
+ const result = parseOpenClawAgentOutput(
114
+ JSON.stringify({
115
+ status: "ok",
116
+ result: {
117
+ payloads: [
118
+ {
119
+ text: [
120
+ "<privateclaw-response>",
121
+ '{"version":1,"messages":[{"text":"voice-reply"},],"data":{}}',
122
+ "</privateclaw-response>",
123
+ ].join("\n"),
124
+ },
125
+ ],
126
+ },
127
+ }),
128
+ );
129
+
130
+ assert.equal(result, "voice-reply");
131
+ });
132
+
133
+ test("parseOpenClawAgentOutput preserves trimmed inline attachment sizes", () => {
134
+ const base64Data = Buffer.from("hello").toString("base64");
135
+ const result = parseOpenClawAgentOutput(
136
+ JSON.stringify({
137
+ status: "ok",
138
+ result: {
139
+ payloads: [
140
+ {
141
+ text: [
142
+ "<privateclaw-response>",
143
+ JSON.stringify({
144
+ version: 1,
145
+ messages: [
146
+ {
147
+ text: "",
148
+ attachments: [
149
+ {
150
+ name: "hello.txt",
151
+ mimeType: "text/plain",
152
+ dataBase64: ` ${base64Data} `,
153
+ },
154
+ ],
155
+ },
156
+ ],
157
+ }),
158
+ "</privateclaw-response>",
159
+ ].join("\n"),
160
+ },
161
+ ],
162
+ },
163
+ }),
164
+ );
165
+
166
+ if (typeof result === "string") {
167
+ assert.fail("Expected an attachment-bearing bridge response.");
168
+ }
169
+ const [message] = result.messages;
170
+ assert(message && typeof message !== "string");
171
+ const [attachment] = message.attachments ?? [];
172
+ assert(attachment);
173
+ assert.equal(attachment.sizeBytes, 5);
174
+ assert.equal(attachment.dataBase64, base64Data);
175
+ });
176
+
177
+ test("parseOpenClawAgentOutput resolves structured filePath attachments from the workspace", async (t) => {
178
+ const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-workspace-"));
179
+ t.after(async () => {
180
+ await fs.rm(workspaceDir, { recursive: true, force: true });
181
+ });
182
+
183
+ const attachmentPath = path.join(workspaceDir, "artifact.txt");
184
+ await fs.writeFile(attachmentPath, "hello from filePath", "utf8");
185
+
186
+ const result = parseOpenClawAgentOutput(
187
+ JSON.stringify({
188
+ status: "ok",
189
+ result: {
190
+ payloads: [
191
+ {
192
+ text: [
193
+ "<privateclaw-response>",
194
+ JSON.stringify({
195
+ version: 1,
196
+ messages: [
197
+ {
198
+ text: "",
199
+ attachments: [
200
+ {
201
+ name: "artifact.txt",
202
+ mimeType: "text/plain",
203
+ filePath: attachmentPath,
204
+ },
205
+ ],
206
+ },
207
+ ],
208
+ }),
209
+ "</privateclaw-response>",
210
+ ].join("\n"),
211
+ },
212
+ ],
213
+ },
214
+ }),
215
+ { workspaceDir },
216
+ );
217
+
218
+ if (typeof result === "string") {
219
+ assert.fail("Expected an attachment-bearing bridge response.");
220
+ }
221
+ const [message] = result.messages;
222
+ assert(message && typeof message !== "string");
223
+ const [attachment] = message.attachments ?? [];
224
+ assert(attachment);
225
+ assert.equal(attachment.name, "artifact.txt");
226
+ assert.equal(attachment.mimeType, "text/plain");
227
+ assert.equal(attachment.sizeBytes, Buffer.byteLength("hello from filePath"));
228
+ assert.equal(attachment.dataBase64, Buffer.from("hello from filePath").toString("base64"));
229
+ });
230
+
231
+ test("parseOpenClawAgentOutput accepts structured absolute filePath attachments outside the workspace", async (t) => {
232
+ const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-workspace-"));
233
+ const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-outside-"));
234
+ t.after(async () => {
235
+ await Promise.all([
236
+ fs.rm(workspaceDir, { recursive: true, force: true }),
237
+ fs.rm(outsideDir, { recursive: true, force: true }),
238
+ ]);
239
+ });
240
+
241
+ const outsidePath = path.join(outsideDir, "outside.txt");
242
+ await fs.writeFile(outsidePath, "should stay outside", "utf8");
243
+
244
+ const result = parseOpenClawAgentOutput(
245
+ JSON.stringify({
246
+ status: "ok",
247
+ result: {
248
+ payloads: [
249
+ {
250
+ text: [
251
+ "<privateclaw-response>",
252
+ JSON.stringify({
253
+ version: 1,
254
+ messages: [
255
+ {
256
+ text: "workspace-only",
257
+ attachments: [
258
+ {
259
+ name: "outside.txt",
260
+ mimeType: "text/plain",
261
+ filePath: outsidePath,
262
+ },
263
+ ],
264
+ },
265
+ ],
266
+ }),
267
+ "</privateclaw-response>",
268
+ ].join("\n"),
269
+ },
270
+ ],
271
+ },
272
+ }),
273
+ { workspaceDir },
274
+ );
275
+
276
+ if (typeof result === "string") {
277
+ assert.fail("Expected an attachment-bearing bridge response.");
278
+ }
279
+ const [message] = result.messages;
280
+ assert(message && typeof message !== "string");
281
+ assert.equal(message.text, "workspace-only");
282
+ const [attachment] = message.attachments ?? [];
283
+ assert(attachment);
284
+ assert.equal(attachment.name, "outside.txt");
285
+ assert.equal(attachment.mimeType, "text/plain");
286
+ assert.equal(attachment.sizeBytes, Buffer.byteLength("should stay outside"));
287
+ assert.equal(attachment.dataBase64, Buffer.from("should stay outside").toString("base64"));
288
+ });
289
+
290
+ test("parseOpenClawAgentOutput tolerates malformed closing tags and qqimg markup inside structured text", async (t) => {
291
+ const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-workspace-"));
292
+ const mediaDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-structured-media-"));
293
+ const imagePath = path.join(mediaDir, "video-frames-001.jpg");
294
+ const imageBytes = Buffer.from("fake-jpg-binary");
295
+ await fs.writeFile(imagePath, imageBytes);
296
+ t.after(async () => {
297
+ await Promise.all([
298
+ fs.rm(workspaceDir, { recursive: true, force: true }),
299
+ fs.rm(mediaDir, { recursive: true, force: true }),
300
+ ]);
301
+ });
302
+
303
+ const result = parseOpenClawAgentOutput(
304
+ JSON.stringify({
305
+ status: "ok",
306
+ result: {
307
+ payloads: [
308
+ {
309
+ text: [
310
+ "好的,这是工作区里的一张图片(视频帧提取):",
311
+ "",
312
+ "<privateclaw-response>",
313
+ JSON.stringify({
314
+ version: 1,
315
+ messages: [
316
+ {
317
+ text: [
318
+ "好的,这是工作区里的一张图片(视频帧提取):",
319
+ "",
320
+ `<qqimg>${imagePath}</qqimg>`,
321
+ "",
322
+ "你那边收到图片了吗?🦊",
323
+ ].join("\n"),
324
+ },
325
+ ],
326
+ data: {},
327
+ }),
328
+ "<privateclaw-response>",
329
+ ].join("\n"),
330
+ },
331
+ ],
332
+ },
333
+ }),
334
+ { workspaceDir },
335
+ );
336
+
337
+ if (typeof result === "string") {
338
+ assert.fail("Expected an attachment-bearing bridge response.");
339
+ }
340
+ const [message] = result.messages;
341
+ assert(message && typeof message !== "string");
342
+ assert.equal(message.text, "好的,这是工作区里的一张图片(视频帧提取):\n\n你那边收到图片了吗?🦊");
343
+ const [attachment] = message.attachments ?? [];
344
+ assert(attachment);
345
+ assert.match(attachment.id, /^structured-attachment-/u);
346
+ assert.equal(attachment.name, "video-frames-001.jpg");
347
+ assert.equal(attachment.mimeType, "image/jpeg");
348
+ assert.equal(attachment.sizeBytes, imageBytes.byteLength);
349
+ assert.equal(attachment.dataBase64, imageBytes.toString("base64"));
350
+ });
351
+
96
352
  test("OpenClawAgentBridge invokes openclaw agent with session-aware arguments", async () => {
97
353
  let invokedFile = "";
98
354
  let invokedArgs: string[] = [];
@@ -143,6 +399,7 @@ test("OpenClawAgentBridge invokes openclaw agent with session-aware arguments",
143
399
  assert.match(prompt, /^hello\b/u);
144
400
  assert.match(prompt, /PrivateClaw response contract:/u);
145
401
  assert.match(prompt, /<privateclaw-response>/u);
402
+ assert.match(prompt, /Never put channel-specific markup such as <qqimg>/u);
146
403
  assert.deepEqual(invokedArgs.slice(5), [
147
404
  "--json",
148
405
  "--agent",
@@ -157,6 +414,54 @@ test("OpenClawAgentBridge invokes openclaw agent with session-aware arguments",
157
414
  assert.equal(result, "bridge-ok");
158
415
  });
159
416
 
417
+ test("OpenClawAgentBridge emits verbose execution logs when enabled", async () => {
418
+ const logs: string[] = [];
419
+
420
+ const bridge = new OpenClawAgentBridge({
421
+ verboseController: { enabled: true },
422
+ onLog: (message) => {
423
+ logs.push(message);
424
+ },
425
+ execFileImpl: (_file, _args, _options, callback) => {
426
+ callback(
427
+ null,
428
+ JSON.stringify({
429
+ status: "ok",
430
+ result: {
431
+ payloads: [{ text: "verbose-ok" }],
432
+ },
433
+ }),
434
+ "",
435
+ );
436
+ },
437
+ });
438
+
439
+ const result = await bridge.handleUserMessage({
440
+ sessionId: "verbose-session",
441
+ message: "hello",
442
+ history: [],
443
+ invite: {
444
+ version: 1,
445
+ sessionId: "verbose-session",
446
+ sessionKey: "test",
447
+ appWsUrl: "ws://127.0.0.1/app?sessionId=verbose-session",
448
+ expiresAt: new Date().toISOString(),
449
+ },
450
+ });
451
+
452
+ assert.equal(result, "verbose-ok");
453
+ assert.ok(
454
+ logs.some((message) => message.includes("[bridge][verbose] exec_start session=verbose-session")),
455
+ "verbose bridge logging should include exec_start details",
456
+ );
457
+ assert.ok(
458
+ logs.some((message) =>
459
+ message.includes("[bridge][verbose] prompt_body_ready session=verbose-session"),
460
+ ),
461
+ "verbose bridge logging should include prompt preparation details",
462
+ );
463
+ });
464
+
160
465
  test("resolveOpenClawLaunchCommand reuses the current OpenClaw CLI script on Windows", () => {
161
466
  const command = resolveOpenClawLaunchCommand({
162
467
  platform: "win32",
@@ -294,6 +599,123 @@ test("OpenClawAgentBridge recovers assistant text from the session log when agen
294
599
  assert.equal(result, "pixel.png");
295
600
  });
296
601
 
602
+ test("OpenClawAgentBridge streams thinking traces from the session log while the agent is running", async (t) => {
603
+ const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-openclaw-state-"));
604
+ const workspaceDir = path.join(stateDir, "workspace");
605
+ const sessionLogPath = path.join(
606
+ stateDir,
607
+ "agents",
608
+ "main",
609
+ "sessions",
610
+ "privateclaw-trace-session.jsonl",
611
+ );
612
+ await fs.mkdir(workspaceDir, { recursive: true });
613
+ t.after(async () => {
614
+ await fs.rm(stateDir, { recursive: true, force: true });
615
+ });
616
+
617
+ const bridge = new OpenClawAgentBridge({
618
+ stateDir,
619
+ workspaceDir,
620
+ execFileImpl: (_file, _args, _options, callback) => {
621
+ void (async () => {
622
+ await fs.mkdir(path.dirname(sessionLogPath), { recursive: true });
623
+ await fs.writeFile(sessionLogPath, "", "utf8");
624
+ await fs.appendFile(
625
+ sessionLogPath,
626
+ JSON.stringify({
627
+ type: "message",
628
+ id: "trace-assistant-1",
629
+ timestamp: "2026-01-01T00:00:00.000Z",
630
+ message: {
631
+ role: "assistant",
632
+ content: [{ type: "text", text: "Planning the reply" }],
633
+ isError: false,
634
+ },
635
+ }),
636
+ "utf8",
637
+ );
638
+ await new Promise((resolve) => setTimeout(resolve, 220));
639
+ await fs.appendFile(sessionLogPath, "\n", "utf8");
640
+ await new Promise((resolve) => setTimeout(resolve, 220));
641
+ await fs.appendFile(
642
+ sessionLogPath,
643
+ `${JSON.stringify({
644
+ type: "message",
645
+ id: "trace-tool-1",
646
+ timestamp: "2026-01-01T00:00:01.000Z",
647
+ message: {
648
+ role: "toolResult",
649
+ toolName: "read",
650
+ content: [{ type: "text", text: "Opened README excerpt" }],
651
+ isError: false,
652
+ },
653
+ })}\n`,
654
+ "utf8",
655
+ );
656
+ await new Promise((resolve) => setTimeout(resolve, 220));
657
+ callback(
658
+ null,
659
+ JSON.stringify({
660
+ status: "ok",
661
+ result: {
662
+ payloads: [
663
+ {
664
+ text: [
665
+ "<privateclaw-response>",
666
+ JSON.stringify({
667
+ version: 1,
668
+ messages: [{ text: "Bridge final answer" }],
669
+ }),
670
+ "</privateclaw-response>",
671
+ ].join("\n"),
672
+ },
673
+ ],
674
+ },
675
+ }),
676
+ "",
677
+ );
678
+ })().catch((error) => {
679
+ callback(error instanceof Error ? error : new Error(String(error)), "", "");
680
+ });
681
+ },
682
+ });
683
+
684
+ const snapshots: Array<{
685
+ summary: string;
686
+ entryCount: number;
687
+ latestKind?: string;
688
+ }> = [];
689
+ const result = await bridge.handleUserMessage({
690
+ sessionId: "privateclaw-trace-session",
691
+ message: "Reply after reading the README.",
692
+ onThinkingTrace: (snapshot) => {
693
+ snapshots.push({
694
+ summary: snapshot.summary,
695
+ entryCount: snapshot.entries.length,
696
+ latestKind:
697
+ snapshot.entries[
698
+ Math.max(0, snapshot.entries.length - 1)
699
+ ]?.kind,
700
+ });
701
+ },
702
+ });
703
+
704
+ assert.equal(result, "Bridge final answer");
705
+ assert.deepEqual(snapshots, [
706
+ {
707
+ summary: "Planning the reply",
708
+ entryCount: 1,
709
+ latestKind: "thought",
710
+ },
711
+ {
712
+ summary: "read: Opened README excerpt",
713
+ entryCount: 2,
714
+ latestKind: "action",
715
+ },
716
+ ]);
717
+ });
718
+
297
719
  test("OpenClawAgentBridge recovers structured assistant text from the session log", async (t) => {
298
720
  const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-openclaw-state-"));
299
721
  const workspaceDir = path.join(stateDir, "workspace");
@@ -319,18 +741,107 @@ test("OpenClawAgentBridge recovers structured assistant text from the session lo
319
741
  sessionLogPath,
320
742
  `${JSON.stringify({
321
743
  type: "message",
744
+ message: {
745
+ role: "assistant",
746
+ content: [
747
+ {
748
+ type: "text",
749
+ text: [
750
+ "Interim text that should be ignored.",
751
+ "<privateclaw-response>",
752
+ '{"version":1,"messages":[{"text":"pixel.png"},],"data":{"filename":"pixel.png"}}',
753
+ "</privateclaw-response>",
754
+ ].join("\n"),
755
+ },
756
+ ],
757
+ isError: false,
758
+ },
759
+ })}\n`,
760
+ "utf8",
761
+ );
762
+ callback(
763
+ null,
764
+ JSON.stringify({
765
+ result: {
766
+ payloads: [],
767
+ },
768
+ }),
769
+ "",
770
+ );
771
+ })().catch((error) => {
772
+ callback(error instanceof Error ? error : new Error(String(error)), "", "");
773
+ });
774
+ },
775
+ });
776
+
777
+ const result = await bridge.handleUserMessage({
778
+ sessionId: "privateclaw-session",
779
+ message: "Reply with the exact filename.",
780
+ });
781
+
782
+ assert.deepEqual(result, {
783
+ messages: [{ text: "pixel.png" }],
784
+ data: { filename: "pixel.png" },
785
+ });
786
+ });
787
+
788
+ test("OpenClawAgentBridge recovers structured assistant attachments from absolute media paths in the session log", async (t) => {
789
+ const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-openclaw-state-"));
790
+ const workspaceDir = path.join(stateDir, "workspace");
791
+ const mediaDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-browser-media-"));
792
+ const imagePath = path.join(mediaDir, "slide-01-cover.png");
793
+ const imageBytes = Buffer.from("fake-png-binary");
794
+ const sessionLogPath = path.join(
795
+ stateDir,
796
+ "agents",
797
+ "main",
798
+ "sessions",
799
+ "privateclaw-session.jsonl",
800
+ );
801
+ await fs.mkdir(workspaceDir, { recursive: true });
802
+ await fs.writeFile(imagePath, imageBytes);
803
+ t.after(async () => {
804
+ await Promise.all([
805
+ fs.rm(stateDir, { recursive: true, force: true }),
806
+ fs.rm(mediaDir, { recursive: true, force: true }),
807
+ ]);
808
+ });
809
+
810
+ const bridge = new OpenClawAgentBridge({
811
+ stateDir,
812
+ workspaceDir,
813
+ execFileImpl: (_file, _args, _options, callback) => {
814
+ void (async () => {
815
+ await fs.mkdir(path.dirname(sessionLogPath), { recursive: true });
816
+ await fs.writeFile(
817
+ sessionLogPath,
818
+ `${JSON.stringify({
819
+ type: "message",
820
+ id: "assistant-message-1",
821
+ timestamp: "2026-03-18T03:16:12.179Z",
322
822
  message: {
323
823
  role: "assistant",
324
824
  content: [
325
825
  {
326
826
  type: "text",
327
827
  text: [
328
- "Interim text that should be ignored.",
828
+ "好了,截图完成!",
829
+ "",
329
830
  "<privateclaw-response>",
330
831
  JSON.stringify({
331
832
  version: 1,
332
- messages: [{ text: "pixel.png" }],
333
- data: { filename: "pixel.png" },
833
+ messages: [
834
+ {
835
+ text: "做了一个演示稿,给你看封面。",
836
+ attachments: [
837
+ {
838
+ name: "slide-01-cover.png",
839
+ mimeType: "image/png",
840
+ filePath: imagePath,
841
+ },
842
+ ],
843
+ },
844
+ ],
334
845
  }),
335
846
  "</privateclaw-response>",
336
847
  ].join("\n"),
@@ -358,13 +869,139 @@ test("OpenClawAgentBridge recovers structured assistant text from the session lo
358
869
 
359
870
  const result = await bridge.handleUserMessage({
360
871
  sessionId: "privateclaw-session",
361
- message: "Reply with the exact filename.",
872
+ message: "Show me the generated slide preview.",
362
873
  });
363
874
 
364
- assert.deepEqual(result, {
365
- messages: [{ text: "pixel.png" }],
366
- data: { filename: "pixel.png" },
875
+ if (typeof result === "string") {
876
+ assert.fail("Expected an attachment-bearing bridge response.");
877
+ }
878
+ const [message] = result.messages;
879
+ assert(message && typeof message !== "string");
880
+ assert.equal(message.text, "做了一个演示稿,给你看封面。");
881
+ const [attachment] = message.attachments ?? [];
882
+ assert(attachment);
883
+ assert.match(attachment.id, /^structured-attachment-/u);
884
+ assert.equal(attachment.name, "slide-01-cover.png");
885
+ assert.equal(attachment.mimeType, "image/png");
886
+ assert.equal(attachment.sizeBytes, imageBytes.byteLength);
887
+ assert.equal(attachment.dataBase64, imageBytes.toString("base64"));
888
+ });
889
+
890
+ test("OpenClawAgentBridge prefers structured session-log replies over earlier stdout text and dedupes recovered artifacts", async (t) => {
891
+ const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-openclaw-state-"));
892
+ const workspaceDir = path.join(stateDir, "workspace");
893
+ const mediaDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-browser-media-"));
894
+ const imagePath = path.join(mediaDir, "google-screenshot.png");
895
+ const imageBytes = Buffer.from("fake-google-screenshot");
896
+ const sessionLogPath = path.join(
897
+ stateDir,
898
+ "agents",
899
+ "main",
900
+ "sessions",
901
+ "privateclaw-session.jsonl",
902
+ );
903
+ await fs.mkdir(workspaceDir, { recursive: true });
904
+ await fs.writeFile(imagePath, imageBytes);
905
+ t.after(async () => {
906
+ await Promise.all([
907
+ fs.rm(stateDir, { recursive: true, force: true }),
908
+ fs.rm(mediaDir, { recursive: true, force: true }),
909
+ ]);
367
910
  });
911
+
912
+ const bridge = new OpenClawAgentBridge({
913
+ stateDir,
914
+ workspaceDir,
915
+ execFileImpl: (_file, _args, _options, callback) => {
916
+ void (async () => {
917
+ await fs.mkdir(path.dirname(sessionLogPath), { recursive: true });
918
+ await fs.writeFile(
919
+ sessionLogPath,
920
+ [
921
+ JSON.stringify({
922
+ type: "message",
923
+ id: "tool-result-1",
924
+ timestamp: "2026-03-18T08:25:18.564Z",
925
+ message: {
926
+ role: "toolResult",
927
+ toolName: "browser_navigate",
928
+ content: [{ type: "text", text: `MEDIA:${imagePath}` }],
929
+ isError: false,
930
+ },
931
+ }),
932
+ JSON.stringify({
933
+ type: "message",
934
+ id: "assistant-message-1",
935
+ timestamp: "2026-03-18T08:25:29.290Z",
936
+ message: {
937
+ role: "assistant",
938
+ content: [
939
+ {
940
+ type: "text",
941
+ text: [
942
+ "<privateclaw-response>",
943
+ JSON.stringify({
944
+ version: 1,
945
+ messages: [
946
+ {
947
+ text: "已为你打开谷歌并截图",
948
+ attachments: [
949
+ {
950
+ name: "google-screenshot.png",
951
+ mimeType: "image/png",
952
+ filePath: imagePath,
953
+ },
954
+ ],
955
+ },
956
+ ],
957
+ data: {},
958
+ }),
959
+ "</privateclaw-response>",
960
+ ].join("\n"),
961
+ },
962
+ ],
963
+ isError: false,
964
+ },
965
+ }),
966
+ "",
967
+ ].join("\n"),
968
+ "utf8",
969
+ );
970
+ callback(
971
+ null,
972
+ JSON.stringify({
973
+ status: "ok",
974
+ result: {
975
+ payloads: [{ text: "我来帮你打开谷歌并截图。" }],
976
+ },
977
+ }),
978
+ "",
979
+ );
980
+ })().catch((error) => {
981
+ callback(error instanceof Error ? error : new Error(String(error)), "", "");
982
+ });
983
+ },
984
+ });
985
+
986
+ const result = await bridge.handleUserMessage({
987
+ sessionId: "privateclaw-session",
988
+ message: "打开谷歌截个图给我",
989
+ });
990
+
991
+ if (typeof result === "string") {
992
+ assert.fail("Expected an attachment-bearing bridge response.");
993
+ }
994
+ assert.equal(result.messages.length, 1);
995
+ const [message] = result.messages;
996
+ assert(message && typeof message !== "string");
997
+ assert.equal(message.text, "已为你打开谷歌并截图");
998
+ assert.equal(message.attachments?.length, 1);
999
+ const [attachment] = message.attachments ?? [];
1000
+ assert(attachment);
1001
+ assert.equal(attachment.name, "google-screenshot.png");
1002
+ assert.equal(attachment.mimeType, "image/png");
1003
+ assert.equal(attachment.sizeBytes, imageBytes.byteLength);
1004
+ assert.equal(attachment.dataBase64, imageBytes.toString("base64"));
368
1005
  });
369
1006
 
370
1007
  test("OpenClawAgentBridge bridges TTS audio artifacts from the session log", async (t) => {