Module: Legion::Helpers::Context

Defined in:
lib/legion/helpers/context.rb

Class Method Summary collapse

Class Method Details

.cleanup(max_age: 86_400) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/helpers/context.rb', line 35

def cleanup(max_age: 86_400)
  return { success: true, removed: 0 } unless Dir.exist?(context_dir)

  cutoff = Time.now.utc - max_age
  removed = 0
  Dir.glob(File.join(context_dir, '**', '*')).select { |f| File.file?(f) }.each do |f|
    next unless File.mtime(f) < cutoff

    File.delete(f)
    removed += 1
  end
  { success: true, removed: removed }
end

.context_dirObject



49
50
51
52
# File 'lib/legion/helpers/context.rb', line 49

def context_dir
  dir = Legion::Settings.dig(:context, :directory) if defined?(Legion::Settings)
  dir || File.join(Dir.pwd, '.legion-context')
end

.list(agent_id: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/helpers/context.rb', line 23

def list(agent_id: nil)
  base = agent_id ? File.join(context_dir, agent_id.to_s) : context_dir
  return { success: true, files: [] } unless Dir.exist?(base)

  files = Dir.glob(File.join(base, '**', '*')).select { |f| File.file?(f) }
                                              .map do |f|
    f.sub("#{context_dir}/",
          '')
  end
  { success: true, files: files }
end

.read(agent_id:, filename:) ⇒ Object



16
17
18
19
20
21
# File 'lib/legion/helpers/context.rb', line 16

def read(agent_id:, filename:)
  path = agent_path(agent_id, filename)
  return { success: false, reason: :not_found } unless File.exist?(path)

  { success: true, content: File.read(path), path: path }
end

.write(agent_id:, filename:, content:) ⇒ Object



9
10
11
12
13
14
# File 'lib/legion/helpers/context.rb', line 9

def write(agent_id:, filename:, content:)
  path = agent_path(agent_id, filename)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, content)
  { success: true, path: path }
end