Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
dc5a92d
docs(01-database): create phase plan
Jun 11, 2026
a607f3f
docs(01-database): update STATE and ROADMAP after planning
Jun 11, 2026
3224efa
test(01-database): add failing test for schema v10->v11 migration and…
Jun 11, 2026
0937b19
feat(01-database): implement schema v10->v11 migration for model_rout…
Jun 11, 2026
f491668
feat(01-database): implement model_routes DAO with full CRUD operations
Jun 11, 2026
2d372cf
test(01-database): add full integration tests for model_routes
Jun 11, 2026
b261cb7
docs(01-database): complete database plan execution
Jun 11, 2026
a14a7a1
docs(02-router): create phase plan
Jun 11, 2026
d23de7f
docs(02-router): update ROADMAP with Phase 2 plan reference
Jun 11, 2026
925c74a
docs(02-router): update STATE.md for Phase 2 planned
Jun 11, 2026
697370b
feat(02-router): create ModelRouter engine with wildcard matching
Jun 11, 2026
ac90391
feat(02-router): integrate ModelRouter into proxy pipeline
Jun 12, 2026
52151e0
test(02-router): add integration tests and formatting fixes
Jun 12, 2026
c6ee75a
docs(02-router): complete model router engine + proxy integration plan
Jun 12, 2026
7ebbd2f
docs(03-cli): create phase 3 plan for CLI model-route commands
Jun 12, 2026
01b600c
feat(03-cli): add ModelRouteCommand enum and integrate into ProxyCommand
Jun 12, 2026
dc58a71
test(03-cli): add failing model-route command tests (RED)
Jun 12, 2026
771b9a7
feat(03-cli): implement model-route command handlers (GREEN)
Jun 12, 2026
204a953
refactor(03-cli): apply cargo fmt formatting fixes
Jun 12, 2026
7004192
docs(03-cli): complete CLI model-route commands plan
Jun 12, 2026
6e527ff
docs(04-tui): create phase 4 TUI plan (2 plans, 2 waves)
Jun 12, 2026
95bc766
feat(04-tui-interface-01): add model routes data types, route variant…
Jun 12, 2026
08fe065
feat(04-tui-interface-01): wire navigation and content-key dispatch f…
Jun 12, 2026
ba48e10
docs(04-tui-interface-01): complete model routes TUI scaffolding plan
Jun 12, 2026
7954563
feat(04-tui-interface): add ModelRoute Action variants, runtime handl…
Jun 12, 2026
2fa3f02
feat(04-tui-interface): wire model routes CRUD keyboard handlers and …
Jun 12, 2026
cd45d94
fix(04-tui-interface): add ModelRouteSnapshot import and field to UiD…
Jun 12, 2026
73ff8cf
style(04-tui-interface): apply cargo fmt formatting
Jun 12, 2026
45169c5
docs(04-tui-interface): complete model routes CRUD plan
Jun 12, 2026
8e04310
docs(06-testing): create phase 6 plan
Jun 12, 2026
efdb020
docs(06-testing): finalize Phase 6 plan list in ROADMAP
Jun 12, 2026
877f905
chore: add .planning to gitignore and fix clippy warning
Jun 12, 2026
5aaadad
fix: align model_routes schema with upstream cc-switch
Jun 12, 2026
64cdecc
feat(tui): add proxy enable/disable toggle to proxy settings page
Jun 12, 2026
bada557
feat: add model_route hit tracking and multi-color dashboard
Jun 12, 2026
3682244
chore: remove transient .planning/ files from PR branch
Jun 12, 2026
2a9677d
feat: per-provider color dot matrix in proxy dashboard
Jun 12, 2026
abc7e4e
fix(proxy): prevent model-routed providers from syncing as current pr…
Jun 12, 2026
59ddc3d
fix(proxy): anchor wildcard model route patterns with ^ prefix
Jun 12, 2026
1d029b4
fix(tui): preserve enabled state when editing model routes
Jun 12, 2026
5394c38
test(database): use SCHEMA_VERSION+2 for future-schema rejection tests
Jun 12, 2026
c4d593c
style: fix cargo fmt indentation in main_page.rs
Jun 12, 2026
b9f279f
fix(proxy): emit Anthropic model fields in GET /v1/models response
Jun 13, 2026
406b0f8
test(database): remove unused serde_json::json import in model_routes
Jun 14, 2026
5ba66e4
fix(proxy+tui+db): address Codex P2 review (Gemini path, edit priorit…
Jun 15, 2026
f3a08ea
fix(tui): refresh model_routes hit counts and gate legend by min hits
Jun 15, 2026
8d98cdc
fix(proxy): record provider activity even when token estimate is 0
Jun 15, 2026
20feb74
fix: preserve manual provider routing after rebase
Jun 15, 2026
a0e223d
fix(proxy+tui): align activity samples and address codex review #10/#11
Jun 15, 2026
5f3b8db
fix(tui): align dashboard wave colors with rendered character height
Jun 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ docs/superpowers/
.superpowers/
skills-lock.json
skills/
.planning/
75 changes: 75 additions & 0 deletions .planning/REQUIREMENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Requirements: Per-Model Provider Routing

**Defined:** 2026-06-11
**Core Value:** 一键切换 AI 编程工具的底层 provider,零配置摩擦

## v1 Requirements (Milestone 1)

### 数据存储 (DB)

- [x] **DB-01**: 数据库 Schema 从 v10 升级到 v11,新增 `model_routes` 表
- [x] **DB-02**: `model_routes` 表包含字段:id, app_type, pattern (通配符), provider_id, priority (排序), enabled (开关), created_at, updated_at
- [x] **DB-03**: 支持 CRUD 操作:创建路由规则、列出所有规则、更新规则、删除规则
- [x] **DB-04**: 规则按 priority 排序,同 priority 按创建时间排序
- [x] **DB-05**: 创建规则时验证 provider_id 存在且属于同一 app_type
- [x] **DB-06**: Schema 升级向下兼容:空 model_routes 表 = 行为不变

### 路由引擎 (Router)

- [x] **RT-01**: ModelRouter 在代理请求处理流程中先于 ProviderRouter 执行
- [x] **RT-02**: 支持 `*` 通配符匹配 model 名称(如 `*sonnet*`、`claude-*`、`*-4-5`)
- [x] **RT-03**: 多个规则匹配时,选择 priority 最高(数字最小)的 enabled 规则
- [x] **RT-04**: 无匹配规则时,回退到现有的 ProviderRouter 逻辑(行为不变)
- [x] **RT-05**: 规则指向的 provider 不存在时,记录 warning 日志并回退
- [x] **RT-06**: 路由选中的 provider 为单 provider(不使用 failover 队列)

### CLI 命令 (CLI)

- [x] **CL-01**: `cc-switch proxy model-route list [--app <app>]` — 列出所有路由规则
- [x] **CL-02**: `cc-switch proxy model-route add <pattern> <provider-id> [--priority <n>] [--app <app>]` — 添加路由
- [x] **CL-03**: `cc-switch proxy model-route remove <id>` — 删除路由
- [x] **CL-04**: `cc-switch proxy model-route toggle <id>` — 切换启用/禁用
- [x] **CL-05**: `cc-switch proxy model-route update <id> [--pattern] [--provider] [--priority]` — 更新路由
- [x] **CL-06**: 命令输出人类可读的表格格式(与现有 proxy 命令风格一致)

### TUI 界面 (TUI)

- [x] **UI-01**: 在代理设置页面中增加模型路由管理入口
- [x] **UI-02**: 路由规则列表表格:显示 pattern、目标 provider、优先级、启用状态
- [x] **UI-03**: 支持创建新规则:输入 pattern + 选择 provider + 设置优先级
- [x] **UI-04**: 支持编辑/删除/切换启用状态
- [ ] **UI-05**: 界面风格与现有 TUI 一致(配色、布局、快捷键)

### 同步 (Sync)

- [ ] **SY-01**: model_routes 变更时触发 WebDAV 自动同步(若已配置)
- [ ] **SY-02**: model_routes 变更时触发 S3 自动同步(若已配置)

### 测试 (TEST)

- [x] **TE-01**: model_routes DAO 的 CRUD 单元测试
- [x] **TE-02**: ModelRouter 通配符匹配逻辑的单元测试
- [x] **TE-03**: Schema v10→v11 迁移测试
- [ ] **TE-04**: 代理路由集成测试:匹配规则→选中正确 provider
- [ ] **TE-05**: 代理回退集成测试:无匹配→回退到现有逻辑
- [x] **TE-06**: CLI 命令集成测试

## Out of Scope

| Feature | Reason |
|---------|--------|
| 正则表达式匹配(仅支持 `*` 通配符) | 与上游 cc-switch PR 保持一致,`*` 覆盖 95% 用例 |
| 多 provider failover for model routes | 设计决策:路由规则选中单 provider,匹配失败回退到现有 failover |
| 基于请求内容的动态路由(非 model 名称) | 复杂度高,无明确用例 |
| 路由规则导入/导出 | 可通过 WebDAV/S3 同步覆盖此需求 |

## Traceability

| Requirement | Phase | Status |
|-------------|-------|--------|
| DB-01 ~ DB-06 | Phase 1: Database | Pending |
| RT-01 ~ RT-06 | Phase 2: Router Engine | Pending |
| CL-01 ~ CL-06 | Phase 3: CLI Commands | Pending |
| UI-01 ~ UI-05 | Phase 4: TUI Interface | Pending |
| SY-01 ~ SY-02 | Phase 5: Sync Integration | Pending |
| TE-01 ~ TE-06 | Phase 6: Testing | Pending |
192 changes: 192 additions & 0 deletions .planning/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Roadmap: Per-Model Provider Routing

**Created:** 2026-06-11
**Milestone:** 1
**Total phases:** 6
**Estimated effort:** 17-27 hours (~2.5-4 days)

---

## Phase Dependency Graph

```
Phase 1: Database Layer
Phase 2: Router Engine + Proxy Integration
┌───────────────┬───────────────┐
↓ ↓ ↓
Phase 3: Phase 4: Phase 5:
CLI Commands TUI Interface Sync Integration
↓ ↓ ↓
└───────────────┴───────────────┘
Phase 6: Final Testing & PR Prep
```

Phases 3, 4, 5 可并行执行(都只依赖 Phase 2)。

---

## Phase 1: Database Layer

**Goal:** 创建 `model_routes` 表和相关 DAO,完成 Schema v10→v11 迁移

**Depends on:** 无
**Estimated effort:** 2-3 小时
**Files to touch:** ~4 files, ~230 lines

### Tasks

1. **Schema v11 migration**
- 在 `database/schema.rs` 中实现 `migrate_v10_to_v11()`
- 创建 `model_routes` 表:id INTEGER PK, app_type TEXT NOT NULL, pattern TEXT NOT NULL, provider_id TEXT NOT NULL, priority INTEGER DEFAULT 0, enabled INTEGER DEFAULT 1, created_at TEXT, updated_at TEXT
- 添加 FOREIGN KEY (provider_id) REFERENCES providers(id) ON DELETE CASCADE
- 更新 `CURRENT_SCHEMA_VERSION` 常量

2. **ModelRoute 类型定义**
- 在 `provider.rs`(或新建 `model_route.rs`)中定义 `ModelRoute` struct
- 实现 Serialize/Deserialize/Clone/Debug

3. **model_routes DAO**
- 新建 `database/dao/model_routes.rs`
- `list_routes(app_type) → Vec<ModelRoute>` — 按 priority ASC, created_at ASC
- `create_route(route) → ModelRoute`
- `update_route(id, updates) → ModelRoute`
- `delete_route(id)`
- `toggle_route(id)`
- `get_route(id) → Option<ModelRoute>`
- 创建时验证 provider_id 存在且属于同 app_type
- 在 `database/dao/mod.rs` 中注册模块

4. **Database 集成**
- 在 `database/mod.rs` 中暴露 DAO 方法
- 确保 `try_new()` 自动执行迁移

### Verification
- [ ] `cargo test database` — 所有数据库测试通过
- [ ] DAO CRUD 测试覆盖所有操作
- [ ] Schema 迁移测试:v10 数据库升级到 v11 后数据完整
- [ ] 向下兼容:无 model_routes 时所有现有功能正常

**Covers:** DB-01 ~ DB-06, TE-01, TE-03

---

## Phase 2: Router Engine + Proxy Integration

**Goal:** 实现 ModelRouter 通配符匹配引擎,并集成到代理请求处理流程

**Depends on:** Phase 1
**Estimated effort:** 4-6 小时
**Files to touch:** ~8 files, ~500 lines
**Plans:** 1 plan

### Plans

- [x] 02-01-PLAN.md — ModelRouter engine creation, HandlerContext integration, ProxyServerState wiring, integration tests

**Covers:** RT-01 ~ RT-06, TE-02


---

## Phase 3: CLI Commands

**Goal:** 实现 `cc-switch proxy model-route` 子命令组

**Depends on:** Phase 1(仅需 DAO,可与 Phase 2 并行)
**Estimated effort:** 1-2 小时
**Files to touch:** ~2 files, ~70 lines
**Plans:** 1/1 plans complete

### Plans

- [x] 03-01-PLAN.md — ModelRouteCommand enum definition + ProxyCommand integration + command handler implementation + tests

**Covers:** CL-01 ~ CL-06, TE-06

---

## Phase 4: TUI Interface

**Goal:** 在 ratatui TUI 的代理设置区域增加模型路由管理界面

**Depends on:** Phase 1 + Phase 2(需要 DAO 和 ModelRouter 工作正常)
**Estimated effort:** 6-10 小时(最大工作量)
**Files to touch:** ~10 files, ~400 lines
**Plans:** 2/2 plans complete

### Plans

- [x] 04-01-PLAN.md — ModelRouteSnapshot data type, Route::SettingsModelRoutes, Settings menu entry, table rendering placeholder
- [x] 04-02-PLAN.md — Action variants, runtime action handlers, multi-step Add/Edit overlays, delete confirmation, toggle, keyboard wiring

**Covers:** UI-01 ~ UI-05

---

## Phase 5: Sync Integration

**Goal:** model_routes 变更时触发 WebDAV/S3 自动同步

**Depends on:** Phase 1(仅需 DAO)
**Estimated effort:** 0.5-1 小时
**Files to touch:** ~2 files, ~10 lines

### Tasks

1. **WebDAV 同步触发**
- 在 `services/webdav_auto_sync.rs` 中添加 model_routes 表变更的触发
- 在 DAO 的 create/update/delete 方法中调用 sync trigger

2. **S3 同步触发**
- 在 `services/s3_auto_sync.rs` 中同样添加触发
- 保持与现有同步机制一致的模式

### Verification
- [ ] 配置 WebDAV 同步后,添加/修改路由规则触发同步
- [ ] 配置 S3 同步后,添加/修改路由规则触发同步

**Covers:** SY-01 ~ SY-02

---

## Phase 6: Final Testing & PR Preparation

**Goal:** 全面测试,清理代码,准备可合并的纯净 PR 分支

**Depends on:** Phase 3, 4, 5(全部完成)
**Estimated effort:** 3-5 小时
**Plans:** 1 plan

### Plans

- [ ] 06-01-PLAN.md — Verify integration tests (TE-04/TE-05 already exist), run full test suite + quality gates, prepare clean PR branch with .planning/ excluded

**Covers:** TE-04, TE-05

---

## Risk Register

| Risk | Severity | Mitigation |
|------|----------|------------|
| handler_context 结构与 cc-switch 差异过大,ModelRouter 集成点不匹配 | MEDIUM | Phase 2 开始前详细对比两个项目的 handler_context 结构 |
| TUI 表单组件不够灵活,无法实现 pattern + provider picker 组合输入 | MEDIUM | Phase 4 开始前评估现有 TUI 组件能力,必要时简化输入流程 |
| Schema 迁移与现有备份/恢复机制冲突 | LOW | Phase 1 先研究现有迁移模式和备份逻辑 |
| 上游 PR 的变更在 cc-switch-cli 中路径/API 不同 | LOW | 每个 Phase 对照当前代码库做适配,不盲目复制 |

---

## Traceability

| Phase | Requirements Covered | Est. Effort |
|-------|---------------------|-------------|
| Phase 1: Database | DB-01~06, TE-01, TE-03 | 2-3h |
| Phase 2: Router Engine | RT-01~06, TE-02 | 4-6h |
| Phase 3: CLI Commands | CL-01~06, TE-06 | 1-2h |
| Phase 4: TUI Interface | UI-01~05 | 6-10h |
| Phase 5: Sync | SY-01~02 | 0.5-1h |
| Phase 6: Testing & PR | TE-04~05 | 3-5h |
| **Total** | **31 requirements** | **17-27h** |
102 changes: 102 additions & 0 deletions .planning/STATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
gsd_state_version: 1.0
milestone: v1.0
milestone_name: milestone
current_phase: Phase 4 (complete)
status: in_progress
last_updated: "2026-06-12T01:22:05.651Z"
progress:
total_phases: 6
completed_phases: 4
total_plans: 6
completed_plans: 6
percent: 67
---

# State: CC-Switch CLI

**Last updated:** 2026-06-12
**Active milestone:** Milestone 1 — Per-Model Provider Routing
**Current phase:** Phase 3 (complete)

## Project Reference

See: `.planning/PROJECT.md` (updated 2026-06-11)

**Core value:** 一键切换 AI 编程工具的底层 provider,零配置摩擦
**Current focus:** 实现 per-model provider routing(根据模型名称将代理请求路由到不同 provider)

## Milestone Progress

| Phase | Status | Est. Effort | Started | Completed |
|-------|--------|-------------|---------|-----------|
| Phase 1: Database | ✅ Complete | 2-3h | 2026-06-11 | 2026-06-11 |
| Phase 2: Router Engine | ✅ Complete | 4-6h | 2026-06-11 | 2026-06-12 |
| Phase 3: CLI Commands | ✅ Complete | 1-2h | 2026-06-11 | 2026-06-12 |
| Phase 4: TUI Interface | ✅ Complete | 6-10h | 2026-06-12 | 2026-06-12 |
| Phase 5: Sync Integration | ⬜ Pending | 0.5-1h | — | — |
| Phase 6: Testing & PR Prep | ⬜ Pending | 3-5h | — | — |

## Reference Artifacts

- Codebase map: `.planning/codebase/` (7 documents, 2391 lines, generated 2026-06-11)
- Phase 1 Research: `.planning/phase-1/RESEARCH.md`
- Phase 1 Plan: `.planning/phases/01-database/01-01-PLAN.md` (1 plan, 3 tasks, 1 wave)
- Phase 1 Summary: `.planning/phases/01-database/01-01-SUMMARY.md`
- Phase 2 Research: `.planning/phase-2/RESEARCH.md`
- Phase 2 Plan: `.planning/phases/02-router/02-01-PLAN.md` (1 plan, 3 tasks, 1 wave)
- Phase 2 Summary: `.planning/phases/02-router/02-01-SUMMARY.md`
- Phase 3 Research: `.planning/phase-3/RESEARCH.md`
- Phase 3 Summary: `.planning/phases/03-cli/03-01-SUMMARY.md`
- Phase 4 Plan 01: `.planning/phases/04-tui-interface/04-01-PLAN.md` (1 plan, 2 tasks, 1 wave)
- Phase 4 Summary 01: `.planning/phases/04-tui-interface/04-01-SUMMARY.md`
- Phase 4 Plan 02: `.planning/phases/04-tui-interface/04-02-PLAN.md` (1 plan, 2 tasks, 1 wave)
- Phase 4 Summary 02: `.planning/phases/04-tui-interface/04-02-SUMMARY.md`

## Working State

- **Branch:** `main` (clean)
- **Last commit:** `e10ef89 style(04-tui-interface): apply cargo fmt formatting fixes`
- **Schema version:** v11

## Quick Start (Next Session)

```bash

# Phase 4 is complete. Phase 5 (Sync Integration) is next.

/gsd-execute-phase 05-sync --wave 1
```

## Notes

- 上游 PR #4081 于 2026-06-11 提交,当前状态 OPEN,有一次 codex review 但无实质性修改要求
- cc-switch-cli 与 cc-switch 的关键差异:无 React 前端、ratatui TUI、代理架构细节可能不同
- Phase 4 (TUI) 是最大的工作量来源(35-40%),取决于现有 TUI 组件的复用程度
- Phase 1 completed: model_routes table, ModelRoute type, CRUD DAO — all foundations in place
- Phase 2 completed: ModelRouter engine, proxy integration — route matching works end-to-end
- Phase 3 complete: CLI commands for model-route CRUD (1 plan, 2 tasks, 1 wave)
- Phase 3 Summary: `.planning/phases/03-cli/03-01-SUMMARY.md`
- Phase 4 Plan 01: `.planning/phases/04-tui-interface/04-01-PLAN.md` (1 plan, 2 tasks, 1 wave)
- Phase 4 Summary 01: `.planning/phases/04-tui-interface/04-01-SUMMARY.md`
- Phase 4 Wave 1 (04-01) complete: model routes TUI scaffolding (data types, navigation, table rendering)
- Phase 4 Wave 2 (04-02) complete: model routes full CRUD operations via TUI overlays

## Performance Metrics

| Phase | Plan | Duration | Notes |
|-------|------|----------|-------|
| Phase 01-database P01 | 18 min | 3 tasks | 7 files |
| Phase 02-router P01 | 67 min | 3 tasks | 6 files |
| Phase 03-cli P01 | 7 min | 2 tasks | 1 file |
| Phase 04-tui-interface P01 | 10 min | 2 tasks | 8 files + 1 new |
| Phase 04-tui-interface P02 | ~10 min | 2 tasks | 9 files + 1 new |

## Decisions

- [Phase 1]: ModelRoute type in separate model_route.rs module (matches upstream PR #4081 structure)
- [Phase 2]: ModelRouter holds Arc<Database> only — no caching, reads routes fresh on every request
- [Phase 2]: Single provider for matched routes (no failover queue) — matches upstream design decision
- [Phase 3]: cli/mod.rs unchanged — Clap derive auto-discovers ProxyCommand::ModelRoute via existing dispatch
- [Phase 4]: Model routes rendering uses dedicated ui/model_routes.rs module (matches existing config.rs sub-page pattern)
- [Phase 4 P2]: Multi-step overlay flow (pattern -> provider -> priority) for Add/Edit; Space toggles with no toast
1 change: 1 addition & 0 deletions src-tauri/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# will have compiled files and executables
/target/
/gen/schemas
.planning/
Loading