Class: RCrewAI::SSEParser
- Inherits:
-
Object
- Object
- RCrewAI::SSEParser
- Defined in:
- lib/rcrewai/sse_parser.rb
Overview
Minimal Server-Sent Events line parser. Supports LF and CRLF line terminators (sufficient for OpenAI, Anthropic, Google, and well-behaved MCP HTTP servers). Lone-CR terminators are NOT handled — see https://html.spec.whatwg.org/multipage/server-sent-events.html if that becomes a requirement. Feed bytes via #feed(chunk); yields { event: String, data: String } per complete event.
Instance Method Summary collapse
- #feed(chunk) ⇒ Object
-
#initialize(&block) ⇒ SSEParser
constructor
A new instance of SSEParser.
Constructor Details
Instance Method Details
#feed(chunk) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rcrewai/sse_parser.rb', line 18 def feed(chunk) chunk = chunk.dup.force_encoding(Encoding::UTF_8) unless chunk.encoding == Encoding::UTF_8 @buffer << chunk while (idx = @buffer.index("\n")) line = @buffer.slice!(0, idx + 1).chomp if line.empty? dispatch elsif line.start_with?(':') # comment line, ignore elsif (colon = line.index(':')) field = line[0...colon] value = line[(colon + 1)..] value = value[1..] if value.start_with?(' ') handle_field(field, value) else handle_field(line, '') end end end |