From 10ad1ba4e35fef95f4062d100ba2aae24dea5fde Mon Sep 17 00:00:00 2001 From: jingrow Date: Mon, 10 Nov 2025 02:55:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=8B=E4=BB=B6=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E9=98=B2=E6=97=A0=E9=99=90=E5=BE=AA=E7=8E=AF=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jingrow/ai/nodes/create_record/create_record.py | 13 ++++++++++++- .../jingrow/ai/nodes/update_record/update_record.py | 13 +++++++++---- .../jingrow/ai/pagetype/local_ai_agent/__init__.py | 12 +++++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/apps/jingrow/jingrow/ai/nodes/create_record/create_record.py b/apps/jingrow/jingrow/ai/nodes/create_record/create_record.py index 3c2610d..8efaf51 100644 --- a/apps/jingrow/jingrow/ai/nodes/create_record/create_record.py +++ b/apps/jingrow/jingrow/ai/nodes/create_record/create_record.py @@ -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") diff --git a/apps/jingrow/jingrow/ai/nodes/update_record/update_record.py b/apps/jingrow/jingrow/ai/nodes/update_record/update_record.py index 64cf3ef..fc523d4 100644 --- a/apps/jingrow/jingrow/ai/nodes/update_record/update_record.py +++ b/apps/jingrow/jingrow/ai/nodes/update_record/update_record.py @@ -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: diff --git a/apps/jingrow/jingrow/ai/pagetype/local_ai_agent/__init__.py b/apps/jingrow/jingrow/ai/pagetype/local_ai_agent/__init__.py index f0de387..3a80c82 100644 --- a/apps/jingrow/jingrow/ai/pagetype/local_ai_agent/__init__.py +++ b/apps/jingrow/jingrow/ai/pagetype/local_ai_agent/__init__.py @@ -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"):