Class: Legion::TTY::Components::ToolCallParser

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/tool_call_parser.rb

Constant Summary collapse

OPEN_TAG =
'<tool_call>'
CLOSE_TAG =
'</tool_call>'
MAX_BUFFER =
4096

Instance Method Summary collapse

Constructor Details

#initialize(on_text:, on_tool_call:) ⇒ ToolCallParser

Returns a new instance of ToolCallParser.



13
14
15
16
17
# File 'lib/legion/tty/components/tool_call_parser.rb', line 13

def initialize(on_text:, on_tool_call:)
  @on_text = on_text
  @on_tool_call = on_tool_call
  reset
end

Instance Method Details

#feed(text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/tty/components/tool_call_parser.rb', line 19

def feed(text)
  @working = @buffer + text
  @buffer = +''

  loop do
    break if @working.empty?

    if @state == :passthrough
      break unless advance_passthrough?
    else
      break unless advance_buffering?
    end
  end

  @buffer = @working
end

#flushObject



36
37
38
39
40
41
42
# File 'lib/legion/tty/components/tool_call_parser.rb', line 36

def flush
  return if @buffer.empty?

  @on_text.call(@buffer)
  @buffer = +''
  @state = :passthrough
end

#resetObject



44
45
46
47
48
# File 'lib/legion/tty/components/tool_call_parser.rb', line 44

def reset
  @buffer = +''
  @working = +''
  @state = :passthrough
end