Class: SwarmSDK::Swarm::LazyDelegateChat

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

Overview

Lazy loader for delegation agent chats

Instead of creating delegation instances eagerly at swarm initialization, this class defers creation until the first delegation call. This improves initialization performance for swarms where not all agents are used.

Thread-safe via Mutex - safe for concurrent access.

Examples:

lazy_chat = LazyDelegateChat.new(
  instance_name: "backend@frontend",
  base_name: :backend,
  agent_definition: backend_def,
  swarm: swarm
)

# Later, when delegation is first called:
chat = lazy_chat.chat  # Creates agent on first access
chat.ask("Do something")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_name:, base_name:, agent_definition:, swarm:) ⇒ LazyDelegateChat

Initialize a lazy delegate chat wrapper

Parameters:

  • instance_name (String)

    Unique instance name ("base@delegator")

  • base_name (Symbol)

    Base agent name (for definition lookup)

  • agent_definition (Agent::Definition)

    Agent definition

  • swarm (Swarm)

    Parent swarm reference



33
34
35
36
37
38
39
40
41
# File 'lib/swarm_sdk/swarm/lazy_delegate_chat.rb', line 33

def initialize(instance_name:, base_name:, agent_definition:, swarm:)
  @instance_name = instance_name
  @base_name = base_name
  @agent_definition = agent_definition
  @swarm = swarm
  @chat = nil
  @mutex = Mutex.new
  @initialized = false
end

Instance Attribute Details

#agent_definitionObject (readonly)

Returns the value of attribute agent_definition.



25
26
27
# File 'lib/swarm_sdk/swarm/lazy_delegate_chat.rb', line 25

def agent_definition
  @agent_definition
end

#base_nameObject (readonly)

Returns the value of attribute base_name.



25
26
27
# File 'lib/swarm_sdk/swarm/lazy_delegate_chat.rb', line 25

def base_name
  @base_name
end

#instance_nameObject (readonly)

Returns the value of attribute instance_name.



25
26
27
# File 'lib/swarm_sdk/swarm/lazy_delegate_chat.rb', line 25

def instance_name
  @instance_name
end

Instance Method Details

#chatAgent::Chat

Get or create the agent chat instance

On first call, creates the agent chat and runs initialization. Subsequent calls return the cached instance.

Returns:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/swarm_sdk/swarm/lazy_delegate_chat.rb', line 49

def chat
  return @chat if @initialized

  @mutex.synchronize do
    return @chat if @initialized

    @chat = initialize_chat
    @initialized = true
    @chat
  end
end

#initialized?Boolean

Check if the agent has been initialized

Returns:

  • (Boolean)

    True if chat has been created



64
65
66
# File 'lib/swarm_sdk/swarm/lazy_delegate_chat.rb', line 64

def initialized?
  @mutex.synchronize { @initialized }
end