use agentfs::{AgentFS, AgentFSOptions};
use serde::{Serialize, Deserialize};
use chrono::Utc;
#[derive(Serialize, Deserialize)]
struct Message {
role: String,
content: String,
timestamp: i64,
}
#[tokio::main]
async fn main() -> Result<(),
Box<dyn std::error::Error>> {
// Initialize AgentFS with persistent storage
let agent = AgentFS::open(
AgentFSOptions::with_id("my-agent")
).await?;
// Creates: .agentfs/my-agent.db
// Store agent configuration
agent.kv.set("agent:id", "assistant-001").await?;
agent.kv.set(
"agent:created",
Utc::now().to_rfc3339()
).await?;
// Create a message
let message = Message {
role: "user".to_string(),
content: "Hello, can you help me with a task?"
.to_string(),
timestamp: Utc::now().timestamp_millis(),
};
// Save message to filesystem
let filename = format!(
"/conversations/{}.json",
message.timestamp
);
let json = serde_json::to_string_pretty(
&message
)?;
agent.fs.write_file(
&filename,
json.as_bytes()
).await?;
// Track as tool call
agent.tools.record(
"save_message",
Utc::now().timestamp() as f64,
Utc::now().timestamp() as f64 + 0.1,
serde_json::json!({
"role": message.role,
"contentLength": message.content.len()
}),
serde_json::json!({
"saved": true,
"filename": filename
})
).await?;
// List saved conversations
let files = agent.fs.readdir("/conversations").await?;
println!("Saved conversations: {:?}", files);
Ok(())
}