Class: Legion::Extensions::Agentic::Memory::Diary::Helpers::DiaryStore

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_id: nil) ⇒ DiaryStore

Returns a new instance of DiaryStore.



14
15
16
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 14

def initialize(agent_id: nil)
  @agent_id = agent_id || resolve_agent_id
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



18
19
20
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 18

def agent_id
  @agent_id
end

Instance Method Details

#countObject



88
89
90
91
92
93
94
95
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 88

def count
  return 0 unless db_ready?

  scoped_ds.count
rescue StandardError => e
  log_warn("count failed: #{e.message}")
  0
end

#db_ready?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 97

def db_ready?
  defined?(Legion::Data::Local) &&
    Legion::Data::Local.respond_to?(:connected?) &&
    Legion::Data::Local.connected? &&
    Legion::Data::Local.connection&.table_exists?(Constants::TABLE_NAME)
rescue StandardError => e
  log_warn("db_ready?: #{e.message}")
  false
end

#delete(entry_id) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 78

def delete(entry_id)
  return false unless db_ready?

  scoped_ds.where(entry_id: entry_id).delete
  true
rescue StandardError => e
  log_warn("delete failed: #{e.message}")
  false
end

#get(entry_id) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 68

def get(entry_id)
  return nil unless db_ready?

  row = scoped_ds.where(entry_id: entry_id).first
  row ? deserialize(row) : nil
rescue StandardError => e
  log_warn("get failed: #{e.message}")
  nil
end

#read(limit: Constants::DEFAULT_LIMIT, since: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 41

def read(limit: Constants::DEFAULT_LIMIT, since: nil)
  return [] unless db_ready?

  effective_limit = [limit, Constants::MAX_LIMIT].min
  ds = scoped_ds.order(Sequel.asc(:created_at)).limit(effective_limit)
  ds = ds.where { created_at >= since } if since
  ds.all.map { |row| deserialize(row) }
rescue StandardError => e
  log_warn("read failed: #{e.message}")
  []
end

#search(query:, limit: Constants::DEFAULT_LIMIT) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 53

def search(query:, limit: Constants::DEFAULT_LIMIT)
  return [] unless db_ready?
  return [] if query.nil? || query.strip.empty?

  effective_limit = [limit, Constants::MAX_LIMIT].min
  ds = scoped_ds
       .where(Sequel.like(:content, "%#{sanitize(query)}%"))
       .order(Sequel.desc(:created_at))
       .limit(effective_limit)
  ds.all.map { |row| deserialize(row) }
rescue StandardError => e
  log_warn("search failed: #{e.message}")
  []
end

#write(session_id:, content:, tags: [], metadata: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/memory/diary/helpers/diary_store.rb', line 20

def write(session_id:, content:, tags: [], metadata: {})
  return nil unless db_ready?

  entry_id = SecureRandom.uuid
  now = Time.now.utc
  row = {
    entry_id:   entry_id,
    agent_id:   @agent_id,
    session_id: session_id,
    content:    sanitize(content.to_s[0...Constants::MAX_CONTENT_SIZE]),
    tags:       tags.is_a?(Array) ? Legion::JSON.dump(tags) : '[]',
    metadata:   .is_a?(Hash) ? Legion::JSON.dump() : '{}',
    created_at: now
  }
  db[Constants::TABLE_NAME].insert(row)
  entry_id
rescue StandardError => e
  log_warn("write failed: #{e.message}")
  nil
end