refactor: 重构格式转换系统为 Hub-and-Spoke Normalizer 架构

- 移除旧的 converters 模块,改用基于 Internal 中间表示的 Normalizer 模式
- 新增 internal.py 定义统一的内部数据结构(InternalRequest/Response/Error)
- 新增 normalizer.py 定义 FormatNormalizer 基类接口
- 实现 OpenAI/Claude/Gemini 及其 CLI 格式的 Normalizer
- 新增 stream_events.py 和 stream_state.py 支持流式转换
- 重构 registry.py 使用 Normalizer 进行格式转换
- 更新 handlers 适配新的转换架构
- 前端移除 CLI 格式转换按钮的限制
- 添加 golden data 测试确保转换正确性
This commit is contained in:
fawney19
2026-01-27 02:17:18 +08:00
parent 6cf251b19a
commit add477fece
84 changed files with 9169 additions and 3932 deletions

View File

@@ -27,8 +27,10 @@ from src.core.api_format import (
ApiFormatDefinition,
detect_format_and_key_from_starlette,
)
from src.core.api_format.conversion import converter_registry
from src.core.api_format.utils import is_cli_format
from src.core.api_format.conversion import (
format_conversion_registry,
register_default_normalizers,
)
from src.core.logger import logger
from src.database import get_db
from src.models.database import ApiKey, User
@@ -114,11 +116,8 @@ def _get_convertible_formats(client_format: str, global_conversion_enabled: bool
client_format_upper = client_format.upper()
# CLI 格式不支持转换
if is_cli_format(client_format_upper):
return _get_formats_for_api(client_format)
# 收集所有可转换的格式
register_default_normalizers()
convertible_formats = []
for target_format in _ALL_CHAT_FORMATS:
# 相同格式始终可用
@@ -127,7 +126,11 @@ def _get_convertible_formats(client_format: str, global_conversion_enabled: bool
continue
# 检查是否有双向转换器
if converter_registry.can_convert_full(client_format_upper, target_format, require_stream=False):
if format_conversion_registry.can_convert_full(
client_format_upper,
target_format,
require_stream=False,
):
convertible_formats.append(target_format)
return convertible_formats if convertible_formats else _get_formats_for_api(client_format)