Class: ActiveMatrix::Memory::ConversationMemory
- Defined in:
- lib/active_matrix/memory/conversation_memory.rb
Overview
Per-conversation memory storage (speaker-specific)
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#room_id ⇒ Object
readonly
Returns the value of attribute room_id.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get or set a specific context value.
- #[]=(key, value) ⇒ Object
-
#active? ⇒ Boolean
Check if conversation is active (recent activity).
-
#add_message(event) ⇒ Object
Add a message to history.
-
#clear_history! ⇒ Object
Clear conversation history but keep context.
-
#context ⇒ Object
Get conversation context.
-
#initialize(agent, user_id, room_id) ⇒ ConversationMemory
constructor
A new instance of ConversationMemory.
-
#last_message_at ⇒ Object
Get last message timestamp.
-
#recent_messages(limit = 10) ⇒ Object
Get recent messages.
-
#remember(key) ⇒ Object
Remember something in conversation context.
-
#summary ⇒ Object
Get conversation summary.
-
#update_context(data) ⇒ Object
Update conversation context.
Methods inherited from Base
#clear!, #delete, #exists?, #get, #get_multi, #set, #set_multi
Methods included from Logging
Constructor Details
#initialize(agent, user_id, room_id) ⇒ ConversationMemory
Returns a new instance of ConversationMemory.
9 10 11 12 13 14 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 9 def initialize(agent, user_id, room_id) super() @agent = agent @user_id = user_id @room_id = room_id end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
7 8 9 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 7 def agent @agent end |
#room_id ⇒ Object (readonly)
Returns the value of attribute room_id.
7 8 9 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 7 def room_id @room_id end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
7 8 9 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 7 def user_id @user_id end |
Instance Method Details
#[](key) ⇒ Object
Get or set a specific context value
97 98 99 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 97 def [](key) context[key.to_s] end |
#[]=(key, value) ⇒ Object
101 102 103 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 101 def []=(key, value) update_context(key.to_s => value) end |
#active? ⇒ Boolean
Check if conversation is active (recent activity)
79 80 81 82 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 79 def active? last_at = last_at.present? && last_at > 1.hour.ago end |
#add_message(event) ⇒ Object
Add a message to history
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 40 def (event) return false unless defined?(::ConversationContext) record = find_or_create_record record.({ event_id: event[:event_id], sender: event[:sender], content: event.dig(:content, :body), timestamp: event[:origin_server_ts] || (Time.current.to_i * 1000) }) # Update agent activity @agent.update_activity! @agent. # Invalidate cache invalidate_cache true end |
#clear_history! ⇒ Object
Clear conversation history but keep context
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 85 def clear_history! return false unless defined?(::ConversationContext) record = conversation_record return false unless record record.prune_history! invalidate_cache true end |
#context ⇒ Object
Get conversation context
17 18 19 20 21 22 23 24 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 17 def context fetch_with_cache('context', expires_in: 1.hour) do return {} unless defined?(::ConversationContext) record = find_or_create_record record.context end end |
#last_message_at ⇒ Object
Get last message timestamp
71 72 73 74 75 76 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 71 def return nil unless defined?(::ConversationContext) record = conversation_record record&. end |
#recent_messages(limit = 10) ⇒ Object
Get recent messages
61 62 63 64 65 66 67 68 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 61 def (limit = 10) fetch_with_cache('recent_messages', expires_in: 5.minutes) do return [] unless defined?(::ConversationContext) record = find_or_create_record record.(limit) end end |
#remember(key) ⇒ Object
Remember something in conversation context
106 107 108 109 110 111 112 113 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 106 def remember(key) value = self[key] return value if value.present? value = yield self[key] = value if value.present? value end |
#summary ⇒ Object
Get conversation summary
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 116 def summary { user_id: @user_id, room_id: @room_id, active: active?, message_count: conversation_record&. || 0, last_message_at: , context: context } end |
#update_context(data) ⇒ Object
Update conversation context
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 27 def update_context(data) return false unless defined?(::ConversationContext) record = find_or_create_record record.context = record.context.merge(data) record.save! # Update cache invalidate_cache true end |