mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-30 17:20:03 +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>
132 lines
5.2 KiB
Python
132 lines
5.2 KiB
Python
import os
|
|
from pathlib import Path
|
|
import re
|
|
from typing import Any
|
|
|
|
from zhenxun.services.ai.tools.core.decorators import tool
|
|
from zhenxun.services.ai.tools.models import ToolResult
|
|
from zhenxun.services.ai.utils.logger import log_knowledge as logger
|
|
|
|
from .base import BaseKnowledge
|
|
|
|
|
|
class FileSystemKnowledge(BaseKnowledge):
|
|
"""
|
|
纯文本文件系统知识库。
|
|
零依赖,无需向量数据库。通过大模型原生工具 (grep, list, read) 让其自主翻阅本地文件。
|
|
"""
|
|
|
|
default_instructions = (
|
|
"## 本地文件知识库\n"
|
|
"你拥有访问本地专业文档的权限。在回答问题前,请遵循以下流程:\n"
|
|
"1. **搜索**:优先使用 `search_knowledge_files` 通过关键词查找相关内容。\n"
|
|
"2. **概览**:如果需要了解文件结构,使用 `list_knowledge_files`。\n"
|
|
"3. **阅读**:找到目标后,使用 `read_knowledge_file` 获取完整上下文。\n"
|
|
"**核心原则**:严禁凭空捏造事实,必须基于文件内容提取信息。"
|
|
)
|
|
|
|
def __init__(
|
|
self,
|
|
base_dir: str | Path,
|
|
allowed_extensions: tuple[str, ...] | None = None,
|
|
**kwargs: Any,
|
|
):
|
|
"""
|
|
初始化文本文件系统知识库。
|
|
|
|
参数:
|
|
base_dir: 知识库对应的本地根目录路径。
|
|
allowed_extensions: 允许读取和检索的文件后缀元组,
|
|
默认支持 txt, md, json, csv, yaml, log。
|
|
**kwargs: 透传给父类 BaseKnowledge 的额外参数。
|
|
"""
|
|
super().__init__(**kwargs)
|
|
self.base_dir = Path(base_dir).resolve()
|
|
self.allowed_extensions = allowed_extensions or (
|
|
".txt",
|
|
".md",
|
|
".json",
|
|
".csv",
|
|
".yaml",
|
|
".log",
|
|
)
|
|
if not self.base_dir.exists():
|
|
logger.warning(f"[FileSystemKnowledge] 警告:目录不存在 {self.base_dir}")
|
|
self.base_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def _is_safe_path(self, target_path: Path) -> bool:
|
|
"""安全检查:防止跨目录访问"""
|
|
try:
|
|
return target_path.resolve().is_relative_to(self.base_dir)
|
|
except Exception:
|
|
return False
|
|
|
|
@tool(
|
|
name="search_knowledge_files",
|
|
description="在知识库中搜索包含指定关键词的文件内容和上下文。",
|
|
)
|
|
async def search_knowledge_files(self, keyword: str) -> ToolResult:
|
|
"""扫描所有文本文件,返回包含关键词的行及上下文。"""
|
|
results = []
|
|
try:
|
|
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
|
|
except Exception:
|
|
return ToolResult(output=f"无效的搜索关键词: {keyword}").as_error()
|
|
|
|
for root, _, files in os.walk(self.base_dir):
|
|
for file in files:
|
|
if not file.endswith(self.allowed_extensions):
|
|
continue
|
|
|
|
file_path = Path(root) / file
|
|
try:
|
|
content = file_path.read_text(encoding="utf-8", errors="ignore")
|
|
lines = content.splitlines()
|
|
|
|
matches = []
|
|
for i, line in enumerate(lines):
|
|
if pattern.search(line):
|
|
start = max(0, i - 1)
|
|
end = min(len(lines), i + 2)
|
|
context = "\n".join(lines[start:end])
|
|
matches.append(context)
|
|
|
|
if matches:
|
|
rel_path = file_path.relative_to(self.base_dir).as_posix()
|
|
match_text = "\n---\n".join(matches[:5])
|
|
results.append(f"📁 文件: {rel_path}\n{match_text}")
|
|
except Exception:
|
|
continue
|
|
|
|
if not results:
|
|
return ToolResult(output=f"未找到包含 '{keyword}' 的内容。尝试更换关键词。")
|
|
|
|
final_output = "\n\n======\n\n".join(results[:10])
|
|
return ToolResult(output=final_output)
|
|
|
|
@tool(name="list_knowledge_files", description="列出知识库中所有可用的文档路径。")
|
|
async def list_knowledge_files(self) -> ToolResult:
|
|
files = []
|
|
for root, _, filenames in os.walk(self.base_dir):
|
|
for filename in filenames:
|
|
file_path = Path(root) / filename
|
|
rel_path = file_path.relative_to(self.base_dir).as_posix()
|
|
files.append(rel_path)
|
|
|
|
if not files:
|
|
return ToolResult(output="知识库当前为空。")
|
|
return ToolResult(output="可用文件列表:\n" + "\n".join(files))
|
|
|
|
@tool(
|
|
name="read_knowledge_file", description="读取知识库中指定文件的完整文本内容。"
|
|
)
|
|
async def read_knowledge_file(self, file_path: str) -> ToolResult:
|
|
target = (self.base_dir / file_path).resolve()
|
|
if not self._is_safe_path(target):
|
|
return ToolResult(output="❌ 安全拦截:越权访问").as_error()
|
|
if not target.is_file():
|
|
return ToolResult(output=f"❌ 文件不存在: {file_path}").as_error()
|
|
|
|
content = target.read_text(encoding="utf-8", errors="ignore")
|
|
return ToolResult(output=content)
|