优化事件触发防无限循环逻辑
This commit is contained in:
parent
8b3f7bcaba
commit
10ad1ba4e3
@ -97,7 +97,18 @@ def execute(context=None, inputs=None, config=None):
|
||||
mapped_fields.add(label)
|
||||
|
||||
# 调用 Jingrow 创建记录
|
||||
created = jingrow.create_pg(record_type, record_data)
|
||||
# 【防无限循环】如果当前在智能体执行上下文中,设置智能体ID标记
|
||||
agent_name = None
|
||||
if isinstance(context, dict):
|
||||
agent_name = context.get("agent_name") or context.get("agent_id")
|
||||
|
||||
if agent_name:
|
||||
jingrow.skip_hooks(f"agent:{agent_name}")
|
||||
try:
|
||||
created = jingrow.create_pg(record_type, record_data)
|
||||
finally:
|
||||
if agent_name:
|
||||
jingrow.restore_hooks()
|
||||
if created is None:
|
||||
return {"success": False, "error": "创建记录失败"}
|
||||
created_name = created.get("name") or record_data.get("name")
|
||||
|
||||
@ -160,13 +160,18 @@ def execute(context=None, inputs=None, config=None):
|
||||
|
||||
# 调用 Jingrow 更新记录(仅当有更新)
|
||||
if updated:
|
||||
# 【防无限循环】设置上下文标记,表示当前更新来自智能体执行
|
||||
# 这样在 on_update 钩子中就不会再次触发智能体,防止循环
|
||||
jingrow.skip_hooks("agent")
|
||||
# 【防无限循环】如果当前在智能体执行上下文中,设置智能体ID标记
|
||||
agent_name = None
|
||||
if isinstance(context, dict):
|
||||
agent_name = context.get("agent_name") or context.get("agent_id")
|
||||
|
||||
if agent_name:
|
||||
jingrow.skip_hooks(f"agent:{agent_name}")
|
||||
try:
|
||||
server_record = jingrow.update_pg(record_type, record_name, mock_record)
|
||||
finally:
|
||||
jingrow.restore_hooks()
|
||||
if agent_name:
|
||||
jingrow.restore_hooks()
|
||||
if server_record is False:
|
||||
return {"success": False, "error": "更新记录失败"}
|
||||
if server_record is True:
|
||||
|
||||
@ -44,10 +44,12 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
pg = pg or page
|
||||
method = event or method or kwargs.get("event")
|
||||
|
||||
# 【防无限循环】检查上下文标记:如果当前调用来自智能体执行,跳过
|
||||
# 【防无限循环】检查上下文标记:如果当前调用来自智能体执行,提取智能体ID
|
||||
# 使用 contextvars.ContextVar 上下文标记,支持同步和异步场景,防止循环触发
|
||||
if jingrow.get_hook_source() == "agent":
|
||||
return
|
||||
hook_source = jingrow.get_hook_source()
|
||||
current_agent_id = None
|
||||
if hook_source and hook_source.startswith("agent:"):
|
||||
current_agent_id = hook_source[6:]
|
||||
|
||||
# 支持的事件
|
||||
event_list = [
|
||||
@ -117,6 +119,10 @@ def run_agent(pg=None, method=None, event=None, page=None, **kwargs):
|
||||
for agent in merged_agents:
|
||||
if agent.get("event_type") == method:
|
||||
agent_name = agent.get("name")
|
||||
|
||||
# 【防无限循环】如果当前在智能体执行上下文中,且要触发的智能体与当前执行的智能体相同,则跳过
|
||||
if current_agent_id and agent_name == current_agent_id:
|
||||
continue # 跳过同一智能体的触发,防止循环
|
||||
|
||||
trigger = False
|
||||
if not agent.get("condition"):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user