mirror of
https://github.com/fawney19/Aether.git
synced 2026-01-02 15:52:26 +08:00
refactor: remove unused response normalizer module
- Delete unused ResponseNormalizer class and its initialization logic - Remove response_normalizer and enable_response_normalization parameters from handlers - Simplify chat adapter base initialization by removing normalizer setup - Clean up unused imports in handler modules
This commit is contained in:
@@ -64,18 +64,6 @@ class ChatAdapterBase(ApiAdapter):
|
|||||||
|
|
||||||
def __init__(self, allowed_api_formats: Optional[list[str]] = None):
|
def __init__(self, allowed_api_formats: Optional[list[str]] = None):
|
||||||
self.allowed_api_formats = allowed_api_formats or [self.FORMAT_ID]
|
self.allowed_api_formats = allowed_api_formats or [self.FORMAT_ID]
|
||||||
self.response_normalizer = None
|
|
||||||
# 可选启用响应规范化
|
|
||||||
self._init_response_normalizer()
|
|
||||||
|
|
||||||
def _init_response_normalizer(self):
|
|
||||||
"""初始化响应规范化器 - 子类可覆盖"""
|
|
||||||
try:
|
|
||||||
from src.services.provider.response_normalizer import ResponseNormalizer
|
|
||||||
|
|
||||||
self.response_normalizer = ResponseNormalizer()
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def handle(self, context: ApiRequestContext):
|
async def handle(self, context: ApiRequestContext):
|
||||||
"""处理 Chat API 请求"""
|
"""处理 Chat API 请求"""
|
||||||
@@ -228,8 +216,6 @@ class ChatAdapterBase(ApiAdapter):
|
|||||||
user_agent=user_agent,
|
user_agent=user_agent,
|
||||||
start_time=start_time,
|
start_time=start_time,
|
||||||
allowed_api_formats=self.allowed_api_formats,
|
allowed_api_formats=self.allowed_api_formats,
|
||||||
response_normalizer=self.response_normalizer,
|
|
||||||
enable_response_normalization=self.response_normalizer is not None,
|
|
||||||
adapter_detector=self.detect_capability_requirements,
|
adapter_detector=self.detect_capability_requirements,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -88,8 +88,6 @@ class ChatHandlerBase(BaseMessageHandler, ABC):
|
|||||||
user_agent: str,
|
user_agent: str,
|
||||||
start_time: float,
|
start_time: float,
|
||||||
allowed_api_formats: Optional[list] = None,
|
allowed_api_formats: Optional[list] = None,
|
||||||
response_normalizer: Optional[Any] = None,
|
|
||||||
enable_response_normalization: bool = False,
|
|
||||||
adapter_detector: Optional[Callable[[Dict[str, str], Optional[Dict[str, Any]]], Dict[str, bool]]] = None,
|
adapter_detector: Optional[Callable[[Dict[str, str], Optional[Dict[str, Any]]], Dict[str, bool]]] = None,
|
||||||
):
|
):
|
||||||
allowed = allowed_api_formats or [self.FORMAT_ID]
|
allowed = allowed_api_formats or [self.FORMAT_ID]
|
||||||
@@ -106,8 +104,6 @@ class ChatHandlerBase(BaseMessageHandler, ABC):
|
|||||||
)
|
)
|
||||||
self._parser: Optional[ResponseParser] = None
|
self._parser: Optional[ResponseParser] = None
|
||||||
self._request_builder = PassthroughRequestBuilder()
|
self._request_builder = PassthroughRequestBuilder()
|
||||||
self.response_normalizer = response_normalizer
|
|
||||||
self.enable_response_normalization = enable_response_normalization
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parser(self) -> ResponseParser:
|
def parser(self) -> ResponseParser:
|
||||||
|
|||||||
@@ -131,10 +131,5 @@ class ClaudeChatHandler(ChatHandlerBase):
|
|||||||
Returns:
|
Returns:
|
||||||
规范化后的响应
|
规范化后的响应
|
||||||
"""
|
"""
|
||||||
if self.response_normalizer and self.response_normalizer.should_normalize(response):
|
# 作为中转站,直接透传响应,不做标准化处理
|
||||||
result: Dict[str, Any] = self.response_normalizer.normalize_claude_response(
|
|
||||||
response_data=response,
|
|
||||||
request_id=self.request_id,
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -148,17 +148,6 @@ class GeminiChatHandler(ChatHandlerBase):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
规范化后的响应
|
规范化后的响应
|
||||||
|
|
||||||
TODO: 如果需要,实现响应规范化逻辑
|
|
||||||
"""
|
"""
|
||||||
# 可选:使用 response_normalizer 进行规范化
|
# 作为中转站,直接透传响应,不做标准化处理
|
||||||
# if (
|
|
||||||
# self.response_normalizer
|
|
||||||
# and self.response_normalizer.should_normalize(response)
|
|
||||||
# ):
|
|
||||||
# return self.response_normalizer.normalize_gemini_response(
|
|
||||||
# response_data=response,
|
|
||||||
# request_id=self.request_id,
|
|
||||||
# strict=False,
|
|
||||||
# )
|
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -128,10 +128,5 @@ class OpenAIChatHandler(ChatHandlerBase):
|
|||||||
Returns:
|
Returns:
|
||||||
规范化后的响应
|
规范化后的响应
|
||||||
"""
|
"""
|
||||||
if self.response_normalizer and self.response_normalizer.should_normalize(response):
|
# 作为中转站,直接透传响应,不做标准化处理
|
||||||
return self.response_normalizer.normalize_openai_response(
|
|
||||||
response_data=response,
|
|
||||||
request_id=self.request_id,
|
|
||||||
strict=False,
|
|
||||||
)
|
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
"""响应标准化服务,用于 STANDARD 模式下的响应格式验证和补全"""
|
|
||||||
|
|
||||||
from typing import Any, Dict, Optional
|
|
||||||
|
|
||||||
from src.core.logger import logger
|
|
||||||
from src.models.claude import ClaudeResponse
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ResponseNormalizer:
|
|
||||||
"""响应标准化器 - 用于标准模式下验证和补全响应字段"""
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def normalize_claude_response(
|
|
||||||
response_data: Dict[str, Any], request_id: Optional[str] = None
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
标准化 Claude API 响应
|
|
||||||
|
|
||||||
Args:
|
|
||||||
response_data: 原始响应数据
|
|
||||||
request_id: 请求ID(用于日志)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
标准化后的响应数据(失败时返回原始数据)
|
|
||||||
"""
|
|
||||||
if "error" in response_data:
|
|
||||||
logger.debug(f"[ResponseNormalizer] 检测到错误响应,跳过标准化 | ID:{request_id}")
|
|
||||||
return response_data
|
|
||||||
|
|
||||||
try:
|
|
||||||
validated = ClaudeResponse.model_validate(response_data)
|
|
||||||
normalized = validated.model_dump(mode="json", exclude_none=False)
|
|
||||||
|
|
||||||
logger.debug(f"[ResponseNormalizer] 响应标准化成功 | ID:{request_id}")
|
|
||||||
return normalized
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.debug(f"[ResponseNormalizer] 响应验证失败,透传原始数据 | ID:{request_id}")
|
|
||||||
return response_data
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def should_normalize(response_data: Dict[str, Any]) -> bool:
|
|
||||||
"""
|
|
||||||
判断是否需要进行标准化
|
|
||||||
|
|
||||||
Args:
|
|
||||||
response_data: 响应数据
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
是否需要标准化
|
|
||||||
"""
|
|
||||||
# 错误响应不需要标准化
|
|
||||||
if "error" in response_data:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# 已经包含新字段的响应不需要再次标准化
|
|
||||||
if "context_management" in response_data and "container" in response_data:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
Reference in New Issue
Block a user