mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-08 05:18:49 +03:00
* feat(webui): add project workspaces and access controls * feat(webui): add project workspaces and access controls * refactor(tools): centralize workspace access resolution * refactor(webui): remove unused workspace host state * fix(webui): hide estimated file edit label * fix(webui): clarify file edit deletion feedback * fix(webui): label deleted file activity * fix(webui): flatten file edit activity rows * fix(core): remove path-only patch deletion * fix(core): keep apply patch non-destructive * refactor(webui): trim workspace host plumbing * fix(tools): register exec with tools config
97 lines
3.6 KiB
Python
97 lines
3.6 KiB
Python
"""Spawn tool for creating background subagents."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextvars import ContextVar
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from nanobot.agent.tools.base import Tool, tool_parameters
|
|
from nanobot.agent.tools.context import ContextAware, RequestContext
|
|
from nanobot.agent.tools.schema import NumberSchema, StringSchema, tool_parameters_schema
|
|
from nanobot.security.workspace_access import current_workspace_scope
|
|
|
|
if TYPE_CHECKING:
|
|
from nanobot.agent.subagent import SubagentManager
|
|
|
|
|
|
@tool_parameters(
|
|
tool_parameters_schema(
|
|
task=StringSchema("The task for the subagent to complete"),
|
|
label=StringSchema("Optional short label for the task (for display)"),
|
|
temperature=NumberSchema(
|
|
description=(
|
|
"Optional sampling temperature for the subagent "
|
|
"(0.0 = deterministic, higher = more creative). "
|
|
"Defaults to the provider's configured temperature."
|
|
),
|
|
minimum=0.0,
|
|
maximum=2.0,
|
|
),
|
|
required=["task"],
|
|
)
|
|
)
|
|
class SpawnTool(Tool, ContextAware):
|
|
"""Tool to spawn a subagent for background task execution."""
|
|
|
|
def __init__(self, manager: "SubagentManager"):
|
|
self._manager = manager
|
|
self._origin_channel: ContextVar[str] = ContextVar("spawn_origin_channel", default="cli")
|
|
self._origin_chat_id: ContextVar[str] = ContextVar("spawn_origin_chat_id", default="direct")
|
|
self._session_key: ContextVar[str] = ContextVar("spawn_session_key", default="cli:direct")
|
|
self._origin_message_id: ContextVar[str | None] = ContextVar(
|
|
"spawn_origin_message_id",
|
|
default=None,
|
|
)
|
|
|
|
@classmethod
|
|
def create(cls, ctx: Any) -> Tool:
|
|
return cls(manager=ctx.subagent_manager)
|
|
|
|
def set_context(self, ctx: RequestContext) -> None:
|
|
"""Set the origin context for subagent announcements."""
|
|
self._origin_channel.set(ctx.channel)
|
|
self._origin_chat_id.set(ctx.chat_id)
|
|
self._session_key.set(ctx.session_key or f"{ctx.channel}:{ctx.chat_id}")
|
|
self._origin_message_id.set(ctx.message_id)
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "spawn"
|
|
|
|
@property
|
|
def description(self) -> str:
|
|
return (
|
|
"Spawn a subagent to handle a task in the background. "
|
|
"Use this for complex or time-consuming tasks that can run independently. "
|
|
"The subagent will complete the task and report back when done. "
|
|
"For deliverables or existing projects, inspect the workspace first "
|
|
"and use a dedicated subdirectory when helpful."
|
|
)
|
|
|
|
async def execute(
|
|
self,
|
|
task: str,
|
|
label: str | None = None,
|
|
temperature: float | None = None,
|
|
**kwargs: Any,
|
|
) -> str:
|
|
"""Spawn a subagent to execute the given task."""
|
|
running = self._manager.get_running_count()
|
|
limit = self._manager.max_concurrent_subagents
|
|
if running >= limit:
|
|
return (
|
|
f"Cannot spawn subagent: concurrency limit reached "
|
|
f"({running}/{limit} running). Wait for a running subagent "
|
|
f"to complete before spawning a new one."
|
|
)
|
|
return await self._manager.spawn(
|
|
task=task,
|
|
label=label,
|
|
origin_channel=self._origin_channel.get(),
|
|
origin_chat_id=self._origin_chat_id.get(),
|
|
session_key=self._session_key.get(),
|
|
origin_message_id=self._origin_message_id.get(),
|
|
temperature=temperature,
|
|
workspace_scope=current_workspace_scope(),
|
|
)
|