use synapse::{Config, SynapseRouter, SimpleMessage, MessageType};
use anyhow::Result;
use tracing::{info, debug};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
info!("🔧 Starting Tool Interaction Example");
let ai_config = Config::default_for_entity("ai_assistant".to_string(), "ai_model".to_string());
let tool_config = Config::default_for_entity("calculator_tool".to_string(), "tool".to_string());
let ai_router = SynapseRouter::new(ai_config, "ai_assistant".to_string()).await?;
let tool_router = SynapseRouter::new(tool_config, "calculator_tool".to_string()).await?;
info!("✅ AI assistant and calculator tool initialized");
let interactions = vec![
("list_capabilities", "What can you do?"),
("calculate", "14 + 28"),
("calculate", "150 / 3"),
("calculate", "2^10"),
("validate", "Is 42 a valid number?"),
];
info!("🤖 Starting AI-Tool interaction simulation...");
for (operation, query) in interactions {
let request = SimpleMessage {
to: "calculator_tool".to_string(),
from_entity: "ai_assistant".to_string(),
content: format!("{}:{}", operation, query),
message_type: MessageType::Direct,
metadata: create_tool_metadata(operation),
};
info!("🔄 AI → Tool: {} ({})", query, operation);
debug!("Request: {}", request.content);
match ai_router.convert_to_secure_message(&request).await {
Ok(secure_msg) => {
debug!("✅ Request secured: {}", secure_msg.message_id);
}
Err(e) => {
debug!("⚠️ Request prepared (secure conversion failed: {})", e);
}
}
let response_content = generate_tool_response(operation, query);
let response = SimpleMessage {
to: "ai_assistant".to_string(),
from_entity: "calculator_tool".to_string(),
content: response_content.clone(),
message_type: MessageType::Direct,
metadata: create_response_metadata(operation),
};
info!("🔧 Tool → AI: {}", response_content);
match tool_router.convert_to_secure_message(&response).await {
Ok(secure_msg) => {
debug!("✅ Response secured: {}", secure_msg.message_id);
}
Err(e) => {
debug!("⚠️ Response prepared (secure conversion failed: {})", e);
}
}
tokio::time::sleep(tokio::time::Duration::from_millis(300)).await;
}
info!("🎉 Tool Interaction example completed successfully!");
Ok(())
}
fn create_tool_metadata(operation: &str) -> HashMap<String, String> {
let mut metadata = HashMap::new();
metadata.insert("operation".to_string(), operation.to_string());
metadata.insert("tool_type".to_string(), "calculator".to_string());
metadata.insert("timestamp".to_string(),
chrono::Utc::now().to_rfc3339());
metadata.insert("request_id".to_string(),
format!("req-{}", uuid::Uuid::new_v4()));
metadata
}
fn create_response_metadata(operation: &str) -> HashMap<String, String> {
let mut metadata = HashMap::new();
metadata.insert("response_to".to_string(), operation.to_string());
metadata.insert("tool_type".to_string(), "calculator".to_string());
metadata.insert("timestamp".to_string(),
chrono::Utc::now().to_rfc3339());
metadata.insert("response_id".to_string(),
format!("resp-{}", uuid::Uuid::new_v4()));
metadata
}
fn generate_tool_response(operation: &str, query: &str) -> String {
match operation {
"list_capabilities" => {
"I can perform basic mathematical operations: addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^)".to_string()
}
"calculate" => {
if query.contains("14 + 28") {
"Result: 42".to_string()
} else if query.contains("150 / 3") {
"Result: 50".to_string()
} else if query.contains("2^10") {
"Result: 1024".to_string()
} else {
"Error: Unable to parse mathematical expression".to_string()
}
}
"validate" => {
if query.contains("42") {
"Yes, 42 is a valid number and quite famous in certain circles!".to_string()
} else {
"Validation result depends on the specific context".to_string()
}
}
_ => {
"Error: Unknown operation".to_string()
}
}
}