Class: SwarmSDK::Observer::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/observer/builder.rb

Overview

DSL for configuring observer agents

Used by Swarm::Builder#observer to provide a clean DSL for defining event handlers and observer configuration options.

Examples:

Basic usage

observer :profiler do
  on :swarm_start do |event|
    "Analyze this prompt: #{event[:prompt]}"
  end

  timeout 120
  max_concurrent 2
end

Instance Method Summary collapse

Constructor Details

#initialize(agent_name, config) ⇒ Builder

Initialize builder with agent name and config

Parameters:

  • agent_name (Symbol)

    Name of the observer agent

  • config (Observer::Config)

    Configuration object to populate



24
25
26
27
# File 'lib/swarm_sdk/observer/builder.rb', line 24

def initialize(agent_name, config)
  @agent_name = agent_name
  @config = config
end

Instance Method Details

#max_concurrent(n) ⇒ void

This method returns an undefined value.

Set maximum concurrent executions for this observer

Limits how many instances of this observer agent can run simultaneously. Useful for resource-intensive observers.

Parameters:

  • n (Integer)

    Maximum concurrent executions



56
57
58
# File 'lib/swarm_sdk/observer/builder.rb', line 56

def max_concurrent(n)
  @config.options[:max_concurrent] = n
end

#on(event_type) {|Hash| ... } ⇒ void

This method returns an undefined value.

Register an event handler

The block receives the event hash and should return:

  • A prompt string to trigger the observer agent
  • nil to skip execution for this event

Examples:

on :tool_call do |event|
  next unless event[:tool_name] == "Bash"
  "Check this command: #{event[:arguments][:command]}"
end

Parameters:

  • event_type (Symbol)

    Type of event to handle (e.g., :swarm_start, :tool_call)

Yields:

  • (Hash)

    Event hash

Yield Returns:

  • (String, nil)

    Prompt or nil to skip



45
46
47
# File 'lib/swarm_sdk/observer/builder.rb', line 45

def on(event_type, &block)
  @config.add_handler(event_type, &block)
end

#timeout(seconds) ⇒ void

This method returns an undefined value.

Set timeout for observer execution

Observer tasks will be cancelled after this duration.

Parameters:

  • seconds (Integer)

    Timeout in seconds (default: 60)



66
67
68
# File 'lib/swarm_sdk/observer/builder.rb', line 66

def timeout(seconds)
  @config.options[:timeout] = seconds
end

#wait_for_completion!void

This method returns an undefined value.

Wait for observer to complete before swarm execution ends

By default, observers are fire-and-forget. This option causes the main execution to wait for this observer to complete.



76
77
78
# File 'lib/swarm_sdk/observer/builder.rb', line 76

def wait_for_completion!
  @config.options[:fire_and_forget] = false
end