feat: 工具系统迁移 + 重型插件骨架 + 前端交互增强
- 工具系统从 kilostar/plugin/tool_plugin/ 迁移到 data/toolset/(manifest.json 声明式) - 新增 plugin_runtime 模块:BaseOrganization / GlobalPluginManager / loader / tool_bridge - 新增 org_task + org_task_event 表及 DAO(alembic 0009) - 新增 /api/v1/plugin 路由(submit/status/stream/install/reload) - 新增 data/plugin/example_dept 示例重型插件 - regulatory_node 支持聊天历史上下文注入 - send_file 改为 artifact 存盘 + SSE 推送下载链接 - 前端 WorkflowFileCard 组件 + ToolSettings README 渲染 - utils 整理:合并 access/role_check、standalone_proxy→ray_compat、删除废弃模块 - 项目结构文档移至 docs/STRUCTURE.md 并详细展开 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+57
-34
@@ -1,57 +1,74 @@
|
||||
# Copyright 2026 zhaoxi826
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from typing import Callable, Dict, List
|
||||
from typing import Callable, Dict, List, Optional
|
||||
|
||||
from kilostar.utils.logger import get_logger
|
||||
from kilostar.utils.settings import get_toolset_dir
|
||||
|
||||
logger = get_logger("get_tool")
|
||||
_tool_cache: Dict[str, Callable] = {}
|
||||
_manifest_cache: Optional[Dict[str, Dict]] = None
|
||||
|
||||
|
||||
def _load_manifests() -> Dict[str, Dict]:
|
||||
"""扫描所有 toolset 的 manifest.json,建立 tool_name → {toolset_dir, file} 的映射。"""
|
||||
global _manifest_cache
|
||||
if _manifest_cache is not None:
|
||||
return _manifest_cache
|
||||
|
||||
_manifest_cache = {}
|
||||
toolset_root = get_toolset_dir()
|
||||
if not toolset_root.exists():
|
||||
return _manifest_cache
|
||||
|
||||
for item in toolset_root.iterdir():
|
||||
if not item.is_dir() or item.name.startswith("__"):
|
||||
continue
|
||||
manifest_path = item / "manifest.json"
|
||||
if not manifest_path.exists():
|
||||
continue
|
||||
try:
|
||||
with open(manifest_path, "r", encoding="utf-8") as f:
|
||||
manifest = json.load(f)
|
||||
for tool in manifest.get("tools", []):
|
||||
tool_name = tool.get("name")
|
||||
if tool_name:
|
||||
_manifest_cache[tool_name] = {
|
||||
"toolset_dir": str(item),
|
||||
"toolset_name": item.name,
|
||||
"file": tool.get("file", f"{tool_name}.py"),
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to read manifest {manifest_path}: {e}")
|
||||
|
||||
return _manifest_cache
|
||||
|
||||
|
||||
def _get_tool_func(tool_name: str) -> Callable | None:
|
||||
"""按名字从 ``kilostar/plugin/tool_plugin/<tool_name>/__init__.py`` 中加载工具函数。
|
||||
"""按名字从 toolset 中加载工具函数。
|
||||
|
||||
加载成功后会被缓存到模块级 ``_tool_cache``;找不到目录、找不到同名函数或
|
||||
导入失败都会记录日志并返回 ``None``。
|
||||
根据 manifest 找到工具所在的 toolset 和文件,动态加载模块并取出同名函数。
|
||||
"""
|
||||
func = _tool_cache.get(tool_name, None)
|
||||
func = _tool_cache.get(tool_name)
|
||||
if func:
|
||||
return func
|
||||
|
||||
tool_plugin_dir = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||
"plugin",
|
||||
"tool_plugin",
|
||||
tool_name,
|
||||
)
|
||||
|
||||
if not os.path.exists(tool_plugin_dir) or not os.path.isdir(tool_plugin_dir):
|
||||
logger.error(f"Tool directory not found: {tool_plugin_dir}")
|
||||
manifests = _load_manifests()
|
||||
info = manifests.get(tool_name)
|
||||
if not info:
|
||||
logger.error(f"Tool '{tool_name}' not found in any toolset manifest")
|
||||
return None
|
||||
|
||||
init_file = os.path.join(tool_plugin_dir, "__init__.py")
|
||||
if not os.path.exists(init_file):
|
||||
logger.error(f"Tool init file not found: {init_file}")
|
||||
tool_file = os.path.join(info["toolset_dir"], info["file"])
|
||||
if not os.path.exists(tool_file):
|
||||
logger.error(f"Tool file not found: {tool_file}")
|
||||
return None
|
||||
|
||||
try:
|
||||
module_name = f"kilostar.plugin.tool_plugin.{tool_name}"
|
||||
spec = importlib.util.spec_from_file_location(module_name, init_file)
|
||||
module_name = f"data.toolset.{info['toolset_name']}.{tool_name}"
|
||||
spec = importlib.util.spec_from_file_location(module_name, tool_file)
|
||||
if spec is None or spec.loader is None:
|
||||
logger.error(f"Failed to create spec for {module_name}")
|
||||
return None
|
||||
@@ -70,7 +87,7 @@ def _get_tool_func(tool_name: str) -> Callable | None:
|
||||
_tool_cache[tool_name] = func
|
||||
return func
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load module {module_name}: {e}")
|
||||
logger.error(f"Failed to load module {tool_name}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
@@ -80,6 +97,12 @@ def del_tool_cache(tool_name: str) -> None:
|
||||
del _tool_cache[tool_name]
|
||||
|
||||
|
||||
def invalidate_manifest_cache() -> None:
|
||||
"""清除 manifest 缓存,下次加载时重新扫描磁盘。"""
|
||||
global _manifest_cache
|
||||
_manifest_cache = None
|
||||
|
||||
|
||||
def load_tools_from_list(tool_names: List[str] | None) -> List[Callable]:
|
||||
"""批量加载工具:传入工具名列表,返回成功加载到的函数对象列表(失败项被跳过)。"""
|
||||
if not tool_names:
|
||||
|
||||
Reference in New Issue
Block a user