1use agentfs::{AgentFS, FileSystem, KvStore, ToolRecorder};
12use agentsql::SqlBackend;
13
14#[tokio::main]
15async fn main() -> Result<(), Box<dyn std::error::Error>> {
16 println!("=== AgentFS Basic Example (SQLite) ===\n");
17
18 println!("1. Initializing SQLite backend...");
20 let backend = SqlBackend::sqlite("agent_example.db").await?;
21 let agent_fs = AgentFS::new(Box::new(backend), "demo-agent", "/agent").await?;
22 println!(" ✓ AgentFS initialized with agent_id: {}\n", agent_fs.agent_id());
23
24 println!("2. File Operations:");
26
27 agent_fs.fs.mkdir("/output").await?;
29 println!(" ✓ Created directory: /output");
30
31 let report_content = b"# Agent Report\n\nThis is a test report generated by the agent.";
33 agent_fs.fs.write_file("/output/report.md", report_content).await?;
34 println!(" ✓ Wrote file: /output/report.md");
35
36 let read_content = agent_fs.fs.read_file("/output/report.md").await?.unwrap();
38 println!(" ✓ Read file: /output/report.md ({} bytes)", read_content.len());
39
40 let entries = agent_fs.fs.readdir("/output").await?.unwrap();
42 println!(" ✓ Directory /output contains: {:?}\n", entries);
43
44 println!("3. Key-Value Store Operations:");
46
47 agent_fs.kv.set("session:id", b"abc-123-def").await?;
49 agent_fs.kv.set("session:user", b"alice").await?;
50 agent_fs.kv.set("config:theme", b"dark").await?;
51 println!(" ✓ Stored 3 key-value pairs");
52
53 let session_id = agent_fs.kv.get("session:id").await?.unwrap();
55 println!(" ✓ Retrieved session:id = {}", String::from_utf8_lossy(&session_id));
56
57 let session_keys = agent_fs.kv.scan("session:").await?;
59 println!(" ✓ Keys with prefix 'session:': {:?}\n", session_keys);
60
61 println!("4. Tool Call Recording:");
63
64 let params = serde_json::json!({
66 "query": "What is the weather in San Francisco?",
67 "location": "San Francisco, CA"
68 });
69 let call_id = agent_fs.tools.start("weather_api", Some(params)).await?;
70 println!(" ✓ Started tool call (ID: {})", call_id);
71
72 tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
74
75 let result = serde_json::json!({
77 "temperature": 65,
78 "condition": "Sunny",
79 "humidity": 60
80 });
81 agent_fs.tools.success(call_id, Some(result)).await?;
82 println!(" ✓ Marked tool call as successful");
83
84 let tool_call = agent_fs.tools.get(call_id).await?.unwrap();
86 println!(" ✓ Tool call status: {:?}", tool_call.status);
87 println!(" ✓ Duration: {} ms\n", tool_call.duration_ms.unwrap_or(0));
88
89 println!("5. Single-Shot Tool Recording:");
91
92 let start = std::time::SystemTime::now()
93 .duration_since(std::time::UNIX_EPOCH)?
94 .as_secs() as i64;
95
96 tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
97
98 let end = std::time::SystemTime::now()
99 .duration_since(std::time::UNIX_EPOCH)?
100 .as_secs() as i64;
101
102 let db_call_id = agent_fs.tools.record(
103 "database_query",
104 start,
105 end,
106 Some(serde_json::json!({"table": "users", "limit": 10})),
107 Some(serde_json::json!({"rows": 10})),
108 None,
109 ).await?;
110 println!(" ✓ Recorded database_query tool call (ID: {})\n", db_call_id);
111
112 println!("6. Tool Call Statistics:");
114
115 for i in 0..3 {
117 let id = agent_fs.tools.start("api_request", None).await?;
118 tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
119 if i < 2 {
120 agent_fs.tools.success(id, None).await?;
121 } else {
122 agent_fs.tools.error(id, "Timeout").await?;
123 }
124 }
125
126 let stats = agent_fs.tools.stats_for("api_request").await?.unwrap();
127 println!(" Tool: {}", stats.name);
128 println!(" Total calls: {}", stats.total_calls);
129 println!(" Successful: {}", stats.successful);
130 println!(" Failed: {}", stats.failed);
131 println!(" Avg duration: {:.2} ms\n", stats.avg_duration_ms);
132
133 println!("=== Example Complete ===");
134 println!("\nDatabase file created: agent_example.db");
135 println!("You can inspect it with: sqlite3 agent_example.db");
136
137 Ok(())
138}