Skip to content

跨协议转换健壮性:delta.reasoning 不识别、上游错误被映射为正常结束/断流、流式 redacted_thinking 丢失 #11

Closed

Description

@coulsontl

复核协议转换代码时发现的几个健壮性问题(与用量相关的另见 #9、#10):

1. 流式解析不识别 delta.reasoning,思维链丢失

非流式解码 decodeChatReasoning 兼容 reasoning_content 和 reasoning 两个字段名:

https://github.com/jasonxu114514/opencode2api/blob/main/convert.go#L883-L886

但流式解析 parseChat 只读 reasoning_content:

https://github.com/jasonxu114514/opencode2api/blob/main/stream.go#L218

if reasoning := stringAt(delta, "reasoning_content"); reasoning != "" {

OpenRouter 等聚合类上游流式输出使用 delta.reasoning。请求方向和响应方向的字段兼容性不对称,导致这类上游经网关跨协议转换时思维链整体丢失。

2. 上游失败被映射为"正常结束"

Stop = "error" 只会来自 Responses 协议上游(非流式 convert.go:1205,流式 stream.go:393),但它没有合法的出口:

  • Chat 目标:chatStop("error") 显式返回字面量 "error"(convert.go:1414),而 OpenAI 的 finish_reason 合法枚举只有 stop/length/tool_calls/content_filter/function_call,客户端解析会异常;
  • Anthropic 目标:anthropicStop("error") 落入 default 返回 "end_turn"——上游明明失败了,Claude 客户端却看到一次正常的自然收尾,无法感知错误并重试。

建议:"error" 在两个出口分别映射为可表达的形态(如 Chat 用 content_filter 不合适的话,可在响应体附 error 字段;Anthropic 至少不应是 end_turn),或直接走错误响应路径。

3. 流式中途出错时不向下游发结构化错误事件

Anthropic 上游中途发 event: error 时 parseAnthropic 返回错误:

https://github.com/jasonxu114514/opencode2api/blob/main/stream.go#L316-L318

而 gateway 侧的处理只是记 Debug 日志后 return:

https://github.com/jasonxu114514/opencode2api/blob/main/gateway.go#L307-L310

此时 HTTP 200 和 SSE 头都已发出(gateway.go:293),下游不会收到任何结构化错误事件,表现为连接被静默掐断。建议按目标协议补发一条错误事件再结束(Anthropic 目标发 event: error;Chat 目标发 data: {"error": {...}}),让客户端有机会重试。

4. 流式 content_block_start 未处理 redacted_thinking

https://github.com/jasonxu114514/opencode2api/blob/main/stream.go#L271-L290 的 switch 只处理 thinking/tool_use/text 三种类型,redacted_thinking 块被静默丢弃。而非流式 decodeAnthropicBlocks 已处理该类型(convert.go:852-853),行为不对称——Anthropic 上游 → 其他协议客户端且命中加密思维块时,历史重放会缺块。


另附几个低优先级观察项(P3,供参考)

  • decodeOpenAIBlocks 不支持 input_audio / 文件类 part,decodeAnthropicBlocks 不支持 document,均静默丢弃;
  • Responses→Chat 编码可能产出 role: "developer" 消息(convert.go:492 附近),部分只认老版角色枚举的上游会拒绝;
  • mergeToolName(stream.go:413)的前缀启发式在工具名分片非单调前缀扩展时可能拼错,无兜底校验;
  • 思考预算映射上限 8192(convert.go:1473-1481):Claude 客户端 budget_tokens: 32000 经 Chat 中转会被压到 high/8192。若是有意保守设计可忽略。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions