修复本地节点里面的import_local_nodes函数
This commit is contained in:
parent
87c38c8502
commit
4498aad84b
@ -158,13 +158,13 @@ def get_node_schema_fields(node_type: str) -> Dict[str, Any]:
|
|||||||
|
|
||||||
@jingrow.whitelist()
|
@jingrow.whitelist()
|
||||||
def import_local_nodes() -> Dict[str, Any]:
|
def import_local_nodes() -> Dict[str, Any]:
|
||||||
"""一键导入本地节点到Ai Node"""
|
"""一键导入本地节点到 Local Ai Node"""
|
||||||
try:
|
try:
|
||||||
# 扫描本地节点目录
|
# 扫描本地节点目录
|
||||||
base_path = jingrow.get_app_path("jingrow")
|
jingrow_root = get_jingrow_root()
|
||||||
nodes_path = os.path.join(base_path, "ai", "nodes")
|
nodes_path = jingrow_root / "ai" / "nodes"
|
||||||
|
|
||||||
if not os.path.exists(nodes_path):
|
if not nodes_path.exists():
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"matched": 0,
|
"matched": 0,
|
||||||
@ -172,50 +172,51 @@ def import_local_nodes() -> Dict[str, Any]:
|
|||||||
"skipped_existing": 0,
|
"skipped_existing": 0,
|
||||||
"errors": []
|
"errors": []
|
||||||
}
|
}
|
||||||
|
|
||||||
matched = 0
|
matched = 0
|
||||||
imported = 0
|
imported = 0
|
||||||
skipped_existing = 0
|
skipped_existing = 0
|
||||||
errors = []
|
errors = []
|
||||||
|
|
||||||
# 遍历节点目录
|
# 遍历节点目录
|
||||||
for item in os.listdir(nodes_path):
|
for node_dir in nodes_path.iterdir():
|
||||||
item_path = os.path.join(nodes_path, item)
|
if not node_dir.is_dir():
|
||||||
if not os.path.isdir(item_path):
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 检查是否有对应的JSON配置文件
|
# 检查是否有对应的JSON配置文件
|
||||||
json_file = os.path.join(item_path, f"{item}.json")
|
json_file = node_dir / f"{node_dir.name}.json"
|
||||||
if not os.path.exists(json_file):
|
if not json_file.exists():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
matched += 1
|
matched += 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 读取节点配置
|
# 读取节点配置
|
||||||
with open(json_file, 'r', encoding='utf-8') as f:
|
with open(json_file, 'r', encoding='utf-8') as f:
|
||||||
node_config = json.load(f)
|
node_config = json.load(f)
|
||||||
|
|
||||||
# 提取元数据
|
# 提取元数据
|
||||||
metadata = node_config.get('metadata', {})
|
metadata = node_config.get('metadata', {})
|
||||||
node_type = item
|
node_type = metadata.get('type') or node_dir.name
|
||||||
node_label = metadata.get('label', item)
|
node_label = metadata.get('label', node_type)
|
||||||
node_group = metadata.get('group', '')
|
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_icon = metadata.get('icon', 'fa-cube')
|
||||||
node_color = metadata.get('color', '#6b7280')
|
node_color = metadata.get('color', '#6b7280')
|
||||||
node_description = metadata.get('description', '')
|
node_description = metadata.get('description', '')
|
||||||
|
|
||||||
# 检查节点是否已存在
|
# 检查节点是否已存在
|
||||||
existing = jingrow.db.exists('Ai Node', {'node_type': node_type})
|
exists_res = jingrow.get_pg_id('Local Ai Node', field='node_type', value=node_type)
|
||||||
|
if exists_res.get('success'):
|
||||||
if existing:
|
|
||||||
skipped_existing += 1
|
skipped_existing += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 生成 schema(移除 metadata)
|
||||||
|
schema = dict(node_config)
|
||||||
|
schema.pop('metadata', None)
|
||||||
|
|
||||||
# 创建新节点
|
# 创建新节点
|
||||||
node_doc = jingrow.get_pg({
|
result = jingrow.create_pg('Local Ai Node', {
|
||||||
'pagetype': 'Ai Node',
|
|
||||||
'node_type': node_type,
|
'node_type': node_type,
|
||||||
'node_label': node_label,
|
'node_label': node_label,
|
||||||
'node_group': node_group,
|
'node_group': node_group,
|
||||||
@ -223,20 +224,20 @@ def import_local_nodes() -> Dict[str, Any]:
|
|||||||
'node_icon': node_icon,
|
'node_icon': node_icon,
|
||||||
'node_color': node_color,
|
'node_color': node_color,
|
||||||
'node_description': node_description,
|
'node_description': node_description,
|
||||||
'node_schema': node_config,
|
'node_schema': schema,
|
||||||
'status': 'Published'
|
'status': 'Published'
|
||||||
})
|
})
|
||||||
node_doc.insert()
|
|
||||||
imported += 1
|
if result:
|
||||||
|
imported += 1
|
||||||
|
else:
|
||||||
|
errors.append(f"{node_type}: create failed")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = f"导入节点 {item} 失败: {str(e)}"
|
error_msg = f"导入节点 {node_dir.name} 失败: {str(e)}"
|
||||||
jingrow.log_error("导入本地节点失败", error_msg)
|
jingrow.log_error("导入本地节点失败", error_msg)
|
||||||
errors.append(error_msg)
|
errors.append(error_msg)
|
||||||
|
|
||||||
# 提交事务
|
|
||||||
jingrow.db.commit()
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"matched": matched,
|
"matched": matched,
|
||||||
@ -244,9 +245,8 @@ def import_local_nodes() -> Dict[str, Any]:
|
|||||||
"skipped_existing": skipped_existing,
|
"skipped_existing": skipped_existing,
|
||||||
"errors": errors
|
"errors": errors
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
jingrow.db.rollback()
|
|
||||||
jingrow.log_error("导入本地节点失败", str(e))
|
jingrow.log_error("导入本地节点失败", str(e))
|
||||||
return {
|
return {
|
||||||
"success": False,
|
"success": False,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user