1+ import inspect
12import json
23import logging
34import uuid
1213
1314from agents .matmaster_agent .base_agents .callback import _get_ak
1415from agents .matmaster_agent .constant import FRONTEND_STATE_KEY
15- from agents .matmaster_agent .model import TransferCheck , UserContent
16- from agents .matmaster_agent .prompt import get_transfer_check_prompt , get_user_content_lang
16+ from agents .matmaster_agent .locales import i18n
17+ from agents .matmaster_agent .model import UserContent
18+ from agents .matmaster_agent .prompt import get_user_content_lang
19+ from agents .matmaster_agent .style import get_job_complete_card
1720from agents .matmaster_agent .utils .job_utils import get_job_status , has_job_running , get_running_jobs_detail
18- from agents .matmaster_agent .utils .llm_response_utils import has_function_call
1921
2022logger = logging .getLogger (__name__ )
2123
@@ -51,7 +53,7 @@ async def matmaster_set_lang(callback_context: CallbackContext) -> Optional[type
5153 response = litellm .completion (model = 'azure/gpt-4o' , messages = [{'role' : 'user' , 'content' : prompt }],
5254 response_format = UserContent )
5355 result : dict = json .loads (response .choices [0 ].message .content )
54- logger .info (f"[matmaster_prepare_state] user_content = { result } " )
56+ logger .info (f"[{ inspect . currentframe (). f_code . co_name } ] result = { result } " )
5557 language = str (result .get ('language' , 'zh' ))
5658 callback_context .state ['target_language' ] = language
5759
@@ -74,10 +76,9 @@ async def matmaster_check_job_status(callback_context: CallbackContext, llm_resp
7476 running_job_ids = get_running_jobs_detail (jobs_dict ) # 从 state 里面拿
7577 access_key = _get_ak (callback_context ) # 从 state 或环境变量里面拿
7678 if callback_context .state ['target_language' ] in ['Chinese' , 'zh-CN' , '简体中文' , 'Chinese (Simplified)' ]:
77- job_complete_intro = '检测到任务 <{job_id}> 已完成,我将立刻转移至对应的 Agent 去获取任务结果。 '
79+ i18n . language = 'zh '
7880 else :
79- job_complete_intro = ('Job <{job_id}> has been detected as completed. '
80- 'I will immediately transfer to the corresponding agent to retrieve the job results.' )
81+ i18n .language = 'en'
8182
8283 reset = False
8384 for origin_job_id , job_id , job_query_url , agent_name in running_job_ids :
@@ -100,7 +101,7 @@ async def matmaster_check_job_status(callback_context: CallbackContext, llm_resp
100101 reset = True
101102 function_call_id = f"call_{ str (uuid .uuid4 ()).replace ('-' , '' )[:24 ]} "
102103 callback_context .state ['origin_job_id' ] = origin_job_id
103- llm_response .content .parts .append (Part (text = job_complete_intro . format ( job_id = job_id )))
104+ llm_response .content .parts .append (Part (text = get_job_complete_card ( i18n = i18n , job_id = job_id )))
104105 llm_response .content .parts .append (Part (function_call = FunctionCall (id = function_call_id ,
105106 name = 'transfer_to_agent' ,
106107 args = {'agent_name' : agent_name })))
@@ -109,40 +110,3 @@ async def matmaster_check_job_status(callback_context: CallbackContext, llm_resp
109110
110111 callback_context .state ['last_llm_response_partial' ] = llm_response .partial
111112 return None
112-
113-
114- async def matmaster_check_transfer (callback_context : CallbackContext , llm_response : LlmResponse ) -> Optional [
115- LlmResponse ]:
116- # 检查响应是否有效
117- if not (
118- llm_response and
119- not llm_response .partial and
120- llm_response .content and
121- llm_response .content .parts and
122- len (llm_response .content .parts ) and
123- llm_response .content .parts [0 ].text
124- ):
125- return None
126-
127- prompt = get_transfer_check_prompt ().format (response_text = llm_response .content .parts [0 ].text )
128- response = litellm .completion (model = 'azure/gpt-4o' , messages = [{'role' : 'user' , 'content' : prompt }],
129- response_format = TransferCheck )
130-
131- result : dict = json .loads (response .choices [0 ].message .content )
132- is_transfer = bool (result .get ('is_transfer' , False ))
133- target_agent = str (result .get ('target_agent' , '' ))
134- reason = str (result .get ('reason' , '' ))
135- logger .info (f"[matmaster_check_transfer] target_agent = { target_agent } , is_transfer = { is_transfer } "
136- f"response_text = { llm_response .content .parts [0 ].text } , reason = { reason } " )
137- if (
138- is_transfer and
139- not has_function_call (llm_response )
140- ):
141- logger .warning (f"[matmaster_check_transfer] add `transfer_to_agent`" )
142- function_call_id = f"added_{ str (uuid .uuid4 ()).replace ('-' , '' )[:24 ]} "
143- llm_response .content .parts .append (Part (function_call = FunctionCall (id = function_call_id , name = 'transfer_to_agent' ,
144- args = {'agent_name' : target_agent })))
145-
146- return llm_response
147-
148- return None
0 commit comments