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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/legion/llm/api/namespaces/openai/uploads/parts.rb', line 20
def self.registered(app)
log.debug('[llm][api][namespaces][openai][parts] registering routes')
app.post '/v1/uploads/:id/parts' do require_llm!
upload_id = params[:id]
entry = Uploads::UPLOAD_MUTEX.synchronize { Uploads::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 #{entry[:status]}, not pending.",
type: 'invalid_request_error', code: 'upload_not_pending' } })
end
raw_data = Parts.(request, params)
if raw_data.bytesize > MAX_PART_SIZE_BYTES
halt 413, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Part size #{raw_data.bytesize} bytes exceeds maximum #{MAX_PART_SIZE_BYTES} bytes.",
type: 'invalid_request_error', code: 'part_too_large' } })
end
existing_total = Uploads::UPLOAD_MUTEX.synchronize do
Uploads::UPLOAD_STORE[upload_id][:parts].sum { |p| p[:data].bytesize }
end
if existing_total + raw_data.bytesize > MAX_TOTAL_UPLOAD_BYTES
halt 413, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Total upload size would exceed #{MAX_TOTAL_UPLOAD_BYTES / 1024 / 1024}MB limit.",
type: 'invalid_request_error', code: 'upload_too_large' } })
end
part_id = "part_#{SecureRandom.hex(16)}"
part = { id: part_id, data: raw_data, created_at: Time.now }
Uploads::UPLOAD_MUTEX.synchronize { Uploads::UPLOAD_STORE[upload_id][:parts] << part }
log.debug("[llm][api][openai][parts] action=create upload_id=#{upload_id} part_id=#{part_id} bytes=#{raw_data.bytesize}")
content_type :json
status 200
Legion::JSON.dump({
id: part_id,
object: 'upload.part',
upload_id: upload_id,
created_at: part[:created_at].to_i
})
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.parts.create')
openai_error(e.message, type: 'server_error', code: 'internal_error', status_code: 500)
end
log.debug('[llm][api][namespaces][openai][parts] routes registered')
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.parts.register')
end
|