Files
zhenxun_bot/zhenxun/services/ai/capabilities/wrappers.py
T
80fc5b86a7 ✨ feat!(llm): 重构并升级大语言模型服务为全新 AI 智能体框架 (#2146)
* ✨ 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>
2026-07-03 08:53:56 +08:00

342 lines
11 KiB
Python

from __future__ import annotations
from collections.abc import Callable
import graphlib
from typing import TYPE_CHECKING, Any
from zhenxun.services.ai.core.messages import ChatRequest, ChatResponse
from zhenxun.services.ai.core.options import GenerationConfig
from .base import (
AbstractCapability,
CapabilityRef,
WrapModelRequestHandler,
WrapRunHandler,
WrapToolExecuteHandler,
WrapToolValidateHandler,
)
if TYPE_CHECKING:
from zhenxun.services.ai.core.models import LLMContext
from zhenxun.services.ai.run import AgentRunResult, RunContext
def sort_capabilities(caps: list["AbstractCapability"]) -> list["AbstractCapability"]:
"""使用标准库 graphlib.TopologicalSorter 实现拦截器拓扑排序,解决执行顺序冲突"""
if len(caps) <= 1:
return caps
ts = graphlib.TopologicalSorter()
n = len(caps)
for i in range(n):
ts.add(i)
orderings = [c.get_ordering() for c in caps]
leaf_types = [{type(c)} for c in caps]
def _ref_matches(
ref: CapabilityRef, types: set[type], inst: AbstractCapability
) -> bool:
if isinstance(ref, type):
return any(issubclass(t, ref) for t in types)
return inst is ref
all_types = set().union(*leaf_types)
for i, o in enumerate(orderings):
if o and o.requires:
for req in o.requires:
if not any(issubclass(t, req) for t in all_types):
raise ValueError(
f"Capability '{type(caps[i]).__name__}' 依赖 '{req.__name__}' "
f"但未在管线中找到该组件。"
)
outermost = {i for i, o in enumerate(orderings) if o and o.position == "outermost"}
innermost = {i for i, o in enumerate(orderings) if o and o.position == "innermost"}
for oi in outermost:
for j in range(n):
if j != oi and j not in outermost:
ts.add(j, oi)
for ii in innermost:
for j in range(n):
if j != ii and j not in innermost:
ts.add(ii, j)
for i, o in enumerate(orderings):
if not o:
continue
for ref in o.wraps:
for j in range(n):
if i != j and _ref_matches(ref, leaf_types[j], caps[j]):
ts.add(j, i)
for ref in o.wrapped_by:
for j in range(n):
if i != j and _ref_matches(ref, leaf_types[j], caps[j]):
ts.add(i, j)
try:
order = list(ts.static_order())
except graphlib.CycleError:
raise ValueError(
"Capability 拓扑排序失败,存在循环依赖约束。"
"请检查 wraps 或 wrapped_by 的配置。"
)
return [caps[i] for i in order]
class CombinedCapability(AbstractCapability):
"""
组合能力容器。
将多个 Capability 按顺序融合成一个复合的洋葱模型,
处理生命周期的正序/倒序和链式调用。
"""
def __init__(self, capabilities: list[AbstractCapability]):
flat = []
for c in capabilities:
if isinstance(c, CombinedCapability):
flat.extend(c.capabilities)
else:
flat.append(c)
deduped = []
seen = set()
for c in flat:
if id(c) not in seen:
seen.add(id(c))
deduped.append(c)
self.capabilities = sort_capabilities(deduped)
async def for_run(self, context: RunContext) -> "AbstractCapability":
new_caps = []
changed = False
for cap in self.capabilities:
new_cap = await cap.for_run(context)
new_caps.append(new_cap)
if new_cap is not cap:
changed = True
if changed:
return CombinedCapability(new_caps)
return self
async def get_generation_config(
self, context: RunContext
) -> GenerationConfig | None:
final_config = None
for cap in self.capabilities:
cap_config = await cap.get_generation_config(context)
if cap_config:
if final_config is None:
final_config = cap_config
else:
final_config = final_config.merge_with(cap_config)
return final_config
async def get_system_prompts(self, context: RunContext) -> list[str]:
prompts = []
for cap in self.capabilities:
prompts.extend(await cap.get_system_prompts(context))
return prompts
async def get_tools(self, context: RunContext) -> list[Any]:
tools = []
for cap in self.capabilities:
tools.extend(await cap.get_tools(context))
return tools
async def prepare_tools(
self, context: RunContext, tool_defs: list[Any]
) -> list[Any]:
current_defs = list(tool_defs)
for cap in self.capabilities:
res = await cap.prepare_tools(context, current_defs)
if res is not None:
current_defs = res
return current_defs
async def wrap_run(
self, context: RunContext, handler: WrapRunHandler
) -> "AgentRunResult[Any]":
chain = handler
for cap in reversed(self.capabilities):
chain = _make_wrap_link(cap, "wrap_run", context, {}, chain, None)
return await chain()
async def wrap_model_request(
self,
context: RunContext,
llm_context: LLMContext[ChatRequest, ChatResponse],
handler: WrapModelRequestHandler,
) -> ChatResponse:
chain = handler
for cap in reversed(self.capabilities):
chain = _make_wrap_link(
cap, "wrap_model_request", context, {}, chain, "llm_context"
)
return await chain(llm_context)
async def wrap_tool_validate(
self,
context: RunContext,
tool_name: str,
args: str | dict[str, Any],
handler: WrapToolValidateHandler,
) -> dict[str, Any]:
chain = handler
for cap in reversed(self.capabilities):
chain = _make_wrap_link(
cap,
"wrap_tool_validate",
context,
{"tool_name": tool_name},
chain,
"args",
)
return await chain(args)
async def wrap_tool_execute(
self,
context: RunContext,
tool_name: str,
arguments: dict[str, Any],
handler: WrapToolExecuteHandler,
) -> Any:
chain = handler
for cap in reversed(self.capabilities):
chain = _make_wrap_link(
cap,
"wrap_tool_execute",
context,
{"tool_name": tool_name},
chain,
"arguments",
)
return await chain(arguments)
def _make_wrap_link(
cap: AbstractCapability,
hook_name: str,
ctx: RunContext,
static_kwargs: dict[str, Any],
inner_handler: Callable[..., Any],
handler_arg: str | None,
) -> Callable[..., Any]:
"""构建洋葱模型中间件链的单一闭包节点。"""
frozen_kwargs = dict(static_kwargs)
if handler_arg:
async def wrapper(value: Any) -> Any:
kw = dict(frozen_kwargs)
kw[handler_arg] = value
hook_method = getattr(cap, hook_name)
return await hook_method(ctx, handler=inner_handler, **kw)
return wrapper
async def wrapper_no_arg() -> Any:
hook_method = getattr(cap, hook_name)
return await hook_method(ctx, handler=inner_handler, **frozen_kwargs)
return wrapper_no_arg
class DynamicCapability(AbstractCapability):
"""动态能力注入:允许在运行时基于上下文生成真正的 Capability"""
def __init__(self, capability_func: Callable):
self.capability_func = capability_func
@classmethod
def get_serialization_name(cls) -> str | None:
return None
async def for_run(self, context: RunContext) -> "AbstractCapability":
from nonebot.utils import is_coroutine_callable
if is_coroutine_callable(self.capability_func):
cap = await self.capability_func(context)
else:
cap = self.capability_func(context)
if cap is None:
return self
return await cap.for_run(context)
class WrapperCapability(AbstractCapability):
"""
代理包装能力基类 (Decorator Pattern)。
默认将所有生命周期钩子透明透传给内部包裹的 (wrapped) 实例。
"""
def __init__(self, wrapped: AbstractCapability):
self.wrapped = wrapped
@classmethod
def get_serialization_name(cls) -> str | None:
return None
async def for_run(self, context: RunContext) -> "AbstractCapability":
new_wrapped = await self.wrapped.for_run(context)
if new_wrapped is self.wrapped:
return self
import copy
new_self = copy.copy(self)
new_self.wrapped = new_wrapped
return new_self
async def get_generation_config(
self, context: RunContext
) -> GenerationConfig | None:
return await self.wrapped.get_generation_config(context)
async def get_system_prompts(self, context: RunContext) -> list[str]:
return await self.wrapped.get_system_prompts(context)
async def get_tools(self, context: RunContext) -> list[Any]:
return await self.wrapped.get_tools(context)
async def prepare_tools(
self, context: RunContext, tool_defs: list[Any]
) -> list[Any]:
return await self.wrapped.prepare_tools(context, tool_defs)
async def wrap_run(
self, context: RunContext, handler: WrapRunHandler
) -> "AgentRunResult[Any]":
return await self.wrapped.wrap_run(context, handler)
async def wrap_model_request(
self,
context: RunContext,
llm_context: LLMContext[ChatRequest, ChatResponse],
handler: WrapModelRequestHandler,
) -> ChatResponse:
return await self.wrapped.wrap_model_request(context, llm_context, handler)
async def wrap_tool_validate(
self,
context: RunContext,
tool_name: str,
args: str | dict[str, Any],
handler: WrapToolValidateHandler,
) -> dict[str, Any]:
return await self.wrapped.wrap_tool_validate(context, tool_name, args, handler)
async def wrap_tool_execute(
self,
context: RunContext,
tool_name: str,
arguments: dict[str, Any],
handler: WrapToolExecuteHandler,
) -> Any:
return await self.wrapped.wrap_tool_execute(
context, tool_name, arguments, handler
)