修复图片生成节点的提示词无法渲染子表变量的问题

This commit is contained in:
jingrow 2025-11-09 21:34:23 +08:00
parent cff0ad3dff
commit 82f00565fa
2 changed files with 66 additions and 40 deletions

View File

@ -9,6 +9,7 @@ from jingrow.ai.api.jart_v1 import call_jingrow_jart_v1_api
from jingrow.ai.api.comfyui_flux import call_jingrow_flux_api from jingrow.ai.api.comfyui_flux import call_jingrow_flux_api
from jingrow.ai.api.comfyui_sd import call_jingrow_sd_api from jingrow.ai.api.comfyui_sd import call_jingrow_sd_api
from jingrow.ai.api.midjourney import process_midjourney_node_task from jingrow.ai.api.midjourney import process_midjourney_node_task
from jingrow.utils.jinja import render_template
def execute(context=None, inputs=None, config=None): def execute(context=None, inputs=None, config=None):
""" """
@ -84,16 +85,7 @@ def execute(context=None, inputs=None, config=None):
} }
try: try:
# 简单的模板变量替换 final_prompts = render_template(prompts, template_context)
final_prompts = prompts
for key, value in template_context.items():
if isinstance(value, (str, int, float)):
# 替换带空格的模板变量 {{ key }}
final_prompts = final_prompts.replace(f"{{{{ {key} }}}}", str(value))
# 替换不带空格的模板变量 {{key}}
final_prompts = final_prompts.replace(f"{{{{{key}}}}}", str(value))
# 替换单花括号模板变量 {key}
final_prompts = final_prompts.replace(f"{{{key}}}", str(value))
except Exception as e: except Exception as e:
return { return {
"success": False, "success": False,

View File

@ -1,4 +1,5 @@
import json import json
import re
import uuid import uuid
import base64 import base64
import requests import requests
@ -197,42 +198,34 @@ def execute(context=None, inputs=None, config=None, **kwargs):
attached_to_field = None attached_to_field = None
attached_to_pagetype = None attached_to_pagetype = None
attached_to_name = None attached_to_name = None
is_subtable_field = False
if save_location != "file" and target_record_info: if save_location != "file" and target_record_info:
attached_to_pagetype = target_record_info["pagetype"] attached_to_pagetype = target_record_info["pagetype"]
attached_to_name = target_record_info["name"] attached_to_name = target_record_info["name"]
if save_field: if save_field:
# 获取字段映射label到fieldname # 检测是否为子表字段格式fieldname[index].subfield支持中文字段名
label2field = jingrow.get_field_mapping_from_jingrow(attached_to_pagetype) # 只支持更新已有行,不支持添加新行(添加新行由其他节点处理)
subtable_match = re.match(r'^([\w\u4e00-\u9fa5]+)\[(\d+)\]\.([\w\u4e00-\u9fa5]+)$', save_field)
# 确定目标字段名 if subtable_match:
target_field = save_field # 子表字段格式上传时不传attached_to_field因为格式不支持上传后单独处理
if save_field in label2field: is_subtable_field = True
target_field = label2field[save_field] attached_to_field = save_field
else: else:
# 尝试直接使用save_field作为字段名 # 普通字段
pass label2field = jingrow.get_field_mapping_from_jingrow(attached_to_pagetype)
target_field = label2field.get(save_field, save_field)
# 检查字段值是否为空 attached_to_field = target_field
if target_field:
current_field_value = jingrow.get_field_value_from_jingrow(
attached_to_pagetype,
attached_to_name,
target_field
)
if not current_field_value:
attached_to_field = target_field
else:
pass
# 上传文件子表字段格式不传给upload_file的attached_to_field但pagetype和name仍然需要传递
upload_result = jingrow.upload_file( upload_result = jingrow.upload_file(
file_data=raw_image_data, file_data=raw_image_data,
filename=filename, filename=filename,
attached_to_pagetype=attached_to_pagetype, attached_to_pagetype=attached_to_pagetype,
attached_to_name=attached_to_name, attached_to_name=attached_to_name,
attached_to_field=attached_to_field attached_to_field=attached_to_field if not is_subtable_field else None
) )
if not upload_result.get('success'): if not upload_result.get('success'):
@ -242,11 +235,52 @@ def execute(context=None, inputs=None, config=None, **kwargs):
# 上传成功,记录文件信息 # 上传成功,记录文件信息
file_url = upload_result.get('file_url') file_url = upload_result.get('file_url')
if file_url: if file_url and save_location != "file":
# 如果关联到字段,更新关联字段 try:
if attached_to_field: # 对于子表字段attached_to_field保存了原始格式如items[0].image
try: # 对于普通字段attached_to_field是映射后的字段名
# 调用Jingrow API更新字段值 if is_subtable_field and attached_to_field:
# 检测是否为子表字段格式:只支持更新已有行(支持中文字段名)
subtable_match = re.match(r'^([\w\u4e00-\u9fa5]+)\[(\d+)\]\.([\w\u4e00-\u9fa5]+)$', attached_to_field)
if subtable_match:
# 处理子表字段:更新指定行的字段值
subtable_field, row_index_str, field_name = subtable_match.groups()
row_index = int(row_index_str)
# 获取字段映射
label2field = jingrow.get_field_mapping_from_jingrow(attached_to_pagetype)
subtable_fieldname = label2field.get(subtable_field, subtable_field)
field_fieldname = label2field.get(field_name, field_name)
# 获取子表数据
subtable_data = jingrow.get_field_value_from_jingrow(
attached_to_pagetype, attached_to_name, subtable_fieldname
) or []
# 只更新已有行,不添加新行
if 0 <= row_index < len(subtable_data):
if not isinstance(subtable_data[row_index], dict):
subtable_data[row_index] = {}
subtable_data[row_index][field_fieldname] = file_url
# 更新整个子表字段
update_api_url = f"{Config.jingrow_server_url}/api/action/jingrow.client.set_value"
headers = get_jingrow_api_headers()
if headers:
update_data = {
'pagetype': attached_to_pagetype,
'name': attached_to_name,
'fieldname': subtable_fieldname,
'value': json.dumps(subtable_data, ensure_ascii=False)
}
requests.post(update_api_url, json=update_data, headers=headers, timeout=10)
else:
log_error(f"子表行索引 {row_index} 超出范围,请先创建子表行")
else:
log_error(f"子表字段格式解析失败: {attached_to_field}")
elif attached_to_field:
# 普通字段更新
update_api_url = f"{Config.jingrow_server_url}/api/action/jingrow.client.set_value" update_api_url = f"{Config.jingrow_server_url}/api/action/jingrow.client.set_value"
headers = get_jingrow_api_headers() headers = get_jingrow_api_headers()
if not headers: if not headers:
@ -260,10 +294,10 @@ def execute(context=None, inputs=None, config=None, **kwargs):
'value': file_url 'value': file_url
} }
update_response = requests.post(update_api_url, json=update_data, headers=headers, timeout=10) requests.post(update_api_url, json=update_data, headers=headers, timeout=10)
except Exception as e: except Exception as e:
log_error(f"更新字段值失败: {str(e)}") log_error(f"更新字段值失败: {str(e)}")
except Exception as e: except Exception as e:
# 记录具体错误信息以便调试 # 记录具体错误信息以便调试
log_error(f"图片上传失败: {str(e)}") log_error(f"图片上传失败: {str(e)}")