Class: OllamaAgent::Streaming::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/streaming/hooks.rb

Overview

Lightweight event bus for agent lifecycle events. All layers share one Hooks instance per Agent run.

Constant Summary collapse

EVENTS =
%i[
  on_token on_thinking on_chunk on_tool_call on_tool_result on_assistant_message on_complete on_error on_retry
].freeze
MISSING_BLOCK_MESSAGE =
"Hooks require a block when registering a handler"

Instance Method Summary collapse

Constructor Details

#initializeHooks

Returns a new instance of Hooks.



14
15
16
# File 'lib/ollama_agent/streaming/hooks.rb', line 14

def initialize
  @handlers = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#emit(event, payload) ⇒ Object

Fire all handlers for the event with the given payload hash. Handler errors are swallowed to prevent a bad subscriber from crashing the agent.



27
28
29
30
31
32
33
# File 'lib/ollama_agent/streaming/hooks.rb', line 27

def emit(event, payload)
  @handlers[event].each do |handler|
    handler.call(payload)
  rescue StandardError
    nil
  end
end

#on(event, &block) ⇒ Object

Register a handler block for a named event.

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'lib/ollama_agent/streaming/hooks.rb', line 19

def on(event, &block)
  raise ArgumentError, MISSING_BLOCK_MESSAGE unless block_given?

  @handlers[event] << block
end

#subscribed?(event) ⇒ Boolean

Returns true if at least one handler is registered for the event.

Returns:

  • (Boolean)


36
37
38
# File 'lib/ollama_agent/streaming/hooks.rb', line 36

def subscribed?(event)
  @handlers[event].any?
end