Skip to main content

tiny_loop/
tool.rs

1mod args;
2mod closure;
3mod executor;
4mod fs;
5mod utils;
6mod web;
7
8use crate::types::{Message, ToolCall};
9use async_trait::async_trait;
10use futures::future::join_all;
11
12pub use args::*;
13pub(crate) use closure::*;
14pub use executor::*;
15pub use fs::*;
16pub use tiny_loop_macros::tool;
17pub use web::*;
18
19/// A trait for tools that can be called with JSON string arguments.
20///
21/// Users must provide the `call` method. The framework auto-provides `call_batch` to run tools in parallel.
22/// At runtime, different tool executors may call `call` or `call_batch` in different ways.
23/// Users can override `call_batch` to customize this behavior.
24#[async_trait]
25pub trait Tool {
26    /// Calls the tool with JSON arguments and returns the result.
27    async fn call(&self, args: String) -> String;
28
29    /// Executes multiple tool calls in parallel. Override to customize execution behavior.
30    async fn call_batch(&self, args: Vec<ToolCall>) -> Vec<Message> {
31        join_all(
32            args.into_iter()
33                .map(async |call| Message::Tool {
34                    tool_call_id: call.id,
35                    content: self.call(call.function.arguments).await,
36                })
37                .collect::<Vec<_>>(),
38        )
39        .await
40    }
41}