Class: Legion::TTY::Screens::Chat
- Defined in:
- lib/legion/tty/screens/chat.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- SLASH_COMMANDS =
%w[/help /quit /clear /compact /copy /diff /model /session /cost /export /tools /dashboard /hotkeys /save /load /sessions /system /delete /plan /palette /extensions /config /theme /search /stats /personality /undo /history /pin /pins /rename /context /alias /snippet /debug /uptime /bookmark].freeze
- PERSONALITIES =
{ 'default' => 'You are Legion, an async cognition engine and AI assistant. Be helpful and concise.', 'concise' => 'You are Legion. Respond in as few words as possible. No filler.', 'detailed' => 'You are Legion. Provide thorough, detailed explanations with examples when helpful.', 'friendly' => 'You are Legion, a friendly AI companion. Use a warm, conversational tone.', 'technical' => 'You are Legion, a senior engineer. Use precise technical language. Include code examples.' }.freeze
Instance Attribute Summary collapse
-
#message_stream ⇒ Object
readonly
Returns the value of attribute message_stream.
-
#status_bar ⇒ Object
readonly
Returns the value of attribute status_bar.
Attributes inherited from Base
Instance Method Summary collapse
-
#activate ⇒ Object
rubocop:enable Metrics/AbcSize.
- #handle_input(key) ⇒ Object
-
#handle_slash_command(input) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #handle_user_message(input) ⇒ Object
-
#initialize(app, output: $stdout, input_bar: nil) ⇒ Chat
constructor
rubocop:disable Metrics/AbcSize.
- #render(width, height) ⇒ Object
-
#run ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #running? ⇒ Boolean
- #send_to_llm(message) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(app, output: $stdout, input_bar: nil) ⇒ Chat
rubocop:disable Metrics/AbcSize
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/legion/tty/screens/chat.rb', line 31 def initialize(app, output: $stdout, input_bar: nil) super(app) @output = output @message_stream = Components::MessageStream.new @status_bar = Components::StatusBar.new @running = false @input_bar = || @llm_chat = app.respond_to?(:llm_chat) ? app.llm_chat : nil @token_tracker = Components::TokenTracker.new(provider: detect_provider) @session_store = SessionStore.new @session_name = 'default' @plan_mode = false @pinned_messages = [] @aliases = {} @snippets = {} @debug_mode = false @session_start = Time.now end |
Instance Attribute Details
#message_stream ⇒ Object (readonly)
Returns the value of attribute message_stream.
28 29 30 |
# File 'lib/legion/tty/screens/chat.rb', line 28 def @message_stream end |
#status_bar ⇒ Object (readonly)
Returns the value of attribute status_bar.
28 29 30 |
# File 'lib/legion/tty/screens/chat.rb', line 28 def @status_bar end |
Instance Method Details
#activate ⇒ Object
rubocop:enable Metrics/AbcSize
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/legion/tty/screens/chat.rb', line 52 def activate @running = true cfg = safe_config @status_bar.update(model: cfg[:provider], session: 'default') setup_system_prompt @message_stream.( role: :system, content: "Welcome#{", #{cfg[:name]}" if cfg[:name]}. Type /help for commands." ) end |
#handle_input(key) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/legion/tty/screens/chat.rb', line 146 def handle_input(key) case key when :up @message_stream.scroll_up :handled when :down @message_stream.scroll_down :handled else :pass end end |
#handle_slash_command(input) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/legion/tty/screens/chat.rb', line 92 def handle_slash_command(input) return nil unless input.start_with?('/') cmd = input.split.first unless SLASH_COMMANDS.include?(cmd) = @aliases[cmd] return nil unless return handle_slash_command("#{} #{input.split(nil, 2)[1]}".strip) end dispatch_slash(cmd, input) end |
#handle_user_message(input) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/legion/tty/screens/chat.rb', line 106 def (input) @message_stream.(role: :user, content: input) if @plan_mode @message_stream.(role: :system, content: '(bookmarked)') else @message_stream.(role: :assistant, content: '') send_to_llm(input) end render_screen end |
#render(width, height) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/legion/tty/screens/chat.rb', line 133 def render(width, height) = @status_bar.render(width: width) divider = Theme.c(:muted, '-' * width) dbg = debug_segment extra_rows = dbg ? 1 : 0 stream_height = [height - 2 - extra_rows, 1].max stream_lines = @message_stream.render(width: width, height: stream_height) @status_bar.update(scroll: @message_stream.scroll_position) lines = stream_lines + [divider, ] lines << dbg if dbg lines end |
#run ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/legion/tty/screens/chat.rb', line 68 def run activate while @running render_screen input = read_input break if input.nil? if @app.respond_to?(:screen_manager) && @app.screen_manager. @app.screen_manager. next end result = handle_slash_command(input) if result == :quit auto_save_session @running = false break elsif result.nil? (input) unless input.strip.empty? end end end |
#running? ⇒ Boolean
63 64 65 |
# File 'lib/legion/tty/screens/chat.rb', line 63 def running? @running end |
#send_to_llm(message) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/legion/tty/screens/chat.rb', line 117 def send_to_llm() unless @llm_chat || daemon_available? @message_stream.append_streaming('LLM not configured. Use /help for commands.') return end if daemon_available? send_via_daemon() else send_via_direct() end rescue StandardError => e @status_bar.update(thinking: false) @message_stream.append_streaming("\n[Error: #{e.}]") end |