Skip to content

Commit f63a3e7

Browse files
committed
fix(responses): bridge routed exec custom tools
1 parent 625a4f2 commit f63a3e7

7 files changed

Lines changed: 647 additions & 3 deletions

File tree

‎docs-site/src/content/docs/guides/codex-integration.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ Non-OpenAI catalog rows use `tool_mode: "code_mode_only"`. This lets Codex expos
200200
only the model's ordinary function call. Tool execution, permissions, and confirmations remain
201201
local to Codex; opencodex does not implement a second browser or desktop-control executor.
202202

203+
For key-auth Responses providers that do not accept Codex's `exec` custom-tool grammar, opencodex
204+
encodes that declaration and its history as an upstream function tool, then restores the streamed
205+
function-call lifecycle to `custom_tool_call` before Codex sees it. Native OpenAI forward routing
206+
and the supported `apply_patch` custom tool stay unchanged.
207+
203208
The selected provider must support function/tool calling. A text-only provider without tool-call
204209
support cannot use `exec`, Browser, or Computer Use. Native OpenAI rows keep their upstream tool
205210
mode unchanged.

‎src/adapters/openai-responses.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers";
1010
import { OCX_REASONING_PREFIX } from "../responses/reasoning-envelope";
1111
import { modelRecordValue } from "../reasoning-effort";
1212
import type { TranslatorBudget } from "../lib/translator-budget";
13+
import { rewriteRoutedCustomToolsForUpstream } from "../responses/custom-tool-compat";
1314

1415
// Headers relayed verbatim from the caller in OAuth-passthrough ("forward") mode.
1516
// Exported so the web-search sidecar reuses the exact same forwarded-auth set for its ChatGPT call.
@@ -1245,6 +1246,9 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
12451246
if (parsed._compactionRequest === true && !isCanonicalOpenAiForwardProvider(provider)) {
12461247
outBody = buildRoutedCompactionBody(outBody);
12471248
}
1249+
if (provider.authMode !== "forward") {
1250+
outBody = rewriteRoutedCustomToolsForUpstream(outBody).body;
1251+
}
12481252
const sanitizedBody = normalizeToolSchemas(stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody), { preserveRawReasoningContent: provider.preserveResponsesReasoningContent === true })))))));
12491253
const body = JSON.stringify(stripDisabledReasoningSummaries(
12501254
normalizeConfiguredReasoningSummaryDelivery(sanitizedBody, provider, parsed.modelId),
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
const ROUTED_CUSTOM_TOOL_PASSTHROUGH = new Set(["apply_patch"]);
2+
3+
function isPlainObject(value: unknown): value is Record<string, unknown> {
4+
return !!value && typeof value === "object" && !Array.isArray(value);
5+
}
6+
7+
function customToolInput(argumentsText: unknown): string {
8+
if (typeof argumentsText !== "string") return "";
9+
try {
10+
const parsed = JSON.parse(argumentsText) as unknown;
11+
if (isPlainObject(parsed) && typeof parsed.input === "string") return parsed.input;
12+
} catch { /* malformed arguments stay visible to the client */ }
13+
return argumentsText;
14+
}
15+
16+
export function customToolItemId(id: unknown): unknown {
17+
if (typeof id !== "string") return id;
18+
return id.startsWith("fc_") ? `ctc_${id.slice(3)}` : id;
19+
}
20+
21+
export function collectRoutedCustomToolNames(body: unknown): Set<string> {
22+
const names = new Set<string>();
23+
const visit = (value: unknown): void => {
24+
if (Array.isArray(value)) {
25+
for (const entry of value) visit(entry);
26+
return;
27+
}
28+
if (!isPlainObject(value)) return;
29+
if (
30+
value.type === "custom"
31+
&& typeof value.name === "string"
32+
&& !ROUTED_CUSTOM_TOOL_PASSTHROUGH.has(value.name)
33+
) {
34+
names.add(value.name);
35+
}
36+
for (const entry of Object.values(value)) visit(entry);
37+
};
38+
visit(body);
39+
return names;
40+
}
41+
42+
function collectConvertedCallIds(value: unknown, names: ReadonlySet<string>, out: Set<string>): void {
43+
if (Array.isArray(value)) {
44+
for (const entry of value) collectConvertedCallIds(entry, names, out);
45+
return;
46+
}
47+
if (!isPlainObject(value)) return;
48+
if (
49+
(value.type === "custom_tool_call" || value.type === "function_call")
50+
&& typeof value.name === "string"
51+
&& names.has(value.name)
52+
&& typeof value.call_id === "string"
53+
) {
54+
out.add(value.call_id);
55+
}
56+
for (const entry of Object.values(value)) collectConvertedCallIds(entry, names, out);
57+
}
58+
59+
function rewriteForUpstream(
60+
value: unknown,
61+
names: ReadonlySet<string>,
62+
callIds: ReadonlySet<string>,
63+
): unknown {
64+
if (Array.isArray(value)) return value.map(entry => rewriteForUpstream(entry, names, callIds));
65+
if (!isPlainObject(value)) return value;
66+
67+
if (value.type === "custom" && typeof value.name === "string" && names.has(value.name)) {
68+
const { format: _format, ...rest } = value;
69+
const isDefinition = typeof value.description === "string"
70+
|| isPlainObject(value.format)
71+
|| isPlainObject(value.parameters);
72+
if (!isDefinition) return { ...rest, type: "function" };
73+
return {
74+
...rest,
75+
type: "function",
76+
parameters: {
77+
type: "object",
78+
properties: {
79+
input: {
80+
type: "string",
81+
description: "Raw input for this client-executed custom tool.",
82+
},
83+
},
84+
required: ["input"],
85+
additionalProperties: false,
86+
},
87+
};
88+
}
89+
90+
if (
91+
value.type === "custom_tool_call"
92+
&& typeof value.name === "string"
93+
&& names.has(value.name)
94+
) {
95+
const { input, id: _id, ...rest } = value;
96+
return {
97+
...rest,
98+
type: "function_call",
99+
arguments: JSON.stringify({ input: typeof input === "string" ? input : "" }),
100+
};
101+
}
102+
103+
if (
104+
value.type === "custom_tool_call_output"
105+
&& typeof value.call_id === "string"
106+
&& callIds.has(value.call_id)
107+
) {
108+
return { ...value, type: "function_call_output" };
109+
}
110+
111+
let changed = false;
112+
const next: Record<string, unknown> = {};
113+
for (const [key, entry] of Object.entries(value)) {
114+
const rewritten = rewriteForUpstream(entry, names, callIds);
115+
next[key] = rewritten;
116+
changed ||= rewritten !== entry;
117+
}
118+
return changed ? next : value;
119+
}
120+
121+
export function rewriteRoutedCustomToolsForUpstream(body: unknown): {
122+
body: unknown;
123+
names: Set<string>;
124+
} {
125+
const names = collectRoutedCustomToolNames(body);
126+
if (names.size === 0) return { body, names };
127+
const callIds = new Set<string>();
128+
collectConvertedCallIds(body, names, callIds);
129+
return { body: rewriteForUpstream(body, names, callIds), names };
130+
}
131+
132+
export function restoreRoutedCustomCalls(
133+
value: unknown,
134+
names: ReadonlySet<string>,
135+
): { value: unknown; changed: boolean } {
136+
if (Array.isArray(value)) {
137+
let changed = false;
138+
const restored = value.map(entry => {
139+
const result = restoreRoutedCustomCalls(entry, names);
140+
changed ||= result.changed;
141+
return result.value;
142+
});
143+
return changed ? { value: restored, changed: true } : { value, changed: false };
144+
}
145+
if (!isPlainObject(value)) return { value, changed: false };
146+
147+
let changed = false;
148+
const restored: Record<string, unknown> = {};
149+
for (const [key, entry] of Object.entries(value)) {
150+
const result = restoreRoutedCustomCalls(entry, names);
151+
restored[key] = result.value;
152+
changed ||= result.changed;
153+
}
154+
155+
if (value.type === "function_call" && typeof value.name === "string" && names.has(value.name)) {
156+
restored.type = "custom_tool_call";
157+
restored.id = customToolItemId(value.id);
158+
restored.input = customToolInput(value.arguments);
159+
delete restored.arguments;
160+
changed = true;
161+
}
162+
return changed ? { value: restored, changed: true } : { value, changed: false };
163+
}
164+
165+
export function restoreRoutedCustomCallsInJson(
166+
text: string,
167+
names: ReadonlySet<string>,
168+
): string {
169+
if (names.size === 0) return text;
170+
let payload: unknown;
171+
try {
172+
payload = JSON.parse(text);
173+
} catch {
174+
return text;
175+
}
176+
const restored = restoreRoutedCustomCalls(payload, names);
177+
return restored.changed ? JSON.stringify(restored.value) : text;
178+
}
179+
180+
export function unwrapRoutedCustomToolArguments(argumentsText: unknown): string {
181+
return customToolInput(argumentsText);
182+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import type { TranslatorBudget } from "../lib/translator-budget";
2+
import {
3+
customToolItemId,
4+
restoreRoutedCustomCalls,
5+
unwrapRoutedCustomToolArguments,
6+
} from "../responses/custom-tool-compat";
7+
import {
8+
replaceSseDataPayload,
9+
sseDataPayload,
10+
type SseBlockRewrite,
11+
} from "./sse-payload-rewrite";
12+
13+
const FREEFORM_WRAP_PREFIX = '{"input":"';
14+
15+
function isPlainObject(value: unknown): value is Record<string, unknown> {
16+
return !!value && typeof value === "object" && !Array.isArray(value);
17+
}
18+
19+
function partialCustomToolInput(argumentsText: string): string {
20+
if (!argumentsText.startsWith(FREEFORM_WRAP_PREFIX)) return argumentsText;
21+
const body = argumentsText.slice(FREEFORM_WRAP_PREFIX.length);
22+
let output = "";
23+
for (let index = 0; index < body.length; index++) {
24+
const char = body[index];
25+
if (char === '"') break;
26+
if (char !== "\\") {
27+
output += char;
28+
continue;
29+
}
30+
const escaped = body[index + 1];
31+
if (escaped === undefined) break;
32+
index += 1;
33+
if (escaped === "n") output += "\n";
34+
else if (escaped === "t") output += "\t";
35+
else if (escaped === "r") output += "\r";
36+
else if (escaped === "u") {
37+
const hex = body.slice(index + 1, index + 5);
38+
if (hex.length !== 4 || !/^[0-9a-fA-F]{4}$/.test(hex)) break;
39+
output += String.fromCharCode(Number.parseInt(hex, 16));
40+
index += 4;
41+
} else output += escaped;
42+
}
43+
return output;
44+
}
45+
46+
function replaceSseEventName(block: string, type: string): string {
47+
const newline = block.includes("\r\n") ? "\r\n" : "\n";
48+
const lines = block.split(/\r?\n/);
49+
let replaced = false;
50+
const next = lines.map(line => {
51+
if (!replaced && line.startsWith("event:")) {
52+
replaced = true;
53+
return `event: ${type}`;
54+
}
55+
return line;
56+
});
57+
return next.join(newline);
58+
}
59+
60+
type OpenCustomCall = {
61+
argumentsText: string;
62+
emittedInput: string;
63+
retainedBytes: number;
64+
};
65+
66+
export function createRoutedCustomToolRestoreBlockRewrite(
67+
names: ReadonlySet<string>,
68+
budget?: TranslatorBudget,
69+
): SseBlockRewrite {
70+
const itemNames = new Map<string, string>();
71+
const openCalls = new Map<string, OpenCustomCall>();
72+
let disposed = false;
73+
74+
const releaseCall = (itemId: string): void => {
75+
const open = openCalls.get(itemId);
76+
if (!open) return;
77+
if (open.retainedBytes > 0) {
78+
budget?.releaseRetained(open.retainedBytes, { kind: "retained_collectors" });
79+
}
80+
openCalls.delete(itemId);
81+
};
82+
83+
const releaseAll = (): void => {
84+
if (disposed) return;
85+
disposed = true;
86+
for (const itemId of openCalls.keys()) releaseCall(itemId);
87+
itemNames.clear();
88+
};
89+
90+
const rewrite: SseBlockRewrite = (block: string): readonly string[] => {
91+
const payload = sseDataPayload(block);
92+
if (payload === null || payload === "[DONE]") return [block];
93+
let parsed: unknown;
94+
try {
95+
parsed = JSON.parse(payload);
96+
} catch {
97+
return [block];
98+
}
99+
if (!isPlainObject(parsed)) return [block];
100+
101+
const type = typeof parsed.type === "string" ? parsed.type : "";
102+
if (
103+
(type === "response.output_item.added" || type === "response.output_item.done")
104+
&& isPlainObject(parsed.item)
105+
&& parsed.item.type === "function_call"
106+
&& typeof parsed.item.name === "string"
107+
&& names.has(parsed.item.name)
108+
) {
109+
const upstreamItemId = typeof parsed.item.id === "string" ? parsed.item.id : undefined;
110+
if (upstreamItemId) {
111+
itemNames.set(upstreamItemId, parsed.item.name);
112+
if (type === "response.output_item.added") {
113+
openCalls.set(upstreamItemId, { argumentsText: "", emittedInput: "", retainedBytes: 0 });
114+
}
115+
}
116+
const restored = restoreRoutedCustomCalls(parsed, names);
117+
if (type === "response.output_item.done" && upstreamItemId) releaseCall(upstreamItemId);
118+
return restored.changed
119+
? [replaceSseDataPayload(block, JSON.stringify(restored.value))]
120+
: [block];
121+
}
122+
123+
const upstreamItemId = typeof parsed.item_id === "string" ? parsed.item_id : undefined;
124+
if (
125+
type === "response.function_call_arguments.delta"
126+
&& upstreamItemId
127+
&& itemNames.has(upstreamItemId)
128+
) {
129+
const open = openCalls.get(upstreamItemId) ?? { argumentsText: "", emittedInput: "", retainedBytes: 0 };
130+
const delta = typeof parsed.delta === "string" ? parsed.delta : "";
131+
const deltaBytes = Buffer.byteLength(delta, "utf8");
132+
if (deltaBytes > 0) budget?.chargeRetained(deltaBytes, { kind: "retained_collectors" });
133+
open.argumentsText += delta;
134+
open.retainedBytes += deltaBytes;
135+
openCalls.set(upstreamItemId, open);
136+
if (FREEFORM_WRAP_PREFIX.startsWith(open.argumentsText)) return [];
137+
const fullInput = partialCustomToolInput(open.argumentsText);
138+
if (!fullInput.startsWith(open.emittedInput) || fullInput.length === open.emittedInput.length) return [];
139+
const inputDelta = fullInput.slice(open.emittedInput.length);
140+
open.emittedInput = fullInput;
141+
const nextType = "response.custom_tool_call_input.delta";
142+
const next = {
143+
...parsed,
144+
type: nextType,
145+
item_id: customToolItemId(upstreamItemId),
146+
delta: inputDelta,
147+
};
148+
return [replaceSseDataPayload(replaceSseEventName(block, nextType), JSON.stringify(next))];
149+
}
150+
151+
if (
152+
type === "response.function_call_arguments.done"
153+
&& upstreamItemId
154+
&& itemNames.has(upstreamItemId)
155+
) {
156+
const nextType = "response.custom_tool_call_input.done";
157+
const source = typeof parsed.arguments === "string"
158+
? parsed.arguments
159+
: openCalls.get(upstreamItemId)?.argumentsText ?? "";
160+
const { arguments: _arguments, ...rest } = parsed;
161+
const next = {
162+
...rest,
163+
type: nextType,
164+
item_id: customToolItemId(upstreamItemId),
165+
input: unwrapRoutedCustomToolArguments(source),
166+
};
167+
return [replaceSseDataPayload(replaceSseEventName(block, nextType), JSON.stringify(next))];
168+
}
169+
170+
const restored = restoreRoutedCustomCalls(parsed, names);
171+
const terminal = type === "response.completed" || type === "response.failed" || type === "response.incomplete";
172+
if (terminal) releaseAll();
173+
return restored.changed
174+
? [replaceSseDataPayload(block, JSON.stringify(restored.value))]
175+
: [block];
176+
};
177+
rewrite.dispose = releaseAll;
178+
return rewrite;
179+
}

0 commit comments

Comments
 (0)