yoi/crates/pod/examples/pod_cli.rs
Hare 22f5d02385 update: SessionId / SessionStart / SessionOrigin 等を Segment 系名称へ
- Type/Function/Variantを Segment* 系へ統一
  - SessionId/SessionStart/SessionOrigin/SessionStartState/SessionState/SessionLogSink/SessionLockInfo
  - new_session_id / session_id / create_session* / list_sessions / lookup_session / update_session / find_by_session
  - protocol Event::SessionRotated → SegmentRotated、CompactDone.new_session_id → new_segment_id
- Module: session_log → segment_log / session → segment (file mv 含む)
  pod 側の session_log_sink → segment_log_sink も同様
- crate 名 (session-store)、CLI flag (--session)、ResumeWithSession (CLI tied) は据え置き
- session-tests/session_metrics_test 等の Store impl も追従
2026-05-20 05:06:04 +09:00

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}"
[model]
scheme = "anthropic"
model_id = "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())?;
// 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.segment_id());
// 4. Run a prompt
let result = pod.run_text("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.segment_id());
Ok(())
}