troupekit-kit 0.4.0

Ergonomic API for the Troupekit LLM agent framework: one builder or one macro per agent, tool and graph.
Documentation
use crate::error::KitError;
use std::fmt;
#[cfg(feature = "sqlite")]
use std::path::PathBuf;
use std::sync::Arc;
use troupekit_core::store::{CheckpointStore, UsageLedger};

pub(crate) type Opened = (
    Option<Arc<dyn CheckpointStore>>,
    Option<Arc<dyn UsageLedger>>,
);

/// Where an agent or graph keeps run checkpoints.
/// `memory()` (the default) uses a fresh in-memory store per run, so
/// nothing grows; `sqlite(path)` and `custom(store)` keep runs for `resume`.
#[derive(Clone)]
pub struct Storage(Kind);

#[derive(Clone)]
enum Kind {
    Memory,
    #[cfg(feature = "sqlite")]
    Sqlite(PathBuf),
    Custom(Arc<dyn CheckpointStore>),
}

impl Storage {
    #[cfg(feature = "knowledge")]
    pub(crate) fn sqlite_path(&self) -> Option<&std::path::Path> {
        match &self.0 {
            Kind::Sqlite(path) => Some(path),
            _ => None,
        }
    }

    /// Keeps each run's checkpoints in memory, dropped when the run ends.
    /// Runs cannot be resumed. This is the default.
    /// Costs nothing at `build`.
    pub fn memory() -> Self {
        Storage(Kind::Memory)
    }

    /// Keeps checkpoints in the SQLite file at `path`, opened at `build`.
    /// Enables `resume(run_id)`, also from a later process. A route's daily
    /// cloud spend is kept there too. Needs the `sqlite` feature, on by default.
    #[cfg(feature = "sqlite")]
    pub fn sqlite(path: impl Into<PathBuf>) -> Self {
        Storage(Kind::Sqlite(path.into()))
    }

    /// Keeps checkpoints in `store`, shared by every run.
    /// Enables `resume(run_id)`.
    /// Use it for a store the host already opened.
    pub fn custom(store: Arc<dyn CheckpointStore>) -> Self {
        Storage(Kind::Custom(store))
    }

    /// Opens the checkpoint store and, for `sqlite`, the same file as the
    /// durable default ledger for a route's daily cloud spend. Output: the
    /// store and the ledger; `memory` and `custom` give no ledger.
    pub(crate) async fn open(&self) -> Result<Opened, KitError> {
        match &self.0 {
            Kind::Memory => Ok((None, None)),
            #[cfg(feature = "sqlite")]
            Kind::Sqlite(path) => {
                let store = Arc::new(troupekit_store_sqlite::SqliteStore::open(path).await?);
                Ok((Some(store.clone()), Some(store)))
            }
            Kind::Custom(store) => Ok((Some(store.clone()), None)),
        }
    }
}

impl Default for Storage {
    fn default() -> Self {
        Storage::memory()
    }
}

impl fmt::Debug for Storage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0 {
            Kind::Memory => f.write_str("Storage::memory()"),
            #[cfg(feature = "sqlite")]
            Kind::Sqlite(path) => write!(f, "Storage::sqlite({path:?})"),
            Kind::Custom(_) => f.write_str("Storage::custom(..)"),
        }
    }
}