use mojentic::agents::simple_recursive_agent::{
AnySolverEvent, GoalAchievedEvent, GoalFailedEvent, GoalSubmittedEvent,
IterationCompletedEvent, SimpleRecursiveAgent,
};
use mojentic::llm::gateways::OllamaGateway;
use mojentic::llm::tools::simple_date_tool::SimpleDateTool;
use mojentic::llm::tools::LlmTool;
use mojentic::llm::LlmBroker;
use std::sync::Arc;
use tokio::sync::Mutex;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("\n{}", "=".repeat(80));
println!("SIMPLE RECURSIVE AGENT - EXAMPLE");
println!("{}\n", "=".repeat(80));
let gateway = Arc::new(OllamaGateway::default());
let broker = Arc::new(LlmBroker::new("qwen3:32b", gateway, None));
example_1_basic_usage(broker.clone()).await?;
example_2_event_handling(broker.clone()).await?;
example_3_with_tools(broker.clone()).await?;
example_4_concurrent_solving(broker.clone()).await?;
example_5_custom_system_prompt(broker.clone()).await?;
println!("{}", "=".repeat(80));
println!("All examples completed!");
println!("{}", "=".repeat(80));
Ok(())
}
async fn example_1_basic_usage(broker: Arc<LlmBroker>) -> Result<(), Box<dyn std::error::Error>> {
println!("Example 1: Basic Usage\n");
let agent = SimpleRecursiveAgent::builder(broker).max_iterations(3).build();
let problem = "What is the capital of France?";
println!("Problem: {}", problem);
let solution = agent.solve(problem).await?;
println!("Solution: {}\n", solution);
Ok(())
}
async fn example_2_event_handling(
broker: Arc<LlmBroker>,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Example 2: With Event Handling\n");
let agent = SimpleRecursiveAgent::builder(broker).max_iterations(3).build();
let problem = "What are the three primary colors?";
println!("Problem: {}", problem);
let iteration_count = Arc::new(Mutex::new(0));
let iteration_count_clone = iteration_count.clone();
agent
.emitter
.subscribe(move |event: AnySolverEvent| {
let iteration_count = iteration_count_clone.clone();
tokio::spawn(async move {
match event {
AnySolverEvent::GoalSubmitted(GoalSubmittedEvent { state }) => {
println!(" 🎯 Goal submitted: {}", state.goal);
}
AnySolverEvent::IterationCompleted(IterationCompletedEvent {
state,
response: _,
}) => {
*iteration_count.lock().await += 1;
println!(" 🔄 Iteration {} completed", state.iteration);
}
AnySolverEvent::GoalAchieved(GoalAchievedEvent { state }) => {
println!(" ✅ Problem solved after {} iterations", state.iteration);
}
AnySolverEvent::GoalFailed(GoalFailedEvent { state }) => {
println!(" ❌ Problem failed after {} iterations", state.iteration);
}
AnySolverEvent::Timeout(_) => {
println!(" ⏱️ Problem timed out");
}
}
});
})
.await;
let solution = agent.solve(problem).await?;
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
println!("Solution: {}\n", solution);
Ok(())
}
async fn example_3_with_tools(broker: Arc<LlmBroker>) -> Result<(), Box<dyn std::error::Error>> {
println!("Example 3: With Tools\n");
let tools: Vec<Box<dyn LlmTool>> = vec![Box::new(SimpleDateTool)];
let agent = SimpleRecursiveAgent::builder(broker).tools(tools).max_iterations(5).build();
let problem = "What's the date next Friday?";
println!("Problem: {}", problem);
let solution = agent.solve(problem).await?;
println!("Solution: {}\n", solution);
Ok(())
}
async fn example_4_concurrent_solving(
broker: Arc<LlmBroker>,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Example 4: Concurrent Problem Solving\n");
println!("Running multiple problems concurrently...");
let agent = Arc::new(SimpleRecursiveAgent::builder(broker).max_iterations(3).build());
let problems = vec![
"What is the Pythagorean theorem?",
"Explain the concept of recursion in programming.",
];
let mut tasks = Vec::new();
for problem in problems {
let agent = agent.clone();
let task = tokio::spawn(async move {
println!("\nStarted solving: {}", problem);
let solution = agent.solve(problem).await.unwrap();
println!("\nSolution for '{}':\n{}", problem, solution);
solution
});
tasks.push(task);
}
for task in tasks {
task.await?;
}
println!("\nAll concurrent problems have been solved!");
Ok(())
}
async fn example_5_custom_system_prompt(
broker: Arc<LlmBroker>,
) -> Result<(), Box<dyn std::error::Error>> {
println!("\n{}", "=".repeat(80));
println!("Example 5: Custom System Prompt\n");
let custom_prompt = "You are a concise assistant that provides brief, factual answers. \
Always respond in exactly one sentence.";
let agent = SimpleRecursiveAgent::builder(broker)
.max_iterations(3)
.system_prompt(custom_prompt)
.build();
let problem = "What is Rust?";
println!("Problem: {}", problem);
let solution = agent.solve(problem).await?;
println!("Solution: {}\n", solution);
Ok(())
}