use claude_agent_sdk::{ClaudeAgentOptions, ClaudeClient};
use futures::stream::StreamExt;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("=== Example 1: Hello World ===\n");
std::fs::create_dir_all("./fixtures")?;
let options = ClaudeAgentOptions::builder()
.allowed_tools(vec!["Write".to_string()])
.permission_mode(claude_agent_sdk::PermissionMode::AcceptEdits)
.max_turns(5)
.stderr_callback(std::sync::Arc::new(|msg| {
eprintln!("STDERR: {}", msg);
}))
.build();
let mut client = ClaudeClient::new(options);
client.connect().await?;
println!("Asking Claude to write a Python hello world script...\n");
client
.query("Write a simple Python hello world script to ./fixtures/hello.py")
.await?;
let mut response_stream = client.receive_response();
while let Some(result) = response_stream.next().await {
match result {
Ok(message) => {
println!("Received: {:?}", message);
}
Err(e) => {
eprintln!("Error: {:?}", e);
break;
}
}
}
let filepath = "./fixtures/hello.py";
if std::path::Path::new(filepath).exists() {
println!("\n✓ File created: {}", filepath);
let content = std::fs::read_to_string(filepath)?;
println!("\nFile contents:");
println!("---");
println!("{}", content);
println!("---");
println!("\nRunning the script...");
let output = std::process::Command::new("python3")
.arg(filepath)
.output()?;
if output.status.success() {
println!("✓ Script executed successfully!");
println!("Output: {}", String::from_utf8_lossy(&output.stdout));
} else {
println!("✗ Script failed to execute");
println!("Error: {}", String::from_utf8_lossy(&output.stderr));
}
} else {
println!("\n✗ File was not created");
}
Ok(())
}