Class: Legion::CLI::Chat::Tools::QueryKnowledge

Inherits:
Tools::Base
  • Object
show all
Defined in:
lib/legion/cli/chat/tools/query_knowledge.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



64
65
66
67
68
69
70
# File 'lib/legion/cli/chat/tools/query_knowledge.rb', line 64

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

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

.apollo_query(query:, domain:, limit:) ⇒ Object



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

def self.apollo_query(query:, domain:, limit:)
  body = { query: query, limit: limit, status: %w[confirmed candidate] }
  body[:domain] = domain if domain

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

.call(query:, domain: nil, limit: nil) ⇒ Object



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

def self.call(query:, domain: nil, limit: nil)
  limit = (limit || 10).clamp(1, 50)
  data = apollo_query(query: query, domain: domain, limit: limit)

  return "Apollo knowledge graph error: #{data[:error]}" if data[:error]

  entries = data[:entries] || []
  return 'No knowledge entries found matching that query.' if entries.empty?

  format_entries(entries)
rescue StandardError => e
  Legion::Logging.warn("QueryKnowledge#execute failed: #{e.message}") if defined?(Legion::Logging)
  "Error querying knowledge graph: #{e.message}"
end

.format_entries(entries) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/legion/cli/chat/tools/query_knowledge.rb', line 72

def self.format_entries(entries)
  parts = entries.map.with_index(1) do |entry, idx|
    confidence = entry[:confidence] ? " (confidence: #{entry[:confidence]})" : ''
    tags = entry[:tags]&.any? ? " [#{entry[:tags].join(', ')}]" : ''
    "#{idx}. [#{entry[:content_type] || 'unknown'}]#{confidence} #{entry[:content]}#{tags}"
  end

  "Found #{entries.size} knowledge entries:\n\n#{parts.join("\n")}"
end