use std::collections::BTreeMap;
use runifold_core::RetrySafety;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub enum ToolErrorKind {
NotFound,
InvalidInput,
CapabilityDenied,
Cancelled,
DeadlineExceeded,
Execution,
InvalidOutput,
}
#[derive(Clone, Debug, Deserialize, Error, PartialEq, Serialize)]
#[error("{kind:?}: {message}")]
pub struct ToolError {
pub kind: ToolErrorKind,
pub message: String,
pub retry_safety: RetrySafety,
pub metadata: BTreeMap<String, Value>,
}
impl ToolError {
pub fn local(kind: ToolErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
retry_safety: RetrySafety::Unknown,
metadata: BTreeMap::new(),
}
}
}
pub trait IntoToolError {
fn into_tool_error(self) -> ToolError;
}
impl IntoToolError for ToolError {
fn into_tool_error(self) -> ToolError {
self
}
}
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum ToolRegistrationError {
#[error("tool name cannot be empty")]
EmptyName,
#[error("tool `{0}` is already registered")]
DuplicateName(String),
#[error("tool `{tool}` has an invalid {direction} schema: {message}")]
InvalidSchema {
tool: String,
direction: &'static str,
message: String,
},
}