Skip to content

StreamingContext

Manages streaming state during execution.

Class: RobotLab::Streaming::Context

context = RobotLab::Streaming::Context.new(callback: ->(e) { handle(e) })

Constructor

Context.new(callback:, robot: nil, network: nil)

Parameters:

Name Type Description
callback Proc Event handler
robot Robot, nil Current robot
network NetworkRun, nil Network context

Attributes

callback

context.callback  # => Proc

The event handler callback.

robot

context.robot  # => Robot | nil

The currently executing robot.

network

context.network  # => NetworkRun | nil

The network run context.

buffer

context.buffer  # => String

Accumulated text content.

tool_calls

context.tool_calls  # => Array<ToolCallMessage>

Tool calls received during streaming.

Methods

emit

context.emit(event)

Send an event to the callback.

emit_text

context.emit_text(text)

Emit a text delta event.

emit_tool_call

context.emit_tool_call(id:, name:, input:)

Emit a tool call event.

emit_error

context.emit_error(error)

Emit an error event.

complete

context.complete

Signal streaming completion.

for_robot

new_context = context.for_robot(robot)

Create a child context for a specific robot.

Examples

Custom Context

context = RobotLab::Streaming::Context.new(
  callback: ->(event) {
    case event.type
    when :text_delta
      @output << event.text
    when :complete
      process_output(@output)
    end
  }
)

# Pass to robot
robot.run(state: state, streaming: context)

Accumulating Content

context = RobotLab::Streaming::Context.new(
  callback: ->(event) {
    print event.text if event.type == :text_delta
  }
)

robot.run(state: state, streaming: context)

# Access accumulated content
puts "Total content: #{context.buffer}"
puts "Tool calls: #{context.tool_calls.size}"

Network Context

context = RobotLab::Streaming::Context.new(
  callback: ->(event) {
    prefix = event.robot_name ? "[#{event.robot_name}] " : ""
    case event.type
    when :text_delta
      print "#{prefix}#{event.text}"
    when :robot_complete
      puts "\n#{prefix}Complete"
    end
  }
)

network.run(state: state, streaming: context)

See Also