Module: RubyLLM::Protocol::Streaming::FaradayHandlers

Defined in:
lib/ruby_llm/protocol/streaming.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.build(faraday_v1:, on_chunk:, on_failed_response:) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/ruby_llm/protocol/streaming.rb', line 201

def build(faraday_v1:, on_chunk:, on_failed_response:)
  if faraday_v1
    v1_on_data(on_chunk)
  else
    v2_on_data(on_chunk, on_failed_response)
  end
end

.v1_on_data(on_chunk) ⇒ Object



209
210
211
212
213
# File 'lib/ruby_llm/protocol/streaming.rb', line 209

def v1_on_data(on_chunk)
  proc do |chunk, _size|
    on_chunk.call(chunk, nil)
  end
end

.v2_on_data(on_chunk, on_failed_response) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ruby_llm/protocol/streaming.rb', line 215

def v2_on_data(on_chunk, on_failed_response)
  proc do |chunk, _bytes, env|
    # Some adapters do not expose response status during on_data.
    status = env&.status

    if status && status != 200
      on_failed_response.call(chunk, env)
    else
      on_chunk.call(chunk, env)
    end
  end
end