Module: Legion::Apollo::Routes

Defined in:
lib/legion/apollo/routes.rb

Overview

Sinatra route module for Apollo API endpoints. Self-registers at boot.

Defined Under Namespace

Modules: ApolloHelpers

Class Method Summary collapse

Class Method Details

.register_expertise_route(app) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/legion/apollo/routes.rb', line 154

def self.register_expertise_route(app)
  app.get '/api/apollo/expertise' do
    halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503) unless apollo_loaded?

    json_response(apollo_expertise_map)
  end
end

.register_graph_route(app) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/legion/apollo/routes.rb', line 146

def self.register_graph_route(app)
  app.get '/api/apollo/graph' do
    halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503) unless apollo_loaded?

    json_response(apollo_graph_topology)
  end
end

.register_ingest_route(app) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/apollo/routes.rb', line 78

def self.register_ingest_route(app) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  app.post '/api/apollo/ingest' do # rubocop:disable Metrics/BlockLength
    unless apollo_api_available?
      halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503)
    end

    body = parse_request_body
    max_tags = defined?(Legion::Settings) ? (Legion::Settings[:apollo]&.dig(:max_tags) || 20) : 20
    # TagNormalizer hard-caps to MAX_TAGS=20 internally; clamp here to make that limit explicit.
    effective_max_tags = [max_tags, Legion::Apollo::Helpers::TagNormalizer::MAX_TAGS].min
    tags = Legion::Apollo::Helpers::TagNormalizer.normalize(Array(body[:tags])).first(effective_max_tags)
    result = Legion::Apollo.ingest(
      content:             body[:content],
      content_type:        body[:content_type] || :observation,
      tags:                tags,
      source_agent:        body[:source_agent] || 'api',
      source_provider:     body[:source_provider],
      source_channel:      body[:source_channel] || 'rest_api',
      knowledge_domain:    body[:knowledge_domain],
      context:             body[:context] || {},
      scope:               normalize_scope(body[:scope]),
      is_inference:        body[:is_inference] == true,
      forget_reason:       body[:forget_reason],
      expires_at:          body[:expires_at],
      parent_knowledge_id: body[:parent_knowledge_id],
      supersession_type:   body[:supersession_type],
      source_uri:          body[:source_uri],
      source_hash:         body[:source_hash],
      relevance_score:     body[:relevance_score],
      extraction_method:   body[:extraction_method]
    )
    json_response(result, status_code: apollo_status_code(result, success_status: 201))
  end
end

.register_maintenance_route(app) ⇒ Object

rubocop:disable Metrics/MethodLength



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/legion/apollo/routes.rb', line 126

def self.register_maintenance_route(app) # rubocop:disable Metrics/MethodLength
  app.post '/api/apollo/maintenance' do
    halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503) unless apollo_loaded?

    body = parse_request_body
    action_str = body[:action]
    unless %w[
      decay_cycle corroboration
    ].include?(action_str)
      halt 400,
           json_error('invalid_action', 'action must be decay_cycle or corroboration', status_code: 400)
    end

    action = action_str.to_sym

    result = run_maintenance(action)
    json_response(result)
  end
end

.register_query_route(app) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/apollo/routes.rb', line 53

def self.register_query_route(app) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  app.post '/api/apollo/query' do
    unless apollo_api_available?
      halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503)
    end

    body = parse_request_body
    default_limit = defined?(Legion::Settings) ? (Legion::Settings[:apollo]&.dig(:default_limit) || 5) : 5
    result = Legion::Apollo.query(
      text:               body[:query],
      limit:              body[:limit] || default_limit,
      min_confidence:     body[:min_confidence],
      status:             body[:status] || [:confirmed],
      tags:               body[:tags],
      domain:             body[:domain],
      agent_id:           body[:agent_id] || 'api',
      scope:              normalize_scope(body[:scope]),
      tier:               body[:tier]&.to_sym,
      include_inferences: body.fetch(:include_inferences, true),
      include_history:    body.fetch(:include_history, false)
    )
    json_response(result, status_code: apollo_status_code(result))
  end
end


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/legion/apollo/routes.rb', line 113

def self.register_related_route(app)
  app.get '/api/apollo/entries/:id/related' do
    halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503) unless apollo_loaded?

    result = apollo_runner.related_entries(
      entry_id:       params[:id].to_i,
      relation_types: params[:relation_types]&.split(','),
      depth:          (params[:depth] || 2).to_i
    )
    json_response(result)
  end
end

.register_stats_route(app) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/apollo/routes.rb', line 40

def self.register_stats_route(app)
  app.get '/api/apollo/stats' do
    halt 503, json_error('apollo_unavailable', 'apollo is not available', status_code: 503) unless apollo_loaded?

    stats = apollo_stats
    if stats[:error]
      halt 503, json_error('apollo_stats_unavailable', stats[:error], status_code: 503)
    else
      json_response(stats)
    end
  end
end

.register_status_route(app) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/apollo/routes.rb', line 29

def self.register_status_route(app)
  app.get '/api/apollo/status' do
    available      = apollo_runner_available?
    data_connected = apollo_data_connected?
    status_code    = available && data_connected ? 200 : 503

    json_response({ available: available, data_connected: data_connected },
                  status_code: status_code)
  end
end

.registered(app) ⇒ Object

rubocop:disable Metrics/ModuleLength



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/legion/apollo/routes.rb', line 17

def self.registered(app)
  app.helpers ApolloHelpers
  register_status_route(app)
  register_stats_route(app)
  register_query_route(app)
  register_ingest_route(app)
  register_related_route(app)
  register_maintenance_route(app)
  register_graph_route(app)
  register_expertise_route(app)
end