Skip to main content

runifold_tool/
tool.rs

1use std::{future::Future, pin::Pin};
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::{ToolContext, ToolDescriptor, ToolError};
7
8/// A boxed, sendable future returned by a tool.
9pub type ToolFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
10
11/// Successful canonical tool output.
12#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
13pub struct ToolOutput {
14    /// Structured output value.
15    pub value: Value,
16    /// Whether the value is safe to expose verbatim to a model.
17    pub model_visible: bool,
18}
19
20impl ToolOutput {
21    /// Creates model-visible output.
22    pub const fn model_visible(value: Value) -> Self {
23        Self {
24            value,
25            model_visible: true,
26        }
27    }
28}
29
30/// Object-safe execution boundary implemented by tools.
31pub trait Tool: Send + Sync {
32    /// Returns the tool's immutable semantic contract.
33    fn descriptor(&self) -> &ToolDescriptor;
34
35    /// Executes one invocation.
36    fn invoke(
37        &self,
38        input: Value,
39        context: ToolContext,
40    ) -> ToolFuture<'_, Result<ToolOutput, ToolError>>;
41}