|
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, |
|
126 | 130 | from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS |
127 | 131 | from agents.matmaster_agent.utils.event_utils import ( |
128 | 132 | all_text_event, |
| 133 | + context_function_call_event, |
129 | 134 | context_function_event, |
130 | 135 | is_text, |
131 | 136 | send_error_event, |
@@ -804,6 +809,30 @@ async def _run_async_impl( |
804 | 809 | yield quota_remaining_event |
805 | 810 | return |
806 | 811 |
|
| 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 | + |
807 | 836 | # 上传文件特殊处理 |
808 | 837 | async for handle_upload_event in self.handle_upload_agent.run_async(ctx): |
809 | 838 | yield handle_upload_event |
|
0 commit comments