mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-05 02:32:16 +03:00
refactor(agent): load MyTool through tool loader
This commit is contained in:
+1
-11
@@ -41,7 +41,6 @@ from nanobot.agent.tools.file_state import FileStateStore, bind_file_states, res
|
||||
from nanobot.agent.tools.message import MessageTool
|
||||
from nanobot.agent.tools.registry import ToolRegistry
|
||||
from nanobot.agent.tools.runtime_control import AgentRuntimeControl
|
||||
from nanobot.agent.tools.self import MyTool
|
||||
from nanobot.agent.turn_delivery import (
|
||||
TurnDelivery,
|
||||
TurnDeliveryFactory,
|
||||
@@ -644,20 +643,11 @@ class AgentLoop:
|
||||
timezone=self.context.timezone or "UTC",
|
||||
workspace_sandbox=self.workspace_scopes.sandbox_status,
|
||||
runtime_events=self.runtime_events,
|
||||
runtime_control=AgentRuntimeControl(self),
|
||||
)
|
||||
loader = ToolLoader()
|
||||
registered = loader.load(ctx, self.tools)
|
||||
|
||||
# MyTool receives only the explicit runtime-control capability.
|
||||
if self.tools_config.my.enable:
|
||||
self.tools.register(
|
||||
MyTool(
|
||||
runtime_control=AgentRuntimeControl(self),
|
||||
modify_allowed=self.tools_config.my.allow_set,
|
||||
)
|
||||
)
|
||||
registered.append("my")
|
||||
|
||||
logger.info("Registered {} tools: {}", len(registered), registered)
|
||||
|
||||
def register_runtime_context_provider(
|
||||
|
||||
@@ -11,6 +11,7 @@ if TYPE_CHECKING:
|
||||
from nanobot.agent.subagent import SubagentManager
|
||||
from nanobot.agent.tools.exec_session import ExecSessionManager
|
||||
from nanobot.agent.tools.file_state import FileStates
|
||||
from nanobot.agent.tools.runtime_control import RuntimeControl
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.bus.runtime_events import RuntimeEventBus
|
||||
from nanobot.config.schema import ProviderConfig, ToolsConfig
|
||||
@@ -90,3 +91,4 @@ class ToolContext:
|
||||
timezone: str = "UTC"
|
||||
workspace_sandbox: WorkspaceSandboxStatus | None = None
|
||||
runtime_events: RuntimeEventBus | None = None
|
||||
runtime_control: RuntimeControl | None = None
|
||||
|
||||
@@ -58,7 +58,6 @@ def _is_string_mapping(value: object) -> TypeGuard[Mapping[str, object]]:
|
||||
class MyTool(Tool):
|
||||
"""Check and set the agent loop's runtime configuration."""
|
||||
|
||||
_plugin_discoverable = False # Requires AgentLoop reference; registered manually
|
||||
config_key = "my"
|
||||
|
||||
@classmethod
|
||||
@@ -67,7 +66,16 @@ class MyTool(Tool):
|
||||
|
||||
@classmethod
|
||||
def enabled(cls, ctx: ToolContext) -> bool:
|
||||
return ctx.config.my.enable
|
||||
return ctx.runtime_control is not None and ctx.config.my.enable
|
||||
|
||||
@classmethod
|
||||
def create(cls, ctx: ToolContext) -> Tool:
|
||||
if ctx.runtime_control is None:
|
||||
raise RuntimeError("MyTool requires a runtime control capability")
|
||||
return cls(
|
||||
runtime_control=ctx.runtime_control,
|
||||
modify_allowed=ctx.config.my.allow_set,
|
||||
)
|
||||
|
||||
BLOCKED = frozenset({
|
||||
# Core infrastructure
|
||||
|
||||
Reference in New Issue
Block a user