agent_with_file_tools/
agent_with_file_tools.rs

1//! # Example: Agent with File Tools
2//!
3//! This example demonstrates how to create an agent with file manipulation tools,
4//! including `FileSearchTool`, `FileReadTool`, `FileEditTool`, and `FileWriteTool`.
5//! It also shows how to use session memory to track the agent's state.
6
7use helios_engine::{Agent, Config, FileEditTool, FileReadTool, FileSearchTool, FileWriteTool};
8
9#[tokio::main]
10async fn main() -> helios_engine::Result<()> {
11    println!("šŸš€ Helios Engine - Agent with File Tools Example");
12    println!("=================================================\n");
13
14    // Load configuration
15    let config = Config::from_file("config.toml").unwrap_or_else(|_| {
16        println!("⚠ No config.toml found, using default configuration");
17        Config::new_default()
18    });
19
20    // Create an agent named "FileAssistant" and equip it with file tools.
21    let mut agent = Agent::builder("FileAssistant")
22        .config(config)
23        .system_prompt(
24            "You are a helpful file management assistant. You can search for files, \
25             read file contents, and edit files. Always confirm with the user before \
26             making changes to files. Keep track of important session information.",
27        )
28        .tool(Box::new(FileSearchTool))
29        .tool(Box::new(FileReadTool))
30        .tool(Box::new(FileEditTool))
31        .tool(Box::new(FileWriteTool))
32        .max_iterations(10)
33        .build()
34        .await?;
35
36    println!("āœ“ Agent created with file tools");
37    println!("āœ“ Available tools: file_search, file_read, file_edit, file_write\n");
38
39    // Set initial session memory for the agent.
40    agent.set_memory("session_start", chrono::Utc::now().to_rfc3339());
41    agent.set_memory(
42        "working_directory",
43        std::env::current_dir()?.display().to_string(),
44    );
45    agent.set_memory("tasks_completed", "0");
46
47    // --- Example 1: Search for Rust files ---
48    println!("Example 1: Searching for Rust files");
49    println!("====================================\n");
50
51    let response = agent
52        .chat("Find all Rust source files in the src directory")
53        .await?;
54    println!("Agent: {}\n", response);
55
56    // Update session memory after the task.
57    agent.increment_tasks_completed();
58    agent.set_memory("last_task", "file_search");
59
60    // --- Example 2: Read a specific file ---
61    println!("\nExample 2: Reading file contents");
62    println!("==================================\n");
63
64    let response = agent
65        .chat("Read the contents of src/lib.rs and give me a summary")
66        .await?;
67    println!("Agent: {}\n", response);
68
69    // Update session memory after the task.
70    agent.increment_tasks_completed();
71    agent.set_memory("last_task", "file_read");
72
73    // --- Example 3: Show session summary ---
74    println!("\nExample 3: Session Summary");
75    println!("==========================\n");
76
77    println!("{}", agent.get_session_summary());
78
79    // --- Example 4: Check session memory ---
80    println!("\nExample 4: Checking Session Memory");
81    println!("===================================\n");
82
83    println!(
84        "Working directory: {}",
85        agent
86            .get_memory("working_directory")
87            .unwrap_or(&"unknown".to_string())
88    );
89    println!(
90        "Tasks completed: {}",
91        agent
92            .get_memory("tasks_completed")
93            .unwrap_or(&"0".to_string())
94    );
95    println!(
96        "Last task: {}",
97        agent.get_memory("last_task").unwrap_or(&"none".to_string())
98    );
99
100    println!("\nāœ… Example completed successfully!");
101    println!("\nšŸ’” Key Features Demonstrated:");
102    println!("  • File search with pattern matching and content search");
103    println!("  • File reading with line range support");
104    println!("  • File editing with find/replace functionality");
105    println!("  • Session memory for tracking agent state");
106    println!("  • Streaming responses (works with both local and remote models)");
107
108    Ok(())
109}