Repository navigation
Expand file tree
/
Copy pathpersistence.rs
More file actions
69 lines (61 loc) · 2.1 KB
/
Copy pathpersistence.rs
File metadata and controls
69 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Save and restore agent conversation state.
//!
//! Demonstrates:
//! - Running a conversation with MockProvider
//! - Saving messages to JSON
//! - Restoring into a fresh agent
//! - Continuing the conversation from saved state
use yoagent::agent::Agent;
use yoagent::provider::MockProvider;
use yoagent::types::*;
#[tokio::main]
async fn main() {
// --- Phase 1: Initial conversation ---
let provider = MockProvider::text("The capital of France is Paris.");
let mut agent = Agent::from_provider(provider, yoagent::provider::ModelConfig::mock())
.with_system_prompt("You are a helpful assistant.");
println!("=== Phase 1: Initial conversation ===");
let mut rx = agent.prompt("What is the capital of France?").await;
while let Some(event) = rx.recv().await {
if let AgentEvent::MessageUpdate {
delta: StreamDelta::Text { delta },
..
} = event
{
print!("{}", delta);
}
}
println!("\n");
// Save state
let json = agent.save_messages().expect("Failed to save");
println!(
"Saved {} messages ({} bytes)\n",
agent.messages().len(),
json.len()
);
// --- Phase 2: Restore and continue ---
let provider2 = MockProvider::text("Paris is also known as the City of Light.");
let mut agent2 = Agent::from_provider(provider2, yoagent::provider::ModelConfig::mock())
.with_system_prompt("You are a helpful assistant.");
agent2.restore_messages(&json).expect("Failed to restore");
println!(
"=== Phase 2: Restored {} messages, continuing... ===",
agent2.messages().len()
);
let mut rx = agent2.prompt("Tell me more about it.").await;
while let Some(event) = rx.recv().await {
if let AgentEvent::MessageUpdate {
delta: StreamDelta::Text { delta },
..
} = event
{
print!("{}", delta);
}
}
println!("\n");
println!("Final message count: {}", agent2.messages().len());
println!(
"Messages match original + new: {}",
agent2.messages().len() == 4
);
}