oxide-agent 0.1.0

Type-safe, high-performance Rust crate for building agentic systems on Ollama
Documentation
//! Stateful multi-turn conversation with automatic context compression.
//!
//! This example demonstrates:
//!   - Setting a system prompt
//!   - Sending multiple turns and reading accumulated history
//!   - Configuring `TruncateOldest` vs `Summarize` compression strategies
//!
//! Run with:
//!   cargo run --example session

use std::sync::Arc;

use oxide_agent::client::HttpOllamaClient;
use oxide_agent::session::{CompressionStrategy, Session, SessionConfig};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let client = Arc::new(HttpOllamaClient::new("http://localhost:11434"));

    // ── Strategy 1: truncate oldest messages when the context fills up ────────
    let truncate_config = SessionConfig {
        max_tokens: 8_000,
        compression_threshold: 0.80,
        compression_strategy: CompressionStrategy::TruncateOldest,
    };

    let mut session = Session::new(Arc::clone(&client), "llama3", truncate_config);
    session.set_system_prompt("You are a helpful Rust expert. Keep answers concise.");

    let r1 = session.ask("What is a lifetime in Rust?").await?;
    println!("Turn 1: {r1}\n");

    let r2 = session.ask("Can you show me a code example?").await?;
    println!("Turn 2: {r2}\n");

    println!(
        "History: {} messages, ~{} tokens\n",
        session.history().len(),
        session.estimated_tokens()
    );

    // ── Strategy 2: summarize oldest history via a (potentially smaller) model ─
    let summarize_config = SessionConfig {
        max_tokens: 32_000,
        compression_threshold: 0.80,
        compression_strategy: CompressionStrategy::Summarize {
            // Can point at a different, faster model for summarisation.
            model: "llama3".into(),
        },
    };

    let mut long_session = Session::new(client, "llama3", summarize_config);
    long_session.set_system_prompt("You are a Rust compiler assistant.");

    // Simulate a long conversation that eventually triggers summarisation.
    for i in 1..=5 {
        let reply = long_session.ask(format!("Question {i}: explain concept {i}")).await?;
        println!("Long session turn {i}: {}", &reply[..reply.len().min(80)]);
    }

    Ok(())
}