DockerSandbox + LangChain file/shell tools extracted into a standalone package. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
"""move_file.py – tool for moving/renaming files inside the sandbox."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from shlex import quote
|
||
from typing import TYPE_CHECKING
|
||
|
||
from langchain_core.tools import BaseTool, tool
|
||
from loguru import logger
|
||
|
||
from docker_agent_sandbox.tools._utils import _parent
|
||
|
||
if TYPE_CHECKING:
|
||
from docker_agent_sandbox.sandbox import DockerSandbox
|
||
|
||
|
||
def make_move_file_tool(sandbox: "DockerSandbox") -> BaseTool:
|
||
"""Return a move_file tool bound to *sandbox*."""
|
||
|
||
@tool
|
||
def move_file(src: str, dst: str) -> str:
|
||
"""
|
||
Move or rename a file from *src* to *dst*.
|
||
|
||
Parent directories of *dst* are created automatically.
|
||
Returns a confirmation message or an error.
|
||
"""
|
||
logger.debug("Moving file inside sandbox: {!r} -> {!r}", src, dst)
|
||
mkdir_cmd = f"mkdir -p -- {quote(_parent(dst))}"
|
||
exit_code, output = sandbox.exec(
|
||
f"{mkdir_cmd} && mv -- {quote(src)} {quote(dst)}"
|
||
)
|
||
if exit_code != 0:
|
||
return f"[ERROR moving {src!r} to {dst!r}] {output.strip()}"
|
||
return f"[OK] Moved {src} -> {dst}"
|
||
|
||
return move_file
|