troupekit-ffi-core 0.4.0

Binding-neutral Troupekit API: plain Rust handles, records and host traits that the UniFFI and PyO3 adapters translate.
Documentation
/// Stable numeric code of every failure the neutral API reports.
/// The numbers are fixed forever: a binding may match on them, store
/// them or send them over the wire. New codes only ever get new numbers.
#[non_exhaustive]
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorCode {
    Config = 1,
    MissingKey = 2,
    Catalog = 3,
    Persona = 4,
    Agent = 5,
    Tool = 6,
    Workflow = 7,
    Graph = 8,
    Store = 9,
    Context = 10,
    Failed = 11,
    Paused = 12,
    Retries = 13,
    Cancelled = 14,
    HostCallback = 15,
    InvalidArgument = 16,
    Unsupported = 17,
    Panic = 18,
    Internal = 99,
}

/// The one error type of the neutral API: a stable `code`, a `message`
/// that never holds a key or secret, and the `run_id` text when the
/// failure belongs to a run that can be resumed or inspected.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
pub struct FfiError {
    pub code: ErrorCode,
    pub message: String,
    pub run_id: Option<String>,
}

impl FfiError {
    /// Builds an error with `code` and `message` and no run id.
    /// Input: the code and a message free of secrets. Output: the error.
    /// The message must not be blank.
    pub fn new(code: ErrorCode, message: impl Into<String>) -> FfiError {
        let message = message.into();
        assert!(!message.trim().is_empty());
        FfiError {
            code,
            message,
            run_id: None,
        }
    }
}

impl From<troupekit_kit::KitError> for FfiError {
    /// Maps every kit error to its stable code, with the kit's message
    /// (which never holds a key) and the run id for run outcomes. An
    /// `InRun` failure takes its inner error's code and its own run id.
    /// A run failed by a host model (`HostFailed`, set from the typed
    /// error kind, never the message text) is `HostCallback`. A kit variant
    /// added later maps to `Internal` until it gets its own code.
    fn from(err: troupekit_kit::KitError) -> FfiError {
        use troupekit_kit::KitError as K;
        let message = err.to_string();
        if let K::InRun { run_id, source } = err {
            let inner = FfiError::from(*source);
            return FfiError {
                code: inner.code,
                message,
                run_id: Some(run_id.to_string()),
            };
        }
        let (code, run_id) = match &err {
            K::Config { .. } => (ErrorCode::Config, None),
            K::MissingKey { .. } => (ErrorCode::MissingKey, None),
            K::Catalog { .. } => (ErrorCode::Catalog, None),
            K::Persona(_) => (ErrorCode::Persona, None),
            K::Agent(_) => (ErrorCode::Agent, None),
            K::Tool(_) => (ErrorCode::Tool, None),
            K::Workflow(_) => (ErrorCode::Workflow, None),
            K::Graph(_) => (ErrorCode::Graph, None),
            K::Store(_) => (ErrorCode::Store, None),
            #[cfg(feature = "knowledge")]
            K::Context(_) => (ErrorCode::Context, None),
            K::HostFailed { run_id, .. } => (ErrorCode::HostCallback, Some(run_id.to_string())),
            K::Failed { run_id, .. } => (ErrorCode::Failed, Some(run_id.to_string())),
            K::Paused { run_id, .. } => (ErrorCode::Paused, Some(run_id.to_string())),
            K::Retries { run_id, .. } => (ErrorCode::Retries, Some(run_id.to_string())),
            K::Cancelled { run_id } => (ErrorCode::Cancelled, Some(run_id.to_string())),
            #[allow(unreachable_patterns)]
            _ => (ErrorCode::Internal, None),
        };
        assert!(!message.is_empty());
        FfiError {
            code,
            message,
            run_id,
        }
    }
}