Skip to content

Commit 8705d81

Browse files
committed
bug fix
1 parent 0999ba1 commit 8705d81

3 files changed

Lines changed: 151 additions & 118 deletions

File tree

astrbot/core/astr_agent_tool_exec.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ async def _run_subagent():
392392
_run_subagent(), timeout=execution_timeout
393393
)
394394
except asyncio.TimeoutError:
395+
# 若超时,保存已产生的部分历史
396+
cls._save_subagent_history(agent_name, runner_messages, umo)
395397
error_msg = f"SubAgent '{agent_name}' execution timeout after {execution_timeout:.1f} seconds."
396398
logger.warning(f"[SubAgentTimeout] {error_msg}")
397399

@@ -528,7 +530,6 @@ async def _run():
528530
is_enhanced = cls._is_enhanced_subagent(umo, agent_name)
529531
if is_enhanced:
530532
await cls._handle_enhanced_subagent_background_result(
531-
cls=cls,
532533
umo=umo,
533534
agent_name=agent_name,
534535
task_id=tool_args.get("subagent_task_id"),
@@ -917,7 +918,9 @@ def _build_background_submission_message(
917918
original_task_id: str,
918919
subagent_task_id: str | None,
919920
) -> mcp.types.TextContent:
920-
if subagent_task_id:
921+
if subagent_task_id and not subagent_task_id.startswith(
922+
"__PENDING_TASK_CREATE_FAILED__"
923+
):
921924
return mcp.types.TextContent(
922925
type="text",
923926
text=(
@@ -969,7 +972,7 @@ def _is_enhanced_subagent(umo: str, agent_name: str | None) -> bool:
969972
return True
970973
return False
971974

972-
@staticmethod
975+
@classmethod
973976
async def _handle_enhanced_subagent_background_result(
974977
cls,
975978
*,
@@ -985,8 +988,20 @@ async def _handle_enhanced_subagent_background_result(
985988
) -> None:
986989
from astrbot.core.dynamic_subagent_manager import DynamicSubAgentManager
987990

991+
success = error_text is None
992+
status = "COMPLETED" if success else "FAILED"
988993
DynamicSubAgentManager.set_subagent_status(
989-
session_id=umo, agent_name=agent_name, status="FAILED"
994+
session_id=umo, agent_name=agent_name, status=status
995+
)
996+
997+
DynamicSubAgentManager.store_subagent_result(
998+
session_id=umo,
999+
agent_name=agent_name,
1000+
success=success,
1001+
result=result_text,
1002+
task_id=task_id,
1003+
error=error_text,
1004+
execution_time=execution_time,
9901005
)
9911006

9921007
if not await cls._maybe_wake_main_agent_after_background(
@@ -1020,9 +1035,9 @@ async def _maybe_wake_main_agent_after_background(
10201035
)
10211036
else:
10221037
main_agent_is_running = False
1023-
except Exception as e: # noqa: BLE001
1038+
except Exception as e:
10241039
logger.error("Failed to check main agent status: %s", e)
1025-
main_agent_is_running = True
1040+
main_agent_is_running = False # 异常时尝试通知,避免结果丢失
10261041

10271042
if main_agent_is_running:
10281043
return False

astrbot/core/astr_main_agent.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def _apply_enhanced_subagent_tools(
10971097
"max_subagent_history", 500
10981098
),
10991099
tools_blacklist=config.enhanced_subagent.get("tools_blacklist", None),
1100-
tools_whitelist=config.enhanced_subagent.get("tools_whitelist", None),
1100+
tools_inherent=config.enhanced_subagent.get("tools_inherent", None),
11011101
execution_timeout=config.enhanced_subagent.get("execution_timeout", 600),
11021102
)
11031103

@@ -1122,12 +1122,16 @@ def _apply_enhanced_subagent_tools(
11221122
dynamic_handoffs = DynamicSubAgentManager.get_handoff_tools_for_session(
11231123
session_id
11241124
)
1125+
# Prevent duplication of name with the original subagent tool
1126+
existing_names = {t.name for t in req.func_tool.tools}
11251127
for handoff in dynamic_handoffs:
1126-
req.func_tool.add_tool(handoff)
1127-
1128+
if handoff.name not in existing_names:
1129+
req.func_tool.add_tool(handoff)
1130+
else:
1131+
logger.warning(
1132+
"[EnhancedSubAgent] Failed to create due to duplicate names between the enhanced sub agent and the original sub agent."
1133+
)
11281134
except ImportError as e:
1129-
from astrbot import logger
1130-
11311135
logger.warning(f"[EnhancedSubAgent] Cannot import module: {e}")
11321136

11331137

0 commit comments

Comments
 (0)