use crate::{
FINAL_OUTPUT_TOOL_NAME, RegisteredTool, ToolActionKind, ToolExecutionContext,
ToolExecutionError, ToolExecutionOutcome, ToolExecutionResult, ToolExecutor,
ToolExecutorFuture,
};
use merry_core::{CoreError, ErrorInfo, PendingToolCall, ToolInputSchema, ToolName, ToolSpec};
use schemars::JsonSchema;
use serde::{Serialize, de::DeserializeOwned};
use std::{fmt, future::Future, marker::PhantomData, sync::Arc};
use thiserror::Error;
#[derive(Clone)]
pub struct Tool {
spec: ToolSpec,
executor: Arc<dyn ToolExecutor>,
}
impl Tool {
pub fn spec_for<I>(
name: impl AsRef<str>,
description: impl AsRef<str>,
) -> Result<ToolSpec, ToolBuildError>
where
I: DeserializeOwned + JsonSchema,
{
let name = ToolName::new(name.as_ref())?;
if name.as_str() == FINAL_OUTPUT_TOOL_NAME {
return Err(ToolBuildError::ReservedName { name });
}
let input_schema = ToolInputSchema::new(schemars::schema_for!(I))?.require_object()?;
Ok(ToolSpec::new(name, description.as_ref(), input_schema)?)
}
pub fn new<I, O, F, Fut, E>(
name: impl AsRef<str>,
description: impl AsRef<str>,
handler: F,
) -> Result<Self, ToolBuildError>
where
I: DeserializeOwned + JsonSchema + Send + 'static,
O: Serialize + Send + 'static,
F: Fn(I) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, E>> + Send + 'static,
E: fmt::Display + Send + Sync + 'static,
{
let spec = Self::spec_for::<I>(name, description)?;
let executor = TypedToolExecutor {
handler,
input_marker: PhantomData,
future_marker: PhantomData,
error_marker: PhantomData,
};
Ok(Self {
spec,
executor: Arc::new(executor),
})
}
#[must_use]
pub fn spec(&self) -> &ToolSpec {
&self.spec
}
#[doc(hidden)]
#[must_use]
pub fn into_registered_tool(self) -> RegisteredTool {
RegisteredTool::new(self.spec, self.executor, ToolActionKind::TrustedExternal)
}
}
impl fmt::Debug for Tool {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Tool")
.field("spec", &self.spec)
.finish_non_exhaustive()
}
}
#[derive(Debug, Error)]
pub enum ToolBuildError {
#[error(transparent)]
Core(#[from] CoreError),
#[error("tool name {name} is reserved by the runtime")]
ReservedName {
name: ToolName,
},
}
impl From<ToolBuildError> for CoreError {
fn from(error: ToolBuildError) -> Self {
match error {
ToolBuildError::Core(source) => source,
ToolBuildError::ReservedName { name } => CoreError::InvalidIdentifier {
kind: "ToolName",
value: name.to_string(),
reason: "tool name is reserved",
},
}
}
}
struct TypedToolExecutor<I, O, F, Fut, E> {
handler: F,
input_marker: PhantomData<fn(I) -> O>,
future_marker: PhantomData<fn() -> Fut>,
error_marker: PhantomData<fn() -> E>,
}
impl<I, O, F, Fut, E> ToolExecutor for TypedToolExecutor<I, O, F, Fut, E>
where
I: DeserializeOwned + JsonSchema + Send + 'static,
O: Serialize + Send + 'static,
F: Fn(I) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, E>> + Send + 'static,
E: fmt::Display + Send + Sync + 'static,
{
fn execute<'a>(
&'a self,
call: PendingToolCall,
context: ToolExecutionContext,
) -> ToolExecutorFuture<'a> {
let input = match call.arguments().deserialize_as::<I>() {
Ok(input) => input,
Err(source) => {
return Box::pin(async move {
failed_tool_result(
format!("tool arguments could not be decoded: {source}"),
"tool_input_decode_failed",
"tool arguments did not match the declared input type",
)
});
}
};
let cancellation_token = context.cancellation_token().clone();
let handler_future = (self.handler)(input);
Box::pin(async move {
tokio::select! {
result = handler_future => match result {
Ok(output) => match serde_json::to_string(&output) {
Ok(content) => Ok(ToolExecutionOutcome::succeeded_json(content)),
Err(source) => failed_tool_result(
format!("tool output could not be serialized: {source}"),
"tool_output_serialization_failed",
"the tool returned a value that could not be serialized",
),
},
Err(error) => failed_tool_result(
error.to_string(),
"tool_handler_failed",
"the tool handler returned a domain error",
),
},
_ = cancellation_token.cancelled() => Err(ToolExecutionError::Cancelled),
}
})
}
}
fn failed_tool_result(
content: String,
code: &'static str,
diagnostic_message: &'static str,
) -> ToolExecutionResult {
let diagnostic = ErrorInfo::new(code, diagnostic_message).map_err(|source| {
ToolExecutionError::infrastructure(format!(
"failed to create tool failure diagnostic: {source}"
))
})?;
Ok(ToolExecutionOutcome::failed_text(content, diagnostic))
}