Class: OpenAICompatibleErrors::SSEInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/openai_compatible_errors/sse.rb

Constant Summary collapse

MAX_EVENT_BYTES =
65_536
MAX_BUFFER_BYTES =
131_072
RESPONSE_TERMINALS =
%w[response.completed response.incomplete response.failed].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_event_bytes: MAX_EVENT_BYTES, max_buffer_bytes: MAX_BUFFER_BYTES, include_provider_message: false) ⇒ SSEInspector

Returns a new instance of SSEInspector.



34
35
36
37
38
39
40
41
42
# File 'lib/openai_compatible_errors/sse.rb', line 34

def initialize(max_event_bytes: MAX_EVENT_BYTES, max_buffer_bytes: MAX_BUFFER_BYTES,
               include_provider_message: false)
  @max_event_bytes = bounded_limit(max_event_bytes, MAX_EVENT_BYTES, 1, 1_048_576)
  @max_buffer_bytes = bounded_limit(max_buffer_bytes, MAX_BUFFER_BYTES, 1, 2_097_152)
  @include_provider_message = (include_provider_message == true)
  @buffer = +"".b
  @at_start = true
  @state = StreamState.new
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



32
33
34
# File 'lib/openai_compatible_errors/sse.rb', line 32

def state
  @state
end

Class Method Details

.inspect_each(enum, inspector: new) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openai_compatible_errors/sse.rb', line 77

def self.inspect_each(enum, inspector: new)
  return enum_for(__method__, enum, inspector: inspector) unless block_given?

  begin
    enum.each do |chunk|
      inspector.feed(chunk)
      yield chunk
    end
    inspector.close
  rescue StandardError
    inspector.fail
    raise
  end
  inspector.state
end

Instance Method Details

#closeObject



63
64
65
66
67
68
69
70
# File 'lib/openai_compatible_errors/sse.rb', line 63

def close
  return @state unless open?

  consume_event(@buffer) unless @buffer.empty?
  @buffer = +"".b
  finish(:unexpected_eof, stream_error) if open?
  @state
end

#failObject



72
73
74
75
# File 'lib/openai_compatible_errors/sse.rb', line 72

def fail
  finish(:error, stream_error) if open?
  @state
end

#feed(chunk) ⇒ Object

Raises:

  • (TypeError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openai_compatible_errors/sse.rb', line 44

def feed(chunk)
  return @state unless open?
  raise TypeError, "SSE chunks must be String instances" unless chunk.is_a?(String)

  @buffer << chunk.b
  return protocol_error! if @buffer.bytesize > @max_buffer_bytes

  loop do
    boundary = event_boundary(@buffer)
    break unless boundary

    raw, offset = boundary
    @buffer = @buffer.byteslice(offset, @buffer.bytesize - offset) || +"".b
    consume_event(raw)
    break unless open?
  end
  @state
end