Files
moloapp/src/vendors/perplexity/batches.cpp
T
retoorandClaude Sonnet 5.5 3404392887 feat(clients): perplexity: Sonar, Search, Agent, embeddings, decisions, batches, skills, orchestrator
Package molo::perplexity covering all 39 operations of the five published
OpenAPI documents, PerplexityChatModel, demo routes under /ai/perplexity,
check_spec.py, SPEC.md, 46 unit cases and 111 route checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 15:01:55 +01:00

210 lines
7.8 KiB
C++

#include "vendors/perplexity/batches.h"
#include "clients/body/form.h"
#include "vendors/perplexity/calls.h"
#include "vendors/perplexity/caps.h"
#include "vendors/perplexity/rules.h"
namespace molo::perplexity {
BatchFileInfo BatchFileInfo::parse(const Json& doc) {
BatchFileInfo f;
f.id = J::str(doc, "id");
f.filename = J::str(doc, "filename");
f.purpose = J::str(doc, "purpose");
f.bytes = J::num(doc, "bytes");
f.created_at = J::num(doc, "created_at");
f.expires_at = J::num(doc, "expires_at");
f.deleted = J::flag(doc, "deleted");
return f;
}
BatchInfo BatchInfo::parse(const Json& doc) {
BatchInfo b;
b.raw = doc;
b.id = J::str(doc, "id");
b.endpoint = J::str(doc, "endpoint");
b.input_file_id = J::str(doc, "input_file_id");
b.output_file_id = J::str(doc, "output_file_id");
b.error_file_id = J::str(doc, "error_file_id");
b.completion_window = J::str(doc, "completion_window");
b.status = J::str(doc, "status");
b.created_at = J::num(doc, "created_at");
b.in_progress_at = J::num(doc, "in_progress_at");
b.expires_at = J::num(doc, "expires_at");
b.finalizing_at = J::num(doc, "finalizing_at");
b.completed_at = J::num(doc, "completed_at");
b.failed_at = J::num(doc, "failed_at");
b.expired_at = J::num(doc, "expired_at");
b.cancelling_at = J::num(doc, "cancelling_at");
b.cancelled_at = J::num(doc, "cancelled_at");
if (const Json* c = J::obj(doc, "request_counts")) {
b.total = J::num(*c, "total");
b.completed = J::num(*c, "completed");
b.failed = J::num(*c, "failed");
}
if (const Json* u = J::obj(doc, "usage")) {
b.input_tokens = J::num(*u, "input_tokens");
b.output_tokens = J::num(*u, "output_tokens");
b.total_tokens = J::num(*u, "total_tokens");
}
if (const Json* e = J::obj(doc, "errors"))
if (const Json* d = J::arr(*e, "data"))
for (const Json& x : d->as_array()) {
if (b.errors.size() >= PerplexityCaps::kMaxItemsReturned) break;
b.errors.push_back({J::str(x, "code"), J::str(x, "message"), J::num(x, "line")});
}
if (const Json* m = J::obj(doc, "metadata"))
for (const auto& [k, v] : m->as_object())
if (v.is_string()) b.metadata[k] = v.as_string();
return b;
}
bool BatchInfo::finished() const {
return status == "completed" || status == "expired" || status == "cancelled" || status == "failed";
}
void BatchInput::validate() const {
Rules::filename(filename);
Rules::require(!jsonl.empty(), "the batch input is empty");
Rules::require(jsonl.size() <= PerplexityCaps::kBatchFileBytes, "batch input over 200 MB");
std::size_t lines = 0, pos = 0;
while (pos < jsonl.size()) {
std::size_t end = jsonl.find('\n', pos);
if (end == std::string::npos) end = jsonl.size();
std::string line = jsonl.substr(pos, end - pos);
pos = end + 1;
if (line.find_first_not_of(" \t\r") == std::string::npos) continue;
++lines;
Rules::require(lines <= PerplexityCaps::kBatchFileLines, "batch input over 50,000 lines");
try {
Rules::require(Json::parse(line).is_object(), "line " + std::to_string(lines) + " is not a JSON object");
} catch (const clients::body::JsonError&) {
Rules::fail("line " + std::to_string(lines) + " is not valid JSON");
}
}
Rules::require(lines > 0, "the batch input has no request lines");
}
namespace {
std::string page_query(int limit, const std::string& after) {
std::string q;
if (limit) {
Rules::require(limit >= 1 && limit <= PerplexityCaps::kPageLimitMax, "limit must be 1..100");
Rules::add_query(q, "limit", std::to_string(limit));
}
if (!after.empty()) {
Rules::require(llm::Service::valid_id(after), "bad after cursor");
Rules::add_query(q, "after", after);
}
return q;
}
} // namespace
BatchFileInfo BatchFiles::upload(const BatchInput& input) {
input.validate();
clients::body::MultipartBody form;
form.add_text("purpose", "batch");
form.add_file("file", input.filename, "application/jsonl", input.jsonl);
return BatchFileInfo::parse(client_->post_multipart(Calls::render(Call::kFileCreate), form));
}
BatchFileList BatchFiles::list(int limit, const std::string& after, const std::string& order,
const std::string& purpose) {
std::string q = page_query(limit, after);
if (!order.empty()) {
Rules::one_of(order, {"asc", "desc"}, "order");
Rules::add_query(q, "order", order);
}
if (!purpose.empty()) {
Rules::one_of(purpose, {"batch", "batch_output"}, "purpose");
Rules::add_query(q, "purpose", purpose);
}
Json doc = client_->get(Calls::render(Call::kFileList) + q);
BatchFileList l;
l.first_id = J::str(doc, "first_id");
l.last_id = J::str(doc, "last_id");
l.has_more = J::flag(doc, "has_more");
if (const Json* d = J::arr(doc, "data"))
for (const Json& f : d->as_array()) {
if (l.files.size() >= PerplexityCaps::kMaxItemsReturned) {
l.truncated = true;
break;
}
l.files.push_back(BatchFileInfo::parse(f));
}
return l;
}
BatchFileInfo BatchFiles::get(const std::string& file_id) {
check_id("file", file_id);
return BatchFileInfo::parse(client_->get(Calls::render(Call::kFileGet, file_id)));
}
BatchFileInfo BatchFiles::remove(const std::string& file_id) {
check_id("file", file_id);
return BatchFileInfo::parse(client_->del(Calls::render(Call::kFileDelete, file_id)));
}
std::string BatchFiles::content(const std::string& file_id) {
check_id("file", file_id);
return client_->request_raw(Calls::method_of(Call::kFileContent), Calls::render(Call::kFileContent, file_id)).body;
}
BatchInfo Batches::create(const std::string& input_file_id, const std::map<std::string, std::string>& metadata,
const std::string& idempotency_key) {
check_id("input file", input_file_id);
Json body = Json::object();
body.set("input_file_id", Json(input_file_id));
body.set("endpoint", Json("/v1/responses"));
body.set("completion_window", Json("24h"));
if (!metadata.empty()) {
Rules::require(metadata.size() <= PerplexityCaps::kBatchMetadataPairs, "at most 16 metadata pairs");
Json m = Json::object();
for (const auto& [k, v] : metadata) {
Rules::require(!k.empty() && k.size() <= PerplexityCaps::kBatchMetadataKey, "metadata keys are 1..64 characters");
Rules::require(v.size() <= PerplexityCaps::kBatchMetadataValue, "metadata values are at most 512 characters");
m.set(k, Json(v));
}
body.set("metadata", m);
}
llm::Headers extra;
if (!idempotency_key.empty()) {
Rules::require(idempotency_key.size() <= PerplexityCaps::kIdempotencyKey, "Idempotency-Key over 200 characters");
for (char c : idempotency_key)
Rules::require(static_cast<unsigned char>(c) > 0x20 && c != 0x7f, "Idempotency-Key has invalid characters");
extra["Idempotency-Key"] = idempotency_key;
}
return BatchInfo::parse(client_->post(Calls::render(Call::kBatchCreate), body, extra));
}
BatchList Batches::list(int limit, const std::string& after) {
Json doc = client_->get(Calls::render(Call::kBatchList) + page_query(limit, after));
BatchList l;
l.first_id = J::str(doc, "first_id");
l.last_id = J::str(doc, "last_id");
l.has_more = J::flag(doc, "has_more");
if (const Json* d = J::arr(doc, "data"))
for (const Json& b : d->as_array()) {
if (l.batches.size() >= PerplexityCaps::kMaxItemsReturned) {
l.truncated = true;
break;
}
l.batches.push_back(BatchInfo::parse(b));
}
return l;
}
BatchInfo Batches::get(const std::string& batch_id) {
check_id("batch", batch_id);
return BatchInfo::parse(client_->get(Calls::render(Call::kBatchGet, batch_id)));
}
BatchInfo Batches::cancel(const std::string& batch_id) {
check_id("batch", batch_id);
Json empty = Json::object();
return BatchInfo::parse(client_->post(Calls::render(Call::kBatchCancel, batch_id), empty));
}
} // namespace molo::perplexity