Module: Legion::Extensions::Apollo::Runners::Knowledge
- Includes:
- Helpers::Lex
- Included in:
- Actor::EntityWatchdog, Client
- Defined in:
- lib/legion/extensions/apollo/runners/knowledge.rb
Constant Summary collapse
- DOMAIN_ISOLATION =
{ 'claims_optimization' => ['claims_optimization'], 'clinical_care' => %w[clinical_care general], 'general' => :all }.freeze
Instance Method Summary collapse
- #deprecate_entry(entry_id:, reason:) ⇒ Object
- #handle_erasure_request(agent_id:) ⇒ Object
-
#handle_ingest(content:, content_type:, tags: [], source_agent: 'unknown', source_provider: nil, source_channel: nil, knowledge_domain: nil, context: {}) ⇒ Object
rubocop:disable Metrics/ParameterLists, Layout/LineLength.
-
#handle_query(query:, limit: 10, min_confidence: 0.3, status: [:confirmed], tags: nil, domain: nil, agent_id: 'unknown') ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #prepare_mesh_export(target_domain:, min_confidence: 0.5, limit: 100) ⇒ Object
- #query_knowledge(query:, limit: 10, min_confidence: 0.3, status: [:confirmed], tags: nil) ⇒ Object
- #redistribute_knowledge(agent_id:, min_confidence: 0.5) ⇒ Object
- #related_entries(entry_id:, relation_types: nil, depth: 2) ⇒ Object
-
#retrieve_relevant(query: nil, limit: 5, min_confidence: 0.3, tags: nil, domain: nil, skip: false) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #store_knowledge(content:, content_type:, tags: [], source_agent: nil, context: {}) ⇒ Object
Instance Method Details
#deprecate_entry(entry_id:, reason:) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 54 def deprecate_entry(entry_id:, reason:, **) { action: :deprecate, entry_id: entry_id, reason: reason } end |
#handle_erasure_request(agent_id:) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 239 def handle_erasure_request(agent_id:, **) unless defined?(Legion::Data) && Legion::Data.respond_to?(:connection) && Legion::Data.connection return { deleted: 0, redacted: 0, error: 'apollo_data_not_available' } end conn = Legion::Data.connection # Delete entries solely from dead agent (not confirmed by others) deleted = conn[:apollo_entries] .where(source_agent: agent_id) .exclude(status: 'confirmed') .delete # Redact attribution on confirmed entries (corroborated, retain knowledge) redacted = conn[:apollo_entries] .where(source_agent: agent_id, status: 'confirmed') .update(source_agent: 'redacted', source_provider: nil, source_channel: nil) { deleted: deleted, redacted: redacted, agent_id: agent_id } rescue Sequel::Error => e { deleted: 0, redacted: 0, error: e. } end |
#handle_ingest(content:, content_type:, tags: [], source_agent: 'unknown', source_provider: nil, source_channel: nil, knowledge_domain: nil, context: {}) ⇒ Object
rubocop:disable Metrics/ParameterLists, Layout/LineLength
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 62 def handle_ingest(content:, content_type:, tags: [], source_agent: 'unknown', source_provider: nil, source_channel: nil, knowledge_domain: nil, context: {}, **) # rubocop:disable Metrics/ParameterLists, Layout/LineLength return { success: false, error: 'apollo_data_not_available' } unless defined?(Legion::Data::Model::ApolloEntry) = Helpers::Embedding.generate(text: content) content_type_sym = content_type.to_s tag_array = Array() domain = knowledge_domain || tag_array.first || 'general' corroborated, existing_id = find_corroboration(, content_type_sym, source_agent, source_channel) unless corroborated new_entry = Legion::Data::Model::ApolloEntry.create( content: content, content_type: content_type_sym, confidence: Helpers::Confidence::INITIAL_CONFIDENCE, source_agent: source_agent, source_provider: source_provider || derive_provider_from_agent(source_agent), source_channel: source_channel, source_context: ::JSON.dump(context.is_a?(Hash) ? context : {}), tags: Sequel.pg_array(tag_array), status: 'candidate', knowledge_domain: domain, embedding: Sequel.lit("'[#{.join(',')}]'::vector") ) existing_id = new_entry.id end upsert_expertise(source_agent: source_agent, domain: domain) Legion::Data::Model::ApolloAccessLog.create( entry_id: existing_id, agent_id: source_agent, action: 'ingest' ) contradictions = detect_contradictions(existing_id, , content) { success: true, entry_id: existing_id, status: corroborated ? 'corroborated' : 'candidate', corroborated: corroborated, contradictions: contradictions } rescue Sequel::Error => e { success: false, error: e. } end |
#handle_query(query:, limit: 10, min_confidence: 0.3, status: [:confirmed], tags: nil, domain: nil, agent_id: 'unknown') ⇒ Object
rubocop:disable Metrics/ParameterLists
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 103 def handle_query(query:, limit: 10, min_confidence: 0.3, status: [:confirmed], tags: nil, domain: nil, agent_id: 'unknown', **) # rubocop:disable Metrics/ParameterLists return { success: false, error: 'apollo_data_not_available' } unless defined?(Legion::Data::Model::ApolloEntry) = Helpers::Embedding.generate(text: query) sql = Helpers::GraphQuery.build_semantic_search_sql( limit: limit, min_confidence: min_confidence, statuses: Array(status).map(&:to_s), tags: , domain: domain ) db = Legion::Data::Model::ApolloEntry.db entries = db.fetch(sql, embedding: Sequel.lit("'[#{.join(',')}]'::vector")).all entries.each do |entry| Legion::Data::Model::ApolloEntry.where(id: entry[:id]).update( access_count: Sequel.expr(:access_count) + 1, confidence: Helpers::Confidence.apply_retrieval_boost( confidence: entry[:confidence] ), updated_at: Time.now ) end if entries.any? Legion::Data::Model::ApolloAccessLog.create( entry_id: entries.first&.dig(:id), agent_id: agent_id, action: 'query' ) end formatted = entries.map do |entry| { id: entry[:id], content: entry[:content], content_type: entry[:content_type], confidence: entry[:confidence], distance: entry[:distance], tags: entry[:tags], source_agent: entry[:source_agent], knowledge_domain: entry[:knowledge_domain] } end { success: true, entries: formatted, count: formatted.size } rescue Sequel::Error => e { success: false, error: e. } end |
#prepare_mesh_export(target_domain:, min_confidence: 0.5, limit: 100) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 211 def prepare_mesh_export(target_domain:, min_confidence: 0.5, limit: 100, **) unless defined?(Legion::Data) && Legion::Data.respond_to?(:connection) && Legion::Data.connection return { success: false, error: 'apollo_data_not_available' } end conn = Legion::Data.connection allowed = allowed_domains_for(target_domain) dataset = conn[:apollo_entries] .where(status: 'confirmed') .where { confidence >= min_confidence } .limit(limit) dataset = dataset.where(knowledge_domain: allowed) unless allowed == :all entries = dataset.all formatted = entries.map do |entry| { id: entry[:id], content: entry[:content], content_type: entry[:content_type], confidence: entry[:confidence], knowledge_domain: entry[:knowledge_domain], tags: entry[:tags], source_agent: entry[:source_agent] } end { success: true, entries: formatted, count: formatted.size, target_domain: target_domain } rescue Sequel::Error => e { success: false, error: e. } end |
#query_knowledge(query:, limit: 10, min_confidence: 0.3, status: [:confirmed], tags: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 34 def query_knowledge(query:, limit: 10, min_confidence: 0.3, status: [:confirmed], tags: nil, **) { action: :query, query: query, limit: limit, min_confidence: min_confidence, status: Array(status), tags: } end |
#redistribute_knowledge(agent_id:, min_confidence: 0.5) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 143 def redistribute_knowledge(agent_id:, min_confidence: 0.5, **) return { success: false, error: 'apollo_data_not_available' } unless defined?(Legion::Data::Model::ApolloEntry) entries = Legion::Data::Model::ApolloEntry .where(source_agent: agent_id, status: 'confirmed') .where { confidence > min_confidence } .all return { success: true, redistributed: 0 } if entries.empty? store = (Legion::Extensions::Agentic::Memory::Trace.shared_store if defined?(Legion::Extensions::Agentic::Memory::Trace)) redistributed = 0 entries.each do |entry| if store trace = Legion::Extensions::Agentic::Memory::Trace::Helpers::Trace.new_trace( type: :semantic, content_payload: { content: entry.content, source_agent: agent_id, content_type: entry.content_type, tags: Array(entry.) }, strength: entry.confidence.to_f, domain_tag: Array(entry.).first || 'general' ) store.store(trace) end redistributed += 1 end log.info "[apollo] redistributed #{redistributed} entries from departing agent=#{agent_id}" { success: true, redistributed: redistributed, agent_id: agent_id } rescue Sequel::Error => e { success: false, error: e. } end |
#related_entries(entry_id:, relation_types: nil, depth: 2) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 45 def (entry_id:, relation_types: nil, depth: 2, **) { action: :traverse, entry_id: entry_id, relation_types: relation_types, depth: depth } end |
#retrieve_relevant(query: nil, limit: 5, min_confidence: 0.3, tags: nil, domain: nil, skip: false) ⇒ Object
rubocop:disable Metrics/ParameterLists
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 176 def retrieve_relevant(query: nil, limit: 5, min_confidence: 0.3, tags: nil, domain: nil, skip: false, **) # rubocop:disable Metrics/ParameterLists return { status: :skipped } if skip return { success: false, error: 'apollo_data_not_available' } unless defined?(Legion::Data::Model::ApolloEntry) return { success: true, entries: [], count: 0 } if query.nil? || query.to_s.strip.empty? = Helpers::Embedding.generate(text: query.to_s) sql = Helpers::GraphQuery.build_semantic_search_sql( limit: limit, min_confidence: min_confidence, statuses: ['confirmed'], tags: , domain: domain ) db = Legion::Data::Model::ApolloEntry.db entries = db.fetch(sql, embedding: Sequel.lit("'[#{.join(',')}]'::vector")).all entries.each do |entry| Legion::Data::Model::ApolloEntry.where(id: entry[:id]).update( confidence: Helpers::Confidence.apply_retrieval_boost(confidence: entry[:confidence]), updated_at: Time.now ) end formatted = entries.map do |entry| { id: entry[:id], content: entry[:content], content_type: entry[:content_type], confidence: entry[:confidence], distance: entry[:distance], tags: entry[:tags], source_agent: entry[:source_agent], knowledge_domain: entry[:knowledge_domain] } end { success: true, entries: formatted, count: formatted.size } rescue Sequel::Error => e { success: false, error: e. } end |
#store_knowledge(content:, content_type:, tags: [], source_agent: nil, context: {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/legion/extensions/apollo/runners/knowledge.rb', line 18 def store_knowledge(content:, content_type:, tags: [], source_agent: nil, context: {}, **) content_type = content_type.to_sym unless Helpers::Confidence::CONTENT_TYPES.include?(content_type) raise ArgumentError, "invalid content_type: #{content_type}. Must be one of #{Helpers::Confidence::CONTENT_TYPES}" end { action: :store, content: content, content_type: content_type, tags: Array(), source_agent: source_agent, context: context } end |