Repository navigation
Conversation
…nversion Preserve non-function tool definitions in dto.ToolCallRequest without emitting an empty function object, and add CustomToolCallResponse to dto.ToolCallResponse so custom tool calls survive Chat Completions marshaling and force_format paths. Translate OpenAI Chat Completions custom tools (including nested grammar formats), custom tool_choice, and multi-turn custom_tool_call history into Responses requests. Return Responses custom_tool_call outputs as type "custom" with custom.name and custom.input across non-streaming, streaming, and buffered accumulator paths. Closes QuantumNous#8496
|
No actionable comments were generated in the recent review. WalkthroughThe changes add custom tool payload handling across DTOs and relay conversion. They preserve custom definitions and choices in request conversion, and map custom tool outputs and streamed input back to Chat Completions custom tool calls. ChangesCustom Tool Conversion
Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix · Severity of issue fixed: Medium Sequence Diagram(s)sequenceDiagram
participant Responses as OpenAI Responses
participant Converter as ResponsesResponseToChatCompletionsResponse
participant Chat as Chat Completions
Responses->>Converter: custom_tool_call with name, call ID, and input
Converter->>Chat: custom tool call with name, call ID, and input
Suggested reviewers: Merge Risk:
|
🔗 关联任务 / Related Issue
🚀 变更类型 / Type of change
📝 变更描述 / Description
修复 OpenAI 官方
type: "custom"工具在 Chat Completions 原生转发、Chat 与 Responses 双向转换以及响应回程中的协议损坏与字段丢失问题。处理的请求与响应类型
/v1/chat/completions(官方嵌套custom){"type":"custom","custom":{"name":"...","format":{...}}}"function":{"name":""}/v1/chat/completions(非标扁平custom/ 其他非function工具){"type":"custom","name":"...","format":{...}}name/format,不注入空function,也不改写结构chat_completions_to_responses_policy(官方嵌套custom工具与tool_choice)tools(含format.type="text"及嵌套format.grammar.{syntax,definition})与tool_choiceformat.grammar展平到format顶层,tool_choice转为{"type":"custom","name":"..."})chat_completions_to_responses_policy(多轮custom历史)assistant.tool_calls中type="custom"及其后同tool_call_id的role="tool"custom_tool_call与custom_tool_call_outputchat_completions_to_responses_policy(非标扁平custom入参)custom对象的扁平写法force_format与thinking_to_content)custom_tool_call(非流式 / 流式 SSE / 缓冲流)或直连 Chat 返回type="custom"{"id":"...","type":"custom","custom":{"name":"...","input":"..."}},不含function,完整保留input请求转换示例(OpenAI 官方 Chat 嵌套格式转 Responses 扁平格式)
改动前后的流转变化
flowchart LR A["Chat 请求: type=custom"] --> B["dto.ToolCallRequest"] B -->|"原生 /v1/chat/completions"| C["保留原始字段, 不再输出空 function"] B -->|"chat_completions_to_responses_policy (官方嵌套)"| D["toolconv + oai_chat"] D --> E["Responses 请求: 扁平 custom, 展开 format.grammar, custom_tool_call 历史"] E --> F["上游 /v1/responses 返回 custom_tool_call (input)"] F --> G["oai_responses 非流式 / 流式 / 缓冲流"] G --> H["Chat 响应: type=custom, custom.name 与 custom.input"]根因与改动文件
relaykit/dto/openai_request.goToolCallRequest.Function无omitempty,非function工具序列化必带"function":{"name":""};未知顶层字段反序列化时丢失function且Function为空时不输出function;保留含未知顶层字段的原始 JSON,不做改写relaykit/dto/openai_response.goToolCallResponse无Custom字段,开启force_format或thinking_to_content会剥离custom调用;ResponsesOutput无InputString()CustomToolCallResponse并在Custom非空时省略function;新增ResponsesOutput.InputString()relaykit/relayconvert/internal/toolconv/decode.gorelaykit/relayconvert/internal/toolconv/encode.gotype: "custom"工具与tool_choice在转换层被丢弃custom工具(含展平format.grammar)与custom的tool_choice,映射到 Responses 扁平结构relaykit/relayconvert/internal/oai_chat/to_oai_responses_req.gorelaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.gotype: "custom"的tool_calls,role: "tool"固定写成function_call_outputcustom_tool_call与custom_tool_call_output多轮历史,响应侧同步识别官方type: "custom"relaykit/relayconvert/internal/oai_responses/to_oai_chat_resp.gorelaykit/relayconvert/internal/oai_responses/to_oai_chat_stream_resp.gotype: "function",且非流式、终态补发与ResponsesBufferedAccumulator只读Arguments导致Input丢空type: "custom"与custom: {name, input};全路径改读并写回Inputrelaykit/relayconvert/internal/oai_responses/to_oai_chat_resp_test.goTestResponsesStreamEventToChatChunksCustomToolAndReasoning沿用旧降级断言,将纯文本patch body断言到Function.ArgumentsType == "custom"与Custom.Name/Custom.Input合并风险评估
relaykit/编解码与转换层改动,无数据库与状态迁移,回滚成本低)relaykit(普通function工具调用及/v1/responses经 Chat 转发链路不受影响)📸 运行证明 / Proof of Work
Before / After 对比
{"type":"custom","custom":{"name":"ApplyPatch","format":{"type":"text"}}}经ToolCallRequest序列化后变成{"type":"custom","function":{"name":""},"custom":{...}}。type: "custom"工具与多轮custom_tool_call历史。custom_tool_call(input: "*** Begin Patch")经ResponsesResponseToChatCompletionsResponse转换后变成{"type":"function","function":{"name":"ApplyPatch","arguments":""}},input丢失。function;扁平工具原样保留顶层name与format且不含function。tools(展平format.grammar)、tool_choice、custom_tool_call与custom_tool_call_output。{"id":"call_c1","type":"custom","custom":{"name":"ApplyPatch","input":"..."}},不含function。本地验证命令与结果
relaykit/子包编译与测试通过(含relaykit/dto、relaykit/relayconvert、internal/toolconv、internal/oai_chat、internal/oai_responses)。relay/channel/openai与service转换相关测试全部通过。force_format直连流式重编)逐项检查序列化 JSON,全部符合预期。✅ 提交前检查项 / Checklist
New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。go build通过或测试通过,不视为有效证明。Summary by CodeRabbit