mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 03:39:59 +08:00
* ✨ feat!(llm): 重构并升级大语言模型服务为全新 AI 智能体框架 - 【重构】将原 services/llm 重构并迁移至全新的 services/ai 架构,提供向下兼容垫片 - 【新增】引入 Agent、Team、Workflow 三大智能体与工作流编排范式 - 【新增】引入基于 RAG 的长期向量记忆与中期槽位记忆系统 - 【新增】引入基于 Docker 的安全代码执行沙箱环境 - 【新增】支持 MCP 协议,允许动态管理和调用 MCP 服务 - 【新增】引入输入输出安全合规护栏与自愈反思机制 - 【优化】重构并优化多厂商 API 适配器 (Gemini, OpenAI, DeepSeek, GLM 等) - 【优化】优化日志脱敏与 Token 预估机制 - 【移除】移除旧版 llm default 和 llm reset-key 命令,新增 llm mcp 管理命令 * 🔧 chore(deps): 更新项目依赖与配置 - 添加 mcp、jieba 和 aiodocker 依赖到配置文件及 requirements.txt - 在 pyright 配置中设置 reportMissingImports 为 none - 调整 .gitignore 中 resources 目录的忽略规则 * ♻️ refactor(tools): 重构工具终止机制并清理知识库日志输出 - 统一使用 `context.state["__end_run__"]` 替代 `EndRunResult` 控制任务结束 - 移除文件系统和向量知识库检索工具中 `ToolResult` 的 `.with_log` 调用 - 调整指令处理器(Directive)的返回值为 `tool_res.output` - 修复部分类型检查警告并优化联合类型判断语法 * ♻️ refactor(tools): 重构工具副作用指令与控制流熔断机制 - 引入 `DirectivePayload` 及 `ToolResult` 的子类以结构化表达工具副作用 - 移除通过 `context.state` 传递魔术变量的隐式控制流设计 - 重构 `DirectiveManager` 处理器接口,直接在处理器中修改 `AgentState` 并构建 `AgentRunResult` - 在 `StandardAgentExecutor` 中统一通过 `directive_manager` 调度工具返回的副作用指令 - 补全 `MessageBuilder` 中部分核心方法的文档注释 * 🐛 fix(sandbox): 修复 Docker 沙箱容器状态检测与会话清理逻辑 -【修复】修正 `is_alive` 中直接读取私有属性的问题,改用 `show()` 返回值 -【修复】解决 `execute_code` 中缓存的执行器与当前会话不一致的问题 -【优化】在清理工作区前增加容器存活检测,避免向已死容器发送请求 -【优化】创建容器时增加运行状态校验,若已停止则自动从缓存中移除并重建 -【优化】优化容器销毁和清理逻辑,静默处理容器不存在 (404) 的异常 * 📝 docs(core): 补充核心模块初始化方法的文档注释 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
350 lines
11 KiB
Python
350 lines
11 KiB
Python
"""
|
|
自定义异常与错误码定义
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
|
|
class ModelRetry(Exception):
|
|
"""用于通知大模型修正并重试的异常"""
|
|
|
|
def __init__(self, message: str):
|
|
self.message = message
|
|
super().__init__(message)
|
|
|
|
|
|
class SchemaParseError(ModelRetry):
|
|
"""格式解析异常。当大模型返回的 JSON 损坏或不符合 Schema 时抛出。"""
|
|
|
|
def __init__(self, message: str):
|
|
super().__init__(message)
|
|
|
|
|
|
class GuardrailViolationError(ModelRetry):
|
|
"""护栏违规异常。当大模型返回的数据格式正确,但违反业务规则时抛出。"""
|
|
|
|
def __init__(self, message: str):
|
|
super().__init__(message)
|
|
|
|
|
|
class ControlFlowExit(BaseException):
|
|
"""控制流退出基类,继承自BaseException以避免被常规Exception捕获,用于静默中断。"""
|
|
|
|
pass
|
|
|
|
|
|
class ToolFatalError(ControlFlowExit):
|
|
"""
|
|
致命工具异常(不可恢复)。
|
|
当工具执行遇到权限不足、严重系统故障等大模型无法通过重试解决的问题时抛出。
|
|
这会直接熔断 Agent 推理流,并将 display_content 抛给用户。
|
|
"""
|
|
|
|
def __init__(self, message: str, display_content: str | None = None):
|
|
self.message = message
|
|
self.display_content = display_content or f"❌ 工具遇到致命错误: {message}"
|
|
super().__init__(self.message)
|
|
|
|
|
|
class GuardrailFatalException(ControlFlowExit):
|
|
"""护栏致命拦截异常 (触发 ABORT/REJECT 时抛出)"""
|
|
|
|
def __init__(self, guard_name: str, reason: str, display: str | None = None):
|
|
self.guard_name = guard_name
|
|
self.reason = reason
|
|
self.display = display or f"🛡️ 安全拦截: {reason}"
|
|
super().__init__(f"Guardrail '{guard_name}' aborted execution: {reason}")
|
|
|
|
|
|
class ToolRetryError(Exception):
|
|
"""
|
|
可恢复工具异常。
|
|
当参数解析错误、业务逻辑校验失败、网络超时等问题发生时抛出。
|
|
会被 ToolExecutor 捕获并转化为引导大模型自我反思 (Reflexion) 的 ToolResult。
|
|
"""
|
|
|
|
def __init__(self, message: str):
|
|
self.message = message
|
|
super().__init__(self.message)
|
|
|
|
|
|
class ToolFinishException(ToolFatalError):
|
|
"""
|
|
工具执行中止异常。
|
|
当工具开发者希望立刻停止大模型的思考循环,并直接将错误/提示信息返回给用户时抛出。
|
|
此异常不会被大模型进行"影子自愈(Reflexion)",而是直接熔断 Agent 执行流。
|
|
"""
|
|
|
|
def __init__(self, message: str, display_content: str | None = None):
|
|
super().__init__(message, display_content)
|
|
|
|
|
|
class AbortException(ControlFlowExit):
|
|
"""异常中止当前 Agent 思考流。"""
|
|
|
|
def __init__(self, reason: str, display: Any = None):
|
|
self.reason = reason
|
|
self.display = display
|
|
super().__init__(f"Aborted: {reason}")
|
|
|
|
|
|
class InterventionHandledException(ControlFlowExit):
|
|
"""
|
|
干预成功处理异常。
|
|
当用户的消息被成功作为 STEER 或 FOLLOW_UP 注入到后台运行中的 Agent 队列时抛出,
|
|
用于中断当前的新请求生命周期,避免重复启动。
|
|
"""
|
|
|
|
def __init__(self, message: str, display_content: str | None = None):
|
|
self.message = message
|
|
self.display_content = display_content
|
|
super().__init__(self.message)
|
|
|
|
|
|
class ConcurrencyRejectException(ControlFlowExit):
|
|
"""并发拒绝异常。当 Agent 设置为 REJECT 且正在忙碌时抛出。"""
|
|
|
|
def __init__(self, message: str, display: Any = None):
|
|
self.message = message
|
|
self.display = display or "⏳ 智能体正在处理您的上一个请求,请稍后再试~"
|
|
super().__init__(message)
|
|
|
|
|
|
class ConcurrencyInterruptException(ControlFlowExit):
|
|
"""并发打断异常。当 Agent 设置为 INTERRUPT 且被新请求打断时抛出。"""
|
|
|
|
def __init__(self, message: str):
|
|
self.message = message
|
|
super().__init__(message)
|
|
|
|
|
|
class NeedsInputException(Exception):
|
|
"""
|
|
当工具配置了 interactive=True 且缺少必要参数(或参数验证失败)时抛出此异常,
|
|
用于交由外部中间件捕获并进行 HITL (Human-in-the-loop) 参数补充。
|
|
"""
|
|
|
|
def __init__(
|
|
self, missing_field: str, missing_description: str, original_kwargs: dict
|
|
):
|
|
self.missing_field = missing_field
|
|
self.missing_description = missing_description
|
|
self.original_kwargs = original_kwargs
|
|
super().__init__(
|
|
f"Need input for parameter: {missing_field} - {missing_description}"
|
|
)
|
|
|
|
|
|
class NeedsAuthException(Exception):
|
|
"""
|
|
当工具在执行过程中发现授权失效或凭证过期时主动抛出。
|
|
用于交由外部中间件捕获并重新发起授权 (HITL) 流程。
|
|
"""
|
|
|
|
def __init__(self, provider: str, message: str):
|
|
self.provider = provider
|
|
self.message = message
|
|
super().__init__(f"Needs auth for: {provider} - {message}")
|
|
|
|
|
|
class SandboxPathEscapeError(Exception):
|
|
"""当沙箱内的路径解析结果试图逃逸出允许的工作区根目录时抛出"""
|
|
|
|
def __init__(self, path: str, resolved_path: str | None = None, reason: str = ""):
|
|
self.path = path
|
|
self.resolved_path = resolved_path
|
|
self.reason = reason
|
|
msg = f"沙箱路径逃逸拦截: {path}"
|
|
if resolved_path:
|
|
msg += f" (解析至 {resolved_path})"
|
|
if reason:
|
|
msg += f" - {reason}"
|
|
super().__init__(msg)
|
|
|
|
|
|
class WorkspaceIOError(Exception):
|
|
"""沙箱文件系统读写操作失败"""
|
|
|
|
def __init__(self, path: str, message: str, cause: Exception | None = None):
|
|
self.path = path
|
|
self.cause = cause
|
|
super().__init__(f"沙箱 IO 异常 [{path}]: {message}")
|
|
|
|
|
|
class SandboxFatalError(ToolFatalError):
|
|
"""沙箱底层容器发生致命崩溃(如 OOM, 被宿主机强杀等)"""
|
|
|
|
def __init__(self, message: str, display_content: str | None = None):
|
|
display = display_content or f"❌ 沙箱不可用: {message}"
|
|
super().__init__(message, display_content=display)
|
|
|
|
|
|
class LLMException(Exception):
|
|
"""LLM 服务相关的基础异常类 (多态基类)"""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str,
|
|
details: dict[str, Any] | None = None,
|
|
cause: Exception | None = None,
|
|
):
|
|
self.message = message
|
|
self.details = details or {}
|
|
self.cause = cause
|
|
super().__init__(message)
|
|
|
|
@property
|
|
def is_retryable(self) -> bool:
|
|
"""是否允许在当前节点进行退避重试(如偶发网络抖动)"""
|
|
return False
|
|
|
|
@property
|
|
def should_failover(self) -> bool:
|
|
"""是否允许触发节点故障转移(切换到下一个备用模型)"""
|
|
return False
|
|
|
|
@property
|
|
def should_rotate_key(self) -> bool:
|
|
"""是否应该标记当前 Key 失效并轮换 API Key"""
|
|
return False
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
"""返回适合向用户展示的错误消息"""
|
|
return "AI服务暂时不可用,请稍后再试。"
|
|
|
|
def __str__(self) -> str:
|
|
if self.details:
|
|
safe_details = {k: v for k, v in self.details.items() if k != "api_key"}
|
|
if safe_details:
|
|
return f"{self.message} (详情: {safe_details})"
|
|
return self.message
|
|
|
|
|
|
class InvalidRequestException(LLMException):
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "请求参数错误或API类型不支持,请检查输入内容。"
|
|
|
|
|
|
class ContextLengthExceededException(LLMException):
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "输入内容过长,请缩短后重试。"
|
|
|
|
|
|
class ContentFilteredException(LLMException):
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "内容被安全过滤,请修改后重试。"
|
|
|
|
|
|
class ConfigurationException(LLMException):
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "AI模型配置错误或未找到,请联系管理员检查配置。"
|
|
|
|
|
|
class AuthenticationException(LLMException):
|
|
@property
|
|
def should_rotate_key(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "API密钥无效或权限不足,请联系管理员更新配置。"
|
|
|
|
|
|
class QuotaExceededException(LLMException):
|
|
@property
|
|
def should_rotate_key(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "API使用配额已用尽,请稍后再试或联系管理员。"
|
|
|
|
|
|
class LocationNotSupportedException(LLMException):
|
|
@property
|
|
def should_failover(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return (
|
|
"当前网络环境不支持此 AI 模型。\n"
|
|
"建议: 请尝试更换代理节点至支持的地区或切换备用模型。"
|
|
)
|
|
|
|
|
|
class RateLimitException(LLMException):
|
|
@property
|
|
def is_retryable(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def should_rotate_key(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "请求过于频繁,已被AI服务限流,请稍后再试。"
|
|
|
|
|
|
class UpstreamServerException(LLMException):
|
|
@property
|
|
def is_retryable(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def should_failover(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "AI服务响应异常或端点宕机,请稍后再试。"
|
|
|
|
|
|
class NetworkTimeoutException(LLMException):
|
|
@property
|
|
def is_retryable(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def should_failover(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "AI服务请求超时,请稍后再试。"
|
|
|
|
|
|
class ResponseParseException(LLMException):
|
|
@property
|
|
def is_retryable(self) -> bool:
|
|
return True
|
|
|
|
@property
|
|
def user_friendly_message(self) -> str:
|
|
return "AI服务响应解析失败,请稍后再试。"
|
|
|
|
|
|
def get_user_friendly_error_message(error: Exception) -> str:
|
|
"""将任何异常转换为用户友好的错误消息"""
|
|
if isinstance(error, LLMException):
|
|
return error.user_friendly_message
|
|
|
|
error_str = str(error).lower()
|
|
|
|
if "timeout" in error_str or "timed out" in error_str:
|
|
return "网络请求超时,请检查服务器网络或代理连接。"
|
|
if "connect" in error_str and ("refused" in error_str or "error" in error_str):
|
|
return "无法连接到 AI 服务商,请检查网络连接或代理设置。"
|
|
if "proxy" in error_str:
|
|
return "代理连接失败,请检查代理服务器是否正常运行。"
|
|
if "ssl" in error_str or "certificate" in error_str:
|
|
return "SSL 证书验证失败,请检查网络环境。"
|
|
|
|
return f"服务暂时不可用 ({type(error).__name__}),请稍后再试。"
|