mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-04 00:35:58 +00:00
fix: add origin_message_id parameter to SubagentManager.spawn()
This commit is contained in:
parent
4e06c00b46
commit
61a8ad27d9
@ -114,6 +114,7 @@ class SubagentManager:
|
|||||||
origin_channel: str = "cli",
|
origin_channel: str = "cli",
|
||||||
origin_chat_id: str = "direct",
|
origin_chat_id: str = "direct",
|
||||||
session_key: str | None = None,
|
session_key: str | None = None,
|
||||||
|
origin_message_id: str | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Spawn a subagent to execute a task in the background."""
|
"""Spawn a subagent to execute a task in the background."""
|
||||||
task_id = str(uuid.uuid4())[:8]
|
task_id = str(uuid.uuid4())[:8]
|
||||||
@ -129,7 +130,7 @@ class SubagentManager:
|
|||||||
self._task_statuses[task_id] = status
|
self._task_statuses[task_id] = status
|
||||||
|
|
||||||
bg_task = asyncio.create_task(
|
bg_task = asyncio.create_task(
|
||||||
self._run_subagent(task_id, task, display_label, origin, status)
|
self._run_subagent(task_id, task, display_label, origin, status, origin_message_id)
|
||||||
)
|
)
|
||||||
self._running_tasks[task_id] = bg_task
|
self._running_tasks[task_id] = bg_task
|
||||||
if session_key:
|
if session_key:
|
||||||
@ -155,6 +156,7 @@ class SubagentManager:
|
|||||||
label: str,
|
label: str,
|
||||||
origin: dict[str, str],
|
origin: dict[str, str],
|
||||||
status: SubagentStatus,
|
status: SubagentStatus,
|
||||||
|
origin_message_id: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Execute the subagent task and announce the result."""
|
"""Execute the subagent task and announce the result."""
|
||||||
logger.info("Subagent [{}] starting task: {}", task_id, label)
|
logger.info("Subagent [{}] starting task: {}", task_id, label)
|
||||||
@ -224,24 +226,24 @@ class SubagentManager:
|
|||||||
await self._announce_result(
|
await self._announce_result(
|
||||||
task_id, label, task,
|
task_id, label, task,
|
||||||
self._format_partial_progress(result),
|
self._format_partial_progress(result),
|
||||||
origin, "error",
|
origin, "error", origin_message_id,
|
||||||
)
|
)
|
||||||
elif result.stop_reason == "error":
|
elif result.stop_reason == "error":
|
||||||
await self._announce_result(
|
await self._announce_result(
|
||||||
task_id, label, task,
|
task_id, label, task,
|
||||||
result.error or "Error: subagent execution failed.",
|
result.error or "Error: subagent execution failed.",
|
||||||
origin, "error",
|
origin, "error", origin_message_id,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
final_result = result.final_content or "Task completed but no final response was generated."
|
final_result = result.final_content or "Task completed but no final response was generated."
|
||||||
logger.info("Subagent [{}] completed successfully", task_id)
|
logger.info("Subagent [{}] completed successfully", task_id)
|
||||||
await self._announce_result(task_id, label, task, final_result, origin, "ok")
|
await self._announce_result(task_id, label, task, final_result, origin, "ok", origin_message_id)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
status.phase = "error"
|
status.phase = "error"
|
||||||
status.error = str(e)
|
status.error = str(e)
|
||||||
logger.error("Subagent [{}] failed: {}", task_id, e)
|
logger.error("Subagent [{}] failed: {}", task_id, e)
|
||||||
await self._announce_result(task_id, label, task, f"Error: {e}", origin, "error")
|
await self._announce_result(task_id, label, task, f"Error: {e}", origin, "error", origin_message_id)
|
||||||
|
|
||||||
async def _announce_result(
|
async def _announce_result(
|
||||||
self,
|
self,
|
||||||
@ -251,6 +253,7 @@ class SubagentManager:
|
|||||||
result: str,
|
result: str,
|
||||||
origin: dict[str, str],
|
origin: dict[str, str],
|
||||||
status: str,
|
status: str,
|
||||||
|
origin_message_id: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Announce the subagent result to the main agent via the message bus."""
|
"""Announce the subagent result to the main agent via the message bus."""
|
||||||
status_text = "completed successfully" if status == "ok" else "failed"
|
status_text = "completed successfully" if status == "ok" else "failed"
|
||||||
@ -269,16 +272,19 @@ class SubagentManager:
|
|||||||
# routed to the correct pending queue (mid-turn injection) instead of
|
# routed to the correct pending queue (mid-turn injection) instead of
|
||||||
# being dispatched as a competing independent task.
|
# being dispatched as a competing independent task.
|
||||||
override = origin.get("session_key") or f"{origin['channel']}:{origin['chat_id']}"
|
override = origin.get("session_key") or f"{origin['channel']}:{origin['chat_id']}"
|
||||||
|
metadata: dict[str, Any] = {
|
||||||
|
"injected_event": "subagent_result",
|
||||||
|
"subagent_task_id": task_id,
|
||||||
|
}
|
||||||
|
if origin_message_id:
|
||||||
|
metadata["origin_message_id"] = origin_message_id
|
||||||
msg = InboundMessage(
|
msg = InboundMessage(
|
||||||
channel="system",
|
channel="system",
|
||||||
sender_id="subagent",
|
sender_id="subagent",
|
||||||
chat_id=f"{origin['channel']}:{origin['chat_id']}",
|
chat_id=f"{origin['channel']}:{origin['chat_id']}",
|
||||||
content=announce_content,
|
content=announce_content,
|
||||||
session_key_override=override,
|
session_key_override=override,
|
||||||
metadata={
|
metadata=metadata,
|
||||||
"injected_event": "subagent_result",
|
|
||||||
"subagent_task_id": task_id,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
await self.bus.publish_inbound(msg)
|
await self.bus.publish_inbound(msg)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user