Skip to main content

troupekit_kit/
error.rs

1use troupekit_agent::{AgentError, PersonaError};
2use troupekit_core::error::CoreError;
3use troupekit_core::event::PauseReason;
4use troupekit_core::ids::RunId;
5use troupekit_graph::{GraphError, WorkflowError};
6use troupekit_providers::CatalogError;
7use troupekit_tools::ToolError;
8
9/// Every failure of the kit: builder settings, catalog and key problems,
10/// and the wrapped engine errors, plus run outcomes that are not an answer.
11/// Messages name the agent and the fix; they never hold an API key.
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum KitError {
15    #[error("agent `{agent}`: {message}")]
16    Config { agent: String, message: String },
17    #[error("vendor `{vendor}` needs an API key: set {env} or call .api_key(..)")]
18    MissingKey { vendor: String, env: String },
19    #[error("agent `{agent}`: {source}; {hint}")]
20    Catalog {
21        agent: String,
22        source: CatalogError,
23        hint: String,
24    },
25    #[error(transparent)]
26    Persona(#[from] PersonaError),
27    #[error(transparent)]
28    Agent(#[from] AgentError),
29    #[error(transparent)]
30    Tool(#[from] ToolError),
31    #[error(transparent)]
32    Workflow(#[from] WorkflowError),
33    #[error(transparent)]
34    Graph(#[from] GraphError),
35    #[error(transparent)]
36    Store(#[from] CoreError),
37    #[cfg(feature = "knowledge")]
38    #[error(transparent)]
39    Context(#[from] troupekit_context::ContextError),
40    #[error(transparent)]
41    Route(troupekit_providers::route::RouteError),
42    #[cfg(feature = "local")]
43    #[error(transparent)]
44    Local(#[from] troupekit_local::LocalError),
45    #[error("run {run_id} failed: {reason}")]
46    Failed { run_id: RunId, reason: String },
47    #[error("run {run_id} failed: {reason}")]
48    HostFailed { run_id: RunId, reason: String },
49    #[error("run {run_id} paused: {reason:?}")]
50    Paused { run_id: RunId, reason: PauseReason },
51    #[error("run {run_id} was cancelled; resume it from its last checkpoint")]
52    Cancelled { run_id: RunId },
53    #[error("run {run_id}: gave up after {attempts} retries: {last_error}")]
54    Retries {
55        run_id: RunId,
56        attempts: u32,
57        last_error: String,
58    },
59    #[error("run {run_id}: {source}")]
60    InRun {
61        run_id: RunId,
62        source: Box<KitError>,
63    },
64}
65
66impl KitError {
67    pub(crate) fn config(agent: &str, message: impl Into<String>) -> Self {
68        KitError::Config {
69            agent: agent.to_string(),
70            message: message.into(),
71        }
72    }
73
74    /// Builds the error of run `run_id` that failed with `reason`.
75    /// Input: `host` is true when a host model failed the run in its own
76    /// callback, read from the run's typed state. Output: `HostFailed`
77    /// then, else `Failed`; both display the same message.
78    pub(crate) fn failed(run_id: RunId, reason: String, host: bool) -> Self {
79        if host {
80            KitError::HostFailed { run_id, reason }
81        } else {
82            KitError::Failed { run_id, reason }
83        }
84    }
85
86    /// Ties an execution failure to run `run_id`, so the caller can
87    /// resume or inspect it. Input: the run id and the failure.
88    /// Output: `InRun`, or `err` as is when it already names a run.
89    pub(crate) fn in_run(run_id: RunId, err: KitError) -> Self {
90        if err.run_id().is_some() {
91            return err;
92        }
93        KitError::InRun {
94            run_id,
95            source: Box::new(err),
96        }
97    }
98
99    /// Returns the run this error belongs to, for traces and callers.
100    /// Input: any error. Output: the run id of a failed, paused,
101    /// cancelled, retried or wrapped run; `None` for pre-run errors.
102    pub(crate) fn run_id(&self) -> Option<RunId> {
103        match self {
104            KitError::Failed { run_id, .. }
105            | KitError::HostFailed { run_id, .. }
106            | KitError::Paused { run_id, .. }
107            | KitError::Cancelled { run_id }
108            | KitError::Retries { run_id, .. }
109            | KitError::InRun { run_id, .. } => Some(*run_id),
110            _ => None,
111        }
112    }
113}