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
-
#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.
-
#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.
17 18 19 20 21 22 23 24 25 |
# File 'lib/legion/tty/components/message_stream.rb', line 17 def initialize @messages = [] @scroll_offset = 0 @mute_system = false @silent_mode = false @highlights = [] @wrap_width = nil @show_numbers = false end |
Instance Attribute Details
#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 |
#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
27 28 29 |
# File 'lib/legion/tty/components/message_stream.rb', line 27 def (role:, content:) @messages << { role: role, content: content, tool_panels: [], timestamp: Time.now } end |
#add_tool_call(name:, args: {}, status: :running) ⇒ Object
43 44 45 46 47 |
# File 'lib/legion/tty/components/message_stream.rb', line 43 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
37 38 39 40 41 |
# File 'lib/legion/tty/components/message_stream.rb', line 37 def add_tool_panel(panel) return if @messages.empty? @messages.last[:tool_panels] << panel end |
#append_streaming(text) ⇒ Object
31 32 33 34 35 |
# File 'lib/legion/tty/components/message_stream.rb', line 31 def append_streaming(text) return if @messages.empty? @messages.last[:content] = @messages.last[:content] + text end |
#render(width:, height:) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/legion/tty/components/message_stream.rb', line 66 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] || [] @last_visible_count = result.size result end |
#scroll_down(lines = 1) ⇒ Object
62 63 64 |
# File 'lib/legion/tty/components/message_stream.rb', line 62 def scroll_down(lines = 1) @scroll_offset = [@scroll_offset - lines, 0].max end |
#scroll_position ⇒ Object
77 78 79 |
# File 'lib/legion/tty/components/message_stream.rb', line 77 def scroll_position { current: @scroll_offset, total: @messages.size, visible: @last_visible_count || 0 } end |
#scroll_up(lines = 1) ⇒ Object
58 59 60 |
# File 'lib/legion/tty/components/message_stream.rb', line 58 def scroll_up(lines = 1) @scroll_offset += lines end |
#update_tool_call(name:, status:, duration: nil, result: nil, error: nil) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/legion/tty/components/message_stream.rb', line 49 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 |