use descry_tool_core::{
tower::{tool_service, ToolRequest},
ToolContext, ToolError,
};
use descry_tool_macros::tool;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tower::Service;
#[derive(Deserialize, JsonSchema)]
struct ProcessParams {
input: String,
}
#[derive(Serialize, JsonSchema)]
struct ProcessOutput {
output: String,
length: usize,
}
#[tool(
name = "process",
description = "Process a string and return its length"
)]
async fn process(_ctx: Arc<ToolContext>, params: ProcessParams) -> Result<ProcessOutput, ToolError> {
tokio::time::sleep(Duration::from_millis(10)).await;
Ok(ProcessOutput {
output: params.input.to_uppercase(),
length: params.input.len(),
})
}
#[tokio::main]
async fn main() {
println!("=== Tower Service Example ===\n");
let mut service = tool_service();
println!("Test 1: Normal request");
let req = ToolRequest {
name: "process".to_string(),
params: serde_json::json!({"input": "hello world"}),
ctx: Arc::new(ToolContext::new()),
};
match service.call(req).await {
Ok(response) => {
println!("Success!");
println!("Output: {}", serde_json::to_string_pretty(&response.output).unwrap());
}
Err(e) => {
println!("Error: {}", e);
}
}
println!();
println!("Test 2: Non-existent tool");
let req = ToolRequest {
name: "nonexistent".to_string(),
params: serde_json::json!({}),
ctx: Arc::new(ToolContext::new()),
};
match service.call(req).await {
Ok(response) => {
println!("Unexpected success: {:?}", response);
}
Err(e) => {
println!("Expected error: {}", e);
}
}
println!();
println!("Test 3: Multiple requests");
for i in 0..3 {
let req = ToolRequest {
name: "process".to_string(),
params: serde_json::json!({"input": &format!("request {}", i)}),
ctx: Arc::new(ToolContext::new()),
};
match service.call(req).await {
Ok(response) => {
println!("Request {}: {:?}", i + 1, response.output);
}
Err(e) => {
println!("Request {} failed: {}", i + 1, e);
}
}
}
}