Class: RubyPi::LLM::StreamEvent
- Inherits:
-
Object
- Object
- RubyPi::LLM::StreamEvent
- Defined in:
- lib/ruby_pi/llm/stream_event.rb
Overview
An event yielded during a streaming completion. Events carry a type indicating what kind of data they contain and a data payload with the actual content.
Constant Summary collapse
- VALID_TYPES =
Valid event types for stream events.
%i[text_delta tool_call_delta done retry_start fallback_start].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The event payload.
-
#type ⇒ Symbol
readonly
The type of stream event — one of :text_delta, :tool_call_delta, :done, :retry_start, or :fallback_start.
Instance Method Summary collapse
-
#done? ⇒ Boolean
Returns true if this is a done/completion event.
-
#fallback_start? ⇒ Boolean
Returns true if this is a fallback_start event, signaling that the primary provider failed mid-stream and the fallback provider is taking over.
-
#initialize(type:, data: nil) ⇒ StreamEvent
constructor
Creates a new StreamEvent instance.
-
#retry_start? ⇒ Boolean
Returns true when a failed streaming attempt is being discarded and the same provider is about to retry from the beginning.
-
#text_delta? ⇒ Boolean
Returns true if this is a text delta event.
-
#to_h ⇒ Hash
Returns a hash representation of the stream event.
-
#to_s ⇒ String
(also: #inspect)
Returns a human-readable string representation.
-
#tool_call_delta? ⇒ Boolean
Returns true if this is a tool call delta event.
Constructor Details
#initialize(type:, data: nil) ⇒ StreamEvent
Creates a new StreamEvent instance.
45 46 47 48 49 50 51 52 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 45 def initialize(type:, data: nil) unless VALID_TYPES.include?(type) raise ArgumentError, "Invalid stream event type: #{type.inspect}. Must be one of: #{VALID_TYPES.join(', ')}" end @type = type @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the event payload. For :text_delta this is a String fragment; for :tool_call_delta it is a Hash with partial tool call data; for :done it is nil or a final summary hash.
37 38 39 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 37 def data @data end |
#type ⇒ Symbol (readonly)
Returns the type of stream event — one of :text_delta, :tool_call_delta, :done, :retry_start, or :fallback_start.
32 33 34 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 32 def type @type end |
Instance Method Details
#done? ⇒ Boolean
Returns true if this is a done/completion event.
71 72 73 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 71 def done? @type == :done end |
#fallback_start? ⇒ Boolean
Returns true if this is a fallback_start event, signaling that the primary provider failed mid-stream and the fallback provider is taking over. Consumers should clear any partial output rendered from the failed primary.
87 88 89 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 87 def fallback_start? @type == :fallback_start end |
#retry_start? ⇒ Boolean
Returns true when a failed streaming attempt is being discarded and the same provider is about to retry from the beginning.
77 78 79 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 77 def retry_start? @type == :retry_start end |
#text_delta? ⇒ Boolean
Returns true if this is a text delta event.
57 58 59 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 57 def text_delta? @type == :text_delta end |
#to_h ⇒ Hash
Returns a hash representation of the stream event.
94 95 96 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 94 def to_h { type: @type, data: @data } end |
#to_s ⇒ String Also known as: inspect
Returns a human-readable string representation.
101 102 103 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 101 def to_s "#<RubyPi::LLM::StreamEvent type=#{@type.inspect} data=#{@data.inspect}>" end |
#tool_call_delta? ⇒ Boolean
Returns true if this is a tool call delta event.
64 65 66 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 64 def tool_call_delta? @type == :tool_call_delta end |