44import traceback
55import typing as T
66import uuid
7+ import datetime
78from collections .abc import Sequence
89from collections .abc import Set as AbstractSet
910
@@ -234,17 +235,13 @@ def _build_handoff_toolset(
234235 elif isinstance (tool_name_or_obj , FunctionTool ):
235236 toolset .add_tool (tool_name_or_obj )
236237
237- # Always add send_public_context tool for shared context feature
238+ # Always add send_shared_context tool for shared context feature
238239 try :
239- from astrbot .core .dynamic_subagent_manager import (
240- DynamicSubAgentManager ,
241- SEND_PUBLIC_CONTEXT_TOOL ,
242- )
243-
240+ from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager , SEND_SHARED_CONTEXT_TOOL
244241 session_id = event .unified_msg_origin
245242 session = DynamicSubAgentManager .get_session (session_id )
246243 if session and session .shared_context_enabled :
247- toolset .add_tool (SEND_PUBLIC_CONTEXT_TOOL )
244+ toolset .add_tool (SEND_SHARED_CONTEXT_TOOL )
248245 except Exception :
249246 pass
250247
@@ -306,70 +303,60 @@ async def _execute_handoff(
306303 except Exception :
307304 continue
308305
309- # 获取受保护subagent的历史上下文
310- agent_name = getattr (tool .agent , " name" , None )
306+ # 获取子代理的历史上下文
307+ agent_name = getattr (tool .agent , ' name' , None )
311308 subagent_history = []
312309 if agent_name :
313310 try :
314311 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
315-
316- stored_history = DynamicSubAgentManager .get_subagent_history (
317- umo , agent_name
318- )
312+ stored_history = DynamicSubAgentManager .get_subagent_history (umo , agent_name )
319313 if stored_history :
320314 # 将历史消息转换为 Message 对象
321315 for hist_msg in stored_history :
322316 try :
323317 if isinstance (hist_msg , dict ):
324- subagent_history .append (
325- Message .model_validate (hist_msg )
326- )
318+ subagent_history .append (Message .model_validate (hist_msg ))
327319 elif isinstance (hist_msg , Message ):
328320 subagent_history .append (hist_msg )
329321 except Exception :
330322 continue
331323 if subagent_history :
332- logger .info (
333- f"[SubAgentHistory] Loaded { len (subagent_history )} history messages for { agent_name } "
334- )
324+ logger .info (f"[SubAgentHistory] Loaded { len (subagent_history )} history messages for { agent_name } " )
335325 except Exception :
336326 pass
337327
338328 prov_settings : dict = ctx .get_config (umo = umo ).get ("provider_settings" , {})
339329 agent_max_step = int (prov_settings .get ("max_agent_step" , 30 ))
340330 stream = prov_settings .get ("streaming_response" , False )
341-
342331 # 如果有历史上下文,合并到 contexts 中
343332 if subagent_history :
344333 if contexts is None :
345334 contexts = subagent_history
346335 else :
347336 contexts = subagent_history + contexts
348337
349- # 构建subagent的 system_prompt,添加 skills 提示词和公共上下文
338+ # 构建子代理的 system_prompt,添加 skills 提示词和公共上下文
350339 subagent_system_prompt = tool .agent .instructions or ""
351340 if agent_name :
352341 try :
353342 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
354343
355344 # 注入 skills
356345 runtime = prov_settings .get ("computer_use_runtime" , "local" )
357- skills_prompt = DynamicSubAgentManager .build_subagent_skills_prompt (
358- umo , agent_name , runtime
359- )
346+ skills_prompt = DynamicSubAgentManager .build_subagent_skills_prompt (umo , agent_name , runtime )
360347 if skills_prompt :
361348 subagent_system_prompt += f"\n \n # Available Skills\n { skills_prompt } "
362349 logger .info (f"[SubAgentSkills] Injected skills for { agent_name } " )
363350
364351 # 注入公共上下文
365- shared_context_prompt = (
366- DynamicSubAgentManager .build_shared_context_prompt (umo , agent_name )
367- )
352+ shared_context_prompt = DynamicSubAgentManager .build_shared_context_prompt (umo , agent_name )
368353 if shared_context_prompt :
369354 subagent_system_prompt += f"\n { shared_context_prompt } "
370- logger .info (
371- f"[SubAgentSharedContext] Injected shared context for { agent_name } "
372- )
355+ logger .info (f"[SubAgentSharedContext] Injected shared context for { agent_name } " )
356+
357+ # 注入时间信息
358+ current_time = datetime .datetime .now ().astimezone ().strftime ("%Y-%m-%d %H:%M (%Z)" )
359+ subagent_system_prompt += f"Current datetime: { current_time } "
373360
374361 except Exception :
375362 pass
@@ -387,27 +374,19 @@ async def _execute_handoff(
387374 stream = stream ,
388375 )
389376
390- # 保存受保护subagent的历史上下文
377+ # 保存历史上下文
391378 try :
392379 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
393-
394- agent_name = getattr (tool .agent , "name" , None )
380+ agent_name = getattr (tool .agent , 'name' , None )
395381 if agent_name :
396382 # 构建当前对话的历史消息
397- history_messages = []
398- if contexts :
399- history_messages .extend (contexts )
400- # 添加用户输入
401- history_messages .append ({"role" : "user" , "content" : input_ })
383+ current_messages = []
384+ # 添加本轮用户输入
385+ current_messages .append ({"role" : "user" , "content" : input_ })
402386 # 添加助手回复
403- history_messages .append (
404- {"role" : "assistant" , "content" : llm_resp .completion_text }
405- )
406- # 保存历史
407- if history_messages :
408- DynamicSubAgentManager .save_subagent_history (
409- umo , agent_name , history_messages
410- )
387+ current_messages .append ({"role" : "assistant" , "content" : llm_resp .completion_text })
388+ if current_messages :
389+ DynamicSubAgentManager .save_subagent_history (umo , agent_name , current_messages )
411390 except Exception :
412391 pass # 不影响主流程
413392
0 commit comments