Files
docker-agent-sandbox/docker_agent_sandbox/tools/bash.py
2026-04-02 14:41:18 +02:00

37 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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