StreamingContext¶
Manages streaming state during execution.
Class: RobotLab::Streaming::Context¶
Constructor¶
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
Proc |
Event handler |
robot |
Robot, nil |
Current robot |
network |
NetworkRun, nil |
Network context |
Attributes¶
callback¶
The event handler callback.
robot¶
The currently executing robot.
network¶
The network run context.
buffer¶
Accumulated text content.
tool_calls¶
Tool calls received during streaming.
Methods¶
emit¶
Send an event to the callback.
emit_text¶
Emit a text delta event.
emit_tool_call¶
Emit a tool call event.
emit_error¶
Emit an error event.
complete¶
Signal streaming completion.
for_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)