codex-grok-bridge 1.7.1 → 1.8.0

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/src/tools.mjs CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  isImageGenerationItem,
5
5
  saveGeneratedImage,
6
6
  } from "./imagegen.mjs";
7
+ import { GROK_VIDEO_TOOL, GROK_VIDEO_TOOL_NAME } from "./videogen.mjs";
7
8
  import { DEFAULT_GROK_MODEL, resolveGrokModel } from "./models.mjs";
8
9
  import {
9
10
  applyCacheUsage,
@@ -27,14 +28,6 @@ const CUSTOM_INPUT_SCHEMA = {
27
28
  additionalProperties: false,
28
29
  };
29
30
 
30
- const WEB_SEARCH_SCHEMA = {
31
- type: "object",
32
- properties: {
33
- query: { type: "string", description: "Search query" },
34
- },
35
- required: ["query"],
36
- };
37
-
38
31
  const EFFORT = {
39
32
  ultra: "xhigh",
40
33
  max: "xhigh",
@@ -50,12 +43,14 @@ const DROP = Symbol("drop");
50
43
  // Items whose payload only the originating provider can read. "reasoning" is
51
44
  // deliberately absent: Codex reasoning items carry a plain-text summary that is
52
45
  // useful to Grok across a multi-call turn, and only their encrypted_content is
53
- // opaque - that field is stripped by the key filter below.
54
- const PROVIDER_OPAQUE_TYPES = new Set([
46
+ // opaque - that field is stripped by the key filter below. Compaction items are
47
+ // likewise absent: a stored summary is rewritten into a user message, and an
48
+ // item with no readable text is dropped.
49
+ const PROVIDER_OPAQUE_TYPES = new Set(["encrypted_content"]);
50
+ const COMPACTION_ITEM_TYPES = new Set([
55
51
  "compaction",
56
52
  "compaction_summary",
57
53
  "context_compaction",
58
- "encrypted_content",
59
54
  ]);
60
55
  const GROK_INPUT_ITEM_TYPES = new Set([
61
56
  "message",
@@ -90,6 +85,14 @@ function pickKeys(node, keys) {
90
85
 
91
86
  function whitelistContentPart(part) {
92
87
  if (!part || typeof part !== "object" || Array.isArray(part)) return part;
88
+ // Chat audio is not a Responses content part Grok accepts. Leaving the bytes
89
+ // in history makes the upstream reject the turn, and every later turn with it.
90
+ if (part.type === "input_audio") {
91
+ return {
92
+ type: "input_text",
93
+ text: "[An audio attachment was not sent to the model. Say so if the user asks about it.]",
94
+ };
95
+ }
93
96
  const keys = CONTENT_PART_FIELDS[part.type];
94
97
  if (!keys) return part;
95
98
  const next = pickKeys(part, keys);
@@ -233,6 +236,21 @@ function decodeCustomInput(argumentsValue) {
233
236
  return argumentsValue;
234
237
  }
235
238
 
239
+ function plainObject(value) {
240
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
241
+ }
242
+
243
+ // xAI runs `{type:"web_search"}` on the server. A hashed function is never
244
+ // called, so Codex's hosted tool stays one server tool. Keep filters and
245
+ // allowed_domains; every other field on that tool is dropped.
246
+ function keptWebSearchFields(tool) {
247
+ const next = {};
248
+ if (plainObject(tool.filters)) next.filters = tool.filters;
249
+ if (Array.isArray(tool.allowed_domains))
250
+ next.allowed_domains = tool.allowed_domains;
251
+ return next;
252
+ }
253
+
236
254
  function addFunction(flattened, map, spec) {
237
255
  // Index names change when Codex reorders tools, which rewrites every
238
256
  // earlier function_call and breaks the prompt-cache prefix.
@@ -264,8 +282,25 @@ function addFunction(flattened, map, spec) {
264
282
  export function flattenCodexTools(tools = []) {
265
283
  const flattened = [];
266
284
  const map = new Map();
285
+ let webSearch = null;
267
286
  for (const tool of tools) {
268
287
  if (!tool || typeof tool !== "object") continue;
288
+ if (tool.type === "web_search") {
289
+ const kept = keptWebSearchFields(tool);
290
+ if (!webSearch) {
291
+ webSearch = { type: "web_search", ...kept };
292
+ flattened.push(webSearch);
293
+ } else {
294
+ if (webSearch.filters === undefined && kept.filters !== undefined)
295
+ webSearch.filters = kept.filters;
296
+ if (
297
+ webSearch.allowed_domains === undefined &&
298
+ kept.allowed_domains !== undefined
299
+ )
300
+ webSearch.allowed_domains = kept.allowed_domains;
301
+ }
302
+ continue;
303
+ }
269
304
  if (tool.type === "function" || tool.type === "custom") {
270
305
  addFunction(flattened, map, {
271
306
  kind: tool.type === "custom" ? "custom" : "function",
@@ -276,16 +311,6 @@ export function flattenCodexTools(tools = []) {
276
311
  });
277
312
  continue;
278
313
  }
279
- if (tool.type === "web_search") {
280
- addFunction(flattened, map, {
281
- kind: "web_search",
282
- namespace: null,
283
- name: "web_search",
284
- description: tool.description || "Search the web",
285
- parameters: tool.parameters || WEB_SEARCH_SCHEMA,
286
- });
287
- continue;
288
- }
289
314
  if (tool.type === "namespace" && Array.isArray(tool.tools)) {
290
315
  for (const nested of tool.tools) {
291
316
  if (!nested || typeof nested !== "object") continue;
@@ -301,6 +326,10 @@ export function flattenCodexTools(tools = []) {
301
326
  }
302
327
  flattened.push(stableJsonValue(structuredClone(tool)));
303
328
  }
329
+ if (webSearch) {
330
+ const index = flattened.indexOf(webSearch);
331
+ flattened[index] = stableJsonValue(structuredClone(webSearch));
332
+ }
304
333
  return { tools: flattened, map };
305
334
  }
306
335
 
@@ -316,13 +345,269 @@ export function proxyToolChoice(choice, map) {
316
345
  return "auto";
317
346
  }
318
347
 
319
- function toProxyInputNode(node, map, state) {
348
+ function readableString(value) {
349
+ return typeof value === "string" && value.trim() ? value : null;
350
+ }
351
+
352
+ function textsFromValue(value) {
353
+ const direct = readableString(value);
354
+ if (direct) return [direct];
355
+ if (!Array.isArray(value)) return [];
356
+ const texts = [];
357
+ for (const part of value) {
358
+ if (!part || typeof part !== "object" || part.type === "encrypted_content")
359
+ continue;
360
+ const text = readableString(part.text);
361
+ if (text) texts.push(text);
362
+ }
363
+ return texts;
364
+ }
365
+
366
+ // Codex replaces older history with a compaction item. The encrypted blob is
367
+ // provider-private, but the item may already carry the summary as plain text.
368
+ // That text is what Grok can read; everything else on the item is dropped.
369
+ function compactionInputTexts(node) {
370
+ for (const key of ["summary", "content", "text", "message"]) {
371
+ const texts = textsFromValue(node[key]);
372
+ if (texts.length) return texts;
373
+ }
374
+ return [];
375
+ }
376
+
377
+ function rememberText(texts, seen, value) {
378
+ const text = readableString(value);
379
+ if (!text || seen.has(text)) return;
380
+ seen.add(text);
381
+ texts.push(text);
382
+ }
383
+
384
+ // Content parts, stdout/stderr rows, and bare string lists. Encrypted parts
385
+ // are skipped. This does not walk arbitrary keys, so ids, status, env, and
386
+ // image bytes are not treated as text.
387
+ function rememberTextValue(texts, seen, value) {
388
+ if (typeof value === "string") {
389
+ rememberText(texts, seen, value);
390
+ return;
391
+ }
392
+ if (Array.isArray(value)) {
393
+ for (const part of value) {
394
+ if (typeof part === "string") {
395
+ rememberText(texts, seen, part);
396
+ continue;
397
+ }
398
+ if (!part || typeof part !== "object" || part.type === "encrypted_content")
399
+ continue;
400
+ rememberText(texts, seen, part.text);
401
+ rememberText(texts, seen, part.stdout);
402
+ rememberText(texts, seen, part.stderr);
403
+ }
404
+ return;
405
+ }
406
+ if (!value || typeof value !== "object") return;
407
+ rememberText(texts, seen, value.text);
408
+ rememberText(texts, seen, value.stdout);
409
+ rememberText(texts, seen, value.stderr);
410
+ if (typeof value.content === "string" || Array.isArray(value.content))
411
+ rememberTextValue(texts, seen, value.content);
412
+ }
413
+
414
+ function commandLines(action) {
415
+ const lines = [];
416
+ for (const list of [action.command, action.commands]) {
417
+ if (Array.isArray(list)) {
418
+ const line = list.filter((part) => typeof part === "string").join(" ").trim();
419
+ if (line) lines.push(line);
420
+ } else if (typeof list === "string" && list.trim()) {
421
+ lines.push(list.trim());
422
+ }
423
+ }
424
+ return lines;
425
+ }
426
+
427
+ function rememberArguments(texts, seen, value) {
428
+ if (typeof value === "string") {
429
+ try {
430
+ const parsed = JSON.parse(value);
431
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
432
+ value = parsed;
433
+ else {
434
+ rememberText(texts, seen, value);
435
+ return;
436
+ }
437
+ } catch {
438
+ rememberText(texts, seen, value);
439
+ return;
440
+ }
441
+ }
442
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
443
+ rememberText(texts, seen, value.query);
444
+ rememberTextValue(texts, seen, value.queries);
445
+ }
446
+
447
+ function stripOpaqueFields(value) {
448
+ if (Array.isArray(value)) return value.map(stripOpaqueFields);
449
+ if (!value || typeof value !== "object") return value;
450
+ const next = {};
451
+ for (const [key, child] of Object.entries(value)) {
452
+ if (
453
+ key === "encrypted_content" ||
454
+ key === "encrypted_function_args" ||
455
+ key.startsWith("internal_")
456
+ )
457
+ continue;
458
+ next[key] = stripOpaqueFields(child);
459
+ }
460
+ return next;
461
+ }
462
+
463
+ function containsReadableText(value) {
464
+ if (typeof value === "string") return Boolean(value.trim());
465
+ if (Array.isArray(value)) return value.some(containsReadableText);
466
+ if (value && typeof value === "object")
467
+ return Object.values(value).some(containsReadableText);
468
+ return false;
469
+ }
470
+
471
+ function rememberTools(texts, seen, tools) {
472
+ if (!Array.isArray(tools) || !tools.length) return;
473
+ const stripped = stripOpaqueFields(tools);
474
+ if (!containsReadableText(stripped)) return;
475
+ rememberText(texts, seen, JSON.stringify(stripped));
476
+ }
477
+
478
+ // Text Grok can read off an item type it will not accept. The caller turns
479
+ // these strings into a user message. Fields that 422 when forwarded
480
+ // (encrypted blobs, internal metadata, image bytes) are not read.
481
+ function droppedItemInputTexts(node) {
482
+ const texts = [];
483
+ const seen = new Set();
484
+ for (const key of [
485
+ "summary",
486
+ "content",
487
+ "text",
488
+ "message",
489
+ "input",
490
+ "output",
491
+ "query",
492
+ "revised_prompt",
493
+ ]) {
494
+ rememberTextValue(texts, seen, node[key]);
495
+ }
496
+ const action = node.action;
497
+ if (action && typeof action === "object" && !Array.isArray(action)) {
498
+ const lines = commandLines(action);
499
+ for (const line of lines) rememberText(texts, seen, line);
500
+ if (lines.length) rememberText(texts, seen, action.working_directory);
501
+ rememberText(texts, seen, action.query);
502
+ rememberTextValue(texts, seen, action.queries);
503
+ rememberText(texts, seen, action.url);
504
+ rememberText(texts, seen, action.pattern);
505
+ if (Array.isArray(action.sources)) {
506
+ for (const source of action.sources) {
507
+ if (typeof source === "string") {
508
+ rememberText(texts, seen, source);
509
+ continue;
510
+ }
511
+ if (!source || typeof source !== "object") continue;
512
+ rememberText(texts, seen, source.url);
513
+ rememberText(texts, seen, source.title);
514
+ rememberText(texts, seen, source.snippet);
515
+ rememberText(texts, seen, source.text);
516
+ }
517
+ }
518
+ }
519
+ rememberArguments(texts, seen, node.arguments);
520
+ rememberTools(texts, seen, node.tools);
521
+ const aggregated = readableString(node.aggregated_output) ?? readableString(node.formatted_output);
522
+ if (aggregated) rememberText(texts, seen, aggregated);
523
+ else {
524
+ rememberText(texts, seen, node.stdout);
525
+ rememberText(texts, seen, node.stderr);
526
+ }
527
+ const rememberExit = (value) => {
528
+ if (Number.isInteger(value) && value !== 0) rememberText(texts, seen, `exit ${value}`);
529
+ };
530
+ rememberExit(node.exit_code);
531
+ const output = node.output;
532
+ if (Array.isArray(output)) {
533
+ for (const row of output) {
534
+ if (!row || typeof row !== "object") continue;
535
+ rememberExit(row.exit_code);
536
+ rememberExit(row.outcome?.exit_code);
537
+ }
538
+ } else if (output && typeof output === "object") {
539
+ rememberExit(output.exit_code);
540
+ rememberExit(output.outcome?.exit_code);
541
+ }
542
+ return texts;
543
+ }
544
+
545
+ const INLINE_IMAGE = /^data:image\/(?:png|jpeg|webp);base64,/i;
546
+
547
+ function functionOutputParts(output) {
548
+ if (typeof output === "string") return { text: output, images: [] };
549
+ const parts = [];
550
+ const images = [];
551
+ const push = (value) => {
552
+ if (typeof value === "string" && value.trim()) parts.push(value.trim());
553
+ };
554
+ const visit = (value) => {
555
+ if (typeof value === "string") {
556
+ push(value);
557
+ return;
558
+ }
559
+ if (Array.isArray(value)) {
560
+ for (const part of value) visit(part);
561
+ return;
562
+ }
563
+ if (!value || typeof value !== "object") return;
564
+ push(value.text);
565
+ push(value.stdout);
566
+ push(value.stderr);
567
+ const image = value.image_url;
568
+ const url =
569
+ typeof image === "string"
570
+ ? image
571
+ : image && typeof image === "object" && typeof image.url === "string"
572
+ ? image.url
573
+ : "";
574
+ if (url && INLINE_IMAGE.test(url)) {
575
+ const part = { type: "input_image", image_url: url };
576
+ const detail =
577
+ typeof value.detail === "string"
578
+ ? value.detail
579
+ : image && typeof image === "object" && typeof image.detail === "string"
580
+ ? image.detail
581
+ : "";
582
+ if (detail) part.detail = detail;
583
+ images.push(part);
584
+ push("[An image from this tool result follows.]");
585
+ } else if (url) {
586
+ push(url);
587
+ }
588
+ if (Array.isArray(value.content)) visit(value.content);
589
+ };
590
+ visit(output);
591
+ return { text: parts.join("\n"), images };
592
+ }
593
+
594
+ function userInputTextMessage(texts) {
595
+ return whitelistInputNode({
596
+ type: "message",
597
+ role: "user",
598
+ content: texts.map((text) => ({ type: "input_text", text })),
599
+ });
600
+ }
601
+
602
+ function toProxyInputNode(node, map, state, salvage = false) {
320
603
  if (!node || typeof node !== "object") return node;
321
- if (Array.isArray(node)) {
604
+ if (Array.isArray(node)) {
322
605
  const items = [];
323
606
  for (const item of node) {
324
607
  const next = toProxyInputNode(item, map, state);
325
- if (next !== DROP) items.push(next);
608
+ if (next === DROP) continue;
609
+ if (Array.isArray(next)) items.push(...next);
610
+ else items.push(next);
326
611
  }
327
612
  return items;
328
613
  }
@@ -342,15 +627,32 @@ function toProxyInputNode(node, map, state) {
342
627
  if (converted !== DROP) next[key] = converted;
343
628
  }
344
629
 
630
+ if (COMPACTION_ITEM_TYPES.has(next.type)) {
631
+ const texts = compactionInputTexts(next);
632
+ if (!texts.length) return DROP;
633
+ return whitelistInputNode({
634
+ type: "message",
635
+ role: "user",
636
+ content: texts.map((text) => ({ type: "input_text", text })),
637
+ });
638
+ }
639
+
345
640
  if (next.type === "reasoning") {
346
641
  // Forward the summary and nothing else. Codex's own item id and null
347
642
  // content fields mean nothing to the upstream and only widen the surface
348
- // for a schema rejection.
349
- const summary = (Array.isArray(next.summary) ? next.summary : []).filter(
350
- (part) => part && typeof part.text === "string" && part.text.trim(),
351
- );
352
- if (!summary.length) return DROP;
353
- return whitelistInputNode({ type: "reasoning", summary });
643
+ // for a schema rejection. When the summary is empty, the plain text in
644
+ // content is the only readable reasoning left.
645
+ const readable = (parts) =>
646
+ (Array.isArray(parts) ? parts : []).filter(
647
+ (part) => part && typeof part.text === "string" && part.text.trim() && part.type !== "encrypted_content",
648
+ );
649
+ const summary = readable(next.summary);
650
+ const chosen = summary.length ? summary : readable(next.content);
651
+ if (!chosen.length) return DROP;
652
+ return whitelistInputNode({
653
+ type: "reasoning",
654
+ summary: chosen.map((part) => ({ type: "summary_text", text: part.text.trim() })),
655
+ });
354
656
  }
355
657
 
356
658
  if (next.type === "agent_message") {
@@ -404,7 +706,34 @@ function toProxyInputNode(node, map, state) {
404
706
  }
405
707
  }
406
708
 
407
- return whitelistInputNode(next);
709
+ // Codex sends a string or a list of content parts. Grok accepts the string.
710
+ // An inline image in that list is attached after the tool result so the
711
+ // model can see it; a remote URL stays as text and is not fetched.
712
+ let attachedImages = [];
713
+ if (next.type === "function_call_output" && typeof next.output !== "string") {
714
+ const parts = functionOutputParts(next.output);
715
+ next.output = parts.text;
716
+ attachedImages = parts.images;
717
+ }
718
+
719
+ const whitelisted = whitelistInputNode(next);
720
+ if (!salvage || isForwardedItem(whitelisted)) {
721
+ if (!attachedImages.length) return whitelisted;
722
+ return [
723
+ whitelisted,
724
+ whitelistInputNode({
725
+ type: "message",
726
+ role: "user",
727
+ content: attachedImages,
728
+ }),
729
+ ];
730
+ }
731
+ // Grok rejects these Codex item types. Keep the readable text as a user
732
+ // message, the same shape as a compaction summary. An item with nothing
733
+ // left but an encrypted blob, image bytes, or ids is dropped.
734
+ const texts = droppedItemInputTexts(next);
735
+ if (!texts.length) return DROP;
736
+ return userInputTextMessage(texts);
408
737
  }
409
738
 
410
739
  function isForwardedItem(item) {
@@ -419,16 +748,22 @@ function isForwardedItem(item) {
419
748
 
420
749
  function projectInput(input, map) {
421
750
  if (!Array.isArray(input)) {
422
- const items = toProxyInputNode(input, map, { callIds: new Map() });
423
- return { items, pairs: [] };
751
+ const items = toProxyInputNode(input, map, { callIds: new Map() }, true);
752
+ return { items: Array.isArray(items) ? items : items === DROP ? [] : [items], pairs: [] };
424
753
  }
425
754
  const state = { callIds: new Map() };
426
755
  const pairs = [];
427
756
  for (const item of input) {
428
757
  const fingerprint = logicalFingerprint(item);
429
- const next = toProxyInputNode(item, map, state);
430
- if (!isForwardedItem(next)) continue;
431
- pairs.push({ fingerprint, item: next });
758
+ const produced = toProxyInputNode(item, map, state, true);
759
+ const list = Array.isArray(produced) ? produced : [produced];
760
+ list.forEach((next, index) => {
761
+ if (!isForwardedItem(next)) return;
762
+ pairs.push({
763
+ fingerprint: index === 0 ? fingerprint : logicalFingerprint(next),
764
+ item: next,
765
+ });
766
+ });
432
767
  }
433
768
  return { items: pairs.map((pair) => pair.item), pairs };
434
769
  }
@@ -463,6 +798,14 @@ export const IMAGE_GENERATION_PROVENANCE =
463
798
  "and do not use OpenAI, image_gen, or a Python image CLI — those send the " +
464
799
  "picture to another vendor.";
465
800
 
801
+ // Codex does not offer a video tool to this provider, and cli-chat-proxy has
802
+ // no video_generation tool type. The function below is the bridge's own.
803
+ export const VIDEO_GENERATION_PROVENANCE =
804
+ "Videos: clips on this transport are generated by the grok_bridge_generate_video " +
805
+ "function, which is already on the request. The bridge calls Grok's videos API " +
806
+ "with the grok login, saves the temporary file locally, and returns that path. " +
807
+ "Do not ask for an API key and do not send the clip to another vendor.";
808
+
466
809
  export function toProxyRequest(body) {
467
810
  const { tools, map } = flattenCodexTools(body.tools ?? []);
468
811
  const effort = EFFORT[body.reasoning?.effort] ?? "high";
@@ -484,13 +827,25 @@ export function toProxyRequest(body) {
484
827
  const imageGenOn = process.env.GROK_BRIDGE_IMAGE_GEN !== "off";
485
828
  if (imageGenOn && !declaresImageTool)
486
829
  request.tools = [...request.tools, GROK_IMAGE_TOOL];
830
+ // Codex tool names are rewritten to codex_<hash>_…, so this literal name
831
+ // cannot collide with one. A second copy is not added if it is already there.
832
+ const videoGenOn = process.env.GROK_BRIDGE_VIDEO_GEN !== "off";
833
+ const declaresVideoTool = request.tools.some(
834
+ (tool) => tool?.name === GROK_VIDEO_TOOL_NAME,
835
+ );
836
+ if (videoGenOn && !declaresVideoTool)
837
+ request.tools = [...request.tools, GROK_VIDEO_TOOL];
487
838
  if (tools.length) {
488
839
  request.tool_choice = proxyToolChoice(body.tool_choice, map);
489
840
  request.parallel_tool_calls = body.parallel_tool_calls !== false;
490
841
  }
491
842
  const provenanceLine = transportProvenance(request.model);
492
- const provenance = imageGenOn
493
- ? `${provenanceLine}\n\n${IMAGE_GENERATION_PROVENANCE}`
843
+ const notes = [
844
+ imageGenOn ? IMAGE_GENERATION_PROVENANCE : "",
845
+ videoGenOn ? VIDEO_GENERATION_PROVENANCE : "",
846
+ ].filter(Boolean);
847
+ const provenance = notes.length
848
+ ? `${provenanceLine}\n\n${notes.join("\n\n")}`
494
849
  : provenanceLine;
495
850
  request.instructions =
496
851
  typeof body.instructions === "string" && body.instructions
@@ -512,14 +867,29 @@ function restoreOriginName(node, origin) {
512
867
  else delete node.namespace;
513
868
  }
514
869
 
870
+ function originForResponseName(map, name) {
871
+ if (typeof name !== "string") return null;
872
+ if (map.has(name)) return map.get(name);
873
+ let found = null;
874
+ for (const origin of map.values()) {
875
+ if (origin.name !== name) continue;
876
+ if (found) return null;
877
+ found = origin;
878
+ }
879
+ return found;
880
+ }
881
+
515
882
  function rewriteResponseItem(node, map, state) {
516
883
  if (!node || typeof node !== "object") return;
517
- if (typeof node.name === "string" && map.has(node.name)) {
518
- const origin = map.get(node.name);
884
+ const origin =
885
+ originForResponseName(map, node.name) ??
886
+ (typeof node.call_id === "string" ? state.callIds.get(node.call_id) : null);
887
+ if (origin) {
519
888
  rememberProxyItem(node, origin, state);
520
889
  restoreOriginName(node, origin);
521
890
  if (origin.kind === "custom") {
522
891
  if (node.type === "function_call") node.type = "custom_tool_call";
892
+ if (node.type === "function_call_output") node.type = "custom_tool_call_output";
523
893
  if (node.type === "custom_tool_call") {
524
894
  if (node.input == null) node.input = decodeCustomInput(node.arguments);
525
895
  delete node.arguments;
@@ -533,14 +903,27 @@ function rewriteResponseItem(node, map, state) {
533
903
  // and no place to put them, so the bridge writes the file and hands back an
534
904
  // ordinary assistant message naming it.
535
905
  function absorbGeneratedImage(item, state) {
906
+ const key = typeof item.id === "string" ? item.id : "";
907
+ if (key && state.savedImages?.has(key)) return state.savedImages.get(key);
536
908
  const saved = saveGeneratedImage(item, state.imageOptions);
537
- return {
909
+ const described = describeGeneratedImage(
910
+ typeof item.prompt === "string" && item.prompt.trim()
911
+ ? item
912
+ : { ...item, prompt: item.revised_prompt },
913
+ saved,
914
+ );
915
+ const message = {
538
916
  type: "message",
539
- id: typeof item.id === "string" ? item.id : undefined,
917
+ id: key || undefined,
540
918
  role: "assistant",
541
919
  status: "completed",
542
- content: [{ type: "output_text", text: describeGeneratedImage(item, saved) }],
920
+ content: [{ type: "output_text", text: described }],
543
921
  };
922
+ if (key) {
923
+ if (!state.savedImages) state.savedImages = new Map();
924
+ state.savedImages.set(key, message);
925
+ }
926
+ return message;
544
927
  }
545
928
 
546
929
  const USAGE_EVENTS = new Set(["response.completed", "response.incomplete"]);
@@ -556,6 +939,24 @@ function rewriteResponseEvent(value, map, state) {
556
939
  const cache = readCacheUsage(value.response.usage);
557
940
  if (cache) state.cacheUsage = cache;
558
941
  }
942
+ if (Array.isArray(value.response?.output)) {
943
+ for (let i = 0; i < value.response.output.length; i += 1) {
944
+ const item = value.response.output[i];
945
+ if (isImageGenerationItem(item))
946
+ value.response.output[i] = absorbGeneratedImage(item, state);
947
+ else rewriteResponseItem(item, map, state);
948
+ }
949
+ }
950
+ if (
951
+ value.type === "response.function_call_arguments.delta" ||
952
+ value.type === "response.function_call_arguments.done"
953
+ ) {
954
+ const origin = originForResponseName(map, value.name);
955
+ if (origin) {
956
+ rememberProxyItem(value, origin, state);
957
+ value.name = origin.name;
958
+ }
959
+ }
559
960
  if (
560
961
  typeof value.item === "object" &&
561
962
  value.item &&
@@ -614,6 +1015,7 @@ export function createSseRewriter(map, options = {}) {
614
1015
  const state = {
615
1016
  callIds: new Map(),
616
1017
  itemIds: new Map(),
1018
+ savedImages: new Map(),
617
1019
  imageOptions: options.imageOptions,
618
1020
  cacheUsage: null,
619
1021
  };