Class: Legion::CLI::Chat::Tools::SearchMemory
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
.api_port ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/legion/cli/chat/tools/search_memory.rb', line 66
def self.api_port
return DEFAULT_PORT unless defined?(Legion::Settings)
Legion::Settings[:api]&.dig(:port) || DEFAULT_PORT
rescue StandardError
DEFAULT_PORT
end
|
.call(query:) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/legion/cli/chat/tools/search_memory.rb', line 25
def self.call(query:)
require 'legion/cli/chat/memory_store'
sections = []
memory_results = MemoryStore.search(query)
unless memory_results.empty?
lines = memory_results.map { |r| "- #{r[:text]}" }
sections << "Memory matches (#{memory_results.size}):\n#{lines.join("\n")}"
end
apollo_results = search_apollo(query)
if apollo_results&.any?
lines = apollo_results.map { |r| "- [#{r[:type] || 'fact'}] #{r[:content]} (confidence: #{r[:confidence] || 'n/a'})" }
sections << "Apollo knowledge (#{apollo_results.size}):\n#{lines.join("\n")}"
end
return 'No matching memories or knowledge found.' if sections.empty?
sections.join("\n\n")
rescue StandardError => e
Legion::Logging.warn("SearchMemory#execute failed: #{e.message}") if defined?(Legion::Logging)
"Error searching memory: #{e.message}"
end
|
.search_apollo(query) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/legion/cli/chat/tools/search_memory.rb', line 49
def self.search_apollo(query)
require 'net/http'
require 'json'
uri = URI("http://#{DEFAULT_HOST}:#{api_port}/api/apollo/query")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 2
http.read_timeout = 5
request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
request.body = ::JSON.generate({ query: query, limit: 5 })
response = http.request(request)
data = ::JSON.parse(response.body, symbolize_names: true)
data[:data] || data[:results] || []
rescue StandardError
nil
end
|