codex-grok-bridge 1.7.2 → 1.8.1

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.
@@ -0,0 +1,212 @@
1
+ import { timingSafeEqual } from "node:crypto";
2
+ import tls from "node:tls";
3
+ import { GrokAuthError, readGrokBearerToken } from "./auth.mjs";
4
+ import { acceptLocalSideband, isLocalVoiceSideband } from "./voice.mjs";
5
+
6
+ // Codex posts the WebRTC SDP offer to the provider base
7
+ // (`POST /v1/realtime/calls`, and v3 `POST /v1/live`). The body is raw bytes
8
+ // (Content-Type application/sdp, or whatever Codex sent). xAI answers with
9
+ // the SDP body, its status, and a Location header. This is a byte pipe:
10
+ // nothing here parses JSON or writes an SDP answer.
11
+
12
+ export const DEFAULT_REALTIME_API_BASE = "https://api.x.ai/v1";
13
+
14
+ export async function forwardRealtime(options) {
15
+ const fetchImpl = options.fetchImpl ?? fetch;
16
+ const base = (options.baseUrl ?? DEFAULT_REALTIME_API_BASE).replace(/\/$/, "");
17
+ const headers = { authorization: `Bearer ${options.token}` };
18
+ if (typeof options.contentType === "string") headers["content-type"] = options.contentType;
19
+ const response = await fetchImpl(`${base}${options.path}${options.search ?? ""}`, {
20
+ method: "POST",
21
+ headers,
22
+ body: options.body,
23
+ redirect: "manual",
24
+ signal: options.signal,
25
+ });
26
+ const body = Buffer.from(await response.arrayBuffer());
27
+ return {
28
+ status: response.status,
29
+ body,
30
+ contentType: response.headers.get("content-type"),
31
+ location: response.headers.get("location"),
32
+ };
33
+ }
34
+
35
+ export function connectSideband() {
36
+ return tls.connect({ host: "api.x.ai", port: 443, servername: "api.x.ai" });
37
+ }
38
+
39
+ export function isSidebandUpgradePath(url) {
40
+ let pathname = "/";
41
+ try {
42
+ pathname = new URL(url ?? "/", "http://127.0.0.1").pathname;
43
+ } catch {
44
+ return false;
45
+ }
46
+ return (
47
+ pathname === "/v1/realtime" ||
48
+ pathname.startsWith("/v1/realtime/") ||
49
+ pathname === "/v1/live" ||
50
+ pathname.startsWith("/v1/live/")
51
+ );
52
+ }
53
+
54
+ function headerText(value) {
55
+ const text = Array.isArray(value) ? value.join(", ") : String(value ?? "");
56
+ if (!text || /[\r\n]/.test(text)) return null;
57
+ return text;
58
+ }
59
+
60
+ function bearerMatches(header, token) {
61
+ const expected = Buffer.from(`Bearer ${token}`);
62
+ const actual = Buffer.from(header ?? "");
63
+ return actual.length === expected.length && timingSafeEqual(actual, expected);
64
+ }
65
+
66
+ function upgradeRequest(url, headers, token) {
67
+ const lines = [
68
+ `GET ${url} HTTP/1.1`,
69
+ "Host: api.x.ai",
70
+ "Upgrade: websocket",
71
+ "Connection: Upgrade",
72
+ `Sec-WebSocket-Key: ${headers.key}`,
73
+ `Sec-WebSocket-Version: ${headers.version}`,
74
+ `Authorization: Bearer ${token}`,
75
+ ];
76
+ if (headers.protocol) lines.push(`Sec-WebSocket-Protocol: ${headers.protocol}`);
77
+ if (headers.extensions) lines.push(`Sec-WebSocket-Extensions: ${headers.extensions}`);
78
+ lines.push("", "");
79
+ return lines.join("\r\n");
80
+ }
81
+
82
+ function readHead(socket) {
83
+ return new Promise((resolve, reject) => {
84
+ let buf = Buffer.alloc(0);
85
+ const cleanup = () => {
86
+ socket.off("readable", onReadable);
87
+ socket.off("error", onError);
88
+ socket.off("close", onClose);
89
+ };
90
+ let reading = false;
91
+ const onReadable = () => {
92
+ if (reading) return;
93
+ reading = true;
94
+ let chunk = socket.read();
95
+ while (chunk) {
96
+ buf = Buffer.concat([buf, chunk]);
97
+ chunk = socket.read();
98
+ }
99
+ reading = false;
100
+ const end = buf.indexOf("\r\n\r\n");
101
+ if (end === -1) {
102
+ if (buf.length > 65536) {
103
+ cleanup();
104
+ reject(new Error("header too large"));
105
+ }
106
+ return;
107
+ }
108
+ cleanup();
109
+ resolve({ head: buf.subarray(0, end + 4), rest: buf.subarray(end + 4) });
110
+ };
111
+ const onError = (error) => {
112
+ cleanup();
113
+ reject(error);
114
+ };
115
+ const onClose = () => {
116
+ cleanup();
117
+ reject(new Error("closed"));
118
+ };
119
+ socket.on("readable", onReadable);
120
+ socket.on("error", onError);
121
+ socket.on("close", onClose);
122
+ onReadable();
123
+ });
124
+ }
125
+
126
+ function switching(head) {
127
+ const line = head.toString("latin1").split("\r\n", 1)[0] ?? "";
128
+ return /^HTTP\/1\.[01] 101\b/.test(line);
129
+ }
130
+
131
+ function fail(socket, upstream) {
132
+ socket.destroy();
133
+ upstream?.destroy();
134
+ }
135
+
136
+ export function attachSidebandUpgrade(server, options = {}) {
137
+ server.on("upgrade", (req, socket, head) => {
138
+ pipeSidebandUpgrade(req, socket, head, options).catch(() => fail(socket));
139
+ });
140
+ }
141
+
142
+ async function pipeSidebandUpgrade(req, socket, head, options) {
143
+ const url = String(req.url ?? "/");
144
+ if (!isSidebandUpgradePath(url) || req.headers.origin) return fail(socket);
145
+ if (!bearerMatches(req.headers.authorization, options.token)) return fail(socket);
146
+ const key = headerText(req.headers["sec-websocket-key"]);
147
+ if (isLocalVoiceSideband(url)) {
148
+ if (!key) return fail(socket);
149
+ acceptLocalSideband(socket, key, url, head);
150
+ return;
151
+ }
152
+ const version =
153
+ req.headers["sec-websocket-version"] == null
154
+ ? "13"
155
+ : headerText(req.headers["sec-websocket-version"]);
156
+ if (!key || !version) return fail(socket);
157
+ const protocol = headerText(req.headers["sec-websocket-protocol"]);
158
+ const extensions = headerText(req.headers["sec-websocket-extensions"]);
159
+ if (
160
+ (req.headers["sec-websocket-protocol"] && !protocol) ||
161
+ (req.headers["sec-websocket-extensions"] && !extensions)
162
+ ) {
163
+ return fail(socket);
164
+ }
165
+ let token;
166
+ try {
167
+ token = (options.grokSession ?? readGrokBearerToken(options.grokHome)).token;
168
+ } catch (error) {
169
+ if (error instanceof GrokAuthError) return fail(socket);
170
+ throw error;
171
+ }
172
+ const connect = options.sidebandConnect ?? connectSideband;
173
+ const upstream = connect();
174
+ if (!upstream) return fail(socket);
175
+ socket.on("error", () => upstream.destroy());
176
+ upstream.on("error", () => socket.destroy());
177
+ let settled = false;
178
+ const timer = setTimeout(() => {
179
+ if (settled) return;
180
+ settled = true;
181
+ fail(socket, upstream);
182
+ }, options.sidebandHandshakeMs ?? 10_000);
183
+ const finishHandshake = () => {
184
+ if (settled) return false;
185
+ settled = true;
186
+ clearTimeout(timer);
187
+ return true;
188
+ };
189
+ try {
190
+ socket.pause();
191
+ upstream.write(
192
+ upgradeRequest(url, { key, version, protocol, extensions }, token),
193
+ );
194
+ if (head?.length) upstream.write(head);
195
+ const response = await readHead(upstream);
196
+ if (!finishHandshake()) return;
197
+ if (!switching(response.head)) return fail(socket, upstream);
198
+ socket.write(response.head);
199
+ if (response.rest.length) socket.write(response.rest);
200
+ socket.resume();
201
+ upstream.pipe(socket);
202
+ socket.pipe(upstream);
203
+ socket.on("close", () => upstream.destroy());
204
+ upstream.on("close", () => socket.destroy());
205
+ } catch {
206
+ if (!settled) {
207
+ settled = true;
208
+ clearTimeout(timer);
209
+ }
210
+ fail(socket, upstream);
211
+ }
212
+ }
package/src/runtime.mjs CHANGED
@@ -43,6 +43,9 @@ export async function startRuntime(options = {}) {
43
43
  "-c",
44
44
  `model_providers.grok_build_cli.${key}=${tomlValue(value)}`,
45
45
  ]);
46
+ // Desktop WebRTC reads this for the sideband. Unset, Codex keeps
47
+ // https://api.openai.com/v1. The value is this bridge, not xAI.
48
+ args.push("-c", `experimental_realtime_ws_base_url=${tomlValue(provider.base_url)}`);
46
49
  return {
47
50
  server,
48
51
  token,
package/src/tools.mjs CHANGED
@@ -85,6 +85,14 @@ function pickKeys(node, keys) {
85
85
 
86
86
  function whitelistContentPart(part) {
87
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
+ }
88
96
  const keys = CONTENT_PART_FIELDS[part.type];
89
97
  if (!keys) return part;
90
98
  const next = pickKeys(part, keys);
@@ -155,22 +163,100 @@ function emitObjectParameters(source, overrides = {}) {
155
163
  return next;
156
164
  }
157
165
 
166
+ function decodeJsonPointerToken(token) {
167
+ return token.replace(/~1/g, "/").replace(/~0/g, "~");
168
+ }
169
+
170
+ // `#/$defs/Name` and `#/definitions/Name` are document-local. Grok rejects
171
+ // those pointers, so the copy sent upstream has the target schema inlined.
172
+ function definitionPointer(ref) {
173
+ if (typeof ref !== "string" || !ref.startsWith("#/")) return null;
174
+ const tokens = ref.slice(2).split("/").map(decodeJsonPointerToken);
175
+ if (tokens.length < 2) return null;
176
+ if (tokens[0] !== "$defs" && tokens[0] !== "definitions") return null;
177
+ return tokens;
178
+ }
179
+
180
+ function readPointer(root, tokens) {
181
+ let node = root;
182
+ for (const token of tokens) {
183
+ if (Array.isArray(node)) {
184
+ if (!/^(0|[1-9]\d*)$/.test(token)) return undefined;
185
+ node = node[Number(token)];
186
+ } else if (
187
+ plainObject(node) &&
188
+ Object.prototype.hasOwnProperty.call(node, token)
189
+ ) {
190
+ node = node[token];
191
+ } else {
192
+ return undefined;
193
+ }
194
+ }
195
+ return node;
196
+ }
197
+
198
+ function inlineLocalSchemaRefs(schema) {
199
+ function resolve(ref, trail) {
200
+ const tokens = definitionPointer(ref);
201
+ if (!tokens || trail.includes(ref))
202
+ throw new Error(`Unresolvable schema $ref: ${ref}`);
203
+ const target = readPointer(schema, tokens);
204
+ if (target === undefined)
205
+ throw new Error(`Unresolvable schema $ref: ${ref}`);
206
+ return walk(structuredClone(target), trail.concat(ref));
207
+ }
208
+
209
+ function walk(node, trail) {
210
+ if (Array.isArray(node)) return node.map((item) => walk(item, trail));
211
+ if (!plainObject(node)) return node;
212
+ if (Object.prototype.hasOwnProperty.call(node, "$ref")) {
213
+ if (typeof node.$ref !== "string")
214
+ throw new Error("Unresolvable schema $ref");
215
+ const resolved = resolve(node.$ref, trail);
216
+ const rest = {};
217
+ for (const [key, value] of Object.entries(node)) {
218
+ if (key !== "$ref") rest[key] = value;
219
+ }
220
+ if (Object.keys(rest).length === 0) return resolved;
221
+ if (!plainObject(resolved))
222
+ throw new Error(`Unresolvable schema $ref: ${node.$ref}`);
223
+ return walk({ ...resolved, ...rest }, trail);
224
+ }
225
+ const next = {};
226
+ for (const [key, value] of Object.entries(node)) {
227
+ if (key === "$defs" || key === "definitions") continue;
228
+ next[key] = walk(value, trail);
229
+ }
230
+ return next;
231
+ }
232
+
233
+ return walk(schema, []);
234
+ }
235
+
236
+ function emitsObjectParameters(schema) {
237
+ return (
238
+ schema.type === "object" ||
239
+ (schema.type == null && plainObject(schema.properties))
240
+ );
241
+ }
242
+
158
243
  function usableParameters(parameters) {
159
244
  if (!parameters || typeof parameters !== "object" || Array.isArray(parameters))
160
245
  return OBJECT_SCHEMA;
246
+ parameters = inlineLocalSchemaRefs(parameters);
161
247
  const variants = Array.isArray(parameters.oneOf)
162
248
  ? parameters.oneOf
163
249
  : Array.isArray(parameters.anyOf)
164
250
  ? parameters.anyOf
165
251
  : null;
166
252
  if (!variants) {
167
- return parameters.type === "object"
253
+ return emitsObjectParameters(parameters)
168
254
  ? emitObjectParameters(parameters)
169
255
  : OBJECT_SCHEMA;
170
256
  }
171
257
  const fragments = variants.filter(isObjectFragment);
172
258
  if (fragments.length === 0) {
173
- return parameters.type === "object"
259
+ return emitsObjectParameters(parameters)
174
260
  ? emitObjectParameters(parameters)
175
261
  : OBJECT_SCHEMA;
176
262
  }
@@ -494,12 +580,95 @@ function droppedItemInputTexts(node) {
494
580
  rememberTextValue(texts, seen, action.queries);
495
581
  rememberText(texts, seen, action.url);
496
582
  rememberText(texts, seen, action.pattern);
583
+ if (Array.isArray(action.sources)) {
584
+ for (const source of action.sources) {
585
+ if (typeof source === "string") {
586
+ rememberText(texts, seen, source);
587
+ continue;
588
+ }
589
+ if (!source || typeof source !== "object") continue;
590
+ rememberText(texts, seen, source.url);
591
+ rememberText(texts, seen, source.title);
592
+ rememberText(texts, seen, source.snippet);
593
+ rememberText(texts, seen, source.text);
594
+ }
595
+ }
497
596
  }
498
597
  rememberArguments(texts, seen, node.arguments);
499
598
  rememberTools(texts, seen, node.tools);
599
+ const aggregated = readableString(node.aggregated_output) ?? readableString(node.formatted_output);
600
+ if (aggregated) rememberText(texts, seen, aggregated);
601
+ else {
602
+ rememberText(texts, seen, node.stdout);
603
+ rememberText(texts, seen, node.stderr);
604
+ }
605
+ const rememberExit = (value) => {
606
+ if (Number.isInteger(value) && value !== 0) rememberText(texts, seen, `exit ${value}`);
607
+ };
608
+ rememberExit(node.exit_code);
609
+ const output = node.output;
610
+ if (Array.isArray(output)) {
611
+ for (const row of output) {
612
+ if (!row || typeof row !== "object") continue;
613
+ rememberExit(row.exit_code);
614
+ rememberExit(row.outcome?.exit_code);
615
+ }
616
+ } else if (output && typeof output === "object") {
617
+ rememberExit(output.exit_code);
618
+ rememberExit(output.outcome?.exit_code);
619
+ }
500
620
  return texts;
501
621
  }
502
622
 
623
+ const INLINE_IMAGE = /^data:image\/(?:png|jpeg|webp);base64,/i;
624
+
625
+ function functionOutputParts(output) {
626
+ if (typeof output === "string") return { text: output, images: [] };
627
+ const parts = [];
628
+ const images = [];
629
+ const push = (value) => {
630
+ if (typeof value === "string" && value.trim()) parts.push(value.trim());
631
+ };
632
+ const visit = (value) => {
633
+ if (typeof value === "string") {
634
+ push(value);
635
+ return;
636
+ }
637
+ if (Array.isArray(value)) {
638
+ for (const part of value) visit(part);
639
+ return;
640
+ }
641
+ if (!value || typeof value !== "object") return;
642
+ push(value.text);
643
+ push(value.stdout);
644
+ push(value.stderr);
645
+ const image = value.image_url;
646
+ const url =
647
+ typeof image === "string"
648
+ ? image
649
+ : image && typeof image === "object" && typeof image.url === "string"
650
+ ? image.url
651
+ : "";
652
+ if (url && INLINE_IMAGE.test(url)) {
653
+ const part = { type: "input_image", image_url: url };
654
+ const detail =
655
+ typeof value.detail === "string"
656
+ ? value.detail
657
+ : image && typeof image === "object" && typeof image.detail === "string"
658
+ ? image.detail
659
+ : "";
660
+ if (detail) part.detail = detail;
661
+ images.push(part);
662
+ push("[An image from this tool result follows.]");
663
+ } else if (url) {
664
+ push(url);
665
+ }
666
+ if (Array.isArray(value.content)) visit(value.content);
667
+ };
668
+ visit(output);
669
+ return { text: parts.join("\n"), images };
670
+ }
671
+
503
672
  function userInputTextMessage(texts) {
504
673
  return whitelistInputNode({
505
674
  type: "message",
@@ -510,11 +679,13 @@ function userInputTextMessage(texts) {
510
679
 
511
680
  function toProxyInputNode(node, map, state, salvage = false) {
512
681
  if (!node || typeof node !== "object") return node;
513
- if (Array.isArray(node)) {
682
+ if (Array.isArray(node)) {
514
683
  const items = [];
515
684
  for (const item of node) {
516
685
  const next = toProxyInputNode(item, map, state);
517
- if (next !== DROP) items.push(next);
686
+ if (next === DROP) continue;
687
+ if (Array.isArray(next)) items.push(...next);
688
+ else items.push(next);
518
689
  }
519
690
  return items;
520
691
  }
@@ -547,12 +718,19 @@ function toProxyInputNode(node, map, state, salvage = false) {
547
718
  if (next.type === "reasoning") {
548
719
  // Forward the summary and nothing else. Codex's own item id and null
549
720
  // content fields mean nothing to the upstream and only widen the surface
550
- // for a schema rejection.
551
- const summary = (Array.isArray(next.summary) ? next.summary : []).filter(
552
- (part) => part && typeof part.text === "string" && part.text.trim(),
553
- );
554
- if (!summary.length) return DROP;
555
- return whitelistInputNode({ type: "reasoning", summary });
721
+ // for a schema rejection. When the summary is empty, the plain text in
722
+ // content is the only readable reasoning left.
723
+ const readable = (parts) =>
724
+ (Array.isArray(parts) ? parts : []).filter(
725
+ (part) => part && typeof part.text === "string" && part.text.trim() && part.type !== "encrypted_content",
726
+ );
727
+ const summary = readable(next.summary);
728
+ const chosen = summary.length ? summary : readable(next.content);
729
+ if (!chosen.length) return DROP;
730
+ return whitelistInputNode({
731
+ type: "reasoning",
732
+ summary: chosen.map((part) => ({ type: "summary_text", text: part.text.trim() })),
733
+ });
556
734
  }
557
735
 
558
736
  if (next.type === "agent_message") {
@@ -606,8 +784,28 @@ function toProxyInputNode(node, map, state, salvage = false) {
606
784
  }
607
785
  }
608
786
 
787
+ // Codex sends a string or a list of content parts. Grok accepts the string.
788
+ // An inline image in that list is attached after the tool result so the
789
+ // model can see it; a remote URL stays as text and is not fetched.
790
+ let attachedImages = [];
791
+ if (next.type === "function_call_output" && typeof next.output !== "string") {
792
+ const parts = functionOutputParts(next.output);
793
+ next.output = parts.text;
794
+ attachedImages = parts.images;
795
+ }
796
+
609
797
  const whitelisted = whitelistInputNode(next);
610
- if (!salvage || isForwardedItem(whitelisted)) return whitelisted;
798
+ if (!salvage || isForwardedItem(whitelisted)) {
799
+ if (!attachedImages.length) return whitelisted;
800
+ return [
801
+ whitelisted,
802
+ whitelistInputNode({
803
+ type: "message",
804
+ role: "user",
805
+ content: attachedImages,
806
+ }),
807
+ ];
808
+ }
611
809
  // Grok rejects these Codex item types. Keep the readable text as a user
612
810
  // message, the same shape as a compaction summary. An item with nothing
613
811
  // left but an encrypted blob, image bytes, or ids is dropped.
@@ -629,15 +827,21 @@ function isForwardedItem(item) {
629
827
  function projectInput(input, map) {
630
828
  if (!Array.isArray(input)) {
631
829
  const items = toProxyInputNode(input, map, { callIds: new Map() }, true);
632
- return { items, pairs: [] };
830
+ return { items: Array.isArray(items) ? items : items === DROP ? [] : [items], pairs: [] };
633
831
  }
634
832
  const state = { callIds: new Map() };
635
833
  const pairs = [];
636
834
  for (const item of input) {
637
835
  const fingerprint = logicalFingerprint(item);
638
- const next = toProxyInputNode(item, map, state, true);
639
- if (!isForwardedItem(next)) continue;
640
- pairs.push({ fingerprint, item: next });
836
+ const produced = toProxyInputNode(item, map, state, true);
837
+ const list = Array.isArray(produced) ? produced : [produced];
838
+ list.forEach((next, index) => {
839
+ if (!isForwardedItem(next)) return;
840
+ pairs.push({
841
+ fingerprint: index === 0 ? fingerprint : logicalFingerprint(next),
842
+ item: next,
843
+ });
844
+ });
641
845
  }
642
846
  return { items: pairs.map((pair) => pair.item), pairs };
643
847
  }
@@ -677,8 +881,8 @@ export const IMAGE_GENERATION_PROVENANCE =
677
881
  export const VIDEO_GENERATION_PROVENANCE =
678
882
  "Videos: clips on this transport are generated by the grok_bridge_generate_video " +
679
883
  "function, which is already on the request. The bridge calls Grok's videos API " +
680
- "with the grok login and returns the video URL. Do not ask for an API key and " +
681
- "do not send the clip to another vendor.";
884
+ "with the grok login, saves the temporary file locally, and returns that path. " +
885
+ "Do not ask for an API key and do not send the clip to another vendor.";
682
886
 
683
887
  export function toProxyRequest(body) {
684
888
  const { tools, map } = flattenCodexTools(body.tools ?? []);
@@ -741,14 +945,47 @@ function restoreOriginName(node, origin) {
741
945
  else delete node.namespace;
742
946
  }
743
947
 
948
+ function originForResponseName(map, name) {
949
+ if (typeof name !== "string") return null;
950
+ if (map.has(name)) return map.get(name);
951
+ let found = null;
952
+ for (const origin of map.values()) {
953
+ if (origin.name !== name) continue;
954
+ if (found) return null;
955
+ found = origin;
956
+ }
957
+ return found;
958
+ }
959
+
960
+ // Grok ciphertext is not an OpenAI reasoning blob. If Codex stores it, a later
961
+ // switch to an OpenAI model fails with "could not be decrypted or parsed".
962
+ function stripEncryptedReasoning(node) {
963
+ if (!node || typeof node !== "object") return;
964
+ if (Array.isArray(node)) {
965
+ for (let i = node.length - 1; i >= 0; i -= 1) {
966
+ const part = node[i];
967
+ if (part && typeof part === "object" && part.type === "encrypted_content")
968
+ node.splice(i, 1);
969
+ else stripEncryptedReasoning(part);
970
+ }
971
+ return;
972
+ }
973
+ delete node.encrypted_content;
974
+ delete node.encrypted_function_args;
975
+ for (const value of Object.values(node)) stripEncryptedReasoning(value);
976
+ }
977
+
744
978
  function rewriteResponseItem(node, map, state) {
745
979
  if (!node || typeof node !== "object") return;
746
- if (typeof node.name === "string" && map.has(node.name)) {
747
- const origin = map.get(node.name);
980
+ const origin =
981
+ originForResponseName(map, node.name) ??
982
+ (typeof node.call_id === "string" ? state.callIds.get(node.call_id) : null);
983
+ if (origin) {
748
984
  rememberProxyItem(node, origin, state);
749
985
  restoreOriginName(node, origin);
750
986
  if (origin.kind === "custom") {
751
987
  if (node.type === "function_call") node.type = "custom_tool_call";
988
+ if (node.type === "function_call_output") node.type = "custom_tool_call_output";
752
989
  if (node.type === "custom_tool_call") {
753
990
  if (node.input == null) node.input = decodeCustomInput(node.arguments);
754
991
  delete node.arguments;
@@ -762,20 +999,34 @@ function rewriteResponseItem(node, map, state) {
762
999
  // and no place to put them, so the bridge writes the file and hands back an
763
1000
  // ordinary assistant message naming it.
764
1001
  function absorbGeneratedImage(item, state) {
1002
+ const key = typeof item.id === "string" ? item.id : "";
1003
+ if (key && state.savedImages?.has(key)) return state.savedImages.get(key);
765
1004
  const saved = saveGeneratedImage(item, state.imageOptions);
766
- return {
1005
+ const described = describeGeneratedImage(
1006
+ typeof item.prompt === "string" && item.prompt.trim()
1007
+ ? item
1008
+ : { ...item, prompt: item.revised_prompt },
1009
+ saved,
1010
+ );
1011
+ const message = {
767
1012
  type: "message",
768
- id: typeof item.id === "string" ? item.id : undefined,
1013
+ id: key || undefined,
769
1014
  role: "assistant",
770
1015
  status: "completed",
771
- content: [{ type: "output_text", text: describeGeneratedImage(item, saved) }],
1016
+ content: [{ type: "output_text", text: described }],
772
1017
  };
1018
+ if (key) {
1019
+ if (!state.savedImages) state.savedImages = new Map();
1020
+ state.savedImages.set(key, message);
1021
+ }
1022
+ return message;
773
1023
  }
774
1024
 
775
1025
  const USAGE_EVENTS = new Set(["response.completed", "response.incomplete"]);
776
1026
 
777
1027
  function rewriteResponseEvent(value, map, state) {
778
1028
  if (!value || typeof value !== "object") return value;
1029
+ stripEncryptedReasoning(value);
779
1030
  if (
780
1031
  USAGE_EVENTS.has(value.type) &&
781
1032
  value.response &&
@@ -785,6 +1036,24 @@ function rewriteResponseEvent(value, map, state) {
785
1036
  const cache = readCacheUsage(value.response.usage);
786
1037
  if (cache) state.cacheUsage = cache;
787
1038
  }
1039
+ if (Array.isArray(value.response?.output)) {
1040
+ for (let i = 0; i < value.response.output.length; i += 1) {
1041
+ const item = value.response.output[i];
1042
+ if (isImageGenerationItem(item))
1043
+ value.response.output[i] = absorbGeneratedImage(item, state);
1044
+ else rewriteResponseItem(item, map, state);
1045
+ }
1046
+ }
1047
+ if (
1048
+ value.type === "response.function_call_arguments.delta" ||
1049
+ value.type === "response.function_call_arguments.done"
1050
+ ) {
1051
+ const origin = originForResponseName(map, value.name);
1052
+ if (origin) {
1053
+ rememberProxyItem(value, origin, state);
1054
+ value.name = origin.name;
1055
+ }
1056
+ }
788
1057
  if (
789
1058
  typeof value.item === "object" &&
790
1059
  value.item &&
@@ -843,6 +1112,7 @@ export function createSseRewriter(map, options = {}) {
843
1112
  const state = {
844
1113
  callIds: new Map(),
845
1114
  itemIds: new Map(),
1115
+ savedImages: new Map(),
846
1116
  imageOptions: options.imageOptions,
847
1117
  cacheUsage: null,
848
1118
  };