Class: ActiveMatrix::ChatSession
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ActiveMatrix::ChatSession
- Defined in:
- app/models/active_matrix/chat_session.rb
Constant Summary collapse
- MAX_HISTORY_SIZE =
Configuration
20
Class Method Summary collapse
Instance Method Summary collapse
-
#add_message(message_data) ⇒ Object
Add a message to the history.
-
#cache_key ⇒ Object
Cache integration.
-
#prune_history! ⇒ Object
Clear old messages but keep context.
-
#recent_messages(limit = 10) ⇒ Object
Get recent messages.
- #write_to_cache ⇒ Object
Class Method Details
.cleanup_stale! ⇒ Object
101 102 103 |
# File 'app/models/active_matrix/chat_session.rb', line 101 def self.cleanup_stale! stale.destroy_all end |
Instance Method Details
#add_message(message_data) ⇒ Object
Add a message to the history
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/active_matrix/chat_session.rb', line 51 def () = ['messages'] || [] # Add new message << { 'event_id' => [:event_id], 'sender' => [:sender], 'content' => [:content], 'timestamp' => [:timestamp] || Time.current.to_i } # Keep only recent messages = .last(MAX_HISTORY_SIZE) # Update record self. = { 'messages' => } self. = Time.current self. = .size save! # Update cache write_to_cache end |
#cache_key ⇒ Object
Cache integration
89 90 91 |
# File 'app/models/active_matrix/chat_session.rb', line 89 def cache_key "conversation/#{agent_id}/#{user_id}/#{room_id}" end |
#prune_history! ⇒ Object
Clear old messages but keep context
82 83 84 85 86 |
# File 'app/models/active_matrix/chat_session.rb', line 82 def prune_history! = ['messages'] || [] self. = { 'messages' => .last(5) } save! end |
#recent_messages(limit = 10) ⇒ Object
Get recent messages
76 77 78 79 |
# File 'app/models/active_matrix/chat_session.rb', line 76 def (limit = 10) = ['messages'] || [] .last(limit) end |
#write_to_cache ⇒ Object
93 94 95 96 97 98 99 |
# File 'app/models/active_matrix/chat_session.rb', line 93 def write_to_cache Rails.cache.write(cache_key, { context: context, recent_messages: , last_message_at: }, expires_in: 1.hour) end |