Module engine

Module engine 

Source
Expand description

Rhai engine setup and tool orchestration.

This module contains the core ToolOrchestrator struct that executes Rhai scripts with access to registered tools. It implements Anthropic’s “Programmatic Tool Calling” pattern.

§Architecture

The orchestrator uses feature-gated thread-safety primitives:

  • native feature: Uses Arc<Mutex<T>> for thread-safe execution
  • wasm feature: Uses Rc<RefCell<T>> for single-threaded WASM

§Key Components

§Example

use tool_orchestrator::{ToolOrchestrator, ExecutionLimits};

let mut orchestrator = ToolOrchestrator::new();

// Register a tool
orchestrator.register_executor("greet", |input| {
    let name = input.as_str().unwrap_or("world");
    Ok(format!("Hello, {}!", name))
});

// Execute a script that uses the tool
let result = orchestrator.execute(
    r#"greet("Claude")"#,
    ExecutionLimits::default()
)?;

assert_eq!(result.output, "Hello, Claude!");

§Security

The Rhai engine is sandboxed by default with no access to:

  • File system
  • Network
  • Shell commands
  • System time (except via provided primitives)

All resource limits are enforced via ExecutionLimits.

Structs§

ToolOrchestrator
Tool orchestrator - executes Rhai scripts with registered tool access.

Functions§

dynamic_to_json
Convert Rhai Dynamic value to serde_json::Value.

Type Aliases§

SharedCounter
Thread-safe counter wrapper (native: Arc<Mutex<usize>>)
SharedVec
Thread-safe vector wrapper (native: Arc<Mutex<Vec<T>>>)
ToolExecutor
Tool executor function type (native: thread-safe Arc<dyn Fn>)