Class: Legion::CLI::Chat::Tools::ConsolidateMemory

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/legion/cli/chat/tools/consolidate_memory.rb

Constant Summary collapse

CONSOLIDATION_PROMPT =
<<~PROMPT
  You are a memory consolidation engine. Given a list of memory entries, produce a cleaned-up version that:

  1. Removes exact or near-duplicate entries (keep the most complete version)
  2. Merges entries about the same topic into a single clear statement
  3. Preserves all unique and valuable information
  4. Keeps entries concise — one line per memory
  5. Drops entries that are purely temporary or session-specific
  6. Preserves the most recent timestamp when merging

  Return ONLY the consolidated entries, one per line, each prefixed with "- ".
  Do NOT add headers, explanations, or commentary.
PROMPT

Instance Method Summary collapse

Instance Method Details

#execute(scope: 'project', dry_run: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/cli/chat/tools/consolidate_memory.rb', line 37

def execute(scope: 'project', dry_run: nil)
  dry_run = dry_run.to_s == 'true'
  scope_sym = scope.to_s == 'global' ? :global : :project

  entries = MemoryStore.list(scope: scope_sym)
  return "No memory entries found in #{scope} scope." if entries.empty?
  return "Only #{entries.size} entries — no consolidation needed." if entries.size < 3

  consolidated = consolidate_entries(entries)
  return 'Consolidation failed: could not generate summary.' unless consolidated

  new_entries = parse_consolidated(consolidated)
  removed = entries.size - new_entries.size

  if dry_run
    preview = new_entries.map.with_index(1) { |e, i| "#{i}. #{e}" }.join("\n")
    "Preview (#{entries.size} -> #{new_entries.size}, #{removed} removed):\n\n#{preview}"
  else
    write_consolidated(new_entries, scope_sym)
    "Consolidated #{scope} memory: #{entries.size} -> #{new_entries.size} entries (#{removed} removed/merged)"
  end
rescue StandardError => e
  Legion::Logging.warn("ConsolidateMemory#execute failed: #{e.message}") if defined?(Legion::Logging)
  "Error consolidating memory: #{e.message}"
end