feat: 错误覆写支持多格式 + 异步规则检测 + undici 超时优化#258
Conversation
- 支持 Claude/Gemini/OpenAI 三种错误响应格式的验证和覆写 - UI 模板选择:下拉菜单替代单一按钮,可选择不同格式模板 - 简化覆写逻辑:只输出用户配置的字段,不额外注入 request_id - message 为空时自动回退到原始错误消息 - 新增异步版本函数,确保规则在检测前已从数据库加载 - 解决冷启动时规则未初始化导致检测失败的问题 - 保留同步版本向后兼容,供测试使用 - 数据库异常保护:失败时不抛异常,保留现有缓存 - 编辑默认规则时自动转换为自定义规则(isDefault: false) - 防止用户配置的覆写被"同步规则"操作覆盖 - 用户自定义规则(priority=0)优先于默认规则匹配 - 全局 Agent 配置 600 秒超时,覆盖默认 300 秒限制 - HTTP/HTTPS ProxyAgent 同步配置,匹配 LLM 最大响应时间 - 解决长时间请求被 undici 默认超时提前中断的问题 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Summary of ChangesHello @sususu98, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求全面增强了系统的错误处理机制和代理功能。它通过支持多种主流 API 错误格式,提升了错误覆写功能的灵活性和兼容性;通过引入异步规则检测,解决了规则在冷启动时可能未加载的问题,确保了错误处理的可靠性。同时,优化了 undici 的全局和代理超时配置,以适应 LLM 服务的长时间响应,并改进了默认错误规则的编辑体验,使其更加符合用户预期。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
本次 PR 提交了数个重要的功能增强和重构,包括:
- 错误覆写多格式支持:为 Claude, Gemini, OpenAI 提供了独立的错误响应模板和验证,实现非常健壮。
- 异步规则检测:将错误规则的加载和检测重构为异步操作,有效解决了冷启动时的竞态条件问题,提升了系统稳定性。
- Undici 超时优化:通过全局和局部配置,精确地解决了
undici的默认超时问题,对长时运行的 LLM 请求是关键优化。 - 数据库异常保护:
ErrorRuleDetector现在能优雅地处理数据库异常,增强了系统的容错能力。
代码整体质量很高,逻辑清晰,注释详尽。异步重构和新的验证逻辑都考虑得非常周到。
我在代码中发现两处由于重构导致的不一致性,主要涉及 request_id 的处理逻辑,已在具体的 review comments 中提出修改建议。修正这些小问题后,代码将更加完善。
整体而言,这是一次出色的工作!
| * | ||
| * 若缓存已命中,直接返回结果; | ||
| * 若缓存尚未初始化,会立即返回当前同步检测结果, | ||
| * 并在后台触发一次 detectErrorRuleOnceAsync 以完成加载并填充缓存。 |
There was a problem hiding this comment.
Medium Potential race condition in error detection caching
Why this is a problem: When errorRuleDetector is not initialized and detectErrorRuleOnce is called, the code triggers an async initialization but returns a synchronous result that isn't cached. If multiple concurrent requests hit this code path before initialization completes, they will all execute errorRuleDetector.detect(content) without caching, potentially leading to inconsistent behavior. Additionally, the .catch(() => undefined) silently swallows errors from the async detection, making debugging difficult.
Suggested fix:
function detectErrorRuleOnce(error: Error): ErrorDetectionResult {
const cached = errorDetectionCache.get(error);
if (cached) {
return cached;
}
const content = extractErrorContentForDetection(error);
// Always use sync detection and cache the result
// The async initialization is handled internally by the detector
const result = errorRuleDetector.detect(content);
// Only cache if the detector has been initialized to avoid caching incomplete results
if (errorRuleDetector.hasInitialized()) {
errorDetectionCache.set(error, result);
}
return result;
}
ding113
left a comment
There was a problem hiding this comment.
Code Review Summary
This PR enhances the error override feature with multi-format support (Claude/OpenAI/Codex) and fixes cold-start issues by migrating to async rule detection. The changes also include undici timeout optimization and HTTP/2 improvements. The code quality is generally good with proper validation and error handling.
PR Size: L
- Lines changed: ~53,423 (mostly from litellm-prices.json update)
- Files changed: 10 (9 code files + 1 data file)
Note: The actual code changes are ~400 lines across 9 TypeScript files. The large line count is dominated by the litellm-prices.json update. Consider separating data file updates from feature changes in future PRs for easier review.
Issues Found
| Category | Critical | High | Medium | Low |
|---|---|---|---|---|
| Code Quality | 0 | 0 | 1 | 0 |
| Security | 0 | 0 | 0 | 0 |
| Dependencies | 0 | 0 | 0 | 0 |
| Documentation | 0 | 0 | 0 | 0 |
Priority Actions
- Review the race condition concern in
detectErrorRuleOnce- the sync fallback path may cache incomplete results during concurrent cold-start scenarios
Review Coverage
- Code quality and correctness
- Security (OWASP Top 10)
- PR size assessment
- Dependency changes - Clean (no package.json changes)
- Documentation changes - N/A
Additional Notes
The architectural change from synchronous to asynchronous error detection is well-implemented:
detectErrorRuleOnceAsyncensures rules are loaded before detection- WeakMap caching prevents repeated detection for the same Error object
ensureInitialized()is now public for explicit initialization when needed
The multi-format error response support follows good practices:
- Format detection is done before validation
- Each format (Claude/Gemini/OpenAI) has its own validator
- Template UI provides clear format options
Automated review by Claude AI
ding113
left a comment
There was a problem hiding this comment.
Code Review Summary
This PR enhances the error override feature with multi-format support (Claude/OpenAI/Codex) and fixes cold-start issues by migrating to async rule detection. The changes also include undici timeout optimization and HTTP/2 improvements. The code quality is generally good with proper validation and error handling.
PR Size: L
- Lines changed: ~53,423 (mostly from litellm-prices.json update)
- Files changed: 10 (9 code files + 1 data file)
Note: The actual code changes are ~400 lines across 9 TypeScript files. The large line count is dominated by the litellm-prices.json update. Consider separating data file updates from feature changes in future PRs for easier review.
Issues Found
| Category | Critical | High | Medium | Low |
|---|---|---|---|---|
| Code Quality | 0 | 0 | 1 | 0 |
| Security | 0 | 0 | 0 | 0 |
| Dependencies | 0 | 0 | 0 | 0 |
| Documentation | 0 | 0 | 0 | 0 |
Priority Actions
- Review the race condition concern in
detectErrorRuleOnce- the sync fallback path may cache incomplete results during concurrent cold-start scenarios
Review Coverage
- Code quality and correctness
- Security (OWASP Top 10)
- PR size assessment
- Dependency changes - Clean (no package.json changes)
- Documentation changes - N/A
Additional Notes
The architectural change from synchronous to asynchronous error detection is well-implemented:
detectErrorRuleOnceAsyncensures rules are loaded before detection- WeakMap caching prevents repeated detection for the same Error object
ensureInitialized()is now public for explicit initialization when needed
The multi-format error response support follows good practices:
- Format detection is done before validation
- Each format (Claude/Gemini/OpenAI) has its own validator
- Template UI provides clear format options
Automated review by Claude AI
- Gemini 透传立即清除首字节超时:收到 HTTP 响应即视为首字节到达 - undici.request 显式配置超时:解决使用代理时超时失效问题 - 添加 UNDICI_REQUEST_TIMEOUT_MS 常量统一管理超时时间 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
…-enhancement # Conflicts: # public/seed/litellm-prices.json
Summary
ErrorOverrideResponse现在支持 Claude/OpenAI/Codex 三种格式,自动根据客户端请求格式返回对应结构detectAsync()和categorizeErrorAsync()方法,确保错误规则在检测前已从数据库加载完成allowH2) 和自定义超时配置Changes
错误覆写增强
src/lib/error-override-validator.ts: 支持验证 Claude/OpenAI/Codex 三种响应格式src/lib/error-rule-detector.ts: 新增detectAsync()异步检测方法src/app/v1/_lib/proxy/errors.ts: 新增categorizeErrorAsync()异步分类函数src/app/v1/_lib/proxy/error-handler.ts: 使用异步检测确保规则已加载代理超时优化
src/lib/proxy-agent.ts: 全局 undici dispatcher 配置 600s 超时,ProxyAgent 同时支持 HTTP/2 和超时配置UI 优化
src/app/[locale]/settings/error-rules/_components/override-section.tsx: 优化覆写响应配置界面Test plan
🤖 Generated with Claude Code