@privateclaw/privateclaw 0.1.17 → 0.1.23

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.
@@ -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({
@@ -114,6 +130,225 @@ test("parseOpenClawAgentOutput tolerates trailing commas inside structured respo
114
130
  assert.equal(result, "voice-reply");
115
131
  });
116
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
+
117
352
  test("OpenClawAgentBridge invokes openclaw agent with session-aware arguments", async () => {
118
353
  let invokedFile = "";
119
354
  let invokedArgs: string[] = [];
@@ -164,6 +399,7 @@ test("OpenClawAgentBridge invokes openclaw agent with session-aware arguments",
164
399
  assert.match(prompt, /^hello\b/u);
165
400
  assert.match(prompt, /PrivateClaw response contract:/u);
166
401
  assert.match(prompt, /<privateclaw-response>/u);
402
+ assert.match(prompt, /Never put channel-specific markup such as <qqimg>/u);
167
403
  assert.deepEqual(invokedArgs.slice(5), [
168
404
  "--json",
169
405
  "--agent",
@@ -363,6 +599,123 @@ test("OpenClawAgentBridge recovers assistant text from the session log when agen
363
599
  assert.equal(result, "pixel.png");
364
600
  });
365
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
+
366
719
  test("OpenClawAgentBridge recovers structured assistant text from the session log", async (t) => {
367
720
  const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-openclaw-state-"));
368
721
  const workspaceDir = path.join(stateDir, "workspace");
@@ -432,6 +785,225 @@ test("OpenClawAgentBridge recovers structured assistant text from the session lo
432
785
  });
433
786
  });
434
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",
822
+ message: {
823
+ role: "assistant",
824
+ content: [
825
+ {
826
+ type: "text",
827
+ text: [
828
+ "好了,截图完成!",
829
+ "",
830
+ "<privateclaw-response>",
831
+ JSON.stringify({
832
+ version: 1,
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
+ ],
845
+ }),
846
+ "</privateclaw-response>",
847
+ ].join("\n"),
848
+ },
849
+ ],
850
+ isError: false,
851
+ },
852
+ })}\n`,
853
+ "utf8",
854
+ );
855
+ callback(
856
+ null,
857
+ JSON.stringify({
858
+ result: {
859
+ payloads: [],
860
+ },
861
+ }),
862
+ "",
863
+ );
864
+ })().catch((error) => {
865
+ callback(error instanceof Error ? error : new Error(String(error)), "", "");
866
+ });
867
+ },
868
+ });
869
+
870
+ const result = await bridge.handleUserMessage({
871
+ sessionId: "privateclaw-session",
872
+ message: "Show me the generated slide preview.",
873
+ });
874
+
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
+ ]);
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"));
1005
+ });
1006
+
435
1007
  test("OpenClawAgentBridge bridges TTS audio artifacts from the session log", async (t) => {
436
1008
  const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-openclaw-state-"));
437
1009
  const workspaceDir = path.join(stateDir, "workspace");