Module: Legion::API::Routes::Coldstart

Defined in:
lib/legion/api/coldstart.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
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
# File 'lib/legion/api/coldstart.rb', line 7

def self.registered(app)
  app.post '/api/coldstart/ingest' do
    Legion::Logging.debug "API: POST /api/coldstart/ingest params=#{params.keys}"
    body = parse_request_body
    path = body[:path]
    if path.nil? || path.empty?
      Legion::Logging.warn 'API POST /api/coldstart/ingest returned 422: path is required'
      halt 422, json_error('missing_field', 'path is required', status_code: 422)
    end

    unless defined?(Legion::Extensions::Coldstart)
      Legion::Logging.warn 'API POST /api/coldstart/ingest returned 503: lex-coldstart is not loaded'
      halt 503, json_error('coldstart_unavailable', 'lex-coldstart is not loaded', status_code: 503)
    end

    unless defined?(Legion::Extensions::Agentic::Memory::Trace)
      Legion::Logging.warn 'API POST /api/coldstart/ingest returned 503: lex-agentic-memory is not loaded'
      halt 503, json_error('memory_unavailable', 'lex-agentic-memory is not loaded', status_code: 503)
    end

    runner = Object.new.extend(Legion::Extensions::Coldstart::Runners::Ingest)
    runner.define_singleton_method(:log) { Legion::Logging } unless runner.respond_to?(:log)

    result = if File.file?(path)
               runner.ingest_file(file_path: File.expand_path(path))
             elsif File.directory?(path)
               runner.ingest_directory(
                 dir_path: File.expand_path(path),
                 pattern:  body[:pattern] || '**/{CLAUDE,MEMORY}.md'
               )
             else
               Legion::Logging.warn "API POST /api/coldstart/ingest returned 404: path not found: #{path}"
               halt 404, json_error('path_not_found', "path not found: #{path}", status_code: 404)
             end

    Legion::Logging.info "API: coldstart ingest completed for path=#{path}"
    json_response(result, status_code: 201)
  rescue StandardError => e
    Legion::Logging.error "API POST /api/coldstart/ingest: #{e.class}#{e.message}"
    json_error('execution_error', e.message, status_code: 500)
  end
end