Skip to content

Commit 2bb5ca7

Browse files
committed
feat:集成前端工具以实现功能调用与结果处理
1 parent 4551bf1 commit 2bb5ca7

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,
@@ -126,6 +130,7 @@
126130
from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS
127131
from agents.matmaster_agent.utils.event_utils import (
128132
all_text_event,
133+
context_function_call_event,
129134
context_function_event,
130135
is_text,
131136
send_error_event,
@@ -804,6 +809,30 @@ async def _run_async_impl(
804809
yield quota_remaining_event
805810
return
806811

812+
# 在需要验证的节点下发前端工具事件,同一轮 run 内等前端回传(wait_for_frontend_tool_result)
813+
function_call_id = f"added_{str(uuid.uuid4()).replace('-', '')[:24]}"
814+
yield context_function_call_event(
815+
ctx,
816+
self.name,
817+
function_call_id,
818+
DEMO_FRONTEND_TOOL,
819+
ModelRole,
820+
{
821+
'message': '请确认',
822+
'title': '前端 Demo Tool',
823+
'session_id': ctx.session.id,
824+
'invocation_id': ctx.invocation_id,
825+
'function_call_id': function_call_id,
826+
},
827+
)
828+
demo_result = await wait_for_frontend_tool_result(
829+
ctx.session.id, ctx.invocation_id, function_call_id
830+
)
831+
ctx.session.state[DEMO_FRONTEND_TOOL_RESULT_STATE_KEY] = demo_result
832+
yield update_state_event(
833+
ctx, state_delta={DEMO_FRONTEND_TOOL_RESULT_STATE_KEY: demo_result}
834+
)
835+
807836
# 上传文件特殊处理
808837
async for handle_upload_event in self.handle_upload_agent.run_async(ctx):
809838
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)