flexgate 0.11.1__tar.gz → 0.11.2__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 (41) hide show
  1. {flexgate-0.11.1 → flexgate-0.11.2}/PKG-INFO +1 -1
  2. {flexgate-0.11.1 → flexgate-0.11.2}/docs/cli.md +5 -1
  3. {flexgate-0.11.1 → flexgate-0.11.2}/docs/development.md +1 -1
  4. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/__init__.py +1 -1
  5. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/server.py +60 -5
  6. {flexgate-0.11.1 → flexgate-0.11.2}/.github/workflows/docs.yml +0 -0
  7. {flexgate-0.11.1 → flexgate-0.11.2}/.github/workflows/release.yml +0 -0
  8. {flexgate-0.11.1 → flexgate-0.11.2}/.gitignore +0 -0
  9. {flexgate-0.11.1 → flexgate-0.11.2}/CLAUDE.md +0 -0
  10. {flexgate-0.11.1 → flexgate-0.11.2}/README.md +0 -0
  11. {flexgate-0.11.1 → flexgate-0.11.2}/config.yaml.template +0 -0
  12. {flexgate-0.11.1 → flexgate-0.11.2}/docs/Makefile +0 -0
  13. {flexgate-0.11.1 → flexgate-0.11.2}/docs/_static/.gitkeep +0 -0
  14. {flexgate-0.11.1 → flexgate-0.11.2}/docs/api.md +0 -0
  15. {flexgate-0.11.1 → flexgate-0.11.2}/docs/conf.py +0 -0
  16. {flexgate-0.11.1 → flexgate-0.11.2}/docs/configuration.md +0 -0
  17. {flexgate-0.11.1 → flexgate-0.11.2}/docs/index.md +0 -0
  18. {flexgate-0.11.1 → flexgate-0.11.2}/docs/quickstart.md +0 -0
  19. {flexgate-0.11.1 → flexgate-0.11.2}/docs/requirements.txt +0 -0
  20. {flexgate-0.11.1 → flexgate-0.11.2}/docs/usage.md +0 -0
  21. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/__main__.py +0 -0
  22. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/cli.py +0 -0
  23. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/config.py +0 -0
  24. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/doctor.py +0 -0
  25. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/fallback.py +0 -0
  26. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/healthcheck.py +0 -0
  27. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/images.py +0 -0
  28. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/main.py +0 -0
  29. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/migrate.py +0 -0
  30. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/proxy.py +0 -0
  31. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/router.py +0 -0
  32. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/service.py +0 -0
  33. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/settings.py +0 -0
  34. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/sync.py +0 -0
  35. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/ui.py +0 -0
  36. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/update.py +0 -0
  37. {flexgate-0.11.1 → flexgate-0.11.2}/flexgate/usage.py +0 -0
  38. {flexgate-0.11.1 → flexgate-0.11.2}/pyproject.toml +0 -0
  39. {flexgate-0.11.1 → flexgate-0.11.2}/test.png +0 -0
  40. {flexgate-0.11.1 → flexgate-0.11.2}/test_multimodal.py +0 -0
  41. {flexgate-0.11.1 → flexgate-0.11.2}/test_multimodal_gateway.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: flexgate
3
- Version: 0.11.1
3
+ Version: 0.11.2
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
@@ -72,6 +72,9 @@ flexgate service uninstall # 停止、禁用并删除 unit
72
72
  - `service reload` 和 `config set/edit` 会在仅路由变化时发送 SIGUSR1;
73
73
  如果 endpoint 变化,则先检查再 restart。若只改 host、仍复用当前 port,
74
74
  为避免误停服务会要求先执行 `service stop`,再执行 `service start`。
75
+ - 网关进程自身每 2 秒监视 config.yaml:无论文件被谁修改(`flexgate config`、
76
+ 编辑器、`flexgate sync pull`),路由变更都会在数秒内自动热重载生效,
77
+ 不依赖编辑端能否触达 systemd;解析失败的半成品文件保留旧配置继续服务。
75
78
  - 查看日志:`flexgate log`(`-f` 跟随、`--since`/`--grep` 过滤、`-r` 只看路由决策行)。
76
79
 
77
80
  ## 状态总览与用量查询
@@ -211,7 +214,8 @@ Flexgate config — ~/.flexgate/config.yaml
211
214
  (与 `flexgate status` 相同),并标注当前 active 的 key。
212
215
  - model 列表包含:`available_models` 中的各个模型、「使用 provider 默认
213
216
  (首个可用模型,不写死 model)」、以及「自定义模型…」(手动输入)。
214
- - 按 `s` 保存(并向运行中的网关发送 SIGUSR1 热重载,**无需重启即生效**),
217
+ - 按 `s` 保存(**无需重启即生效**:网关监视配置文件,数秒内自动热重载;
218
+ SIGUSR1 仍可用作立即触发),
215
219
  按 `q` 退出(有未保存改动时会询问 "Config changed — activate now?":
216
220
  选 Yes 立即保存并热重载生效,选 No 放弃改动);子菜单中按 `Esc`/`←`
217
221
  返回上一级。
@@ -10,7 +10,7 @@
10
10
  | `flexgate/router.py` | `resolve(config, model)` — 先定时路由后默认路由,首个正则命中生效;匹配前归一化模型别名(`sonnet`/`claude-3-7-sonnet-latest` → `^claude-<tier>`) |
11
11
  | `flexgate/proxy.py` | `handle_request()` — httpx 异步代理,per-key fallback 重试,SSE 流式 + JSON 透传;记录每个请求由哪把 key 服务 |
12
12
  | `flexgate/usage.py` | `flexgate status` / `flexgate usage` 的用量查询 — 各平台适配器 + minimal chat probe 兜底 + 查询失败缓存(`usage --force` 强制重查) |
13
- | `flexgate/server.py` | Starlette 应用,`POST /v1/messages` + `POST /v1/images/{generations,edits}` 端点,SIGUSR1 热重载 |
13
+ | `flexgate/server.py` | Starlette 应用,`POST /v1/messages` + `POST /v1/images/{generations,edits}` 端点,SIGUSR1 热重载 + 配置文件 watcher 自动重载 |
14
14
  | `flexgate/images.py` | OpenAI Images API → MiniMax `/v1/image_generation` 翻译(模型名映射、size 钳制、`b64_json` 透传;edits 返回 501) |
15
15
  | `flexgate/fallback.py` | 兜底路由:所有未服务路径返回 Anthropic/OpenAI 双兼容的结构化 JSON 错误(404 `route_not_found` / 405 / 501 `endpoint_not_supported`) |
16
16
  | `flexgate/main.py` | 启动引导:加载配置 → 创建 app → 跑 uvicorn |
@@ -1,3 +1,3 @@
1
1
  """Flexgate — Flexible API Gateway for Claude Code."""
2
2
 
3
- __version__ = "0.11.1"
3
+ __version__ = "0.11.2"
@@ -1,7 +1,9 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import asyncio
3
4
  import json
4
5
  import logging
6
+ import os
5
7
  import signal
6
8
  import time
7
9
  from collections.abc import AsyncGenerator
@@ -62,6 +64,56 @@ async def messages(request: Request) -> JSONResponse:
62
64
  return resp
63
65
 
64
66
 
67
+ def _load_new_config(config_path: str) -> GatewayConfig | None:
68
+ try:
69
+ return load_config(config_path)
70
+ except Exception as e:
71
+ logger.error("Failed to reload config: %s", e)
72
+ return None
73
+
74
+
75
+ # The gateway watches its config file so edits take effect within seconds no
76
+ # matter how the file was changed (flexgate config, an editor, sync). The
77
+ # CLI-side SIGUSR1 reload depends on the editing process reaching the systemd
78
+ # user bus, which is not always available (SSH sessions, agent shells).
79
+ CONFIG_WATCH_INTERVAL = 2.0
80
+
81
+
82
+ async def _watch_config(app, config_path: str | None) -> None:
83
+ last_mtime: float | None = None
84
+ if config_path:
85
+ try:
86
+ last_mtime = os.path.getmtime(config_path)
87
+ except OSError:
88
+ pass
89
+ while True:
90
+ await asyncio.sleep(CONFIG_WATCH_INTERVAL)
91
+ try:
92
+ if not config_path:
93
+ continue
94
+ try:
95
+ mtime = os.path.getmtime(config_path)
96
+ except OSError:
97
+ continue
98
+ if last_mtime is None:
99
+ last_mtime = mtime
100
+ continue
101
+ if mtime == last_mtime:
102
+ continue
103
+ new_config = _load_new_config(config_path)
104
+ if new_config is not None:
105
+ app.state.config = new_config
106
+ logger.info("Config auto-reloaded (file changed)")
107
+ # Advance the marker even on failure so a broken file logs once,
108
+ # not every poll; the corrected save rewrites the file (new mtime)
109
+ # and reloads again.
110
+ last_mtime = mtime
111
+ except asyncio.CancelledError:
112
+ raise
113
+ except Exception:
114
+ logger.exception("Config watcher error")
115
+
116
+
65
117
  def create_app(config: GatewayConfig, config_path: str | None = None) -> Starlette:
66
118
  client = httpx.AsyncClient(
67
119
  timeout=httpx.Timeout(connect=10, read=3600, write=10, pool=10),
@@ -82,16 +134,19 @@ def create_app(config: GatewayConfig, config_path: str | None = None) -> Starlet
82
134
  async def lifespan(app: Starlette) -> AsyncGenerator[None, None]:
83
135
  app.state.config = config
84
136
  app.state.client = client
137
+ watcher = asyncio.create_task(_watch_config(app, config_path))
85
138
 
86
139
  def _handle_sigusr1(signum, frame):
87
- try:
88
- app.state.config = load_config(config_path)
140
+ new_config = _load_new_config(config_path)
141
+ if new_config is not None:
142
+ app.state.config = new_config
89
143
  logger.info("Config reloaded via SIGUSR1")
90
- except Exception as e:
91
- logger.error("Failed to reload config: %s", e)
92
144
 
93
145
  signal.signal(signal.SIGUSR1, _handle_sigusr1)
94
- yield
146
+ try:
147
+ yield
148
+ finally:
149
+ watcher.cancel()
95
150
  await client.aclose()
96
151
 
97
152
  return Starlette(routes=routes, lifespan=lifespan)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes