mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-04 11:20:01 +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>
228 lines
7.4 KiB
Python
228 lines
7.4 KiB
Python
from collections.abc import Callable
|
|
import re
|
|
from typing import Any, Generic, TypeVar
|
|
from typing_extensions import Self
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from zhenxun.utils.utils import infer_plugin_namespace
|
|
|
|
|
|
class ScopeSelector(BaseModel):
|
|
"""领域驱动:统一的作用域与实体资源选择器"""
|
|
|
|
base_prefix: str | None = None
|
|
"""基础路径前缀。"""
|
|
session_id: str | None = None
|
|
"""特定的会话 ID,如指定则绕过前缀拼接,直接作为统一路径。"""
|
|
platform: str | None = None
|
|
"""目标平台标识(如 'qq')。"""
|
|
group_id: str | None = None
|
|
"""目标群组 ID。"""
|
|
user_id: str | None = None
|
|
"""目标用户 ID。"""
|
|
namespace: str | None = None
|
|
"""插件命名空间标识。"""
|
|
agent_name: str | None = None
|
|
"""具体智能体标识。"""
|
|
bot_id: str | None = None
|
|
"""触发环境的 Bot ID。"""
|
|
custom_dimensions: dict[str, str] = Field(default_factory=dict)
|
|
"""动态的自定义隔离维度字典。"""
|
|
|
|
def get_scope_parts(self) -> list[str]:
|
|
"""获取标准化的路径分段"""
|
|
parts = []
|
|
if self.base_prefix:
|
|
clean = self.base_prefix.strip("/")
|
|
if clean:
|
|
parts.append(clean)
|
|
if self.platform:
|
|
parts.append(f"p_{self.platform}")
|
|
if self.bot_id:
|
|
parts.append(f"b_{self.bot_id}")
|
|
if self.group_id:
|
|
parts.append(f"g_{self.group_id}")
|
|
if self.user_id:
|
|
parts.append(f"u_{self.user_id}")
|
|
if self.namespace:
|
|
parts.append(f"ns_{self.namespace}")
|
|
if self.agent_name:
|
|
parts.append(f"ag_{self.agent_name}")
|
|
for k, v in self.custom_dimensions.items():
|
|
parts.append(f"{k}_{v}")
|
|
return parts
|
|
|
|
@property
|
|
def scope_prefix(self) -> str:
|
|
"""统一的路径生成逻辑"""
|
|
if self.session_id:
|
|
return self.session_id
|
|
parts = self.get_scope_parts()
|
|
return "/" + "/".join(parts) if parts else "/"
|
|
|
|
|
|
T_Builder = TypeVar("T_Builder", bound="BaseScopeBuilder")
|
|
|
|
|
|
class BaseScopeBuilder(Generic[T_Builder]):
|
|
"""
|
|
泛型化作用域构建器基类 (Fluent Builder)。
|
|
为继承 of 子类提供极简的链式调用 API,用于快速指定平台、群组、用户等。
|
|
"""
|
|
|
|
def __init__(self):
|
|
self._selector = ScopeSelector()
|
|
|
|
def bot(self, b: str) -> Self:
|
|
"""指定目标 Bot ID"""
|
|
self._selector.bot_id = b
|
|
return self
|
|
|
|
def platform(self, p: str) -> Self:
|
|
"""指定目标平台标识 (如 'qq')"""
|
|
self._selector.platform = p
|
|
return self
|
|
|
|
def group(self, g: str) -> Self:
|
|
"""指定目标群组 ID"""
|
|
self._selector.group_id = g
|
|
return self
|
|
|
|
def user(self, u: str) -> Self:
|
|
"""指定目标用户 ID"""
|
|
self._selector.user_id = u
|
|
return self
|
|
|
|
def namespace(self, ns: str) -> Self:
|
|
"""指定插件命名空间 (如 'rpg_game')"""
|
|
self._selector.namespace = ns
|
|
return self
|
|
|
|
def agent(self, a: str) -> Self:
|
|
"""指定具体的 Agent 智能体名称"""
|
|
self._selector.agent_name = a
|
|
return self
|
|
|
|
def session(self, sid: str) -> Self:
|
|
"""直接指定完整的 Session ID 绕过前缀拼接"""
|
|
self._selector.session_id = sid
|
|
return self
|
|
|
|
def current(self, bot: Any = None, event: Any = None) -> Self:
|
|
"""自动提取当前触发上下文的特征,匹配当前用户/群组"""
|
|
from zhenxun.services.ai.run.context import NoneBotDeps
|
|
from zhenxun.services.ai.utils.runtime import ContextUtils
|
|
|
|
deps = (
|
|
NoneBotDeps(bot=bot, event=event)
|
|
if bot and event
|
|
else NoneBotDeps.get_current()
|
|
)
|
|
if deps:
|
|
self._selector.platform = ContextUtils.extract_platform(deps)
|
|
bot_inst = getattr(deps, "bot", None)
|
|
if bot_inst and hasattr(bot_inst, "self_id"):
|
|
self._selector.bot_id = str(bot_inst.self_id)
|
|
self._selector.group_id = ContextUtils.extract_group_id(deps)
|
|
self._selector.user_id = ContextUtils.extract_user_id(deps)
|
|
return self
|
|
|
|
|
|
def normalize_scope_path(path: str) -> str:
|
|
"""标准化作用域路径,消除多余的斜杠并确保以 / 开头"""
|
|
if not path or path == "/":
|
|
return "/"
|
|
path = re.sub(r"/+", "/", path)
|
|
if not path.startswith("/"):
|
|
path = "/" + path
|
|
if len(path) > 1:
|
|
path = path.rstrip("/")
|
|
return path
|
|
|
|
|
|
class ScopeBuilder:
|
|
"""流式作用域声明构建器,用于在顶层配置并在底层延迟提取具体的维度值"""
|
|
|
|
def __init__(self):
|
|
"""初始化作用域声明构建器。"""
|
|
self._dims: set[str] = set()
|
|
self._customs: dict[str, Callable[[Any], str | None]] = {}
|
|
|
|
def bot(self) -> Self:
|
|
"""声明隔离维度包含 Bot ID。"""
|
|
self._dims.add("bot")
|
|
return self
|
|
|
|
def platform(self) -> Self:
|
|
"""声明隔离维度包含平台类型。"""
|
|
self._dims.add("platform")
|
|
return self
|
|
|
|
def group(self) -> Self:
|
|
"""声明隔离维度包含群组 ID。"""
|
|
self._dims.add("group")
|
|
return self
|
|
|
|
def user(self) -> Self:
|
|
"""声明隔离维度包含用户 ID。"""
|
|
self._dims.add("user")
|
|
return self
|
|
|
|
def namespace(self) -> Self:
|
|
"""声明隔离维度包含插件命名空间。"""
|
|
self._dims.add("namespace")
|
|
return self
|
|
|
|
def agent(self) -> Self:
|
|
"""声明隔离维度包含智能体名称。"""
|
|
self._dims.add("agent")
|
|
return self
|
|
|
|
def custom(
|
|
self, key: str, value_extractor: str | Callable[[Any], str | None]
|
|
) -> Self:
|
|
"""声明自定义的隔离维度及值提取器。"""
|
|
if isinstance(value_extractor, str):
|
|
self._customs[key] = lambda _: value_extractor
|
|
else:
|
|
self._customs[key] = value_extractor
|
|
return self
|
|
|
|
def resolve(
|
|
self,
|
|
deps: Any,
|
|
prefix: str = "",
|
|
default_namespace: str | None = None,
|
|
default_agent: str | None = None,
|
|
) -> ScopeSelector:
|
|
"""解析当前上下文依赖并生成作用域选择器实例。"""
|
|
from zhenxun.services.ai.utils.runtime import ContextUtils
|
|
|
|
selector = ScopeSelector(base_prefix=prefix)
|
|
if "platform" in self._dims:
|
|
selector.platform = ContextUtils.extract_platform(deps)
|
|
if "bot" in self._dims:
|
|
bot_inst = getattr(deps, "bot", None)
|
|
if bot_inst and hasattr(bot_inst, "self_id"):
|
|
selector.bot_id = str(bot_inst.self_id)
|
|
if "group" in self._dims:
|
|
selector.group_id = ContextUtils.extract_group_id(deps)
|
|
if "user" in self._dims:
|
|
selector.user_id = ContextUtils.extract_user_id(deps)
|
|
if "namespace" in self._dims:
|
|
selector.namespace = (
|
|
default_namespace
|
|
or getattr(deps, "namespace", None)
|
|
or infer_plugin_namespace(default="global")
|
|
)
|
|
if "agent" in self._dims:
|
|
selector.agent_name = default_agent
|
|
|
|
for k, extractor in self._customs.items():
|
|
val = extractor(deps)
|
|
if val is not None:
|
|
selector.custom_dimensions[k] = str(val)
|
|
|
|
return selector
|