更新render_template,确保渲染正常
This commit is contained in:
parent
368ac78d63
commit
41a4eead05
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
import jingrow
|
import jingrow
|
||||||
import logging
|
import logging
|
||||||
from jinja2 import Environment
|
|
||||||
from jingrow.utils.jingrow_api import get_record_list, get_pagetype_module_app, get_record
|
from jingrow.utils.jingrow_api import get_record_list, get_pagetype_module_app, get_record
|
||||||
|
from jingrow.utils.jinja import render_template
|
||||||
|
|
||||||
|
|
||||||
def _get_all_local_ai_agents():
|
def _get_all_local_ai_agents():
|
||||||
@ -99,10 +99,8 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
|||||||
trigger = True
|
trigger = True
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# 使用与 ai_content_generation 节点一致的 Jinja2 渲染方式(宽松 undefined)
|
# 使用统一的 render_template 进行 Jinja2 条件渲染
|
||||||
cond_tpl = str(agent.get("condition") or "").strip()
|
cond_tpl = str(agent.get("condition") or "").strip()
|
||||||
jenv = Environment()
|
|
||||||
jtpl = jenv.from_string(cond_tpl)
|
|
||||||
# 统一从触发记录的 pagetype/name 拉取完整记录,构建渲染上下文
|
# 统一从触发记录的 pagetype/name 拉取完整记录,构建渲染上下文
|
||||||
base_ctx = pg.as_dict() if hasattr(pg, "as_dict") else getattr(pg, "__dict__", {})
|
base_ctx = pg.as_dict() if hasattr(pg, "as_dict") else getattr(pg, "__dict__", {})
|
||||||
_pt = base_ctx.get("pagetype") or getattr(pg, "pagetype", None)
|
_pt = base_ctx.get("pagetype") or getattr(pg, "pagetype", None)
|
||||||
@ -112,7 +110,7 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
|||||||
api_res = get_record(str(_pt), str(_nm))
|
api_res = get_record(str(_pt), str(_nm))
|
||||||
if api_res and api_res.get("success") and isinstance(api_res.get("data"), dict):
|
if api_res and api_res.get("success") and isinstance(api_res.get("data"), dict):
|
||||||
pg_ctx = api_res.get("data")
|
pg_ctx = api_res.get("data")
|
||||||
result = jtpl.render(pg=pg_ctx)
|
result = render_template(cond_tpl, {"pg": pg_ctx})
|
||||||
if str(result).strip().lower() in ("true", "1", "yes"):
|
if str(result).strip().lower() in ("true", "1", "yes"):
|
||||||
trigger = True
|
trigger = True
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import json
|
import json
|
||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional
|
||||||
from jinja2 import Environment
|
|
||||||
|
|
||||||
from jingrow.utils.jingrow_api import get_record
|
from jingrow.utils.jingrow_api import get_record
|
||||||
from jingrow.utils.jingrow_cloud import call_ai_model
|
from jingrow.utils.jingrow_cloud import call_ai_model
|
||||||
|
from jingrow.utils.jinja import render_template
|
||||||
|
|
||||||
def execute(context=None, inputs=None, config=None):
|
def execute(context=None, inputs=None, config=None):
|
||||||
"""
|
"""
|
||||||
@ -90,11 +90,9 @@ def execute(context=None, inputs=None, config=None):
|
|||||||
if key not in template_context or not template_context.get(key):
|
if key not in template_context or not template_context.get(key):
|
||||||
template_context[key] = value
|
template_context[key] = value
|
||||||
|
|
||||||
# 4. 渲染模板(使用Jinja2,容错处理undefined变量)
|
# 4. 渲染模板(使用统一的 render_template,容错处理undefined变量)
|
||||||
try:
|
try:
|
||||||
jenv = Environment()
|
prompt = render_template(template, template_context)
|
||||||
jtpl = jenv.from_string(template)
|
|
||||||
prompt = jtpl.render(**template_context)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {
|
return {
|
||||||
"success": False,
|
"success": False,
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
最佳实践原则:
|
最佳实践原则:
|
||||||
- 复用单例 Environment,启用 trim_blocks/lstrip_blocks 提升可读性;
|
- 复用单例 Environment,启用 trim_blocks/lstrip_blocks 提升可读性;
|
||||||
- 采用 StrictUndefined,缺失变量时报错,避免静默吞错;
|
- 默认采用宽松 Undefined(与 Jinja2 默认一致),便于条件表达式渲染;
|
||||||
- 不启用 autoescape(后端逻辑条件渲染,不输出到 HTML)。
|
- 不启用 autoescape(后端逻辑条件渲染,不输出到 HTML)。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -22,10 +22,10 @@ def _get_env():
|
|||||||
if _env is not None:
|
if _env is not None:
|
||||||
return _env
|
return _env
|
||||||
try:
|
try:
|
||||||
from jinja2 import Environment, StrictUndefined
|
from jinja2 import Environment, Undefined
|
||||||
_env = Environment(
|
_env = Environment(
|
||||||
autoescape=False,
|
autoescape=False,
|
||||||
undefined=StrictUndefined,
|
undefined=Undefined,
|
||||||
trim_blocks=True,
|
trim_blocks=True,
|
||||||
lstrip_blocks=True,
|
lstrip_blocks=True,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user