Skip to content

响应重建

当上游 Provider 返回 Chat Completions 响应后,GodeX 必须将其重建为 OpenAI Responses API ResponseObject 的格式。这是请求构建的逆过程——工具调用必须还原为原始类型,完成原因必须映射到 Responses 状态模型,推理文本必须被提取,输出契约必须被验证。reconstructResponseObject 函数负责整个转换过程。

概览

步骤函数用途
1firstChoice 提取从 Provider 响应中获取第一个 choice
2outputText 提取从 choice 消息中读取文本内容
3validateOutputContractrequiresValidJson 时,解析并验证输出
4mapProviderFinishReason将 Provider 完成原因映射到 Responses 状态
5restoreToolCall(每个调用)将每个工具调用还原为其原始 Responses 类型
6推理文本提取从 choice 消息中提取 reasoning_content
7组装 ResponseObject将所有部分组合为最终响应

完成原因映射

各 Provider 使用不同的 finish_reason 字符串。mapProviderFinishReason 函数将这些映射到 Responses API 的终端状态(completedincompletefailed):

Provider finish_reasonResponses statusincomplete_details.reasonerror
stopcompletednullnull
tool_callscompletednullnull
lengthincomplete"max_output_tokens"null
model_context_window_exceededincomplete"max_output_tokens"null
content_filterincomplete"content_filter"null
sensitiveincomplete"content_filter"null
network_errorfailednull{ code: SERVER_ERROR, message }
null / undefinedfailednull"Provider returned no finish reason"
其他任何值failednull"Unexpected finish reason"

重建序列

工具调用还原

restoreToolCall 函数使用 ToolIdentityMap 将 Provider 端的工具名称反向映射回原始 Responses 类型。每个 Provider 函数调用携带 (callId, name, arguments)。身份映射提供 requestedType,决定重建路径:

requestedType重建方式回退
function{ type: "function_call", call_id, name, arguments }始终成功
shellarguments 解析为 JSON;提取 commands 数组回退为 function_call
local_shellarguments 解析为 JSON;提取 command 数组 + env回退为 function_call
apply_patcharguments 解析为 JSON;提取 operation 对象回退为 function_call
customarguments 解析为 JSON;提取 input 字符串回退为 function_call

当解析失败(格式错误的 JSON、缺失字段)时,restoreToolCall 会回退到使用身份映射中 requestedName 的通用 function_call ResponseItem。这确保了即使 Provider 的输出出乎意料,响应也始终有效。

工具身份映射

ToolIdentityMap 在请求构建期间创建,携带请求的工具名称/类型与 Provider 工具名称/类型之间的双向映射。在重建阶段,它被反向使用:

请求构建方向重建方向
requestedName -> providerNameproviderName -> requestedName
requestedType -> providerTypeproviderName -> requestedType

该映射强制唯一性:如果两个不同的工具映射到同一个 Provider 名称,请求构建时会抛出 BridgeError

输出契约验证

当输出契约被降级(例如 json_schema 降为 json_objectstrict: true)时,requiresValidJson 被设置。重建完成后,validateOutputContract 将输出文本解析为 JSON:

  • 通过:响应是有效的 JSON;ResponseObject 正常返回。
  • 失败:抛出 BridgeFailure,错误代码为 BRIDGE_RESPONSE_INVALID_OUTPUT_FORMAT,元数据中包含 Provider、模型和响应 ID。

ResponseObject 组装

最终的 ResponseObject 由所有收集的部分组装而成:

字段来源
id从请求身份生成的 responseId
object始终为 "response"
created_at上下文创建时的时间戳
completed_at重建时的当前时间戳
status来自 mapProviderFinishReason
model已解析的模型名称
outputResponseItem 数组:推理、工具调用、助手消息
output_text提取的文本内容
usage来自 accessor.usage()
error来自完成原因映射(completed 时为 null)
incomplete_details来自完成原因映射(completed 时为 null)

output 数组的顺序为:推理项在前,然后是还原的工具调用,最后是助手消息(当有文本或不存在工具调用时)。

交叉引用

  • 架构概览:重建在完整请求生命周期中的位置
  • 兼容性:输出契约如何被规划(包括 requiresValidJson
  • 请求构建:工具身份和输出契约如何建立

参考