mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
fix: 统一格式兼容性检查,透传格式也需开关启用
- 同族格式(如 CLAUDE/CLAUDE_CLI)现在也需要全局开关和端点开关启用才能透传 - 格式转换关闭时,Models API 只返回客户端格式本身 - 添加测试用例验证透传格式受开关限制
This commit is contained in:
@@ -5,8 +5,9 @@
|
||||
|
||||
转换逻辑:
|
||||
1. 格式完全匹配 -> 透传(无需转换)
|
||||
2. data_format_id 相同 -> 透传(如 CLAUDE/CLAUDE_CLI 数据格式相同)
|
||||
3. data_format_id 不同 -> 需要转换,检查全局开关 + 端点配置 + 转换器能力
|
||||
2. 格式不同 -> 需要检查全局开关 + 端点开关
|
||||
- data_format_id 相同 -> 可透传(无需数据转换),但需全局开关 + 端点开关
|
||||
- data_format_id 不同 -> 需要转换,检查全局开关 + 端点配置 + 转换器能力
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -64,38 +65,20 @@ def is_format_compatible(
|
||||
if provider_format == client_format_upper:
|
||||
return True, False, None
|
||||
|
||||
# 2. 检查是否可以透传(data_format_id 相同)
|
||||
# 例如:CLAUDE/CLAUDE_CLI 的 data_format_id 都是 "claude",数据格式相同可透传
|
||||
# OPENAI 是 "openai_chat",OPENAI_CLI 是 "openai_responses",需要转换
|
||||
if can_passthrough(client_format_upper, provider_format):
|
||||
# 可透传,但仍需检查端点的格式限制配置(如果有)
|
||||
if endpoint_format_acceptance_config and isinstance(endpoint_format_acceptance_config, dict):
|
||||
config = endpoint_format_acceptance_config
|
||||
# 检查 reject_formats(即使可透传也应遵守拒绝列表)
|
||||
reject_formats = config.get("reject_formats", [])
|
||||
if client_format_upper in [f.upper() for f in reject_formats]:
|
||||
return False, False, f"端点拒绝 {client_format} 格式"
|
||||
# 检查 accept_formats(如果配置了白名单,也需在白名单中)
|
||||
accept_formats = config.get("accept_formats", [])
|
||||
if accept_formats and client_format_upper not in [f.upper() for f in accept_formats]:
|
||||
return False, False, f"端点不接受 {client_format} 格式"
|
||||
# 通过检查后,可透传(无需转换)
|
||||
return True, False, None
|
||||
|
||||
# 3. 需要转换的情况(data_format_id 不同)
|
||||
# 检查全局开关(来自环境变量,默认开启)
|
||||
# 2. 格式不同 -> 需要检查全局格式转换开关
|
||||
# 即使 data_format_id 相同(如 CLAUDE/CLAUDE_CLI),也需要全局开关启用
|
||||
if not global_conversion_enabled:
|
||||
return False, False, "全局格式转换未启用(环境变量 FORMAT_CONVERSION_ENABLED=false)"
|
||||
|
||||
# 4. 检查端点配置(核心控制)
|
||||
# 3. 格式不同时,统一检查端点配置(核心控制)
|
||||
if endpoint_format_acceptance_config is None:
|
||||
return False, False, "端点未配置格式转换"
|
||||
return False, False, "端点未配置格式接受策略"
|
||||
|
||||
config = endpoint_format_acceptance_config
|
||||
if not isinstance(config, dict):
|
||||
return False, False, "端点格式配置无效"
|
||||
if not config.get("enabled", False):
|
||||
return False, False, "端点格式转换未启用"
|
||||
return False, False, "端点格式接受未启用"
|
||||
|
||||
# 检查 reject_formats(优先)
|
||||
reject_formats = config.get("reject_formats", [])
|
||||
@@ -107,11 +90,19 @@ def is_format_compatible(
|
||||
if accept_formats and client_format_upper not in [f.upper() for f in accept_formats]:
|
||||
return False, False, f"端点不接受 {client_format} 格式"
|
||||
|
||||
# 4. 检查是否可以透传(data_format_id 相同)
|
||||
# 例如:CLAUDE/CLAUDE_CLI 的 data_format_id 都是 "claude",数据格式相同可透传
|
||||
# OPENAI 是 "openai_chat",OPENAI_CLI 是 "openai_responses",需要转换
|
||||
if can_passthrough(client_format_upper, provider_format):
|
||||
# data_format_id 相同,可透传(无需数据转换)
|
||||
return True, False, None
|
||||
|
||||
# 5. 需要数据转换的情况(data_format_id 不同)
|
||||
# 检查流式转换
|
||||
if is_stream and not config.get("stream_conversion", True):
|
||||
return False, False, "端点不支持流式格式转换"
|
||||
|
||||
# 5. 检查转换器能力
|
||||
# 6. 检查转换器能力
|
||||
if not registry.can_convert_full(
|
||||
client_format_upper,
|
||||
provider_format,
|
||||
|
||||
Reference in New Issue
Block a user