Class: OllamaAgent::Memory::LongTerm

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/memory/long_term.rb

Overview

Persistent cross-session, cross-project memory. Stores user preferences, project facts, and reusable summaries. Backed by YAML files in ~/.config/ollama_agent/memory/<namespace>.yml

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFAULT_BASE =
File.join(Dir.home, ".config", "ollama_agent", "memory")

Instance Method Summary collapse

Constructor Details

#initialize(base_path: DEFAULT_BASE) ⇒ LongTerm

Returns a new instance of LongTerm.



17
18
19
# File 'lib/ollama_agent/memory/long_term.rb', line 17

def initialize(base_path: DEFAULT_BASE)
  @base_path = base_path
end

Instance Method Details

#all(namespace: "default") ⇒ Object

All entries in namespace as => value hash.



45
46
47
# File 'lib/ollama_agent/memory/long_term.rb', line 45

def all(namespace: "default")
  load_namespace(namespace).transform_values { |v| v["value"] }
end

#clear_namespace!(namespace) ⇒ Object



84
85
86
87
# File 'lib/ollama_agent/memory/long_term.rb', line 84

def clear_namespace!(namespace)
  path = namespace_path(namespace)
  FileUtils.rm_f(path)
end

#delete(key, namespace: "default") ⇒ Object

Delete a key.



50
51
52
53
54
55
# File 'lib/ollama_agent/memory/long_term.rb', line 50

def delete(key, namespace: "default")
  data = load_namespace(namespace)
  removed = data.delete(key.to_s)
  persist_namespace(namespace, data) if removed
  removed
end

#fetch(key, namespace: "default") ⇒ Object

Fetch a value. Returns nil if missing.



40
41
42
# File 'lib/ollama_agent/memory/long_term.rb', line 40

def fetch(key, namespace: "default")
  load_namespace(namespace).dig(key.to_s, "value")
end

#keys(namespace: "default") ⇒ Object

List all keys in a namespace.



58
59
60
# File 'lib/ollama_agent/memory/long_term.rb', line 58

def keys(namespace: "default")
  load_namespace(namespace).keys
end

#namespacesObject

All namespace names that have been created.



77
78
79
80
81
82
# File 'lib/ollama_agent/memory/long_term.rb', line 77

def namespaces
  return [] unless File.directory?(@base_path)

  Dir.glob(File.join(@base_path, "*.yml"))
     .map { |f| File.basename(f, ".yml") }
end

#search(pattern, namespace: "default") ⇒ Object

Search for entries whose key or value matches a pattern.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ollama_agent/memory/long_term.rb', line 63

def search(pattern, namespace: "default")
  data = load_namespace(namespace)
  re   = begin
    Regexp.new(pattern, Regexp::IGNORECASE)
  rescue StandardError
    nil
  end
  return {} unless re

  data.select { |k, v| re.match?(k) || re.match?(v["value"].to_s) }
      .transform_values { |v| v["value"] }
end

#store(key, value, namespace: "default") ⇒ Object

Store a value under key in namespace.

Parameters:

  • key (String)
  • value (Object)

    YAML-serialisable

  • namespace (String) (defaults to: "default")


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ollama_agent/memory/long_term.rb', line 25

def store(key, value, namespace: "default")
  data = load_namespace(namespace)
  now  = Time.now.iso8601

  data[key.to_s] = {
    "value" => value,
    "created_at" => data.dig(key.to_s, "created_at") || now,
    "updated_at" => now
  }

  persist_namespace(namespace, data)
  value
end