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
85 86 87 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 85 def [](key) context[key.to_s] end |
#[]=(key, value) ⇒ Object
89 90 91 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 89 def []=(key, value) update_context(key.to_s => value) end |
#active? ⇒ Boolean
Check if conversation is active (recent activity)
69 70 71 72 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 69 def active? last_at = last_at.present? && last_at > 1.hour.ago end |
#add_message(event) ⇒ Object
Add a message to history
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 36 def (event) 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
75 76 77 78 79 80 81 82 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 75 def clear_history! 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 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 17 def context fetch_with_cache('context', expires_in: 1.hour) do record = find_or_create_record record.context end end |
#last_message_at ⇒ Object
Get last message timestamp
63 64 65 66 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 63 def record = conversation_record record&. end |
#recent_messages(limit = 10) ⇒ Object
Get recent messages
55 56 57 58 59 60 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 55 def (limit = 10) fetch_with_cache('recent_messages', expires_in: 5.minutes) do record = find_or_create_record record.(limit) end end |
#remember(key) ⇒ Object
Remember something in conversation context
94 95 96 97 98 99 100 101 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 94 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
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 104 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/active_matrix/memory/conversation_memory.rb', line 25 def update_context(data) record = find_or_create_record record.context = record.context.merge(data) record.save! # Update cache invalidate_cache true end |