Skip to content

Commit c70d0a5

Browse files
committed
no message
1 parent 01b78d7 commit c70d0a5

5 files changed

Lines changed: 386 additions & 149 deletions

File tree

astrbot/core/astr_agent_tool_exec.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

astrbot/core/astr_main_agent.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class MainAgentBuildConfig:
145145
"""Maximum number of images injected from quoted-message fallback extraction."""
146146
enhanced_subagent: dict = field(default_factory=dict)
147147
"""Log level for enhanced SubAgent: info or debug."""
148+
149+
148150
@dataclass(slots=True)
149151
class MainAgentBuildResult:
150152
agent_runner: AgentRunner
@@ -939,7 +941,7 @@ def _apply_enhanced_subagent_tools(
939941
2. Register dynamic SubAgent management tools
940942
3. Register session's transfer_to_xxx tools
941943
"""
942-
if not config.enhanced_subagent.get('enabled', False):
944+
if not config.enhanced_subagent.get("enabled", False):
943945
return
944946

945947
if req.func_tool is None:
@@ -957,6 +959,7 @@ def _apply_enhanced_subagent_tools(
957959
DynamicSubAgentManager,
958960
)
959961
from astrbot.core.subagent_logger import SubAgentLogger
962+
960963
# Register dynamic SubAgent management tools
961964
req.func_tool.add_tool(CREATE_DYNAMIC_SUBAGENT_TOOL)
962965
req.func_tool.add_tool(CLEANUP_DYNAMIC_SUBAGENT_TOOL)
@@ -967,20 +970,26 @@ def _apply_enhanced_subagent_tools(
967970
req.func_tool.add_tool(SEND_PUBLIC_CONTEXT_TOOL)
968971

969972
# Configure logger
970-
SubAgentLogger.configure(level=config.enhanced_subagent.get('log_level'))
973+
SubAgentLogger.configure(level=config.enhanced_subagent.get("log_level"))
971974

972975
# Configure DynamicSubAgentManager with settings
973-
shared_context_enabled = config.enhanced_subagent.get('shared_context_enabled', False)
976+
shared_context_enabled = config.enhanced_subagent.get(
977+
"shared_context_enabled", False
978+
)
974979
DynamicSubAgentManager.configure(
975-
max_subagent_count=config.enhanced_subagent.get('max_subagent_count'),
976-
auto_cleanup_per_turn=config.enhanced_subagent.get('auto_cleanup_per_turn'),
980+
max_subagent_count=config.enhanced_subagent.get("max_subagent_count"),
981+
auto_cleanup_per_turn=config.enhanced_subagent.get("auto_cleanup_per_turn"),
977982
shared_context_enabled=shared_context_enabled,
978-
shared_context_maxlen=config.enhanced_subagent.get('shared_context_maxlen', 200)
983+
shared_context_maxlen=config.enhanced_subagent.get(
984+
"shared_context_maxlen", 200
985+
),
979986
)
980987

981988
# Enable shared context if configured
982989
if shared_context_enabled:
983-
DynamicSubAgentManager.set_shared_context_enabled(event.unified_msg_origin, True)
990+
DynamicSubAgentManager.set_shared_context_enabled(
991+
event.unified_msg_origin, True
992+
)
984993

985994
# Inject enhanced system prompt
986995
enhanced_prompt = DynamicSubAgentManager.ENHANCED_SUBAGENT_SYSTEM_PROMPT
@@ -995,19 +1004,23 @@ def _apply_enhanced_subagent_tools(
9951004
req.func_tool.add_tool(tool)
9961005
# Register dynamically created handoff tools
9971006
session_id = event.unified_msg_origin
998-
dynamic_handoffs = DynamicSubAgentManager.get_handoff_tools_for_session(session_id)
1007+
dynamic_handoffs = DynamicSubAgentManager.get_handoff_tools_for_session(
1008+
session_id
1009+
)
9991010
for handoff in dynamic_handoffs:
10001011
req.func_tool.add_tool(handoff)
10011012

1002-
# Check if we should cleanup subagents from previous turn
1003-
# This is done at the START of a new turn to clean up from previous turn
1013+
# Check if we should cleanup subagents from previous turn
1014+
# This is done at the START of a new turn to clean up from previous turn
10041015
try:
10051016
DynamicSubAgentManager.cleanup_session_turn_start(session_id)
10061017
except Exception as e:
10071018
from astrbot import logger
1019+
10081020
logger.warning(f"[EnhancedSubAgent] Cleanup failed: {e}")
10091021
except ImportError as e:
10101022
from astrbot import logger
1023+
10111024
logger.warning(f"[EnhancedSubAgent] Cannot import module: {e}")
10121025

10131026

astrbot/core/config/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
"max_subagent_count": 3,
203203
"auto_cleanup_per_turn": True,
204204
"shared_context_enabled": False,
205-
"shared_context_maxlen": 200
205+
"shared_context_maxlen": 200,
206206
},
207207
"provider_stt_settings": {
208208
"enable": False,

0 commit comments

Comments
 (0)