mod command;
mod output;
mod process;
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
use unix as platform;
#[cfg(windows)]
use windows as platform;
use std::str::FromStr;
use serde_json::json;
use tea_control::CancellationScope;
use tea_protocol::ToolIdempotency;
use tea_tools::{
BoxToolExecutionStream, ToolConcurrency, ToolEffect, ToolExecutionSemantics, ToolExecutor,
ToolName, ToolResource, ToolResourceAccess, ToolResourceError, ToolRetrySafety, ToolSpec,
ToolSpecError, ToolTimeout, ToolVersion, ValidatedToolInvocation,
};
pub use command::{BashConfig, BashOutputDirectory, BashShell};
use crate::{FileToolError, FileToolErrorCode, WorkspaceRoot};
#[derive(Debug, Clone)]
pub struct BashTool {
workspace: WorkspaceRoot,
config: BashConfig,
}
impl BashTool {
#[must_use]
pub const fn new(workspace: WorkspaceRoot, config: BashConfig) -> Self {
Self { workspace, config }
}
pub fn workspace_resource() -> Result<ToolResource, ToolResourceError> {
ToolResource::new("file", "/workspace", ToolResourceAccess::Execute)
}
pub fn spec() -> Result<ToolSpec, ToolSpecError> {
ToolSpec::new(
ToolName::from_str("bash").map_err(|_| ToolSpecError::InvalidDescription)?,
ToolVersion::from_str("1.0.0").map_err(|_| ToolSpecError::InvalidDescription)?,
"Run a command through the configured shell in the workspace.",
json!({"type":"object","properties":{"command":{"type":"string","minLength":1,"maxLength":65536}},"required":["command"],"additionalProperties":false}),
json!({"type":"object","properties":{"stdout":{"type":"string"},"stderr":{"type":"string"},"exitCode":{"type":["integer","null"]},"success":{"type":"boolean"},"truncated":{"type":"boolean"},"overflowReference":{"type":["string","null"]}},"required":["stdout","stderr","exitCode","success","truncated","overflowReference"],"additionalProperties":false}),
[ToolEffect::ProcessSpawn],
ToolExecutionSemantics::new(
ToolIdempotency::NonIdempotent,
ToolRetrySafety::Never,
ToolConcurrency::Serial,
ToolTimeout::from_millis(120_000)?,
)?,
)?
.with_prompt_hint("Commands run in the workspace through a host-configured shell; cancellation after spawn is uncertain.")
}
}
impl ToolExecutor for BashTool {
fn execute(
&self,
invocation: ValidatedToolInvocation,
cancellation: CancellationScope,
) -> BoxToolExecutionStream {
let command = invocation
.arguments()
.get("command")
.and_then(serde_json::Value::as_str)
.filter(|command| !command.is_empty() && command.len() <= 65_536)
.map(str::to_owned);
match command {
Some(command) => process::execute_process(
self.workspace.clone(),
self.config.clone(),
command,
cancellation,
),
None => Box::pin(futures_util::stream::iter([
tea_tools::ToolExecutionEvent::Failed(
tea_tools::ToolExecutionFailure::execution(
FileToolError::new(FileToolErrorCode::InvalidArguments).message(),
)
.unwrap_or_else(|_| tea_tools::ToolExecutionFailure::internal_contract()),
),
])),
}
}
}