mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-07 21:00:21 +08:00
* ♻️ refactor(core): 重构 AI 能力与定时任务调度系统 - 【AI 能力与工具】重构 Capability 注册与管理机制,引入 CapabilityManager 统一管理 - 移除全局能力注册表,改用声明式装饰器 `@capability` 进行解耦注册 - 重构工具解析器链,使用统一的 BaseToolResolver 代替原有的多个特定解析器 - 增强工具查询过滤,支持通配符匹配、工具箱过滤和排除标签 - 【定时任务调度】重构定时任务管理器,引入 SchedulerRegistry 统一管理任务元数据 - 引入 JobConfig 聚合定时任务配置,支持用户维度的定时任务调度 - 重构执行分发器,支持并发限制、串行间隔和随机延迟打散 - 【运行上下文】引入 ScheduledDeps 以支持后台和定时任务环境下的依赖注入 - 优化 RunContext,支持从定时任务上下文快速构造,并提供 emit 辅助方法 - 【日志与监控】引入 AILoggerProxy,实现 AI 各模块的专属日志输出 - 将各模块的全局 logger 替换为对应的模块专属日志代理 - 【其他优化】修复 Pydantic V1 兼容层中 model_validator 的装饰器兼容性问题 - 在非交互式环境(如定时任务)中自动隐藏 HITL 交互工具以节省 Token * ♻️ refactor(core): 优化内部导入路径并提升 Pydantic 兼容性 - 【重构】将 `services/ai` 模块内的绝对导入重构为相对导入,优化包结构 - 【重构】移除不必要的 `if TYPE_CHECKING` 保护,通过 `from __future__ import annotations` 直接导入类型 - 【清理】清理 `core/messages/types.py` 中未使用的 `AssistantContentUnion` 等联合类型定义 - 【优化】在 `utils/pydantic_compat.py` 中新增 `model_rebuild` 兼容函数,统一 Pydantic V1/V2 的模型重建逻辑 - 【优化】将部分函数内部的延迟导入提升至模块顶部,规范代码结构 * ♻️ refactor(imports): 优化导入路径为相对导入并清理冗余导入 - 【重构】将 AI 服务相关模块中的绝对导入路径修改为相对导入,提升模块内聚性与可移植性 - 【清理】移除多处函数内部或类方法中未使用的冗余导入,避免循环引用和资源浪费 - 【格式化】微调部分工具装饰器和返回语句的格式与尾随逗号 * 🚨 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>
268 lines
8.7 KiB
Python
268 lines
8.7 KiB
Python
from __future__ import annotations
|
||
|
||
"""
|
||
运行时(Run)相关核心类型定义
|
||
"""
|
||
|
||
from collections.abc import AsyncIterator, Callable
|
||
import json
|
||
from typing import Any, Generic, cast
|
||
from typing_extensions import TypeVar
|
||
|
||
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr
|
||
|
||
from zhenxun.services.ai.core.messages import AgentMessage, LLMMessage, UsageInfo
|
||
from zhenxun.services.ai.core.options import BaseOutputDefinition
|
||
from zhenxun.services.ai.core.protocols.tool import ToolResolvable
|
||
from zhenxun.services.ai.core.stream_events import (
|
||
AgentStreamEvent,
|
||
EventBus,
|
||
ToolCallStartEvent,
|
||
ToolStreamChunkEvent,
|
||
)
|
||
from zhenxun.services.ai.guardrails import BaseGuardrail, GuardrailSource
|
||
from zhenxun.services.ai.tools.core.tool import BaseTool
|
||
from zhenxun.utils.pydantic_compat import model_dump, model_validator
|
||
|
||
|
||
class ChatSummary(BaseModel):
|
||
total: int = 0
|
||
"""大模型调用总次数"""
|
||
total_latency_ms: float = 0.0
|
||
"""大模型调用总耗时(毫秒)"""
|
||
by_stop_reason: dict[str, int] = Field(default_factory=dict)
|
||
"""按停止原因分类的大模型调用计数"""
|
||
|
||
|
||
class ToolSummary(BaseModel):
|
||
total: int = 0
|
||
"""工具执行总次数"""
|
||
ok: int = 0
|
||
"""工具成功执行次数"""
|
||
error: int = 0
|
||
"""工具执行失败次数"""
|
||
total_latency_ms: float = 0.0
|
||
"""工具执行总耗时(毫秒)"""
|
||
by_name: dict[str, dict[str, float | int]] = Field(default_factory=dict)
|
||
"""按工具名称细分的执行状态统计"""
|
||
|
||
|
||
class AgentRunSummary(BaseModel):
|
||
"""Agent 单次运行的全局可观测性遥测摘要"""
|
||
|
||
chats: ChatSummary = Field(default_factory=ChatSummary)
|
||
"""大模型调用遥测摘要"""
|
||
tools: ToolSummary = Field(default_factory=ToolSummary)
|
||
"""工具执行遥测摘要"""
|
||
usage: UsageInfo = Field(default_factory=UsageInfo)
|
||
"""Token 消耗总计"""
|
||
total_latency_ms: float = 0.0
|
||
"""智能体运行总耗时(毫秒)"""
|
||
|
||
|
||
class HandoffPayload(BaseModel):
|
||
"""移交信息载荷"""
|
||
|
||
target: str
|
||
"""移交的目标智能体名称"""
|
||
reason: str = ""
|
||
"""移交的原因描述"""
|
||
context_data: Any = ""
|
||
"""随移交传递的上下文数据"""
|
||
|
||
|
||
OutputDataT = TypeVar("OutputDataT", default=str)
|
||
|
||
|
||
class AgentRunResult(BaseModel, Generic[OutputDataT]):
|
||
"""Agent 单次无状态运行的结果"""
|
||
|
||
output: OutputDataT
|
||
"""最终输出数据"""
|
||
messages: list[AgentMessage] = Field(default_factory=list)
|
||
"""本次运行产生/更新的历史消息"""
|
||
usage: UsageInfo = Field(default_factory=UsageInfo)
|
||
"""本次运行的Token消耗总计"""
|
||
structured_data: Any | None = None
|
||
"""拦截到的结构化结果字典"""
|
||
telemetry: AgentRunSummary | None = None
|
||
"""单次运行的完整可观测性遥测摘要"""
|
||
handoff: HandoffPayload | None = None
|
||
"""向外抛出的软移交载荷(存在时说明Agent发起了移交请求)"""
|
||
|
||
class Config:
|
||
arbitrary_types_allowed = True
|
||
|
||
@property
|
||
def llm_messages(self) -> list[LLMMessage]:
|
||
"""动态视图:过滤掉内部业务事件 (AgentEvent),
|
||
仅返回纯净的底层聊天消息历史"""
|
||
return [m for m in self.messages if isinstance(m, LLMMessage)]
|
||
|
||
|
||
class AgentRunStart(AgentStreamEvent):
|
||
"""智能体运行开始"""
|
||
|
||
agent_name: str
|
||
"""启动运行的智能体名称"""
|
||
|
||
|
||
class AgentRunError(AgentStreamEvent):
|
||
"""智能体运行发生异常"""
|
||
|
||
error: BaseException
|
||
"""智能体运行过程中抛出的异常"""
|
||
|
||
|
||
class AgentRunEnd(AgentStreamEvent):
|
||
"""智能体运行完全结束"""
|
||
|
||
result: AgentRunResult[Any]
|
||
"""智能体运行结束后的完整结果"""
|
||
|
||
|
||
class StreamedRunResult(Generic[OutputDataT]):
|
||
"""
|
||
智能体流式运行的结果代理对象。
|
||
提供高度解耦的方法来消费底层事件流,支持获取纯净文本或全部事件。
|
||
"""
|
||
|
||
def __init__(self, event_bus: EventBus):
|
||
self._event_bus = event_bus
|
||
self.is_complete: bool = False
|
||
self._result: AgentRunResult[OutputDataT] | None = None
|
||
|
||
async def stream_events(self) -> "AsyncIterator[AgentStreamEvent]":
|
||
"""获取底层的所有原始事件(包含工具调用过程等)"""
|
||
|
||
async for event in self._event_bus:
|
||
if isinstance(event, AgentRunEnd):
|
||
self._result = cast(AgentRunResult[OutputDataT], event.result)
|
||
self.is_complete = True
|
||
elif isinstance(event, AgentRunError):
|
||
raise event.error.with_traceback(None) from None
|
||
yield event
|
||
|
||
async def stream_text(self, delta: bool = False) -> AsyncIterator[str]:
|
||
"""
|
||
过滤大模型的输出文本。
|
||
"""
|
||
full_text = ""
|
||
async for _ in self.stream_events():
|
||
pass
|
||
|
||
if self._result is not None:
|
||
output = self._result.output
|
||
if isinstance(output, str):
|
||
full_text = output
|
||
else:
|
||
if isinstance(output, BaseModel):
|
||
full_text = json.dumps(model_dump(output), ensure_ascii=False)
|
||
else:
|
||
full_text = str(output)
|
||
|
||
if delta:
|
||
yield full_text
|
||
else:
|
||
yield full_text
|
||
|
||
async def get_output(self) -> OutputDataT:
|
||
"""阻塞并等待整个 Agent 执行完毕,返回最终的解析输出数据"""
|
||
if self._result is not None:
|
||
return self._result.output
|
||
|
||
async for _ in self.stream_events():
|
||
pass
|
||
|
||
if self._result is None:
|
||
raise RuntimeError("Agent 运行异常结束,未产生最终结果。")
|
||
|
||
return self._result.output
|
||
|
||
async def get_run_result(self) -> AgentRunResult[OutputDataT]:
|
||
"""获取完整的运行结果对象(包含 Token 消耗等)"""
|
||
if self._result is not None:
|
||
return self._result
|
||
|
||
await self.get_output()
|
||
|
||
return cast(AgentRunResult[OutputDataT], self._result)
|
||
|
||
async def forward_to(
|
||
self, target_event_bus: EventBus | None, prefix_name: str
|
||
) -> AgentRunResult[OutputDataT]:
|
||
"""将底层的事件流自动格式化并转发给另一个事件发射器,
|
||
常用于 DelegateTool 嵌套调用"""
|
||
async for event in self.stream_events():
|
||
if not target_event_bus:
|
||
continue
|
||
|
||
if isinstance(event, ToolStreamChunkEvent):
|
||
await target_event_bus.emit(
|
||
ToolStreamChunkEvent(
|
||
tool_name=f"{prefix_name} -> {event.tool_name}",
|
||
content=event.content,
|
||
metadata=event.metadata,
|
||
)
|
||
)
|
||
elif isinstance(event, ToolCallStartEvent):
|
||
intent_str = (
|
||
f" (意图: {event.intent})" if getattr(event, "intent", None) else ""
|
||
)
|
||
await target_event_bus.emit(
|
||
ToolStreamChunkEvent(
|
||
tool_name=prefix_name,
|
||
content=f"🔁 正在调用工具: {event.tool_name}...{intent_str}",
|
||
)
|
||
)
|
||
|
||
return await self.get_run_result()
|
||
|
||
|
||
class AgentTask(BaseModel):
|
||
"""标准化数据契约(意图载体 Payload),定义大模型需要做什么及产出什么格式"""
|
||
|
||
id: str = Field(default_factory=lambda: __import__("uuid").uuid4().hex)
|
||
"""任务的唯一标识符"""
|
||
|
||
name: str | None = None
|
||
"""任务的简短名称"""
|
||
|
||
description: str
|
||
"""详细的任务指令(告诉大模型具体需要做什么)"""
|
||
|
||
expected_output: str
|
||
"""预期输出的自然语言描述(指导大模型如何组织最终答案)"""
|
||
|
||
response_model: type[BaseModel] | BaseOutputDefinition | None = None
|
||
"""强制要求返回的强类型结构 (Pydantic Model) 或
|
||
OutputDefinition,为空则返回普通文本"""
|
||
|
||
tools: list[str | Callable | dict[str, Any] | BaseTool | ToolResolvable] | None = (
|
||
None
|
||
)
|
||
"""针对此特定任务动态追加或覆盖的工具列表"""
|
||
|
||
guardrails: list[GuardrailSource] | None = None
|
||
"""护栏验证列表。支持传入函数、BaseGuardrail 实例,
|
||
或直接传入自然语言字符串规则(自动转为 LLM 裁判)"""
|
||
|
||
_parsed_guardrails: list[BaseGuardrail] = PrivateAttr(default_factory=list)
|
||
|
||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||
|
||
@model_validator(mode="after")
|
||
def _parse_and_set_guardrails(self) -> AgentTask:
|
||
from zhenxun.services.ai.guardrails import parse_guardrails
|
||
|
||
self._parsed_guardrails = parse_guardrails(self.guardrails)
|
||
return self
|
||
|
||
|
||
__all__ = [
|
||
"AgentRunResult",
|
||
"AgentTask",
|
||
"OutputDataT",
|
||
"StreamedRunResult",
|
||
]
|