feat(copilot): 新增 GitHub Copilot 中间层后端,泛化 N-tier 链式路由#4
Merged
Conversation
- base.py: 新增 PROXY_SKIP_HEADERS 常量,should_trigger_failover 从抽象方法改为基于 FailoverConfig 的默认实现 - anthropic.py: 删除重复的 failover 逻辑和 _SKIP_HEADERS,精简至 31 行 - zhipu.py: 删除 should_trigger_failover 覆写,终端后端由基类默认返回 False - tier.py: 新增 BackendTier 数据类,封装 backend + circuit_breaker + quota_guard 为路由单元 - router.py: 重写为 for-loop 链式路由,支持任意数量的后端层级 - quota_guard.py: 新增 window_hours 属性供基线加载使用 - app.py: 提取 _build_circuit_breaker/_build_quota_guard 辅助函数,tier 化装配 - cli.py: 适配新的 tiers 列表格式 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
- copilot.py: 实现 CopilotTokenManager(GitHub PAT → Copilot token 交换、缓存、主动/被动刷新)和 CopilotBackend(Anthropic 兼容端点转发) - schema.py: 新增 CopilotConfig、copilot_circuit_breaker、copilot_quota_guard 配置段;新增 model_validator 支持 anthropic/zhipu 旧字段向后兼容 - config.example.yaml: 补充 Copilot 后端配置示例 优先级链: Anthropic → GitHub Copilot → Zhipu/GLM 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
- test_tier.py: BackendTier 可用性判断、成功/失败记录、终端层属性(15 个用例) - test_router_chain.py: 链式故障转移、CB/QG 跳过、流式路由、构造与关闭(15 个用例) - test_copilot.py: token 交换/缓存/过期刷新/失效、请求头注入、failover 继承(9 个用例) - test_backends.py: 基类 failover 默认实现验证(2 个用例) - test_config_loader.py: 旧字段迁移、Copilot 配置加载与默认值(4 个用例) 总计 87 个测试全部通过 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.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.
概述
在 Claude Team Plan (Anthropic) 和 ZAI/GLM API (Zhipu) 之间,新增 GitHub Copilot Team Plan 作为中间层后端。同时将硬编码的 2-tier 路由泛化为 N-tier 链式责任链,消除代码重复,提升功能密度。
优先级链:
变更内容
1. 路由架构重构 (
e88ddbc)backends/base.py:should_trigger_failover从抽象方法改为基于FailoverConfig的默认实现;新增PROXY_SKIP_HEADERS常量backends/anthropic.py: 删除重复的 failover 逻辑和_SKIP_HEADERS,精简至 31 行backends/zhipu.py: 删除should_trigger_failover覆写,终端后端由基类默认返回Falserouting/tier.py(新增):BackendTier数据类,封装 backend + circuit_breaker + quota_guard 为路由单元routing/router.py: 重写为 for-loop 链式路由,支持任意数量后端层级server/app.py: 提取_build_circuit_breaker/_build_quota_guard辅助函数,tier 化装配2. Copilot 后端 (
aca9875)backends/copilot.py(新增):CopilotTokenManager: GitHub PAT → Copilot Token 交换、缓存、主动/被动刷新(asyncio.Lock 线程安全)CopilotBackend: Anthropic 兼容端点透传,401/403 自动标记 token 失效config/schema.py: 新增CopilotConfig、copilot_circuit_breaker、copilot_quota_guard;model_validator支持旧anthropic/zhipu字段向后兼容config.example.yaml: 补充 Copilot 配置示例3. 测试覆盖 (
29736ee)新增 45 个测试,总计 87 个测试全部通过:
test_tier.pytest_router_chain.pytest_copilot.pytest_backends.pytest_config_loader.py设计决策
FailoverConfig:Anthropic 和 Copilot 使用相同的故障转移判断规则测试验证
uv run pytest tests/ -v # 87 passed ✅🤖 Generated with Claude Code