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    let tasks = agent.get_memory("tasks_completed")
56        .and_then(|v| v.parse::<u32>().ok())
57        .unwrap_or(0);
58    agent.set_memory("tasks_completed", (tasks + 1).to_string());
59    agent.set_memory("last_task", "file_search");
60
61    // Example 2: Read a specific file
62    println!("\nExample 2: Reading file contents");
63    println!("==================================\n");
64
65    let response = agent
66        .chat("Read the contents of src/lib.rs and give me a summary")
67        .await?;
68    println!("Agent: {}\n", response);
69
70    // Update session memory
71    let tasks = agent.get_memory("tasks_completed")
72        .and_then(|v| v.parse::<u32>().ok())
73        .unwrap_or(0);
74    agent.set_memory("tasks_completed", (tasks + 1).to_string());
75    agent.set_memory("last_task", "file_read");
76
77    // Example 3: Show session summary
78    println!("\nExample 3: Session Summary");
79    println!("==========================\n");
80
81    println!("{}", agent.get_session_summary());
82
83    // Example 4: Check session memory
84    println!("\nExample 4: Checking Session Memory");
85    println!("===================================\n");
86
87    println!("Working directory: {}", agent.get_memory("working_directory").unwrap_or(&"unknown".to_string()));
88    println!("Tasks completed: {}", agent.get_memory("tasks_completed").unwrap_or(&"0".to_string()));
89    println!("Last task: {}", agent.get_memory("last_task").unwrap_or(&"none".to_string()));
90
91    println!("\nāœ… Example completed successfully!");
92    println!("\nšŸ’” Key Features Demonstrated:");
93    println!("  • File search with pattern matching and content search");
94    println!("  • File reading with line range support");
95    println!("  • File editing with find/replace functionality");
96    println!("  • Session memory for tracking agent state");
97    println!("  • Streaming responses (works with both local and remote models)");
98
99    Ok(())
100}