tool_builder_demo/
tool_builder_demo.rs

1//! # Example: Tool Builder Demo
2//!
3//! This example demonstrates the SIMPLEST way to create custom tools.
4//! Just define your function and pass it directly with ftool!
5//! The system automatically infers types from your function signature!
6
7use helios_engine::{Agent, Config, ToolBuilder};
8
9// Your regular Rust functions - nothing special needed!
10// The system automatically infers the types!
11
12fn adder(x: i32, y: i32) -> i32 {
13    x + y
14}
15
16fn multiplier(a: i32, b: i32) -> i32 {
17    a * b
18}
19
20fn calculate_area(length: f64, width: f64) -> f64 {
21    length * width
22}
23
24fn calculate_volume(width: f64, height: f64, depth: f64) -> f64 {
25    width * height * depth
26}
27
28fn calculate_bmi(weight_kg: f64, height_m: f64) -> f64 {
29    weight_kg / (height_m * height_m)
30}
31
32// Mixed types - String and bool!
33fn greet(name: String, formal: bool) -> String {
34    if formal {
35        format!("Good day, {}. It's a pleasure to meet you.", name)
36    } else {
37        format!("Hey {}! What's up?", name)
38    }
39}
40
41// Different numeric types
42fn calculate_discount(price: f64, percent: i32) -> String {
43    let discount = price * (percent as f64 / 100.0);
44    format!(
45        "Discount: ${:.2}, Final price: ${:.2}",
46        discount,
47        price - discount
48    )
49}
50
51#[tokio::main]
52async fn main() -> helios_engine::Result<()> {
53    let config = Config::from_file("config.toml")?;
54
55    println!("=== Tool Builder Demo ===\n");
56    println!("Creating tools with the ultra-simple ftool API!");
57    println!("The system automatically infers types from your function signature!\n");
58
59    // Example 1: Integer parameters (i32, i32) - types inferred automatically!
60    let add_tool = ToolBuilder::new("add")
61        .description("Add two integers")
62        .parameters("x:i32:First number, y:i32:Second number")
63        .ftool(adder) // Automatically knows these are i32!
64        .build();
65
66    // Example 2: Integer multiplication (i32, i32)
67    let multiply_tool = ToolBuilder::new("multiply")
68        .description("Multiply two integers")
69        .parameters("a:i32:First number, b:i32:Second number")
70        .ftool(multiplier)
71        .build();
72
73    // Example 3: Float parameters (f64, f64) - automatically inferred!
74    let area_tool = ToolBuilder::new("calculate_area")
75        .description("Calculate the area of a rectangle")
76        .parameters("length:f64:Length in meters, width:f64:Width in meters")
77        .ftool(calculate_area) // Same ftool method, but knows these are f64!
78        .build();
79
80    // Example 4: Three float parameters (f64, f64, f64)
81    let volume_tool = ToolBuilder::new("calculate_volume")
82        .description("Calculate the volume of a box")
83        .parameters(
84            "width:f64:Width in meters, height:f64:Height in meters, depth:f64:Depth in meters",
85        )
86        .ftool3(calculate_volume)
87        .build();
88
89    // Example 5: BMI calculator with floats
90    let bmi_tool = ToolBuilder::new("calculate_bmi")
91        .description("Calculate Body Mass Index")
92        .parameters("weight_kg:f64:Weight in kilograms, height_m:f64:Height in meters")
93        .ftool(calculate_bmi)
94        .build();
95
96    // Example 6: Mixed types - String and bool!
97    let greet_tool = ToolBuilder::new("greet")
98        .description("Greet someone by name")
99        .parameters("name:string:Person's name, formal:bool:Use formal greeting")
100        .ftool(greet) // Automatically knows: String, bool!
101        .build();
102
103    // Example 7: Mixed numeric types - f64 and i32
104    let discount_tool = ToolBuilder::new("calculate_discount")
105        .description("Calculate discount on a price")
106        .parameters("price:f64:Original price, percent:i32:Discount percentage")
107        .ftool(calculate_discount) // Automatically knows: f64, i32!
108        .build();
109
110    // Create an agent with all the tools
111    let mut agent = Agent::builder("ToolDemo")
112        .config(config)
113        .system_prompt(
114            "You are a helpful assistant with access to various calculation and utility tools. \
115             Use them to help answer questions accurately.",
116        )
117        .tool(add_tool)
118        .tool(multiply_tool)
119        .tool(area_tool)
120        .tool(volume_tool)
121        .tool(bmi_tool)
122        .tool(greet_tool)
123        .tool(discount_tool)
124        .build()
125        .await?;
126
127    println!("Created 7 tools with minimal code!\n");
128    println!("All types automatically inferred from function signatures!\n");
129    println!("===========================================\n");
130
131    // Test the tools
132    println!("Test 1: Integer addition");
133    let response = agent.chat("What is 42 plus 17?").await?;
134    println!("Agent: {}\n", response);
135
136    println!("Test 2: Integer multiplication");
137    let response = agent.chat("What is 12 times 8?").await?;
138    println!("Agent: {}\n", response);
139
140    println!("Test 3: Calculate area");
141    let response = agent
142        .chat("What is the area of a rectangle that is 5 meters long and 3 meters wide?")
143        .await?;
144    println!("Agent: {}\n", response);
145
146    println!("Test 4: Calculate volume");
147    let response = agent
148        .chat("What's the volume of a box that is 2m wide, 3m high, and 1.5m deep?")
149        .await?;
150    println!("Agent: {}\n", response);
151
152    println!("Test 5: Calculate BMI");
153    let response = agent
154        .chat("Calculate BMI for someone weighing 70 kg and 1.75 meters tall")
155        .await?;
156    println!("Agent: {}\n", response);
157
158    println!("Test 6: Greeting with String and bool");
159    let response = agent.chat("Greet Bob in a casual way").await?;
160    println!("Agent: {}\n", response);
161
162    println!("Test 7: Discount calculation with mixed types");
163    let response = agent
164        .chat("What's the discount on a $100 item with 20% off?")
165        .await?;
166    println!("Agent: {}\n", response);
167
168    println!("\n===========================================");
169    println!("✨ That's how easy it is to create tools!");
170    println!("   Just define your function and the system automatically:");
171    println!("   • Infers all parameter types from your function signature");
172    println!("   • Extracts values from JSON in the right order");
173    println!("   • Handles i32, i64, u32, u64, f32, f64, bool, String");
174    println!(
175        "   • Works with .ftool() for 2 params, .ftool3() for 3 params, .ftool4() for 4 params"
176    );
177
178    Ok(())
179}