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].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)

  • data (Object) (defaults to: nil)

    event payload

Raises:

  • (ArgumentError)

    if the type is not recognized



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

#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, or :done.

Returns:

  • (Symbol)

    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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

def text_delta?
  @type == :text_delta
end

#to_hHash

Returns a hash representation of the stream event.

Returns:

  • (Hash)


77
78
79
# File 'lib/ruby_pi/llm/stream_event.rb', line 77

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

#to_sString Also known as: inspect

Returns a human-readable string representation.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


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

def tool_call_delta?
  @type == :tool_call_delta
end