Class: LLMs::Stream::EventEmitter
- Inherits:
-
Object
- Object
- LLMs::Stream::EventEmitter
- Defined in:
- lib/llms/stream/event_emitter.rb
Constant Summary collapse
- VALID_EVENTS =
[ :message_started, :usage_updated, :text_delta, :thinking_delta, :tool_call_started, :tool_call_arguments_json_delta, :tool_call_arguments_updated, :tool_call_completed, :message_completed ]
Instance Method Summary collapse
- #add_handler(event_type, callable) ⇒ Object
- #connect(obj) ⇒ Object
- #emit(event_type, data) ⇒ Object
-
#initialize ⇒ EventEmitter
constructor
A new instance of EventEmitter.
- #on(event_type, &block) ⇒ Object
Constructor Details
#initialize ⇒ EventEmitter
Returns a new instance of EventEmitter.
16 17 18 |
# File 'lib/llms/stream/event_emitter.rb', line 16 def initialize @handlers = {} end |
Instance Method Details
#add_handler(event_type, callable) ⇒ Object
34 35 36 37 38 |
# File 'lib/llms/stream/event_emitter.rb', line 34 def add_handler(event_type, callable) raise ArgumentError, "Unknown event type: #{event_type}" unless VALID_EVENTS.include?(event_type) @handlers[event_type] ||= [] @handlers[event_type] << callable end |
#connect(obj) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/llms/stream/event_emitter.rb', line 25 def connect(obj) VALID_EVENTS.each do |event_type| self.on(event_type) do |data| obj.send(event_type, data) end end self end |
#emit(event_type, data) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/llms/stream/event_emitter.rb', line 40 def emit(event_type, data) raise ArgumentError, "Unknown event type: #{event_type}" unless VALID_EVENTS.include?(event_type) @handlers[event_type]&.each do |handler| handler.call(data) end end |
#on(event_type, &block) ⇒ Object
20 21 22 23 |
# File 'lib/llms/stream/event_emitter.rb', line 20 def on(event_type, &block) add_handler(event_type, block) self end |