diff --git a/apps/jingrow/jingrow/ai/nodes/ai_image_generation/ai_image_generation.py b/apps/jingrow/jingrow/ai/nodes/ai_image_generation/ai_image_generation.py index 208da13..1967c4f 100644 --- a/apps/jingrow/jingrow/ai/nodes/ai_image_generation/ai_image_generation.py +++ b/apps/jingrow/jingrow/ai/nodes/ai_image_generation/ai_image_generation.py @@ -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_sd import call_jingrow_sd_api 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): """ @@ -84,16 +85,7 @@ def execute(context=None, inputs=None, config=None): } try: - # 简单的模板变量替换 - 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)) + final_prompts = render_template(prompts, template_context) except Exception as e: return { "success": False, diff --git a/apps/jingrow/jingrow/ai/nodes/image_upload/image_upload.py b/apps/jingrow/jingrow/ai/nodes/image_upload/image_upload.py index 393e52f..8138a01 100644 --- a/apps/jingrow/jingrow/ai/nodes/image_upload/image_upload.py +++ b/apps/jingrow/jingrow/ai/nodes/image_upload/image_upload.py @@ -1,4 +1,5 @@ import json +import re import uuid import base64 import requests @@ -197,42 +198,34 @@ def execute(context=None, inputs=None, config=None, **kwargs): attached_to_field = None attached_to_pagetype = None attached_to_name = None + is_subtable_field = False if save_location != "file" and target_record_info: attached_to_pagetype = target_record_info["pagetype"] attached_to_name = target_record_info["name"] if save_field: - # 获取字段映射(label到fieldname) - label2field = jingrow.get_field_mapping_from_jingrow(attached_to_pagetype) + # 检测是否为子表字段格式:fieldname[index].subfield(支持中文字段名) + # 只支持更新已有行,不支持添加新行(添加新行由其他节点处理) + subtable_match = re.match(r'^([\w\u4e00-\u9fa5]+)\[(\d+)\]\.([\w\u4e00-\u9fa5]+)$', save_field) - # 确定目标字段名 - target_field = save_field - if save_field in label2field: - target_field = label2field[save_field] + if subtable_match: + # 子表字段格式,上传时不传attached_to_field(因为格式不支持),上传后单独处理 + is_subtable_field = True + attached_to_field = save_field else: - # 尝试直接使用save_field作为字段名 - pass - - # 检查字段值是否为空 - 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 + # 普通字段 + label2field = jingrow.get_field_mapping_from_jingrow(attached_to_pagetype) + target_field = label2field.get(save_field, save_field) + attached_to_field = target_field + # 上传文件:子表字段格式不传给upload_file的attached_to_field,但pagetype和name仍然需要传递 upload_result = jingrow.upload_file( file_data=raw_image_data, filename=filename, attached_to_pagetype=attached_to_pagetype, 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'): @@ -242,11 +235,52 @@ def execute(context=None, inputs=None, config=None, **kwargs): # 上传成功,记录文件信息 file_url = upload_result.get('file_url') - if file_url: - # 如果关联到字段,更新关联字段 - if attached_to_field: - try: - # 调用Jingrow API更新字段值 + if file_url and save_location != "file": + try: + # 对于子表字段,attached_to_field保存了原始格式(如items[0].image) + # 对于普通字段,attached_to_field是映射后的字段名 + 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" headers = get_jingrow_api_headers() if not headers: @@ -260,10 +294,10 @@ def execute(context=None, inputs=None, config=None, **kwargs): '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: - log_error(f"更新字段值失败: {str(e)}") + except Exception as e: + log_error(f"更新字段值失败: {str(e)}") except Exception as e: # 记录具体错误信息以便调试 log_error(f"图片上传失败: {str(e)}")