fix(arch): 迁移 LLM 异常到 llm/exceptions.py 消除分层异味
将 LLMError/LLMProviderError/ModelNotFoundError 定义从 core/exceptions.py 迁移到 llm/exceptions.py,使 LLM 异常定义归位 LLM 层。 - core/exceptions.py 用 PEP 562 __getattr__ 延迟 re-export,打破循环依赖: core.exceptions → llm.exceptions → llm.__init__ → llm.retry → core.exceptions - llm/exceptions.py 中 LLMError 仍继承 AgentFrameworkError,保持 isinstance 兼容 - llm/__init__.py 导出 exceptions,支持 from agentkit.llm import LLMError - 现有 from agentkit.core.exceptions import LLMProviderError 调用无需修改 零破坏性变更,ruff + import 兼容性验证通过。
This commit is contained in:
parent
d74b41b0aa
commit
5f5c09835f
|
|
@ -132,24 +132,32 @@ class EvolutionError(AgentFrameworkError):
|
|||
super().__init__(f"Evolution failed for agent '{agent_name}': {reason}")
|
||||
|
||||
|
||||
class LLMError(AgentFrameworkError):
|
||||
"""LLM 基础异常"""
|
||||
|
||||
def __init__(self, message: str = "LLM error"):
|
||||
super().__init__(message)
|
||||
# Re-export LLM exceptions from llm/exceptions.py for backward compatibility.
|
||||
# New code should import from agentkit.llm.exceptions directly.
|
||||
# ponytail: 架构分层修复 — LLM 异常定义归位 LLM 层;re-export 保留以避免破坏
|
||||
# 现有 `from agentkit.core.exceptions import LLMProviderError` 调用方。
|
||||
# 升级路径:调用方逐步迁移到 llm.exceptions 后可移除 __getattr__。
|
||||
#
|
||||
# 用 PEP 562 模块级 __getattr__ 延迟导入,打破循环依赖:
|
||||
# core.exceptions → llm.exceptions → llm.__init__ → llm.retry → core.exceptions.LLMProviderError
|
||||
# (若顶层 import,LLMProviderError 尚未绑定 → ImportError)
|
||||
_LLM_EXCEPTION_NAMES = ("LLMError", "LLMProviderError", "ModelNotFoundError")
|
||||
|
||||
|
||||
class LLMProviderError(LLMError):
|
||||
"""LLM Provider 特定异常"""
|
||||
def __getattr__(name: str): # PEP 562
|
||||
if name in _LLM_EXCEPTION_NAMES:
|
||||
from agentkit.llm.exceptions import ( # noqa: PLC0415
|
||||
LLMError,
|
||||
LLMProviderError,
|
||||
ModelNotFoundError,
|
||||
)
|
||||
|
||||
def __init__(self, provider: str, reason: str = ""):
|
||||
self.provider = provider
|
||||
super().__init__(f"LLM provider '{provider}' error: {reason}")
|
||||
globals()["LLMError"] = LLMError
|
||||
globals()["LLMProviderError"] = LLMProviderError
|
||||
globals()["ModelNotFoundError"] = ModelNotFoundError
|
||||
return globals()[name]
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
class ModelNotFoundError(LLMError):
|
||||
"""模型别名未找到异常"""
|
||||
|
||||
def __init__(self, model: str):
|
||||
self.model = model
|
||||
super().__init__(f"Model not found: {model}")
|
||||
def __dir__(): # 让 from agentkit.core.exceptions import * 仍能发现 re-export
|
||||
return [*(globals().keys()), *_LLM_EXCEPTION_NAMES]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from agentkit.llm.providers.anthropic import AnthropicProvider
|
|||
from agentkit.llm.providers.openai import OpenAICompatibleProvider
|
||||
from agentkit.llm.providers.tracker import UsageSummary, UsageTracker
|
||||
from agentkit.llm.providers.usage_store import UsageRecord
|
||||
from agentkit.llm.exceptions import LLMError, LLMProviderError, ModelNotFoundError
|
||||
from agentkit.llm.remote_provider import RemoteLLMProvider
|
||||
from agentkit.llm.retry import (
|
||||
CircuitBreaker,
|
||||
|
|
@ -38,4 +39,7 @@ __all__ = [
|
|||
"UsageTracker",
|
||||
"UsageRecord",
|
||||
"UsageSummary",
|
||||
"LLMError",
|
||||
"LLMProviderError",
|
||||
"ModelNotFoundError",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
"""LLM 层异常定义。
|
||||
|
||||
架构分层原则:LLM 异常定义在 LLM 层,而非 core/exceptions.py。
|
||||
core/exceptions.py 保留 re-export 以保持向后兼容。
|
||||
"""
|
||||
|
||||
from agentkit.core.exceptions import AgentFrameworkError
|
||||
|
||||
|
||||
class LLMError(AgentFrameworkError):
|
||||
"""LLM 基础异常"""
|
||||
|
||||
def __init__(self, message: str = "LLM error"):
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class LLMProviderError(LLMError):
|
||||
"""LLM Provider 特定异常"""
|
||||
|
||||
def __init__(self, provider: str, reason: str = ""):
|
||||
self.provider = provider
|
||||
super().__init__(f"LLM provider '{provider}' error: {reason}")
|
||||
|
||||
|
||||
class ModelNotFoundError(LLMError):
|
||||
"""模型别名未找到异常"""
|
||||
|
||||
def __init__(self, model: str):
|
||||
self.model = model
|
||||
super().__init__(f"Model not found: {model}")
|
||||
|
||||
|
||||
__all__ = [
|
||||
"LLMError",
|
||||
"LLMProviderError",
|
||||
"ModelNotFoundError",
|
||||
]
|
||||
Loading…
Reference in New Issue