Class: Phronomy::Memory::Storage::InMemory

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/memory/storage/in_memory.rb

Overview

In-process Hash-backed storage for conversation messages. Messages are lost when the process exits.

Examples:

storage = Phronomy::Memory::Storage::InMemory.new
manager = Phronomy::Memory::ConversationManager.new(storage: storage, ...)

Instance Method Summary collapse

Methods inherited from Base

#purge

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



13
14
15
16
17
# File 'lib/phronomy/memory/storage/in_memory.rb', line 13

def initialize
  @store = {}
  @raw_store = {}       # thread_id => [{seq:, message:}, ...]
  @compaction_store = {} # thread_id => [{start_seq:, end_seq:, summary_text:}, ...]
end

Instance Method Details

#append_raw(thread_id:, messages:, starting_seq:) ⇒ Object

Parameters:

  • thread_id (String)
  • messages (Array)
  • starting_seq (Integer)


49
50
51
52
53
54
55
# File 'lib/phronomy/memory/storage/in_memory.rb', line 49

def append_raw(thread_id:, messages:, starting_seq:)
  now = Time.now
  @raw_store[thread_id] ||= []
  messages.each_with_index do |msg, i|
    @raw_store[thread_id] << {seq: starting_seq + i, message: msg, recorded_at: now}
  end
end

#clear(thread_id:) ⇒ Object

Parameters:

  • thread_id (String)


36
37
38
39
40
# File 'lib/phronomy/memory/storage/in_memory.rb', line 36

def clear(thread_id:)
  @store.delete(thread_id)
  clear_raw(thread_id: thread_id)
  clear_compactions(thread_id: thread_id)
end

#clear_compactions(thread_id:) ⇒ Object

Parameters:

  • thread_id (String)


88
89
90
# File 'lib/phronomy/memory/storage/in_memory.rb', line 88

def clear_compactions(thread_id:)
  @compaction_store.delete(thread_id)
end

#clear_raw(thread_id:) ⇒ Object

Parameters:

  • thread_id (String)


64
65
66
# File 'lib/phronomy/memory/storage/in_memory.rb', line 64

def clear_raw(thread_id:)
  @raw_store.delete(thread_id)
end

#load(thread_id:) ⇒ Array

Parameters:

  • thread_id (String)

Returns:

  • (Array)


25
26
27
# File 'lib/phronomy/memory/storage/in_memory.rb', line 25

def load(thread_id:)
  (@store[thread_id] || []).dup
end

#load_compactions(thread_id:) ⇒ Array<Hash>

Parameters:

  • thread_id (String)

Returns:

  • (Array<Hash>)


83
84
85
# File 'lib/phronomy/memory/storage/in_memory.rb', line 83

def load_compactions(thread_id:)
  (@compaction_store[thread_id] || []).dup
end

#load_raw(thread_id:) ⇒ Array<Hash>

Parameters:

  • thread_id (String)

Returns:

  • (Array<Hash>)


59
60
61
# File 'lib/phronomy/memory/storage/in_memory.rb', line 59

def load_raw(thread_id:)
  (@raw_store[thread_id] || []).dup
end

#purge_older_than(thread_id:, older_than:) ⇒ Object

Remove raw messages recorded before +older_than+ for this thread.

Parameters:

  • thread_id (String)
  • older_than (Time)


96
97
98
99
100
# File 'lib/phronomy/memory/storage/in_memory.rb', line 96

def purge_older_than(thread_id:, older_than:)
  return unless @raw_store[thread_id]

  @raw_store[thread_id].reject! { |entry| entry[:recorded_at] && entry[:recorded_at] < older_than }
end

#save(thread_id:, messages:) ⇒ Object

Parameters:

  • thread_id (String)
  • messages (Array)


31
32
33
# File 'lib/phronomy/memory/storage/in_memory.rb', line 31

def save(thread_id:, messages:)
  @store[thread_id] = messages.dup
end

#save_compaction(thread_id:, start_seq:, end_seq:, summary_text:) ⇒ Object

Parameters:

  • thread_id (String)
  • start_seq (Integer)
  • end_seq (Integer)
  • summary_text (String)


76
77
78
79
# File 'lib/phronomy/memory/storage/in_memory.rb', line 76

def save_compaction(thread_id:, start_seq:, end_seq:, summary_text:)
  @compaction_store[thread_id] ||= []
  @compaction_store[thread_id] << {start_seq: start_seq, end_seq: end_seq, summary_text: summary_text}
end