Module: Legion::LLM::API::Namespaces::OpenAI::Uploads
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/namespaces/openai/uploads.rb,
lib/legion/llm/api/namespaces/openai/uploads/parts.rb
Defined Under Namespace
Modules: Parts
Constant Summary collapse
- UPLOAD_STORE =
rubocop:disable Style/MutableConstant
{}
- UPLOAD_MUTEX =
Mutex.new
- UPLOAD_TTL =
1 hour
3600
Class Method Summary collapse
-
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- .serialize_file(entry) ⇒ Object
- .serialize_upload(entry) ⇒ Object
Class Method Details
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
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 68 69 70 71 72 73 74 75 76 77 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/legion/llm/api/namespaces/openai/uploads.rb', line 21 def self.registered(app) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength log.debug('[llm][api][namespaces][openai][uploads] registering routes') # POST /v1/uploads app.post '/v1/uploads' do require_llm! body = parse_request_body validate_required!(body, :filename, :purpose, :bytes) upload_id = "upload_#{SecureRandom.hex(16)}" now = Time.now entry = { id: upload_id, object: 'upload', filename: body[:filename].to_s, purpose: body[:purpose].to_s, bytes: body[:bytes].to_i, created_at: now, expires_at: now + UPLOAD_TTL, status: 'pending', parts: [], file_id: nil } UPLOAD_MUTEX.synchronize { UPLOAD_STORE[upload_id] = entry } log.debug("[llm][api][openai][uploads] action=create upload_id=#{upload_id} filename=#{entry[:filename]}") content_type :json status 200 Legion::JSON.dump(Uploads.serialize_upload(entry)) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.uploads.create') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end # POST /v1/uploads/:id/cancel app.post '/v1/uploads/:id/cancel' do require_llm! upload_id = params[:id] entry = UPLOAD_MUTEX.synchronize { UPLOAD_STORE[upload_id] } unless entry halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Upload '#{upload_id}' not found.", type: 'invalid_request_error', code: 'upload_not_found' } }) end UPLOAD_MUTEX.synchronize { UPLOAD_STORE[upload_id][:status] = 'cancelled' } log.debug("[llm][api][openai][uploads] action=cancel upload_id=#{upload_id}") content_type :json status 200 Legion::JSON.dump(Uploads.serialize_upload(UPLOAD_MUTEX.synchronize { UPLOAD_STORE[upload_id] })) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.uploads.cancel') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end # POST /v1/uploads/:id/complete app.post '/v1/uploads/:id/complete' do # rubocop:disable Metrics/BlockLength require_llm! upload_id = params[:id] body = parse_request_body part_ids = body[:part_ids] || [] entry = UPLOAD_MUTEX.synchronize { UPLOAD_STORE[upload_id] } unless entry halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Upload '#{upload_id}' not found.", type: 'invalid_request_error', code: 'upload_not_found' } }) end unless entry[:status] == 'pending' halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Upload '#{upload_id}' is already #{entry[:status]}.", type: 'invalid_request_error', code: 'upload_already_completed' } }) end ordered_parts = if part_ids.any? part_ids.filter_map { |pid| entry[:parts].find { |p| p[:id] == pid } } else entry[:parts] end assembled = ordered_parts.map { |p| p[:data] }.join file_id = "file-#{SecureRandom.hex(16)}" ::FileUtils.mkdir_p(Files.files_dir) ::File.binwrite(::File.join(Files.files_dir, file_id), assembled) file_entry = { id: file_id, filename: entry[:filename], purpose: entry[:purpose], bytes: assembled.bytesize, created_at: Time.now, status: 'uploaded' } Files::FILE_MUTEX.synchronize { Files::FILE_STORE[file_id] = file_entry } UPLOAD_MUTEX.synchronize do UPLOAD_STORE[upload_id][:status] = 'completed' UPLOAD_STORE[upload_id][:file_id] = file_id end completed_entry = UPLOAD_MUTEX.synchronize { UPLOAD_STORE[upload_id] } log.debug("[llm][api][openai][uploads] action=complete upload_id=#{upload_id} file_id=#{file_id} bytes=#{assembled.bytesize}") content_type :json status 200 Legion::JSON.dump(Uploads.serialize_upload(completed_entry).merge(file: Uploads.serialize_file(file_entry))) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.uploads.complete') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end log.debug('[llm][api][namespaces][openai][uploads] routes registered') rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.uploads.register') end |
.serialize_file(entry) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/legion/llm/api/namespaces/openai/uploads.rb', line 159 def self.serialize_file(entry) { id: entry[:id], object: 'file', bytes: entry[:bytes], created_at: entry[:created_at]&.to_i, filename: entry[:filename], purpose: entry[:purpose], status: entry[:status] } end |
.serialize_upload(entry) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/legion/llm/api/namespaces/openai/uploads.rb', line 145 def self.serialize_upload(entry) { id: entry[:id], object: 'upload', filename: entry[:filename], purpose: entry[:purpose], bytes: entry[:bytes], created_at: entry[:created_at]&.to_i, expires_at: entry[:expires_at]&.to_i, status: entry[:status], file_id: entry[:file_id] }.compact end |