From 8a318860f55598310e5710dd1f8b16816c519fca Mon Sep 17 00:00:00 2001 From: Gan332 <269835883+Gan332@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:23:46 +0800 Subject: [PATCH] fix(protocol): bridge Responses custom_tool_call history Codex declares apply_patch as a Responses custom tool, so its replayed conversation history carries custom_tool_call and custom_tool_call_output items. The Responses -> Chat/Anthropic bridge only recognized function_call/function_call_output, so Codex auto-compaction resubmitting that history failed with: input[n] has unsupported Responses item type "custom_tool_call" Decode both item types. The freeform custom-tool input is wrapped as JSON arguments under an "input" key (Chat and Anthropic only carry JSON tool arguments), and the output maps to a tool_result keyed by call_id. --- internal/protocol/request.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/internal/protocol/request.go b/internal/protocol/request.go index 1cfb77f3..7c4eaf75 100644 --- a/internal/protocol/request.go +++ b/internal/protocol/request.go @@ -530,6 +530,23 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest CallID: jsonutil.StringAt(item, "call_id"), Result: item["output"], }) + case "custom_tool_call": + // Codex declares apply_patch as a Responses custom tool, so its + // replayed history carries custom_tool_call items. Chat and + // Anthropic only carry JSON tool arguments, so the freeform input + // is preserved under an "input" key. + appendBridgeBlock(&request.Messages, "assistant", bridgeBlock{ + Kind: "tool_call", + ID: jsonutil.FirstString(jsonutil.StringAt(item, "call_id"), jsonutil.StringAt(item, "id")), + Name: jsonutil.StringAt(item, "name"), + ArgumentsJSON: customToolCallArguments(item["input"]), + }) + case "custom_tool_call_output": + appendBridgeBlock(&request.Messages, "user", bridgeBlock{ + Kind: "tool_result", + CallID: jsonutil.StringAt(item, "call_id"), + Result: item["output"], + }) case "message", "": role := jsonutil.StringAt(item, "role") blocks, err := decodeOpenAIBlocksChecked(item["content"]) @@ -661,6 +678,21 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest return request, nil } +// customToolCallArguments encodes a Responses custom tool call's freeform input +// as JSON arguments. The Chat and Anthropic bridges only carry JSON tool +// arguments, so the original text is preserved under an "input" key and the +// turn stays meaningful when auto-compaction replays it. +func customToolCallArguments(input any) string { + if input == nil { + return "{}" + } + encoded, err := jsonutil.Marshal(map[string]any{"input": input}) + if err != nil { + return "{}" + } + return string(encoded) +} + // appendBridgeBlock adds blocks decoded from a standalone Responses item. It // joins adjacent assistant items into one turn, including interleaved text, // reasoning and tool calls. Tool results join only a preceding tool-only user