Class: Legion::CLI::Memory

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/memory_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/legion/cli/memory_command.rb', line 9

def self.exit_on_failure?
  true
end

Instance Method Details

#add(text) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/legion/cli/memory_command.rb', line 40

def add(text)
  out = formatter
  require 'legion/cli/chat/memory_store'
  scope = options[:global] ? :global : :project
  path = Chat::MemoryStore.add(text, scope: scope)
  out.success("Added to #{scope} memory (#{path})")
end

#clearObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/legion/cli/memory_command.rb', line 85

def clear
  out = formatter
  scope = options[:global] ? :global : :project

  unless options[:yes]
    $stderr.print "Clear all #{scope} memory? [y/n] "
    response = $stdin.gets&.strip&.downcase
    return unless %w[y yes].include?(response)
  end

  require 'legion/cli/chat/memory_store'
  if Chat::MemoryStore.clear(scope: scope)
    out.success("#{scope.to_s.capitalize} memory cleared.")
  else
    out.warn('No memory file to clear.')
  end
end

#consolidateObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/legion/cli/memory_command.rb', line 106

def consolidate
  out = formatter
  require 'legion/memory/consolidator'

  out.header('Cross-Session Memory Consolidation')

  unless Legion::Memory::Consolidator.enabled?
    out.warn('Consolidation is disabled. Enable with memory.consolidation.enabled: true')
    return
  end

  unless options[:force]
    gates = Legion::Memory::Consolidator.gate_status
    out.detail({
                 'Time gate'    => gates[:time_gate] ? 'pass' : 'fail',
                 'Session gate' => gates[:session_gate] ? 'pass' : 'fail',
                 'Lock gate'    => gates[:lock_gate] ? 'pass' : 'fail'
               })
  end

  result = Legion::Memory::Consolidator.run(force: options[:force])

  if options[:json]
    out.json(result)
    return
  end

  if result[:success]
    out.success("Consolidated #{result[:insights_count]} insights from #{result[:transcripts_scanned]} sessions")
  else
    out.warn("Consolidation skipped: #{result[:reason]}")
  end
end

#forget(pattern) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/cli/memory_command.rb', line 49

def forget(pattern)
  out = formatter
  require 'legion/cli/chat/memory_store'
  scope = options[:global] ? :global : :project
  removed = Chat::MemoryStore.forget(pattern, scope: scope)

  if removed.zero?
    out.warn("No entries matching '#{pattern}' found.")
  else
    out.success("Removed #{removed} entry/entries matching '#{pattern}'")
  end
end

#listObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/cli/memory_command.rb', line 19

def list
  out = formatter
  require 'legion/cli/chat/memory_store'
  scope = options[:global] ? :global : :project
  entries = Chat::MemoryStore.list(scope: scope)

  if entries.empty?
    out.warn('No memory entries found.')
    return
  end

  if options[:json]
    out.json({ entries: entries, scope: scope.to_s })
  else
    out.header("#{scope.to_s.capitalize} Memory (#{entries.length} entries)")
    entries.each { |e| puts "  - #{e}" }
  end
end

#search(query) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/cli/memory_command.rb', line 63

def search(query)
  out = formatter
  require 'legion/cli/chat/memory_store'
  results = Chat::MemoryStore.search(query)

  if results.empty?
    out.warn("No results for '#{query}'")
    return
  end

  if options[:json]
    out.json({ results: results, query: query })
  else
    results.each do |r|
      source = File.basename(File.dirname(r[:source]))
      puts "  #{source}:#{r[:line]} #{r[:text]}"
    end
  end
end

#statusObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/legion/cli/memory_command.rb', line 141

def status
  out = formatter
  require 'legion/memory/consolidator'

  gates = Legion::Memory::Consolidator.gate_status
  settings = Legion::Memory::Consolidator.consolidation_settings

  if options[:json]
    out.json({ gates: gates, settings: settings, enabled: Legion::Memory::Consolidator.enabled? })
    return
  end

  out.header('Consolidation Status')
  out.detail({
               'Enabled'      => Legion::Memory::Consolidator.enabled?.to_s,
               'Time gate'    => gates[:time_gate] ? 'pass' : "fail (< #{settings[:min_hours]}h since last run)",
               'Session gate' => gates[:session_gate] ? 'pass' : "fail (< #{settings[:min_sessions]} new sessions)",
               'Lock gate'    => gates[:lock_gate] ? 'pass' : 'fail (consolidation in progress)'
             })
end