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:
@@ -0,0 +1,105 @@
|
||||
// Routes: POST /ai/perplexity/* (405, 422, dry runs, 503 without a credential)
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
unsetenv("PERPLEXITY_API_KEY");
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& route, const std::string& body) {
|
||||
return f.client->post("/ai/perplexity/" + route, body, "application/json");
|
||||
};
|
||||
const char* all[] = {"sonar", "stream", "async", "search", "embeddings", "agent", "orchestrator",
|
||||
"decisions", "models", "usage", "batches", "skills"};
|
||||
for (const char* r : all) {
|
||||
std::string route = std::string("/ai/perplexity/") + r;
|
||||
check(f.client->get(route).status == 405, route + " get is 405");
|
||||
check(post(r, "not json").status == 422, route + " bad json is 422");
|
||||
check(post(r, "[1]").status == 422, route + " non-object is 422");
|
||||
}
|
||||
// 422: input rules run before anything else
|
||||
check(post("sonar", "{}").status == 422, "sonar needs prompt");
|
||||
check(post("sonar", "{\"prompt\":\"hi\",\"max_tokens\":0}").status == 422, "max_tokens 0");
|
||||
check(post("sonar", "{\"prompt\":\"hi\",\"temperature\":3}").status == 422, "temperature over 2");
|
||||
check(post("sonar", "{\"prompt\":\"hi\",\"search_mode\":\"dark\"}").status == 422, "bad search_mode");
|
||||
check(post("sonar", "{\"prompt\":\"hi\",\"search_after_date_filter\":\"2025-01-01\"}").status == 422, "bad date format");
|
||||
check(post("sonar", "{\"prompt\":\"hi\",\"json_schema\":[1]}").status == 422, "schema must be object");
|
||||
check(post("async", "{\"action\":\"fly\"}").status == 422, "bad async action");
|
||||
check(post("async", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad async id");
|
||||
check(post("search", "{\"query\":\" \"}").status == 422, "blank query");
|
||||
check(post("search", "{\"query\":\"x\",\"max_results\":21}").status == 422, "max_results over 20");
|
||||
check(post("search", "{\"queries\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"]}").status == 422, "more than 5 queries");
|
||||
check(post("embeddings", "{}").status == 422, "embeddings need input");
|
||||
check(post("embeddings", "{\"input\":[\"x\"],\"dimensions\":64}").status == 422, "dimensions under 128");
|
||||
check(post("embeddings", "{\"documents\":[[]]}").status == 422, "empty document");
|
||||
check(post("agent", "{}").status == 422, "agent needs input");
|
||||
check(post("agent", "{\"input\":\"x\",\"model\":\"anthropic/claude-sonnet-5-5\"}").status == 422, "anthropic models need max_output_tokens");
|
||||
check(post("agent", "{\"input\":\"x\",\"tools\":[\"telepathy\"]}").status == 422, "unknown tool");
|
||||
check(post("agent", "{\"action\":\"get\",\"id\":\"a/b\"}").status == 422, "bad agent id");
|
||||
check(post("orchestrator", "{\"model\":\"kimi\",\"prompt\":\"x\"}").status == 422, "model needs a creator prefix");
|
||||
check(post("orchestrator", "{\"api\":\"graphql\",\"model\":\"a/b\",\"prompt\":\"x\"}").status == 422, "bad orchestrator api");
|
||||
check(post("decisions", "{\"state\":\"x\"}").status == 422, "decisions need questions");
|
||||
check(post("models", "{\"kind\":\"other\"}").status == 422, "bad models kind");
|
||||
check(post("usage", "{}").status == 422, "usage needs dataset");
|
||||
check(post("usage", "{\"dataset\":\"query_volume\",\"bucket_width\":\"1h\",\"start_time\":1}").status == 422, "daily-only dataset rejects 1h");
|
||||
check(post("batches", "{\"action\":\"upload\",\"jsonl\":\"not json\"}").status == 422, "bad jsonl");
|
||||
check(post("batches", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad batch id");
|
||||
check(post("skills", "{\"action\":\"delete\",\"id\":\"skill_1\"}").status == 422, "delete needs expected_revision");
|
||||
check(post("skills", "{\"action\":\"create\",\"data_base64\":\"aGVsbG8=\"}").status == 422, "not a zip");
|
||||
check(post("skills", "{\"action\":\"create\",\"data_base64\":\"@@\"}").status == 422, "bad base64");
|
||||
|
||||
// dry runs: paid or resource-changing routes show the exact request
|
||||
auto dry = post("sonar", "{\"prompt\":\"hi\",\"model\":\"sonar-pro\",\"search_mode\":\"academic\"}");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"model\":\"sonar-pro\"") &&
|
||||
contains(dry.body, "/v1/sonar") && contains(dry.body, "\"search_mode\":\"academic\""),
|
||||
"sonar is a dry run without confirm");
|
||||
check(contains(post("stream", "{\"prompt\":\"hi\"}").body, "\"stream\":true"), "stream preview");
|
||||
check(contains(post("async", "{\"action\":\"create\",\"prompt\":\"hi\",\"model\":\"sonar-deep-research\"}").body, "/v1/async/sonar"),
|
||||
"async create is a dry run");
|
||||
check(contains(post("search", "{\"query\":\"x\"}").body, "\"dry_run\":true"), "search is a dry run");
|
||||
check(contains(post("embeddings", "{\"input\":[\"x\"]}").body, "/v1/embeddings"), "embeddings dry run");
|
||||
check(contains(post("embeddings", "{\"documents\":[[\"a\",\"b\"]]}").body, "/v1/contextualizedembeddings"), "contextualized dry run");
|
||||
check(contains(post("agent", "{\"input\":\"x\",\"tools\":[\"web_search\"]}").body, "\"type\":\"web_search\""), "agent dry run shows tools");
|
||||
check(contains(post("agent", "{\"action\":\"cancel\",\"id\":\"resp_1\"}").body, "\"dry_run\":true"), "cancel is a dry run");
|
||||
check(contains(post("orchestrator", "{\"model\":\"perplexity/kimi-k3\",\"prompt\":\"x\"}").body, "/orchestrator/v1/chat/completions"),
|
||||
"orchestrator dry run");
|
||||
check(contains(post("decisions", "{\"state\":\"x\",\"questions\":{\"q\":{\"type\":\"noul\",\"instructions\":\"?\"}}}").body, "\"dry_run\":true"),
|
||||
"decisions dry run");
|
||||
check(contains(post("batches", "{\"action\":\"create\",\"input_file_id\":\"file_1\"}").body, "\"dry_run\":true"), "batch create dry run");
|
||||
check(contains(post("batches", "{\"action\":\"cancel\",\"id\":\"batch_1\"}").body, "\"dry_run\":true"), "batch cancel dry run");
|
||||
check(contains(post("batches", "{\"action\":\"file_delete\",\"id\":\"file_1\"}").body, "\"dry_run\":true"), "file delete dry run");
|
||||
check(contains(post("batches", "{\"action\":\"upload\",\"jsonl\":\"{\\\"custom_id\\\":\\\"1\\\"}\"}").body, "\"dry_run\":true"), "upload dry run");
|
||||
check(contains(post("skills", "{\"action\":\"delete\",\"id\":\"skill_1\",\"expected_revision\":\"r1\"}").body, "\"dry_run\":true"), "skill delete dry run");
|
||||
check(contains(post("skills", "{\"action\":\"create\",\"data_base64\":\"UEsDBHJlc3Q=\"}").body, "\"dry_run\":true"), "skill create dry run");
|
||||
|
||||
// 503: free calls and confirmed calls need the credential
|
||||
const char* reads[] = {"{}", "{\"kind\":\"orchestrator\"}"};
|
||||
for (const char* body : reads) {
|
||||
auto res = post("models", body);
|
||||
check(res.status == 503 && contains(res.body, "perplexity_not_configured"), "models without key is 503");
|
||||
}
|
||||
check(post("usage", "{\"dataset\":\"credit_usage\",\"start_time\":1}").status == 503, "usage without key is 503");
|
||||
check(post("usage", "{\"dataset\":\"credit_usage\",\"start_time\":1,\"per_member\":true}").status == 503, "per member usage without key is 503");
|
||||
check(post("async", "{\"action\":\"list\"}").status == 503, "async list without key is 503");
|
||||
check(post("async", "{\"action\":\"get\",\"id\":\"req_1\"}").status == 503, "async get without key is 503");
|
||||
check(post("agent", "{\"action\":\"get\",\"id\":\"resp_1\"}").status == 503, "agent get without key is 503");
|
||||
check(post("agent", "{\"action\":\"files\",\"id\":\"resp_1\"}").status == 503, "agent files without key is 503");
|
||||
check(post("batches", "{\"action\":\"list\"}").status == 503, "batches list without key is 503");
|
||||
check(post("batches", "{\"action\":\"files\"}").status == 503, "batch files without key is 503");
|
||||
check(post("skills", "{\"action\":\"list\"}").status == 503, "skills list without key is 503");
|
||||
check(post("sonar", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed sonar without key is 503");
|
||||
check(post("stream", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed stream without key is 503");
|
||||
check(post("search", "{\"query\":\"x\",\"confirm\":true}").status == 503, "confirmed search without key is 503");
|
||||
check(post("embeddings", "{\"input\":[\"x\"],\"confirm\":true}").status == 503, "confirmed embeddings without key is 503");
|
||||
check(post("agent", "{\"input\":\"x\",\"confirm\":true}").status == 503, "confirmed agent without key is 503");
|
||||
check(post("orchestrator", "{\"model\":\"a/b\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed orchestrator without key is 503");
|
||||
check(post("decisions", "{\"state\":\"x\",\"questions\":{\"q\":{\"type\":\"noul\",\"instructions\":\"?\"}},\"confirm\":true}").status == 503,
|
||||
"confirmed decisions without key is 503");
|
||||
check(post("skills", "{\"action\":\"delete\",\"id\":\"skill_1\",\"expected_revision\":\"r1\",\"confirm\":true}").status == 503,
|
||||
"confirmed skill delete without key is 503");
|
||||
check(post("batches", "{\"action\":\"cancel\",\"id\":\"b1\",\"confirm\":true}").status == 503, "confirmed batch cancel without key is 503");
|
||||
return finish("ai/perplexity/routes");
|
||||
}
|
||||
Reference in New Issue
Block a user