use synapse::transport::{
LlmDiscoveryManager, LlmDiscoveryConfig, DiscoveredLlm, LlmRequest
};
use std::time::Duration;
use tokio::time::{timeout, sleep};
use tracing::{info, warn};
use std::collections::HashMap;
struct AiTaskCoordinator {
llm_discovery: LlmDiscoveryManager,
task_counter: u32, }
#[derive(Debug, Clone)]
struct NetworkTask {
pub id: String,
pub task_type: String,
pub description: String,
pub priority: u8,
pub max_response_tokens: Option<u32>,
pub temperature: Option<f32>,
pub system_prompt: Option<String>,
}
#[derive(Debug, Clone)]
struct TaskResult {
pub task_id: String,
pub llm_used: String,
pub success: bool,
pub result: String,
pub processing_time_ms: u64,
pub confidence: f64,
pub tokens_used: u32,
}
impl AiTaskCoordinator {
async fn new() -> Result<Self, Box<dyn std::error::Error>> {
let config = LlmDiscoveryConfig {
scan_interval: Duration::from_secs(30),
cache_ttl: Duration::from_secs(600), preferred_capabilities: vec![
"conversation".to_string(),
"reasoning".to_string(),
"code_generation".to_string(),
"analysis".to_string(),
"creative_writing".to_string(),
],
max_llms: 25,
required_capabilities: vec!["conversation".to_string()],
};
let llm_discovery = LlmDiscoveryManager::new(Some(config)).await?;
Ok(Self {
llm_discovery,
task_counter: 0,
})
}
async fn start(&self) -> Result<(), Box<dyn std::error::Error>> {
info!("🚀 Starting AI Task Coordinator");
self.llm_discovery.start_discovery().await?;
sleep(Duration::from_secs(10)).await;
Ok(())
}
async fn get_llm_network_status(&self) -> Vec<DiscoveredLlm> {
self.llm_discovery.get_cached_llms().await
}
async fn execute_task(&mut self, task: NetworkTask) -> Result<TaskResult, Box<dyn std::error::Error>> {
let start_time = std::time::Instant::now();
self.task_counter += 1;
let task_number = self.task_counter;
info!("🎯 Executing task #{} ({}): {}", task_number, task.task_type, task.id);
if let Some(llm) = self.llm_discovery.find_best_llm(&task.task_type).await? {
info!("🎯 Routing task #{} to: {}", task_number, llm.display_name);
match self.execute_task_with_llm(&task, &llm).await {
Ok(result) => return Ok(result),
Err(e) => {
warn!("❌ Primary LLM failed for task #{}: {}. Trying fallback...", task_number, e);
}
}
}
let available_llms = self.llm_discovery
.find_llms_with_capabilities(&[task.task_type.clone()])
.await?;
if let Some(fallback_llm) = available_llms.first() {
warn!("🔄 Using fallback LLM: {}", fallback_llm.display_name);
return self.execute_task_with_llm(&task, fallback_llm).await;
}
warn!("🤖 No suitable LLM found. Using local simulation for {}", task.task_type);
let processing_time = start_time.elapsed().as_millis() as u64;
Ok(TaskResult {
task_id: task.id,
llm_used: "local_simulation".to_string(),
success: true,
result: format!("Simulated response for: {}", task.description),
processing_time_ms: processing_time,
confidence: 0.5,
tokens_used: 50,
})
}
async fn execute_task_with_llm(
&self,
task: &NetworkTask,
llm: &DiscoveredLlm
) -> Result<TaskResult, Box<dyn std::error::Error>> {
let start_time = std::time::Instant::now();
let connection = self.llm_discovery.connect_to_llm(llm).await?;
let mut metadata = HashMap::new();
metadata.insert("task_id".to_string(), task.id.clone());
metadata.insert("task_type".to_string(), task.task_type.clone());
metadata.insert("priority".to_string(), task.priority.to_string());
let request = LlmRequest {
prompt: task.description.clone(),
max_tokens: task.max_response_tokens,
temperature: task.temperature,
system_prompt: task.system_prompt.clone(),
metadata,
};
let response = timeout(
Duration::from_secs(45),
connection.send_request(request)
).await??;
let processing_time = start_time.elapsed().as_millis() as u64;
Ok(TaskResult {
task_id: task.id.clone(),
llm_used: llm.display_name.clone(),
success: true,
result: response.content,
processing_time_ms: processing_time,
confidence: response.metadata.confidence_score,
tokens_used: response.metadata.tokens_used,
})
}
async fn execute_parallel_tasks(&mut self, tasks: Vec<NetworkTask>) -> Vec<TaskResult> {
let mut results = Vec::new();
for task in tasks {
match self.execute_task(task).await {
Ok(result) => results.push(result),
Err(_) => {
results.push(TaskResult {
task_id: "unknown".to_string(),
llm_used: "failed".to_string(),
success: false,
result: "Task execution failed".to_string(),
processing_time_ms: 0,
confidence: 0.0,
tokens_used: 0,
});
}
}
}
results
}
async fn generate_network_report(&self) -> String {
let llms = self.get_llm_network_status().await;
let mut report = String::from("🔍 Synapse LLM Network Status Report\n");
report.push_str("=====================================\n\n");
if llms.is_empty() {
report.push_str("❌ No LLMs discovered on the network.\n");
return report;
}
report.push_str(&format!("📊 Total LLMs Discovered: {}\n\n", llms.len()));
let mut capability_map: HashMap<String, Vec<&DiscoveredLlm>> = HashMap::new();
for llm in &llms {
for cap in &llm.capabilities {
capability_map.entry(cap.clone()).or_default().push(llm);
}
}
report.push_str("🧠 Capabilities Distribution:\n");
for (capability, llm_list) in capability_map {
report.push_str(&format!(" • {}: {} LLMs\n", capability, llm_list.len()));
}
report.push_str("\n🤖 Individual LLM Status:\n");
for (i, llm) in llms.iter().enumerate() {
report.push_str(&format!(
" {}. {} ({})\n Model: {} | Status: {:?}\n Avg Response: {:.0}ms | Success Rate: {:.1}%\n Load: {:.1}% | Quality: {:.1}/1.0\n\n",
i + 1,
llm.display_name,
llm.model_info.provider,
llm.model_info.model_name,
llm.status,
llm.performance_metrics.avg_response_time_ms,
llm.performance_metrics.success_rate * 100.0,
llm.performance_metrics.load_factor * 100.0,
llm.performance_metrics.quality_score
));
}
report
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
println!("🌐 Synapse AI Network Coordination Demo");
println!("=======================================");
println!();
let mut coordinator = AiTaskCoordinator::new().await?;
coordinator.start().await?;
println!("⏳ Discovering LLMs on the network (15 seconds)...");
sleep(Duration::from_secs(15)).await;
let network_report = coordinator.generate_network_report().await;
println!("{}", network_report);
let tasks = vec![
NetworkTask {
id: "task-001".to_string(),
task_type: "code_generation".to_string(),
description: "Write a Python function to implement binary search".to_string(),
priority: 1,
max_response_tokens: Some(200),
temperature: Some(0.2),
system_prompt: Some("You are an expert programmer.".to_string()),
},
NetworkTask {
id: "task-002".to_string(),
task_type: "reasoning".to_string(),
description: "Solve this logic puzzle: If all birds can fly and penguins are birds, why can't penguins fly?".to_string(),
priority: 2,
max_response_tokens: Some(150),
temperature: Some(0.3),
system_prompt: Some("You are a logic expert.".to_string()),
},
NetworkTask {
id: "task-003".to_string(),
task_type: "creative_writing".to_string(),
description: "Write a short poem about artificial intelligence and human collaboration".to_string(),
priority: 3,
max_response_tokens: Some(100),
temperature: Some(0.8),
system_prompt: Some("You are a creative writer.".to_string()),
},
NetworkTask {
id: "task-004".to_string(),
task_type: "analysis".to_string(),
description: "Analyze the pros and cons of distributed AI systems".to_string(),
priority: 1,
max_response_tokens: Some(250),
temperature: Some(0.5),
system_prompt: Some("You are a technology analyst.".to_string()),
},
];
println!("🔄 Processing {} tasks across the LLM network...", tasks.len());
println!();
for task in &tasks {
println!("� Executing: {}", task.description);
let result = coordinator.execute_task(task.clone()).await?;
if result.success {
println!("✅ Completed by {} in {}ms",
result.llm_used,
result.processing_time_ms);
println!("📊 Confidence: {:.1}% | Tokens: {}",
result.confidence * 100.0,
result.tokens_used);
println!("📄 Result: {}",
if result.result.len() > 120 {
format!("{}...", &result.result[..120])
} else {
result.result
});
} else {
println!("❌ Task failed: {}", result.result);
}
println!();
}
println!("⚡ Now executing all tasks in parallel...");
let parallel_tasks = tasks.clone();
let parallel_results = coordinator.execute_parallel_tasks(parallel_tasks).await;
println!("📊 Parallel Execution Results:");
for result in parallel_results {
if result.success {
println!(" ✅ {} completed by {} in {}ms",
result.task_id,
result.llm_used,
result.processing_time_ms);
} else {
println!(" ❌ {} failed", result.task_id);
}
}
println!();
println!("🎊 Synapse AI Network Coordination Demo completed!");
println!(" Features demonstrated:");
println!(" ✓ Multi-LLM network discovery");
println!(" ✓ Intelligent task routing by capability");
println!(" ✓ Performance monitoring and metrics");
println!(" ✓ Fault tolerance and fallback strategies");
println!(" ✓ Parallel task execution across multiple LLMs");
println!(" ✓ Network status reporting and analytics");
Ok(())
}