Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions agents/matmaster_agent/flow_agents/agent.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions agents/matmaster_agent/flow_agents/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '重试工具'
Expand Down
Loading