Class: OllamaAgent::Streaming::Hooks
- Inherits:
-
Object
- Object
- OllamaAgent::Streaming::Hooks
- 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
-
#emit(event, payload) ⇒ Object
Fire all handlers for the event with the given payload hash.
-
#initialize ⇒ Hooks
constructor
A new instance of Hooks.
-
#on(event, &block) ⇒ Object
Register a handler block for a named event.
-
#subscribed?(event) ⇒ Boolean
Returns true if at least one handler is registered for the event.
Constructor Details
#initialize ⇒ Hooks
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.
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.
36 37 38 |
# File 'lib/ollama_agent/streaming/hooks.rb', line 36 def subscribed?(event) @handlers[event].any? end |