Skip to content

feat: 错误覆写支持多格式 + 异步规则检测 + undici 超时优化#258

Merged
ding113 merged 6 commits intoding113:devfrom
sususu98:feat/error-override-enhancement
Dec 5, 2025
Merged

feat: 错误覆写支持多格式 + 异步规则检测 + undici 超时优化#258
ding113 merged 6 commits intoding113:devfrom
sususu98:feat/error-override-enhancement

Conversation

@sususu98
Copy link
Contributor

@sususu98 sususu98 commented Dec 3, 2025

Summary

  • 错误覆写支持多格式响应ErrorOverrideResponse 现在支持 Claude/OpenAI/Codex 三种格式,自动根据客户端请求格式返回对应结构
  • 异步规则检测:新增 detectAsync()categorizeErrorAsync() 方法,确保错误规则在检测前已从数据库加载完成
  • undici 超时优化:HTTP/HTTPS 代理配置 600 秒超时,覆盖 undici 默认的 300 秒限制,匹配 LLM 最大响应时间
  • HTTP/2 + 超时合并:ProxyAgent 同时支持 HTTP/2 (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

  • 验证错误规则覆写功能正常工作(Claude/OpenAI/Codex 格式)
  • 验证异步规则检测在新 worker 进程中正确加载
  • 验证 HTTP/HTTPS 代理超时配置生效
  • 验证 HTTP/2 支持与超时配置兼容

🤖 Generated with Claude Code

sususu98 and others added 2 commits December 3, 2025 09:25
- 支持 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>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • 错误覆写支持多格式: 错误覆写响应现在支持 Claude、Gemini 和 OpenAI 三种 API 错误格式,系统会根据客户端请求自动返回匹配的结构。UI 界面也更新了模板选择功能。
  • 异步错误规则检测: 引入了异步版本的错误规则检测方法(如 detectAsync()categorizeErrorAsync()),确保在进行错误分类和覆写前,规则已从数据库完全加载,解决了新 worker 进程冷启动时规则未初始化的问题。
  • undici 超时优化: 将 undici 的全局和 ProxyAgent 的 HTTP/HTTPS 代理超时时间从默认的 300 秒延长至 600 秒(10 分钟),以更好地匹配大型语言模型(LLM)服务的最大响应时间,避免因超时过早中断请求。
  • 默认错误规则编辑行为: 当用户编辑一条默认错误规则时,该规则将自动转换为自定义规则,避免用户修改被后续的“同步规则”操作覆盖,提升了用户体验和数据持久性。
  • 代理支持 HTTP/2: ProxyAgent 现在同时支持 HTTP/2 (allowH2) 和自定义超时配置,增强了代理的灵活性和性能。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ding113 ding113 added enhancement New feature or request size/XL Extra Large PR (> 1000 lines) labels Dec 3, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 提交了数个重要的功能增强和重构,包括:

  • 错误覆写多格式支持:为 Claude, Gemini, OpenAI 提供了独立的错误响应模板和验证,实现非常健壮。
  • 异步规则检测:将错误规则的加载和检测重构为异步操作,有效解决了冷启动时的竞态条件问题,提升了系统稳定性。
  • Undici 超时优化:通过全局和局部配置,精确地解决了 undici 的默认超时问题,对长时运行的 LLM 请求是关键优化。
  • 数据库异常保护ErrorRuleDetector 现在能优雅地处理数据库异常,增强了系统的容错能力。

代码整体质量很高,逻辑清晰,注释详尽。异步重构和新的验证逻辑都考虑得非常周到。

我在代码中发现两处由于重构导致的不一致性,主要涉及 request_id 的处理逻辑,已在具体的 review comments 中提出修改建议。修正这些小问题后,代码将更加完善。

整体而言,这是一次出色的工作!

@ding113 ding113 added the size/L Large PR (< 1000 lines) label Dec 3, 2025
*
* 若缓存已命中,直接返回结果;
* 若缓存尚未初始化,会立即返回当前同步检测结果,
* 并在后台触发一次 detectErrorRuleOnceAsync 以完成加载并填充缓存。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Owner

@ding113 ding113 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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:

  • detectErrorRuleOnceAsync ensures 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

Copy link
Owner

@ding113 ding113 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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:

  • detectErrorRuleOnceAsync ensures 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

sususu98 and others added 4 commits December 4, 2025 11:25
- 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
@ding113 ding113 merged commit 756ec22 into ding113:dev Dec 5, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size/L Large PR (< 1000 lines) size/XL Extra Large PR (> 1000 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants

Comments