Class: Legion::TTY::Components::MessageStream
- Inherits:
-
Object
- Object
- Legion::TTY::Components::MessageStream
- 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
-
#colorize ⇒ Object
Returns the value of attribute colorize.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#highlights ⇒ Object
Returns the value of attribute highlights.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#mute_system ⇒ Object
Returns the value of attribute mute_system.
-
#scroll_offset ⇒ Object
readonly
Returns the value of attribute scroll_offset.
-
#show_numbers ⇒ Object
Returns the value of attribute show_numbers.
-
#show_timestamps ⇒ Object
Returns the value of attribute show_timestamps.
-
#silent_mode ⇒ Object
Returns the value of attribute silent_mode.
-
#truncate_limit ⇒ Object
Returns the value of attribute truncate_limit.
-
#wrap_width ⇒ Object
Returns the value of attribute wrap_width.
Instance Method Summary collapse
- #add_message(role:, content:) ⇒ Object
- #add_tool_call(name:, args: {}, status: :running) ⇒ Object
- #add_tool_panel(panel) ⇒ Object
- #append_streaming(text) ⇒ Object
-
#initialize ⇒ MessageStream
constructor
A new instance of MessageStream.
- #render(width:, height:) ⇒ Object
- #scroll_down(lines = 1) ⇒ Object
- #scroll_position ⇒ Object
- #scroll_up(lines = 1) ⇒ Object
- #update_tool_call(name:, status:, duration: nil, result: nil, error: nil) ⇒ Object
Constructor Details
#initialize ⇒ MessageStream
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
#colorize ⇒ Object
Returns the value of attribute colorize.
12 13 14 |
# File 'lib/legion/tty/components/message_stream.rb', line 12 def colorize @colorize end |
#filter ⇒ Object
Returns the value of attribute filter.
12 13 14 |
# File 'lib/legion/tty/components/message_stream.rb', line 12 def filter @filter end |
#highlights ⇒ Object
Returns the value of attribute highlights.
12 13 14 |
# File 'lib/legion/tty/components/message_stream.rb', line 12 def highlights @highlights end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
11 12 13 |
# File 'lib/legion/tty/components/message_stream.rb', line 11 def @messages end |
#mute_system ⇒ Object
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_offset ⇒ Object (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_numbers ⇒ Object
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_timestamps ⇒ Object
Returns the value of attribute show_timestamps.
12 13 14 |
# File 'lib/legion/tty/components/message_stream.rb', line 12 def @show_timestamps end |
#silent_mode ⇒ Object
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_limit ⇒ Object
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_width ⇒ Object
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 (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_position ⇒ Object
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 |