Class: TUI::Screens::Chat
- Inherits:
-
Object
- Object
- TUI::Screens::Chat
- 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
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#message_store ⇒ Object
readonly
Returns the value of attribute message_store.
-
#scroll_offset ⇒ Object
readonly
Returns the value of attribute scroll_offset.
-
#session_info ⇒ Object
readonly
Returns the value of attribute session_info.
Instance Method Summary collapse
- #finalize ⇒ Object
-
#handle_event(event) ⇒ Object
Scrolling bypasses the loading guard so users can read chat history during LLM calls.
-
#initialize(cable_client:, message_store: nil) ⇒ Chat
constructor
A new instance of Chat.
- #loading? ⇒ Boolean
- #messages ⇒ Object
-
#new_session ⇒ Object
Creates a new session through the WebSocket protocol.
- #render(frame, area, tui) ⇒ Object
Constructor Details
#initialize(cable_client:, message_store: nil) ⇒ Chat
Returns a new instance of Chat.
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 = || 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
#input ⇒ Object (readonly)
Returns the value of attribute input.
17 18 19 |
# File 'lib/tui/screens/chat.rb', line 17 def input @input end |
#message_store ⇒ Object (readonly)
Returns the value of attribute message_store.
17 18 19 |
# File 'lib/tui/screens/chat.rb', line 17 def @message_store end |
#scroll_offset ⇒ Object (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_info ⇒ Object (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
#finalize ⇒ Object
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? 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
84 85 86 |
# File 'lib/tui/screens/chat.rb', line 84 def loading? @loading end |
#messages ⇒ Object
33 34 35 |
# File 'lib/tui/screens/chat.rb', line 33 def @message_store. end |
#new_session ⇒ Object
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) chat_area, input_area = tui.split( area, direction: :vertical, constraints: [ tui.constraint_fill(1), tui.constraint_length(INPUT_HEIGHT) ] ) (frame, chat_area, tui) render_input(frame, input_area, tui) end |