Newer
Older
navi-1 / navi / tools / reload_tools.py
"""Built-in tool to hot-reload user tools from the tools/ directory.

The agent calls this after writing or editing a file in tools/.
Errors in individual tool files are reported without crashing anything.
"""

from navi.config import settings

from .base import Tool, ToolResult


class ReloadToolsTool(Tool):
    name = "reload_tools"
    description = (
        "Hot-reload all tools from the tools/ directory without restarting the server. "
        "Call this after writing or editing a tool file. "
        "Returns a report of what was loaded and any errors per file."
    )
    parameters = {
        "type": "object",
        "properties": {},
        "required": [],
    }

    def __init__(self, registry=None) -> None:  # registry injected at startup
        self._registry = registry

    async def execute(self, params: dict) -> ToolResult:
        if self._registry is None:
            return ToolResult(success=False, output="Tool registry not available.", error="no_registry")

        result = self._registry.reload_user_tools(settings.tools_dir)

        lines = []
        if result.loaded:
            lines.append(f"Loaded ({len(result.loaded)}): {', '.join(t.name for t in result.loaded)}")
        else:
            lines.append("No tools loaded.")

        if result.errors:
            lines.append(f"\nErrors ({len(result.errors)}):")
            for filename, error in result.errors.items():
                lines.append(f"  {filename}: {error}")

        success = not result.errors
        return ToolResult(success=success, output="\n".join(lines))