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

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
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.



16
17
18
19
20
# File 'lib/legion/tty/components/tool_call_parser.rb', line 16

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



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

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



39
40
41
42
43
44
45
# File 'lib/legion/tty/components/tool_call_parser.rb', line 39

def flush
  return if @buffer.empty?

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

#resetObject



47
48
49
50
51
# File 'lib/legion/tty/components/tool_call_parser.rb', line 47

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