lex-apollo 0.4.30 → 0.4.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +125 -0
- data/README.md +113 -18
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +5 -19
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +116 -51
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -63
- data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +107 -67
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +67 -109
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +165 -144
- data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
- data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -47
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/transport.rb +20 -0
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +26 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +250 -28
- data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +88 -80
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +98 -71
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
- data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
- data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
- data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
- data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +108 -0
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +146 -107
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/spec_helper.rb +13 -15
- metadata +71 -13
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -49
- data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
- data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
- data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/extensions/actors/subscription'
|
|
4
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
5
|
+
require 'legion/extensions/apollo/transport/subscription_delivery'
|
|
4
6
|
|
|
5
7
|
module Legion
|
|
6
8
|
module Extensions
|
|
7
9
|
module Apollo
|
|
8
10
|
module Actor
|
|
9
11
|
class Ingest < Legion::Extensions::Actors::Subscription
|
|
10
|
-
|
|
12
|
+
include Legion::Extensions::Apollo::Transport::SubscriptionDelivery
|
|
13
|
+
|
|
14
|
+
def runner_class = Legion::Extensions::Apollo::Runners::Knowledge
|
|
11
15
|
def runner_function = 'handle_ingest'
|
|
12
16
|
def check_subtask? = false
|
|
13
17
|
def generate_task? = false
|
|
14
18
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
Legion.const_defined?(:Transport, false)
|
|
18
|
-
rescue StandardError => e
|
|
19
|
-
handle_exception(e, level: :warn, operation: 'apollo.ingest.enabled')
|
|
20
|
-
false
|
|
19
|
+
def process_message(message, metadata, delivery_info, **)
|
|
20
|
+
{ identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }.merge(super(message, metadata, delivery_info))
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
|
@@ -1,22 +1,41 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/extensions/actors/subscription'
|
|
4
|
+
require 'legion/extensions/apollo/runners/query_response'
|
|
5
|
+
require 'legion/extensions/apollo/transport/subscription_delivery'
|
|
4
6
|
|
|
5
7
|
module Legion
|
|
6
8
|
module Extensions
|
|
7
9
|
module Apollo
|
|
8
10
|
module Actor
|
|
9
11
|
class QueryResponder < Legion::Extensions::Actors::Subscription
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
include Legion::Extensions::Apollo::Transport::SubscriptionDelivery
|
|
13
|
+
|
|
14
|
+
def runner_class = Legion::Extensions::Apollo::Runners::QueryResponse
|
|
15
|
+
def runner_function = 'handle_request'
|
|
16
|
+
def use_runner? = false
|
|
12
17
|
def check_subtask? = false
|
|
13
18
|
def generate_task? = false
|
|
14
19
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
def process_message(message, metadata, delivery_info, **)
|
|
21
|
+
super(message, metadata, delivery_info).tap do |request|
|
|
22
|
+
request[:reply_to] = metadata.reply_to if metadata.respond_to?(:reply_to) && metadata.reply_to
|
|
23
|
+
request[:correlation_id] = metadata.correlation_id if metadata.respond_to?(:correlation_id) && metadata.correlation_id
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def republish_with_retry_count(_delivery_info, metadata, payload, new_count, **)
|
|
30
|
+
headers = (metadata&.headers || {}).merge(Legion::Extensions::Actors::RetryPolicy::RETRY_COUNT_HEADER => new_count)
|
|
31
|
+
@queue.channel.default_exchange.publish(
|
|
32
|
+
payload, routing_key: @queue.name, headers: headers, content_type: metadata&.content_type,
|
|
33
|
+
content_encoding: metadata&.content_encoding, reply_to: metadata&.reply_to,
|
|
34
|
+
correlation_id: metadata&.correlation_id, persistent: true
|
|
35
|
+
)
|
|
36
|
+
true
|
|
18
37
|
rescue StandardError => e
|
|
19
|
-
handle_exception(e, level: :warn, operation: 'apollo.query_responder.
|
|
38
|
+
handle_exception(e, level: :warn, operation: 'apollo.query_responder.retry')
|
|
20
39
|
false
|
|
21
40
|
end
|
|
22
41
|
end
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/extensions/actors/subscription'
|
|
4
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
5
|
+
require 'legion/extensions/apollo/transport/subscription_delivery'
|
|
4
6
|
|
|
5
7
|
module Legion
|
|
6
8
|
module Extensions
|
|
7
9
|
module Apollo
|
|
8
10
|
module Actor
|
|
9
11
|
class WritebackStore < Legion::Extensions::Actors::Subscription
|
|
10
|
-
|
|
12
|
+
include Legion::Extensions::Apollo::Transport::SubscriptionDelivery
|
|
13
|
+
|
|
14
|
+
def runner_class = Legion::Extensions::Apollo::Runners::Knowledge
|
|
11
15
|
def runner_function = 'handle_ingest'
|
|
12
16
|
def check_subtask? = false
|
|
13
17
|
def generate_task? = false
|
|
14
18
|
|
|
19
|
+
def process_message(message, metadata, delivery_info, **)
|
|
20
|
+
{ identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }.merge(super(message, metadata, delivery_info))
|
|
21
|
+
end
|
|
22
|
+
|
|
15
23
|
def enabled? # rubocop:disable Legion/Extension/ActorEnabledSideEffects
|
|
16
|
-
|
|
17
|
-
Legion.const_defined?(:Transport, false) &&
|
|
18
|
-
Helpers::Capability.apollo_write_enabled?
|
|
24
|
+
settings[:data][:apollo_write] == true
|
|
19
25
|
rescue StandardError => e
|
|
20
26
|
handle_exception(e, level: :warn, operation: 'apollo.writeback_store.enabled')
|
|
21
27
|
false
|
|
@@ -1,55 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/extensions/actors/subscription'
|
|
4
|
+
require 'legion/extensions/apollo/runners/writeback'
|
|
5
|
+
require 'legion/extensions/apollo/transport/subscription_delivery'
|
|
4
6
|
|
|
5
7
|
module Legion
|
|
6
8
|
module Extensions
|
|
7
9
|
module Apollo
|
|
8
10
|
module Actor
|
|
9
11
|
class WritebackVectorize < Legion::Extensions::Actors::Subscription
|
|
10
|
-
|
|
12
|
+
include Legion::Extensions::Apollo::Helpers::Capability
|
|
13
|
+
include Legion::Extensions::Apollo::Transport::SubscriptionDelivery
|
|
14
|
+
|
|
15
|
+
def runner_class(**) = Legion::Extensions::Apollo::Runners::Writeback
|
|
11
16
|
def runner_function = 'handle_vectorize'
|
|
17
|
+
def use_runner? = false
|
|
12
18
|
def check_subtask? = false
|
|
13
19
|
def generate_task? = false
|
|
14
20
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
log.debug("WritebackVectorize handle_vectorize content_length=#{payload[:content].to_s.length} content_type=#{payload[:content_type] || 'nil'}")
|
|
18
|
-
result = Legion::LLM::Call::Embeddings.generate(text: payload[:content])
|
|
19
|
-
vector = result.is_a?(Hash) ? result[:vector] : result
|
|
20
|
-
embedding = vector.is_a?(Array) && vector.any? ? vector : Array.new(1024, 0.0)
|
|
21
|
-
log.debug("WritebackVectorize embedding_dimensions=#{embedding.length} vector_generated=#{vector.is_a?(Array) && vector.any?}")
|
|
22
|
-
enriched = payload.merge(embedding: embedding)
|
|
23
|
-
|
|
24
|
-
if Helpers::Capability.can_write?
|
|
25
|
-
log.debug('WritebackVectorize route=direct_ingest')
|
|
26
|
-
Runners::Knowledge.handle_ingest(**enriched)
|
|
27
|
-
else
|
|
28
|
-
log.debug('WritebackVectorize route=transport_writeback')
|
|
29
|
-
Transport::Messages::Writeback.new(
|
|
30
|
-
**enriched, has_embedding: true
|
|
31
|
-
).publish
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
log.info('WritebackVectorize completed action=vectorized')
|
|
35
|
-
{ success: true, action: :vectorized }
|
|
36
|
-
rescue StandardError => e
|
|
37
|
-
handle_exception(e, level: :error, operation: 'apollo.writeback_vectorize.handle_vectorize')
|
|
38
|
-
{ success: false, error: e.message }
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def enabled? # rubocop:disable Legion/Extension/ActorEnabledSideEffects
|
|
42
|
-
Legion.const_defined?(:Transport, false) && Helpers::Capability.can_embed?
|
|
21
|
+
def enabled?(**) # rubocop:disable Legion/Extension/ActorEnabledSideEffects
|
|
22
|
+
capability_can_embed?
|
|
43
23
|
rescue StandardError => e
|
|
44
24
|
handle_exception(e, level: :warn, operation: 'apollo.writeback_vectorize.enabled')
|
|
45
25
|
false
|
|
46
26
|
end
|
|
47
|
-
|
|
48
|
-
private
|
|
49
|
-
|
|
50
|
-
def symbolize(hash)
|
|
51
|
-
hash.transform_keys(&:to_sym)
|
|
52
|
-
end
|
|
53
27
|
end
|
|
54
28
|
end
|
|
55
29
|
end
|
|
@@ -10,12 +10,14 @@ module Legion
|
|
|
10
10
|
module Apollo
|
|
11
11
|
class Api < Sinatra::Base
|
|
12
12
|
set :host_authorization, permitted: :any
|
|
13
|
+
include Legion::Extensions::Apollo::Helpers::DataModels
|
|
14
|
+
extend Legion::Extensions::Apollo::Helpers::DataModels
|
|
13
15
|
|
|
14
16
|
class << self
|
|
15
17
|
def stats_payload(now: Time.now)
|
|
16
|
-
return { error: 'apollo_data_not_available' }
|
|
18
|
+
return { error: 'apollo_data_not_available' } if data_model_entry.nil?
|
|
17
19
|
|
|
18
|
-
entries =
|
|
20
|
+
entries = data_model_entry
|
|
19
21
|
by_status = grouped_counts(entries, :status)
|
|
20
22
|
by_status['active'] = entries.exclude(status: 'archived').count
|
|
21
23
|
|
|
@@ -26,7 +28,7 @@ module Legion
|
|
|
26
28
|
by_status: by_status,
|
|
27
29
|
by_content_type: grouped_counts(entries, :content_type)
|
|
28
30
|
}
|
|
29
|
-
stats[:total_relations] =
|
|
31
|
+
stats[:total_relations] = data_model_relation.count unless data_model_relation.nil?
|
|
30
32
|
stats
|
|
31
33
|
end
|
|
32
34
|
|
|
@@ -51,50 +53,83 @@ module Legion
|
|
|
51
53
|
include Legion::JSON::Helper
|
|
52
54
|
|
|
53
55
|
def json_body
|
|
54
|
-
body = request.body
|
|
55
|
-
return {} if body.empty?
|
|
56
|
+
body = request.body&.read
|
|
57
|
+
return {} if body.nil? || body.empty?
|
|
56
58
|
|
|
57
59
|
json_parse(body)
|
|
58
60
|
rescue Legion::JSON::ParseError => e
|
|
59
61
|
handle_exception(e, level: :warn, operation: 'apollo.api.json_body')
|
|
60
|
-
halt 400, json_dump(error: "invalid JSON: #{e.message}")
|
|
62
|
+
halt 400, json_dump({ error: "invalid JSON: #{e.message}" })
|
|
61
63
|
end
|
|
62
64
|
|
|
65
|
+
def apollo_request_identity(resolve_identity_id: false, **)
|
|
66
|
+
identity = { identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }
|
|
67
|
+
claims = env['legion.auth']
|
|
68
|
+
caller = env['legion.principal']
|
|
69
|
+
return identity unless claims.is_a?(Hash) && !claims.empty? && caller
|
|
70
|
+
return identity if caller.canonical_name.to_s.empty?
|
|
71
|
+
|
|
72
|
+
return identity unless defined?(Legion::Data::Model::Identity::Principal)
|
|
73
|
+
|
|
74
|
+
principal = Legion::Data::Model::Identity::Principal[
|
|
75
|
+
canonical_name: caller.canonical_name, kind: caller.kind.to_s, active: true
|
|
76
|
+
]
|
|
77
|
+
return identity unless principal && principal[:id].is_a?(Integer) && principal[:id].positive?
|
|
78
|
+
|
|
79
|
+
identity[:identity_principal_id] = principal[:id]
|
|
80
|
+
identity[:identity_canonical_name] = caller.canonical_name
|
|
81
|
+
identity[:identity_id] = apollo_request_identity_id(caller: caller, principal_id: principal[:id]) if resolve_identity_id
|
|
82
|
+
identity
|
|
83
|
+
rescue Sequel::Error => e
|
|
84
|
+
handle_exception(e, level: :warn, operation: 'apollo.api.request_identity')
|
|
85
|
+
{ identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def apollo_request_identity_id(caller:, principal_id:, **)
|
|
89
|
+
return unless defined?(Legion::Data::Model::Identity::Provider) && defined?(Legion::Data::Model::Identity::Identity)
|
|
90
|
+
return if caller.source.nil? || caller.principal_id.nil?
|
|
91
|
+
|
|
92
|
+
provider = Legion::Data::Model::Identity::Provider[name: caller.source.to_s]
|
|
93
|
+
return unless provider
|
|
94
|
+
|
|
95
|
+
identity = Legion::Data::Model::Identity::Identity[
|
|
96
|
+
principal_id: principal_id, provider_id: provider[:id], provider_identity_key: caller.principal_id.to_s, active: true
|
|
97
|
+
]
|
|
98
|
+
identity && identity[:id]
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
helpers do
|
|
63
103
|
def runner
|
|
64
|
-
|
|
65
|
-
obj = Object.new
|
|
66
|
-
obj.extend(Runners::Knowledge)
|
|
67
|
-
obj
|
|
68
|
-
end
|
|
104
|
+
Legion::Extensions::Apollo::Runners::Knowledge
|
|
69
105
|
end
|
|
70
106
|
|
|
71
107
|
def expertise_runner
|
|
72
|
-
|
|
73
|
-
obj = Object.new
|
|
74
|
-
obj.extend(Runners::Expertise)
|
|
75
|
-
obj
|
|
76
|
-
end
|
|
108
|
+
Legion::Extensions::Apollo::Runners::Expertise
|
|
77
109
|
end
|
|
78
110
|
end
|
|
79
111
|
|
|
80
112
|
# Health check
|
|
81
113
|
get '/api/apollo/health' do
|
|
82
|
-
available =
|
|
83
|
-
json_dump(status: available ? 'ok' : 'degraded', data_available: available)
|
|
114
|
+
available = !data_model_entry.nil?
|
|
115
|
+
json_dump({ status: available ? 'ok' : 'degraded', data_available: available })
|
|
84
116
|
end
|
|
85
117
|
|
|
86
118
|
# Query knowledge (semantic search)
|
|
87
119
|
post '/api/apollo/query' do
|
|
88
120
|
req = json_body
|
|
89
|
-
halt 400, json_dump(error: 'query is required') unless req[:query]
|
|
121
|
+
halt 400, json_dump({ error: 'query is required' }) unless req[:query]
|
|
90
122
|
|
|
91
123
|
query_options = {
|
|
92
|
-
query:
|
|
93
|
-
limit:
|
|
94
|
-
min_confidence:
|
|
95
|
-
tags:
|
|
96
|
-
|
|
97
|
-
|
|
124
|
+
query: req[:query],
|
|
125
|
+
limit: req[:limit] || 10,
|
|
126
|
+
min_confidence: req[:min_confidence] || 0.3,
|
|
127
|
+
tags: req[:tags],
|
|
128
|
+
tag_match: req[:tag_match] || :any,
|
|
129
|
+
exclude_source_agents: req[:exclude_source_agents] || [],
|
|
130
|
+
domain: req[:domain],
|
|
131
|
+
agent_id: req[:agent_id] || 'api',
|
|
132
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
98
133
|
}
|
|
99
134
|
query_options[:status] = req[:status] if req.key?(:status)
|
|
100
135
|
|
|
@@ -106,18 +141,30 @@ module Legion
|
|
|
106
141
|
# Ingest knowledge
|
|
107
142
|
post '/api/apollo/ingest' do
|
|
108
143
|
req = json_body
|
|
109
|
-
halt 400, json_dump(error: 'content is required') unless req[:content]
|
|
110
|
-
halt 400, json_dump(error: 'content_type is required') unless req[:content_type]
|
|
144
|
+
halt 400, json_dump({ error: 'content is required' }) unless req[:content]
|
|
145
|
+
halt 400, json_dump({ error: 'content_type is required' }) unless req[:content_type]
|
|
146
|
+
scope = req.fetch(:access_scope, 'global')
|
|
147
|
+
halt 400, json_dump({ error: 'invalid access_scope' }) unless %w[global private team].include?(scope)
|
|
148
|
+
identity = apollo_request_identity(resolve_identity_id: true)
|
|
149
|
+
if scope != 'global' && identity[:identity_principal_id].nil?
|
|
150
|
+
halt 403, json_dump({ error: 'authenticated principal is required for restricted writes' })
|
|
151
|
+
end
|
|
111
152
|
|
|
112
153
|
result = runner.handle_ingest(
|
|
113
|
-
content:
|
|
114
|
-
content_type:
|
|
115
|
-
tags:
|
|
116
|
-
source_agent:
|
|
117
|
-
source_provider:
|
|
118
|
-
source_channel:
|
|
119
|
-
knowledge_domain:
|
|
120
|
-
|
|
154
|
+
content: req[:content],
|
|
155
|
+
content_type: req[:content_type],
|
|
156
|
+
tags: req[:tags] || [],
|
|
157
|
+
source_agent: req[:source_agent] || 'api',
|
|
158
|
+
source_provider: req[:source_provider],
|
|
159
|
+
source_channel: req[:source_channel],
|
|
160
|
+
knowledge_domain: req[:knowledge_domain],
|
|
161
|
+
submitted_by: req[:submitted_by],
|
|
162
|
+
submitted_from: req[:submitted_from],
|
|
163
|
+
access_scope: scope,
|
|
164
|
+
context: req[:context] || {},
|
|
165
|
+
embedding: req[:embedding],
|
|
166
|
+
embedding_provenance: req[:embedding_provenance],
|
|
167
|
+
**identity
|
|
121
168
|
)
|
|
122
169
|
status result[:success] ? 201 : 500
|
|
123
170
|
json_dump(result)
|
|
@@ -126,13 +173,14 @@ module Legion
|
|
|
126
173
|
# Graph traversal
|
|
127
174
|
post '/api/apollo/traverse' do
|
|
128
175
|
req = json_body
|
|
129
|
-
halt 400, json_dump(error: 'entry_id is required') unless req[:entry_id]
|
|
176
|
+
halt 400, json_dump({ error: 'entry_id is required' }) unless req[:entry_id]
|
|
130
177
|
|
|
131
178
|
result = runner.handle_traverse(
|
|
132
|
-
entry_id:
|
|
133
|
-
depth:
|
|
134
|
-
relation_types:
|
|
135
|
-
agent_id:
|
|
179
|
+
entry_id: req[:entry_id],
|
|
180
|
+
depth: req[:depth] || 2,
|
|
181
|
+
relation_types: req[:relation_types],
|
|
182
|
+
agent_id: req[:agent_id] || 'api',
|
|
183
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
136
184
|
)
|
|
137
185
|
status result[:success] ? 200 : 500
|
|
138
186
|
json_dump(result)
|
|
@@ -141,15 +189,21 @@ module Legion
|
|
|
141
189
|
# Retrieve relevant (GAIA-compatible)
|
|
142
190
|
post '/api/apollo/retrieve' do
|
|
143
191
|
req = json_body
|
|
144
|
-
halt 400, json_dump(error: 'query is required') unless req[:query]
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
query:
|
|
148
|
-
limit:
|
|
149
|
-
min_confidence:
|
|
150
|
-
tags:
|
|
151
|
-
|
|
152
|
-
|
|
192
|
+
halt 400, json_dump({ error: 'query is required' }) unless req[:query]
|
|
193
|
+
|
|
194
|
+
retrieve_options = {
|
|
195
|
+
query: req[:query],
|
|
196
|
+
limit: req[:limit] || 5,
|
|
197
|
+
min_confidence: req[:min_confidence] || 0.3,
|
|
198
|
+
tags: req[:tags],
|
|
199
|
+
tag_match: req[:tag_match] || :any,
|
|
200
|
+
exclude_source_agents: req[:exclude_source_agents] || [],
|
|
201
|
+
domain: req[:domain],
|
|
202
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
203
|
+
}
|
|
204
|
+
retrieve_options[:status] = req[:status] if req.key?(:status)
|
|
205
|
+
|
|
206
|
+
result = runner.retrieve_relevant(**retrieve_options)
|
|
153
207
|
status result[:success] ? 200 : 500
|
|
154
208
|
json_dump(result)
|
|
155
209
|
end
|
|
@@ -157,9 +211,20 @@ module Legion
|
|
|
157
211
|
# Deprecate entry
|
|
158
212
|
post '/api/apollo/entries/:id/deprecate' do
|
|
159
213
|
result = runner.deprecate_entry(
|
|
160
|
-
entry_id:
|
|
161
|
-
reason:
|
|
214
|
+
entry_id: params[:id],
|
|
215
|
+
reason: json_body[:reason] || 'deprecated via API',
|
|
216
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
162
217
|
)
|
|
218
|
+
halt 503, json_dump({ success: false, error: 'apollo_data_not_available' }) if result[:action] == :deprecate
|
|
219
|
+
|
|
220
|
+
status_code = if result[:success]
|
|
221
|
+
200
|
|
222
|
+
elsif result[:error] == 'entry_not_found'
|
|
223
|
+
404
|
|
224
|
+
else
|
|
225
|
+
500
|
|
226
|
+
end
|
|
227
|
+
status status_code
|
|
163
228
|
json_dump(result)
|
|
164
229
|
end
|
|
165
230
|
|
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
require 'legion/extensions/apollo/helpers/confidence'
|
|
4
|
+
require 'legion/extensions/apollo/helpers/similarity'
|
|
5
|
+
require 'legion/extensions/apollo/helpers/graph_query'
|
|
6
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
7
|
+
require 'legion/extensions/apollo/runners/expertise'
|
|
8
|
+
require 'legion/extensions/apollo/runners/maintenance'
|
|
9
9
|
|
|
10
10
|
module Legion
|
|
11
11
|
module Extensions
|
|
12
12
|
module Apollo
|
|
13
13
|
class Client
|
|
14
|
-
include
|
|
15
|
-
include
|
|
16
|
-
include
|
|
14
|
+
include Legion::Extensions::Apollo::Helpers::DataModels
|
|
15
|
+
include Legion::Extensions::Apollo::Helpers::Confidence
|
|
16
|
+
include Legion::Extensions::Apollo::Helpers::TagNormalizer
|
|
17
|
+
include Legion::Extensions::Apollo::Helpers::Storage
|
|
18
|
+
include Legion::Extensions::Apollo::Helpers::Embedding
|
|
19
|
+
include Legion::Extensions::Apollo::Helpers::Similarity
|
|
20
|
+
include Legion::Extensions::Apollo::Helpers::GraphQuery
|
|
21
|
+
include Legion::Extensions::Apollo::Runners::Knowledge
|
|
22
|
+
include Legion::Extensions::Apollo::Runners::Expertise
|
|
23
|
+
include Legion::Extensions::Apollo::Runners::Maintenance
|
|
17
24
|
|
|
18
25
|
attr_reader :agent_id
|
|
19
26
|
|
|
@@ -21,6 +28,10 @@ module Legion
|
|
|
21
28
|
@agent_id = agent_id
|
|
22
29
|
end
|
|
23
30
|
|
|
31
|
+
def segments
|
|
32
|
+
[:apollo]
|
|
33
|
+
end
|
|
34
|
+
|
|
24
35
|
def store_knowledge(source_agent: nil, **)
|
|
25
36
|
super(**, source_agent: source_agent || @agent_id)
|
|
26
37
|
end
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require 'legion/extensions/apollo/helpers/data_models'
|
|
4
4
|
|
|
5
5
|
module Legion
|
|
6
6
|
module Extensions
|
|
7
7
|
module Apollo
|
|
8
8
|
module GaiaIntegration
|
|
9
|
+
extend Legion::Extensions::Apollo::Helpers::DataModels
|
|
10
|
+
|
|
9
11
|
PUBLISH_CONFIDENCE_THRESHOLD = 0.6
|
|
10
12
|
PUBLISH_NOVELTY_THRESHOLD = 0.3
|
|
11
13
|
|
|
@@ -30,13 +32,13 @@ module Legion
|
|
|
30
32
|
end
|
|
31
33
|
|
|
32
34
|
def handle_mesh_departure(agent_id:)
|
|
33
|
-
return nil
|
|
35
|
+
return nil if data_model_expertise.nil?
|
|
34
36
|
|
|
35
|
-
sole_expert_domains =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
sole_expert_domains = data_model_expertise
|
|
38
|
+
.where(agent_id: agent_id)
|
|
39
|
+
.all
|
|
40
|
+
.select { |e| sole_expert?(e.domain, agent_id) }
|
|
41
|
+
.map(&:domain)
|
|
40
42
|
|
|
41
43
|
return nil if sole_expert_domains.empty?
|
|
42
44
|
|
|
@@ -51,12 +53,12 @@ module Legion
|
|
|
51
53
|
private
|
|
52
54
|
|
|
53
55
|
def sole_expert?(domain, agent_id)
|
|
54
|
-
return false
|
|
56
|
+
return false if data_model_expertise.nil?
|
|
55
57
|
|
|
56
|
-
count =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
count = data_model_expertise
|
|
59
|
+
.where(domain: domain)
|
|
60
|
+
.exclude(agent_id: agent_id)
|
|
61
|
+
.count
|
|
60
62
|
count.zero?
|
|
61
63
|
end
|
|
62
64
|
end
|
|
@@ -1,77 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'legion/data'
|
|
4
|
+
require 'legion/llm'
|
|
5
|
+
require 'legion/settings/helper'
|
|
6
|
+
|
|
3
7
|
module Legion
|
|
4
8
|
module Extensions
|
|
5
9
|
module Apollo
|
|
6
10
|
module Helpers
|
|
7
11
|
module Capability
|
|
8
|
-
|
|
9
|
-
extend Legion::Settings::Helper
|
|
10
|
-
|
|
11
|
-
EMBEDDING_MODELS = %w[mxbai-embed-large bge-large snowflake-arctic-embed].freeze
|
|
12
|
-
PRIVILEGE_MUTEX = Mutex.new
|
|
13
|
-
|
|
14
|
-
module_function
|
|
15
|
-
|
|
16
|
-
def can_embed?
|
|
17
|
-
return false unless defined?(Legion::LLM) && Legion::LLM.started?
|
|
18
|
-
|
|
19
|
-
ollama_embedding_available? || cloud_embedding_configured?
|
|
20
|
-
rescue StandardError => e
|
|
21
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.can_embed')
|
|
22
|
-
false
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def can_write?
|
|
26
|
-
return false unless apollo_write_enabled?
|
|
27
|
-
return false unless defined?(Legion::Data) && Legion::Data.connected?
|
|
28
|
-
|
|
29
|
-
check_db_write_privilege
|
|
30
|
-
rescue StandardError => e
|
|
31
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.can_write')
|
|
32
|
-
false
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def apollo_write_enabled?
|
|
36
|
-
settings[:data][:apollo_write] == true
|
|
37
|
-
rescue StandardError => e
|
|
38
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.apollo_write_enabled')
|
|
39
|
-
false
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def ollama_embedding_available?
|
|
43
|
-
return false unless defined?(Legion::LLM::Discovery::Ollama)
|
|
44
|
-
|
|
45
|
-
EMBEDDING_MODELS.any? { |m| Legion::LLM::Discovery::Ollama.model_available?(m) }
|
|
46
|
-
rescue StandardError => e
|
|
47
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.ollama_embedding_available')
|
|
48
|
-
false
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def cloud_embedding_configured?
|
|
52
|
-
provider = settings[:embedding][:provider]
|
|
53
|
-
model = settings[:embedding][:model]
|
|
54
|
-
!provider.nil? && !model.nil?
|
|
55
|
-
rescue StandardError => e
|
|
56
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.cloud_embedding_configured')
|
|
57
|
-
false
|
|
58
|
-
end
|
|
12
|
+
include Legion::Settings::Helper
|
|
59
13
|
|
|
60
|
-
|
|
61
|
-
PRIVILEGE_MUTEX.synchronize do
|
|
62
|
-
return @apollo_write_privilege unless @apollo_write_privilege.nil?
|
|
14
|
+
private
|
|
63
15
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
.first[:can_insert] == true
|
|
67
|
-
end
|
|
68
|
-
rescue StandardError => e
|
|
69
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.check_db_write_privilege')
|
|
70
|
-
@apollo_write_privilege = false
|
|
16
|
+
def capability_can_embed?
|
|
17
|
+
Legion::LLM.started? && Legion::LLM.can_embed?
|
|
71
18
|
end
|
|
72
19
|
|
|
73
|
-
def
|
|
74
|
-
|
|
20
|
+
def capability_can_write?
|
|
21
|
+
settings[:data][:apollo_write] == true && Legion::Data.can_write?(:apollo_entries)
|
|
75
22
|
end
|
|
76
23
|
end
|
|
77
24
|
end
|