Repository navigation
fix: 错误信息中显示 OpenRouter 转述的上游错误 - #125
Open
sheepbox8646 wants to merge 1 commit into
Open
sheepbox8646 wants to merge 1 commit into
sheepbox8646 wants to merge 1 commit into
Conversation
OpenRouter 在上游服务商报错时只返回 "Provider returned error",真正原因在 metadata.raw,服务商在 metadata.provider_name。新增 AddOpenRouterUpstream,兼容 error.metadata 与顶层 metadata,以及 JSON 对象、数组、SSE 和纯文本形式的 raw,把服务商名称与截断后的上游 message 追加到 Message。DecodeOpenAI、DecodeOpenAIErrorEvent、DecodeOpenRouter 和 Anthropic Messages 的 decodeError 都调用它。 APIError.Error() 在 code 只是重复 HTTP 状态码时不再重复输出(如 "422 422")。 Linear: TWI-13 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
上游服务商报错时,OpenRouter 只返回
Provider returned error这类通用 message。真正原因在metadata.raw,服务商在metadata.provider_name。Twilight 原来只显示通用 message,排查时必须另外抓包。errorformat.AddOpenRouterUpstream:它读取 OpenRouter 的 metadata,把服务商名称和上游 message 追加到APIError.Message,例如Provider returned error (DeepInfra: Input should be 'text')。error.metadata,Responses 与 Anthropic Messages 放在顶层metadata,两种都兼容。带顶层object字段的 Responses 响应对象不读,因为那里的 metadata 是调用方自己的。raw形式:JSON 字符串或 JSON 值;字符串内容依次按 JSON 对象、JSON 数组、SSEdata:行、纯文本解析。HTML 页面忽略。DecodeOpenAI(Chat Completions、Responses、embedding、images 等)、Responses 的error流事件、DecodeOpenRouter(provider/openrouter/*),以及 Anthropic Messages 的decodeError。只改DecodeOpenRouter不够,因为 completions、responses 和 anthropic 指向 OpenRouter 时都不经过它。APIError.Error()在 code 只是重复 HTTP 状态码时不再输出它,422 422: ...变成422: ...。Kind、Type、Code、StatusCode都不变。关联
json_schema包装问题。验证
internal/errorformat/openrouter_upstream_test.go,并补充 Anthropic 与sdk.APIError的用例。detail、数组拼接、超长截断、Responseserror事件,以及各种缺失或无法解析的情况。go build ./...、go vet ./...、go vet -tags integration ./...、go test ./... -short -count=1 -race、golangci-lint run ./...,全部通过。go mod tidy无变化。422 422: Provider returned error422: Provider returned error (DeepInfra: Input should be 'text')400 400: Provider returned error400: Provider returned error (Alibaba: <400> InternalError.Algo.InvalidParameter: Format error : ...)429 429: Provider returned error429: Provider returned error (Alibaba: qwen/qwen3.8-flash is temporarily rate-limited upstream. ...)400 invalid_prompt: Invalid Responses API request400 invalid_prompt: Invalid Responses API request (Too big: expected number to be <=2)400 400: Provider returned error400: Provider returned error (Alibaba: <400> ... is not valid under any of the given schemas.)400 invalid_request_error: <400> ... in thinking mode400 invalid_request_error: <400> ... in thinking mode (Alibaba)未验证
error流事件没有抓到真实样本,只有构造样本的单元测试。APIError,不在本 PR 范围内。🤖 Generated with Claude Code