Class: Legion::CLI::Chat::Tools::KnowledgeStats

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

Constant Summary collapse

DEFAULT_PORT =
4567
DEFAULT_HOST =
'127.0.0.1'

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



48
49
50
51
52
53
54
# File 'lib/legion/cli/chat/tools/knowledge_stats.rb', line 48

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

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

.callObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/cli/chat/tools/knowledge_stats.rb', line 26

def self.call
  data = fetch_stats
  return "Apollo error: #{data[:error]}" if data[:error]

  format_stats(data)
rescue Errno::ECONNREFUSED
  'Apollo unavailable (daemon not running).'
rescue StandardError => e
  Legion::Logging.warn("KnowledgeStats#execute failed: #{e.message}") if defined?(Legion::Logging)
  "Error fetching knowledge stats: #{e.message}"
end

.fetch_statsObject



38
39
40
41
42
43
44
45
46
# File 'lib/legion/cli/chat/tools/knowledge_stats.rb', line 38

def self.fetch_stats
  uri = URI("http://#{DEFAULT_HOST}:#{apollo_port}/api/apollo/stats")
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = 3
  http.read_timeout = 10
  response = http.get(uri.request_uri)
  parsed = ::JSON.parse(response.body, symbolize_names: true)
  parsed[:data] || parsed
end

.format_breakdown(title, hash) ⇒ Object



68
69
70
71
72
73
# File 'lib/legion/cli/chat/tools/knowledge_stats.rb', line 68

def self.format_breakdown(title, hash)
  return nil if hash.nil? || hash.empty?

  parts = hash.map { |key, count| "    #{key}: #{count}" }
  "\n  #{title}:\n#{parts.join("\n")}"
end

.format_stats(data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cli/chat/tools/knowledge_stats.rb', line 56

def self.format_stats(data)
  lines = ["Apollo Knowledge Graph Statistics:\n"]
  lines << "  Total entries: #{data[:total_entries] || 0}"
  lines << "  Recent (24h): #{data[:recent_24h] || 0}"
  lines << "  Avg confidence: #{data[:avg_confidence] || 0.0}"

  lines << format_breakdown('By Status', data[:by_status])
  lines << format_breakdown('By Content Type', data[:by_content_type])

  lines.compact.join("\n")
end