37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""bash.py – tool for executing shell commands inside the sandbox."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import TYPE_CHECKING
|
||
|
||
from langchain_core.tools import BaseTool, tool
|
||
from loguru import logger
|
||
|
||
from docker_agent_sandbox.tools._utils import truncate_output
|
||
|
||
if TYPE_CHECKING:
|
||
from docker_agent_sandbox.sandbox import DockerSandbox
|
||
|
||
|
||
def make_bash_tool(sandbox: "DockerSandbox") -> BaseTool:
|
||
"""
|
||
Return a bash tool that executes commands inside the Docker sandbox container.
|
||
|
||
The model's working directory is the sandbox root; all paths it uses are
|
||
identical on the host (via the bind mount) and inside the container.
|
||
"""
|
||
|
||
@tool
|
||
def bash(command: str, timeout: int = 120) -> str:
|
||
"""
|
||
Execute a shell command in the sandbox container.
|
||
|
||
Returns EXIT:<code> followed by combined stdout+stderr.
|
||
Large outputs are truncated to stay within token limits.
|
||
"""
|
||
logger.debug("Running inside sandbox: {}", command)
|
||
exit_code, output = sandbox.exec(command, timeout=timeout)
|
||
return f"EXIT:{exit_code}\n{truncate_output(output)}"
|
||
|
||
return bash
|