Module simple_recursive_agent

Module simple_recursive_agent 

Source
Expand description

Simple recursive agent using event-driven architecture.

This module provides a declarative, event-driven agent that recursively attempts to solve problems using available tools. The agent continues iterating until it succeeds, fails explicitly, or reaches the maximum number of iterations.

§Architecture

The agent uses three main components:

  1. GoalState - Tracks the problem-solving state through iterations
  2. EventEmitter - Manages event subscriptions and async dispatch
  3. SimpleRecursiveAgent - Orchestrates the problem-solving process

§Events

The agent emits the following events during problem-solving:

  • GoalSubmittedEvent - When a problem is submitted
  • IterationCompletedEvent - After each iteration completes
  • GoalAchievedEvent - When the goal is successfully achieved
  • GoalFailedEvent - When the goal explicitly fails
  • TimeoutEvent - When the process times out

§Examples

use mojentic::agents::SimpleRecursiveAgent;
use mojentic::llm::{LlmBroker, LlmGateway};
use mojentic::llm::gateways::OllamaGateway;
use mojentic::llm::tools::simple_date_tool::SimpleDateTool;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let gateway = Arc::new(OllamaGateway::default());
    let broker = Arc::new(LlmBroker::new("qwen3:32b", gateway, None));

    let tools: Vec<Box<dyn LlmTool>> = vec![Box::new(SimpleDateTool)];

    let agent = SimpleRecursiveAgent::builder(broker)
        .tools(tools)
        .max_iterations(5)
        .build();

    // Subscribe to events
    agent.emitter.subscribe(|event: IterationCompletedEvent| {
        println!("Iteration {}: {}", event.state.iteration, event.response);
    });

    let result = agent.solve("What's the date next Friday?").await?;
    println!("Result: {}", result);

    Ok(())
}

§Completion Indicators

The agent monitors responses for these keywords (case-insensitive, word boundaries):

  • “DONE” - Task completed successfully
  • “FAIL” - Task cannot be completed

Structs§

EventEmitter
A simple event emitter that allows subscribing to and emitting events.
GoalAchievedEvent
Event triggered when a goal is successfully achieved.
GoalFailedEvent
Event triggered when a goal cannot be solved.
GoalState
Represents the state of a problem-solving process.
GoalSubmittedEvent
Event triggered when a goal is submitted for solving.
IterationCompletedEvent
Event triggered when an iteration of the problem-solving process is completed.
SimpleRecursiveAgent
An agent that recursively attempts to solve a problem using available tools.
SimpleRecursiveAgentBuilder
Builder for constructing a SimpleRecursiveAgent with custom configuration.
TimeoutEvent
Event triggered when the problem-solving process times out.

Enums§

AnySolverEvent
Union type of all solver events

Traits§

SolverEvent
Base trait for solver events.