Class: Legion::TTY::Components::MessageStream

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

HIGHLIGHT_COLOR =
"\e[1;33m"
HIGHLIGHT_RESET =
"\e[0m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessageStream

Returns a new instance of MessageStream.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/tty/components/message_stream.rb', line 18

def initialize
  @messages = []
  @scroll_offset = 0
  @mute_system = false
  @silent_mode = false
  @highlights = []
  @wrap_width = nil
  @show_numbers = false
  @colorize = true
  @show_timestamps = true
end

Instance Attribute Details

#colorizeObject

Returns the value of attribute colorize.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def colorize
  @colorize
end

#filterObject

Returns the value of attribute filter.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def filter
  @filter
end

#highlightsObject

Returns the value of attribute highlights.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def highlights
  @highlights
end

#messagesObject (readonly)

Returns the value of attribute messages.



11
12
13
# File 'lib/legion/tty/components/message_stream.rb', line 11

def messages
  @messages
end

#mute_systemObject

Returns the value of attribute mute_system.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def mute_system
  @mute_system
end

#scroll_offsetObject (readonly)

Returns the value of attribute scroll_offset.



11
12
13
# File 'lib/legion/tty/components/message_stream.rb', line 11

def scroll_offset
  @scroll_offset
end

#show_numbersObject

Returns the value of attribute show_numbers.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def show_numbers
  @show_numbers
end

#show_timestampsObject

Returns the value of attribute show_timestamps.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def show_timestamps
  @show_timestamps
end

#silent_modeObject

Returns the value of attribute silent_mode.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def silent_mode
  @silent_mode
end

#truncate_limitObject

Returns the value of attribute truncate_limit.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def truncate_limit
  @truncate_limit
end

#wrap_widthObject

Returns the value of attribute wrap_width.



12
13
14
# File 'lib/legion/tty/components/message_stream.rb', line 12

def wrap_width
  @wrap_width
end

Instance Method Details

#add_message(role:, content:) ⇒ Object



30
31
32
# File 'lib/legion/tty/components/message_stream.rb', line 30

def add_message(role:, content:)
  @messages << { role: role, content: content, tool_panels: [], timestamp: Time.now }
end

#add_tool_call(name:, args: {}, status: :running) ⇒ Object



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

def add_tool_call(name:, args: {}, status: :running)
  require_relative 'tool_panel'
  panel = ToolPanel.new(name: name, args: args, status: status)
  @messages << { role: :tool, content: panel, tool_panel: true }
end

#add_tool_panel(panel) ⇒ Object



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

def add_tool_panel(panel)
  return if @messages.empty?

  @messages.last[:tool_panels] << panel
end

#append_streaming(text) ⇒ Object



34
35
36
37
38
# File 'lib/legion/tty/components/message_stream.rb', line 34

def append_streaming(text)
  return if @messages.empty?

  @messages.last[:content] = @messages.last[:content] + text
end

#render(width:, height:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/legion/tty/components/message_stream.rb', line 69

def render(width:, height:)
  effective_width = @wrap_width || width
  all_lines = build_all_lines(effective_width)
  total = all_lines.size
  start_idx = [total - height - @scroll_offset, 0].max
  start_idx = [start_idx, total].min
  result = all_lines[start_idx, height] || []
  result = result.map { |l| strip_ansi(l) } unless @colorize
  @last_visible_count = result.size
  result
end

#scroll_down(lines = 1) ⇒ Object



65
66
67
# File 'lib/legion/tty/components/message_stream.rb', line 65

def scroll_down(lines = 1)
  @scroll_offset = [@scroll_offset - lines, 0].max
end

#scroll_positionObject



81
82
83
# File 'lib/legion/tty/components/message_stream.rb', line 81

def scroll_position
  { current: @scroll_offset, total: @messages.size, visible: @last_visible_count || 0 }
end

#scroll_up(lines = 1) ⇒ Object



61
62
63
# File 'lib/legion/tty/components/message_stream.rb', line 61

def scroll_up(lines = 1)
  @scroll_offset += lines
end

#update_tool_call(name:, status:, duration: nil, result: nil, error: nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/legion/tty/components/message_stream.rb', line 52

def update_tool_call(name:, status:, duration: nil, result: nil, error: nil)
  tool_msg = @messages.reverse.find do |m|
    m[:tool_panel] && m[:content].is_a?(ToolPanel) && m[:content].instance_variable_get(:@name) == name
  end
  return unless tool_msg

  apply_tool_panel_update(tool_msg[:content], status: status, duration: duration, result: result, error: error)
end