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