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
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#message_collector ⇒ Object
readonly
Returns the value of attribute message_collector.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #finalize ⇒ Object
- #handle_event(event) ⇒ Object
-
#initialize(message_collector: nil, persister: nil, session: nil, shell_session: nil) ⇒ Chat
constructor
A new instance of Chat.
- #loading? ⇒ Boolean
- #messages ⇒ Object
- #new_session ⇒ Object
- #render(frame, area, tui) ⇒ Object
Constructor Details
#initialize(message_collector: nil, persister: nil, session: nil, shell_session: nil) ⇒ Chat
Returns a new instance of Chat.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tui/screens/chat.rb', line 16 def initialize(message_collector: nil, persister: nil, session: nil, shell_session: nil) @message_collector = || Events::Subscribers::MessageCollector.new @input = "" @loading = false @client = nil @submit_thread = nil @session = session || Session.order(id: :desc).first || Session.create! @persister = persister || Events::Subscribers::Persister.new(@session) @shell_session = shell_session || ShellSession.new(session_id: @session.id) Events::Bus.subscribe(@message_collector) Events::Bus.subscribe(@persister) end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
14 15 16 |
# File 'lib/tui/screens/chat.rb', line 14 def input @input end |
#message_collector ⇒ Object (readonly)
Returns the value of attribute message_collector.
14 15 16 |
# File 'lib/tui/screens/chat.rb', line 14 def @message_collector end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
14 15 16 |
# File 'lib/tui/screens/chat.rb', line 14 def session @session end |
Instance Method Details
#finalize ⇒ Object
79 80 81 82 83 84 |
# File 'lib/tui/screens/chat.rb', line 79 def finalize @submit_thread&.join @shell_session&.finalize Events::Bus.unsubscribe(@message_collector) Events::Bus.unsubscribe(@persister) end |
#handle_event(event) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tui/screens/chat.rb', line 50 def handle_event(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
86 87 88 |
# File 'lib/tui/screens/chat.rb', line 86 def loading? @loading end |
#messages ⇒ Object
32 33 34 |
# File 'lib/tui/screens/chat.rb', line 32 def @message_collector. end |
#new_session ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tui/screens/chat.rb', line 67 def new_session @submit_thread&.join @shell_session&.finalize @session = Session.create! @persister.session = @session @message_collector.clear @input = "" @loading = false @shell_session = ShellSession.new(session_id: @session.id) @registry = nil end |
#render(frame, area, tui) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tui/screens/chat.rb', line 36 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 |