From 4498aad84b3f84896ed4a6ab4b6384bd371b82d1 Mon Sep 17 00:00:00 2001 From: jingrow Date: Mon, 16 Mar 2026 00:13:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AC=E5=9C=B0=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=87=8C=E9=9D=A2=E7=9A=84import=5Flocal=5Fnodes?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/jingrow/jingrow/ai/utils/jlocal.py | 74 ++++++++++++------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/apps/jingrow/jingrow/ai/utils/jlocal.py b/apps/jingrow/jingrow/ai/utils/jlocal.py index 93eebaa..070fefb 100644 --- a/apps/jingrow/jingrow/ai/utils/jlocal.py +++ b/apps/jingrow/jingrow/ai/utils/jlocal.py @@ -158,13 +158,13 @@ def get_node_schema_fields(node_type: str) -> Dict[str, Any]: @jingrow.whitelist() def import_local_nodes() -> Dict[str, Any]: - """一键导入本地节点到Ai Node""" + """一键导入本地节点到 Local Ai Node""" try: # 扫描本地节点目录 - base_path = jingrow.get_app_path("jingrow") - nodes_path = os.path.join(base_path, "ai", "nodes") - - if not os.path.exists(nodes_path): + jingrow_root = get_jingrow_root() + nodes_path = jingrow_root / "ai" / "nodes" + + if not nodes_path.exists(): return { "success": True, "matched": 0, @@ -172,50 +172,51 @@ def import_local_nodes() -> Dict[str, Any]: "skipped_existing": 0, "errors": [] } - + matched = 0 imported = 0 skipped_existing = 0 errors = [] - + # 遍历节点目录 - for item in os.listdir(nodes_path): - item_path = os.path.join(nodes_path, item) - if not os.path.isdir(item_path): + for node_dir in nodes_path.iterdir(): + if not node_dir.is_dir(): continue - + # 检查是否有对应的JSON配置文件 - json_file = os.path.join(item_path, f"{item}.json") - if not os.path.exists(json_file): + json_file = node_dir / f"{node_dir.name}.json" + if not json_file.exists(): continue - + matched += 1 - + try: # 读取节点配置 with open(json_file, 'r', encoding='utf-8') as f: node_config = json.load(f) - + # 提取元数据 metadata = node_config.get('metadata', {}) - node_type = item - node_label = metadata.get('label', item) + node_type = metadata.get('type') or node_dir.name + node_label = metadata.get('label', node_type) node_group = metadata.get('group', '') - node_component = metadata.get('component', 'GenericNode') + node_component = metadata.get('component_type', 'GenericNode') node_icon = metadata.get('icon', 'fa-cube') node_color = metadata.get('color', '#6b7280') node_description = metadata.get('description', '') - + # 检查节点是否已存在 - existing = jingrow.db.exists('Ai Node', {'node_type': node_type}) - - if existing: + exists_res = jingrow.get_pg_id('Local Ai Node', field='node_type', value=node_type) + if exists_res.get('success'): skipped_existing += 1 continue - + + # 生成 schema(移除 metadata) + schema = dict(node_config) + schema.pop('metadata', None) + # 创建新节点 - node_doc = jingrow.get_pg({ - 'pagetype': 'Ai Node', + result = jingrow.create_pg('Local Ai Node', { 'node_type': node_type, 'node_label': node_label, 'node_group': node_group, @@ -223,20 +224,20 @@ def import_local_nodes() -> Dict[str, Any]: 'node_icon': node_icon, 'node_color': node_color, 'node_description': node_description, - 'node_schema': node_config, + 'node_schema': schema, 'status': 'Published' }) - node_doc.insert() - imported += 1 - + + if result: + imported += 1 + else: + errors.append(f"{node_type}: create failed") + except Exception as e: - error_msg = f"导入节点 {item} 失败: {str(e)}" + error_msg = f"导入节点 {node_dir.name} 失败: {str(e)}" jingrow.log_error("导入本地节点失败", error_msg) errors.append(error_msg) - - # 提交事务 - jingrow.db.commit() - + return { "success": True, "matched": matched, @@ -244,9 +245,8 @@ def import_local_nodes() -> Dict[str, Any]: "skipped_existing": skipped_existing, "errors": errors } - + except Exception as e: - jingrow.db.rollback() jingrow.log_error("导入本地节点失败", str(e)) return { "success": False,