Class: Legion::Extensions::Agentic::Memory::Trace::PersistentStore
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Memory::Trace::PersistentStore
- Defined in:
- lib/legion/extensions/agentic/memory/trace/persistent_store.rb
Constant Summary collapse
- TABLE =
:memory_traces
Instance Method Summary collapse
- #count ⇒ Object
- #delete_least_recently_used(count: 1) ⇒ Object
- #delete_lowest_confidence(count: 1) ⇒ Object
-
#initialize(agent_id:) ⇒ PersistentStore
constructor
A new instance of PersistentStore.
- #read(trace_type: nil, limit: 50, min_confidence: 0.0) ⇒ Object
- #total_bytes ⇒ Object
- #touch(id) ⇒ Object
- #write(trace_type:, content:, associations: {}, confidence: 1.0) ⇒ Object
Constructor Details
#initialize(agent_id:) ⇒ PersistentStore
Returns a new instance of PersistentStore.
11 12 13 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 11 def initialize(agent_id:) @agent_id = agent_id end |
Instance Method Details
#count ⇒ Object
46 47 48 49 50 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 46 def count return 0 unless db_ready? Legion::Data.connection[TABLE].where(agent_id: @agent_id).count end |
#delete_least_recently_used(count: 1) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 71 def delete_least_recently_used(count: 1) return 0 unless db_ready? ids = Legion::Data.connection[TABLE] .where(agent_id: @agent_id) .order(:accessed_at) .limit(count) .select(:id) Legion::Data.connection[TABLE].where(id: ids).delete end |
#delete_lowest_confidence(count: 1) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 60 def delete_lowest_confidence(count: 1) return 0 unless db_ready? ids = Legion::Data.connection[TABLE] .where(agent_id: @agent_id) .order(:confidence) .limit(count) .select(:id) Legion::Data.connection[TABLE].where(id: ids).delete end |
#read(trace_type: nil, limit: 50, min_confidence: 0.0) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 29 def read(trace_type: nil, limit: 50, min_confidence: 0.0) return [] unless db_ready? ds = Legion::Data.connection[TABLE].where(agent_id: @agent_id) ds = ds.where(trace_type: trace_type.to_s) if trace_type ds = ds.where { confidence >= min_confidence } ds.order(Sequel.desc(:accessed_at)).limit(limit).all end |
#total_bytes ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 52 def total_bytes return 0 unless db_ready? Legion::Data.connection[TABLE] .where(agent_id: @agent_id) .sum(Sequel.function(:length, :content)) || 0 end |
#touch(id) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 38 def touch(id) return unless db_ready? Legion::Data.connection[TABLE] .where(id: id, agent_id: @agent_id) .update(accessed_at: Time.now) end |
#write(trace_type:, content:, associations: {}, confidence: 1.0) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/legion/extensions/agentic/memory/trace/persistent_store.rb', line 15 def write(trace_type:, content:, associations: {}, confidence: 1.0) return nil unless db_ready? Legion::Data.connection[TABLE].insert( agent_id: @agent_id, trace_type: trace_type.to_s, content: content.is_a?(String) ? content : json_dump(content), associations: json_dump(associations), confidence: confidence, created_at: Time.now, accessed_at: Time.now ) end |