Class: LLM::EventStream::Parser
- Inherits:
-
Object
- Object
- LLM::EventStream::Parser
- Defined in:
- lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb
Instance Method Summary collapse
-
#<<(event) ⇒ void
Append an event to the internal buffer.
-
#body ⇒ String
Returns the internal buffer.
-
#free ⇒ void
Free the internal buffer.
- #initialize ⇒ LLM::EventStream::Parser constructor
-
#on(evtname, &block) ⇒ void
Subscribe to an event.
-
#register(visitor) ⇒ void
Register a visitor.
Constructor Details
#initialize ⇒ LLM::EventStream::Parser
9 10 11 12 13 14 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb', line 9 def initialize @buffer = StringIO.new @events = Hash.new { |h, k| h[k] = [] } @offset = 0 @visitors = [] end |
Instance Method Details
#<<(event) ⇒ void
This method returns an undefined value.
Append an event to the internal buffer
36 37 38 39 40 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb', line 36 def <<(event) io = StringIO.new(event) IO.copy_stream io, @buffer each_line { parse!(_1) } end |
#body ⇒ String
Returns the internal buffer
45 46 47 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb', line 45 def body @buffer.string end |
#free ⇒ void
This method returns an undefined value.
Free the internal buffer
52 53 54 55 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb', line 52 def free @buffer.truncate(0) @buffer.rewind end |
#on(evtname, &block) ⇒ void
This method returns an undefined value.
Subscribe to an event
29 30 31 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb', line 29 def on(evtname, &block) @events[evtname.to_s] << block end |
#register(visitor) ⇒ void
This method returns an undefined value.
Register a visitor
20 21 22 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb', line 20 def register(visitor) @visitors << visitor end |