Skip to content

Commit a045361

Browse files
committed
Added INTERPRETER_SIMPLE_BASH option
1 parent dbd3838 commit a045361

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

interpreter_1/tools/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import os
2+
13
from .base import CLIResult, ToolResult
2-
from .bash import BashTool
34
from .collection import ToolCollection
45
from .computer import ComputerTool
56
from .edit import EditTool
67

8+
if os.environ.get("INTERPRETER_SIMPLE_BASH") == "true":
9+
from .simple_bash import BashTool
10+
else:
11+
from .bash import BashTool
12+
713
__ALL__ = [
814
BashTool,
915
CLIResult,

interpreter_1/tools/simple_bash.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import asyncio
2+
import os
3+
from typing import ClassVar, Literal
4+
5+
from anthropic.types.beta import BetaToolBash20241022Param
6+
7+
from .base import BaseAnthropicTool, CLIResult, ToolError, ToolResult
8+
9+
print("Using simple bash tool")
10+
11+
12+
class BashTool(BaseAnthropicTool):
13+
"""A tool that executes bash commands and returns their output."""
14+
15+
name: ClassVar[Literal["bash"]] = "bash"
16+
api_type: ClassVar[Literal["bash_20241022"]] = "bash_20241022"
17+
18+
async def __call__(
19+
self, command: str | None = None, restart: bool = False, **kwargs
20+
):
21+
if not command:
22+
raise ToolError("no command provided")
23+
24+
try:
25+
# Create process with shell=True to handle all bash features properly
26+
process = await asyncio.create_subprocess_shell(
27+
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
28+
)
29+
30+
try:
31+
stdout, stderr = await asyncio.wait_for(
32+
process.communicate(), timeout=300
33+
)
34+
35+
# Decode and combine output
36+
output = stdout.decode() + stderr.decode()
37+
38+
# Print output
39+
print(output, end="", flush=True)
40+
41+
# Return combined output
42+
return CLIResult(output=output if output else "<No output>")
43+
44+
except asyncio.TimeoutError:
45+
process.kill()
46+
msg = "Command timed out after 5 minutes"
47+
print(msg)
48+
return ToolResult(error=msg)
49+
50+
except Exception as e:
51+
msg = f"Failed to run command: {str(e)}"
52+
print(msg)
53+
return ToolResult(error=msg)
54+
55+
def to_params(self) -> BetaToolBash20241022Param:
56+
return {
57+
"type": self.api_type,
58+
"name": self.name,
59+
}

0 commit comments

Comments
 (0)