优化node_management.py

This commit is contained in:
jingrow 2025-11-03 03:12:22 +08:00
parent d348a656c0
commit 5a758b3a8f

View File

@ -13,6 +13,7 @@ from jingrow.utils.fs import atomic_write_json
from jingrow.utils.jingrow_api import get_record_id, create_record, update_record, get_record_list
from jingrow.utils.auth import get_jingrow_cloud_url, get_jingrow_cloud_api_headers
from jingrow.config import Config
from jingrow.utils.path import get_root_path, get_apps_path
logger = logging.getLogger(__name__)
@ -33,8 +34,7 @@ async def export_node_definition(payload: Dict[str, Any]):
export_data = {"metadata": metadata, **(schema or {})}
current_file = Path(__file__).resolve()
jingrow_root = current_file.parents[1] # 修正路径层级
jingrow_root = get_apps_path() / "jingrow"
new_root = jingrow_root / "ai" / "nodes"
target = new_root / node_type / f"{node_type}.json"
atomic_write_json(target, export_data)
@ -49,8 +49,7 @@ async def import_local_node_definitions():
扫描本地节点定义目录 metadata 去重后导入到 Local Ai Node
"""
try:
current_file = Path(__file__).resolve()
jingrow_root = current_file.parents[1] # 修正路径层级
jingrow_root = get_apps_path() / "jingrow"
nodes_root = jingrow_root / "ai" / "nodes"
if not nodes_root.exists():
return {"success": True, "matched": 0, "imported": 0, "skipped_existing": 0}
@ -128,8 +127,7 @@ async def get_all_node_metadata():
获取所有节点的元数据用于流程编排界面
"""
try:
current_file = Path(__file__).resolve()
jingrow_root = current_file.parents[1] # 修正路径层级
jingrow_root = get_apps_path() / "jingrow"
nodes_root = jingrow_root / "ai" / "nodes"
if not nodes_root.exists():
@ -177,8 +175,7 @@ async def get_node_schema(node_type: str):
获取指定节点类型的Schema配置
"""
try:
current_file = Path(__file__).resolve()
jingrow_root = current_file.parents[1]
jingrow_root = get_apps_path() / "jingrow"
nodes_root = jingrow_root / "ai" / "nodes"
json_file = nodes_root / node_type / f"{node_type}.json"
@ -309,8 +306,7 @@ async def get_installed_node_types():
"""
try:
# 确定节点目录路径apps/jingrow/jingrow/ai/nodes
current_file = Path(__file__).resolve()
jingrow_root = current_file.parents[1] # jingrow
jingrow_root = get_apps_path() / "jingrow"
nodes_root = jingrow_root / "ai" / "nodes"
node_types = []
@ -339,8 +335,7 @@ async def install_node_from_url(url: str = Form(...)):
"""从URL安装节点"""
try:
# 下载文件
current = Path(__file__).resolve()
root = current.parents[4]
root = get_root_path()
tmp_dir = root / "tmp"
tmp_dir.mkdir(parents=True, exist_ok=True)
@ -375,8 +370,7 @@ async def install_node_from_url(url: str = Form(...)):
async def install_node_from_git(repo_url: str = Form(...)):
"""从git仓库克隆并安装节点"""
try:
current = Path(__file__).resolve()
root = current.parents[4]
root = get_root_path()
tmp_dir = root / "tmp"
tmp_dir.mkdir(parents=True, exist_ok=True)
@ -533,10 +527,7 @@ def _install_single_node_directory(node_dir: str) -> Dict[str, Any]:
return {'success': False, 'error': '节点定义中缺少 metadata.type'}
# 确定目标目录apps/jingrow/jingrow/ai/nodes
current_file = Path(__file__).resolve()
# node_management.py 位于 jingrow/api/
# parents[0] = jingrow/api, parents[1] = jingrow
jingrow_root = current_file.parents[1] # jingrow
jingrow_root = get_apps_path() / "jingrow"
nodes_root = jingrow_root / "ai" / "nodes"
nodes_root.mkdir(parents=True, exist_ok=True)
@ -598,8 +589,7 @@ async def package_node(node_type: str):
try:
from datetime import datetime
current_file = Path(__file__).resolve()
jingrow_root = current_file.parents[1]
jingrow_root = get_apps_path() / "jingrow"
nodes_root = jingrow_root / "ai" / "nodes"
node_dir = nodes_root / node_type
@ -607,7 +597,7 @@ async def package_node(node_type: str):
raise HTTPException(status_code=404, detail=f"节点目录不存在: {node_type}")
# 创建临时打包目录
root = current_file.parents[4]
root = get_root_path()
tmp_dir = root / "tmp"
tmp_dir.mkdir(parents=True, exist_ok=True)