Module: Legion::Gaia::Routes
- Defined in:
- lib/legion/gaia/routes.rb
Class Method Summary collapse
- .register_buffer_route(app) ⇒ Object
- .register_channels_route(app) ⇒ Object
- .register_ingest_route(app) ⇒ Object
- .register_sessions_route(app) ⇒ Object
- .register_status_route(app) ⇒ Object
- .register_teams_webhook_route(app) ⇒ Object
- .register_ticks_route(app) ⇒ Object
- .registered(app) ⇒ Object
- .teams_adapter ⇒ Object
Class Method Details
.register_buffer_route(app) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/legion/gaia/routes.rb', line 110 def self.register_buffer_route(app) app.get '/api/gaia/buffer' do halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/legion/gaia/routes.rb', line 94 def self.register_channels_route(app) app.get '/api/gaia/channels' do halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) 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
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/legion/gaia/routes.rb', line 177 def self.register_ingest_route(app) app.post '/api/gaia/ingest' do halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) 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', status_code: 400) 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}" if defined?(Legion::Logging) json_response({ status: 'accepted', frame_id: frame.id }) end end |
.register_sessions_route(app) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/legion/gaia/routes.rb', line 123 def self.register_sessions_route(app) app.get '/api/gaia/sessions' do halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) unless gaia_available? store = Legion::Gaia.session_store json_response({ count: store&.size || 0, active: gaia_available? }) end end |
.register_status_route(app) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/legion/gaia/routes.rb', line 67 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
135 136 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 162 163 164 165 |
# File 'lib/legion/gaia/routes.rb', line 135 def self.register_teams_webhook_route(app) app.post '/api/channels/teams/webhook' do if defined?(Legion::Logging) Legion::Logging.debug "API: POST /api/channels/teams/webhook params=#{params.keys}" end body = request.body.read activity = Legion::JSON.load(body) adapter = Routes.teams_adapter unless adapter if defined?(Legion::Logging) Legion::Logging.warn 'API POST /api/channels/teams/webhook returned 503: teams adapter not available' end halt 503, json_response({ error: 'teams adapter not available' }, status_code: 503) end input_frame = adapter.translate_inbound(activity) unless input_frame if defined?(Legion::Logging) Legion::Logging.warn 'API POST /api/channels/teams/webhook returned 422: ' \ 'unsupported or malformed Teams activity' end halt 422, json_error('invalid_teams_activity', 'unsupported or malformed Teams activity', status_code: 422) end Legion::Gaia.ingest(input_frame) if defined?(Legion::Gaia) Legion::Logging.info "API: accepted Teams webhook frame_id=#{input_frame.id}" if defined?(Legion::Logging) json_response({ status: 'accepted', frame_id: input_frame.id }) end end |
.register_ticks_route(app) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/legion/gaia/routes.rb', line 77 def self.register_ticks_route(app) app.get '/api/gaia/ticks' do halt 503, json_error('gaia_unavailable', 'gaia is not started', status_code: 503) 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
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 |
# File 'lib/legion/gaia/routes.rb', line 13 def self.registered(app) 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.}" if defined?(Legion::Logging) 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, , status_code: 400| content_type :json status status_code Legion::JSON.dump({ error: { code: code, 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) end |
.teams_adapter ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/legion/gaia/routes.rb', line 167 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.}" if defined?(Legion::Logging) nil end |