refactor: simplify /stop dispatch, inline commands, trim verbose docstrings
This commit is contained in:
@@ -18,13 +18,7 @@ from nanobot.agent.tools.web import WebSearchTool, WebFetchTool
|
||||
|
||||
|
||||
class SubagentManager:
|
||||
"""
|
||||
Manages background subagent execution.
|
||||
|
||||
Subagents are lightweight agent instances that run in the background
|
||||
to handle specific tasks. They share the same LLM provider but have
|
||||
isolated context and a focused system prompt.
|
||||
"""
|
||||
"""Manages background subagent execution."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -59,43 +53,24 @@ class SubagentManager:
|
||||
origin_chat_id: str = "direct",
|
||||
session_key: str | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
Spawn a subagent to execute a task in the background.
|
||||
|
||||
Args:
|
||||
task: The task description for the subagent.
|
||||
label: Optional human-readable label for the task.
|
||||
origin_channel: The channel to announce results to.
|
||||
origin_chat_id: The chat ID to announce results to.
|
||||
|
||||
Returns:
|
||||
Status message indicating the subagent was started.
|
||||
"""
|
||||
"""Spawn a subagent to execute a task in the background."""
|
||||
task_id = str(uuid.uuid4())[:8]
|
||||
display_label = label or task[:30] + ("..." if len(task) > 30 else "")
|
||||
|
||||
origin = {
|
||||
"channel": origin_channel,
|
||||
"chat_id": origin_chat_id,
|
||||
}
|
||||
|
||||
# Create background task
|
||||
origin = {"channel": origin_channel, "chat_id": origin_chat_id}
|
||||
|
||||
bg_task = asyncio.create_task(
|
||||
self._run_subagent(task_id, task, display_label, origin)
|
||||
)
|
||||
self._running_tasks[task_id] = bg_task
|
||||
|
||||
if session_key:
|
||||
self._session_tasks.setdefault(session_key, set()).add(task_id)
|
||||
|
||||
def _cleanup(_: asyncio.Task) -> None:
|
||||
self._running_tasks.pop(task_id, None)
|
||||
if session_key:
|
||||
ids = self._session_tasks.get(session_key)
|
||||
if ids:
|
||||
ids.discard(task_id)
|
||||
if not ids:
|
||||
self._session_tasks.pop(session_key, None)
|
||||
if session_key and (ids := self._session_tasks.get(session_key)):
|
||||
ids.discard(task_id)
|
||||
if not ids:
|
||||
del self._session_tasks[session_key]
|
||||
|
||||
bg_task.add_done_callback(_cleanup)
|
||||
|
||||
@@ -267,17 +242,14 @@ Skills are available at: {self.workspace}/skills/ (read SKILL.md files as needed
|
||||
When you have completed the task, provide a clear summary of your findings or actions."""
|
||||
|
||||
async def cancel_by_session(self, session_key: str) -> int:
|
||||
"""Cancel all subagents spawned under the given session. Returns count cancelled."""
|
||||
task_ids = list(self._session_tasks.get(session_key, []))
|
||||
to_cancel: list[asyncio.Task] = []
|
||||
for tid in task_ids:
|
||||
t = self._running_tasks.get(tid)
|
||||
if t and not t.done():
|
||||
t.cancel()
|
||||
to_cancel.append(t)
|
||||
if to_cancel:
|
||||
await asyncio.gather(*to_cancel, return_exceptions=True)
|
||||
return len(to_cancel)
|
||||
"""Cancel all subagents for the given session. Returns count cancelled."""
|
||||
tasks = [self._running_tasks[tid] for tid in self._session_tasks.get(session_key, [])
|
||||
if tid in self._running_tasks and not self._running_tasks[tid].done()]
|
||||
for t in tasks:
|
||||
t.cancel()
|
||||
if tasks:
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
return len(tasks)
|
||||
|
||||
def get_running_count(self) -> int:
|
||||
"""Return the number of currently running subagents."""
|
||||
|
||||
Reference in New Issue
Block a user