Class: TUI::Screens::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/tui/screens/chat.rb

Constant Summary collapse

INPUT_HEIGHT =
3
MAX_INPUT_LENGTH =
10_000
PRINTABLE_CHAR =
/\A[[:print:]]\z/
ROLE_USER =
"user"
ROLE_ASSISTANT =
"assistant"
ROLE_LABELS =
{ROLE_USER => "You", ROLE_ASSISTANT => "Anima"}.freeze
SCROLL_STEP =
1
MOUSE_SCROLL_STEP =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cable_client:, message_store: nil) ⇒ Chat

Returns a new instance of Chat.

Parameters:



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

def initialize(cable_client:, message_store: nil)
  @cable_client = cable_client
  @message_store = message_store || MessageStore.new
  @input = ""
  @loading = false
  @scroll_offset = 0
  @auto_scroll = true
  @visible_height = 0
  @max_scroll = 0
  @session_info = {id: cable_client.session_id, message_count: 0}
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



17
18
19
# File 'lib/tui/screens/chat.rb', line 17

def input
  @input
end

#message_storeObject (readonly)

Returns the value of attribute message_store.



17
18
19
# File 'lib/tui/screens/chat.rb', line 17

def message_store
  @message_store
end

#scroll_offsetObject (readonly)

Returns the value of attribute scroll_offset.



17
18
19
# File 'lib/tui/screens/chat.rb', line 17

def scroll_offset
  @scroll_offset
end

#session_infoObject (readonly)

Returns the value of attribute session_info.



17
18
19
# File 'lib/tui/screens/chat.rb', line 17

def session_info
  @session_info
end

Instance Method Details

#finalizeObject



81
82
# File 'lib/tui/screens/chat.rb', line 81

def finalize
end

#handle_event(event) ⇒ Object

Scrolling bypasses the loading guard so users can read chat history during LLM calls



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

def handle_event(event)
  return handle_mouse_event(event) if event.mouse?
  return handle_scroll_key(event) if scroll_key?(event)
  return false if @loading

  if event.enter?
    submit_message
    true
  elsif event.backspace?
    @input = @input.chop
    true
  elsif printable_char?(event) && @input.length < MAX_INPUT_LENGTH
    @input += event.code
    true
  else
    false
  end
end

#loading?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/tui/screens/chat.rb', line 84

def loading?
  @loading
end

#messagesObject



33
34
35
# File 'lib/tui/screens/chat.rb', line 33

def messages
  @message_store.messages
end

#new_sessionObject

Creates a new session through the WebSocket protocol. The brain creates the session, switches the channel stream, and sends a session_changed signal followed by (empty) history. The client-side state reset happens when session_changed is received.



77
78
79
# File 'lib/tui/screens/chat.rb', line 77

def new_session
  @cable_client.create_session
end

#render(frame, area, tui) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tui/screens/chat.rb', line 37

def render(frame, area, tui)
  process_incoming_messages

  chat_area, input_area = tui.split(
    area,
    direction: :vertical,
    constraints: [
      tui.constraint_fill(1),
      tui.constraint_length(INPUT_HEIGHT)
    ]
  )

  render_messages(frame, chat_area, tui)
  render_input(frame, input_area, tui)
end