Class: OpenAI::Responses::IncrementalResponse
- Inherits:
-
Object
- Object
- OpenAI::Responses::IncrementalResponse
- Includes:
- WebSocket::Protocol
- Defined in:
- lib/openai/helpers/responses_websocket/incremental.rb
Overview
Optional, caller-fed provisional output for one WebSocket lane. Feed the events returned by lane.receive; use a separate instance per lane. This helper never reads, closes, or writes to a connection. Tools are data. Terminal events stay exact; provisional facts never manufacture a Response.
Instance Attribute Summary collapse
-
#phase ⇒ Object
readonly
Returns the value of attribute phase.
Instance Method Summary collapse
- #add(event) ⇒ Object
-
#detailed_snapshot ⇒ Object
Detached observed facts, including sparse indices and response metadata.
-
#initialize ⇒ IncrementalResponse
constructor
A new instance of IncrementalResponse.
-
#output ⇒ Object
Detached typed output: the truthful contiguous projectable prefix.
- #reset ⇒ Object
-
#terminal_event ⇒ Object
The exact consumed completion, failure, incomplete, or error event.
Constructor Details
#initialize ⇒ IncrementalResponse
Returns a new instance of IncrementalResponse.
33 34 35 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 33 def initialize reset end |
Instance Attribute Details
#phase ⇒ Object (readonly)
Returns the value of attribute phase.
31 32 33 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 31 def phase @phase end |
Instance Method Details
#add(event) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 108 def add(event) return if event.is_a?(OpenAI::Responses::UnknownServerEvent) type = event[:type].to_s if %w[response.completed response.failed response.incomplete error].include?(type) reset @terminal_event = copy(event) @phase = :terminal return end return if @phase == :terminal && type != "response.created" if %w[response.created response.queued response.in_progress].include?(type) response = copy(event)[:response] unless response.is_a?(OpenAI::Responses::Response) && (response[:output].nil? || response[:output].is_a?(Array)) raise SessionError, "Invalid Responses WebSocket created response." end reset if type == "response.created" return if @phase == :unavailable data = response.deep_to_h @response = data.reject { |key, _| key == :output } @stream_id = copy(event[:stream_id]) if data[:output].is_a?(Array) || type == "response.created" @items = {} @ids = {} @command_bounds = {} (data[:output] || []).each_with_index do |item, index| replace_item(index, item) break if @phase == :unavailable end end @phase = :provisional unless @phase == :unavailable return end return if @phase == :unavailable data = copy(event).deep_to_h @stream_id ||= data[:stream_id] if (status = STATUS_ITEMS[type]) record = item_for(data, status.first) record[:item][:status] = status.last if record else apply_output(type, data, event.class) end nil end |
#detailed_snapshot ⇒ Object
Detached observed facts, including sparse indices and response metadata. This is not a complete Response. No absent item/part type is inferred.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 60 def detailed_snapshot return if @phase.nil? || @phase == :unavailable if @phase == :terminal data = @terminal_event.deep_to_h response = data[:response] items = response.is_a?(Hash) && response[:output].is_a?(Array) ? response[:output] : [] records = items.map { |item| make_record(item, full: true) } rows = records.each_with_index.map { |record, index| detailed_item(index, record) } return copy( phase: @phase, stream_id: data[:stream_id], response_id: response.is_a?(Hash) ? response[:id] : nil, terminal_type: data[:type], response: response.is_a?(Hash) ? response.reject { |key, _| key == :output } : nil, output: rows, output_text: observed_text(records) ) end indices = @items.keys.sort copy( phase: @phase, stream_id: @stream_id, response_id: @response && @response[:id], terminal_type: nil, response: @response, output: indices.map { |index| detailed_item(index, @items.fetch(index)) }, output_text: observed_text(indices.map { |index| @items.fetch(index) }) ) end |
#output ⇒ Object
Detached typed output: the truthful contiguous projectable prefix. Sparse or incomplete records stop this prefix; detailed_snapshot retains their actual indices. Full reads cost time proportional to retained data. Use original deltas for per-event progress and read snapshots at boundaries.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 41 def output return unless @phase == :provisional result = [] while (record = @items[result.length]) break unless record[:full] result << OpenAI::Internal::Type::Converter.coerce( OpenAI::Responses::ResponseOutputItem, project_item(record) ) end result end |
#reset ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 97 def reset @phase = nil @items = {} @ids = {} @command_bounds = {} @response = nil @stream_id = nil @terminal_event = nil nil end |
#terminal_event ⇒ Object
The exact consumed completion, failure, incomplete, or error event.
93 94 95 |
# File 'lib/openai/helpers/responses_websocket/incremental.rb', line 93 def terminal_event @terminal_event && copy(@terminal_event) end |