Files
zhenxun_bot/zhenxun/services/ai/flow/agent/models.py
T
922d092650 ♻️ refactor(core): 重构 AI 能力与定时任务调度系统 (#2148)
* ♻️ 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>
2026-07-10 09:14:06 +08:00

182 lines
7.6 KiB
Python

from __future__ import annotations
from collections.abc import Sequence
from pathlib import Path
from typing import Any
from pydantic import BaseModel, ConfigDict, Field
from zhenxun.services.ai.capabilities import CapabilitySource, CombinedCapability
from zhenxun.services.ai.context.memory.builder import MemoryBuilder
from zhenxun.services.ai.context.memory.engine import MemoryReader, MemoryWriter
from zhenxun.services.ai.context.memory.models import MemoryConfig
from zhenxun.services.ai.context.memory.types import SessionMetadata
from zhenxun.services.ai.core.messages import (
AgentMessage,
ChatResponse,
LLMMessage,
ToolCallPart,
UsageInfo,
)
from zhenxun.services.ai.core.options import GenerationConfig
from zhenxun.services.ai.flow.base import BaseRuntimeConfig
from zhenxun.services.ai.run import RunContext
from zhenxun.services.ai.run.models import AgentRunResult, AgentTask, HandoffPayload
from zhenxun.services.ai.tools.core.toolkit import BaseToolkit
from zhenxun.services.ai.tools.engine.registry import ToolCollection
from zhenxun.utils.pydantic_compat import model_copy
class Persona(BaseModel):
"""智能体人设与上下文背景"""
role: str = Field(...)
"""扮演的角色身份"""
goal: str = Field(...)
"""角色的核心目标"""
backstory: str | None = Field(default=None)
"""角色背景故事或性格设定"""
model_config = ConfigDict(extra="ignore") # type: ignore
class AgentConfig(BaseRuntimeConfig):
"""统一的智能体全局与单次运行配置 (Unification of Settings & Profile)"""
model_config = ConfigDict(arbitrary_types_allowed=True)
max_cycles: int = Field(default=10)
"""工具调用最大循环次数"""
global_max_cycles: int | None = Field(default=None)
"""整个会话生命周期内的绝对最大循环次数上限(覆盖全局配置)。"""
enable_parallel_calls: bool = Field(default=True)
"""允许并行工具调用"""
reflexion_retries: int = Field(default=1)
"""反思重试次数"""
enable_fallback_summary: bool = Field(default=True)
"""达到最大循环次数时,是否触发大模型兜底总结(而不是直接报错)"""
enable_hitl: bool | None = Field(default=None)
"""是否允许智能体主动挂起任务,向用户求助 (Human-in-the-Loop)。
若为 None 则跟随全局设置。
"""
message_history: Sequence[AgentMessage] | None = Field(default=None)
"""初始化的底层对话历史记录。"""
memory: MemoryConfig | MemoryBuilder | bool | None = Field(default=None)
"""单次运行级别的记忆门面覆盖 (支持 bool, MemoryConfig, MemoryBuilder)。"""
generation_config: GenerationConfig | None = Field(default=None)
"""单次运行覆盖的大模型生成配置。"""
capabilities: list[CapabilitySource] | None = Field(default=None)
"""仅针对本次运行动态注入的临时拦截器/能力组件列表。"""
skills: Sequence[str | Path | Any] | None = Field(default=None)
"""仅针对本次运行动态注入的临时技能集合。"""
executor: Any | None = Field(default=None)
"""单次运行覆盖的核心执行引擎策略 (BaseAgentExecutor)。"""
verbose_ui: bool = Field(default=False)
"""是否在 UI 前端展示细粒度的工具执行中间过程。
在不支持流式更新的平台(如QQ)建议保持 False。"""
def merge_with(self, other: "AgentConfig | dict | None") -> "AgentConfig":
"""深度合并另一份配置,生成一个新的覆盖实例"""
if not other:
return model_copy(self, deep=True)
update_dict = {}
if isinstance(other, dict):
other_dict = {k: v for k, v in other.items() if v is not None}
else:
fields_set = getattr(
other, "model_fields_set", getattr(other, "__fields_set__", set())
)
other_dict = {}
for k in fields_set:
val = getattr(other, k)
if val is not None:
other_dict[k] = val
for k, v in other_dict.items():
if k in ("capabilities", "skills") and isinstance(v, list):
base_list = getattr(self, k) or []
update_dict[k] = base_list + v
else:
update_dict[k] = v
return model_copy(self, update=update_dict, deep=True)
class AgentState(BaseModel):
"""大模型思考循环的有限状态机 (FSM) 流转状态"""
model_config = ConfigDict(arbitrary_types_allowed=True)
static_system_prompt: str | list[str] = ""
"""绝对不变的系统提示词(用于前缀缓存)"""
dynamic_system_messages: list[LLMMessage] = Field(default_factory=list)
"""包含变量与实时状态的动态独立提示消息列表(绝对头部注入)"""
tools: ToolCollection | None = None
"""当前轮次生效的、已完成鉴权和过滤的工具集合"""
messages: list[AgentMessage] = Field(default_factory=list)
"""大模型将看到的完整历史消息列表 (执行历史)"""
usage: UsageInfo = Field(default_factory=UsageInfo)
"""累计的 Token 消耗"""
structured_result: Any | None = None
"""拦截到的结构化输出结果"""
early_result_output: Any | None = None
"""拦截到的早期终止输出结果"""
should_terminate: bool = False
"""标记是否应提前终止循环"""
handoff_triggered: HandoffPayload | None = None
"""标记是否触发了移交"""
is_finished: bool = False
"""标记大模型循环是否彻底结束"""
final_result: AgentRunResult[Any] | None = None
"""最终的运行结果 (AgentRunResult)"""
origin_msg_len: int = 0
"""初始进入循环时的消息历史长度 (用于增量保存记忆)"""
current_cycle: int = 0
"""当前思考循环的轮次索引"""
current_request_messages: list[AgentMessage] = Field(default_factory=list)
"""当前即将发往大模型的实际请求消息"""
current_request_extra: dict[str, Any] = Field(default_factory=dict)
"""当前请求附加的Extra控制参数"""
current_response: ChatResponse | None = None
"""大模型最新返回的响应实体"""
current_tool_calls: list[ToolCallPart] = Field(default_factory=list)
"""当前轮次被提取出准备执行的客户端工具调用"""
current_tool_results: list[Any] = Field(default_factory=list)
"""当前轮次工具执行的结果或异常收集"""
class AgentRunResources(BaseModel):
"""大模型执行过程中的全局静态资源与配置载体"""
model_config = ConfigDict(arbitrary_types_allowed=True)
run_context: RunContext
"""保留依赖注入(DI)与黑板引用的全局运行时上下文"""
session_meta: SessionMetadata | None = None
"""隔离会话的元信息(Session ID, 命名空间, 权限等)"""
memory_reader: MemoryReader | None = None
"""用于读取短/中/长期上下文记忆的读取器"""
memory_writer: MemoryWriter | None = None
"""用于将对话历史安全落盘的写入器"""
run_scoped_cap: CombinedCapability | None = None
"""聚合了 Agent/AgentTask/全局 的复合能力拦截器 (CombinedCapability)"""
task_obj: AgentTask | None = None
"""(如有) 解析后的结构化数据任务契约"""
toolkits: list[BaseToolkit] = Field(default_factory=list)
"""当前轮次生效的工具箱列表 (需要执行生命周期挂载)"""
config: AgentConfig = Field(default_factory=AgentConfig)
"""Agent 全局与运行时的统一策略配置"""
generation_config: GenerationConfig | None = None
"""大模型生成配置"""
model_name: str | None = None
"""当前实际调用的模型名称"""