Class: Agents::CallbackManager

Inherits:
Object
  • Object
show all
Defined in:
lib/agents/callback_manager.rb

Overview

Manager for handling and emitting callback events in a thread-safe manner. Provides both generic emit() method and typed convenience methods.

Examples:

Using generic emit

manager.emit(:tool_start, tool_name, args)

Using typed methods

manager.emit_tool_start(tool_name, args)
manager.emit_agent_thinking(agent_name, input)

Constant Summary collapse

EVENT_TYPES =

Supported callback event types

%i[
  run_start
  run_complete
  agent_complete
  tool_start
  tool_complete
  agent_thinking
  agent_handoff
  llm_call_complete
  chat_created
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(callbacks = {}) ⇒ CallbackManager

Returns a new instance of CallbackManager.



27
28
29
# File 'lib/agents/callback_manager.rb', line 27

def initialize(callbacks = {})
  @callbacks = callbacks.dup.freeze
end

Instance Method Details

#emit(event_type, *args) ⇒ Object

Generic method to emit any callback event type. Handles arity-aware dispatch: lambdas with strict arity receive only the arguments they expect (extra trailing args are sliced off), while procs and blocks (which have flexible arity) receive all arguments. This ensures backwards compatibility when new arguments (e.g. context_wrapper) are appended to existing callback signatures.

Parameters:

  • event_type (Symbol)

    The type of event to emit

  • args (Array)

    Arguments to pass to callbacks



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/agents/callback_manager.rb', line 40

def emit(event_type, *args)
  callback_list = @callbacks[event_type] || []

  callback_list.each do |callback|
    safe_args = arity_safe_args(callback, args)
    callback.call(*safe_args)
  rescue StandardError => e
    # Log callback errors but don't let them crash execution
    warn "Callback error for #{event_type}: #{e.message}"
  end
end