yoi/crates/pod/examples/pod_cli.rs

83 lines
2.2 KiB
Rust

//! Minimal example: Pod running a single prompt with persistence.
//!
//! Demonstrates the core insomnia abstraction — a TOML manifest drives
//! provider selection, model config, and system prompt, while FsStore
//! persists the session to disk automatically.
//!
//! ## Usage
//!
//! ```bash
//! echo "ANTHROPIC_API_KEY=your-key" > .env
//! cargo run -p pod --example pod_cli
//! ```
use pod::{Pod, PodManifest, PodRunResult};
use session_store::FsStore;
fn manifest_toml(pwd: &std::path::Path) -> String {
let pwd = pwd.display();
format!(
r#"
[pod]
name = "hello-pod"
pwd = "{pwd}"
[provider]
kind = "anthropic"
model = "claude-sonnet-4-20250514"
[worker]
system_prompt = "You are a concise assistant. Reply in one or two sentences."
max_tokens = 256
[[scope.allow]]
target = "{pwd}"
permission = "write"
"#
)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv::dotenv().ok();
// 1. Build a manifest rooted at the current working directory.
// All paths in a manifest must be absolute — see the pod-factory ticket.
let pwd = std::env::current_dir()?;
let toml = manifest_toml(&pwd);
// 2. Create a persistent store (temp dir for demo)
let tmp = tempfile::tempdir()?;
let store = FsStore::new(tmp.path()).await?;
// 3. Build the Pod from the single-layer manifest TOML
let mut pod = Pod::from_manifest_toml(&toml, store).await?;
let manifest: &PodManifest = pod.manifest();
println!("Pod: {}", manifest.pod.name);
println!("Session: {}", pod.session_id());
// 4. Run a prompt
let result = pod.run("What is the capital of France?").await?;
match result {
PodRunResult::Finished => println!("(finished)"),
PodRunResult::Paused => println!("(paused)"),
PodRunResult::LimitReached => println!("(turn limit reached)"),
}
// 5. Extract the assistant's reply from history
let history = pod.worker().history();
if let Some(text) = history
.iter()
.rev()
.find(|item| item.is_assistant_message())
.and_then(|item| item.as_text())
{
println!("\nAssistant: {text}");
}
// 6. Session ID for potential restore
println!("\nSession ID: {}", pod.session_id());
Ok(())
}