use std::{error, fmt};
use futures_util::future::BoxFuture;
use serde::Serialize;
use serde_json::{Map, Value};
pub trait Host {
fn tools(&self) -> Vec<Tool>;
fn call(&self, call: &Call) -> BoxFuture<'static, Result<Value, Error>>;
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Tool {
pub id: String,
pub description: String,
pub input_schema: Value,
}
impl Tool {
pub fn new(id: impl Into<String>, description: impl Into<String>, input_schema: Value) -> Self {
Self {
id: id.into(),
description: description.into(),
input_schema,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Call {
pub tool_id: String,
pub args: Map<String, Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) truncated_args: Option<String>,
}
impl Call {
pub fn new(tool_id: impl Into<String>, args: Map<String, Value>) -> Self {
Self {
tool_id: tool_id.into(),
args,
truncated_args: None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Error {
Refused(String),
Failed(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Refused(reason) => f.write_str(reason),
Self::Failed(message) => f.write_str(message),
}
}
}
impl error::Error for Error {}