use async_trait::async_trait;
use crate::ast::Value;
use crate::interpreter::{ExecResult, OutputData};
use crate::tools::{ExecContext, Tool, ToolArgs, ToolSchema, ParamSchema};
pub struct Echo;
#[async_trait]
impl Tool for Echo {
fn name(&self) -> &str {
"echo"
}
fn schema(&self) -> ToolSchema {
ToolSchema::new("echo", "Print arguments to standard output")
.param(ParamSchema::optional(
"args",
"any",
Value::Null,
"Values to print",
))
.param(ParamSchema::optional(
"no_newline",
"bool",
Value::Bool(false),
"Do not output trailing newline (-n)",
))
.example("Print a message", "echo hello world")
.example("No trailing newline", "echo -n \"prompt: \"")
}
async fn execute(&self, args: ToolArgs, _ctx: &mut ExecContext) -> ExecResult {
let no_newline = args.has_flag("no_newline") || args.has_flag("n");
let parts: Vec<String> = args
.positional
.iter()
.map(value_to_string)
.collect();
let mut output = parts.join(" ");
if !no_newline && !output.is_empty() {
output.push('\n');
}
ExecResult::with_output(OutputData::text(output))
}
}
fn value_to_string(value: &Value) -> String {
match value {
Value::Null => "null".to_string(),
Value::Bool(b) => b.to_string(),
Value::Int(i) => i.to_string(),
Value::Float(f) => f.to_string(),
Value::String(s) => s.clone(),
Value::Json(json) => json.to_string(),
Value::Blob(blob) => format!("[blob: {} {}]", blob.formatted_size(), blob.content_type),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vfs::{MemoryFs, VfsRouter};
use std::sync::Arc;
fn make_ctx() -> ExecContext {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
ExecContext::new(Arc::new(vfs))
}
#[tokio::test]
async fn test_echo_simple() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("hello".into()));
let result = Echo.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "hello\n");
}
#[tokio::test]
async fn test_echo_multiple() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("hello".into()));
args.positional.push(Value::String("world".into()));
let result = Echo.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "hello world\n");
}
#[tokio::test]
async fn test_echo_types() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::Int(42));
args.positional.push(Value::Bool(true));
args.positional.push(Value::Float(3.14));
let result = Echo.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "42 true 3.14\n");
}
#[tokio::test]
async fn test_echo_empty() {
let mut ctx = make_ctx();
let args = ToolArgs::new();
let result = Echo.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), ""); }
#[tokio::test]
async fn test_echo_n_no_newline() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("hello".into()));
args.flags.insert("n".to_string());
let result = Echo.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "hello"); }
#[tokio::test]
async fn test_echo_no_newline_flag() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("test".into()));
args.flags.insert("no_newline".to_string());
let result = Echo.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(&*result.text_out(), "test");
}
}