#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OutputTrust {
Trusted,
Untrusted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ToolOutput {
text: String,
trust: OutputTrust,
}
impl ToolOutput {
#[must_use]
pub fn trusted(text: impl Into<String>) -> ToolOutput {
ToolOutput {
text: text.into(),
trust: OutputTrust::Trusted,
}
}
#[must_use]
pub fn untrusted(text: impl Into<String>) -> ToolOutput {
ToolOutput {
text: text.into(),
trust: OutputTrust::Untrusted,
}
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn trust(&self) -> OutputTrust {
self.trust
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ToolErrorKind {
InvalidArguments,
Backend,
Transport,
Cancelled,
Other,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct ToolError {
kind: ToolErrorKind,
message: String,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl ToolError {
#[must_use]
pub fn message(text: impl Into<String>) -> ToolError {
ToolError {
kind: ToolErrorKind::Other,
message: text.into(),
source: None,
}
}
#[must_use]
pub fn with_source(
text: impl Into<String>,
src: impl std::error::Error + Send + Sync + 'static,
) -> ToolError {
ToolError {
kind: ToolErrorKind::Backend,
message: text.into(),
source: Some(Box::new(src)),
}
}
#[must_use]
pub fn with_kind(mut self, kind: ToolErrorKind) -> ToolError {
self.kind = kind;
self
}
#[must_use]
pub fn kind(&self) -> ToolErrorKind {
self.kind
}
#[must_use]
pub fn is_cancelled(&self) -> bool {
matches!(self.kind, ToolErrorKind::Cancelled)
}
#[must_use]
pub fn is_retryable(&self) -> bool {
matches!(self.kind, ToolErrorKind::Transport)
}
}
impl std::fmt::Display for ToolError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.message)
}
}
impl std::error::Error for ToolError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_ref()
.map(|boxed| boxed.as_ref() as &(dyn std::error::Error + 'static))
}
}