Class: TUI::Screens::Chat
- Inherits:
-
Object
- Object
- TUI::Screens::Chat
- Defined in:
- lib/tui/screens/chat.rb
Constant Summary collapse
- MIN_INPUT_HEIGHT =
3- 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- TOOL_ICON =
"\u{1F527}"- CLOCK_ICON =
"\u{1F552}"- CHECKMARK =
"\u2713"- RETURN_ARROW =
"\u21A9"- ERROR_ICON =
"\u274C"- THOUGHT_BUBBLE =
"\u{1F4AD}"- ROLE_COLORS =
{"user" => "green", "assistant" => "cyan"}.freeze
- VIEW_MODES =
Intentionally duplicated from Session::VIEW_MODES to keep the TUI independent of Rails. Must stay in sync when adding new modes.
%w[basic verbose debug].freeze
Instance Attribute Summary collapse
-
#authentication_required ⇒ Object
readonly
Returns the value of attribute authentication_required.
-
#chat_focused ⇒ Object
readonly
Returns the value of attribute chat_focused.
-
#message_store ⇒ Object
readonly
Returns the value of attribute message_store.
-
#parent_session_id ⇒ Object
readonly
Returns the value of attribute parent_session_id.
-
#scroll_offset ⇒ Object
readonly
Returns the value of attribute scroll_offset.
-
#session_info ⇒ Object
readonly
Returns the value of attribute session_info.
-
#sessions_list ⇒ Object
readonly
Returns the value of attribute sessions_list.
-
#token_save_result ⇒ Object
readonly
Returns the value of attribute token_save_result.
-
#view_mode ⇒ Object
readonly
Returns the value of attribute view_mode.
Instance Method Summary collapse
-
#clear_authentication_required ⇒ void
Clears the authentication_required flag after the App has consumed it.
-
#clear_input ⇒ void
Clears the input buffer.
-
#consume_token_save_result ⇒ Hash?
Returns and clears the token save result for one-shot consumption by the App.
-
#cursor_pos ⇒ Integer
Current cursor position (delegates to InputBuffer).
- #finalize ⇒ Object
-
#focus_chat ⇒ void
Switches focus to the chat pane for keyboard scrolling.
-
#handle_event(event) ⇒ Object
Dispatches keyboard, mouse, and paste events.
-
#initialize(cable_client:, message_store: nil) ⇒ Chat
constructor
A new instance of Chat.
-
#input ⇒ String
Current input text (delegates to InputBuffer).
-
#interrupt_execution ⇒ void
Sends an interrupt request to the server to stop the current tool chain.
- #loading? ⇒ Boolean
- #messages ⇒ Object
-
#new_session ⇒ Object
Creates a new session through the WebSocket protocol.
- #render(frame, area, tui) ⇒ Object
-
#switch_session(session_id) ⇒ Object
Switches to an existing session through the WebSocket protocol.
-
#switch_view_mode(mode) ⇒ Object
Sends an explicit view mode switch command to the server.
-
#unfocus_chat ⇒ void
Returns focus from the chat pane to the input field.
Constructor Details
#initialize(cable_client:, message_store: nil) ⇒ Chat
Returns a new instance of Chat.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tui/screens/chat.rb', line 38 def initialize(cable_client:, message_store: nil) @cable_client = cable_client @message_store = || MessageStore.new @input_buffer = InputBuffer.new @loading = false @scroll_offset = 0 @auto_scroll = true @visible_height = 0 @max_scroll = 0 @input_scroll_offset = 0 @view_mode = "basic" @session_info = {id: cable_client.session_id || 0, message_count: 0, active_skills: [], active_workflow: nil, goals: []} @sessions_list = nil @parent_session_id = nil @authentication_required = false @token_save_result = nil @chat_focused = false @input_history = [] @history_index = nil @saved_input = nil end |
Instance Attribute Details
#authentication_required ⇒ Object (readonly)
Returns the value of attribute authentication_required.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def authentication_required @authentication_required end |
#chat_focused ⇒ Object (readonly)
Returns the value of attribute chat_focused.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def chat_focused @chat_focused end |
#message_store ⇒ Object (readonly)
Returns the value of attribute message_store.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def @message_store end |
#parent_session_id ⇒ Object (readonly)
Returns the value of attribute parent_session_id.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def parent_session_id @parent_session_id end |
#scroll_offset ⇒ Object (readonly)
Returns the value of attribute scroll_offset.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def scroll_offset @scroll_offset end |
#session_info ⇒ Object (readonly)
Returns the value of attribute session_info.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def session_info @session_info end |
#sessions_list ⇒ Object (readonly)
Returns the value of attribute sessions_list.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def sessions_list @sessions_list end |
#token_save_result ⇒ Object (readonly)
Returns the value of attribute token_save_result.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def token_save_result @token_save_result end |
#view_mode ⇒ Object (readonly)
Returns the value of attribute view_mode.
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def view_mode @view_mode end |
Instance Method Details
#clear_authentication_required ⇒ void
This method returns an undefined value.
Clears the authentication_required flag after the App has consumed it.
187 188 189 |
# File 'lib/tui/screens/chat.rb', line 187 def clear_authentication_required @authentication_required = false end |
#clear_input ⇒ void
This method returns an undefined value.
Clears the input buffer. Used when Escape is pressed with non-empty input.
181 182 183 |
# File 'lib/tui/screens/chat.rb', line 181 def clear_input @input_buffer.clear end |
#consume_token_save_result ⇒ Hash?
Returns and clears the token save result for one-shot consumption by the App.
193 194 195 196 197 |
# File 'lib/tui/screens/chat.rb', line 193 def consume_token_save_result result = @token_save_result @token_save_result = nil result end |
#cursor_pos ⇒ Integer
Returns current cursor position (delegates to InputBuffer).
70 71 72 |
# File 'lib/tui/screens/chat.rb', line 70 def cursor_pos @input_buffer.cursor_pos end |
#finalize ⇒ Object
199 200 |
# File 'lib/tui/screens/chat.rb', line 199 def finalize end |
#focus_chat ⇒ void
This method returns an undefined value.
Switches focus to the chat pane for keyboard scrolling.
208 209 210 |
# File 'lib/tui/screens/chat.rb', line 208 def focus_chat @chat_focused = true end |
#handle_event(event) ⇒ Object
Dispatches keyboard, mouse, and paste events. Supports two focus modes: input mode (default) where arrows navigate the input buffer with bash-style history overflow, and chat-focused mode where arrows scroll the chat pane.
Page Up/Down and mouse scroll always control the chat pane regardless of focus mode.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/tui/screens/chat.rb', line 99 def handle_event(event) return handle_mouse_event(event) if event.mouse? return handle_paste_event(event) if event.paste? return handle_scroll_key(event) if event.page_up? || event.page_down? return handle_chat_focused_event(event) if @chat_focused if event.up? return true if @input_buffer.move_up return true if @input_buffer.text.empty? && return navigate_history_back end if event.down? return true if @input_buffer.move_down return navigate_history_forward end if event.enter? true elsif event.backspace? reset_history_browsing @input_buffer.backspace true elsif event.delete? reset_history_browsing @input_buffer.delete true elsif event.left? @input_buffer.move_left elsif event.right? @input_buffer.move_right elsif event.home? @input_buffer.move_home elsif event.end? @input_buffer.move_end elsif printable_char?(event) && !@input_buffer.full? reset_history_browsing @input_buffer.insert(event.code) else false end end |
#input ⇒ String
Returns current input text (delegates to InputBuffer).
65 66 67 |
# File 'lib/tui/screens/chat.rb', line 65 def input @input_buffer.text end |
#interrupt_execution ⇒ void
This method returns an undefined value.
Sends an interrupt request to the server to stop the current tool chain. Called when Escape is pressed with empty input during active processing.
174 175 176 |
# File 'lib/tui/screens/chat.rb', line 174 def interrupt_execution @cable_client.interrupt end |
#loading? ⇒ Boolean
202 203 204 |
# File 'lib/tui/screens/chat.rb', line 202 def loading? @loading end |
#messages ⇒ Object
60 61 62 |
# File 'lib/tui/screens/chat.rb', line 60 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.
148 149 150 |
# File 'lib/tui/screens/chat.rb', line 148 def new_session @cable_client.create_session end |
#render(frame, area, tui) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tui/screens/chat.rb', line 74 def render(frame, area, tui) input_height = calculate_input_height(tui, area.width, area.height) 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 |
#switch_session(session_id) ⇒ Object
Switches to an existing session through the WebSocket protocol. The brain switches the channel stream and sends a session_changed signal followed by chat history.
157 158 159 |
# File 'lib/tui/screens/chat.rb', line 157 def switch_session(session_id) @cable_client.switch_session(session_id) end |
#switch_view_mode(mode) ⇒ Object
Sends an explicit view mode switch command to the server. The server broadcasts the mode change and re-transmits the viewport decorated in the new mode to all connected clients.
166 167 168 |
# File 'lib/tui/screens/chat.rb', line 166 def switch_view_mode(mode) @cable_client.change_view_mode(mode) end |
#unfocus_chat ⇒ void
This method returns an undefined value.
Returns focus from the chat pane to the input field.
214 215 216 |
# File 'lib/tui/screens/chat.rb', line 214 def unfocus_chat @chat_focused = false end |