use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
use base64::{engine::general_purpose::STANDARD, Engine as _};
use rpi_agent::agent_tool::AgentTool;
use rpi_agent::error::AgentError;
use rpi_agent::types::{AgentToolResult, TextContentOrImage, ToolResultPartial};
use rpi_ai::types::Tool;
use schemars::JsonSchema;
use serde::Deserialize;
use tokio_util::sync::CancellationToken;
use crate::env::{FileContent, ShellOverride};
use crate::shell_output::{execute_shell_with_capture, ShellCaptureOptions};
use crate::tools::tool_context::ExecutionToolContext;
#[derive(Debug, Clone, JsonSchema, Deserialize)]
pub struct PowerShellInput {
pub command: String,
#[serde(default)]
pub timeout: Option<f64>,
}
pub struct PowerShellTool {
schema: Tool,
env: Arc<dyn crate::env::ExecutionEnv>,
}
impl PowerShellTool {
fn schema() -> Tool {
Tool {
name: "powershell".into(),
description: "Execute a PowerShell command on Windows. Returns stdout and stderr. Prefer bash for file/text work; use this only for PowerShell object operations (registry, COM/WMI, scheduled tasks, .NET)."
.into(),
parameters: rpi_ai::types::Schema::new(
serde_json::to_value(schemars::schema_for!(PowerShellInput)).unwrap_or_default(),
),
constrained_sampling: None,
}
}
}
pub fn create_powershell_tool(context: &ExecutionToolContext) -> Arc<dyn AgentTool> {
Arc::new(PowerShellTool {
schema: PowerShellTool::schema(),
env: context.env().clone(),
})
}
fn resolve_powershell_program(program_files: Option<&str>, path: Option<&str>) -> PathBuf {
if let Some(path) = path {
for dir in std::env::split_paths(path) {
let candidate = dir.join("pwsh.exe");
if candidate.is_file() {
return candidate;
}
}
}
if let Some(program_files) = program_files {
let candidate = PathBuf::from(program_files).join("PowerShell/7/pwsh.exe");
if candidate.is_file() {
return candidate;
}
}
PathBuf::from("powershell.exe")
}
fn build_wrapper_command(script_path: &str) -> String {
let escaped = script_path.replace('\'', "\u{27}\u{27}");
format!("$ProgressPreference='SilentlyContinue'; . '{}'", escaped)
}
fn build_staged_script(command: &str) -> Vec<u8> {
let mut bytes = Vec::with_capacity(command.len() + 3);
bytes.extend_from_slice(&[0xEF, 0xBB, 0xBF]);
bytes.extend_from_slice(command.as_bytes());
bytes
}
#[async_trait]
impl AgentTool for PowerShellTool {
fn schema(&self) -> &Tool {
&self.schema
}
fn label(&self) -> &str {
"powershell"
}
async fn execute(
&self,
_tool_call_id: &str,
params: serde_json::Value,
signal: CancellationToken,
_on_update: Arc<dyn Fn(ToolResultPartial) + Send + Sync>,
) -> Result<AgentToolResult, AgentError> {
if !cfg!(windows) {
return Err(AgentError::Tool(
"PowerShell tool is only available on Windows".into(),
));
}
let input: PowerShellInput = serde_json::from_value(params)
.map_err(|e| AgentError::Validation(format!("powershell input invalid: {e}")))?;
let staged_path = self
.env
.create_temp_file("rpi-ps-", ".ps1", Some(&signal))
.await
.map_err(|e| AgentError::Tool(format!("create staged PowerShell script: {e}")))?;
self.env
.write_file(
&staged_path.to_string_lossy(),
FileContent::Bytes(build_staged_script(&input.command)),
Some(&signal),
)
.await
.map_err(|e| AgentError::Tool(format!("stage PowerShell script: {e}")))?;
let script = build_wrapper_command(&staged_path.to_string_lossy());
let utf16: Vec<u8> = script.encode_utf16().flat_map(u16::to_le_bytes).collect();
let encoded = STANDARD.encode(utf16);
let command = encoded;
let program = if cfg!(windows) {
resolve_powershell_program(
std::env::var("ProgramFiles").ok().as_deref(),
std::env::var("PATH").ok().as_deref(),
)
} else {
PathBuf::from("powershell.exe")
};
let staged_path_text = staged_path.to_string_lossy().into_owned();
let capture = execute_shell_with_capture(
&self.env,
&command,
ShellCaptureOptions {
shell: Some(ShellOverride {
program,
args: vec![
"-NoLogo".into(),
"-NoProfile".into(),
"-NonInteractive".into(),
"-ExecutionPolicy".into(),
"Bypass".into(),
"-EncodedCommand".into(),
],
}),
cwd: Some(self.env.cwd().to_path_buf()),
env: None,
inherit_env: true,
timeout: input.timeout,
cancel: Some(&signal),
on_chunk: None,
return_execution_errors: true,
},
)
.await
.map_err(|e| AgentError::Tool(e.to_string()))?;
let _ = self
.env
.remove(&staged_path_text, false, true, Some(&signal))
.await;
if let Some(dir) = staged_path.parent() {
let _ = self
.env
.remove(&dir.to_string_lossy(), false, true, Some(&signal))
.await;
}
let mut output = capture.output;
if capture.cancelled {
return Err(AgentError::Tool("PowerShell command aborted".into()));
}
if let Some(error) = capture.execution_error {
return Err(AgentError::Tool(error.to_string()));
}
if let Some(code) = capture.exit_code.filter(|code| *code != 0) {
output.push_str(&format!("\n\nPowerShell exited with code {code}"));
return Err(AgentError::Tool(output));
}
if output.trim().is_empty() {
output = "(no output)".into();
}
Ok(AgentToolResult {
content: vec![TextContentOrImage::text(output)],
..Default::default()
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn touch(dir: &std::path::Path, name: &str) -> PathBuf {
std::fs::create_dir_all(dir).unwrap();
let file = dir.join(name);
std::fs::write(&file, b"").unwrap();
file
}
#[test]
fn review_issue14_path_pwsh_wins_over_fixed_location_and_51_fallback() {
let tmp = tempfile::tempdir().unwrap();
let on_path = tmp.path().join("scoop").join("shims");
let pwsh = touch(&on_path, "pwsh.exe");
let program_files = tmp.path().join("pf");
let fixed = touch(&program_files.join("PowerShell").join("7"), "pwsh.exe");
let path = on_path.to_string_lossy().into_owned();
let pf = program_files.to_string_lossy().into_owned();
assert_eq!(resolve_powershell_program(Some(&pf), Some(&path)), pwsh);
assert_eq!(resolve_powershell_program(Some(&pf), None), fixed);
assert_eq!(resolve_powershell_program(Some(&pf), Some("")), fixed);
let missing_pf = tmp.path().join("no-such-program-files");
assert_eq!(
resolve_powershell_program(Some(&missing_pf.to_string_lossy()), None),
PathBuf::from("powershell.exe")
);
assert_eq!(
resolve_powershell_program(None, None),
PathBuf::from("powershell.exe")
);
}
#[test]
fn review_issue14_empty_path_entries_do_not_shadow_powershell() {
let tmp = tempfile::tempdir().unwrap();
let empty = tmp.path().join("empty");
std::fs::create_dir_all(&empty).unwrap();
let path = empty.to_string_lossy().into_owned();
assert_eq!(
resolve_powershell_program(None, Some(&path)),
PathBuf::from("powershell.exe")
);
}
}
#[cfg(test)]
mod review_probes {
use super::*;
const REVIEW_USING_SCRIPT: &str =
"using namespace System.Text\n[StringBuilder]::new().GetType().Name\n";
const REVIEW_PARAM_SCRIPT: &str = "param([string]$Name='ok')\nWrite-Output $Name\n";
fn wrapper_for(script_path: &str) -> String {
build_wrapper_command(script_path)
}
#[test]
fn review270_wrapper_never_touches_the_user_script_text() {
for script in [REVIEW_USING_SCRIPT, REVIEW_PARAM_SCRIPT] {
let wrapper = wrapper_for("C:\\temp\\x.ps1");
assert!(
!wrapper.contains(script.trim_end()),
"the user script must not be inlined into the wrapper: {wrapper}"
);
}
}
#[test]
fn review270_wrapper_sets_progress_preference_then_dot_sources_the_file() {
let wrapper = wrapper_for("C:\\temp\\x.ps1");
assert!(
wrapper.starts_with("$ProgressPreference='SilentlyContinue';"),
"the preference must still be set first: {wrapper}"
);
assert!(
wrapper.contains(". 'C:\\temp\\x.ps1'"),
"the wrapper must dot-source the staged script: {wrapper}"
);
}
#[test]
fn review270_wrapper_quotes_the_path_for_the_single_quote_edge() {
let wrapper = wrapper_for("C:\\temp\\it's\\x.ps1");
assert!(
wrapper.contains(". 'C:\\temp\\it''s\\x.ps1'"),
"single quotes in the staged path must be doubled: {wrapper}"
);
}
#[test]
fn review270_user_script_is_written_verbatim_with_utf8_bom() {
let mut staged = build_staged_script(REVIEW_USING_SCRIPT);
assert!(staged.starts_with(b"\xEF\xBB\xBF"), "BOM required for 5.1");
assert_eq!(&staged[3..], REVIEW_USING_SCRIPT.as_bytes());
staged = build_staged_script(REVIEW_PARAM_SCRIPT);
assert_eq!(&staged[3..], REVIEW_PARAM_SCRIPT.as_bytes());
}
#[test]
fn review270_encoded_wrapper_decodes_to_the_expected_wrapper() {
let wrapper = wrapper_for("C:\\temp\\x.ps1");
let utf16: Vec<u8> = wrapper.encode_utf16().flat_map(u16::to_le_bytes).collect();
let encoded = STANDARD.encode(utf16);
let bytes = STANDARD.decode(&encoded).unwrap();
let units: Vec<u16> = bytes
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect();
assert_eq!(String::from_utf16(&units).unwrap(), wrapper);
}
#[test]
fn review270_encoded_command_carries_no_statement_before_using_or_param() {
let old_prefix = "$ProgressPreference='SilentlyContinue'; ";
for script in [REVIEW_USING_SCRIPT, REVIEW_PARAM_SCRIPT] {
let staged = build_staged_script(script);
let wrapper = wrapper_for("C:\\temp\\rpi-ps-x.ps1");
let encoded = STANDARD.encode(
wrapper
.encode_utf16()
.flat_map(u16::to_le_bytes)
.collect::<Vec<u8>>(),
);
let decoded = STANDARD.decode(&encoded).unwrap();
let units: Vec<u16> = decoded
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect();
let wire = String::from_utf16(&units).unwrap();
assert!(
!wire.contains(script.trim_end()),
"the caller's script must not be inlined into -EncodedCommand: {wire}"
);
assert!(
!wire.contains(&format!("{old_prefix}{}", script.trim_end())),
"the old prefix+script concatenation must be gone: {wire}"
);
assert_eq!(&staged[3..], script.as_bytes());
}
}
}