Skip to main content

ToolInput

Trait ToolInput 

Source
pub trait ToolInput {
    // Required methods
    fn tool_name() -> &'static str;
    fn tool_description() -> &'static str;
    fn input_schema() -> ToolSchema;

    // Provided method
    fn definition() -> ToolDefinition { ... }
}
Expand description

Trait implemented by tool input types.

Most users should prefer #[derive(Tool)] instead of implementing this by hand.

Required Methods§

Provided Methods§

Source

fn definition() -> ToolDefinition

Examples found in repository?
examples/agent.rs (line 116)
112fn logged_tool<T>(verbose: bool, spinner_state: Arc<SpinnerState>) -> Tool
113where
114    T: ToolExecute,
115{
116    let definition = T::definition();
117    let name = definition.name.clone();
118
119    Tool::new_async(
120        definition.name,
121        definition.description,
122        definition.input_schema,
123        definition.output_schema,
124        move |input| {
125            let name = name.clone();
126            let spinner_state = Arc::clone(&spinner_state);
127            async move {
128                let _pause = SpinnerPause::new(spinner_state);
129                println!("{} {} {}", paint(YELLOW, "tool>"), paint(YELLOW, &name), paint(DIM, &input));
130
131                let parsed = serde_json::from_str::<T>(&input)
132                    .map_err(|error| format!("invalid tool input for {name}: {error}"))?;
133                let output = parsed.execute().await?;
134                let output = serde_json::to_string(&output)
135                    .map_err(|error| format!("invalid tool output for {name}: {error}"))?;
136
137                if verbose {
138                    println!("{} {} {}", paint(YELLOW, "tool<"), paint(YELLOW, &name), paint(DIM, &output));
139                }
140                Ok(output)
141            }
142        },
143    )
144}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§