Class: RubyLLM::Agents::StreamEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/agents/stream_event.rb

Overview

Typed event emitted during streaming execution.

When ‘stream_events: true` is passed to an agent call, the stream block receives StreamEvent objects instead of raw RubyLLM chunks. This provides visibility into the full execution lifecycle —text chunks, tool invocations, and errors.

Examples:

Basic usage

MyAgent.call(query: "test", stream_events: true) do |event|
  case event.type
  when :chunk       then print event.data[:content]
  when :tool_start  then puts "Running #{event.data[:tool_name]}..."
  when :tool_end    then puts "Done (#{event.data[:duration_ms]}ms)"
  when :error       then puts "Error: #{event.data[:message]}"
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data = {}) ⇒ StreamEvent

Creates a new StreamEvent

Parameters:

  • type (Symbol)

    The event type

  • data (Hash) (defaults to: {})

    Event-specific data



33
34
35
36
# File 'lib/ruby_llm/agents/stream_event.rb', line 33

def initialize(type, data = {})
  @type = type
  @data = data
end

Instance Attribute Details

#dataHash (readonly)

Returns Event-specific data.

Returns:

  • (Hash)

    Event-specific data



27
28
29
# File 'lib/ruby_llm/agents/stream_event.rb', line 27

def data
  @data
end

#typeSymbol (readonly)

Returns Event type (:chunk, :tool_start, :tool_end, :error).

Returns:

  • (Symbol)

    Event type (:chunk, :tool_start, :tool_end, :error)



24
25
26
# File 'lib/ruby_llm/agents/stream_event.rb', line 24

def type
  @type
end

Instance Method Details

#chunk?Boolean

Returns Whether this is a text chunk event.

Returns:

  • (Boolean)

    Whether this is a text chunk event



39
40
41
# File 'lib/ruby_llm/agents/stream_event.rb', line 39

def chunk?
  @type == :chunk
end

#error?Boolean

Returns Whether this is an error event.

Returns:

  • (Boolean)

    Whether this is an error event



49
50
51
# File 'lib/ruby_llm/agents/stream_event.rb', line 49

def error?
  @type == :error
end

#to_hObject



53
54
55
# File 'lib/ruby_llm/agents/stream_event.rb', line 53

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

#tool_event?Boolean

Returns Whether this is a tool lifecycle event.

Returns:

  • (Boolean)

    Whether this is a tool lifecycle event



44
45
46
# File 'lib/ruby_llm/agents/stream_event.rb', line 44

def tool_event?
  @type == :tool_start || @type == :tool_end
end