From 32cb43b238804345ed8e31e40ee2bbf63f6892c2 Mon Sep 17 00:00:00 2001 From: Rasic2 <1051987201@qq.com> Date: Tue, 3 Feb 2026 21:29:08 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E9=9B=86=E6=88=90=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E5=B7=A5=E5=85=B7=E4=BB=A5=E5=AE=9E=E7=8E=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E8=B0=83=E7=94=A8=E4=B8=8E=E7=BB=93=E6=9E=9C=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agents/matmaster_agent/flow_agents/agent.py | 29 +++++++++++++++++++ .../matmaster_agent/flow_agents/constant.py | 4 +++ 2 files changed, 33 insertions(+) diff --git a/agents/matmaster_agent/flow_agents/agent.py b/agents/matmaster_agent/flow_agents/agent.py index a6d88f8f..a6f36585 100644 --- a/agents/matmaster_agent/flow_agents/agent.py +++ b/agents/matmaster_agent/flow_agents/agent.py @@ -1,9 +1,11 @@ import copy import json import logging +import uuid from asyncio import CancelledError from typing import AsyncGenerator +from app.tools import wait_for_frontend_tool_result from google.adk.agents import InvocationContext, LlmAgent from google.adk.events import Event from opik.integrations.adk import track_adk_agent_recursive @@ -39,6 +41,8 @@ ChatAgentInstruction, ) from agents.matmaster_agent.flow_agents.constant import ( + DEMO_FRONTEND_TOOL, + DEMO_FRONTEND_TOOL_RESULT_STATE_KEY, MATMASTER_FLOW, MATMASTER_FLOW_PLANS, MATMASTER_GENERATE_NPS, @@ -138,6 +142,7 @@ from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS from agents.matmaster_agent.utils.event_utils import ( all_text_event, + context_function_call_event, context_function_event, is_text, send_error_event, @@ -936,6 +941,30 @@ async def _run_async_impl( yield quota_remaining_event return + # 在需要验证的节点下发前端工具事件,同一轮 run 内等前端回传(wait_for_frontend_tool_result) + function_call_id = f"added_{str(uuid.uuid4()).replace('-', '')[:24]}" + yield context_function_call_event( + ctx, + self.name, + function_call_id, + DEMO_FRONTEND_TOOL, + ModelRole, + { + 'message': '请确认', + 'title': '前端 Demo Tool', + 'session_id': ctx.session.id, + 'invocation_id': ctx.invocation_id, + 'function_call_id': function_call_id, + }, + ) + demo_result = await wait_for_frontend_tool_result( + ctx.session.id, ctx.invocation_id, function_call_id + ) + ctx.session.state[DEMO_FRONTEND_TOOL_RESULT_STATE_KEY] = demo_result + yield update_state_event( + ctx, state_delta={DEMO_FRONTEND_TOOL_RESULT_STATE_KEY: demo_result} + ) + # 上传文件特殊处理 async for handle_upload_event in self.handle_upload_agent.run_async(ctx): yield handle_upload_event diff --git a/agents/matmaster_agent/flow_agents/constant.py b/agents/matmaster_agent/flow_agents/constant.py index d3069852..40652b93 100644 --- a/agents/matmaster_agent/flow_agents/constant.py +++ b/agents/matmaster_agent/flow_agents/constant.py @@ -9,6 +9,10 @@ MATMASTER_FLOW = 'matmaster_flow' MATMASTER_FLOW_PLANS = 'matmaster_flow_plans' MATMASTER_GENERATE_NPS = 'matmaster_generate_nps' +# 前端 demo 工具名;ADK 只下发 function_call,等前端执行后回传 function_response 再继续 +DEMO_FRONTEND_TOOL = 'demo_frontend_tool' +# 前端 tool 返回值写入 session.state 的 key,后续逻辑可用 ctx.session.state[DEMO_FRONTEND_TOOL_RESULT_STATE_KEY] 读取 +DEMO_FRONTEND_TOOL_RESULT_STATE_KEY = 'demo_frontend_tool_result' # matmaster_flow 展示文案,直接传给前端(正常执行无标签则传空字符串) EXECUTION_TYPE_LABEL_RETRY = '重试工具'