agent_with_file_tools/
agent_with_file_tools.rs

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