flexgate 0.4.0__tar.gz → 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. flexgate-0.5.1/.github/workflows/docs.yml +65 -0
  2. {flexgate-0.4.0 → flexgate-0.5.1}/.gitignore +3 -0
  3. {flexgate-0.4.0 → flexgate-0.5.1}/CLAUDE.md +8 -10
  4. {flexgate-0.4.0 → flexgate-0.5.1}/PKG-INFO +6 -1
  5. {flexgate-0.4.0 → flexgate-0.5.1}/README.md +5 -0
  6. flexgate-0.5.1/docs/Makefile +14 -0
  7. flexgate-0.5.1/docs/_static/.gitkeep +0 -0
  8. flexgate-0.5.1/docs/cli.md +235 -0
  9. flexgate-0.5.1/docs/conf.py +46 -0
  10. flexgate-0.5.1/docs/configuration.md +125 -0
  11. flexgate-0.5.1/docs/development.md +62 -0
  12. flexgate-0.5.1/docs/index.md +52 -0
  13. flexgate-0.5.1/docs/quickstart.md +53 -0
  14. flexgate-0.5.1/docs/requirements.txt +4 -0
  15. flexgate-0.5.1/docs/usage.md +43 -0
  16. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/__init__.py +1 -1
  17. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/cli.py +180 -61
  18. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/config.py +29 -4
  19. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/migrate.py +14 -1
  20. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/proxy.py +20 -9
  21. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/router.py +17 -17
  22. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/server.py +2 -2
  23. flexgate-0.5.1/flexgate/sync.py +109 -0
  24. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/update.py +17 -0
  25. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/usage.py +22 -10
  26. flexgate-0.4.0/flexgate/sync.py +0 -203
  27. {flexgate-0.4.0 → flexgate-0.5.1}/.github/workflows/release.yml +0 -0
  28. {flexgate-0.4.0 → flexgate-0.5.1}/config.yaml.template +0 -0
  29. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/__main__.py +0 -0
  30. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/doctor.py +0 -0
  31. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/guardian.py +0 -0
  32. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/healthcheck.py +0 -0
  33. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/main.py +0 -0
  34. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/service.py +0 -0
  35. {flexgate-0.4.0 → flexgate-0.5.1}/flexgate/settings.py +0 -0
  36. {flexgate-0.4.0 → flexgate-0.5.1}/pyproject.toml +0 -0
  37. {flexgate-0.4.0 → flexgate-0.5.1}/test.png +0 -0
  38. {flexgate-0.4.0 → flexgate-0.5.1}/test_multimodal.py +0 -0
  39. {flexgate-0.4.0 → flexgate-0.5.1}/test_multimodal_gateway.py +0 -0
@@ -0,0 +1,65 @@
1
+ name: docs
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ paths:
7
+ - 'docs/**'
8
+ - 'flexgate/__init__.py'
9
+ - '.github/workflows/docs.yml'
10
+ pull_request:
11
+ branches: [main]
12
+ paths:
13
+ - 'docs/**'
14
+ - '.github/workflows/docs.yml'
15
+ workflow_dispatch:
16
+
17
+ permissions:
18
+ contents: read
19
+ pages: write
20
+ id-token: write
21
+
22
+ concurrency:
23
+ group: "pages"
24
+ cancel-in-progress: false
25
+
26
+ jobs:
27
+ build-docs:
28
+ name: Build Documentation
29
+ runs-on: ubuntu-latest
30
+ steps:
31
+ - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
32
+
33
+ - name: Set up Python
34
+ uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
35
+ with:
36
+ python-version: '3.12'
37
+
38
+ - name: Install dependencies
39
+ run: |
40
+ python -m pip install --upgrade pip
41
+ pip install -r docs/requirements.txt
42
+ pip install .
43
+
44
+ - name: Build docs
45
+ run: |
46
+ cd docs
47
+ make html
48
+
49
+ - name: Upload artifact
50
+ uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1
51
+ with:
52
+ path: docs/_build/html
53
+
54
+ deploy-docs:
55
+ name: Deploy to GitHub Pages
56
+ environment:
57
+ name: github-pages
58
+ url: ${{ steps.deployment.outputs.page_url }}
59
+ runs-on: ubuntu-latest
60
+ needs: build-docs
61
+ if: github.ref == 'refs/heads/main'
62
+ steps:
63
+ - name: Deploy to GitHub Pages
64
+ id: deployment
65
+ uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5
@@ -6,3 +6,6 @@ config.yaml
6
6
  .github/copilot-instructions.md
7
7
  dist/
8
8
  .venv/
9
+
10
+ # Sphinx build artifacts
11
+ docs/_build/
@@ -35,9 +35,8 @@ flexgate settings import # read from ~/.claude/settings.json
35
35
  flexgate settings apply # write ANTHROPIC_BASE_URL → localhost
36
36
 
37
37
  # confsync config sync
38
- flexgate sync # pull config.yaml from your confsync server (merge keys)
38
+ flexgate sync # pull: replace local config.yaml with the remote document (backup first)
39
39
  flexgate sync push # upload local config.yaml to the server
40
- flexgate sync --full # replace local config with the remote document (backup first)
41
40
  flexgate sync --dry-run # preview changes without writing
42
41
 
43
42
  # Versioning / upgrades
@@ -95,15 +94,15 @@ Claude Code → POST /v1/messages (model="claude-sonnet-4-6")
95
94
  | `sync.py` | `flexgate sync` — pushes/pulls the whole config.yaml as an encrypted document on a confsync server (lazily imports the `confsync` client package) |
96
95
  | `migrate.py` | Config schema versioning: `config_version` marker, per-step `MIGRATIONS` chain (N → N+1), backup + atomic rewrite |
97
96
  | `doctor.py` | `flexgate doctor` — read-only diagnostics (Python, PyPI update, config schema/semantics, port, systemd, Claude settings) |
98
- | `update.py` | `flexgate update` — PyPI version check, package upgrade via detected installer (pipx/uv/pip), config migration, service reload; also the cached (24h) new-version notice shown by bare `flexgate` / `service status` |
97
+ | `update.py` | `flexgate update` — PyPI version check, package upgrade via detected installer (pipx/uv/pip), config migration, service reload; after a successful package upgrade the config migration is re-run in a fresh process so it uses the new code's schema version (`FLEXGATE_UPDATE_DELEGATED` guards against re-delegation); also the cached (24h) new-version notice shown by bare `flexgate` / `service status` |
99
98
 
100
99
  ### Key design points
101
100
 
102
101
  - **Regex-first routing**: Routes are regex patterns matched against the `model` field in the request body. First match wins. A catch-all `".*"` pattern at the end handles fallback.
103
102
  - **Model resolution & `available_models` fallback**: A route may omit `model`; `router.resolve()` then falls back to the provider's first `available_models` entry, so `model_override` handed to the proxy is always a concrete name. `config._parse_routes` rejects routes that omit `model` on a provider with no `available_models` — so adding a provider without models requires an explicit `model` on every route using it.
104
103
  - **Proxy rewrite contract** (`proxy.py`): the upstream request gets the provider's `x-api-key` plus a fixed header set, and the JSON `model` field is rewritten only when the route set an override. Streaming responses are forwarded as raw bytes (`aiter_bytes`), never parsed.
105
- - **Key fallback** (`proxy.py`): a provider's `api_keys` list holds one or more keys for the same upstream (entries are key strings or `{key, note}` mappings; `note` is a free-form label shown by `flexgate status` and in fallback logs). Multiple accounts on one upstream — e.g. several MiniMax subscriptions — belong in ONE provider, not separate ones. On a retryable failure (HTTP 401/402/403/429/500/502/503/529, connect error, or timeout) the request is retried with the next key in order. Streaming requests can only fall back before the upstream returns 200. `_regular_proxy`/`_stream_proxy` return `(response, retryable)`; `handle_request` owns the loop and returns the last error when all keys fail. The legacy `api_key` + `fallback_keys` schema is still parsed (`config._parse_api_keys`); migration v3 → v4 rewrites it to `api_keys`. `ProviderConfig.api_key` remains as a first-key property (getter/setter) for sync/settings merge code.
106
- - **Usage inspection** (`usage.py`): `flexgate status` maps `base_url` to a platform adapter (`_ADAPTERS`): MiniMax → `{origin}/v1/api/openplatform/coding_plan/remains` (the `*_usage_count` fields are REMAINING counts, an upstream naming quirk), Kimi Code → `{base}/v1/usages` (undocumented, same endpoint as the CLI's `/usage`; reports weekly quota, 5-hour window and parallel limit), z.ai/bigmodel → `{origin}/api/monitor/usage/quota/limit` (undocumented but used by z.ai's own plugin), USTC/LiteLLM → `{origin}/key/info`. Hosts in `_PROBE_ONLY_MARKERS` (e.g. xiaomimimo.com — no key-based usage API) and unknown platforms get the minimal chat probe: `POST /v1/messages` with input `"hi"`, `max_tokens=128`. A failed adapter call also degrades to the probe.
104
+ - **Key fallback** (`proxy.py`): a provider's `api_keys` list holds one or more keys for the same upstream (entries are key strings or `{key, note}` mappings; `note` is a free-form label shown by `flexgate status` and in fallback logs). Multiple accounts on one upstream — e.g. several MiniMax subscriptions — belong in ONE provider, not separate ones. Each *route* has an active-key pointer (`active_key` in YAML, 1-based; `RouteConfig.key_index` 0-based, default first key): requests on that route start from the pointed key, and on a retryable failure (HTTP 401/402/403/429/500/502/503/529, connect error, or timeout) the next key is tried and the pointer advances with it (in memory only, wrapping around — it is not written back to the file); every key is tried at most once and a full circle of failures returns the last error. The pointer is set interactively via `flexgate config edit` → "api keys" (pick a route, with live per-key usage) or by editing the route's `active_key` directly. `resolve()` (`router.py`) returns the matched `RouteConfig` so the proxy can read/advance its pointer. Streaming requests can only fall back before the upstream returns 200. `_regular_proxy`/`_stream_proxy` return `(response, retryable)`; `handle_request` owns the loop and returns the last error when all keys fail. The legacy `api_key` + `fallback_keys` schema is still parsed (`config._parse_api_keys`); migration v3 → v4 rewrites it to `api_keys`. `ProviderConfig.api_key` remains as a first-key property (getter/setter) for the settings import code.
105
+ - **Usage inspection** (`usage.py`): `flexgate status` maps `base_url` to a platform adapter (`_ADAPTERS`): MiniMax → `{origin}/v1/api/openplatform/coding_plan/remains` (only the "general" text-model quota entry is used; its counts are usually 0/0, so usage is read from the `*_remaining_percent` fields), Kimi Code → `{base}/v1/usages` (undocumented, same endpoint as the CLI's `/usage`; reports weekly quota, 5-hour window and parallel limit), z.ai/bigmodel → `{origin}/api/monitor/usage/quota/limit` (undocumented but used by z.ai's own plugin), USTC/LiteLLM → `{origin}/key/info`. Hosts in `_PROBE_ONLY_MARKERS` (e.g. xiaomimimo.com — no key-based usage API) and unknown platforms get the minimal chat probe: `POST /v1/messages` with input `"hi"`, `max_tokens=128`. A failed adapter call also degrades to the probe.
107
106
  - **Multimodal degradation**: `MULTIMODAL_MODELS` (currently `{"MiniMax-M3", "glm-4.6v"}`) is the allowlist. Requests carrying image blocks aimed at any other model have images stripped and a `[flexgate]` text note injected into both the outgoing request and the returned response, so non-multimodal backends don't 4xx.
108
107
  - **Schedule-based overrides**: Optional time windows (e.g. 22:00-06:00) override default routes. Overnight wrap is supported.
109
108
  - **Service-first lifecycle**: `flexgate.service` is the sole persistent runtime. systemd owns restart, boot startup, logs, and process state.
@@ -119,11 +118,10 @@ Config lives at `~/.flexgate/config.yaml` (override with `FLEXGATE_CONFIG`).
119
118
  `flexgate sync` needs no flexgate-side setup — connection details come from
120
119
  the shared confsync credentials written by `confsync login --server <url>`
121
120
  (`~/.confsync/credentials.json`). The whole config.yaml is synced as one
122
- encrypted document (app `flexgate`, name `config.yaml`): pull merges
123
- provider api_keys and imports remote-only providers (local routes/schedules
124
- untouched), bootstraps the file when missing, and `--full` replaces it with a
125
- timestamped backup. The `confsync-client` package is a declared dependency
126
- (PyPI), so sync works out of the box once logged in.
121
+ encrypted document (app `flexgate`, name `config.yaml`): pull always replaces
122
+ the local file with the remote document (a timestamped backup is kept), or
123
+ bootstraps the file when missing. The `confsync-client` package is a declared
124
+ dependency (PyPI), so sync works out of the box once logged in.
127
125
  The persistent unit lives at `~/.config/systemd/user/flexgate.service`; logs are
128
126
  in the systemd user journal. `~/.flexgate/service-state.json` records the last
129
127
  successfully applied config path and endpoint. PID/guardian files are legacy artifacts only.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: flexgate
3
- Version: 0.4.0
3
+ Version: 0.5.1
4
4
  Summary: Flexible local API gateway for Claude Code — route models to different providers
5
5
  Project-URL: Homepage, https://github.com/Agony5757/flexible-gateway
6
6
  Project-URL: Repository, https://github.com/Agony5757/flexible-gateway
@@ -17,8 +17,13 @@ Description-Content-Type: text/markdown
17
17
 
18
18
  # Flexgate
19
19
 
20
+ [![PyPI](https://img.shields.io/pypi/v/flexgate)](https://pypi.org/project/flexgate/)
21
+ [![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://agony5757.github.io/flexible-gateway/)
22
+
20
23
  本地 Anthropic API 网关,根据请求中的 model 名称自动路由到不同的 provider。同时提供 Claude Code settings.json 的自动管理功能。
21
24
 
25
+ 完整文档见 **[agony5757.github.io/flexible-gateway](https://agony5757.github.io/flexible-gateway/)**(Sphinx 构建,源文件在 `docs/`,随 main 分支自动发布)。
26
+
22
27
  ## 用途
23
28
 
24
29
  Claude Code 只能配置一个 `ANTHROPIC_BASE_URL`,所有 tier(opus/sonnet/haiku)都指向同一个 provider。Flexgate 在本地启动一个 Anthropic 兼容端点,按 model 名称路由到不同 provider。
@@ -1,7 +1,12 @@
1
1
  # Flexgate
2
2
 
3
+ [![PyPI](https://img.shields.io/pypi/v/flexgate)](https://pypi.org/project/flexgate/)
4
+ [![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://agony5757.github.io/flexible-gateway/)
5
+
3
6
  本地 Anthropic API 网关,根据请求中的 model 名称自动路由到不同的 provider。同时提供 Claude Code settings.json 的自动管理功能。
4
7
 
8
+ 完整文档见 **[agony5757.github.io/flexible-gateway](https://agony5757.github.io/flexible-gateway/)**(Sphinx 构建,源文件在 `docs/`,随 main 分支自动发布)。
9
+
5
10
  ## 用途
6
11
 
7
12
  Claude Code 只能配置一个 `ANTHROPIC_BASE_URL`,所有 tier(opus/sonnet/haiku)都指向同一个 provider。Flexgate 在本地启动一个 Anthropic 兼容端点,按 model 名称路由到不同 provider。
@@ -0,0 +1,14 @@
1
+ # Minimal makefile for Sphinx documentation
2
+
3
+ SPHINXOPTS ?=
4
+ SPHINXBUILD ?= sphinx-build
5
+ SOURCEDIR = .
6
+ BUILDDIR = _build
7
+
8
+ help:
9
+ @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
10
+
11
+ .PHONY: help Makefile
12
+
13
+ %: Makefile
14
+ @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
File without changes
@@ -0,0 +1,235 @@
1
+ # 命令参考
2
+
3
+ ```text
4
+ usage: flexgate [-h] [--config CONFIG] [--version]
5
+ {service,run,check,status,usage,settings,sync,help,doctor,update,config} ...
6
+ ```
7
+
8
+ 全局参数:
9
+
10
+ - `--config PATH` 指定配置文件(默认 `~/.flexgate/config.yaml`)
11
+ - `--port PORT` 覆盖配置文件中的端口(仅 `flexgate run`)
12
+ - `--version` 打印版本号(`service status` / 裸 `flexgate` 也会显示)
13
+
14
+ ## 服务管理(默认持久化模式)
15
+
16
+ systemd **用户服务**是 Linux 上唯一推荐的持久化运行方式,负责开机/登录
17
+ 自启、崩溃重启、日志和进程生命周期。
18
+
19
+ ```bash
20
+ flexgate service install # 安装、启用并立即启动
21
+ flexgate service install --no-start # 仅安装并启用,不立即启动
22
+ flexgate service start # 启动;自动修复旧格式或失效的 unit
23
+ flexgate service stop # 停止
24
+ flexgate service restart # 重启
25
+ flexgate service reload # 热重载;host/port 变化时自动安全重启
26
+ flexgate service status # 查看 systemd 状态和当前路由表
27
+ flexgate service uninstall # 停止、禁用并删除 unit
28
+ ```
29
+
30
+ 说明:
31
+
32
+ - unit 写入 `~/.config/systemd/user/flexgate.service`,直接运行前台
33
+ server,由 systemd 监督(`Type=simple`、`Restart=on-failure`)。
34
+ - `install` 会执行 `loginctl enable-linger`,使服务在未登录时仍保持运行
35
+ 并随开机启动。
36
+ - `install --no-start` 不会修改 Claude Code settings,避免把客户端指向
37
+ 尚未运行的 endpoint。
38
+ - unit 只能引用持久化配置路径;为避免重启后失效,`/tmp` 下的配置会被拒绝。
39
+ - `start`/`restart` 会清理旧 PID/guardian 残留、修复旧 unit 或已失效的
40
+ 配置路径,并在端口被其他进程占用时拒绝启动。
41
+ - 若升级时检测到旧版后台 gateway 仍在运行,会先准备好 systemd unit,但
42
+ 不会强杀正在服务的进程;按提示手动 `kill <PID>` 停掉旧进程,再执行
43
+ `flexgate service start` 完成切换。
44
+ - unit 设置了启动速率限制,永久配置错误不会再无限快速重启。
45
+ - `service reload` 和 `config set/edit` 会在仅路由变化时发送 SIGUSR1;
46
+ 如果 endpoint 变化,则先检查再 restart。若只改 host、仍复用当前 port,
47
+ 为避免误停服务会要求先执行 `service stop`,再执行 `service start`。
48
+ - 查看日志:`journalctl --user -u flexgate -e`。
49
+
50
+ ## 状态总览与用量查询
51
+
52
+ ```bash
53
+ flexgate status # providers + fallback 链 + 每个 key 的用量 + 当前路由
54
+ flexgate status --no-usage # 跳过用量查询,只看配置
55
+ flexgate status --usage-timeout 30
56
+ flexgate usage # 只看每个 key 的用量/额度(不打印配置和路由)
57
+ flexgate usage --usage-timeout 30
58
+ ```
59
+
60
+ 各平台的用量查询适配器详见[用量查询](usage.md)。
61
+
62
+ ## 上游连通性预检
63
+
64
+ `flexgate check` 会向每个**被路由引用的 `(provider, model)` 组合**发送一次
65
+ `POST /v1/messages`(`max_tokens=1`,消耗约 1~2 token),用于主动检查:
66
+
67
+ - DNS / TCP / TLS 不可达(`base_url` 写错、网络不通)
68
+ - API key 无效或过期(HTTP 401 / 403)
69
+ - 仍是默认占位符(如 `your-zai-api-key`)
70
+ - Provider 侧 5xx 故障
71
+
72
+ 可通过 `--verify-timeout N` 调整每个 provider 的超时时间(默认 15 秒)。
73
+
74
+ ## 前台调试
75
+
76
+ `run` / `check` 是独立的顶层调试命令,不属于持久化服务模式:
77
+
78
+ ```bash
79
+ flexgate run # 单个前台进程,仅用于开发/调试
80
+ flexgate check # 上游 provider 连通性检测
81
+ ```
82
+
83
+ 非 systemd 环境只能使用 `flexgate run` 前台运行。`--port PORT` 也只对
84
+ `run` 生效;持久化服务的端口必须写入 `server.port`。
85
+
86
+ ## 配置管理
87
+
88
+ ```bash
89
+ flexgate config init # 创建默认配置(~/.flexgate/config.yaml)
90
+ flexgate config show # 查看当前配置(providers、路由、定时规则)
91
+ flexgate config edit # 交互式选择每个 tier(opus/sonnet/haiku)的 provider/model
92
+ flexgate config path # 打印配置文件路径
93
+ flexgate config set <tier> <target> [model] # 快速设置路由(tier 可为 all/opus/sonnet/haiku)
94
+ ```
95
+
96
+ `config set` 支持按 provider 名或 model 名设置路由:
97
+
98
+ ```bash
99
+ # 批量切换所有 tier(opus/sonnet/haiku)到同一个 provider
100
+ flexgate config set all xiaomi
101
+
102
+ # 用逗号组合多个 tier
103
+ flexgate config set opus,sonnet xiaomi
104
+
105
+ # 按 provider 名 + model 名
106
+ flexgate config set sonnet minimax MiniMax-M3
107
+
108
+ # 按 provider 名(不改写 model)
109
+ flexgate config set opus zai
110
+
111
+ # 按 model 名自动查找 provider
112
+ flexgate config set haiku MiniMax-M3
113
+ # → 自动解析为 minimax / MiniMax-M3
114
+ ```
115
+
116
+ 如果 model 名在多个 provider 中存在,会提示歧义并给出带 provider 名的
117
+ 写法。
118
+
119
+ :::{note}
120
+ `config set` 不会修改 API key。如需添加新 provider 或修改密钥,请手动
121
+ 编辑配置文件。
122
+ :::
123
+
124
+ ### 交互式编辑(`config edit`)
125
+
126
+ 运行 `flexgate config edit` 进入全屏交互界面,用 **↑/↓ 方向键移动、回车
127
+ 选择**,无需记忆 provider/model 名称:
128
+
129
+ ```text
130
+ Flexgate config — ~/.flexgate/config.yaml
131
+ ↑/↓ move · Enter edit · s save · q quit
132
+
133
+ ▶ opus ustc / deepseek-v4-pro
134
+ sonnet ustc / deepseek-v4-pro
135
+ haiku ustc / deepseek-v4-pro
136
+ fallback ustc / deepseek-v4-pro
137
+ api keys select the active key per route
138
+
139
+ ○ no unsaved changes
140
+ ```
141
+
142
+ - 方向键选中某个 tier(opus/sonnet/haiku)或 `fallback`(兜底路由
143
+ `.*`,未命中任何 tier 的请求走它),回车进入:先从候选
144
+ **provider** 列表选择,再从该 provider 的候选 **model** 列表选择。
145
+ - 选中 `api keys` 回车进入:先选一条**路由**,再选它的 **active key**
146
+ (该路由的请求从这个 key 开始;fallback 会自动前移指针,所有 key
147
+ 轮换一圈都失败则返回错误)。key 列表会实时查询每个 key 的用量/有效性
148
+ (与 `flexgate status` 相同),并标注当前 active 的 key。
149
+ - model 列表包含:`available_models` 中的各个模型、「使用 provider 默认
150
+ (首个可用模型,不写死 model)」、以及「自定义模型…」(手动输入)。
151
+ - 按 `s` 保存(并向运行中的网关发送 SIGUSR1 热重载,**无需重启即生效**),
152
+ 按 `q` 退出(有未保存改动时会询问 "Config changed — activate now?":
153
+ 选 Yes 立即保存并热重载生效,选 No 放弃改动);子菜单中按 `Esc`/`←`
154
+ 返回上一级。
155
+ - 需要交互式终端(TTY);非交互场景请改用 `flexgate config set`。
156
+
157
+ ## Settings 管理
158
+
159
+ ```bash
160
+ flexgate settings import # 从 ~/.claude/settings.json* 导入凭证到 config.yaml
161
+ flexgate settings apply # 将 config.yaml 配置写入 ~/.claude/settings.json
162
+ ```
163
+
164
+ **import** 会扫描 `~/.claude/settings.json*`,从每个文件中提取
165
+ `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_AUTH_TOKEN`,自动写入 config.yaml 的
166
+ providers 部分。文件名与 provider 名称的映射规则:
167
+
168
+ - `settings.json` → 根据域名自动推断(如含 `z.ai` → `zai`)
169
+ - `settings.json.zai` → provider 名 `zai`
170
+ - `settings.json.bak.*` → 跳过(备份文件)
171
+
172
+ **apply** 会读取 config.yaml 中的 `server` 和 `claude_settings`,备份当前
173
+ `~/.claude/settings.json` 为 `settings.json.bak.{timestamp}`,然后生成新的
174
+ settings.json,将 `ANTHROPIC_BASE_URL` 指向本地网关,并保留原有的
175
+ `permissions` 等非 env 字段:
176
+
177
+ ```json
178
+ {
179
+ "env": {
180
+ "ANTHROPIC_BASE_URL": "http://127.0.0.1:8765",
181
+ "ANTHROPIC_AUTH_TOKEN": "gateway",
182
+ "API_TIMEOUT_MS": "3000000",
183
+ "ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-4-7",
184
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
185
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5"
186
+ },
187
+ "permissions": {
188
+ "defaultMode": "bypassPermissions"
189
+ }
190
+ }
191
+ ```
192
+
193
+ `ANTHROPIC_AUTH_TOKEN` 值任意但不能为空,网关会替换为对应 provider 的 key。
194
+
195
+ ## 配置同步(sync)
196
+
197
+ `flexgate sync` 把整个 `config.yaml` 作为一份**加密文档**,通过 confsync
198
+ 服务器在多机之间同步(使用共享的 confsync 凭证,配置文件中不再保存
199
+ confsync 配置段):
200
+
201
+ ```bash
202
+ flexgate sync # 默认 pull:用远端文档整体替换本地配置(先备份)
203
+ flexgate sync pull # 同上
204
+ flexgate sync push # 上传本地 config.yaml
205
+ flexgate sync --dry-run # 只预览,不写配置
206
+ ```
207
+
208
+ 更多细节见 `flexgate help`。
209
+
210
+ ## 版本与升级
211
+
212
+ ```bash
213
+ flexgate --version # 打印版本号
214
+ flexgate doctor # 只读体检:Python、PyPI 新版、配置 schema、端口、systemd、Claude settings
215
+ flexgate doctor --offline # 跳过 PyPI 检查
216
+ flexgate update # 一键升级:pip/pipx/uv 升级包 + 迁移配置 schema + 热重载服务
217
+ flexgate update --check # 只报告将要做什么,不改动
218
+ flexgate update --config-only # 只迁移配置,不升级包
219
+ ```
220
+
221
+ 升级策略:
222
+
223
+ - **包升级**:版本号唯一来源是 `flexgate/__init__.py`;发布到 PyPI 后,
224
+ `flexgate update` 自动检测安装方式(pipx / uv tool / pip)并升级到最新
225
+ release。
226
+ - **新版本自动提示**:裸 `flexgate` 和 `flexgate service status` 会自动
227
+ 比对 PyPI 上的最新版本,有新版时打印一行升级提示。检查结果缓存在
228
+ `~/.flexgate/update-check.json`,每 24 小时最多访问一次 PyPI,离线时
229
+ 静默跳过。
230
+ - **配置迁移**:`config.yaml` 带 `config_version` 标记。每次 schema 变化
231
+ 在 `flexgate/migrate.py` 的 `MIGRATIONS` 中登记一条 N → N+1 规则,升级
232
+ 时逐级走完整个迁移链。迁移前自动备份为 `config.yaml.bak-<时间戳>`;
233
+ 配置比当前 flexgate 更新时会被拒绝并提示先升级。
234
+ - **自检**:发版或排障时跑 `flexgate doctor`,有 FAIL 项时退出码为 1,
235
+ 可直接用于 CI 门禁。
@@ -0,0 +1,46 @@
1
+ # Configuration file for the Sphinx documentation builder.
2
+ #
3
+ # For the full list of built-in configuration values, see the documentation:
4
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html
5
+
6
+ import pathlib
7
+ import sys
8
+
9
+ parent_path = pathlib.Path(__file__).resolve().parent.parent
10
+ sys.path.insert(0, str(parent_path))
11
+
12
+ from flexgate import __version__ # noqa: E402
13
+
14
+ # -- Project information -----------------------------------------------------
15
+
16
+ project = "flexgate"
17
+ author = "agony"
18
+ version = __version__
19
+ release = __version__
20
+
21
+ # -- General configuration ---------------------------------------------------
22
+
23
+ extensions = [
24
+ "myst_parser",
25
+ ]
26
+
27
+ myst_enable_extensions = [
28
+ "colon_fence",
29
+ "linkify",
30
+ ]
31
+
32
+ source_suffix = {".md": "markdown"}
33
+ templates_path = ["_templates"]
34
+ exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
35
+ language = "zh_CN"
36
+
37
+ # -- Options for HTML output -------------------------------------------------
38
+
39
+ html_theme = "furo"
40
+ html_static_path = ["_static"]
41
+ html_title = f"flexgate {version} 文档"
42
+ html_theme_options = {
43
+ "source_repository": "https://github.com/Agony5757/flexible-gateway",
44
+ "source_branch": "main",
45
+ "source_directory": "docs/",
46
+ }
@@ -0,0 +1,125 @@
1
+ # 配置文件
2
+
3
+ 主要运行时资源:
4
+
5
+ | 文件 | 说明 |
6
+ |------|------|
7
+ | `~/.flexgate/config.yaml` | 主配置文件 |
8
+ | `~/.flexgate/service-state.json` | 最近一次成功启动所应用的 config 路径与 endpoint |
9
+ | `~/.flexgate/update-check.json` | PyPI 新版本检查的缓存(24h 有效期) |
10
+ | `~/.config/systemd/user/flexgate.service` | 唯一的持久化服务 unit |
11
+ | systemd journal | 服务日志(`journalctl --user -u flexgate`) |
12
+
13
+ 旧版本的 `~/.flexgate/flexgate.pid`、`flexgate.guardian.pid` 和
14
+ `flexgate.log` 不再属于当前运行架构;service 启动时会安全清理 PID 残留,
15
+ 历史日志文件可按需手动删除。
16
+
17
+ ## 完整示例
18
+
19
+ 运行 `flexgate config init` 创建默认配置,或手动编辑:
20
+
21
+ ```yaml
22
+ server:
23
+ host: "127.0.0.1"
24
+ port: 8765
25
+
26
+ providers:
27
+ zai:
28
+ base_url: "https://api.z.ai/api/anthropic"
29
+ api_keys:
30
+ - "your-zai-api-key"
31
+ minimax:
32
+ base_url: "https://api.minimaxi.com/anthropic"
33
+ api_keys: # 同一上游可配多个 key,互为 fallback
34
+ - key: "your-minimax-api-key"
35
+ note: "主账号" # 可选备注,存在配置里,status/日志中显示
36
+ - key: "your-minimax-api-key-2"
37
+ note: "备用账号"
38
+ - "your-minimax-api-key-3" # 纯字符串写法(无备注)
39
+
40
+ claude_settings:
41
+ default_opus_model: "claude-opus-4-7"
42
+ default_sonnet_model: "claude-sonnet-4-6"
43
+ default_haiku_model: "claude-haiku-4-5"
44
+ api_timeout_ms: 3000000
45
+
46
+ # 定时路由(可选):按时间自动切换,首个时间窗口命中生效
47
+ # schedule:
48
+ # - name: "night-shift"
49
+ # start: "22:00"
50
+ # end: "06:00"
51
+ # routes:
52
+ # - pattern: "^claude-sonnet"
53
+ # provider: zai
54
+ # model: "glm-5.1"
55
+
56
+ routes: # 从上到下匹配,首个命中生效
57
+ - pattern: "^claude-opus"
58
+ provider: zai
59
+ model: "glm-5.1" # 可选,发给 provider 的实际模型名
60
+ - pattern: "^claude-sonnet"
61
+ provider: minimax
62
+ model: "MiniMax-M3"
63
+ active_key: 2 # 可选:该路由从 minimax 的第 2 个 key 开始用(1 起始,默认 1)
64
+ - pattern: "^claude-haiku"
65
+ provider: minimax
66
+ model: "MiniMax-M3"
67
+ - pattern: ".*" # 兜底
68
+ provider: minimax
69
+ model: "MiniMax-M3"
70
+ ```
71
+
72
+ ## 配置字段说明
73
+
74
+ | 字段 | 说明 |
75
+ |------|------|
76
+ | `server.host/port` | 网关监听地址 |
77
+ | `providers.<name>.base_url` | Provider 的 API 地址 |
78
+ | `providers.<name>.api_keys` | API key 列表(一个或多个,互为 fallback);每项为 key 字符串或 `{key, note}`,`note` 是存在配置里的备注 |
79
+ | `providers.<name>.available_models` | 该 provider 的可用模型列表,首个条目作为路由省略 `model` 时的回退模型 |
80
+ | `claude_settings.*` | 写入 settings.json 的模型和超时配置 |
81
+ | `routes[].pattern` | 正则匹配请求中的 model 字段 |
82
+ | `routes[].provider` | 路由到的 provider 名称 |
83
+ | `routes[].model` | 可选,替换发给 provider 的模型名 |
84
+ | `routes[].active_key` | 可选,1 起始的 provider api_keys 序号,该路由的请求从哪个 key 开始(默认 1,即第一个 key) |
85
+ | `schedule[].name` | 定时规则名称 |
86
+ | `schedule[].start/end` | 时间窗口(HH:MM 格式,支持跨夜如 22:00-06:00) |
87
+ | `schedule[].routes` | 该时间窗口内生效的路由(格式同 `routes`) |
88
+
89
+ ## Key fallback
90
+
91
+ 同一上游有多个账号/key 时(例如多个 MiniMax 订阅),在 `api_keys` 里配
92
+ 多个 key,而不是拆成多个 provider。每项可以是纯 key 字符串,也可以写成
93
+ `{key, note}` 加一个存在配置里的备注(`flexgate status` 和 fallback 日志
94
+ 都会显示 note,方便分辨是哪个账号的 key):
95
+
96
+ - 每条路由有一个「当前 key」指针(路由上的 `active_key`,默认指向
97
+ provider 的第一个 key):该路由的请求从指针指向的 key 开始;key 失败
98
+ 时自动换用下一个 key(循环一圈),指针也随之自动前进——后续请求直接
99
+ 从能用的 key 开始。**所有 key 轮换一圈都失败时,请求返回最后一次的
100
+ 错误**。指针在运行时的前进只存在于网关进程内存中,不写回配置文件。
101
+ - 触发切换的条件:HTTP **401 / 402 / 403 / 429 / 500 / 502 / 503 / 529**
102
+ (key 失效、余额/额度耗尽、限流、平台过载)以及连接错误、超时。
103
+ 其他 4xx(如 400 请求格式错误)不会触发切换。
104
+ - 流式请求只有在上游返回非 200 状态码之前才能切换 key;一旦开始吐
105
+ token,响应已提交,无法再 fallback。
106
+ - 每次切换都会在服务日志中留下记录(key 只显示前后各 4 位)。
107
+ - 用 `flexgate status` 可以查看每个 provider 的 fallback 链、每条路由
108
+ 当前使用的 key 和每个 key 的实时用量;在 `flexgate config edit` 交互
109
+ 界面的「api keys」入口可以按路由查看各 key 的实时用量/有效性并切换
110
+ active key。
111
+ - 旧版 `api_key` + `fallback_keys` 写法仍然兼容,`flexgate update` 会
112
+ 自动迁移为 `api_keys` 列表(config_version 3 → 4);路由上的
113
+ `active_key` 字段在 config_version 5 引入,旧配置的每条路由默认指向
114
+ 第一个 key。
115
+
116
+ ## 环境变量
117
+
118
+ | 变量 | 默认值 | 说明 |
119
+ |------|--------|------|
120
+ | `FLEXGATE_CONFIG` | `~/.flexgate/config.yaml` | 覆盖配置文件路径 |
121
+
122
+ :::{warning}
123
+ `config.yaml` 已加入 `.gitignore`,不会被提交到 Git。如果 API 密钥曾经
124
+ 被推送到远程仓库,请立即轮换(rotate)该密钥。
125
+ :::
@@ -0,0 +1,62 @@
1
+ # 开发与发布
2
+
3
+ ## 项目结构
4
+
5
+ | 文件 | 作用 |
6
+ |------|------|
7
+ | `flexgate/cli.py` | argparse CLI;service 生命周期命令、前台 `run`/`check`、config/settings/sync 命令 |
8
+ | `flexgate/config.py` | dataclass 配置模型(`GatewayConfig`、`ProviderConfig`、`RouteConfig` 等),YAML 加载/保存 |
9
+ | `flexgate/router.py` | `resolve(config, model)` — 先定时路由后默认路由,首个正则命中生效 |
10
+ | `flexgate/proxy.py` | `handle_request()` — httpx 异步代理,per-key fallback 重试,SSE 流式 + JSON 透传 |
11
+ | `flexgate/usage.py` | `flexgate status` / `flexgate usage` 的用量查询 — 各平台适配器 + minimal chat probe 兜底 |
12
+ | `flexgate/server.py` | Starlette 应用,`POST /v1/messages` 端点,SIGUSR1 热重载 |
13
+ | `flexgate/service.py` | systemd 用户服务的安装/启停/重载/状态(持久化运行的唯一入口) |
14
+ | `flexgate/settings.py` | config.yaml ↔ `~/.claude/settings.json` 双向桥接 |
15
+ | `flexgate/sync.py` | `flexgate sync` — 通过 confsync 服务器加密同步整份配置 |
16
+ | `flexgate/migrate.py` | 配置 schema 版本化:逐级迁移链 + 备份 + 原子重写 |
17
+ | `flexgate/doctor.py` | `flexgate doctor` — 只读诊断 |
18
+ | `flexgate/update.py` | `flexgate update` — PyPI 版本检查、按安装方式升级、配置迁移、服务重载 |
19
+
20
+ ## 关键设计
21
+
22
+ - **正则路由**:路由按顺序匹配请求体中的 `model` 字段,首个命中生效,
23
+ 末尾用 `".*"` 兜底。
24
+ - **代理改写约定**:上游请求携带 provider 的 `x-api-key` 和固定头集;
25
+ 只有路由显式设置了 `model` 时才改写 JSON 里的 model 字段。流式响应按
26
+ 原始字节转发,不做解析。
27
+ - **Key fallback**:可重试错误(401/402/403/429/500/502/503/529、连接
28
+ 错误、超时)按 `api_keys` 顺序切换重试;流式请求只能在上游返回 200
29
+ 之前切换。
30
+ - **配置版本化**:`config_version` 标记 + `MIGRATIONS` 迁移链,升级时
31
+ 逐级迁移,先备份再原子重写。
32
+
33
+ ## 发布流程(维护者)
34
+
35
+ 仓库托管在 <https://github.com/Agony5757/flexible-gateway>,通过 GitHub
36
+ Actions 自动发布到 PyPI(trusted publishing,无需 API token):
37
+
38
+ ```bash
39
+ # 1. 修改 flexgate/__init__.py 中的 __version__(唯一版本来源)
40
+ # 2. 提交后打 tag,tag 必须与 __version__ 一致(CI 会校验)
41
+ git tag v0.4.0
42
+ git push origin main --tags
43
+ ```
44
+
45
+ 推送 `v*` tag 触发 `.github/workflows/release.yml`:校验 tag 与
46
+ `__version__` 一致 → 构建 sdist/wheel → 发布到 PyPI。首次发布前需在
47
+ PyPI 项目设置中配置 Trusted Publisher(repo:
48
+ `Agony5757/flexible-gateway`,workflow: `release.yml`)。
49
+
50
+ ## 文档构建
51
+
52
+ 文档使用 [Sphinx](https://www.sphinx-doc.org/) + [Furo](https://pradyunsg.me/furo/)
53
+ 主题编写,源文件在 `docs/` 目录(MyST Markdown):
54
+
55
+ ```bash
56
+ pip install -r docs/requirements.txt
57
+ cd docs && make html
58
+ # 打开 docs/_build/html/index.html
59
+ ```
60
+
61
+ 推送到 `main`(`docs/**` 变更)或打 `v*` tag 时,`.github/workflows/docs.yml`
62
+ 会自动构建并部署到 GitHub Pages。