Skip to main content

Crate limit_agent

Crate limit_agent 

Source
Expand description

§limit-agent

Crates.io Docs.rs License: MIT

Agent runtime for AI applications with tool registry and Docker sandbox.

Build autonomous AI agents that can execute tools, run code in isolated containers, and maintain state across conversations.

§Features

  • Tool Registry: Define, register, and execute tools dynamically
  • Docker Sandbox: Isolated execution environment for untrusted code
  • Event-driven: Subscribe to agent lifecycle events
  • State Management: Persist and restore agent state
  • LLM Integration: Works seamlessly with limit-llm

§Quick Start

§Define a Custom Tool

use async_trait::async_trait;
use limit_agent::{Tool, AgentError};
use serde_json::{json, Value};

struct WeatherTool;

#[async_trait]
impl Tool for WeatherTool {
    fn name(&self) -> &str {
        "get_weather"
    }
     
    async fn execute(&self, args: Value) -> Result<Value, AgentError> {
        let location = args["location"].as_str().unwrap_or("Unknown");
        // In a real implementation, call a weather API
        Ok(json!({
            "location": location,
            "temp": 22,
            "condition": "sunny"
        }))
    }
}

§Register and Execute Tools

use limit_agent::ToolRegistry;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut registry = ToolRegistry::new();
     
    // Register tools
    registry.register(WeatherTool)?;
     
    // Execute a tool by name
    let result = registry
        .execute("get_weather", json!({ "location": "Tokyo" }))
        .await?;
     
    println!("Weather: {:?}", result);
     
    Ok(())
}

§Docker Sandbox

Run untrusted code in isolated Docker containers:

use limit_agent::sandbox::DockerSandbox;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Check if Docker is available
    if !DockerSandbox::check_docker_available().await {
        return Err("Docker is not available".into());
    }
     
    let sandbox = DockerSandbox::new().await?;
     
    // Create a container
    let container = sandbox.create_container("alpine:latest").await?;
     
    // Start and execute
    sandbox.start_container(&container).await?;
    let output = sandbox.execute_in_container(&container, &["echo".into(), "hello".into()]).await?;
    println!("{}", output);
     
    // Cleanup
    sandbox.cleanup_container(&container).await;
     
    Ok(())
}

§Event System

Subscribe to agent lifecycle events for logging, monitoring, or debugging:

use limit_agent::events::{EventBus, Event};

let events = EventBus::new();

events.subscribe(|event| {
    match event {
        Event::ToolCall { name, args, version: _ } => {
            println!("Tool {} started with {:?}", name, args);
        }
        Event::ToolResult { output, version: _ } => {
            println!("Tool completed: {}", output);
        }
        Event::Error { message, version: _ } => {
            eprintln!("Error: {}", message);
        }
        _ => {}
    }
});

§Core Types

TypeDescription
ToolTrait for defining executable tools
ToolRegistryRegistry for managing and executing tools
[DockerSandbox]Isolated execution environment
[StateManager]Persist/restore agent state
EventBusEvent subscription system

Re-exports§

pub use error::AgentError;
pub use events::EventBus;
pub use registry::ToolRegistry;
pub use tool::EchoTool;
pub use tool::Tool;

Modules§

error
events
executor
registry
sandbox
state
tool
Tool trait and built-in tools for agent execution.