Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
265 lines (233 loc) · 10.3 KB
/
Copy pathapp.py
File metadata and controls
265 lines (233 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""FastAPI 应用工厂与生命周期管理.
路由端点注册已正交分解至 :mod:`.routes`,管理 API 与安全边界见
:mod:`.config_routes` / :mod:`.security`.
"""
from __future__ import annotations
import logging
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any
from fastapi import FastAPI
from .. import __version__
from ..auth.providers.github import GitHubDeviceFlowProvider
from ..auth.providers.google import GoogleOAuthProvider
from ..auth.runtime import RuntimeReauthCoordinator
from ..auth.store import TokenStoreManager
from ..compat.session_store import CompatSessionStore
from ..config.importer import StoredConfigInvalidError
from ..config.resolver import resolve_config
from ..config.schema import ProxyConfig
from ..logging.db import TokenLogger
from ..native_api import NativeProxyHandler
from ..routing.router import RequestRouter
from ..routing.session_policy import SessionPolicyResolver
from ..routing.tier import VendorTier
from ..routing.usage_recorder import UsageRecorder
from ..vendors.antigravity import AntigravityVendor
from ..vendors.copilot import CopilotVendor
from .config_routes import install_admin_surface
from .factory import ( # noqa: F401
_build_circuit_breaker,
_build_quota_guard,
_create_vendor_from_config,
)
from .routes import register_all_routes
from .security import install_security
from .subscription_status import SubscriptionStatusService
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理(启动 / 关闭)."""
router: RequestRouter = app.state.router
token_logger: TokenLogger = app.state.token_logger
compat_session_store: CompatSessionStore = app.state.compat_session_store
config: ProxyConfig = app.state.config
await token_logger.init()
await compat_session_store.init()
# 从配置加载模型定价表
from ..pricing import PricingTable # noqa: F401
pricing_table = PricingTable(config.pricing)
app.state.pricing_table = pricing_table
router.set_pricing_table(pricing_table)
# 原生 API 透传 handler:运行时注入 pricing_table(启动期创建时尚未就绪)
native_handler: NativeProxyHandler | None = getattr(
app.state, "native_handler", None
)
if native_handler is not None:
native_handler._pricing_table = pricing_table # noqa: SLF001
if native_handler._usage_recorder is not None: # noqa: SLF001
native_handler._usage_recorder.set_pricing_table(pricing_table) # noqa: SLF001
# Antigravity 客户端透传 handler:同上运行时注入
antigravity_handler = getattr(app.state, "antigravity_handler", None)
if antigravity_handler is not None:
antigravity_handler._pricing_table = pricing_table # noqa: SLF001
if antigravity_handler._usage_recorder is not None: # noqa: SLF001
antigravity_handler._usage_recorder.set_pricing_table(pricing_table) # noqa: SLF001
# 为每个有 QuotaGuard 的 tier 加载基线
for tier in router.tiers:
if tier.quota_guard and tier.quota_guard.enabled:
total = await token_logger.query_window_total(
tier.quota_guard.window_hours,
vendor=tier.name,
)
tier.quota_guard.load_baseline(total, vendor=tier.name)
if tier.weekly_quota_guard and tier.weekly_quota_guard.enabled:
total = await token_logger.query_window_total(
tier.weekly_quota_guard.window_hours,
vendor=tier.name,
)
tier.weekly_quota_guard.load_baseline(total, vendor=tier.name)
yield
await router.close()
if native_handler is not None:
await native_handler.aclose()
if antigravity_handler is not None:
await antigravity_handler.aclose()
await compat_session_store.close()
await token_logger.close()
logger.info("coding-proxy stopped")
def create_app(config: ProxyConfig | None = None) -> FastAPI:
"""创建 FastAPI 应用实例.
``config`` 缺省时经 :func:`resolve_config` 读取 config.db(不再回退旧 YAML 搜索链);
存储的配置无效时抛 :class:`StoredConfigInvalidError`。
"""
if config is None:
resolved = resolve_config()
if resolved.config is None:
locs = ", ".join(".".join(map(str, e["loc"])) for e in resolved.errors)
raise StoredConfigInvalidError(f"存储的配置无效: {locs}")
config = resolved.config
token_logger = TokenLogger(config.db_path)
compat_session_store = CompatSessionStore(
config.compat_state_path,
ttl_seconds=config.database.compat_state_ttl_seconds,
)
from ..routing.model_mapper import ModelMapper # noqa: E402
from ..vendors.model_catalog import ( # noqa: E402
builtin_identity_rules,
compose_bindable_catalog,
)
# 阶段二之前先推导激活链(enabled 过滤 + tiers 排序),供目录合成与
# ModelMapper identity 规则使用(与下方 tiers 组装同一判定口径)
_enabled_vendor_names = {v.vendor for v in config.vendors if v.enabled}
if config.tiers is not None:
_active_vendor_names = [
name for name in config.tiers if name in _enabled_vendor_names
]
else:
_active_vendor_names = [v.vendor for v in config.vendors if v.enabled]
_bindable_catalog = compose_bindable_catalog(
_active_vendor_names, config.session_policies.bindable_models, config.pricing
)
mapper = ModelMapper(
[
*config.model_mapping,
*builtin_identity_rules(_bindable_catalog, config.model_mapping),
]
)
# 加载 Token Store 用于凭证合并
token_store = TokenStoreManager(
store_path=Path(config.auth.token_store_path)
if config.auth.token_store_path
else None
)
token_store.load()
# 阶段一:构建 vendor_name → VendorTier 映射表(与顺序无关)
_vendor_map: dict[str, Any] = {}
for vendor_cfg in config.vendors:
if not vendor_cfg.enabled:
continue
vendor = _create_vendor_from_config(
vendor_cfg, config.failover, mapper, token_store
)
cb = (
_build_circuit_breaker(
vendor_cfg.circuit_breaker, vendor_name=vendor_cfg.vendor
)
if vendor_cfg.circuit_breaker
else None
)
qg = _build_quota_guard(vendor_cfg.quota_guard)
wqg = _build_quota_guard(vendor_cfg.weekly_quota_guard)
_vendor_map[vendor_cfg.vendor] = VendorTier(
vendor=vendor, circuit_breaker=cb, quota_guard=qg, weekly_quota_guard=wqg
)
# 阶段二:按 tiers 指定的顺序组装最终链路(或回退到 vendors 原始顺序)
if config.tiers is not None:
tiers = [_vendor_map[name] for name in config.tiers if name in _vendor_map]
else:
tiers = [_vendor_map[v.vendor] for v in config.vendors if v.enabled]
# 构建运行时重认证协调器
reauth_providers: dict[str, Any] = {}
token_updaters: dict[str, Any] = {}
for tier in tiers:
if isinstance(tier.vendor, CopilotVendor):
reauth_providers["github"] = GitHubDeviceFlowProvider()
token_updaters["github"] = tier.vendor._token_manager.update_github_token
elif isinstance(tier.vendor, AntigravityVendor):
reauth_providers["google"] = GoogleOAuthProvider()
token_updaters["google"] = tier.vendor._token_manager.update_refresh_token
reauth_coordinator: RuntimeReauthCoordinator | None = None
if reauth_providers:
reauth_coordinator = RuntimeReauthCoordinator(
token_store, reauth_providers, token_updaters
)
router = RequestRouter(
tiers,
token_logger,
reauth_coordinator,
compat_session_store,
session_policy_resolver=SessionPolicyResolver(config.session_policies.policies),
title_vendor_bindings=config.session_policies.title_vendor_bindings,
title_exempt_prefixes=config.session_policies.title_exempt_prefixes,
bindable_models=_bindable_catalog,
)
app = FastAPI(
title="coding-proxy",
version=__version__,
lifespan=lifespan,
docs_url=None,
redoc_url=None,
openapi_url=None,
)
install_security(app, allowed_hosts=config.server.allowed_hosts)
app.state.router = router
app.state.token_logger = token_logger
app.state.compat_session_store = compat_session_store
app.state.config = config
app.state.reauth_coordinator = reauth_coordinator
# 订阅状态聚合(GLM/Antigravity 主动查询 + Claude 被动信号):
# 随 generation 整体重建,天然持有当前 config/router 的凭证引用
app.state.subscription_status = SubscriptionStatusService(
config, router, token_store=token_store
)
# 原生 API 透传 handler — 仅在配置中至少启用一个 provider 时实例化
if any(config.native_api.is_enabled(p) for p in ("openai", "gemini", "anthropic")):
native_usage_recorder = UsageRecorder(
token_logger=token_logger, pricing_table=None
)
native_handler = NativeProxyHandler(
config.native_api,
token_logger=token_logger,
pricing_table=None,
usage_recorder=native_usage_recorder,
)
app.state.native_handler = native_handler
# Antigravity IDE 客户端透传 handler — 独立于 native_api 的并列通道
if config.antigravity_gateway.enabled:
from ..antigravity_gateway.handler import AntigravityGatewayHandler
antigravity_usage_recorder = UsageRecorder(
token_logger=token_logger, pricing_table=None
)
antigravity_handler = AntigravityGatewayHandler(
config.antigravity_gateway,
token_logger=token_logger,
pricing_table=None,
usage_recorder=antigravity_usage_recorder,
title_exempt_prefixes=config.session_policies.title_exempt_prefixes,
)
app.state.antigravity_handler = antigravity_handler
# 注册所有路由端点(管理 API 先于原生透传 catch-all)
install_admin_surface(app)
register_all_routes(app, router, reauth_coordinator)
return app