Class: Nanobot::Agent::MemoryStore
- Inherits:
-
Object
- Object
- Nanobot::Agent::MemoryStore
- Defined in:
- lib/nanobot/agent/memory.rb
Overview
MemoryStore manages persistent memory for the agent Supports both long-term memory and daily notes
Instance Attribute Summary collapse
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Instance Method Summary collapse
-
#append_long_term(content) ⇒ Object
Append to long-term memory.
-
#append_today(content, timestamp: true) ⇒ Object
Append to today's daily note.
-
#get_memory_context(include_today: true) ⇒ String
Get memory context for agent prompts.
-
#initialize(workspace) ⇒ MemoryStore
constructor
A new instance of MemoryStore.
-
#list_daily_notes ⇒ Array<Hash>
List all daily notes.
-
#read_daily_note(date) ⇒ String?
Read a specific daily note.
-
#read_long_term ⇒ String?
Read long-term memory.
-
#read_today ⇒ String?
Read today's daily note.
-
#write_long_term(content) ⇒ Object
Write to long-term memory.
-
#write_today(content) ⇒ Object
Write to today's daily note.
Constructor Details
#initialize(workspace) ⇒ MemoryStore
Returns a new instance of MemoryStore.
13 14 15 16 17 |
# File 'lib/nanobot/agent/memory.rb', line 13 def initialize(workspace) @workspace = Pathname.new(workspace). @memory_dir = @workspace / 'memory' @memory_dir.mkpath unless @memory_dir.exist? end |
Instance Attribute Details
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
10 11 12 |
# File 'lib/nanobot/agent/memory.rb', line 10 def workspace @workspace end |
Instance Method Details
#append_long_term(content) ⇒ Object
Append to long-term memory
37 38 39 40 41 42 43 44 |
# File 'lib/nanobot/agent/memory.rb', line 37 def append_long_term(content) memory_file = @memory_dir / 'MEMORY.md' current = memory_file.exist? ? memory_file.read : '' # Add separator if there's existing content separator = current.empty? ? '' : "\n\n" memory_file.write(current + separator + content) end |
#append_today(content, timestamp: true) ⇒ Object
Append to today's daily note
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nanobot/agent/memory.rb', line 65 def append_today(content, timestamp: true) file = today_file current = file.exist? ? file.read : "# Daily Notes - #{Date.today}\n\n" entry = if "\n## #{Time.now.strftime('%H:%M:%S')}\n\n#{content}" else "\n#{content}" end today_file.write(current + entry) end |
#get_memory_context(include_today: true) ⇒ String
Get memory context for agent prompts
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/nanobot/agent/memory.rb', line 81 def get_memory_context(include_today: true) parts = [] # Long-term memory long_term = read_long_term parts << "## Long-term Memory\n\n#{long_term}" if long_term && !long_term.strip.empty? # Today's notes if include_today today = read_today parts << "## Today's Notes\n\n#{today}" if today && !today.strip.empty? end return nil if parts.empty? parts.join("\n\n---\n\n") end |
#list_daily_notes ⇒ Array<Hash>
List all daily notes
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/nanobot/agent/memory.rb', line 101 def list_daily_notes notes = @memory_dir.glob('????-??-??.md').map do |path| date_str = path.basename('.md').to_s { date: Date.parse(date_str), path: path } end notes.sort_by { |n| n[:date] }.reverse end |
#read_daily_note(date) ⇒ String?
Read a specific daily note
115 116 117 118 119 120 121 122 |
# File 'lib/nanobot/agent/memory.rb', line 115 def read_daily_note(date) date_obj = date.is_a?(Date) ? date : Date.parse(date.to_s) note_file = @memory_dir / "#{date_obj.strftime('%Y-%m-%d')}.md" return nil unless note_file.exist? note_file.read end |
#read_long_term ⇒ String?
Read long-term memory
21 22 23 24 25 26 |
# File 'lib/nanobot/agent/memory.rb', line 21 def read_long_term memory_file = @memory_dir / 'MEMORY.md' return nil unless memory_file.exist? memory_file.read end |
#read_today ⇒ String?
Read today's daily note
48 49 50 51 52 53 |
# File 'lib/nanobot/agent/memory.rb', line 48 def read_today file = today_file return nil unless file.exist? file.read end |
#write_long_term(content) ⇒ Object
Write to long-term memory
30 31 32 33 |
# File 'lib/nanobot/agent/memory.rb', line 30 def write_long_term(content) memory_file = @memory_dir / 'MEMORY.md' memory_file.write(content) end |
#write_today(content) ⇒ Object
Write to today's daily note
57 58 59 60 |
# File 'lib/nanobot/agent/memory.rb', line 57 def write_today(content) file = today_file file.write(content) end |