Class: AgentsControl::Dispatcher

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

Overview

Decides what to do with an agent event: stay quiet, notify, or ask.

All the policy lives here, because it's the one part of the system people actually want to tune with settings. Agent adapters only turn a payload into an Event, a channel only displays it — Dispatcher does the thinking.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agents:, channel:, config:, pending: nil, logger: nil) ⇒ Dispatcher

The key distinction: is a human at the keyboard.

While they are, intercepting permission requests is counterproductive: they'll answer in the terminal faster than they can reach for their phone, and a blocked hook keeps the dialog from ever reaching the screen at all. So by default this only notifies, and interception is turned on explicitly — with /away before stepping out.



17
18
19
20
21
22
23
# File 'lib/agents_control/dispatcher.rb', line 17

def initialize(agents:, channel:, config:, pending: nil, logger: nil)
  @agents = agents
  @channel = channel
  @config = config
  @pending = pending || Pending.new
  @logger = logger
end

Instance Attribute Details

#pendingObject (readonly)

Returns the value of attribute pending.



25
26
27
# File 'lib/agents_control/dispatcher.rb', line 25

def pending
  @pending
end

Instance Method Details

#handle(payload) ⇒ Object

Returns the hook response body. An empty hash means "no decision" — the agent behaves as it would without us.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/agents_control/dispatcher.rb', line 29

def handle(payload)
  agent = @agents.find { |candidate| candidate.class.handles?(payload) }
  return {} unless agent

  event = agent.to_event(payload)
  return {} unless event

  reply = decide(event)
  return {} unless reply

  agent.to_response(event, reply)
rescue StandardError => e
  log("failed to handle event: #{e.class}: #{e.message}")
  {}
end