// API 错误分类系统 -- 一次分类,多处消费. // // 升华改进(ELEVATED): 早期方案有三套平行的 if-else 链(classifyAPIError / getAssistantMessageFromError / // categorizeRetryableAPIError)各自独立做字符串匹配,改一个忘另一个就是 bug. // 早期方案注释甚至承认 "Patterns MUST stay in sync"--需要注释来保证同步说明结构就是错的. // // 本设计:解析 HTTP 响应时一次性创建结构化 APIError(含 Category + RetryInfo + TokenGap + Hint), // 所有消费者(分析,用户消息,重试决策)直接读字段,不再各自做 string matching. // // 替代方案:<原方案三套独立分类器 + 字符串匹配> package api import ( "git.flytoex.net/yuanwei/flyto-agent/core/internal/apierror" ) // ============================================================ // ErrorCategory - 错误的语义分类枚举 // ============================================================ // ErrorCategory 是 API 错误的语义分类(使用 apierror 包的定义). type ErrorCategory = apierror.ErrorCategory // 以下常量与 apierror.Err* 对应,保持向后兼容. const ( ErrUnknown ErrorCategory = apierror.ErrUnknown ErrAborted ErrorCategory = apierror.ErrAborted ErrTimeout ErrorCategory = apierror.ErrTimeout ErrRateLimit ErrorCategory = apierror.ErrRateLimit ErrOverloaded ErrorCategory = apierror.ErrOverloaded ErrPromptTooLong ErrorCategory = apierror.ErrPromptTooLong ErrMediaTooLarge ErrorCategory = apierror.ErrMediaTooLarge ErrRequestTooLarge ErrorCategory = apierror.ErrRequestTooLarge ErrInvalidRequest ErrorCategory = apierror.ErrInvalidRequest ErrAuthentication ErrorCategory = apierror.ErrAuthentication ErrModelNotFound ErrorCategory = apierror.ErrModelNotFound ErrBilling ErrorCategory = apierror.ErrBilling ErrServerError ErrorCategory = apierror.ErrServerError ErrConnection ErrorCategory = apierror.ErrConnection ErrSSL ErrorCategory = apierror.ErrSSL ErrToolMismatch ErrorCategory = apierror.ErrToolMismatch ErrUnexpectedTool ErrorCategory = apierror.ErrUnexpectedTool ErrDuplicateToolID ErrorCategory = apierror.ErrDuplicateToolID ErrInvalidModel ErrorCategory = apierror.ErrInvalidModel ErrContentPolicy ErrorCategory = apierror.ErrContentPolicy ) // String 和 IsRetryableByDefault 方法继承自 apierror.ErrorCategory // ============================================================ // RetryInfo - 重试元信息 // ============================================================ // RetryInfo 封装重试建议(使用 apierror 包的定义). type RetryInfo = apierror.RetryInfo // ============================================================ // APIError - 结构化 API 错误(别名转发到 apierror 包) // ============================================================ // APIError is the structured API error. Its definition now lives in the // neutral apierror package so that both the wire path and the transport path // can build and consume one error type without a circular dependency; this // alias keeps the api.APIError surface stable for existing consumers. // // APIError 是结构化的 API 错误. 定义已移至中立的 apierror 包, 使 wire 路径 // 和 transport 路径都能构造并消费同一套错误类型而不产生循环依赖; 此别名为 // 既有消费者保留 api.APIError 符号不变. type APIError = apierror.APIError // ============================================================ // 错误体 / token / header 解析器(别名转发到 apierror 包) // ============================================================ // The parsers below moved into the apierror package alongside APIError. These // package-level function aliases keep the api.* names callable so existing // consumers (and this package's AnthropicClassifier) need no change. // // 下列解析器随 APIError 一同移入 apierror 包. 这些包级函数别名保留 api.* // 名称可调用, 使既有消费者 (以及本包的 AnthropicClassifier) 无需改动. var ( // ParseTokenGap 从错误消息中提取 prompt_too_long 的 token 溢出量. ParseTokenGap = apierror.ParseTokenGap // SanitizeErrorHTML 清理可能包含 HTML 的错误消息. SanitizeErrorHTML = apierror.SanitizeErrorHTML // ParseAPIErrorBody 从 API JSON 响应体中提取错误类型和消息. ParseAPIErrorBody = apierror.ParseAPIErrorBody // ParseRetryAfter 从 retry-after header 值解析等待时间. ParseRetryAfter = apierror.ParseRetryAfter // ParseShouldRetry 从 x-should-retry header 解析服务端重试建议. // 注: 随 parseRetryInfoFromHeaders 移入 apierror 包 (后者是 DefaultClassifier // 的依赖, 而 apierror 不能反向 import transport), 此处保留别名. ParseShouldRetry = apierror.ParseShouldRetry )