agent_with_file_tools/
agent_with_file_tools.rs1use 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 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 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 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 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 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 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 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 println!("\nExample 3: Session Summary");
79 println!("==========================\n");
80
81 println!("{}", agent.get_session_summary());
82
83 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}