Class: TUI::MessageStore
- Inherits:
-
Object
- Object
- TUI::MessageStore
- Defined in:
- lib/tui/message_store.rb
Overview
Thread-safe in-memory store for chat messages displayed in the TUI. Replaces Events::Subscribers::MessageCollector in the WebSocket-based TUI, with no dependency on Rails or the Events module.
Accepts Action Cable event payloads and extracts displayable messages.
Constant Summary collapse
- DISPLAYABLE_TYPES =
%w[user_message agent_message].freeze
- ROLE_MAP =
{ "user_message" => "user", "agent_message" => "assistant" }.freeze
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize ⇒ MessageStore
constructor
A new instance of MessageStore.
-
#messages ⇒ Array<Hash>
Thread-safe copy of collected messages.
-
#process_event(event_data) ⇒ Boolean
Processes a raw event payload from the WebSocket channel.
Constructor Details
#initialize ⇒ MessageStore
Returns a new instance of MessageStore.
17 18 19 20 |
# File 'lib/tui/message_store.rb', line 17 def initialize @messages = [] @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
45 46 47 |
# File 'lib/tui/message_store.rb', line 45 def clear @mutex.synchronize { @messages = [] } end |
#messages ⇒ Array<Hash>
Returns thread-safe copy of collected messages.
23 24 25 |
# File 'lib/tui/message_store.rb', line 23 def @mutex.synchronize { @messages.dup } end |
#process_event(event_data) ⇒ Boolean
Processes a raw event payload from the WebSocket channel. Only user_message and agent_message events are stored.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tui/message_store.rb', line 32 def process_event(event_data) type = event_data["type"] return false unless DISPLAYABLE_TYPES.include?(type) content = event_data["content"] return false if content.nil? @mutex.synchronize do @messages << {role: ROLE_MAP.fetch(type), content: content} end true end |