Class: Legion::CLI::Chat::Tools::RelateKnowledge

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



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

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

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


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

def self.apollo_related(entry_id, params)
  query_string = params.map { |k, v| "#{k}=#{v}" }.join('&')
  path = "/api/apollo/entries/#{entry_id}/related"
  path += "?#{query_string}" unless query_string.empty?

  uri = URI("http://#{DEFAULT_HOST}:#{apollo_port}#{path}")
  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

.call(entry_id:, relation_types: nil, depth: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/cli/chat/tools/relate_knowledge.rb', line 35

def self.call(entry_id:, relation_types: nil, depth: nil)
  depth = (depth || 2).clamp(1, 3)
  params = { depth: depth }
  params[:relation_types] = relation_types if relation_types

  data = apollo_related(entry_id, params)
  return "Apollo error: #{data[:error]}" if data[:error]

  entries = data[:entries] || []
  return "No related entries found for entry ##{entry_id}." if entries.empty?

  format_related(entry_id, entries, depth)
rescue Errno::ECONNREFUSED
  'Apollo unavailable (daemon not running).'
rescue StandardError => e
  Legion::Logging.warn("RelateKnowledge#execute failed: #{e.message}") if defined?(Legion::Logging)
  "Error finding related entries: #{e.message}"
end


76
77
78
79
80
81
82
83
84
# File 'lib/legion/cli/chat/tools/relate_knowledge.rb', line 76

def self.format_related(entry_id, entries, depth)
  header = "Related entries for ##{entry_id} (depth: #{depth}, found: #{entries.size}):\n\n"
  parts = entries.map.with_index(1) do |entry, idx|
    relation = entry[:relation_type] ? " [#{entry[:relation_type]}]" : ''
    confidence = entry[:confidence] ? " (conf: #{entry[:confidence]})" : ''
    "#{idx}.#{relation}#{confidence} #{entry[:content]}"
  end
  header + parts.join("\n")
end