8484from agents .matmaster_agent .flow_agents .scene_agent .constant import SCENE_AGENT
8585from agents .matmaster_agent .flow_agents .scene_agent .prompt import SCENE_INSTRUCTION
8686from agents .matmaster_agent .flow_agents .scene_agent .schema import SceneSchema
87- from agents .matmaster_agent .flow_agents .schema import FlowStatusEnum
8887from agents .matmaster_agent .flow_agents .step_title_agent .callback import (
8988 filter_llm_contents ,
9089)
9190from agents .matmaster_agent .flow_agents .step_title_agent .prompt import (
9291 STEP_TITLE_INSTRUCTION ,
9392)
9493from agents .matmaster_agent .flow_agents .step_title_agent .schema import StepTitleSchema
94+ from agents .matmaster_agent .flow_agents .step_utils import (
95+ get_current_step ,
96+ is_job_submitted_step ,
97+ )
9598from agents .matmaster_agent .flow_agents .step_validation_agent .prompt import (
9699 STEP_VALIDATION_INSTRUCTION ,
97100)
101104from agents .matmaster_agent .flow_agents .thinking_agent .agent import ThinkingAgent
102105from agents .matmaster_agent .flow_agents .thinking_agent .constant import THINKING_AGENT
103106from agents .matmaster_agent .flow_agents .utils import (
104- check_plan ,
105107 get_tools_list ,
106- is_plan_confirmed ,
107108 scenes_contain_query_job_status ,
108109 should_bypass_confirmation ,
109110)
@@ -343,28 +344,20 @@ def _build_execution_agent_for_plan(
343344 before_model_callback = filter_llm_contents ,
344345 after_model_callback = MatMasterLlmConfig .opik_tracer .after_model_callback ,
345346 )
346- plan_steps = ctx .session .state .get ('plan' , {}).get ('steps' , [])
347- agent_names = []
348- for step in plan_steps :
349- tool_name = step .get ('tool_name' )
350- if not tool_name :
351- continue
352- belonging_agent = ALL_TOOLS .get (tool_name , {}).get ('belonging_agent' )
353- if belonging_agent and belonging_agent not in agent_names :
354- agent_names .append (belonging_agent )
355-
356- sub_agents = [
357- AGENT_CLASS_MAPPING [agent_name ](MatMasterLlmConfig )
358- for agent_name in agent_names
359- if agent_name in AGENT_CLASS_MAPPING
360- ]
347+ current_step = get_current_step (ctx )
348+ tool_name = current_step .get ('tool_name' )
349+ belonging_agent = ALL_TOOLS .get (tool_name , {}).get ('belonging_agent' )
361350
362351 execution_agent = MatMasterSupervisorAgent (
363352 name = 'execution_agent' ,
364353 model = MatMasterLlmConfig .default_litellm_model ,
365354 description = '根据 materials_plan 返回的计划进行总结' ,
366355 instruction = '' ,
367- sub_agents = sub_agents + [step_title_agent ] + [step_validation_agent ],
356+ sub_agents = [
357+ AGENT_CLASS_MAPPING [belonging_agent ](MatMasterLlmConfig ),
358+ step_title_agent ,
359+ step_validation_agent ,
360+ ],
368361 )
369362 track_adk_agent_recursive (execution_agent , MatMasterLlmConfig .opik_tracer )
370363 return execution_agent
@@ -611,8 +604,6 @@ async def _run_step_make_agent(
611604 async def _run_plan_execute_agent (
612605 self , ctx : InvocationContext
613606 ) -> AsyncGenerator [Event , None ]:
614- # 重置 scenes
615- yield update_state_event (ctx , state_delta = {'scenes' : []})
616607 # 执行计划
617608 self ._execution_agent = self ._build_execution_agent_for_plan (ctx )
618609 if self ._execution_agent :
@@ -741,26 +732,6 @@ async def _run_summary_agent(
741732 yield matmaster_flow_event
742733 yield update_state_event (ctx , state_delta = {'matmaster_flow_active' : None })
743734
744- # 渲染追问组件
745- follow_up_list = await get_random_questions (i18n = i18n )
746- for generate_follow_up_event in context_function_event (
747- ctx ,
748- self .name ,
749- 'matmaster_generate_follow_up' ,
750- {},
751- ModelRole ,
752- {
753- 'follow_up_result' : json .dumps (
754- {
755- 'invocation_id' : ctx .invocation_id ,
756- 'title' : i18n .t ('MoreQuestions' ),
757- 'list' : follow_up_list ,
758- }
759- )
760- },
761- ):
762- yield generate_follow_up_event
763-
764735 async def _run_research_flow (
765736 self , ctx : InvocationContext
766737 ) -> AsyncGenerator [Event , None ]:
@@ -791,14 +762,8 @@ async def _run_research_flow(
791762 yield _scene_event
792763
793764 while True :
794- # 制定计划(1. 无计划;2. 计划已完成;3. 计划失败;4. 用户未确认计划)
795- # 仅查询任务状态时跳过 thinking(查任务状态不 thinking)
796- skip_thinking = scenes_contain_query_job_status (ctx )
797- if check_plan (ctx ) in [
798- FlowStatusEnum .NO_PLAN ,
799- FlowStatusEnum .COMPLETE ,
800- FlowStatusEnum .FAILED ,
801- ] or not is_plan_confirmed (ctx ):
765+ if not is_job_submitted_step (ctx ):
766+ skip_thinking = scenes_contain_query_job_status (ctx )
802767 async for _step_make_event in self ._run_step_make_agent (
803768 ctx ,
804769 UPDATE_USER_CONTENT ,
@@ -807,27 +772,50 @@ async def _run_research_flow(
807772 ):
808773 yield _step_make_event
809774
810- # 计划未确认,暂停往下执行
811- # if is_plan_confirmed(ctx):
812775 async for _plan_execute_event in self ._run_plan_execute_agent (ctx ):
813776 yield _plan_execute_event
814777
815- # 回顾历史执行
816- user_request = ctx .user_content .parts [0 ].text
817- history_steps = ctx .session .state [HISTORY_STEPS ]
818- session_files = await get_session_files (ctx .session .id )
819- self .all_finished_agent .instruction = create_all_finished_instruction (
820- user_request , history_steps , session_files
821- )
822- async for _all_finished_event in self .all_finished_agent .run_async (ctx ):
823- yield _all_finished_event
778+ # 检查是否为等待异步任务执行完成的阶段
779+ if not is_job_submitted_step (ctx ):
780+ # 回顾历史执行
781+ user_request = ctx .user_content .parts [0 ].text
782+ history_steps = ctx .session .state [HISTORY_STEPS ]
783+ session_files = await get_session_files (ctx .session .id )
784+ self .all_finished_agent .instruction = create_all_finished_instruction (
785+ user_request , history_steps , session_files
786+ )
787+ async for _all_finished_event in self .all_finished_agent .run_async (ctx ):
788+ yield _all_finished_event
824789
825- if ctx .session .state [FINISHED_STATE ]['finished' ]:
790+ if ctx .session .state [FINISHED_STATE ]['finished' ]:
791+ break
792+ else :
826793 break
827794
828- # 总结计划
829- async for _plan_summary_event in self ._run_summary_agent (ctx ):
830- yield _plan_summary_event
795+ if not is_job_submitted_step (ctx ):
796+ # 总结计划
797+ async for _plan_summary_event in self ._run_summary_agent (ctx ):
798+ yield _plan_summary_event
799+
800+ # 渲染追问组件
801+ follow_up_list = await get_random_questions (i18n = i18n )
802+ for generate_follow_up_event in context_function_event (
803+ ctx ,
804+ self .name ,
805+ 'matmaster_generate_follow_up' ,
806+ {},
807+ ModelRole ,
808+ {
809+ 'follow_up_result' : json .dumps (
810+ {
811+ 'invocation_id' : ctx .invocation_id ,
812+ 'title' : i18n .t ('MoreQuestions' ),
813+ 'list' : follow_up_list ,
814+ }
815+ )
816+ },
817+ ):
818+ yield generate_follow_up_event
831819
832820 async def _run_async_impl (
833821 self , ctx : InvocationContext
0 commit comments