Skip to main content

Module callback

Module callback 

Source
Expand description

Advanced callback system for agent events.

This module provides a type-safe, extensible callback system that allows hooking into various agent lifecycle events with support for:

  • Type-based dispatch: Register callbacks for specific step types
  • Priority ordering: Control callback execution order
  • Async support: Both sync and async callbacks
  • Context passing: Access agent state in callbacks

§Example

use machi::callback::{CallbackRegistry, Priority};
use machi::memory::ActionStep;

// Sync callbacks
let registry = CallbackRegistry::builder()
    .on::<ActionStep>(|step, ctx| {
        println!("Step {} completed", step.step_number);
    })
    .on_any(|step, ctx| {
        println!("Any step: {:?}", step.to_value());
    })
    .with_logging()
    .build();

// Async callbacks
let async_registry = AsyncCallbackRegistry::builder()
    .on_async::<ActionStep>(|step, ctx| async move {
        // Async operation like sending to a channel
        println!("Async: Step {} completed", step.step_number);
    })
    .build();

Structs§

AsyncCallbackRegistry
Async callback registry for managing async callbacks.
AsyncCallbackRegistryBuilder
Builder for constructing an AsyncCallbackRegistry with fluent API.
CallbackContext
Context passed to callbacks during agent execution.
CallbackRegistry
Registry for managing callbacks by step type.
CallbackRegistryBuilder
Builder for constructing a CallbackRegistry with fluent API.
LoggingConfig
Configuration for the logging handler.
MetricsCollector
Metrics collector for tracking agent execution statistics.
MetricsSnapshot
Snapshot of metrics at a point in time.
Priority
Priority level for callback execution order.
RunMetrics
Metrics collected during an agent run, including duration.

Functions§

logging_handler
Create a logging handler with the given configuration.
metrics_handler
Create a metrics handler that updates the given collector.
tracing_handler
Create a tracing handler that emits spans for each step.

Type Aliases§

BoxedCallback
Type alias for boxed synchronous callback.
CallbackContextRef
Reference-counted callback context for shared access.
CallbackFn
Type alias for synchronous callback function.