refactor: 全局适配 ApiFamily/EndpointKind 结构化标识体系

将新的 (ApiFamily, EndpointKind) / `family:kind` 签名体系应用到整个代码库:
- API Handlers: 所有 adapter/handler 使用新的签名格式
- Services: provider, model, usage, cache, auth 等服务层适配
- Database: ProviderEndpoint 新增 api_family/endpoint_kind 字段
- Frontend: Provider 管理、Usage 表格等组件适配
- Tests: 更新所有相关测试用例
This commit is contained in:
fawney19
2026-02-01 17:28:00 +08:00
parent c246ccfc91
commit 7b66505634
219 changed files with 4732 additions and 2545 deletions

View File

@@ -15,7 +15,6 @@ from src.core.logger import logger
from src.models.database import ApiKey, Usage
class ApiKeyService:
"""API密钥管理服务"""
@@ -86,8 +85,10 @@ class ApiKeyService:
db.commit()
db.refresh(api_key)
logger.info(f"创建API密钥: 用户ID {user_id}, 密钥名 {api_key.name}, "
f"独立Key={is_standalone}, 初始余额={initial_balance_usd}")
logger.info(
f"创建API密钥: 用户ID {user_id}, 密钥名 {api_key.name}, "
f"独立Key={is_standalone}, 初始余额={initial_balance_usd}"
)
return api_key, key # 返回密钥对象和明文密钥
@staticmethod
@@ -256,7 +257,9 @@ class ApiKeyService:
is_allowed = request_count < api_key.rate_limit
if not is_allowed:
logger.warning(f"API密钥速率限制: Key ID {api_key.id}, 请求数 {request_count}/{api_key.rate_limit}")
logger.warning(
f"API密钥速率限制: Key ID {api_key.id}, 请求数 {request_count}/{api_key.rate_limit}"
)
return is_allowed, api_key.rate_limit - request_count
@@ -289,7 +292,9 @@ class ApiKeyService:
if amount_usd < 0:
current = api_key.current_balance_usd or 0
if abs(amount_usd) > current:
logger.warning(f"余额扣除失败: 扣除金额 ${abs(amount_usd):.4f} 超过当前余额 ${current:.4f}")
logger.warning(
f"余额扣除失败: 扣除金额 ${abs(amount_usd):.4f} 超过当前余额 ${current:.4f}"
)
return None
# 调整当前余额
@@ -303,8 +308,10 @@ class ApiKeyService:
db.refresh(api_key)
action = "增加" if amount_usd > 0 else "扣除"
logger.info(f"余额调整成功: Key ID {key_id}, {action} ${abs(amount_usd):.4f}, "
f"余额 ${api_key.current_balance_usd:.4f}")
logger.info(
f"余额调整成功: Key ID {key_id}, {action} ${abs(amount_usd):.4f}, "
f"新余额 ${api_key.current_balance_usd:.4f}"
)
return api_key
@staticmethod
@@ -338,14 +345,18 @@ class ApiKeyService:
if should_delete:
# 物理删除Usage记录会保留因为是 SET NULL
db.delete(api_key)
logger.info(f"删除过期API密钥: ID {api_key.id}, 名称 {api_key.name}, "
f"过期时间 {api_key.expires_at}")
logger.info(
f"删除过期API密钥: ID {api_key.id}, 名称 {api_key.name}, "
f"过期时间 {api_key.expires_at}"
)
else:
# 仅禁用
api_key.is_active = False
api_key.updated_at = now
logger.info(f"禁用过期API密钥: ID {api_key.id}, 名称 {api_key.name}, "
f"过期时间 {api_key.expires_at}")
logger.info(
f"禁用过期API密钥: ID {api_key.id}, 名称 {api_key.name}, "
f"过期时间 {api_key.expires_at}"
)
count += 1
if count > 0:

View File

@@ -2,7 +2,6 @@
用户偏好设置服务
"""
from __future__ import annotations
from sqlalchemy.orm import Session
@@ -12,7 +11,6 @@ from src.core.logger import logger
from src.models.database import Provider, User, UserPreference
class PreferenceService:
"""用户偏好设置服务"""

View File

@@ -18,7 +18,6 @@ from src.services.cache.user_cache import UserCacheService
from src.utils.transaction_manager import retry_on_database_error, transactional
class UserService:
"""用户管理服务"""
@@ -198,7 +197,12 @@ class UserService:
]
# 允许设置为 None 的字段(表示无限制)
nullable_fields = ["quota_usd", "allowed_providers", "allowed_api_formats", "allowed_models"]
nullable_fields = [
"quota_usd",
"allowed_providers",
"allowed_api_formats",
"allowed_models",
]
for field, value in kwargs.items():
if field not in updatable_fields:
@@ -442,7 +446,9 @@ class UserService:
# 应用访问限制过滤
filtered_models = []
for model in all_models:
model_name = model.global_model.name if model.global_model else model.provider_model_name
model_name = (
model.global_model.name if model.global_model else model.provider_model_name
)
# 使用 AccessRestrictions.is_model_allowed 检查模型是否可访问
if restrictions.is_model_allowed(model_name, model.provider_id):
filtered_models.append(model)