Skip to content

Commit 32cb43b

Browse files
committed
feat:集成前端工具以实现功能调用与结果处理
1 parent f8d2591 commit 32cb43b

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import copy
22
import json
33
import logging
4+
import uuid
45
from asyncio import CancelledError
56
from typing import AsyncGenerator
67

8+
from app.tools import wait_for_frontend_tool_result
79
from google.adk.agents import InvocationContext, LlmAgent
810
from google.adk.events import Event
911
from opik.integrations.adk import track_adk_agent_recursive
@@ -39,6 +41,8 @@
3941
ChatAgentInstruction,
4042
)
4143
from agents.matmaster_agent.flow_agents.constant import (
44+
DEMO_FRONTEND_TOOL,
45+
DEMO_FRONTEND_TOOL_RESULT_STATE_KEY,
4246
MATMASTER_FLOW,
4347
MATMASTER_FLOW_PLANS,
4448
MATMASTER_GENERATE_NPS,
@@ -138,6 +142,7 @@
138142
from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS
139143
from agents.matmaster_agent.utils.event_utils import (
140144
all_text_event,
145+
context_function_call_event,
141146
context_function_event,
142147
is_text,
143148
send_error_event,
@@ -936,6 +941,30 @@ async def _run_async_impl(
936941
yield quota_remaining_event
937942
return
938943

944+
# 在需要验证的节点下发前端工具事件,同一轮 run 内等前端回传(wait_for_frontend_tool_result)
945+
function_call_id = f"added_{str(uuid.uuid4()).replace('-', '')[:24]}"
946+
yield context_function_call_event(
947+
ctx,
948+
self.name,
949+
function_call_id,
950+
DEMO_FRONTEND_TOOL,
951+
ModelRole,
952+
{
953+
'message': '请确认',
954+
'title': '前端 Demo Tool',
955+
'session_id': ctx.session.id,
956+
'invocation_id': ctx.invocation_id,
957+
'function_call_id': function_call_id,
958+
},
959+
)
960+
demo_result = await wait_for_frontend_tool_result(
961+
ctx.session.id, ctx.invocation_id, function_call_id
962+
)
963+
ctx.session.state[DEMO_FRONTEND_TOOL_RESULT_STATE_KEY] = demo_result
964+
yield update_state_event(
965+
ctx, state_delta={DEMO_FRONTEND_TOOL_RESULT_STATE_KEY: demo_result}
966+
)
967+
939968
# 上传文件特殊处理
940969
async for handle_upload_event in self.handle_upload_agent.run_async(ctx):
941970
yield handle_upload_event

agents/matmaster_agent/flow_agents/constant.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
MATMASTER_FLOW = 'matmaster_flow'
1010
MATMASTER_FLOW_PLANS = 'matmaster_flow_plans'
1111
MATMASTER_GENERATE_NPS = 'matmaster_generate_nps'
12+
# 前端 demo 工具名;ADK 只下发 function_call,等前端执行后回传 function_response 再继续
13+
DEMO_FRONTEND_TOOL = 'demo_frontend_tool'
14+
# 前端 tool 返回值写入 session.state 的 key,后续逻辑可用 ctx.session.state[DEMO_FRONTEND_TOOL_RESULT_STATE_KEY] 读取
15+
DEMO_FRONTEND_TOOL_RESULT_STATE_KEY = 'demo_frontend_tool_result'
1216

1317
# matmaster_flow 展示文案,直接传给前端(正常执行无标签则传空字符串)
1418
EXECUTION_TYPE_LABEL_RETRY = '重试工具'

0 commit comments

Comments
 (0)