8686from agents .matmaster_agent .flow_agents .scene_agent .constant import SCENE_AGENT
8787from agents .matmaster_agent .flow_agents .scene_agent .prompt import SCENE_INSTRUCTION
8888from agents .matmaster_agent .flow_agents .scene_agent .schema import SceneSchema
89- from agents .matmaster_agent .flow_agents .schema import FlowStatusEnum
9089from agents .matmaster_agent .flow_agents .step_title_agent .callback import (
9190 filter_llm_contents ,
9291)
9392from agents .matmaster_agent .flow_agents .step_title_agent .prompt import (
9493 STEP_TITLE_INSTRUCTION ,
9594)
9695from agents .matmaster_agent .flow_agents .step_title_agent .schema import StepTitleSchema
96+ from agents .matmaster_agent .flow_agents .step_utils import (
97+ get_current_step ,
98+ is_job_submitted_step ,
99+ )
97100from agents .matmaster_agent .flow_agents .step_validation_agent .prompt import (
98101 STEP_VALIDATION_INSTRUCTION ,
99102)
103106from agents .matmaster_agent .flow_agents .thinking_agent .agent import ThinkingAgent
104107from agents .matmaster_agent .flow_agents .thinking_agent .constant import THINKING_AGENT
105108from agents .matmaster_agent .flow_agents .utils import (
106- check_plan ,
107109 get_tools_list ,
108- is_plan_confirmed ,
109110 scenes_contain_query_job_status ,
110111 should_bypass_confirmation ,
111112)
@@ -345,28 +346,20 @@ def _build_execution_agent_for_plan(
345346 before_model_callback = filter_llm_contents ,
346347 after_model_callback = MatMasterLlmConfig .opik_tracer .after_model_callback ,
347348 )
348- plan_steps = ctx .session .state .get ('plan' , {}).get ('steps' , [])
349- agent_names = []
350- for step in plan_steps :
351- tool_name = step .get ('tool_name' )
352- if not tool_name :
353- continue
354- belonging_agent = ALL_TOOLS .get (tool_name , {}).get ('belonging_agent' )
355- if belonging_agent and belonging_agent not in agent_names :
356- agent_names .append (belonging_agent )
357-
358- sub_agents = [
359- AGENT_CLASS_MAPPING [agent_name ](MatMasterLlmConfig )
360- for agent_name in agent_names
361- if agent_name in AGENT_CLASS_MAPPING
362- ]
349+ current_step = get_current_step (ctx )
350+ tool_name = current_step .get ('tool_name' )
351+ belonging_agent = ALL_TOOLS .get (tool_name , {}).get ('belonging_agent' )
363352
364353 execution_agent = MatMasterSupervisorAgent (
365354 name = 'execution_agent' ,
366355 model = MatMasterLlmConfig .default_litellm_model ,
367356 description = '根据 materials_plan 返回的计划进行总结' ,
368357 instruction = '' ,
369- sub_agents = sub_agents + [step_title_agent ] + [step_validation_agent ],
358+ sub_agents = [
359+ AGENT_CLASS_MAPPING [belonging_agent ](MatMasterLlmConfig ),
360+ step_title_agent ,
361+ step_validation_agent ,
362+ ],
370363 )
371364 track_adk_agent_recursive (execution_agent , MatMasterLlmConfig .opik_tracer )
372365 return execution_agent
@@ -711,8 +704,6 @@ async def _run_step_make_agent(
711704 async def _run_plan_execute_agent (
712705 self , ctx : InvocationContext
713706 ) -> AsyncGenerator [Event , None ]:
714- # 重置 scenes
715- yield update_state_event (ctx , state_delta = {'scenes' : []})
716707 # 执行计划
717708 self ._execution_agent = self ._build_execution_agent_for_plan (ctx )
718709 if self ._execution_agent :
@@ -841,26 +832,6 @@ async def _run_summary_agent(
841832 yield matmaster_flow_event
842833 yield update_state_event (ctx , state_delta = {'matmaster_flow_active' : None })
843834
844- # 渲染追问组件
845- follow_up_list = await get_random_questions (i18n = i18n )
846- for generate_follow_up_event in context_function_event (
847- ctx ,
848- self .name ,
849- 'matmaster_generate_follow_up' ,
850- {},
851- ModelRole ,
852- {
853- 'follow_up_result' : json .dumps (
854- {
855- 'invocation_id' : ctx .invocation_id ,
856- 'title' : i18n .t ('MoreQuestions' ),
857- 'list' : follow_up_list ,
858- }
859- )
860- },
861- ):
862- yield generate_follow_up_event
863-
864835 async def _run_research_flow (
865836 self , ctx : InvocationContext
866837 ) -> AsyncGenerator [Event , None ]:
@@ -891,14 +862,8 @@ async def _run_research_flow(
891862 yield _scene_event
892863
893864 while True :
894- # 制定计划(1. 无计划;2. 计划已完成;3. 计划失败;4. 用户未确认计划)
895- # 仅查询任务状态时跳过 thinking(查任务状态不 thinking)
896- skip_thinking = scenes_contain_query_job_status (ctx )
897- if check_plan (ctx ) in [
898- FlowStatusEnum .NO_PLAN ,
899- FlowStatusEnum .COMPLETE ,
900- FlowStatusEnum .FAILED ,
901- ] or not is_plan_confirmed (ctx ):
865+ if not is_job_submitted_step (ctx ):
866+ skip_thinking = scenes_contain_query_job_status (ctx )
902867 async for _step_make_event in self ._run_step_make_agent (
903868 ctx ,
904869 UPDATE_USER_CONTENT ,
@@ -907,27 +872,50 @@ async def _run_research_flow(
907872 ):
908873 yield _step_make_event
909874
910- # 计划未确认,暂停往下执行
911- # if is_plan_confirmed(ctx):
912875 async for _plan_execute_event in self ._run_plan_execute_agent (ctx ):
913876 yield _plan_execute_event
914877
915- # 回顾历史执行
916- user_request = ctx .user_content .parts [0 ].text
917- history_steps = ctx .session .state [HISTORY_STEPS ]
918- session_files = await get_session_files (ctx .session .id )
919- self .all_finished_agent .instruction = create_all_finished_instruction (
920- user_request , history_steps , session_files
921- )
922- async for _all_finished_event in self .all_finished_agent .run_async (ctx ):
923- yield _all_finished_event
878+ # 检查是否为等待异步任务执行完成的阶段
879+ if not is_job_submitted_step (ctx ):
880+ # 回顾历史执行
881+ user_request = ctx .user_content .parts [0 ].text
882+ history_steps = ctx .session .state [HISTORY_STEPS ]
883+ session_files = await get_session_files (ctx .session .id )
884+ self .all_finished_agent .instruction = create_all_finished_instruction (
885+ user_request , history_steps , session_files
886+ )
887+ async for _all_finished_event in self .all_finished_agent .run_async (ctx ):
888+ yield _all_finished_event
924889
925- if ctx .session .state [FINISHED_STATE ]['finished' ]:
890+ if ctx .session .state [FINISHED_STATE ]['finished' ]:
891+ break
892+ else :
926893 break
927894
928- # 总结计划
929- async for _plan_summary_event in self ._run_summary_agent (ctx ):
930- yield _plan_summary_event
895+ if not is_job_submitted_step (ctx ):
896+ # 总结计划
897+ async for _plan_summary_event in self ._run_summary_agent (ctx ):
898+ yield _plan_summary_event
899+
900+ # 渲染追问组件
901+ follow_up_list = await get_random_questions (i18n = i18n )
902+ for generate_follow_up_event in context_function_event (
903+ ctx ,
904+ self .name ,
905+ 'matmaster_generate_follow_up' ,
906+ {},
907+ ModelRole ,
908+ {
909+ 'follow_up_result' : json .dumps (
910+ {
911+ 'invocation_id' : ctx .invocation_id ,
912+ 'title' : i18n .t ('MoreQuestions' ),
913+ 'list' : follow_up_list ,
914+ }
915+ )
916+ },
917+ ):
918+ yield generate_follow_up_event
931919
932920 async def _run_async_impl (
933921 self , ctx : InvocationContext
0 commit comments