Class: Legion::CLI::Chat::Tools::KnowledgeMaintenance

Inherits:
Tools::Base
  • Object
show all
Defined in:
lib/legion/cli/chat/tools/knowledge_maintenance.rb

Constant Summary collapse

DEFAULT_PORT =
4567
DEFAULT_HOST =
'127.0.0.1'
VALID_ACTIONS =
%w[decay_cycle corroboration].freeze

Class Method Summary collapse

Methods inherited from Tools::Base

deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, sticky, tags, text_response, tool_name, trigger_words

Class Method Details

.apollo_portObject



61
62
63
64
65
66
67
# File 'lib/legion/cli/chat/tools/knowledge_maintenance.rb', line 61

def self.apollo_port
  return DEFAULT_PORT unless defined?(Legion::Settings)

  Legion::Settings[:api]&.dig(:port) || DEFAULT_PORT
rescue StandardError
  DEFAULT_PORT
end

.call(action:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/cli/chat/tools/knowledge_maintenance.rb', line 34

def self.call(action:)
  action = action.to_s.strip
  return "Invalid action: #{action}. Must be one of: #{VALID_ACTIONS.join(', ')}" unless VALID_ACTIONS.include?(action)

  data = run_maintenance(action)
  return "Apollo error: #{data[:error]}" if data[:error]

  format_result(action, data)
rescue Errno::ECONNREFUSED
  'Apollo unavailable (daemon not running).'
rescue StandardError => e
  Legion::Logging.warn("KnowledgeMaintenance#execute failed: #{e.message}") if defined?(Legion::Logging)
  "Error running maintenance: #{e.message}"
end

.format_corroboration_result(data) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/legion/cli/chat/tools/knowledge_maintenance.rb', line 90

def self.format_corroboration_result(data)
  checked = data[:checked_count] || data[:checked] || 0
  boosted = data[:boosted_count] || data[:boosted] || 0
  header = "Corroboration check complete:\n"
  header += "  Entries checked: #{checked}\n"
  header += "  Entries boosted (mutually supporting): #{boosted}\n"
  header += "  Duration: #{data[:duration_ms]}ms" if data[:duration_ms]
  header
end

.format_decay_result(data) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/legion/cli/chat/tools/knowledge_maintenance.rb', line 80

def self.format_decay_result(data)
  decayed = data[:decayed_count] || data[:decayed] || 0
  removed = data[:removed_count] || data[:removed] || 0
  header = "Decay cycle complete:\n"
  header += "  Entries decayed: #{decayed}\n"
  header += "  Entries removed (below threshold): #{removed}\n"
  header += "  Duration: #{data[:duration_ms]}ms" if data[:duration_ms]
  header
end

.format_result(action, data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/cli/chat/tools/knowledge_maintenance.rb', line 69

def self.format_result(action, data)
  case action
  when 'decay_cycle'
    format_decay_result(data)
  when 'corroboration'
    format_corroboration_result(data)
  else
    "Maintenance completed: #{data.inspect}"
  end
end

.run_maintenance(action) ⇒ Object



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

def self.run_maintenance(action)
  uri = URI("http://#{DEFAULT_HOST}:#{apollo_port}/api/apollo/maintenance")
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = 3
  http.read_timeout = 30
  req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  req.body = ::JSON.dump({ action: action })
  response = http.request(req)
  parsed = ::JSON.parse(response.body, symbolize_names: true)
  parsed[:data] || parsed
end