@@ -236,7 +236,11 @@ def _build_handoff_toolset(
236236
237237 # Always add send_public_context tool for shared context feature
238238 try :
239- from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager , SEND_PUBLIC_CONTEXT_TOOL
239+ from astrbot .core .dynamic_subagent_manager import (
240+ DynamicSubAgentManager ,
241+ SEND_PUBLIC_CONTEXT_TOOL ,
242+ )
243+
240244 session_id = event .unified_msg_origin
241245 session = DynamicSubAgentManager .get_session (session_id )
242246 if session and session .shared_context_enabled :
@@ -302,25 +306,32 @@ async def _execute_handoff(
302306 except Exception :
303307 continue
304308
305- # 获取受保护子代理的历史上下文
306- agent_name = getattr (tool .agent , ' name' , None )
309+ # 获取受保护subagent的历史上下文
310+ agent_name = getattr (tool .agent , " name" , None )
307311 subagent_history = []
308312 if agent_name :
309313 try :
310314 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
311- stored_history = DynamicSubAgentManager .get_subagent_history (umo , agent_name )
315+
316+ stored_history = DynamicSubAgentManager .get_subagent_history (
317+ umo , agent_name
318+ )
312319 if stored_history :
313320 # 将历史消息转换为 Message 对象
314321 for hist_msg in stored_history :
315322 try :
316323 if isinstance (hist_msg , dict ):
317- subagent_history .append (Message .model_validate (hist_msg ))
324+ subagent_history .append (
325+ Message .model_validate (hist_msg )
326+ )
318327 elif isinstance (hist_msg , Message ):
319328 subagent_history .append (hist_msg )
320329 except Exception :
321330 continue
322331 if subagent_history :
323- logger .info (f"[SubAgentHistory] Loaded { len (subagent_history )} history messages for { agent_name } " )
332+ logger .info (
333+ f"[SubAgentHistory] Loaded { len (subagent_history )} history messages for { agent_name } "
334+ )
324335 except Exception :
325336 pass
326337
@@ -335,27 +346,31 @@ async def _execute_handoff(
335346 else :
336347 contexts = subagent_history + contexts
337348
338- # 构建子代理的 system_prompt,添加 skills 提示词和公共上下文
349+ # 构建subagent的 system_prompt,添加 skills 提示词和公共上下文
339350 subagent_system_prompt = tool .agent .instructions or ""
340351 if agent_name :
341352 try :
342353 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
343354
344355 # 注入 skills
345356 runtime = prov_settings .get ("computer_use_runtime" , "local" )
346- skills_prompt = DynamicSubAgentManager .build_subagent_skills_prompt (umo , agent_name , runtime )
357+ skills_prompt = DynamicSubAgentManager .build_subagent_skills_prompt (
358+ umo , agent_name , runtime
359+ )
347360 if skills_prompt :
348361 subagent_system_prompt += f"\n \n # Available Skills\n { skills_prompt } "
349362 logger .info (f"[SubAgentSkills] Injected skills for { agent_name } " )
350363
351364 # 注入公共上下文
352- shared_context_prompt = DynamicSubAgentManager .build_shared_context_prompt (umo , agent_name )
365+ shared_context_prompt = (
366+ DynamicSubAgentManager .build_shared_context_prompt (umo , agent_name )
367+ )
353368 if shared_context_prompt :
354369 subagent_system_prompt += f"\n { shared_context_prompt } "
355- logger .info (f"[SubAgentSharedContext] Injected shared context for { agent_name } " )
370+ logger .info (
371+ f"[SubAgentSharedContext] Injected shared context for { agent_name } "
372+ )
356373
357- # 注入发送公共上下文的工具给子代理
358- from astrbot .core .dynamic_subagent_manager import SEND_PUBLIC_CONTEXT_TOOL
359374 except Exception :
360375 pass
361376
@@ -372,23 +387,27 @@ async def _execute_handoff(
372387 stream = stream ,
373388 )
374389
375- # 保存受保护子代理的历史上下文
390+ # 保存受保护subagent的历史上下文
376391 try :
377392 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
378- agent_name = getattr (tool .agent , 'name' , None )
393+
394+ agent_name = getattr (tool .agent , "name" , None )
379395 if agent_name :
380- history = DynamicSubAgentManager .get_subagent_history (umo , agent_name )
381396 # 构建当前对话的历史消息
382397 history_messages = []
383398 if contexts :
384399 history_messages .extend (contexts )
385400 # 添加用户输入
386401 history_messages .append ({"role" : "user" , "content" : input_ })
387402 # 添加助手回复
388- history_messages .append ({"role" : "assistant" , "content" : llm_resp .completion_text })
403+ history_messages .append (
404+ {"role" : "assistant" , "content" : llm_resp .completion_text }
405+ )
389406 # 保存历史
390407 if history_messages :
391- DynamicSubAgentManager .save_subagent_history (umo , agent_name , history_messages )
408+ DynamicSubAgentManager .save_subagent_history (
409+ umo , agent_name , history_messages
410+ )
392411 except Exception :
393412 pass # 不影响主流程
394413
0 commit comments