Class: SwarmSDK::Swarm::SwarmRegistryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/swarm/swarm_registry_builder.rb

Overview

Builder for swarm registry in DSL

Supports registering external swarms for composable swarms pattern.

Examples:

swarms do
  register "code_review", file: "./swarms/code_review.rb"
  register "testing", file: "./swarms/testing.yml", keep_context: false
end

Inline swarm definition

swarms do
  register "tester" do
    lead :tester
    agent :tester do
      model "gpt-4o-mini"
      system "You test code"
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSwarmRegistryBuilder

Returns a new instance of SwarmRegistryBuilder.



29
30
31
# File 'lib/swarm_sdk/swarm/swarm_registry_builder.rb', line 29

def initialize
  @registrations = []
end

Instance Attribute Details

#registrationsObject (readonly)

Returns the value of attribute registrations.



27
28
29
# File 'lib/swarm_sdk/swarm/swarm_registry_builder.rb', line 27

def registrations
  @registrations
end

Instance Method Details

#register(name, file: nil, yaml: nil, keep_context: true) { ... } ⇒ Object

Register a swarm from file, YAML string, or inline block

Parameters:

  • name (String, Symbol)

    Registration name

  • file (String, nil) (defaults to: nil)

    Path to swarm file (.rb or .yml)

  • yaml (String, nil) (defaults to: nil)

    YAML content string

  • keep_context (Boolean) (defaults to: true)

    Whether to preserve conversation state (default: true)

Yields:

  • Optional block for inline swarm definition

Raises:

  • (ArgumentError)

    If neither file, yaml, nor block provided



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swarm_sdk/swarm/swarm_registry_builder.rb', line 41

def register(name, file: nil, yaml: nil, keep_context: true, &block)
  # Validate that exactly one source is provided
  sources = [file, yaml, block].compact
  if sources.empty?
    raise ArgumentError, "register '#{name}' requires either file:, yaml:, or a block"
  elsif sources.size > 1
    raise ArgumentError, "register '#{name}' accepts only one of: file:, yaml:, or block (got #{sources.size})"
  end

  # Determine source type and store
  source = if file
    { type: :file, value: file }
  elsif yaml
    { type: :yaml, value: yaml }
  else
    { type: :block, value: block }
  end

  @registrations << {
    name: name.to_s,
    source: source,
    keep_context: keep_context,
  }
end