优化local_ai_agent/__init__.py
This commit is contained in:
parent
f94cee7b54
commit
3b0bddcbf3
@ -40,10 +40,24 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
- condition 支持 Jinja2 表达式
|
||||
- 使用本地任务队列,避免链式重复触发
|
||||
"""
|
||||
# 兼容多种调用方式:
|
||||
# 兼容多种调用方式
|
||||
pg = pg or page
|
||||
method = event or method or kwargs.get("event")
|
||||
|
||||
# 支持的事件
|
||||
event_list = [
|
||||
"on_update",
|
||||
"after_insert",
|
||||
"on_submit",
|
||||
"on_cancel",
|
||||
"on_trash",
|
||||
"on_change",
|
||||
"before_update_after_submit",
|
||||
]
|
||||
# 事件快速失败
|
||||
if method not in event_list:
|
||||
return
|
||||
|
||||
ai_agents = _get_all_local_ai_agents()
|
||||
|
||||
# pagetype 分组
|
||||
@ -58,42 +72,46 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
|
||||
agents_by_pagetype = ai_agents.get(pg_pagetype, []) or []
|
||||
|
||||
# module 分组
|
||||
# 获取 pagetype 所属 module(通过 API 获取 module/app)
|
||||
module_name = None
|
||||
try:
|
||||
ma = jingrow.get_module_app(pg_pagetype)
|
||||
if isinstance(ma, dict) and ma.get("success"):
|
||||
module_name = ma.get("module")
|
||||
except Exception as _e:
|
||||
jingrow.log_error("get_module_app failed", f"pagetype={pg_pagetype}", exc=_e)
|
||||
module_name = None
|
||||
# module 分组(仅在存在 module:* 分组时才解析所属模块)
|
||||
agents_by_module = []
|
||||
if module_name:
|
||||
key = f"module:{module_name}"
|
||||
agents_by_module = ai_agents.get(key, []) or []
|
||||
if any(isinstance(k, str) and k.startswith("module:") for k in ai_agents.keys()):
|
||||
module_name = None
|
||||
try:
|
||||
ma = jingrow.get_module_app(pg_pagetype)
|
||||
if isinstance(ma, dict) and ma.get("success"):
|
||||
module_name = ma.get("module")
|
||||
except Exception as _e:
|
||||
jingrow.log_error("get_module_app failed", f"pagetype={pg_pagetype}", exc=_e)
|
||||
module_name = None
|
||||
if module_name:
|
||||
key = f"module:{module_name}"
|
||||
agents_by_module = ai_agents.get(key, []) or []
|
||||
|
||||
# 合并去重
|
||||
# 合并去重(以 name 为键,保持现有行为)
|
||||
merged_agents = list({a.get("name"): a for a in agents_by_pagetype + agents_by_module}.values())
|
||||
|
||||
if not merged_agents:
|
||||
jingrow.log_error("LocalAIAgent", "No Local Ai Agents matched for this page")
|
||||
return
|
||||
|
||||
# 支持的事件
|
||||
event_list = [
|
||||
"on_update",
|
||||
"after_insert",
|
||||
"on_submit",
|
||||
"on_cancel",
|
||||
"on_trash",
|
||||
"on_change",
|
||||
"before_update_after_submit",
|
||||
]
|
||||
# 一次性准备渲染上下文(仅当至少一个 agent 有 condition 时才构建)
|
||||
any_has_condition = any((a.get("condition") or "").strip() for a in merged_agents)
|
||||
base_ctx = None
|
||||
pg_ctx_full = None
|
||||
if any_has_condition:
|
||||
if isinstance(pg, dict):
|
||||
base_ctx = pg
|
||||
else:
|
||||
base_ctx = pg.as_dict() if hasattr(pg, "as_dict") else getattr(pg, "__dict__", {})
|
||||
_pt = (base_ctx or {}).get("pagetype") or (getattr(pg, "pagetype", None))
|
||||
_nm = (base_ctx or {}).get("name") or (getattr(pg, "name", None))
|
||||
if _pt and _nm:
|
||||
res_pg = jingrow.get_pg(str(_pt), str(_nm))
|
||||
if isinstance(res_pg, dict):
|
||||
pg_ctx_full = res_pg
|
||||
|
||||
for agent in merged_agents:
|
||||
event = method if method in event_list else None
|
||||
if event and (agent.get("event_type") == event):
|
||||
if agent.get("event_type") == method:
|
||||
|
||||
trigger = False
|
||||
if not agent.get("condition"):
|
||||
@ -101,18 +119,10 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
else:
|
||||
try:
|
||||
cond_tpl = str(agent.get("condition") or "").strip()
|
||||
# 统一从触发记录的 pagetype/name 拉取完整记录,构建渲染上下文
|
||||
if isinstance(pg, dict):
|
||||
base_ctx = pg
|
||||
else:
|
||||
base_ctx = pg.as_dict() if hasattr(pg, "as_dict") else getattr(pg, "__dict__", {})
|
||||
_pt = base_ctx.get("pagetype") or _get_pg_field(pg, "pagetype")
|
||||
_nm = base_ctx.get("name") or _get_pg_field(pg, "name")
|
||||
pg_ctx = base_ctx or {}
|
||||
if _pt and _nm:
|
||||
api_res = jingrow.get_pg(str(_pt), str(_nm))
|
||||
if isinstance(api_res, dict):
|
||||
pg_ctx = api_res
|
||||
# 构建渲染上下文:优先复用已获取的完整记录
|
||||
pg_ctx = (base_ctx or {})
|
||||
if isinstance(pg_ctx_full, dict):
|
||||
pg_ctx = pg_ctx_full
|
||||
result = render_template(cond_tpl, {"pg": pg_ctx})
|
||||
if str(result).strip().lower() in ("true", "1", "yes"):
|
||||
trigger = True
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user