Rebase onto upstream (a4d95fd)
#12
@@ -16,6 +16,7 @@ from nanobot.agent.tools.filesystem import ReadFileTool, WriteFileTool, EditFile
|
||||
from nanobot.agent.tools.shell import ExecTool
|
||||
from nanobot.agent.tools.web import WebSearchTool, WebFetchTool
|
||||
from nanobot.agent.tools.spawn import SpawnTool
|
||||
from nanobot.agent.tools.subagent_message import SubagentMessageTool
|
||||
from nanobot.agent.tools.wait import WaitForSubagentsTool
|
||||
|
||||
|
||||
@@ -121,6 +122,17 @@ class SubagentManager:
|
||||
))
|
||||
tools.register(WebSearchTool(api_key=self.brave_api_key))
|
||||
tools.register(WebFetchTool())
|
||||
|
||||
# Message tool for communicating with user (via main agent)
|
||||
message_tool = SubagentMessageTool(
|
||||
bus=self.bus,
|
||||
origin_channel=origin["channel"],
|
||||
origin_chat_id=origin["chat_id"],
|
||||
origin_metadata=origin.get("metadata"),
|
||||
)
|
||||
tools.register(message_tool)
|
||||
|
||||
# Spawn tool for creating child subagents
|
||||
spawn_tool = SpawnTool(manager=self)
|
||||
spawn_tool.set_context("subagent", origin["chat_id"], origin.get("metadata"))
|
||||
tools.register(spawn_tool)
|
||||
@@ -252,11 +264,12 @@ You are a subagent spawned by the main agent to complete a specific task.
|
||||
- Read and write files in the workspace
|
||||
- Execute shell commands
|
||||
- Search the web and fetch web pages
|
||||
- Send messages to the user (via the message tool)
|
||||
- Spawn child subagents for parallel tasks
|
||||
- Complete the task thoroughly
|
||||
|
||||
## What You Cannot Do
|
||||
- Send messages directly to users (no message tool available)
|
||||
- Access the main agent's conversation history
|
||||
- Access the main agent's conversation history directly
|
||||
|
||||
## Workspace
|
||||
Your workspace is at: {self.workspace}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Message tool for subagents to communicate with the main agent."""
|
||||
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
from nanobot.agent.tools.base import Tool
|
||||
from nanobot.bus.events import InboundMessage
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.bus.queue import MessageBus
|
||||
|
||||
|
||||
class SubagentMessageTool(Tool):
|
||||
"""
|
||||
Tool for subagents to send messages to the main agent.
|
||||
|
||||
Messages are sent via the bus and preserve metadata (e.g. suppress_output)
|
||||
from the originating message that spawned the subagent.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
bus: "MessageBus",
|
||||
origin_channel: str,
|
||||
origin_chat_id: str,
|
||||
origin_metadata: dict[str, Any] | None = None,
|
||||
):
|
||||
self._bus = bus
|
||||
self._origin_channel = origin_channel
|
||||
self._origin_chat_id = origin_chat_id
|
||||
self._origin_metadata = origin_metadata or {}
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "message"
|
||||
|
||||
@property
|
||||
def description(self) -> str:
|
||||
return (
|
||||
"Send a message to the user through the main agent. "
|
||||
"Use this to communicate findings, ask questions, or provide updates. "
|
||||
"Messages will be delivered through the same channel that spawned this subagent."
|
||||
)
|
||||
|
||||
@property
|
||||
def parameters(self) -> dict[str, Any]:
|
||||
return {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "The message content to send to the user"
|
||||
},
|
||||
},
|
||||
"required": ["content"]
|
||||
}
|
||||
|
||||
async def execute(self, content: str, **kwargs: Any) -> str:
|
||||
"""Send a message to the main agent via the bus."""
|
||||
# Create InboundMessage to trigger main agent
|
||||
msg = InboundMessage(
|
||||
channel="system",
|
||||
sender_id="subagent",
|
||||
chat_id=f"{self._origin_channel}:{self._origin_chat_id}",
|
||||
content=f"[Subagent message]\n\n{content}",
|
||||
metadata=self._origin_metadata,
|
||||
)
|
||||
|
||||
try:
|
||||
await self._bus.publish_inbound(msg)
|
||||
return "Message sent to user"
|
||||
except Exception as e:
|
||||
return f"Error sending message: {str(e)}"
|
||||
Reference in New Issue
Block a user