Module: Legion::Gaia::Routes

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

Class Method Summary collapse

Class Method Details

.register_bond_terminate_route(app) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/legion/gaia/routes.rb', line 198

def self.register_bond_terminate_route(app)
  app.delete '/api/gaia/bond/:identity' do
    identity = params[:identity]
    halt 400, json_error('missing_identity', 'identity is required') if identity.to_s.empty?

    result = Legion::Gaia::DeathProtocol.terminate_bond(identity: identity, confirm: true)
    json_response(result)
  end
end

.register_buffer_route(app) ⇒ Object



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

def self.register_buffer_route(app)
  app.get '/api/gaia/buffer' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started') unless gaia_available?

    buffer = Legion::Gaia.sensory_buffer
    json_response({
                    depth: buffer&.size || 0,
                    empty: buffer.nil? || buffer.empty?,
                    max_size: gaia_buffer_max_size
                  })
  end
end

.register_channels_route(app) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/gaia/routes.rb', line 96

def self.register_channels_route(app)
  app.get '/api/gaia/channels' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started') unless gaia_available?

    registry = Legion::Gaia.channel_registry
    return json_response({ channels: [] }) unless registry

    channels = registry.active_channels.map do |ch_id|
      adapter = registry.adapter_for(ch_id)
      build_channel_info(ch_id, adapter)
    end

    json_response({ channels: channels, count: channels.size })
  end
end

.register_ingest_route(app) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/legion/gaia/routes.rb', line 173

def self.register_ingest_route(app)
  app.post '/api/gaia/ingest' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started') unless gaia_available?

    body = Legion::JSON.load(request.body.read)
    content  = body[:content] || body['content']
    identity = body[:identity] || body['identity'] || 'unknown'
    channel  = (body[:channel_id] || body['channel_id'] || 'cli').to_sym

    halt 400, json_error('missing_content', 'content is required') if content.to_s.empty?

    frame = Legion::Gaia::InputFrame.new(
      content: content,
      channel_id: channel,
      content_type: :text,
      auth_context: { identity: identity },
      metadata: { source_type: :human_direct, salience: 0.8 }
    )

    Legion::Gaia.ingest(frame)
    Legion::Logging.info "API: gaia ingest frame_id=#{frame.id} identity=#{identity}"
    json_response({ status: 'accepted', frame_id: frame.id })
  end
end

.register_partner_report_route(app) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/legion/gaia/routes.rb', line 208

def self.register_partner_report_route(app)
  app.get '/api/gaia/partner/:identity/report' do
    identity = params[:identity]
    halt 400, json_error('missing_identity', 'identity is required') if identity.to_s.empty?

    result = Legion::Gaia::Disclosure.report(identity: identity)
    json_response(result)
  end
end

.register_sessions_route(app) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/legion/gaia/routes.rb', line 125

def self.register_sessions_route(app)
  app.get '/api/gaia/sessions' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started') unless gaia_available?

    store = Legion::Gaia.session_store
    json_response({
                    count: store&.size || 0,
                    active: gaia_available?
                  })
  end
end

.register_status_route(app) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/legion/gaia/routes.rb', line 69

def self.register_status_route(app)
  app.get '/api/gaia/status' do
    if gaia_available?
      json_response(Legion::Gaia.status)
    else
      json_response({ started: false }, status_code: 503)
    end
  end
end

.register_teams_webhook_route(app) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/legion/gaia/routes.rb', line 137

def self.register_teams_webhook_route(app)
  app.post '/api/channels/teams/webhook' do
    Legion::Logging.debug "API: POST /api/channels/teams/webhook params=#{params.keys}"
    body = request.body.read

    adapter = Routes.teams_adapter
    unless adapter
      Legion::Logging.warn 'API POST /api/channels/teams/webhook returned 503: teams adapter not available'
      halt 503, json_response({ error: 'teams adapter not available' }, status_code: 503)
    end

    result = Legion::Gaia::Channels::Teams::WebhookHandler.new(adapter).handle(
      request_body: body,
      auth_header: request.env['HTTP_AUTHORIZATION']
    )
    unless result[:status].to_i == 200
      Legion::Logging.warn "API POST /api/channels/teams/webhook returned #{result[:status]}: #{result[:type]}"
      halt result[:status], json_response({ error: result[:type], detail: result[:detail] },
                                          status_code: result[:status])
    end

    Legion::Logging.info "API: accepted Teams webhook frame_id=#{result[:frame_id]}"
    json_response({ status: 'accepted', frame_id: result[:frame_id] })
  end
end

.register_ticks_route(app) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/gaia/routes.rb', line 79

def self.register_ticks_route(app)
  app.get '/api/gaia/ticks' do
    halt 503, json_error('gaia_unavailable', 'gaia is not started') unless gaia_available?

    max_limit =
      if defined?(Legion::Gaia::TickHistory) && Legion::Gaia::TickHistory.const_defined?(:MAX_ENTRIES)
        Legion::Gaia::TickHistory::MAX_ENTRIES
      else
        200
      end

    limit = (params[:limit] || 50).to_i.clamp(1, max_limit)
    events = Legion::Gaia.tick_history&.recent(limit: limit) || []
    json_response({ events: events })
  end
end

.registered(app) ⇒ Object

rubocop:disable Metrics/AbcSize



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/gaia/routes.rb', line 13

def self.registered(app) # rubocop:disable Metrics/AbcSize
  app.helpers do # rubocop:disable Metrics/BlockLength
    unless method_defined?(:gaia_available?)
      define_method(:gaia_available?) do
        defined?(Legion::Gaia) && Legion::Gaia.respond_to?(:started?) && Legion::Gaia.started?
      end
    end

    unless method_defined?(:gaia_buffer_max_size)
      define_method(:gaia_buffer_max_size) do
        return nil unless defined?(Legion::Gaia::SensoryBuffer)

        Legion::Gaia::SensoryBuffer::MAX_BUFFER_SIZE
      rescue NameError => e
        Legion::Logging.debug "[gaia] route helpers failed #{e.class}: #{e.message}"
        nil
      end
    end

    unless method_defined?(:build_channel_info)
      define_method(:build_channel_info) do |channel_id, adapter|
        info = { id: channel_id, started: adapter&.started? || false }
        info[:capabilities] = adapter.capabilities if adapter.respond_to?(:capabilities)
        info[:type] = adapter.class.name.split('::').last if adapter
        info
      end
    end

    unless method_defined?(:json_response)
      define_method(:json_response) do |data, status_code: 200|
        content_type :json
        status status_code
        Legion::JSON.dump({ data: data })
      end
    end

    unless method_defined?(:json_error)
      define_method(:json_error) do |code, message, status_code: nil|
        content_type :json
        status status_code if status_code
        Legion::JSON.dump({ error: { code: code, message: message } })
      end
    end
  end

  register_status_route(app)
  register_ticks_route(app)
  register_channels_route(app)
  register_buffer_route(app)
  register_sessions_route(app)
  register_teams_webhook_route(app)
  register_ingest_route(app)
  register_bond_terminate_route(app)
  register_partner_report_route(app)
end

.teams_adapterObject



163
164
165
166
167
168
169
170
171
# File 'lib/legion/gaia/routes.rb', line 163

def self.teams_adapter
  return nil unless defined?(Legion::Gaia) && Legion::Gaia.respond_to?(:channel_registry)
  return nil unless Legion::Gaia.channel_registry

  Legion::Gaia.channel_registry.adapter_for(:teams)
rescue StandardError => e
  Legion::Logging.warn "Gaia#teams_adapter failed: #{e.message}"
  nil
end