Skip to content

Commit 6104bad

Browse files
committed
feat:集成前端工具以实现功能调用与结果处理
1 parent 94ffcc2 commit 6104bad

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,
@@ -935,6 +940,30 @@ async def _run_async_impl(
935940
yield quota_remaining_event
936941
return
937942

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