Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(codex): 按 item 类型改写 legacy call_ 工具项 id,跨 Agent 切模型后不再被 ctc/fc 前缀校验拒绝(#4023) (#4798)

FicoWang6ea32d461413
diff --git a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
index 62646211a5..8c2db19af9 100644
--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
@@ -3092,6 +3092,11 @@ describe('codex proxy host', () => {
["Invalid 'input[2].call_id': 'fc_old'. Expected an ID that begins with 'ctc'.", null],
["Invalid 'input[2].id': 'fc_old'. Expected an ID that begins with 'msg'.", null],
["Invalid 'input[2].id': 'unknown_old'. Expected an ID that begins with 'ctc'.", null],
+ // issue #4023: legacy call_ item ids are rewritten to whichever dialect the target asks for.
+ ["Invalid 'input[5].id': 'call_00_nRi4LX3KqCkex4kDJqBo0978'. Expected an ID that begins with 'ctc'.", 'tool_item_id'],
+ [JSON.stringify({ error: { message: "Invalid \"input[5].id\": \"call_fn\". Expected an ID that begins with \"fc\"." } }), 'tool_item_id'],
+ ["Invalid 'input[5].call_id': 'call_fn'. Expected an ID that begins with 'ctc'.", null],
+ ["Invalid 'input[5].id': 'CALL_fn'. Expected an ID that begins with 'ctc'.", null],
["Invalid 'input[2].id': 'FC_old'. Expected an ID that begins with 'ctc'.", null],
['invalid_value', null],
])('arms recovery only for repairable tool item errors: %s', async (message, expected) => {
diff --git a/apps/desktop/src/main/maker-host/codex-proxy-host.ts b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
index 64d00fe6d2..bfdeeb44b3 100644
--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
@@ -229,8 +229,12 @@ const CODEX_BODY_RECOVERY_RULES = [
function isRepairableToolItemIdError(errorText: string): boolean {
// Codex additionalDetails may contain an escaped JSON error inside another error envelope.
const text = errorText.replace(/\\+(?=["'])/g, '');
- const match = /Invalid\s+["']input\[\d+\]\.id["']:\s*["'](fc|fco|ctc|ctco)_[^"']+["']\.?\s+Expected an ID that begins with ["'](fc|fco|ctc|ctco)_?["']/i.exec(text);
+ const match = /Invalid\s+["']input\[\d+\]\.id["']:\s*["'](fc|fco|ctc|ctco|call)_[^"']+["']\.?\s+Expected an ID that begins with ["'](fc|fco|ctc|ctco)_?["']/i.exec(text);
if (!match) return false;
+ // Legacy `call_…` item ids (issue #4023) are rewritten to whichever tool dialect the target
+ // asks for, so any of the four expected prefixes is repairable. Case-sensitive like the
+ // normalizer's `startsWith('call_')`: an upper-case prefix would not be rewritten on retry.
+ if (match[1] === 'call') return true;
return ({ fc: 'ctc', fco: 'ctco', ctc: 'fc', ctco: 'fco' } as Record<string, string>)[match[1]!]
=== match[2]!;
}
diff --git a/packages/model-compat/src/cindy/custom-tool-function-adapter.ts b/packages/model-compat/src/cindy/custom-tool-function-adapter.ts
index 2a6cc995d0..e48b6f8596 100644
--- a/packages/model-compat/src/cindy/custom-tool-function-adapter.ts
+++ b/packages/model-compat/src/cindy/custom-tool-function-adapter.ts
@@ -80,6 +80,14 @@ function adaptItemId(item: Record<string, unknown>): Record<string, unknown> {
return next;
}
+/**
+ * Legacy histories (Pi / chat-completions bridges) minted the tool item `id` from the invocation's
+ * `call_...` id (issue #4023, `Invalid 'input[5].id': 'call_00_…'. Expected an ID that begins with 'ctc'`).
+ * It is a known, non-opaque shape, so it is rewritten to the target dialect like the other known
+ * prefixes instead of being forwarded and rejected. The suffix is kept, so ids stay unique.
+ */
+const LEGACY_CALL_ITEM_ID_PREFIX = 'call_';
+
/** Repair only known dialect mismatches; opaque provider ids and absent ids stay intact.
* `call_id` identifies the invocation/result pair and must never be rewritten.
*/
@@ -88,8 +96,11 @@ function normalizeToolItemId(item: Record<string, unknown>): Record<string, unkn
: item.type === 'custom_tool_call_output' ? ['fco_', 'ctco_']
: item.type === 'function_call' ? ['ctc_', 'fc_']
: item.type === 'function_call_output' ? ['ctco_', 'fco_'] : null;
- if (!prefixes || typeof item.id !== 'string' || !item.id.startsWith(prefixes[0])) return item;
- return { ...item, id: prefixes[1] + item.id.slice(prefixes[0].length) };
+ if (!prefixes || typeof item.id !== 'string') return item;
+ const from = item.id.startsWith(prefixes[0]) ? prefixes[0]
+ : item.id.startsWith(LEGACY_CALL_ITEM_ID_PREFIX) ? LEGACY_CALL_ITEM_ID_PREFIX : null;
+ if (from === null) return item;
+ return { ...item, id: prefixes[1] + item.id.slice(from.length) };
}
/**
diff --git a/packages/responses-chat-bridge/src/__tests__/custom-tool-function-adapter.test.ts b/packages/responses-chat-bridge/src/__tests__/custom-tool-function-adapter.test.ts
index 155e167992..c34a70fe31 100644
--- a/packages/responses-chat-bridge/src/__tests__/custom-tool-function-adapter.test.ts
+++ b/packages/responses-chat-bridge/src/__tests__/custom-tool-function-adapter.test.ts
@@ -61,6 +61,33 @@ describe("legacy Responses tool item ids", () => {
});
});
+describe("legacy call_ tool item ids (issue #4023)", () => {
+ it("rewrites call_ item ids to the dialect of the item type and keeps call_id pairing", () => {
+ const body = { input: [
+ { type: "custom_tool_call", id: "call_00_nRi4LX3KqCkex4kDJqBo0978", call_id: "call_00_nRi4LX3KqCkex4kDJqBo0978", name: "exec", input: "x" },
+ { type: "custom_tool_call_output", id: "call_00_nRi4LX3KqCkex4kDJqBo0978", call_id: "call_00_nRi4LX3KqCkex4kDJqBo0978", output: "ok" },
+ { type: "function_call", id: "call_fn", call_id: "call_fn", name: "read_file", arguments: "{}" },
+ { type: "function_call_output", id: "call_fn", call_id: "call_fn", output: "{}" },
+ // Not a tool item: a legacy-looking id elsewhere stays opaque.
+ { type: "message", id: "call_message", content: "unchanged" },
+ { type: "item_reference", id: "call_ref" },
+ ] };
+ const original = structuredClone(body);
+ const repaired = normalizeResponsesToolItemIds(body)!;
+ const items = repaired.input as typeof body.input;
+ expect(items.map(item => item.id)).toEqual([
+ "ctc_00_nRi4LX3KqCkex4kDJqBo0978", "ctco_00_nRi4LX3KqCkex4kDJqBo0978", "fc_fn", "fco_fn", "call_message", "call_ref",
+ ]);
+ expect(items.map(item => (item as { call_id?: string }).call_id)).toEqual(
+ original.input.map(item => (item as { call_id?: string }).call_id),
+ );
+ expect(items[0]!.input).toBe("x");
+ expect(items[1]!.output).toBe("ok");
+ expect(body).toEqual(original);
+ expect(normalizeResponsesToolItemIds(repaired)).toBeNull();
+ });
+});
+
describe("Responses custom-tool function adapter", () => {
afterEach(() => {
vi.useRealTimers();