Class: BruteCLI::CLIEventHandler

Inherits:
Brute::Events::Handler
  • Object
show all
Defined in:
lib/brute_cli/cli_event_handler.rb

Constant Summary collapse

SAVE_CURSOR =
"\e7"
RESTORE_CURSOR =
"\e8"
CLEAR_TO_END =
"\e[J"
DEBUG_LOG =
File.join(Dir.home, ".brute", "debug.log")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inner, terminal:, spinner:, streamer:) ⇒ CLIEventHandler

Returns a new instance of CLIEventHandler.



16
17
18
19
20
21
22
# File 'lib/brute_cli/cli_event_handler.rb', line 16

def initialize(inner, terminal:, spinner:, streamer:)
  super(inner)
  @terminal = terminal
  @spinner  = spinner
  @streamer = streamer
  @metadata = { tokens: {}, timing: {}, tool_calls: 0 }
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



14
15
16
# File 'lib/brute_cli/cli_event_handler.rb', line 14

def 
  @metadata
end

Instance Method Details

#<<(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/brute_cli/cli_event_handler.rb', line 24

def <<(event)
  h = event.is_a?(Hash) ? event : event.to_h
  type = h[:type]
  data = h[:data]

  case type
  when :content
    on_content(data)

  when :reasoning
    on_reasoning(data)

  when :tool_call_start
    calls = data.map do |tc|
      { name: tc[:name], arguments: tc[:arguments] }
    end
    on_tool_call_start(calls)

  when :tool_result
    on_tool_result(data[:name], data[:content])

  when :log, :error, :assistant_complete
    # ignored
  end

  super
end

#flush_contentObject

Flush any buffered streamed content through the markdown renderer. Call at the end of an agent turn, before printing stats.



70
71
72
# File 'lib/brute_cli/cli_event_handler.rb', line 70

def flush_content
  @streamer.flush
end

#pause_spinner(&block) ⇒ Object



62
63
64
65
66
# File 'lib/brute_cli/cli_event_handler.rb', line 62

def pause_spinner(&block)
  stop_spinner
  yield
  start_spinner
end

#reset_contentObject

Reset streamer state for the next agent turn.



75
76
77
# File 'lib/brute_cli/cli_event_handler.rb', line 75

def reset_content
  @streamer.reset
end

#start_spinnerObject



52
53
54
55
56
# File 'lib/brute_cli/cli_event_handler.rb', line 52

def start_spinner
  stop_spinner

  @spinner.start
end

#stop_spinnerObject



58
59
60
# File 'lib/brute_cli/cli_event_handler.rb', line 58

def stop_spinner
  @spinner.stop if @spinner.spinning?
end