Class: Legion::TTY::Screens::Chat
- Includes:
- CustomCommands, ExportCommands, MessageCommands, ModelCommands, SessionCommands, UiCommands
- Defined in:
- lib/legion/tty/screens/chat.rb,
lib/legion/tty/screens/chat/ui_commands.rb,
lib/legion/tty/screens/chat/model_commands.rb,
lib/legion/tty/screens/chat/custom_commands.rb,
lib/legion/tty/screens/chat/export_commands.rb,
lib/legion/tty/screens/chat/message_commands.rb,
lib/legion/tty/screens/chat/session_commands.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Modules: CustomCommands, ExportCommands, MessageCommands, ModelCommands, SessionCommands, UiCommands
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 /grep /stats /personality /undo /history /pin /pins /rename /context /alias /snippet /debug /uptime /time /bookmark /welcome /tips /wc /import /mute /autosave /react /macro /tag /tags /repeat /count /template /fav /favs /log /version /focus /retry /merge /sort /chain /info /scroll /summary /prompt /reset /replace /highlight /multiline /annotate /annotations /filter /truncate /tee /pipe /archive /archives /calc /rand /echo /env /ls /pwd /wrap /number /speak /silent /color /timestamps /top /bottom /head /tail /draft /revise /mark /freq].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
Constants included from CustomCommands
Constants included from UiCommands
UiCommands::CALC_MATH_PATTERN, UiCommands::CALC_SAFE_PATTERN, UiCommands::FREQ_ROW_FMT, UiCommands::FREQ_STOP_WORDS, UiCommands::HELP_TEXT, UiCommands::TIPS
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, Metrics/MethodLength.
- #handle_input(key) ⇒ Object
-
#handle_slash_command(input) ⇒ Object
rubocop:enable Metrics/AbcSize.
- #handle_user_message(input) ⇒ Object
-
#initialize(app, output: $stdout, input_bar: nil) ⇒ Chat
constructor
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #render(width, height) ⇒ Object
-
#run ⇒ Object
rubocop:disable Metrics/AbcSize.
- #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, Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/legion/tty/screens/chat.rb', line 61 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 = {} @macros = {} @debug_mode = false @session_start = Time.now @muted_system = false @autosave_enabled = false @autosave_interval = 60 @last_autosave = Time.now @recording_macro = nil @macro_buffer = [] @last_command = nil @focus_mode = false @last_user_input = nil @highlights = [] @multiline_mode = false @speak_mode = false @silent_mode = false @draft = nil end |
Instance Attribute Details
#message_stream ⇒ Object (readonly)
Returns the value of attribute message_stream.
58 59 60 |
# File 'lib/legion/tty/screens/chat.rb', line 58 def @message_stream end |
#status_bar ⇒ Object (readonly)
Returns the value of attribute status_bar.
58 59 60 |
# File 'lib/legion/tty/screens/chat.rb', line 58 def @status_bar end |
Instance Method Details
#activate ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/legion/tty/screens/chat.rb', line 97 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." ) @status_bar.update(message_count: @message_stream..size) end |
#handle_input(key) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/legion/tty/screens/chat.rb', line 192 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/legion/tty/screens/chat.rb', line 138 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 result = dispatch_slash(cmd, input) @last_command = input if cmd != '/repeat' record_macro_step(input, cmd, result) result end |
#handle_user_message(input) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/legion/tty/screens/chat.rb', line 155 def (input) @last_user_input = input @message_stream.(role: :user, content: input) ("[user] #{input}") if @tee_path if @plan_mode @message_stream.(role: :system, content: '(bookmarked)') else @message_stream.(role: :assistant, content: '') send_to_llm(input) end @status_bar.update(message_count: @message_stream..size) check_autosave render_screen end |
#render(width, height) ⇒ Object
186 187 188 189 190 |
# File 'lib/legion/tty/screens/chat.rb', line 186 def render(width, height) return render_focus(width, height) if @focus_mode render_normal(width, height) end |
#run ⇒ Object
rubocop:disable Metrics/AbcSize
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/legion/tty/screens/chat.rb', line 114 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
109 110 111 |
# File 'lib/legion/tty/screens/chat.rb', line 109 def running? @running end |
#send_to_llm(message) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/legion/tty/screens/chat.rb', line 170 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 |