144 lines
5.7 KiB
C++
144 lines
5.7 KiB
C++
#include "deepseek/files.h"
|
|||
|
|
|
||
|
|
#include "clients/body/form.h"
|
||
|
|
#include "deepseek/calls.h"
|
||
|
|
#include "deepseek/caps.h"
|
||
|
|
#include "deepseek/rules.h"
|
||
|
|
|
||
|
|
namespace molo::deepseek {
|
||
|
|
|
||
|
|
std::string UploadParams::sniff(const std::string& d) {
|
||
|
|
auto starts = [&](const char* magic, std::size_t n) { return d.size() >= n && d.compare(0, n, magic, n) == 0; };
|
||
|
|
if (starts("\xFF\xD8\xFF", 3)) return "jpeg";
|
||
|
|
if (starts("\x89PNG\r\n\x1a\n", 8)) return "png";
|
||
|
|
if (starts("GIF87a", 6) || starts("GIF89a", 6)) return "gif";
|
||
|
|
if (d.size() >= 12 && d.compare(0, 4, "RIFF") == 0 && d.compare(8, 4, "WEBP") == 0) return "webp";
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
void UploadParams::validate() const {
|
||
|
|
Rules::require(!filename.empty() && filename.size() <= DeepseekCaps::kMaxFilenameBytes,
|
||
|
|
"filename must be 1..512 bytes");
|
||
|
|
for (char c : filename)
|
||
|
|
Rules::require(c != '"' && c != '\r' && c != '\n' && c != '\\' && c != '/',
|
||
|
|
"filename has invalid characters");
|
||
|
|
Rules::require(!data.empty(), "file is empty");
|
||
|
|
Rules::require(data.size() <= DeepseekCaps::kMaxUploadBytes, "file over 64 MiB");
|
||
|
|
Rules::require(!sniff(data).empty(), "only JPEG, PNG, GIF and WebP images can be uploaded");
|
||
|
|
if (expires_after_seconds)
|
||
|
|
Rules::require(*expires_after_seconds >= DeepseekCaps::kExpireMinSeconds &&
|
||
|
|
*expires_after_seconds <= DeepseekCaps::kExpireMaxSeconds,
|
||
|
|
"expires_after seconds must be 3600..2592000");
|
||
|
|
}
|
||
|
|
|
||
|
|
void FileListParams::validate() const {
|
||
|
|
Rules::require(limit >= 1 && limit <= DeepseekCaps::kFilesMaxLimit, "limit must be 1..1000");
|
||
|
|
Rules::require(order.empty() || order == "asc" || order == "desc", "order must be asc or desc");
|
||
|
|
Rules::require(after.empty() || llm::Service::valid_id(after), "bad after cursor");
|
||
|
|
}
|
||
|
|
|
||
|
|
void AnthropicFileListParams::validate() const {
|
||
|
|
Rules::require(limit >= 1 && limit <= DeepseekCaps::kFilesMaxLimit, "limit must be 1..1000");
|
||
|
|
Rules::require(after_id.empty() || before_id.empty(), "after_id and before_id are mutually exclusive");
|
||
|
|
Rules::require(after_id.empty() || llm::Service::valid_id(after_id), "bad after_id cursor");
|
||
|
|
Rules::require(before_id.empty() || llm::Service::valid_id(before_id), "bad before_id cursor");
|
||
|
|
}
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::string mime_of(const std::string& kind) { return "image/" + kind; }
|
||
|
|
|
||
|
|
clients::body::MultipartBody upload_form(const UploadParams& p, bool with_purpose) {
|
||
|
|
clients::body::MultipartBody form;
|
||
|
|
if (with_purpose) form.add_text("purpose", Files::kPurpose);
|
||
|
|
form.add_file("file", p.filename, mime_of(UploadParams::sniff(p.data)), p.data);
|
||
|
|
if (with_purpose && p.expires_after_seconds) {
|
||
|
|
form.add_text("expires_after[anchor]", "created_at");
|
||
|
|
form.add_text("expires_after[seconds]", std::to_string(*p.expires_after_seconds));
|
||
|
|
}
|
||
|
|
return form;
|
||
|
|
}
|
||
|
|
|
||
|
|
FileDeleted parse_deleted(const Json& doc, bool anthropic) {
|
||
|
|
FileDeleted d;
|
||
|
|
d.id = J::str(doc, "id");
|
||
|
|
d.deleted = anthropic ? J::str(doc, "type") == "file_deleted" : J::flag(doc, "deleted");
|
||
|
|
return d;
|
||
|
|
}
|
||
|
|
|
||
|
|
llm::Headers beta_header() { return {{"anthropic-beta", Calls::anthropic_files_beta()}}; }
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
FileInfo Files::upload(const UploadParams& params) {
|
||
|
|
params.validate();
|
||
|
|
return FileInfo::parse(client_->post_multipart(Calls::render(Call::kFileCreate), upload_form(params, true)));
|
||
|
|
}
|
||
|
|
|
||
|
|
FileList Files::list(const FileListParams& params) {
|
||
|
|
params.validate();
|
||
|
|
std::string path = Calls::render(Call::kFileList) + "?limit=" + std::to_string(params.limit) +
|
||
|
|
"&purpose=" + kPurpose;
|
||
|
|
if (!params.order.empty()) path += "&order=" + params.order;
|
||
|
|
if (!params.after.empty()) path += "&after=" + params.after;
|
||
|
|
return FileList::parse(client_->get(path), false);
|
||
|
|
}
|
||
|
|
|
||
|
|
FileList Files::list_all(int page_limit) {
|
||
|
|
FileListParams p;
|
||
|
|
p.limit = page_limit;
|
||
|
|
FileList all;
|
||
|
|
for (std::size_t page = 0; page < DeepseekCaps::kMaxFilePages; ++page) {
|
||
|
|
FileList one = list(p);
|
||
|
|
for (FileInfo& f : one.files) {
|
||
|
|
if (all.files.size() >= DeepseekCaps::kMaxFilesReturned) {
|
||
|
|
all.truncated = true;
|
||
|
|
return all;
|
||
|
|
}
|
||
|
|
all.files.push_back(std::move(f));
|
||
|
|
}
|
||
|
|
if (all.first_id.empty()) all.first_id = one.first_id;
|
||
|
|
all.last_id = one.last_id;
|
||
|
|
all.has_more = one.has_more;
|
||
|
|
if (!one.has_more || one.last_id.empty()) return all;
|
||
|
|
p.after = one.last_id;
|
||
|
|
}
|
||
|
|
all.truncated = all.has_more;
|
||
|
|
return all;
|
||
|
|
}
|
||
|
|
|
||
|
|
FileInfo Files::get(const std::string& file_id) {
|
||
|
|
check_id("file", file_id);
|
||
|
|
return FileInfo::parse(client_->get(Calls::render(Call::kFileGet, file_id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
FileDeleted Files::remove(const std::string& file_id) {
|
||
|
|
check_id("file", file_id);
|
||
|
|
return parse_deleted(client_->del(Calls::render(Call::kFileDelete, file_id)), false);
|
||
|
|
}
|
||
|
|
|
||
|
|
FileInfo AnthropicFiles::upload(const UploadParams& params) {
|
||
|
|
params.validate();
|
||
|
|
return FileInfo::parse_anthropic(client_->post_multipart(
|
||
|
|
Calls::render(Call::kAnthropicFileCreate), upload_form(params, false), beta_header()));
|
||
|
|
}
|
||
|
|
|
||
|
|
FileList AnthropicFiles::list(const AnthropicFileListParams& params) {
|
||
|
|
params.validate();
|
||
|
|
std::string path = Calls::render(Call::kAnthropicFileList) + "?limit=" + std::to_string(params.limit);
|
||
|
|
if (!params.after_id.empty()) path += "&after_id=" + params.after_id;
|
||
|
|
if (!params.before_id.empty()) path += "&before_id=" + params.before_id;
|
||
|
|
return FileList::parse(client_->get(path, beta_header()), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
FileInfo AnthropicFiles::get(const std::string& file_id) {
|
||
|
|
check_id("file", file_id);
|
||
|
|
return FileInfo::parse_anthropic(
|
||
|
|
client_->get(Calls::render(Call::kAnthropicFileGet, file_id), beta_header()));
|
||
|
|
}
|
||
|
|
|
||
|
|
FileDeleted AnthropicFiles::remove(const std::string& file_id) {
|
||
|
|
check_id("file", file_id);
|
||
|
|
return parse_deleted(client_->del(Calls::render(Call::kAnthropicFileDelete, file_id), beta_header()), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::deepseek
|