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].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, or :done.
Instance Method Summary collapse
-
#done? ⇒ Boolean
Returns true if this is a done/completion event.
-
#initialize(type:, data: nil) ⇒ StreamEvent
constructor
Creates a new StreamEvent instance.
-
#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.
44 45 46 47 48 49 50 51 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 44 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, or :done.
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.
70 71 72 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 70 def done? @type == :done end |
#text_delta? ⇒ Boolean
Returns true if this is a text delta event.
56 57 58 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 56 def text_delta? @type == :text_delta end |
#to_h ⇒ Hash
Returns a hash representation of the stream event.
77 78 79 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 77 def to_h { type: @type, data: @data } end |
#to_s ⇒ String Also known as: inspect
Returns a human-readable string representation.
84 85 86 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 84 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.
63 64 65 |
# File 'lib/ruby_pi/llm/stream_event.rb', line 63 def tool_call_delta? @type == :tool_call_delta end |