1use std::collections::BTreeMap;
2
3use runifold_core::RetrySafety;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6use thiserror::Error;
7
8#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
10#[non_exhaustive]
11pub enum ToolErrorKind {
12 NotFound,
14 InvalidInput,
16 CapabilityDenied,
18 Cancelled,
20 DeadlineExceeded,
22 Execution,
24 InvalidOutput,
26}
27
28#[derive(Clone, Debug, Deserialize, Error, PartialEq, Serialize)]
30#[error("{kind:?}: {message}")]
31pub struct ToolError {
32 pub kind: ToolErrorKind,
34 pub message: String,
36 pub retry_safety: RetrySafety,
38 pub metadata: BTreeMap<String, Value>,
40}
41
42impl ToolError {
43 pub fn local(kind: ToolErrorKind, message: impl Into<String>) -> Self {
45 Self {
46 kind,
47 message: message.into(),
48 retry_safety: RetrySafety::Unknown,
49 metadata: BTreeMap::new(),
50 }
51 }
52}
53
54pub trait IntoToolError {
59 fn into_tool_error(self) -> ToolError;
61}
62
63impl IntoToolError for ToolError {
64 fn into_tool_error(self) -> ToolError {
65 self
66 }
67}
68
69#[derive(Clone, Debug, Eq, Error, PartialEq)]
71#[non_exhaustive]
72pub enum ToolRegistrationError {
73 #[error("tool name cannot be empty")]
75 EmptyName,
76 #[error("tool `{0}` is already registered")]
78 DuplicateName(String),
79}