Skip to main content

Crate runagent

Crate runagent 

Source
Expand description

§RunAgent Rust SDK

A comprehensive Rust SDK for deploying and managing AI agents with support for multiple frameworks including LangChain, LangGraph, LlamaIndex, and more.

§Features

  • Client SDK: REST and WebSocket clients for interacting with deployed agents
  • Real-time Streaming: WebSocket-based streaming for real-time agent interactions
  • Type Safety: Full Rust type safety with comprehensive error handling
  • Async/Await: Built on Tokio for high-performance async operations

§Quick Start

§Basic Agent Interaction

use runagent::{RunAgentClient, RunAgentClientConfig};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    runagent::init_logging();

    // Create a client for a local agent
    let client = RunAgentClient::new(RunAgentClientConfig {
        agent_id: "my-agent-id".to_string(),
        entrypoint_tag: "generic".to_string(),
        local: Some(true),
        ..RunAgentClientConfig::default()
    }).await?;
     
    // Run the agent with input
    let response = client.run(&[
        ("message", json!("Hello, world!")),
        ("temperature", json!(0.7))
    ]).await?;
     
    println!("Response: {}", response);
    Ok(())
}

§Streaming Agent Interaction

use runagent::{RunAgentClient, RunAgentClientConfig};
use futures::StreamExt;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = RunAgentClient::new(RunAgentClientConfig {
        agent_id: "my-agent-id".to_string(),
        entrypoint_tag: "generic_stream".to_string(),
        local: Some(true),
        ..RunAgentClientConfig::default()
    }).await?;
     
    // Create a streaming connection
    let mut stream = client.run_stream(&[
        ("message", json!("Tell me a story"))
    ]).await?;
     
    // Process streaming responses
    while let Some(chunk) = stream.next().await {
        match chunk {
            Ok(data) => println!("Chunk: {}", data),
            Err(e) => eprintln!("Stream error: {}", e),
        }
    }
     
    Ok(())
}

§Connecting to Local Agents

use runagent::{RunAgentClient, RunAgentClientConfig};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to a local agent running on localhost:8450
    let client = RunAgentClient::new(RunAgentClientConfig {
        agent_id: "my-agent-id".to_string(),
        entrypoint_tag: "generic".to_string(),
        local: Some(true),
        host: Some("127.0.0.1".to_string()),
        port: Some(8450),
        ..RunAgentClientConfig::default()
    }).await?;
     
    let response = client.run(&[
        ("message", json!("Hello, world!"))
    ]).await?;
     
    println!("Response: {}", response);
    Ok(())
}

§Framework-Specific Examples

§LangChain Integration

use runagent::{RunAgentClient, RunAgentClientConfig};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = RunAgentClient::new(RunAgentClientConfig {
        agent_id: "langchain-agent".to_string(),
        entrypoint_tag: "invoke".to_string(),
        local: Some(true),
        ..RunAgentClientConfig::default()
    }).await?;
     
    // LangChain invoke pattern
    let response = client.run(&[
        ("input", json!({
            "messages": [
                {"role": "user", "content": "What is the weather like?"}
            ]
        }))
    ]).await?;
     
    println!("LangChain response: {}", response);
    Ok(())
}

§LangGraph Workflows

use runagent::{RunAgentClient, RunAgentClientConfig};
use serde_json::json;
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = RunAgentClient::new(RunAgentClientConfig {
        agent_id: "langgraph-agent".to_string(),
        entrypoint_tag: "stream".to_string(),
        local: Some(true),
        ..RunAgentClientConfig::default()
    }).await?;
     
    // Stream LangGraph execution
    let mut stream = client.run_stream(&[
        ("input", json!({
            "messages": [{"role": "user", "content": "Analyze this data"}]
        }))
    ]).await?;
     
    while let Some(chunk) = stream.next().await {
        match chunk {
            Ok(data) => {
                if let Some(node) = data.get("node") {
                    println!("Executing node: {}", node);
                }
            }
            Err(e) => eprintln!("Error: {}", e),
        }
    }
     
    Ok(())
}

§Configuration

§Using Environment Variables

Set these environment variables for configuration:

export RUNAGENT_API_KEY="your-api-key"
export RUNAGENT_BASE_URL="https://api.runagent.ai"
export RUNAGENT_CACHE_DIR="~/.runagent"
export RUNAGENT_LOGGING_LEVEL="info"

§Using Configuration Builder

use runagent::RunAgentConfig;

let config = RunAgentConfig::new()
    .with_api_key("your-api-key")
    .with_base_url("https://api.runagent.ai")
    .with_logging()
    .build();

§Error Handling

The SDK provides comprehensive error handling with specific error types:

use runagent::{RunAgentError, RunAgentResult};

fn handle_errors() -> RunAgentResult<()> {
    // Your operation here
    match some_operation() {
        Ok(result) => {
            println!("Success: {}", result);
            Ok(())
        },
        Err(RunAgentError::Authentication { message }) => {
            eprintln!("Auth error: {}", message);
            Err(RunAgentError::authentication("Invalid credentials"))
        }
        Err(RunAgentError::Connection { message }) => {
            eprintln!("Connection error: {}", message);
            // Retry logic for retryable errors
            if message.contains("retryable") {
                retry_operation()
            } else {
                Err(RunAgentError::connection("Connection failed"))
            }
        }
        Err(e) => Err(e),
    }
}

fn some_operation() -> RunAgentResult<String> {
    Ok("test".to_string())
}

fn retry_operation() -> RunAgentResult<()> {
    Ok(())
}

§Architecture Overview

The RunAgent SDK focuses on client-side functionality for interacting with agents:

  • Client Components: High-level REST and WebSocket clients for interacting with deployed agents
  • Configuration Management: Environment-based and programmatic configuration
  • Streaming Support: WebSocket-based streaming for real-time agent interactions
  • Type Safety: Comprehensive error handling and type definitions

Re-exports§

pub use client::RestClient;
pub use client::RunAgentClient;
pub use client::RunAgentClientConfig;
pub use client::SocketClient;
pub use types::RunAgentError;
pub use types::RunAgentResult;
pub use blocking::BlockingStream;
pub use blocking::RunAgentClient as BlockingRunAgentClient;
pub use db::DatabaseService;

Modules§

blocking
Blocking (synchronous) wrapper for RunAgentClient
client
Client components for interacting with RunAgent deployments
constants
Constants for the RunAgent Client SDK
db
Minimal database module for agent lookups
prelude
Prelude module for convenient imports
types
Type definitions for the RunAgent SDK
utils
Utility modules for the RunAgent SDK

Structs§

RunAgentConfig
Configuration builder for the RunAgent SDK

Constants§

VERSION

Functions§

init_logging
Initialize logging for the RunAgent SDK