refactor: 用 EndpointFetchConfig 纯数据类替代 ORM 对象传递,统一上游模型缓存管理

- 引入 EndpointFetchConfig dataclass 替代直接传递 ProviderEndpoint ORM 对象,
  避免 DB session 关闭后 DetachedInstanceError
- 新增 build_format_to_config() 统一构建 api_format -> EndpointFetchConfig 映射
- KeyAllowedModelsDialog 改用 useUpstreamModelsCache composable 管理上游模型获取
- useUpstreamModelsCache 增加 error 字段透传部分格式获取失败的 warning
- 删除废弃的 queryProviderUpstreamModels API 函数
- ProviderCandidate 添加 __lt__ 方法支持排序比较
This commit is contained in:
fawney19
2026-02-09 12:47:47 +08:00
parent 8702786fa2
commit 8684548072
7 changed files with 71 additions and 55 deletions

View File

@@ -27,7 +27,9 @@ from src.services.model.fetch_scheduler import (
set_upstream_models_to_cache,
)
from src.services.model.upstream_fetcher import (
EndpointFetchConfig,
UpstreamModelsFetchContext,
build_format_to_config,
fetch_models_for_key,
get_adapter_for_format,
)
@@ -171,11 +173,8 @@ async def query_available_models(
if not provider:
raise HTTPException(status_code=404, detail="Provider not found")
# 构建 api_format -> endpoint 映射
format_to_endpoint: dict[str, ProviderEndpoint] = {}
for endpoint in provider.endpoints:
if endpoint.is_active:
format_to_endpoint[endpoint.api_format] = endpoint
# 构建 api_format -> EndpointFetchConfig 映射(纯数据,不依赖 ORM session
format_to_endpoint = build_format_to_config(provider.endpoints)
if not format_to_endpoint:
raise HTTPException(status_code=400, detail="No active endpoints found for this provider")
@@ -327,7 +326,7 @@ def _aggregate_models_by_id(models: list[dict]) -> list[dict]:
async def _fetch_models_for_single_key(
provider: Provider,
api_key_id: str,
format_to_endpoint: dict[str, ProviderEndpoint],
format_to_endpoint: dict[str, EndpointFetchConfig],
force_refresh: bool,
) -> Any:
"""获取单个 Key 的模型列表"""