Class: Nanobot::Agent::MemoryStore

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(workspace) ⇒ MemoryStore

Returns a new instance of MemoryStore.

Parameters:

  • workspace (String, Pathname) —

    path to the agent workspace directory



13
14
15
16
17
# File 'lib/nanobot/agent/memory.rb', line 13

def initialize(workspace)
  @workspace = Pathname.new(workspace).expand_path
  @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

Parameters:

  • content (String) —

    content to append



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

Parameters:

  • content (String) —

    content to append

  • timestamp (Boolean) (defaults to: true) —

    whether to add timestamp (default: true)



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 timestamp
            "\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

Parameters:

  • include_today (Boolean) (defaults to: true) —

    whether to include today's notes

Returns:

  • (String) —

    formatted memory context



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

Returns:

  • (Array<Hash>) —

    array of Date, path: Pathname



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

Parameters:

  • date (Date, String) —

    the date to read

Returns:

  • (String, nil)


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

Returns:

  • (String, nil) —

    content of MEMORY.md or nil if not exists



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

Returns:

  • (String, nil) —

    content of today's note or nil if not exists



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

Parameters:

  • content (String) —

    content to write



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

Parameters:

  • content (String) —

    content to write



57
58
59
60
# File 'lib/nanobot/agent/memory.rb', line 57

def write_today(content)
  file = today_file
  file.write(content)
end