66
77import litellm
88from google .adk .agents .callback_context import CallbackContext
9- from google .adk .models import LlmResponse
9+ from google .adk .models import LlmResponse , LlmRequest
1010from google .genai import types
11- from google .genai .types import FunctionCall , Part
11+ from google .genai .types import FunctionCall , Part , FunctionResponse
1212
13+ from agents .matmaster_agent .base_agents .callback import _get_ak
1314from agents .matmaster_agent .constant import FRONTEND_STATE_KEY
1415from agents .matmaster_agent .model import TransferCheck , UserContent
1516from agents .matmaster_agent .prompt import get_transfer_check_prompt , get_user_content_lang
17+ from agents .matmaster_agent .utils .job_utils import get_job_status , has_job_running , get_running_jobs_detail
1618from agents .matmaster_agent .utils .llm_response_utils import has_function_call
1719
1820logger = logging .getLogger (__name__ )
2224async def matmaster_prepare_state (callback_context : CallbackContext ) -> Optional [types .Content ]:
2325 callback_context .state ['current_time' ] = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )
2426 callback_context .state ['error_occurred' ] = False
27+ callback_context .state ['origin_job_id' ] = None
2528
2629 callback_context .state [FRONTEND_STATE_KEY ] = callback_context .state .get (FRONTEND_STATE_KEY , {})
2730 callback_context .state [FRONTEND_STATE_KEY ]['biz' ] = callback_context .state [FRONTEND_STATE_KEY ].get ('biz' , {})
@@ -38,6 +41,8 @@ async def matmaster_prepare_state(callback_context: CallbackContext) -> Optional
3841 callback_context .state ['invocation_id_with_tool_call' ] = callback_context .state .get ('invocation_id_with_tool_call' ,
3942 None )
4043
44+
45+ async def matmaster_set_lang (callback_context : CallbackContext ) -> Optional [types .Content ]:
4146 user_content = callback_context .user_content .parts [0 ].text
4247 prompt = get_user_content_lang ().format (user_content = user_content )
4348 response = litellm .completion (model = 'azure/gpt-4o' , messages = [{'role' : 'user' , 'content' : prompt }],
@@ -48,6 +53,37 @@ async def matmaster_prepare_state(callback_context: CallbackContext) -> Optional
4853 callback_context .state ['target_language' ] = language
4954
5055
56+ async def matmaster_check_job_status (callback_context : CallbackContext , llm_response : LlmRequest ) -> Optional [
57+ LlmResponse ]:
58+ if (
59+ (jobs_dict := callback_context .state ['long_running_jobs' ]) and
60+ has_job_running (jobs_dict )
61+ ):
62+ running_job_ids = get_running_jobs_detail (jobs_dict )
63+ access_key = _get_ak (callback_context )
64+ if callback_context .state ['target_language' ] in ['Chinese' ]:
65+ job_complete_intro = '检测到任务 <{job_id}> 已完成,我将立刻转移至对应的 Agent 去获取任务结果,请稍等...'
66+ else :
67+ job_complete_intro = ('Job <{job_id}> has been detected as completed. '
68+ 'I will immediately transfer to the corresponding agent to retrieve the job results. Please wait...' )
69+
70+ for origin_job_id , job_id , job_query_url , agent_name in running_job_ids :
71+ job_status = get_job_status (job_query_url , access_key = access_key )
72+ if job_status in ['Failed' , 'Finished' ]:
73+ logger .info (f"[matmaster_check_job_status] job_id = { job_id } , job_status = { job_status } " )
74+ function_call_id = f"call_{ str (uuid .uuid4 ()).replace ('-' , '' )[:24 ]} "
75+ callback_context .state ['origin_job_id' ] = origin_job_id
76+ llm_response .content .parts .insert (0 , Part (text = job_complete_intro .format (job_id = job_id ),
77+ function_call = FunctionCall (id = function_call_id ,
78+ name = 'transfer_to_agent' ,
79+ args = {'agent_name' : agent_name }),
80+ function_response = FunctionResponse (id = function_call_id ,
81+ name = 'transfer_to_agent' ,
82+ response = None )
83+ )
84+ )
85+
86+
5187# after_model_callback
5288async def matmaster_check_transfer (callback_context : CallbackContext , llm_response : LlmResponse ) -> Optional [
5389 LlmResponse ]:
0 commit comments