Class: Kernai::StreamParser
- Inherits:
-
Object
- Object
- Kernai::StreamParser
- Defined in:
- lib/kernai/stream_parser.rb
Constant Summary collapse
- OPEN_TAG_START =
'<block'- CLOSE_TAG =
'</block>'- SHORTHAND_TYPES =
Block::TYPES.map(&:to_s).freeze
- NESTED_OPEN_LITERAL =
Used by locate_matching_close to count nested opens. We match any ‘<block` token followed by whitespace or `>` — even ones embedded in escaped JSON strings (`<block type="…">`) — because the LLM often pastes literal examples of blocks inside plan JSON payloads.
'<block'- NESTED_OPEN_PATTERN =
/<block(?:\s|>)/
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize ⇒ StreamParser
constructor
A new instance of StreamParser.
- #on(event, &block) ⇒ Object
- #push(chunk) ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize ⇒ StreamParser
Returns a new instance of StreamParser.
18 19 20 21 |
# File 'lib/kernai/stream_parser.rb', line 18 def initialize @callbacks = {} reset end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
16 17 18 |
# File 'lib/kernai/stream_parser.rb', line 16 def state @state end |
Instance Method Details
#flush ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kernai/stream_parser.rb', line 32 def flush case @state when :text # nothing to flush when :tag_open emit(:text_chunk, @tag_buffer) unless @tag_buffer.empty? @tag_buffer = +'' when :block_content emit(:text_chunk, @content_buffer) unless @content_buffer.empty? @content_buffer = +'' @full_block_content = +'' @tag_buffer = +'' end @buffer = +'' @state = :text end |
#on(event, &block) ⇒ Object
23 24 25 |
# File 'lib/kernai/stream_parser.rb', line 23 def on(event, &block) @callbacks[event] = block end |
#push(chunk) ⇒ Object
27 28 29 30 |
# File 'lib/kernai/stream_parser.rb', line 27 def push(chunk) @buffer << chunk consume end |
#reset ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/kernai/stream_parser.rb', line 49 def reset @state = :text @buffer = +'' @tag_buffer = +'' @content_buffer = +'' @full_block_content = +'' @current_type = nil @current_name = nil @close_tag = nil end |