mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-19 16:12:30 +00:00
This commit implements a progressive refactoring of the tool system to support plugin discovery, scoped loading, and protocol-driven runtime context injection. Key changes: - Add Tool ABC metadata (tool_name, _scopes) and ToolContext dataclass for dependency injection. - Introduce ToolLoader with pkgutil-based builtin discovery and entry_points-based third-party plugin loading. - Add scope filtering (core/subagent/memory) so different contexts load appropriate tool sets. - Introduce ContextAware protocol and RequestContext dataclass to replace hardcoded per-tool context injection in AgentLoop. - Add RuntimeState / MutableRuntimeState protocols to decouple MyTool from AgentLoop. - Migrate all built-in tools to declare scopes and implement create()/enabled() hooks. - Migrate MessageTool, SpawnTool, CronTool, and MyTool to ContextAware. - Refactor AgentLoop to use ToolLoader and protocol-driven context injection. - Refactor SubagentManager to use ToolLoader(scope="subagent") with per-run FileStates isolation. - Register all built-in tools via pyproject.toml entry_points. - Add comprehensive tests for loader scopes, entry_points, ContextAware, subagent tools, and runtime state sync.
78 lines
1.5 KiB
Python
78 lines
1.5 KiB
Python
import pytest
|
|
|
|
from nanobot.agent.tools.base import Tool
|
|
from nanobot.agent.tools.context import ToolContext
|
|
from nanobot.agent.tools.loader import ToolLoader
|
|
|
|
|
|
class _CoreOnlyTool(Tool):
|
|
_scopes = {"core"}
|
|
|
|
@property
|
|
def name(self):
|
|
return "core_only"
|
|
|
|
@property
|
|
def description(self):
|
|
return "..."
|
|
|
|
@property
|
|
def parameters(self):
|
|
return {"type": "object"}
|
|
|
|
async def execute(self, **_):
|
|
return "ok"
|
|
|
|
|
|
class _SubagentOnlyTool(Tool):
|
|
_scopes = {"subagent"}
|
|
|
|
@property
|
|
def name(self):
|
|
return "sub_only"
|
|
|
|
@property
|
|
def description(self):
|
|
return "..."
|
|
|
|
@property
|
|
def parameters(self):
|
|
return {"type": "object"}
|
|
|
|
async def execute(self, **_):
|
|
return "ok"
|
|
|
|
|
|
class _UniversalTool(Tool):
|
|
_scopes = {"core", "subagent", "memory"}
|
|
|
|
@property
|
|
def name(self):
|
|
return "universal"
|
|
|
|
@property
|
|
def description(self):
|
|
return "..."
|
|
|
|
@property
|
|
def parameters(self):
|
|
return {"type": "object"}
|
|
|
|
async def execute(self, **_):
|
|
return "ok"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_loader_filters_by_scope():
|
|
from nanobot.agent.tools.registry import ToolRegistry
|
|
|
|
loader = ToolLoader(test_classes=[_CoreOnlyTool, _SubagentOnlyTool, _UniversalTool])
|
|
|
|
registry = ToolRegistry()
|
|
ctx = ToolContext(config={}, workspace="/tmp")
|
|
loader.load(ctx, registry, scope="core")
|
|
|
|
assert registry.has("core_only")
|
|
assert not registry.has("sub_only")
|
|
assert registry.has("universal")
|