11import logging
22from typing import AsyncGenerator , override
33
4- from google .adk .agents import InvocationContext , LlmAgent
4+ from google .adk .agents import InvocationContext , LlmAgent , LoopAgent
55from google .adk .events import Event
66from google .adk .models .lite_llm import LiteLlm
77from opik .integrations .adk import track_adk_agent_recursive
1313 matmaster_check_job_status ,
1414 matmaster_prepare_state ,
1515 matmaster_set_lang ,
16+ matmaster_should_continue ,
1617)
1718from agents .matmaster_agent .chembrain_agent .agent import init_chembrain_agent
18- from agents .matmaster_agent .constant import MATMASTER_AGENT_NAME , ModelRole
19+ from agents .matmaster_agent .constant import MATMASTER_CORE_AGENT_NAME , ModelRole
1920from agents .matmaster_agent .document_parser_agent .agent import (
2021 init_document_parser_agent ,
2122)
5859)
5960
6061
61- class MatMasterAgent (HandleFileUploadLlmAgent ):
62+ class MatMasterCoreAgent (HandleFileUploadLlmAgent ):
6263 def __init__ (self , llm_config ):
6364 piloteye_electro_agent = init_piloteye_electro_agent (llm_config )
6465 traj_analysis_agent = init_traj_analysis_agent (llm_config )
@@ -79,7 +80,7 @@ def __init__(self, llm_config):
7980 document_parser_agent = init_document_parser_agent (llm_config )
8081
8182 super ().__init__ (
82- name = MATMASTER_AGENT_NAME ,
83+ name = MATMASTER_CORE_AGENT_NAME ,
8384 model = llm_config .gpt_5_chat ,
8485 sub_agents = [
8586 piloteye_electro_agent ,
@@ -111,6 +112,7 @@ def __init__(self, llm_config):
111112 target_agent_enum = MatMasterTargetAgentEnum ,
112113 ),
113114 ],
115+ after_agent_callback = matmaster_should_continue ,
114116 )
115117
116118 @override
@@ -141,7 +143,28 @@ async def _run_async_impl(
141143 yield error_handel_event
142144
143145
144- def init_matmaster_agent () -> LlmAgent :
146+ class MatMasterAgent (LoopAgent ):
147+ def __init__ (self , llm_config ):
148+ matmaster_core_agent = MatMasterCoreAgent (llm_config )
149+
150+ super ().__init__ (
151+ name = 'matmaster_agent' ,
152+ sub_agents = [matmaster_core_agent ],
153+ before_agent_callback = matmaster_prepare_state ,
154+ description = AgentDescription ,
155+ )
156+
157+ @override
158+ async def _run_async_impl (
159+ self , ctx : InvocationContext
160+ ) -> AsyncGenerator [Event , None ]:
161+ async for event in super ()._run_async_impl (ctx ):
162+ if not ctx .session .state ['should_continue' ]:
163+ return
164+ yield event
165+
166+
167+ def init_matmaster_agent () -> MatMasterAgent :
145168 matmaster_agent = MatMasterAgent (MatMasterLlmConfig )
146169 track_adk_agent_recursive (matmaster_agent , MatMasterLlmConfig .opik_tracer )
147170
0 commit comments