Module: Legion::Extensions::Knowledge::Runners::Monitor

Included in:
Client
Defined in:
lib/legion/extensions/knowledge/runners/monitor.rb

Overview

rubocop:disable Legion/Extension/RunnerIncludeHelpers

Constant Summary collapse

DEFAULT_EXTENSIONS =
%w[.md .txt].freeze

Class Method Summary collapse

Class Method Details

.add_monitor(path:, extensions: nil, label: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/knowledge/runners/monitor.rb', line 25

def add_monitor(path:, extensions: nil, label: nil)
  abs_path = File.expand_path(path)
  return { success: false, error: "Path #{abs_path} does not exist or is not a directory" } unless File.directory?(abs_path)

  existing = Array(read_monitors_setting)
  return { success: false, error: "Path #{abs_path} is already registered" } if existing.any? { |m| m[:path] == abs_path }

  entry = {
    path:       abs_path,
    extensions: extensions || DEFAULT_EXTENSIONS.dup,
    label:      label || File.basename(abs_path),
    added_at:   Time.now.utc.iso8601
  }

  existing << entry
  persist_monitors(existing)

  { success: true, monitor: entry }
rescue StandardError => e
  { success: false, error: e.message }
end

.list_monitorsObject



60
61
62
63
64
# File 'lib/legion/extensions/knowledge/runners/monitor.rb', line 60

def list_monitors
  { success: true, monitors: resolve_monitors }
rescue StandardError => e
  { success: false, error: e.message }
end

.monitor_statusObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/knowledge/runners/monitor.rb', line 66

def monitor_status
  monitors    = resolve_monitors
  total_files = 0

  monitors.each do |m|
    scan = Helpers::Manifest.scan(path: m[:path], extensions: m[:extensions])
    total_files += scan.size
  rescue StandardError => _e
    next
  end

  { success: true, total_monitors: monitors.size, total_files: total_files }
rescue StandardError => e
  { success: false, error: e.message }
end

.remove_monitor(identifier:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/knowledge/runners/monitor.rb', line 47

def remove_monitor(identifier:)
  existing = Array(read_monitors_setting)
  found = existing.find { |m| m[:path] == identifier || m[:label] == identifier }
  return { success: false, error: "Monitor '#{identifier}' not found" } unless found

  existing.delete(found)
  persist_monitors(existing)

  { success: true, removed: found }
rescue StandardError => e
  { success: false, error: e.message }
end

.resolve_monitorsObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/knowledge/runners/monitor.rb', line 12

def resolve_monitors
  monitors = Array(read_monitors_setting)
  legacy   = read_legacy_corpus_path

  if legacy && !legacy.empty? && monitors.none? { |m| m[:path] == legacy }
    monitors << { path: legacy, extensions: %w[.md .txt .docx .pdf], label: 'legacy' }
  end

  monitors
rescue StandardError => _e
  []
end