修复智能体事件触发Condition条件无法渲染的问题
This commit is contained in:
parent
fa01e3c5ff
commit
5de12da59c
@ -3,8 +3,9 @@
|
|||||||
|
|
||||||
import jingrow
|
import jingrow
|
||||||
import logging
|
import logging
|
||||||
from jingrow.utils.jinja import render_template
|
from jinja2 import Environment
|
||||||
from jingrow.utils.jingrow_api import get_record_list, get_pagetype_module_app
|
from collections import defaultdict
|
||||||
|
from jingrow.utils.jingrow_api import get_record_list, get_pagetype_module_app, get_record
|
||||||
|
|
||||||
|
|
||||||
def _get_all_local_ai_agents():
|
def _get_all_local_ai_agents():
|
||||||
@ -99,7 +100,30 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
|||||||
trigger = True
|
trigger = True
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
result = render_template(agent.get("condition"), {"pg": pg})
|
# 使用与 ai_content_generation 节点一致的 Jinja2 渲染方式(宽松 undefined)
|
||||||
|
cond_tpl = str(agent.get("condition") or "").strip()
|
||||||
|
jenv = Environment()
|
||||||
|
jtpl = jenv.from_string(cond_tpl)
|
||||||
|
# 统一从触发记录的 pagetype/name 拉取完整记录,构建渲染上下文
|
||||||
|
base_ctx = pg.as_dict() if hasattr(pg, "as_dict") else getattr(pg, "__dict__", {})
|
||||||
|
_pt = base_ctx.get("pagetype") or getattr(pg, "pagetype", None)
|
||||||
|
_nm = base_ctx.get("name") or getattr(pg, "name", None)
|
||||||
|
pg_ctx = base_ctx or {}
|
||||||
|
if _pt and _nm:
|
||||||
|
api_res = get_record(str(_pt), str(_nm))
|
||||||
|
if api_res and api_res.get("success") and isinstance(api_res.get("data"), dict):
|
||||||
|
pg_ctx = api_res.get("data")
|
||||||
|
# 将 None 统一视为空字符串;并为缺失键提供空字符串默认值
|
||||||
|
pg_coerced = {}
|
||||||
|
for _k, _v in (pg_ctx or {}).items():
|
||||||
|
if _v is None:
|
||||||
|
pg_coerced[_k] = ""
|
||||||
|
elif isinstance(_v, str):
|
||||||
|
pg_coerced[_k] = _v
|
||||||
|
else:
|
||||||
|
pg_coerced[_k] = _v
|
||||||
|
pg_safe = defaultdict(lambda: "", pg_coerced)
|
||||||
|
result = jtpl.render(pg=pg_safe)
|
||||||
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:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user