flexgate 0.3.0__tar.gz → 0.4.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.
- flexgate-0.4.1/.github/workflows/docs.yml +67 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/.gitignore +3 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/CLAUDE.md +6 -2
- {flexgate-0.3.0 → flexgate-0.4.1}/PKG-INFO +65 -5
- {flexgate-0.3.0 → flexgate-0.4.1}/README.md +64 -4
- {flexgate-0.3.0 → flexgate-0.4.1}/config.yaml.template +17 -15
- flexgate-0.4.1/docs/Makefile +14 -0
- flexgate-0.4.1/docs/_static/.gitkeep +0 -0
- flexgate-0.4.1/docs/cli.md +229 -0
- flexgate-0.4.1/docs/conf.py +46 -0
- flexgate-0.4.1/docs/configuration.md +116 -0
- flexgate-0.4.1/docs/development.md +62 -0
- flexgate-0.4.1/docs/index.md +52 -0
- flexgate-0.4.1/docs/quickstart.md +53 -0
- flexgate-0.4.1/docs/requirements.txt +4 -0
- flexgate-0.4.1/docs/usage.md +43 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/__init__.py +1 -1
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/cli.py +108 -4
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/config.py +72 -19
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/doctor.py +4 -1
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/migrate.py +21 -2
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/proxy.py +73 -20
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/settings.py +2 -1
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/sync.py +20 -4
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/update.py +17 -0
- flexgate-0.4.1/flexgate/usage.py +364 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/.github/workflows/release.yml +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/__main__.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/guardian.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/healthcheck.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/main.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/router.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/server.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/flexgate/service.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/pyproject.toml +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/test.png +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/test_multimodal.py +0 -0
- {flexgate-0.3.0 → flexgate-0.4.1}/test_multimodal_gateway.py +0 -0
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
tags:
|
|
11
|
+
- 'v*'
|
|
12
|
+
pull_request:
|
|
13
|
+
branches: [main]
|
|
14
|
+
paths:
|
|
15
|
+
- 'docs/**'
|
|
16
|
+
- '.github/workflows/docs.yml'
|
|
17
|
+
workflow_dispatch:
|
|
18
|
+
|
|
19
|
+
permissions:
|
|
20
|
+
contents: read
|
|
21
|
+
pages: write
|
|
22
|
+
id-token: write
|
|
23
|
+
|
|
24
|
+
concurrency:
|
|
25
|
+
group: "pages"
|
|
26
|
+
cancel-in-progress: false
|
|
27
|
+
|
|
28
|
+
jobs:
|
|
29
|
+
build-docs:
|
|
30
|
+
name: Build Documentation
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
steps:
|
|
33
|
+
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
|
|
34
|
+
|
|
35
|
+
- name: Set up Python
|
|
36
|
+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
|
37
|
+
with:
|
|
38
|
+
python-version: '3.12'
|
|
39
|
+
|
|
40
|
+
- name: Install dependencies
|
|
41
|
+
run: |
|
|
42
|
+
python -m pip install --upgrade pip
|
|
43
|
+
pip install -r docs/requirements.txt
|
|
44
|
+
pip install .
|
|
45
|
+
|
|
46
|
+
- name: Build docs
|
|
47
|
+
run: |
|
|
48
|
+
cd docs
|
|
49
|
+
make html
|
|
50
|
+
|
|
51
|
+
- name: Upload artifact
|
|
52
|
+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1
|
|
53
|
+
with:
|
|
54
|
+
path: docs/_build/html
|
|
55
|
+
|
|
56
|
+
deploy-docs:
|
|
57
|
+
name: Deploy to GitHub Pages
|
|
58
|
+
environment:
|
|
59
|
+
name: github-pages
|
|
60
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
61
|
+
runs-on: ubuntu-latest
|
|
62
|
+
needs: build-docs
|
|
63
|
+
if: github.ref == 'refs/heads/main' || github.ref_type == 'tag'
|
|
64
|
+
steps:
|
|
65
|
+
- name: Deploy to GitHub Pages
|
|
66
|
+
id: deployment
|
|
67
|
+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5
|
|
@@ -22,6 +22,7 @@ flexgate service install # install + enable + start systemd u
|
|
|
22
22
|
flexgate service {install|start|stop|restart|reload|status|uninstall}
|
|
23
23
|
flexgate run # foreground/debug only
|
|
24
24
|
flexgate check # provider connectivity diagnostics
|
|
25
|
+
flexgate status # providers, fallback chains, per-key usage, active routes
|
|
25
26
|
|
|
26
27
|
# Config
|
|
27
28
|
flexgate config {init|show|set|path|edit}
|
|
@@ -83,7 +84,8 @@ Claude Code → POST /v1/messages (model="claude-sonnet-4-6")
|
|
|
83
84
|
| `cli.py` | argparse CLI; service lifecycle commands, foreground `run`/`check`, config/settings/sync commands |
|
|
84
85
|
| `config.py` | Pydantic-like dataclasses (`GatewayConfig`, `ProviderConfig`, `RouteConfig`, `ScheduleEntry`), YAML load/save, `TIER_PATTERNS` regex map |
|
|
85
86
|
| `router.py` | `resolve(config, model)` — schedule-first then default routes, first regex match wins |
|
|
86
|
-
| `proxy.py` | `handle_request()` — httpx async proxy, SSE streaming + JSON pass-through |
|
|
87
|
+
| `proxy.py` | `handle_request()` — httpx async proxy, per-key fallback retry loop, SSE streaming + JSON pass-through |
|
|
88
|
+
| `usage.py` | `flexgate status` usage queries — per-platform adapters (MiniMax coding_plan API, Kimi Code usages API, z.ai quota API, LiteLLM `/key/info`) plus a minimal chat probe fallback |
|
|
87
89
|
| `server.py` | Starlette app creation, `POST /v1/messages` endpoint, `SIGUSR1` lifespan reload |
|
|
88
90
|
| `main.py` | Thin bootstrap: load config → create app → run uvicorn |
|
|
89
91
|
| `service.py` | Authoritative systemd user-service install/start/stop/restart/reload/status and legacy PID migration |
|
|
@@ -93,13 +95,15 @@ Claude Code → POST /v1/messages (model="claude-sonnet-4-6")
|
|
|
93
95
|
| `sync.py` | `flexgate sync` — pushes/pulls the whole config.yaml as an encrypted document on a confsync server (lazily imports the `confsync` client package) |
|
|
94
96
|
| `migrate.py` | Config schema versioning: `config_version` marker, per-step `MIGRATIONS` chain (N → N+1), backup + atomic rewrite |
|
|
95
97
|
| `doctor.py` | `flexgate doctor` — read-only diagnostics (Python, PyPI update, config schema/semantics, port, systemd, Claude settings) |
|
|
96
|
-
| `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` |
|
|
98
|
+
| `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` |
|
|
97
99
|
|
|
98
100
|
### Key design points
|
|
99
101
|
|
|
100
102
|
- **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.
|
|
101
103
|
- **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.
|
|
102
104
|
- **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.
|
|
103
107
|
- **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.
|
|
104
108
|
- **Schedule-based overrides**: Optional time windows (e.g. 22:00-06:00) override default routes. Overnight wrap is supported.
|
|
105
109
|
- **Service-first lifecycle**: `flexgate.service` is the sole persistent runtime. systemd owns restart, boot startup, logs, and process state.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: flexgate
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.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
|
+
[](https://pypi.org/project/flexgate/)
|
|
21
|
+
[](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。
|
|
@@ -154,6 +159,34 @@ Trusted Publisher(repo: `Agony5757/flexible-gateway`,workflow: `release.yml`
|
|
|
154
159
|
|
|
155
160
|
可通过 `--verify-timeout N` 调整每个 provider 的超时时间(默认 15 秒)。
|
|
156
161
|
|
|
162
|
+
### 状态总览与用量查询(`flexgate status` / `flexgate usage`)
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
flexgate status # providers + fallback 链 + 每个 key 的用量 + 当前路由
|
|
166
|
+
flexgate status --no-usage # 跳过用量查询,只看配置
|
|
167
|
+
flexgate status --usage-timeout 30
|
|
168
|
+
flexgate usage # 只看每个 key 的用量/额度(不打印配置和路由)
|
|
169
|
+
flexgate usage --usage-timeout 30
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`flexgate status` 展示当前配置中的所有 provider、每个 provider 的 key 及
|
|
173
|
+
fallback 链、当前生效的路由,并逐一查询**每个 key** 的用量/额度;
|
|
174
|
+
`flexgate usage` 只输出其中的用量部分。
|
|
175
|
+
|
|
176
|
+
各平台的用量查询方式差异很大,flexgate 按 `base_url` 自动选择适配器:
|
|
177
|
+
|
|
178
|
+
| 平台 | 查询方式 | 说明 |
|
|
179
|
+
|------|----------|------|
|
|
180
|
+
| MiniMax(api.minimaxi.com / api.minimax.io) | `GET /v1/api/openplatform/coding_plan/remains`(Bearer 认证,复用 provider key) | 官方 token plan 接口,返回 5 小时窗口和周窗口的**剩余**次数(注意响应里 `*_usage_count` 字段实际是剩余量) |
|
|
181
|
+
| Kimi Code(api.kimi.com) | `GET {base}/v1/usages`(Bearer 认证,复用 provider key) | 未公开文档、与 Kimi Code CLI `/usage` 相同的接口,返回周配额、5 小时滚动窗口剩余量和并发上限 |
|
|
182
|
+
| z.ai / 智谱(api.z.ai / open.bigmodel.cn) | `GET /api/monitor/usage/quota/limit`(Authorization 直接带 key) | 未公开文档、但 z.ai 官方 coding 插件在用的接口,返回各窗口已用百分比与重置时间 |
|
|
183
|
+
| USTC(api.llm.ustc.edu.cn,LiteLLM) | `GET /key/info`(Bearer 认证) | LiteLLM proxy 自带的 key 信息接口,返回 spend / max_budget / 限速等 |
|
|
184
|
+
| 小米 MiMo(token-plan-cn.xiaomimimo.com) | **无 key 可用的官方接口**(控制台内部接口需要浏览器 cookie,不采用) | 退化为 minimal probe |
|
|
185
|
+
| 其他/未知平台 | minimal chat probe | 发一条输入 `"hi"`、`max_tokens=128` 的最小 `/v1/messages` 请求,验证 key 是否还能正常服务(会消耗极少量额度) |
|
|
186
|
+
|
|
187
|
+
如果某平台的专用接口调用失败,flexgate 会自动退化为 minimal probe 再试一次。
|
|
188
|
+
用量查询接口多为平台内部接口,可能随时变动;查询结果仅供参考。
|
|
189
|
+
|
|
157
190
|
### 前台调试与连通性检查
|
|
158
191
|
|
|
159
192
|
`run` / `check` 是独立的顶层调试命令,不属于持久化服务模式:
|
|
@@ -220,7 +253,7 @@ Flexgate config — ~/.flexgate/config.yaml
|
|
|
220
253
|
|
|
221
254
|
- 方向键选中某个 tier(opus/sonnet/haiku),回车进入:先从候选 **provider** 列表选择,再从该 provider 的候选 **model** 列表选择。
|
|
222
255
|
- model 列表包含:`available_models` 中的各个模型、「使用 provider 默认(首个可用模型,不写死 model)」、以及「自定义模型…」(手动输入)。
|
|
223
|
-
- 按 `s` 保存(并向运行中的网关发送 SIGUSR1
|
|
256
|
+
- 按 `s` 保存(并向运行中的网关发送 SIGUSR1 热重载,**无需重启即生效**),按 `q` 退出(有未保存改动时会询问 "Config changed — activate now?":选 Yes 立即保存并热重载生效,选 No 放弃改动);子菜单中按 `Esc`/`←` 返回上一级。
|
|
224
257
|
- 需要交互式终端(TTY);非交互场景请改用 `flexgate config set`。
|
|
225
258
|
|
|
226
259
|
### Settings 管理
|
|
@@ -261,10 +294,16 @@ server:
|
|
|
261
294
|
providers:
|
|
262
295
|
zai:
|
|
263
296
|
base_url: "https://api.z.ai/api/anthropic"
|
|
264
|
-
|
|
297
|
+
api_keys:
|
|
298
|
+
- "your-zai-api-key"
|
|
265
299
|
minimax:
|
|
266
300
|
base_url: "https://api.minimaxi.com/anthropic"
|
|
267
|
-
|
|
301
|
+
api_keys: # 同一上游可配多个 key,互为 fallback
|
|
302
|
+
- key: "your-minimax-api-key"
|
|
303
|
+
note: "主账号" # 可选备注,存在配置里,status/日志中显示
|
|
304
|
+
- key: "your-minimax-api-key-2"
|
|
305
|
+
note: "备用账号"
|
|
306
|
+
- "your-minimax-api-key-3" # 纯字符串写法(无备注)
|
|
268
307
|
|
|
269
308
|
claude_settings:
|
|
270
309
|
default_opus_model: "claude-opus-4-7"
|
|
@@ -303,7 +342,8 @@ routes: # 从上到下匹配,首个命中生效
|
|
|
303
342
|
|------|------|
|
|
304
343
|
| `server.host/port` | 网关监听地址 |
|
|
305
344
|
| `providers.<name>.base_url` | Provider 的 API 地址 |
|
|
306
|
-
| `providers.<name>.
|
|
345
|
+
| `providers.<name>.api_keys` | API key 列表(一个或多个,互为 fallback);每项为 key 字符串或 `{key, note}`,`note` 是存在配置里的备注 |
|
|
346
|
+
| `providers.<name>.available_models` | 该 provider 的可用模型列表,首个条目作为路由省略 `model` 时的回退模型 |
|
|
307
347
|
| `claude_settings.*` | 写入 settings.json 的模型和超时配置 |
|
|
308
348
|
| `routes[].pattern` | 正则匹配请求中的 model 字段 |
|
|
309
349
|
| `routes[].provider` | 路由到的 provider 名称 |
|
|
@@ -312,6 +352,26 @@ routes: # 从上到下匹配,首个命中生效
|
|
|
312
352
|
| `schedule[].start/end` | 时间窗口(HH:MM 格式,支持跨夜如 22:00-06:00) |
|
|
313
353
|
| `schedule[].routes` | 该时间窗口内生效的路由(格式同 `routes`) |
|
|
314
354
|
|
|
355
|
+
### Key fallback
|
|
356
|
+
|
|
357
|
+
同一上游有多个账号/key 时(例如多个 MiniMax 订阅),在 `api_keys` 里配
|
|
358
|
+
多个 key,而不是拆成多个 provider。每项可以是纯 key 字符串,也可以写成
|
|
359
|
+
`{key, note}` 加一个存在配置里的备注(`flexgate status` 和 fallback 日志
|
|
360
|
+
都会显示 note,方便分辨是哪个账号的 key):
|
|
361
|
+
|
|
362
|
+
- 请求先走第一个 key;失败时按列表顺序自动重试后续 key,直到某个 key
|
|
363
|
+
成功或返回不可重试的错误。
|
|
364
|
+
- 触发切换的条件:HTTP **401 / 402 / 403 / 429 / 500 / 502 / 503 / 529**
|
|
365
|
+
(key 失效、余额/额度耗尽、限流、平台过载)以及连接错误、超时。
|
|
366
|
+
其他 4xx(如 400 请求格式错误)不会触发切换。
|
|
367
|
+
- 流式请求只有在上游返回非 200 状态码之前才能切换 key;一旦开始吐
|
|
368
|
+
token,响应已提交,无法再 fallback。
|
|
369
|
+
- 每次切换都会在服务日志中留下记录(key 只显示前后各 4 位)。
|
|
370
|
+
- 用 `flexgate status` 可以查看每个 provider 的 fallback 链和每个 key 的
|
|
371
|
+
实时用量。
|
|
372
|
+
- 旧版 `api_key` + `fallback_keys` 写法仍然兼容,`flexgate update` 会
|
|
373
|
+
自动迁移为 `api_keys` 列表(config_version 3 → 4)。
|
|
374
|
+
|
|
315
375
|
## Settings Import
|
|
316
376
|
|
|
317
377
|
`flexgate settings import` 会扫描 `~/.claude/settings.json*`,从每个文件中提取 `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_AUTH_TOKEN`,自动写入 config.yaml 的 providers 部分。
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
# Flexgate
|
|
2
2
|
|
|
3
|
+
[](https://pypi.org/project/flexgate/)
|
|
4
|
+
[](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。
|
|
@@ -137,6 +142,34 @@ Trusted Publisher(repo: `Agony5757/flexible-gateway`,workflow: `release.yml`
|
|
|
137
142
|
|
|
138
143
|
可通过 `--verify-timeout N` 调整每个 provider 的超时时间(默认 15 秒)。
|
|
139
144
|
|
|
145
|
+
### 状态总览与用量查询(`flexgate status` / `flexgate usage`)
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
flexgate status # providers + fallback 链 + 每个 key 的用量 + 当前路由
|
|
149
|
+
flexgate status --no-usage # 跳过用量查询,只看配置
|
|
150
|
+
flexgate status --usage-timeout 30
|
|
151
|
+
flexgate usage # 只看每个 key 的用量/额度(不打印配置和路由)
|
|
152
|
+
flexgate usage --usage-timeout 30
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`flexgate status` 展示当前配置中的所有 provider、每个 provider 的 key 及
|
|
156
|
+
fallback 链、当前生效的路由,并逐一查询**每个 key** 的用量/额度;
|
|
157
|
+
`flexgate usage` 只输出其中的用量部分。
|
|
158
|
+
|
|
159
|
+
各平台的用量查询方式差异很大,flexgate 按 `base_url` 自动选择适配器:
|
|
160
|
+
|
|
161
|
+
| 平台 | 查询方式 | 说明 |
|
|
162
|
+
|------|----------|------|
|
|
163
|
+
| MiniMax(api.minimaxi.com / api.minimax.io) | `GET /v1/api/openplatform/coding_plan/remains`(Bearer 认证,复用 provider key) | 官方 token plan 接口,返回 5 小时窗口和周窗口的**剩余**次数(注意响应里 `*_usage_count` 字段实际是剩余量) |
|
|
164
|
+
| Kimi Code(api.kimi.com) | `GET {base}/v1/usages`(Bearer 认证,复用 provider key) | 未公开文档、与 Kimi Code CLI `/usage` 相同的接口,返回周配额、5 小时滚动窗口剩余量和并发上限 |
|
|
165
|
+
| z.ai / 智谱(api.z.ai / open.bigmodel.cn) | `GET /api/monitor/usage/quota/limit`(Authorization 直接带 key) | 未公开文档、但 z.ai 官方 coding 插件在用的接口,返回各窗口已用百分比与重置时间 |
|
|
166
|
+
| USTC(api.llm.ustc.edu.cn,LiteLLM) | `GET /key/info`(Bearer 认证) | LiteLLM proxy 自带的 key 信息接口,返回 spend / max_budget / 限速等 |
|
|
167
|
+
| 小米 MiMo(token-plan-cn.xiaomimimo.com) | **无 key 可用的官方接口**(控制台内部接口需要浏览器 cookie,不采用) | 退化为 minimal probe |
|
|
168
|
+
| 其他/未知平台 | minimal chat probe | 发一条输入 `"hi"`、`max_tokens=128` 的最小 `/v1/messages` 请求,验证 key 是否还能正常服务(会消耗极少量额度) |
|
|
169
|
+
|
|
170
|
+
如果某平台的专用接口调用失败,flexgate 会自动退化为 minimal probe 再试一次。
|
|
171
|
+
用量查询接口多为平台内部接口,可能随时变动;查询结果仅供参考。
|
|
172
|
+
|
|
140
173
|
### 前台调试与连通性检查
|
|
141
174
|
|
|
142
175
|
`run` / `check` 是独立的顶层调试命令,不属于持久化服务模式:
|
|
@@ -203,7 +236,7 @@ Flexgate config — ~/.flexgate/config.yaml
|
|
|
203
236
|
|
|
204
237
|
- 方向键选中某个 tier(opus/sonnet/haiku),回车进入:先从候选 **provider** 列表选择,再从该 provider 的候选 **model** 列表选择。
|
|
205
238
|
- model 列表包含:`available_models` 中的各个模型、「使用 provider 默认(首个可用模型,不写死 model)」、以及「自定义模型…」(手动输入)。
|
|
206
|
-
- 按 `s` 保存(并向运行中的网关发送 SIGUSR1
|
|
239
|
+
- 按 `s` 保存(并向运行中的网关发送 SIGUSR1 热重载,**无需重启即生效**),按 `q` 退出(有未保存改动时会询问 "Config changed — activate now?":选 Yes 立即保存并热重载生效,选 No 放弃改动);子菜单中按 `Esc`/`←` 返回上一级。
|
|
207
240
|
- 需要交互式终端(TTY);非交互场景请改用 `flexgate config set`。
|
|
208
241
|
|
|
209
242
|
### Settings 管理
|
|
@@ -244,10 +277,16 @@ server:
|
|
|
244
277
|
providers:
|
|
245
278
|
zai:
|
|
246
279
|
base_url: "https://api.z.ai/api/anthropic"
|
|
247
|
-
|
|
280
|
+
api_keys:
|
|
281
|
+
- "your-zai-api-key"
|
|
248
282
|
minimax:
|
|
249
283
|
base_url: "https://api.minimaxi.com/anthropic"
|
|
250
|
-
|
|
284
|
+
api_keys: # 同一上游可配多个 key,互为 fallback
|
|
285
|
+
- key: "your-minimax-api-key"
|
|
286
|
+
note: "主账号" # 可选备注,存在配置里,status/日志中显示
|
|
287
|
+
- key: "your-minimax-api-key-2"
|
|
288
|
+
note: "备用账号"
|
|
289
|
+
- "your-minimax-api-key-3" # 纯字符串写法(无备注)
|
|
251
290
|
|
|
252
291
|
claude_settings:
|
|
253
292
|
default_opus_model: "claude-opus-4-7"
|
|
@@ -286,7 +325,8 @@ routes: # 从上到下匹配,首个命中生效
|
|
|
286
325
|
|------|------|
|
|
287
326
|
| `server.host/port` | 网关监听地址 |
|
|
288
327
|
| `providers.<name>.base_url` | Provider 的 API 地址 |
|
|
289
|
-
| `providers.<name>.
|
|
328
|
+
| `providers.<name>.api_keys` | API key 列表(一个或多个,互为 fallback);每项为 key 字符串或 `{key, note}`,`note` 是存在配置里的备注 |
|
|
329
|
+
| `providers.<name>.available_models` | 该 provider 的可用模型列表,首个条目作为路由省略 `model` 时的回退模型 |
|
|
290
330
|
| `claude_settings.*` | 写入 settings.json 的模型和超时配置 |
|
|
291
331
|
| `routes[].pattern` | 正则匹配请求中的 model 字段 |
|
|
292
332
|
| `routes[].provider` | 路由到的 provider 名称 |
|
|
@@ -295,6 +335,26 @@ routes: # 从上到下匹配,首个命中生效
|
|
|
295
335
|
| `schedule[].start/end` | 时间窗口(HH:MM 格式,支持跨夜如 22:00-06:00) |
|
|
296
336
|
| `schedule[].routes` | 该时间窗口内生效的路由(格式同 `routes`) |
|
|
297
337
|
|
|
338
|
+
### Key fallback
|
|
339
|
+
|
|
340
|
+
同一上游有多个账号/key 时(例如多个 MiniMax 订阅),在 `api_keys` 里配
|
|
341
|
+
多个 key,而不是拆成多个 provider。每项可以是纯 key 字符串,也可以写成
|
|
342
|
+
`{key, note}` 加一个存在配置里的备注(`flexgate status` 和 fallback 日志
|
|
343
|
+
都会显示 note,方便分辨是哪个账号的 key):
|
|
344
|
+
|
|
345
|
+
- 请求先走第一个 key;失败时按列表顺序自动重试后续 key,直到某个 key
|
|
346
|
+
成功或返回不可重试的错误。
|
|
347
|
+
- 触发切换的条件:HTTP **401 / 402 / 403 / 429 / 500 / 502 / 503 / 529**
|
|
348
|
+
(key 失效、余额/额度耗尽、限流、平台过载)以及连接错误、超时。
|
|
349
|
+
其他 4xx(如 400 请求格式错误)不会触发切换。
|
|
350
|
+
- 流式请求只有在上游返回非 200 状态码之前才能切换 key;一旦开始吐
|
|
351
|
+
token,响应已提交,无法再 fallback。
|
|
352
|
+
- 每次切换都会在服务日志中留下记录(key 只显示前后各 4 位)。
|
|
353
|
+
- 用 `flexgate status` 可以查看每个 provider 的 fallback 链和每个 key 的
|
|
354
|
+
实时用量。
|
|
355
|
+
- 旧版 `api_key` + `fallback_keys` 写法仍然兼容,`flexgate update` 会
|
|
356
|
+
自动迁移为 `api_keys` 列表(config_version 3 → 4)。
|
|
357
|
+
|
|
298
358
|
## Settings Import
|
|
299
359
|
|
|
300
360
|
`flexgate settings import` 会扫描 `~/.claude/settings.json*`,从每个文件中提取 `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_AUTH_TOKEN`,自动写入 config.yaml 的 providers 部分。
|
|
@@ -3,37 +3,39 @@ server:
|
|
|
3
3
|
port: 8765
|
|
4
4
|
|
|
5
5
|
# 每个 provider 必须声明 available_models,首个条目作为路由省略 model 时的回退模型。
|
|
6
|
-
#
|
|
6
|
+
# api_keys:同一上游的一个或多个 API key。每项可以是纯 key 字符串,也可以
|
|
7
|
+
# 写成 {key, note},note 是存在配置里的备注(flexgate status 和 fallback
|
|
8
|
+
# 日志都会显示)。某个 key 遇到 401/402/403/429/5xx/529 或连接错误(如额度
|
|
9
|
+
# 用尽、平台过载)时,按顺序自动切换到下一个 key 重试。同一上游的多个账号
|
|
10
|
+
# 应这样配置,而不是拆成多个 provider。
|
|
7
11
|
providers:
|
|
8
12
|
minimax:
|
|
9
13
|
base_url: "https://api.minimaxi.com/anthropic"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
available_models:
|
|
17
|
-
- "MiniMax-M3"
|
|
18
|
-
minimax-ywj:
|
|
19
|
-
base_url: "https://api.minimaxi.com/anthropic"
|
|
20
|
-
api_key: "your-minimax-ywj-api-key"
|
|
14
|
+
api_keys:
|
|
15
|
+
- key: "your-minimax-api-key"
|
|
16
|
+
note: "主账号"
|
|
17
|
+
- key: "your-minimax-api-key-2"
|
|
18
|
+
note: "备用账号"
|
|
19
|
+
- "your-minimax-api-key-3" # 纯字符串写法(无备注)
|
|
21
20
|
available_models:
|
|
22
21
|
- "MiniMax-M3"
|
|
23
22
|
zai:
|
|
24
23
|
base_url: "https://api.z.ai/api/anthropic"
|
|
25
|
-
|
|
24
|
+
api_keys:
|
|
25
|
+
- "your-zai-api-key"
|
|
26
26
|
available_models:
|
|
27
27
|
- "glm-5.3" # 路由省略 model 时的回退模型
|
|
28
28
|
- "glm-4.6v"
|
|
29
29
|
xiaomi:
|
|
30
30
|
base_url: "https://token-plan-cn.xiaomimimo.com/anthropic"
|
|
31
|
-
|
|
31
|
+
api_keys:
|
|
32
|
+
- "your-xiaomi-api-key"
|
|
32
33
|
available_models:
|
|
33
34
|
- "mimo-v2.5-pro"
|
|
34
35
|
ustc:
|
|
35
36
|
base_url: "https://api.llm.ustc.edu.cn"
|
|
36
|
-
|
|
37
|
+
api_keys:
|
|
38
|
+
- "your-ustc-api-key"
|
|
37
39
|
available_models:
|
|
38
40
|
- "deepseek-v4-pro"
|
|
39
41
|
|
|
@@ -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,229 @@
|
|
|
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 tier · s save · q quit
|
|
132
|
+
|
|
133
|
+
▶ opus ustc / deepseek-v4-pro
|
|
134
|
+
sonnet ustc / deepseek-v4-pro
|
|
135
|
+
haiku ustc / deepseek-v4-pro
|
|
136
|
+
|
|
137
|
+
○ no unsaved changes
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
- 方向键选中某个 tier(opus/sonnet/haiku),回车进入:先从候选
|
|
141
|
+
**provider** 列表选择,再从该 provider 的候选 **model** 列表选择。
|
|
142
|
+
- model 列表包含:`available_models` 中的各个模型、「使用 provider 默认
|
|
143
|
+
(首个可用模型,不写死 model)」、以及「自定义模型…」(手动输入)。
|
|
144
|
+
- 按 `s` 保存(并向运行中的网关发送 SIGUSR1 热重载,**无需重启即生效**),
|
|
145
|
+
按 `q` 退出(有未保存改动时会询问 "Config changed — activate now?":
|
|
146
|
+
选 Yes 立即保存并热重载生效,选 No 放弃改动);子菜单中按 `Esc`/`←`
|
|
147
|
+
返回上一级。
|
|
148
|
+
- 需要交互式终端(TTY);非交互场景请改用 `flexgate config set`。
|
|
149
|
+
|
|
150
|
+
## Settings 管理
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
flexgate settings import # 从 ~/.claude/settings.json* 导入凭证到 config.yaml
|
|
154
|
+
flexgate settings apply # 将 config.yaml 配置写入 ~/.claude/settings.json
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**import** 会扫描 `~/.claude/settings.json*`,从每个文件中提取
|
|
158
|
+
`ANTHROPIC_BASE_URL` 和 `ANTHROPIC_AUTH_TOKEN`,自动写入 config.yaml 的
|
|
159
|
+
providers 部分。文件名与 provider 名称的映射规则:
|
|
160
|
+
|
|
161
|
+
- `settings.json` → 根据域名自动推断(如含 `z.ai` → `zai`)
|
|
162
|
+
- `settings.json.zai` → provider 名 `zai`
|
|
163
|
+
- `settings.json.bak.*` → 跳过(备份文件)
|
|
164
|
+
|
|
165
|
+
**apply** 会读取 config.yaml 中的 `server` 和 `claude_settings`,备份当前
|
|
166
|
+
`~/.claude/settings.json` 为 `settings.json.bak.{timestamp}`,然后生成新的
|
|
167
|
+
settings.json,将 `ANTHROPIC_BASE_URL` 指向本地网关,并保留原有的
|
|
168
|
+
`permissions` 等非 env 字段:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"env": {
|
|
173
|
+
"ANTHROPIC_BASE_URL": "http://127.0.0.1:8765",
|
|
174
|
+
"ANTHROPIC_AUTH_TOKEN": "gateway",
|
|
175
|
+
"API_TIMEOUT_MS": "3000000",
|
|
176
|
+
"ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-4-7",
|
|
177
|
+
"ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
|
|
178
|
+
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5"
|
|
179
|
+
},
|
|
180
|
+
"permissions": {
|
|
181
|
+
"defaultMode": "bypassPermissions"
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
`ANTHROPIC_AUTH_TOKEN` 值任意但不能为空,网关会替换为对应 provider 的 key。
|
|
187
|
+
|
|
188
|
+
## 配置同步(sync)
|
|
189
|
+
|
|
190
|
+
`flexgate sync` 把整个 `config.yaml` 作为一份**加密文档**,通过 confsync
|
|
191
|
+
服务器在多机之间同步(使用共享的 confsync 凭证,配置文件中不再保存
|
|
192
|
+
confsync 配置段):
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
flexgate sync # 默认 pull:下载并合并远端配置
|
|
196
|
+
flexgate sync pull # 同上
|
|
197
|
+
flexgate sync push # 上传本地 config.yaml
|
|
198
|
+
flexgate sync --dry-run # 只显示哪些 key 会变化,不写配置
|
|
199
|
+
flexgate sync --full # 用远端文档整体替换本地配置(先备份)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
更多细节见 `flexgate help`。
|
|
203
|
+
|
|
204
|
+
## 版本与升级
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
flexgate --version # 打印版本号
|
|
208
|
+
flexgate doctor # 只读体检:Python、PyPI 新版、配置 schema、端口、systemd、Claude settings
|
|
209
|
+
flexgate doctor --offline # 跳过 PyPI 检查
|
|
210
|
+
flexgate update # 一键升级:pip/pipx/uv 升级包 + 迁移配置 schema + 热重载服务
|
|
211
|
+
flexgate update --check # 只报告将要做什么,不改动
|
|
212
|
+
flexgate update --config-only # 只迁移配置,不升级包
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
升级策略:
|
|
216
|
+
|
|
217
|
+
- **包升级**:版本号唯一来源是 `flexgate/__init__.py`;发布到 PyPI 后,
|
|
218
|
+
`flexgate update` 自动检测安装方式(pipx / uv tool / pip)并升级到最新
|
|
219
|
+
release。
|
|
220
|
+
- **新版本自动提示**:裸 `flexgate` 和 `flexgate service status` 会自动
|
|
221
|
+
比对 PyPI 上的最新版本,有新版时打印一行升级提示。检查结果缓存在
|
|
222
|
+
`~/.flexgate/update-check.json`,每 24 小时最多访问一次 PyPI,离线时
|
|
223
|
+
静默跳过。
|
|
224
|
+
- **配置迁移**:`config.yaml` 带 `config_version` 标记。每次 schema 变化
|
|
225
|
+
在 `flexgate/migrate.py` 的 `MIGRATIONS` 中登记一条 N → N+1 规则,升级
|
|
226
|
+
时逐级走完整个迁移链。迁移前自动备份为 `config.yaml.bak-<时间戳>`;
|
|
227
|
+
配置比当前 flexgate 更新时会被拒绝并提示先升级。
|
|
228
|
+
- **自检**:发版或排障时跑 `flexgate doctor`,有 FAIL 项时退出码为 1,
|
|
229
|
+
可直接用于 CI 门禁。
|