Class: Legion::TTY::Screens::Chat

Inherits:
Base
  • Object
show all
Defined in:
lib/legion/tty/screens/chat.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

SLASH_COMMANDS =
%w[/help /quit /clear /model /session /cost /export /tools /dashboard /hotkeys /save /load
/sessions /system /delete /plan /palette /extensions /config].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods inherited from Base

#deactivate, #teardown

Constructor Details

#initialize(app, output: $stdout, input_bar: nil) ⇒ Chat

Returns a new instance of Chat.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/tty/screens/chat.rb', line 20

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 = input_bar || build_default_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
end

Instance Attribute Details

#message_streamObject (readonly)

Returns the value of attribute message_stream.



18
19
20
# File 'lib/legion/tty/screens/chat.rb', line 18

def message_stream
  @message_stream
end

#status_barObject (readonly)

Returns the value of attribute status_bar.



18
19
20
# File 'lib/legion/tty/screens/chat.rb', line 18

def status_bar
  @status_bar
end

Instance Method Details

#activateObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/tty/screens/chat.rb', line 34

def activate
  @running = true
  cfg = safe_config
  @status_bar.update(model: cfg[:provider], session: 'default')
  setup_system_prompt
  @message_stream.add_message(
    role: :system,
    content: "Welcome#{", #{cfg[:name]}" if cfg[:name]}. Type /help for commands."
  )
end

#handle_input(key) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/legion/tty/screens/chat.rb', line 122

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



74
75
76
77
78
79
80
81
# File 'lib/legion/tty/screens/chat.rb', line 74

def handle_slash_command(input)
  return nil unless input.start_with?('/')

  cmd = input.split.first
  return nil unless SLASH_COMMANDS.include?(cmd)

  dispatch_slash(cmd, input)
end

#handle_user_message(input) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/tty/screens/chat.rb', line 83

def handle_user_message(input)
  @message_stream.add_message(role: :user, content: input)
  if @plan_mode
    @message_stream.add_message(role: :system, content: '(bookmarked)')
  else
    @message_stream.add_message(role: :assistant, content: '')
    send_to_llm(input)
  end
  render_screen
end

#render(width, height) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/legion/tty/screens/chat.rb', line 114

def render(width, height)
  bar_line = @status_bar.render(width: width)
  divider = Theme.c(:muted, '-' * width)
  stream_height = [height - 2, 1].max
  stream_lines = @message_stream.render(width: width, height: stream_height)
  stream_lines + [divider, bar_line]
end

#runObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legion/tty/screens/chat.rb', line 50

def run
  activate
  while @running
    render_screen
    input = read_input
    break if input.nil?

    if @app.respond_to?(:screen_manager) && @app.screen_manager.overlay
      @app.screen_manager.dismiss_overlay
      next
    end

    result = handle_slash_command(input)
    if result == :quit
      auto_save_session
      @running = false
      break
    elsif result.nil?
      handle_user_message(input) unless input.strip.empty?
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/legion/tty/screens/chat.rb', line 45

def running?
  @running
end

#send_to_llm(message) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/legion/tty/screens/chat.rb', line 94

def send_to_llm(message)
  unless @llm_chat
    @message_stream.append_streaming('LLM not configured. Use /help for commands.')
    return
  end

  @status_bar.update(thinking: true)
  render_screen
  response = @llm_chat.ask(message) do |chunk|
    @status_bar.update(thinking: false)
    @message_stream.append_streaming(chunk.content) if chunk.content
    render_screen
  end
  @status_bar.update(thinking: false)
  track_response_tokens(response)
rescue StandardError => e
  @status_bar.update(thinking: false)
  @message_stream.append_streaming("\n[Error: #{e.message}]")
end