Class: Legion::CLI::Chat::Tools::IngestKnowledge

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

Constant Summary collapse

DEFAULT_PORT =
4567
DEFAULT_HOST =
'127.0.0.1'
VALID_TYPES =
%w[fact observation concept procedure decision].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_ingest(content:, content_type:, tags:, knowledge_domain:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/cli/chat/tools/ingest_knowledge.rb', line 69

def self.apollo_ingest(content:, content_type:, tags:, knowledge_domain:)
  body = {
    content:        content,
    content_type:   content_type,
    tags:           tags,
    source_agent:   'chat',
    source_channel: 'chat_tool'
  }
  body[:knowledge_domain] = knowledge_domain if knowledge_domain

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

.apollo_portObject



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

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

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

.call(content:, content_type: nil, tags: nil, knowledge_domain: nil) ⇒ Object



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

def self.call(content:, content_type: nil, tags: nil, knowledge_domain: nil)
  content_type = sanitize_type(content_type)
  tag_list = parse_tags(tags)

  data = apollo_ingest(
    content:          content,
    content_type:     content_type,
    tags:             tag_list,
    knowledge_domain: knowledge_domain
  )

  return "Failed to ingest: #{data[:error]}" if data[:error]

  id = data[:id] || data[:entry_id]
  "Saved to Apollo knowledge graph (id: #{id}, type: #{content_type}, tags: #{tag_list.join(', ')})"
rescue Errno::ECONNREFUSED
  'Apollo unavailable (daemon not running). Knowledge was not saved.'
rescue StandardError => e
  Legion::Logging.warn("IngestKnowledge#execute failed: #{e.message}") if defined?(Legion::Logging)
  "Error saving to knowledge graph: #{e.message}"
end

.parse_tags(tags) ⇒ Object



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

def self.parse_tags(tags)
  return [] unless tags.is_a?(String) && !tags.empty?

  tags.split(',').map(&:strip).reject(&:empty?)
end

.sanitize_type(content_type) ⇒ Object



58
59
60
61
# File 'lib/legion/cli/chat/tools/ingest_knowledge.rb', line 58

def self.sanitize_type(content_type)
  type = (content_type || 'observation').to_s.downcase
  VALID_TYPES.include?(type) ? type : 'observation'
end