Class: Agents::Dispatcher

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

Direct Known Subclasses

DispatchingGptAgent

Instance Attribute Summary collapse

Attributes inherited from Agent

#actions

Instance Method Summary collapse

Constructor Details

#initialize(agents: [], **args) ⇒ Dispatcher

Returns a new instance of Dispatcher.



4
5
6
7
# File 'lib/agents/dispatcher.rb', line 4

def initialize(agents: [], **args)
  super(**args)
  @agents = agents
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



3
4
5
# File 'lib/agents/dispatcher.rb', line 3

def agents
  @agents
end

Instance Method Details

#agent_for_request(request:) ⇒ Object

Default behavior is a random agent. Use a subclass to specify behavior.



23
24
25
26
27
# File 'lib/agents/dispatcher.rb', line 23

def agent_for_request(request:)
  return UnhandleableRequestAgent.new if agents.empty?

  @agents.shuffle.first
end

#handle(request:) ⇒ Object



17
18
19
20
# File 'lib/agents/dispatcher.rb', line 17

def handle(request:)
  agent = agent_for_request(request: request)
  agent.handle(request: request)
end

#register(agent) ⇒ Object



9
10
11
# File 'lib/agents/dispatcher.rb', line 9

def register(agent)
  @agents << agent
end

#unregister(agent) ⇒ Object



13
14
15
# File 'lib/agents/dispatcher.rb', line 13

def unregister(agent)
  @agents.delete(agent)
end