mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-19 11:10:10 +08:00
♻️ 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>
This commit is contained in:
co-authored by
webjoin111
pre-commit-ci[bot]
parent
0b32d69c9c
commit
922d092650
@@ -22,19 +22,23 @@ class StablePrefix:
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化稳定前缀实例。"""
|
||||
self._snapshot: StablePrefixSnapshot | None = None
|
||||
self._version = 0
|
||||
|
||||
@property
|
||||
def fingerprint(self) -> str:
|
||||
"""获取当前快照的唯一指纹。"""
|
||||
return self._snapshot.fingerprint if self._snapshot else "<unbuilt>"
|
||||
|
||||
@property
|
||||
def version(self) -> int:
|
||||
"""获取当前前缀的版本号。"""
|
||||
return self._version
|
||||
|
||||
@property
|
||||
def built(self) -> bool:
|
||||
"""判断当前是否已构建快照。"""
|
||||
return self._snapshot is not None
|
||||
|
||||
def build(self, system_prompt: list[str], tools: list[Any]) -> bool:
|
||||
@@ -50,9 +54,11 @@ class StablePrefix:
|
||||
return True
|
||||
|
||||
def invalidate(self):
|
||||
"""使当前前缀快照失效。"""
|
||||
self._snapshot = None
|
||||
|
||||
def to_context(self) -> tuple[list[str], list[Any]]:
|
||||
"""将前缀快照导出为上下文元组。"""
|
||||
if not self._snapshot:
|
||||
raise RuntimeError("StablePrefix.to_context() called before build()")
|
||||
return self._snapshot.system_prompt, self._snapshot.tools
|
||||
@@ -60,6 +66,7 @@ class StablePrefix:
|
||||
def _take_snapshot(
|
||||
self, system_prompt: list[str], tools: list[Any]
|
||||
) -> StablePrefixSnapshot:
|
||||
"""为系统提示词与工具列表生成指纹快照。"""
|
||||
parsed_tools = []
|
||||
for t in tools:
|
||||
if hasattr(t, "name"):
|
||||
@@ -83,19 +90,24 @@ class AppendOnlyLog:
|
||||
"""追加写入模式 of 对话日志管理器"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化追加对话日志。"""
|
||||
self._entries: list[Any] = []
|
||||
|
||||
@property
|
||||
def length(self) -> int:
|
||||
"""获取当前对话日志的条数。"""
|
||||
return len(self._entries)
|
||||
|
||||
def append(self, message: Any):
|
||||
"""向对话日志中追加单条消息。"""
|
||||
self._entries.append(message)
|
||||
|
||||
def extend(self, messages: list[Any]):
|
||||
"""批量追加多条消息到对话日志。"""
|
||||
self._entries.extend(messages)
|
||||
|
||||
def clear(self):
|
||||
"""清空所有对话日志。"""
|
||||
self._entries.clear()
|
||||
|
||||
def to_messages(self) -> list[Any]:
|
||||
@@ -110,6 +122,7 @@ class AppendOnlyContextManager:
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化上下文管理器。"""
|
||||
self.prefix = StablePrefix()
|
||||
self.log = AppendOnlyLog()
|
||||
self._last_sync_count = 0
|
||||
@@ -158,7 +171,7 @@ class AppendOnlyContextManager:
|
||||
|
||||
def _compute_digest(self, messages: list[Any]) -> int:
|
||||
"""核心:计算消息列表 of 指纹,用于识别内容篡改。包含 role 与 content。"""
|
||||
from zhenxun.services.ai.core.engine.context_renderer import ContextConverter
|
||||
from .context_renderer import ContextConverter
|
||||
|
||||
payloads = []
|
||||
flattened = ContextConverter.flatten_to_llm_messages(messages)
|
||||
|
||||
@@ -2,7 +2,7 @@ from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
from zhenxun.services.ai.core.messages import AgentEvent, AgentMessage, LLMMessage
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.ai.utils.logger import log_core as logger
|
||||
|
||||
|
||||
class ContextConverter:
|
||||
|
||||
@@ -14,7 +14,7 @@ from zhenxun.services.ai.core.models import ToolDefinition
|
||||
from zhenxun.services.ai.run.models import OutputDataT
|
||||
from zhenxun.services.ai.tools.core.tool import BaseTool
|
||||
from zhenxun.services.ai.tools.models import StructuredSubmissionResult, ToolResult
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.ai.utils.logger import log_core as logger
|
||||
from zhenxun.utils.pydantic_compat import model_json_schema, model_validate
|
||||
|
||||
DEFAULT_IVR_TEMPLATE = (
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
消息与响应域类型定义 - 统一导出门面
|
||||
"""
|
||||
|
||||
from nonebot.compat import PYDANTIC_V2
|
||||
from zhenxun.utils.pydantic_compat import model_rebuild
|
||||
|
||||
from .context_events import (
|
||||
AgentEvent,
|
||||
@@ -59,30 +59,25 @@ from .shared import (
|
||||
from .types import (
|
||||
AgentMessage,
|
||||
AnyLLMMessage,
|
||||
AssistantContentUnion,
|
||||
ContentT,
|
||||
PromptInput,
|
||||
RoleT,
|
||||
SystemContentUnion,
|
||||
ToolContentUnion,
|
||||
UserContentUnion,
|
||||
)
|
||||
|
||||
if PYDANTIC_V2:
|
||||
ChatResponse.model_rebuild()
|
||||
LLMMessage.model_rebuild()
|
||||
SystemMessage.model_rebuild()
|
||||
UserMessage.model_rebuild()
|
||||
AssistantMessage.model_rebuild()
|
||||
ToolMessage.model_rebuild()
|
||||
RerankResponse.model_rebuild()
|
||||
model_rebuild(ChatResponse)
|
||||
model_rebuild(LLMMessage)
|
||||
model_rebuild(SystemMessage)
|
||||
model_rebuild(UserMessage)
|
||||
model_rebuild(AssistantMessage)
|
||||
model_rebuild(ToolMessage)
|
||||
model_rebuild(RerankResponse)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AgentEvent",
|
||||
"AgentMessage",
|
||||
"AnyLLMMessage",
|
||||
"AssistantContentUnion",
|
||||
"AssistantMessage",
|
||||
"AudioPart",
|
||||
"AudioResponse",
|
||||
@@ -112,7 +107,6 @@ __all__ = [
|
||||
"RerankResult",
|
||||
"RoleT",
|
||||
"SpeechRequest",
|
||||
"SystemContentUnion",
|
||||
"SystemMessage",
|
||||
"TaskLifecycleEvent",
|
||||
"TextDeltaPart",
|
||||
@@ -121,7 +115,6 @@ __all__ = [
|
||||
"ThoughtPart",
|
||||
"ToolCallDeltaPart",
|
||||
"ToolCallPart",
|
||||
"ToolContentUnion",
|
||||
"ToolMessage",
|
||||
"ToolReturnPart",
|
||||
"UsageInfo",
|
||||
|
||||
@@ -7,6 +7,7 @@ from typing_extensions import Self
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from zhenxun.services.ai.utils.logger import log_core as logger
|
||||
from zhenxun.utils.pydantic_compat import model_validator
|
||||
|
||||
|
||||
@@ -309,7 +310,6 @@ class EmbedBatch(BaseModel):
|
||||
|
||||
def to_text_only(self, context_name: str) -> list[str]:
|
||||
"""降级工具:将多模态的批量向量安全剔除图片等内容,回退为纯文本数组"""
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
texts = []
|
||||
for payload in self.payloads:
|
||||
|
||||
@@ -12,33 +12,12 @@ from .parts import (
|
||||
ImagePart,
|
||||
LLMContentPart,
|
||||
TextPart,
|
||||
ThoughtPart,
|
||||
ToolCallPart,
|
||||
ToolReturnPart,
|
||||
VideoPart,
|
||||
)
|
||||
|
||||
SystemContentUnion = TextPart
|
||||
"""系统消息允许的内容片段联合类型"""
|
||||
|
||||
UserContentUnion = TextPart | ImagePart | AudioPart | VideoPart | FilePart
|
||||
"""用户消息允许的多模态内容片段联合类型"""
|
||||
|
||||
AssistantContentUnion = (
|
||||
TextPart
|
||||
| ThoughtPart
|
||||
| ToolCallPart
|
||||
| ToolReturnPart
|
||||
| ImagePart
|
||||
| AudioPart
|
||||
| VideoPart
|
||||
| FilePart
|
||||
)
|
||||
"""助手回复允许的内容片段联合类型"""
|
||||
|
||||
ToolContentUnion = ToolReturnPart | ImagePart | AudioPart | VideoPart | FilePart
|
||||
"""工具消息允许的内容片段联合类型"""
|
||||
|
||||
|
||||
RoleT = TypeVar("RoleT", default=str, covariant=True)
|
||||
"""泛型:消息参与者角色类型变量"""
|
||||
@@ -72,12 +51,9 @@ AgentMessage = LLMMessage | AgentEvent
|
||||
__all__ = [
|
||||
"AgentMessage",
|
||||
"AnyLLMMessage",
|
||||
"AssistantContentUnion",
|
||||
"ContentT",
|
||||
"LLMContentPart",
|
||||
"PromptInput",
|
||||
"RoleT",
|
||||
"SystemContentUnion",
|
||||
"ToolContentUnion",
|
||||
"UserContentUnion",
|
||||
]
|
||||
|
||||
@@ -9,7 +9,7 @@ from typing import Any, Generic, Literal, TypeVar
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from zhenxun.services.ai.core.options import GenerationConfig
|
||||
from .options import GenerationConfig
|
||||
|
||||
ModelName = str | None
|
||||
|
||||
@@ -131,6 +131,8 @@ class ModelCapabilities(BaseModel):
|
||||
"""模型支持的输出模态集合。"""
|
||||
supports_tool_calling: bool = False
|
||||
"""是否支持工具调用能力。"""
|
||||
supports_thinking_toggle: bool = False
|
||||
"""是否支持通过 {"thinking": {"type": "enabled/disabled"}} 显式控制思考模式。"""
|
||||
is_embedding_model: bool = False
|
||||
"""是否为嵌入模型。"""
|
||||
is_rerank_model: bool = False
|
||||
@@ -187,7 +189,7 @@ class ModelDetail(BaseModel):
|
||||
"""模型是否可用。"""
|
||||
temperature: float | None = None
|
||||
"""采样温度参数。"""
|
||||
generation_max_tokens: int | None = None
|
||||
max_output_tokens: int | None = None
|
||||
"""单次生成最大 Token 数。"""
|
||||
api_type: str | None = None
|
||||
"""API 类型标识。"""
|
||||
@@ -197,6 +199,10 @@ class ModelDetail(BaseModel):
|
||||
"""显式声明的主任务类型 (如 'image_generation')。"""
|
||||
path_prefix: str | None = Field(default=None)
|
||||
"""中转路由前缀,例如 '/cogvideox' 或 '/minimax'。"""
|
||||
max_input_tokens: int | None = None
|
||||
"""最大输入上下文窗口(用于控制记忆压缩策略)"""
|
||||
reasoning_effort: str | None = None
|
||||
"""该模型的默认思考/推理等级(如 'high', 'low', 'none')"""
|
||||
|
||||
|
||||
__all__ = [
|
||||
|
||||
@@ -212,13 +212,6 @@ class GeminiOptions(BaseProviderOption):
|
||||
"""检索定位配置,如 LBS 经纬度信息,配合 Google Maps 工具使用"""
|
||||
|
||||
|
||||
class DeepSeekOptions(BaseProviderOption):
|
||||
"""DeepSeek 专属特权参数"""
|
||||
|
||||
thinking: bool | None = Field(default=None)
|
||||
"""是否强制开启或关闭 R1 模型的思维链过程"""
|
||||
|
||||
|
||||
class OpenAITTSOptions(BaseProviderOption):
|
||||
"""OpenAI TTS 专属特权参数"""
|
||||
|
||||
@@ -331,8 +324,6 @@ class GenerationConfig(BaseModel):
|
||||
"""OpenAI 厂商专属请求参数集"""
|
||||
gemini_options: GeminiOptions = Field(default_factory=GeminiOptions)
|
||||
"""Gemini 厂商专属请求参数集"""
|
||||
deepseek_options: DeepSeekOptions = Field(default_factory=DeepSeekOptions)
|
||||
"""DeepSeek 厂商专属请求参数集"""
|
||||
|
||||
enable_caching: bool | None = Field(default=None)
|
||||
"""是否在此次生成中开启上下文缓存 (Context Caching)"""
|
||||
|
||||
@@ -67,8 +67,6 @@ class ToolProvider(Protocol):
|
||||
|
||||
async def discover_tools(
|
||||
self,
|
||||
allowed_servers: list[str] | None = None,
|
||||
excluded_servers: list[str] | None = None,
|
||||
) -> dict[str, ToolExecutable]:
|
||||
"""
|
||||
异步发现此提供者提供的所有工具。
|
||||
|
||||
@@ -8,7 +8,7 @@ from typing import Any, TypeVar
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.ai.utils.logger import log_core as logger
|
||||
|
||||
|
||||
class AgentStreamEvent(BaseModel):
|
||||
|
||||
@@ -4,7 +4,7 @@ from typing_extensions import TypeVar
|
||||
|
||||
from jinja2 import Environment
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.ai.utils.logger import log_core as logger
|
||||
from zhenxun.utils.pydantic_compat import model_dump
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
Reference in New Issue
Block a user