ducklm/duck_core/tools/gateway.py

32 lines
1.0 KiB
Python

from typing import Any
from duck_core.tools.base import Tool, ToolResult
from duck_core.tools.file_read import FileReadTool
from duck_core.tools.file_write import FileWriteTool
from duck_core.tools.shell_exec_safe import ShellExecSafeTool
class ToolGateway:
def __init__(self, tools: list[Tool]):
self.tools = {tool.name: tool for tool in tools}
@classmethod
def default(cls, workspace: str) -> "ToolGateway":
return cls(
[
FileReadTool(workspace),
FileWriteTool(workspace),
ShellExecSafeTool(workspace),
]
)
async def run_action(self, action: dict[str, Any]) -> ToolResult:
tool_name = str(action.get("tool", ""))
tool = self.tools.get(tool_name)
if tool is None:
return ToolResult(ok=False, error=f"Unknown tool: {tool_name}")
args = action.get("args") or {}
if not isinstance(args, dict):
return ToolResult(ok=False, error="Tool args must be an object")
return await tool.run(args)