优化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 表达式
|
- condition 支持 Jinja2 表达式
|
||||||
- 使用本地任务队列,避免链式重复触发
|
- 使用本地任务队列,避免链式重复触发
|
||||||
"""
|
"""
|
||||||
# 兼容多种调用方式:
|
# 兼容多种调用方式
|
||||||
pg = pg or page
|
pg = pg or page
|
||||||
method = event or method or kwargs.get("event")
|
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()
|
ai_agents = _get_all_local_ai_agents()
|
||||||
|
|
||||||
# pagetype 分组
|
# 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 []
|
agents_by_pagetype = ai_agents.get(pg_pagetype, []) or []
|
||||||
|
|
||||||
# module 分组
|
# module 分组(仅在存在 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
|
|
||||||
agents_by_module = []
|
agents_by_module = []
|
||||||
if module_name:
|
if any(isinstance(k, str) and k.startswith("module:") for k in ai_agents.keys()):
|
||||||
key = f"module:{module_name}"
|
module_name = None
|
||||||
agents_by_module = ai_agents.get(key, []) or []
|
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())
|
merged_agents = list({a.get("name"): a for a in agents_by_pagetype + agents_by_module}.values())
|
||||||
|
|
||||||
if not merged_agents:
|
if not merged_agents:
|
||||||
jingrow.log_error("LocalAIAgent", "No Local Ai Agents matched for this page")
|
jingrow.log_error("LocalAIAgent", "No Local Ai Agents matched for this page")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 支持的事件
|
# 一次性准备渲染上下文(仅当至少一个 agent 有 condition 时才构建)
|
||||||
event_list = [
|
any_has_condition = any((a.get("condition") or "").strip() for a in merged_agents)
|
||||||
"on_update",
|
base_ctx = None
|
||||||
"after_insert",
|
pg_ctx_full = None
|
||||||
"on_submit",
|
if any_has_condition:
|
||||||
"on_cancel",
|
if isinstance(pg, dict):
|
||||||
"on_trash",
|
base_ctx = pg
|
||||||
"on_change",
|
else:
|
||||||
"before_update_after_submit",
|
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:
|
for agent in merged_agents:
|
||||||
event = method if method in event_list else None
|
if agent.get("event_type") == method:
|
||||||
if event and (agent.get("event_type") == event):
|
|
||||||
|
|
||||||
trigger = False
|
trigger = False
|
||||||
if not agent.get("condition"):
|
if not agent.get("condition"):
|
||||||
@ -101,18 +119,10 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
cond_tpl = str(agent.get("condition") or "").strip()
|
cond_tpl = str(agent.get("condition") or "").strip()
|
||||||
# 统一从触发记录的 pagetype/name 拉取完整记录,构建渲染上下文
|
# 构建渲染上下文:优先复用已获取的完整记录
|
||||||
if isinstance(pg, dict):
|
pg_ctx = (base_ctx or {})
|
||||||
base_ctx = pg
|
if isinstance(pg_ctx_full, dict):
|
||||||
else:
|
pg_ctx = pg_ctx_full
|
||||||
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
|
|
||||||
result = render_template(cond_tpl, {"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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user