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
18
# File 'lib/phronomy/memory/storage/in_memory.rb', line 13

def initialize
  @mutex = Mutex.new
  @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)


52
53
54
55
56
57
58
59
60
# File 'lib/phronomy/memory/storage/in_memory.rb', line 52

def append_raw(thread_id:, messages:, starting_seq:)
  now = Time.now
  @mutex.synchronize do
    @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
end

#clear(thread_id:) ⇒ Object

Parameters:

  • thread_id (String)


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

def clear(thread_id:)
  @mutex.synchronize do
    @store.delete(thread_id)
    @raw_store.delete(thread_id)
    @compaction_store.delete(thread_id)
  end
end

#clear_compactions(thread_id:) ⇒ Object

Parameters:

  • thread_id (String)


95
96
97
# File 'lib/phronomy/memory/storage/in_memory.rb', line 95

def clear_compactions(thread_id:)
  @mutex.synchronize { @compaction_store.delete(thread_id) }
end

#clear_raw(thread_id:) ⇒ Object

Parameters:

  • thread_id (String)


69
70
71
# File 'lib/phronomy/memory/storage/in_memory.rb', line 69

def clear_raw(thread_id:)
  @mutex.synchronize { @raw_store.delete(thread_id) }
end

#load(thread_id:) ⇒ Array

Parameters:

  • thread_id (String)

Returns:

  • (Array)


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

def load(thread_id:)
  @mutex.synchronize { (@store[thread_id] || []).dup }
end

#load_compactions(thread_id:) ⇒ Array<Hash>

Parameters:

  • thread_id (String)

Returns:

  • (Array<Hash>)


90
91
92
# File 'lib/phronomy/memory/storage/in_memory.rb', line 90

def load_compactions(thread_id:)
  @mutex.synchronize { (@compaction_store[thread_id] || []).dup }
end

#load_raw(thread_id:) ⇒ Array<Hash>

Parameters:

  • thread_id (String)

Returns:

  • (Array<Hash>)


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

def load_raw(thread_id:)
  @mutex.synchronize { (@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)


103
104
105
106
107
108
109
# File 'lib/phronomy/memory/storage/in_memory.rb', line 103

def purge_older_than(thread_id:, older_than:)
  @mutex.synchronize do
    next unless @raw_store[thread_id]

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

#save(thread_id:, messages:) ⇒ Object

Parameters:

  • thread_id (String)
  • messages (Array)


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

def save(thread_id:, messages:)
  @mutex.synchronize { @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)


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

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