Class: RubyPi::LLM::StreamEvent

Inherits:
Object
  • Object
show all
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.

Examples:

Processing streaming events

provider.complete(messages: msgs, stream: true) do |event|
  case event.type
  when :text_delta
    print event.data
  when :tool_call_delta
    accumulate_tool_call(event.data)
  when :done
    puts "\nStream complete"
  end
end

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

Instance Method Summary collapse

Constructor Details

#initialize(type:, data: nil) ⇒ StreamEvent

Creates a new StreamEvent instance.

Parameters:

  • type (Symbol)

    event type (:text_delta, :tool_call_delta, :done, :retry_start, :fallback_start)

  • data (Object) (defaults to: nil)

    event payload

Raises:

  • (ArgumentError)

    if the type is not recognized



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

#dataObject (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.

Returns:

  • (Object)

    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

#typeSymbol (readonly)

Returns the type of stream event — one of :text_delta, :tool_call_delta, :done, :retry_start, or :fallback_start.

Returns:

  • (Symbol)

    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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_pi/llm/stream_event.rb', line 57

def text_delta?
  @type == :text_delta
end

#to_hHash

Returns a hash representation of the stream event.

Returns:

  • (Hash)


94
95
96
# File 'lib/ruby_pi/llm/stream_event.rb', line 94

def to_h
  { type: @type, data: @data }
end

#to_sString Also known as: inspect

Returns a human-readable string representation.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


64
65
66
# File 'lib/ruby_pi/llm/stream_event.rb', line 64

def tool_call_delta?
  @type == :tool_call_delta
end