Class: ActiveMatrix::Memory::AgentMemory

Inherits:
Base
  • Object
show all
Defined in:
lib/active_matrix/memory/agent_memory.rb

Overview

Per-agent private memory storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_multi, #set_multi

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(agent) ⇒ AgentMemory

Returns a new instance of AgentMemory.



9
10
11
12
# File 'lib/active_matrix/memory/agent_memory.rb', line 9

def initialize(agent)
  super()
  @agent = agent
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



7
8
9
# File 'lib/active_matrix/memory/agent_memory.rb', line 7

def agent
  @agent
end

Instance Method Details

#allObject

Get all memory as hash



54
55
56
# File 'lib/active_matrix/memory/agent_memory.rb', line 54

def all
  AsyncQuery.async_pluck(@agent.agent_stores.active, :key, :value).to_h
end

#clear!Object

Clear all agent memory



59
60
61
62
63
64
65
66
# File 'lib/active_matrix/memory/agent_memory.rb', line 59

def clear!
  @agent.agent_stores.destroy_all

  # Clear cache entries
  keys.each { |key| Rails.cache.delete(cache_key(key)) } if @cache_enabled

  true
end

#decrement(key, amount = 1) ⇒ Object

Decrement a counter



87
88
89
# File 'lib/active_matrix/memory/agent_memory.rb', line 87

def decrement(key, amount = 1)
  increment(key, -amount)
end

#delete(key) ⇒ Object

Delete a key



42
43
44
45
46
# File 'lib/active_matrix/memory/agent_memory.rb', line 42

def delete(key)
  delete_through(key) do
    @agent.agent_stores.where(key: key).destroy_all.any?
  end
end

#exists?(key) ⇒ Boolean

Check if a key exists

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/active_matrix/memory/agent_memory.rb', line 33

def exists?(key)
  if @cache_enabled && Rails.cache.exist?(cache_key(key))
    true
  else
    @agent.agent_stores.active.exists?(key: key)
  end
end

#get(key) ⇒ Object

Get a value from agent memory



15
16
17
18
19
20
# File 'lib/active_matrix/memory/agent_memory.rb', line 15

def get(key)
  fetch_with_cache(key) do
    memory = @agent.agent_stores.active.find_by(key: key)
    memory&.value
  end
end

#increment(key, amount = 1) ⇒ Object

Increment a counter



79
80
81
82
83
84
# File 'lib/active_matrix/memory/agent_memory.rb', line 79

def increment(key, amount = 1)
  current = get(key) || 0
  new_value = current + amount
  set(key, new_value)
  new_value
end

#keysObject

Get all keys



49
50
51
# File 'lib/active_matrix/memory/agent_memory.rb', line 49

def keys
  AsyncQuery.async_pluck(@agent.agent_stores.active, :key)
end

#pull(key, value) ⇒ Object

Remove from a list



100
101
102
103
104
105
# File 'lib/active_matrix/memory/agent_memory.rb', line 100

def pull(key, value)
  list = get(key) || []
  list.delete(value)
  set(key, list)
  list
end

#push(key, value) ⇒ Object

Add to a list



92
93
94
95
96
97
# File 'lib/active_matrix/memory/agent_memory.rb', line 92

def push(key, value)
  list = get(key) || []
  list << value
  set(key, list)
  list
end

#remember(key, expires_in: nil) ⇒ Object

Remember something with optional TTL



69
70
71
72
73
74
75
76
# File 'lib/active_matrix/memory/agent_memory.rb', line 69

def remember(key, expires_in: nil)
  value = get(key)
  return value if value.present?

  value = yield
  set(key, value, expires_in: expires_in) if value.present?
  value
end

#set(key, value, expires_in: nil) ⇒ Object

Set a value in agent memory



23
24
25
26
27
28
29
30
# File 'lib/active_matrix/memory/agent_memory.rb', line 23

def set(key, value, expires_in: nil)
  write_through(key, value, expires_in: expires_in) do
    memory = @agent.agent_stores.find_or_initialize_by(key: key)
    memory.value = value
    memory.expires_at = expires_in.present? ? Time.current + expires_in : nil
    memory.save!
  end
end