Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion trae_agent/tools/bash_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
from typing import override

from rich.prompt import Prompt

from .base import Tool, ToolCallArguments, ToolError, ToolExecResult, ToolParameter


Expand Down Expand Up @@ -136,6 +138,7 @@ class BashTool(Tool):
def __init__(self, model_provider: str | None = None):
super().__init__(model_provider)
self._session: _BashSession | None = None
self.sudo: bool | None = None

@override
def get_model_provider(self) -> str | None:
Expand Down Expand Up @@ -175,10 +178,30 @@ def get_parameters(self) -> list[ToolParameter]:
description="Set to true to restart the bash session.",
required=restart_required,
),
ToolParameter(
name="dangerous",
type="boolean",
description="Set to true if the command is dangerous.",
required=True,
),
]

@override
async def execute(self, arguments: ToolCallArguments) -> ToolExecResult:
command = str(arguments["command"]) if "command" in arguments else None
if arguments.get("dangerous") and not self.sudo:
allow = Prompt.ask(
f'Command "{command}" is considered dangerous. Are you sure you want to allow the agent to run this command? [Y/N/A] \n Y: Yes , N: No , A: Always Allow'
)

if allow != "Y" and allow != "A":
return ToolExecResult(error=f"user does not allow to run {command}", error_code=-1)

if allow == "A":
self.sudo = True
else:
self.sudo = False

if arguments.get("restart"):
if self._session:
self._session.stop()
Expand All @@ -194,7 +217,6 @@ async def execute(self, arguments: ToolCallArguments) -> ToolExecResult:
except Exception as e:
return ToolExecResult(error=f"Error starting bash session: {e}", error_code=-1)

command = str(arguments["command"]) if "command" in arguments else None
if command is None:
return ToolExecResult(
error=f"No command provided for the {self.get_name()} tool",
Expand Down
Loading