Class: Phronomy::Memory::Storage::ActiveRecord
- Defined in:
- lib/phronomy/memory/storage/active_record.rb
Instance Method Summary collapse
- #append_raw(thread_id:, messages:, starting_seq:) ⇒ Object
- #clear(thread_id:) ⇒ Object
- #clear_compactions(thread_id:) ⇒ Object
- #clear_raw(thread_id:) ⇒ Object
-
#initialize(model_class:, raw_model_class: nil, compaction_model_class: nil) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#load(thread_id:) ⇒ Array<MessageStruct>
Load all messages for a thread, ordered by creation time.
- #load_compactions(thread_id:) ⇒ Array<Hash>
- #load_raw(thread_id:) ⇒ Array<Hash>
-
#purge_older_than(thread_id:, older_than:) ⇒ Object
Remove messages for a thread that were created before +older_than+.
-
#save(thread_id:, messages:) ⇒ Object
Replace all stored messages for a thread.
- #save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(model_class:, raw_model_class: nil, compaction_model_class: nil) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
48 49 50 51 52 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 48 def initialize(model_class:, raw_model_class: nil, compaction_model_class: nil) @model_class = model_class @raw_model_class = raw_model_class @compaction_model_class = compaction_model_class end |
Instance Method Details
#append_raw(thread_id:, messages:, starting_seq:) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 100 def append_raw(thread_id:, messages:, starting_seq:) return unless @raw_model_class .each_with_index do |msg, i| @raw_model_class.create!( thread_id: thread_id, seq: starting_seq + i, role: msg.role.to_s, content: msg.content.to_s, tool_calls_json: serialize_tool_calls(msg), model_id: (msg.model_id if msg.respond_to?(:model_id)) ) end end |
#clear(thread_id:) ⇒ Object
87 88 89 90 91 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 87 def clear(thread_id:) @model_class.where(thread_id: thread_id).delete_all clear_raw(thread_id: thread_id) clear_compactions(thread_id: thread_id) end |
#clear_compactions(thread_id:) ⇒ Object
157 158 159 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 157 def clear_compactions(thread_id:) @compaction_model_class&.where(thread_id: thread_id)&.delete_all end |
#clear_raw(thread_id:) ⇒ Object
125 126 127 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 125 def clear_raw(thread_id:) @raw_model_class&.where(thread_id: thread_id)&.delete_all end |
#load(thread_id:) ⇒ Array<MessageStruct>
Load all messages for a thread, ordered by creation time.
62 63 64 65 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 62 def load(thread_id:) records = @model_class.where(thread_id: thread_id).order(:created_at).to_a records.map { |r| (r) } end |
#load_compactions(thread_id:) ⇒ Array<Hash>
149 150 151 152 153 154 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 149 def load_compactions(thread_id:) return [] unless @compaction_model_class records = @compaction_model_class.where(thread_id: thread_id).order(:start_seq).to_a records.map { |r| {start_seq: r.start_seq, end_seq: r.end_seq, summary_text: r.summary_text} } end |
#load_raw(thread_id:) ⇒ Array<Hash>
117 118 119 120 121 122 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 117 def load_raw(thread_id:) return [] unless @raw_model_class records = @raw_model_class.where(thread_id: thread_id).order(:seq).to_a records.map { |r| {seq: r.seq, message: (r)} } end |
#purge_older_than(thread_id:, older_than:) ⇒ Object
Remove messages for a thread that were created before +older_than+. Only the legacy message store is filtered; raw and compaction records are left untouched because they use seq-based addressing.
167 168 169 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 167 def purge_older_than(thread_id:, older_than:) @model_class.where(thread_id: thread_id).where("created_at < ?", older_than).delete_all end |
#save(thread_id:, messages:) ⇒ Object
Replace all stored messages for a thread.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 71 def save(thread_id:, messages:) @model_class.transaction do @model_class.where(thread_id: thread_id).delete_all .each do |msg| @model_class.create!( thread_id: thread_id, role: msg.role.to_s, content: msg.content.to_s, tool_calls_json: serialize_tool_calls(msg), model_id: (msg.model_id if msg.respond_to?(:model_id)) ) end end end |
#save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/phronomy/memory/storage/active_record.rb', line 137 def save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ensure_compaction_model! @compaction_model_class.create!( thread_id: thread_id, start_seq: start_seq, end_seq: end_seq, summary_text: summary_text ) end |