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>
This commit is contained in:
Vendored
+151
@@ -0,0 +1,151 @@
|
||||
#include "vendors/perplexity/account.h"
|
||||
|
||||
#include "vendors/perplexity/calls.h"
|
||||
#include "vendors/perplexity/caps.h"
|
||||
#include "vendors/perplexity/rules.h"
|
||||
|
||||
namespace molo::perplexity {
|
||||
|
||||
namespace {
|
||||
void add(std::string& q, const std::string& k, const std::string& v) { Rules::add_query(q, k, v); }
|
||||
std::string enc(const std::string& s) { return s; } // add() encodes
|
||||
|
||||
void check_common(const UsageQuery& q) {
|
||||
Rules::require(q.start_time >= 0, "start_time must be unix seconds");
|
||||
if (q.end_time) Rules::require(*q.end_time >= q.start_time, "end_time must not be before start_time");
|
||||
Rules::require(q.page.size() <= 1024, "page cursor over 1024 characters");
|
||||
for (char c : q.page) Rules::require(static_cast<unsigned char>(c) > 0x20 && c != 0x7f, "page cursor has invalid characters");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void UsageQuery::validate_v1() const {
|
||||
Rules::one_of(dataset,
|
||||
{"credit_usage", "connectors", "artifacts", "skills", "spaces", "workflows", "task_durations",
|
||||
"query_volume", "daily_active_users"},
|
||||
"dataset");
|
||||
check_common(*this);
|
||||
if (!bucket_width.empty()) Rules::one_of(bucket_width, {"1d", "1h"}, "bucket_width");
|
||||
bool hourly = bucket_width == "1h";
|
||||
bool daily_org = dataset == "query_volume" || dataset == "daily_active_users";
|
||||
Rules::require(!(daily_org && hourly), "query_volume and daily_active_users reject bucket_width=1h");
|
||||
Rules::require(!(daily_org && !user_email.empty()), "query_volume and daily_active_users reject user_email");
|
||||
if (limit) Rules::require(*limit >= 1 && *limit <= (hourly ? 168 : 31), "limit must be 1..31 (1d) or 1..168 (1h)");
|
||||
if (!user_email.empty())
|
||||
Rules::require(user_email.size() <= 254 && user_email.find('@') != std::string::npos &&
|
||||
user_email.find_first_of(" \t\r\n&?#") == std::string::npos,
|
||||
"user_email must be an email address");
|
||||
Rules::require(group_by.empty(), "group_by is a v2 parameter");
|
||||
}
|
||||
|
||||
void UsageQuery::validate_v2() const {
|
||||
Rules::one_of(dataset, {"credit_usage", "query_volume"}, "dataset");
|
||||
Rules::one_of(group_by, {"user_email"}, "group_by");
|
||||
check_common(*this);
|
||||
Rules::require(bucket_width.empty() && user_email.empty(), "bucket_width and user_email are v1 parameters");
|
||||
if (limit) Rules::require(*limit >= 1, "limit must be at least 1");
|
||||
}
|
||||
|
||||
std::string UsageQuery::query_v1() const {
|
||||
validate_v1();
|
||||
std::string q;
|
||||
add(q, "dataset", dataset);
|
||||
add(q, "start_time", std::to_string(start_time));
|
||||
if (end_time) add(q, "end_time", std::to_string(*end_time));
|
||||
if (!bucket_width.empty()) add(q, "bucket_width", bucket_width);
|
||||
if (limit) add(q, "limit", std::to_string(*limit));
|
||||
if (!page.empty()) add(q, "page", enc(page));
|
||||
if (!user_email.empty()) add(q, "user_email", enc(user_email));
|
||||
return q;
|
||||
}
|
||||
|
||||
std::string UsageQuery::query_v2() const {
|
||||
validate_v2();
|
||||
std::string q;
|
||||
add(q, "dataset", dataset);
|
||||
add(q, "group_by", group_by);
|
||||
add(q, "start_time", std::to_string(start_time));
|
||||
if (end_time) add(q, "end_time", std::to_string(*end_time));
|
||||
if (limit) add(q, "limit", std::to_string(*limit));
|
||||
if (!page.empty()) add(q, "page", enc(page));
|
||||
return q;
|
||||
}
|
||||
|
||||
UsageReport UsageReport::parse_v1(const Json& doc) {
|
||||
UsageReport r;
|
||||
r.raw = doc;
|
||||
r.categories = J::strings(doc, "categories");
|
||||
r.has_more = J::flag(doc, "has_more");
|
||||
r.next_page = J::str(doc, "next_page");
|
||||
if (const Json* d = J::arr(doc, "data"))
|
||||
for (const Json& b : d->as_array()) {
|
||||
if (r.buckets.size() >= PerplexityCaps::kMaxItemsReturned) {
|
||||
r.truncated = true;
|
||||
break;
|
||||
}
|
||||
UsageBucket u;
|
||||
u.start_time = J::num(b, "start_time");
|
||||
u.end_time = J::num(b, "end_time");
|
||||
u.count = J::num(b, "count");
|
||||
if (const Json* c = J::obj(b, "by_categories")) u.by_categories = *c;
|
||||
r.buckets.push_back(std::move(u));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
UsageReport UsageReport::parse_v2(const Json& doc) {
|
||||
UsageReport r;
|
||||
r.raw = doc;
|
||||
r.categories = J::strings(doc, "categories");
|
||||
r.has_more = J::flag(doc, "has_more");
|
||||
r.next_page = J::str(doc, "next_page");
|
||||
if (const Json* d = J::arr(doc, "data"))
|
||||
for (const Json& day : d->as_array())
|
||||
if (const Json* rs = J::arr(day, "results"))
|
||||
for (const Json& m : rs->as_array()) {
|
||||
if (r.buckets.size() >= PerplexityCaps::kMaxItemsReturned) {
|
||||
r.truncated = true;
|
||||
return r;
|
||||
}
|
||||
UsageBucket u;
|
||||
u.start_time = J::num(day, "start_time");
|
||||
u.end_time = J::num(day, "end_time");
|
||||
u.user_email = J::str(m, "user_email");
|
||||
u.count = J::num(m, "count");
|
||||
if (const Json* c = J::obj(m, "by_categories")) u.by_categories = *c;
|
||||
r.buckets.push_back(std::move(u));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
ModelList Account::models() { return ModelList::parse(client_->get(Calls::render(Call::kModels))); }
|
||||
|
||||
UsageReport Account::usage(const UsageQuery& q) {
|
||||
return UsageReport::parse_v1(client_->get(Calls::render(Call::kUsageV1) + q.query_v1()));
|
||||
}
|
||||
|
||||
UsageReport Account::usage_per_member(const UsageQuery& q) {
|
||||
return UsageReport::parse_v2(client_->get(Calls::render(Call::kUsageV2) + q.query_v2()));
|
||||
}
|
||||
|
||||
AuthToken Account::generate_token(const std::string& token_name) {
|
||||
Json body = Json::object();
|
||||
if (!token_name.empty()) {
|
||||
Rules::require(token_name.size() <= 128, "token_name over 128 characters");
|
||||
body.set("token_name", Json(token_name));
|
||||
}
|
||||
Json doc = client_->post(Calls::render(Call::kAuthGenerate), body);
|
||||
AuthToken t;
|
||||
t.auth_token = J::str(doc, "auth_token");
|
||||
t.created_at_epoch_seconds = J::dbl(doc, "created_at_epoch_seconds");
|
||||
t.token_name = J::str(doc, "token_name");
|
||||
return t;
|
||||
}
|
||||
|
||||
Json Account::revoke_token(const std::string& auth_token) {
|
||||
Rules::require(!auth_token.empty() && auth_token.size() <= 512, "auth_token must be 1..512 characters");
|
||||
Json body = Json::object();
|
||||
body.set("auth_token", Json(auth_token));
|
||||
return client_->post(Calls::render(Call::kAuthRevoke), body);
|
||||
}
|
||||
|
||||
} // namespace molo::perplexity
|
||||
Reference in New Issue
Block a user