|
1 | 1 | import copy |
2 | 2 | import json |
3 | 3 | import logging |
| 4 | +import uuid |
4 | 5 | from asyncio import CancelledError |
5 | 6 | from typing import AsyncGenerator |
6 | 7 |
|
| 8 | +from app.tools import wait_for_frontend_tool_result |
7 | 9 | from google.adk.agents import InvocationContext, LlmAgent |
8 | 10 | from google.adk.events import Event |
9 | 11 | from opik.integrations.adk import track_adk_agent_recursive |
|
39 | 41 | ChatAgentInstruction, |
40 | 42 | ) |
41 | 43 | from agents.matmaster_agent.flow_agents.constant import ( |
| 44 | + DEMO_FRONTEND_TOOL, |
| 45 | + DEMO_FRONTEND_TOOL_RESULT_STATE_KEY, |
42 | 46 | MATMASTER_FLOW, |
43 | 47 | MATMASTER_FLOW_PLANS, |
44 | 48 | MATMASTER_GENERATE_NPS, |
|
138 | 142 | from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS |
139 | 143 | from agents.matmaster_agent.utils.event_utils import ( |
140 | 144 | all_text_event, |
| 145 | + context_function_call_event, |
141 | 146 | context_function_event, |
142 | 147 | is_text, |
143 | 148 | send_error_event, |
@@ -936,6 +941,30 @@ async def _run_async_impl( |
936 | 941 | yield quota_remaining_event |
937 | 942 | return |
938 | 943 |
|
| 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 | + |
939 | 968 | # 上传文件特殊处理 |
940 | 969 | async for handle_upload_event in self.handle_upload_agent.run_async(ctx): |
941 | 970 | yield handle_upload_event |
|
0 commit comments