2026-10-25 09:14:26 +01:00
|
|
|
#include "../AiDemos.hpp"
|
|
|
|
|
|
2026-10-25 15:01:55 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
2026-10-25 09:14:26 +01:00
|
|
|
#include "../../fw/Router.hpp"
|
2026-10-25 15:01:55 +01:00
|
|
|
#include "../../fw/Templates.hpp"
|
|
|
|
|
#include "security/crypto/Base64.h"
|
|
|
|
|
#include "vendors/perplexity/account.h"
|
|
|
|
|
#include "vendors/perplexity/agent.h"
|
|
|
|
|
#include "vendors/perplexity/batches.h"
|
|
|
|
|
#include "vendors/perplexity/calls.h"
|
|
|
|
|
#include "vendors/perplexity/caps.h"
|
|
|
|
|
#include "vendors/perplexity/decisions.h"
|
|
|
|
|
#include "vendors/perplexity/embeddings.h"
|
|
|
|
|
#include "vendors/perplexity/errors.h"
|
|
|
|
|
#include "vendors/perplexity/orchestrator.h"
|
|
|
|
|
#include "vendors/perplexity/platform.h"
|
|
|
|
|
#include "vendors/perplexity/rules.h"
|
|
|
|
|
#include "vendors/perplexity/search.h"
|
|
|
|
|
#include "vendors/perplexity/skills.h"
|
|
|
|
|
#include "vendors/perplexity/sonar.h"
|
2026-10-25 09:14:26 +01:00
|
|
|
|
|
|
|
|
namespace molo::example::app {
|
|
|
|
|
|
2026-10-25 15:01:55 +01:00
|
|
|
// Perplexity API platform over the real src/vendors/perplexity part. GET routes
|
|
|
|
|
// need no credential. Every route that calls Perplexity is a POST: 422 on bad
|
|
|
|
|
// input, 503 perplexity_not_configured without PERPLEXITY_API_KEY. Routes that
|
|
|
|
|
// cost money (Sonar, Search, Agent, embeddings, decisions, orchestrator, batch
|
|
|
|
|
// create) or change resources (cancel, delete, upload) are dry runs that show
|
|
|
|
|
// the exact request unless the body has "confirm":true.
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
namespace pp = perplexity;
|
|
|
|
|
using Json = clients::body::Json;
|
|
|
|
|
|
|
|
|
|
constexpr std::size_t kMaxBody = 8 * 1024 * 1024; // uploads travel as base64
|
|
|
|
|
|
|
|
|
|
const char kPage[] = R"PERPLEXITY({% extends "base.html" %}
|
|
|
|
|
{% block content %}
|
|
|
|
|
<h1>Perplexity API</h1>
|
|
|
|
|
<p class="hint">Sonar chat (search options, citations, structured output, streaming, async), Search API,
|
|
|
|
|
embeddings, Agent API (tools, skills, background), Decisions, Orchestrator, batches, skills, models, usage.
|
|
|
|
|
Paid and destructive calls are dry runs until the body says <code>"confirm":true</code>.</p>
|
|
|
|
|
<p id="pp-config" class="hint">Checking credential...</p>
|
|
|
|
|
<section class="pp">
|
|
|
|
|
<label>Route
|
|
|
|
|
<select id="pp-route"></select></label>
|
|
|
|
|
<label>Body (JSON object) <textarea id="pp-body" rows="8"></textarea></label>
|
|
|
|
|
<div class="pp-actions">
|
|
|
|
|
<button id="pp-send" type="button" class="primary">Send</button>
|
|
|
|
|
<button id="pp-confirm" type="button">Send with confirm</button>
|
|
|
|
|
</div>
|
|
|
|
|
<p id="pp-error" class="error" role="alert"></p>
|
|
|
|
|
<pre id="pp-out" class="pp-out"></pre>
|
|
|
|
|
</section>
|
|
|
|
|
<style>
|
|
|
|
|
.pp label{display:block;margin:.5rem 0}
|
|
|
|
|
.pp select,.pp textarea{width:100%;box-sizing:border-box}
|
|
|
|
|
.pp-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
|
|
|
|
.pp-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
|
|
|
|
</style>
|
|
|
|
|
<script src="/embedded/ai_perplexity.js"></script>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
)PERPLEXITY";
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
|
|
|
|
|
|
|
|
|
fw::Response json_out(const Json& j, int status = 200) {
|
|
|
|
|
fw::Response res = fw::JsonResponse(j.dump(), status);
|
|
|
|
|
res.headers.set("Cache-Control", "no-store");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json parse_body(const fw::Request& req) {
|
|
|
|
|
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
|
|
|
if (req.body.empty()) return Json::object();
|
|
|
|
|
try {
|
|
|
|
|
Json j = Json::parse(req.body);
|
|
|
|
|
if (!j.is_object()) bad("body must be a JSON object");
|
|
|
|
|
return j;
|
|
|
|
|
} catch (const clients::body::JsonError&) {
|
|
|
|
|
bad("body is not valid JSON");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string text(const Json& b, const char* key, bool required = false) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) {
|
|
|
|
|
if (required) bad(std::string(key) + " is required");
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (!v->is_string()) bad(std::string(key) + " must be a string");
|
|
|
|
|
return v->as_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<int> number(const Json& b, const char* key) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return std::nullopt;
|
|
|
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
|
|
|
return static_cast<int>(v->as_int());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<double> real(const Json& b, const char* key) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return std::nullopt;
|
|
|
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
|
|
|
return v->as_number();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool flag(const Json& b, const char* key) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return false;
|
|
|
|
|
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
|
|
|
|
|
return v->as_bool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<bool> opt_flag(const Json& b, const char* key) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return std::nullopt;
|
|
|
|
|
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
|
|
|
|
|
return v->as_bool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> strings(const Json& b, const char* key) {
|
|
|
|
|
std::vector<std::string> out;
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return out;
|
|
|
|
|
if (!v->is_array()) bad(std::string(key) + " must be an array of strings");
|
|
|
|
|
for (const Json& s : v->as_array()) {
|
|
|
|
|
if (!s.is_string()) bad(std::string(key) + " must be an array of strings");
|
|
|
|
|
out.push_back(s.as_string());
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json strs(const std::vector<std::string>& v) {
|
|
|
|
|
Json a = Json::array();
|
|
|
|
|
for (const std::string& s : v) a.push(Json(s));
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json sonar_json(const pp::SonarResult& r) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(r.id));
|
|
|
|
|
o.set("model", Json(r.model));
|
|
|
|
|
if (!r.choices.empty()) {
|
|
|
|
|
o.set("finish_reason", Json(r.first().finish_reason));
|
|
|
|
|
o.set("content", Json(r.first().content));
|
|
|
|
|
}
|
|
|
|
|
o.set("citations", strs(r.citations));
|
|
|
|
|
Json results = Json::array();
|
|
|
|
|
for (const pp::WebResult& w : r.search_results) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("title", Json(w.title));
|
|
|
|
|
x.set("url", Json(w.url));
|
|
|
|
|
x.set("date", Json(w.date));
|
|
|
|
|
results.push(x);
|
|
|
|
|
}
|
|
|
|
|
o.set("search_results", results);
|
|
|
|
|
o.set("related_questions", strs(r.related_questions));
|
|
|
|
|
Json u = Json::object();
|
|
|
|
|
u.set("prompt_tokens", Json(r.usage.prompt_tokens));
|
|
|
|
|
u.set("completion_tokens", Json(r.usage.completion_tokens));
|
|
|
|
|
u.set("total_tokens", Json(r.usage.total_tokens));
|
|
|
|
|
u.set("reasoning_tokens", Json(r.usage.reasoning_tokens));
|
|
|
|
|
u.set("num_search_queries", Json(r.usage.num_search_queries));
|
|
|
|
|
u.set("total_cost_usd", Json(r.usage.cost.total));
|
|
|
|
|
o.set("usage", u);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json agent_json(const pp::AgentResult& r) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(r.id));
|
|
|
|
|
o.set("status", Json(r.status));
|
|
|
|
|
o.set("model", Json(r.model));
|
|
|
|
|
o.set("output_text", Json(r.output_text()));
|
|
|
|
|
o.set("queries", strs(r.queries()));
|
|
|
|
|
Json results = Json::array();
|
|
|
|
|
for (const pp::AgentSearchResult& s : r.search_results()) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("id", Json(static_cast<int>(s.id)));
|
|
|
|
|
x.set("title", Json(s.title));
|
|
|
|
|
x.set("url", Json(s.url));
|
|
|
|
|
results.push(x);
|
|
|
|
|
}
|
|
|
|
|
o.set("search_results", results);
|
|
|
|
|
Json calls = Json::array();
|
|
|
|
|
for (const pp::AgentFunctionCall& c : r.function_calls()) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("call_id", Json(c.call_id));
|
|
|
|
|
x.set("name", Json(c.name));
|
|
|
|
|
x.set("arguments", Json(c.arguments));
|
|
|
|
|
calls.push(x);
|
|
|
|
|
}
|
|
|
|
|
o.set("function_calls", calls);
|
|
|
|
|
if (r.error.present()) o.set("error", Json(r.error.message));
|
|
|
|
|
if (r.has_pause) o.set("paused", Json(r.pause.window));
|
|
|
|
|
Json u = Json::object();
|
|
|
|
|
u.set("input_tokens", Json(static_cast<int>(r.usage.input_tokens)));
|
|
|
|
|
u.set("output_tokens", Json(static_cast<int>(r.usage.output_tokens)));
|
|
|
|
|
u.set("total_cost_usd", Json(r.usage.cost.total));
|
|
|
|
|
o.set("usage", u);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json dry_run(const Json& request, const std::string& path) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("dry_run", Json(true));
|
|
|
|
|
o.set("would_send_to", Json(path));
|
|
|
|
|
o.set("request", request);
|
|
|
|
|
o.set("hint", Json("send confirm:true to run it"));
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pp::SonarParams sonar_params(const Json& b) {
|
|
|
|
|
pp::SonarParams p;
|
|
|
|
|
std::string model = text(b, "model");
|
|
|
|
|
if (!model.empty()) p.model = model;
|
|
|
|
|
std::string system = text(b, "system");
|
|
|
|
|
if (!system.empty()) p.add(pp::Messages::system(system));
|
|
|
|
|
p.add(pp::Messages::user(text(b, "prompt", true)));
|
|
|
|
|
p.max_tokens = number(b, "max_tokens");
|
|
|
|
|
p.temperature = real(b, "temperature");
|
|
|
|
|
p.top_p = real(b, "top_p");
|
|
|
|
|
p.search_mode = text(b, "search_mode");
|
|
|
|
|
p.search_domain_filter = strings(b, "search_domain_filter");
|
|
|
|
|
p.search_language_filter = strings(b, "search_language_filter");
|
|
|
|
|
p.search_recency_filter = text(b, "search_recency_filter");
|
|
|
|
|
p.search_after_date_filter = text(b, "search_after_date_filter");
|
|
|
|
|
p.search_before_date_filter = text(b, "search_before_date_filter");
|
|
|
|
|
p.return_images = opt_flag(b, "return_images");
|
|
|
|
|
p.return_related_questions = opt_flag(b, "return_related_questions");
|
|
|
|
|
p.disable_search = opt_flag(b, "disable_search");
|
|
|
|
|
p.reasoning_effort = text(b, "reasoning_effort");
|
|
|
|
|
p.language_preference = text(b, "language_preference");
|
|
|
|
|
p.web_search_options.search_context_size = text(b, "search_context_size");
|
|
|
|
|
p.web_search_options.search_type = text(b, "search_type");
|
|
|
|
|
p.web_search_options.user_location.country = text(b, "country");
|
|
|
|
|
if (const Json* schema = b.find("json_schema"); schema && !schema->is_null()) {
|
|
|
|
|
if (!schema->is_object()) bad("json_schema must be a JSON Schema object");
|
|
|
|
|
p.response_format = pp::StructuredOutput::json_schema(*schema, text(b, "schema_name"));
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One route = validate into a Plan, then the generic runner decides dry run,
|
|
|
|
|
// credential, call and error mapping.
|
|
|
|
|
struct Plan {
|
|
|
|
|
Json request; // what would be sent (shown in a dry run)
|
|
|
|
|
std::string path; // for the dry-run message
|
|
|
|
|
bool needs_confirm = false; // paid or changes a resource
|
|
|
|
|
std::function<Json()> call;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PerplexityRoutes {
|
|
|
|
|
public:
|
|
|
|
|
explicit PerplexityRoutes(pp::Platform platform) : p_(std::move(platform)) {}
|
|
|
|
|
const pp::Platform& platform() const { return p_; }
|
|
|
|
|
|
|
|
|
|
using Planner = std::function<Plan(const Json&)>;
|
|
|
|
|
|
|
|
|
|
fw::Response run(const fw::Request& req, const Planner& planner) const {
|
|
|
|
|
Json body = parse_body(req);
|
|
|
|
|
Plan plan;
|
|
|
|
|
try {
|
|
|
|
|
plan = planner(body);
|
|
|
|
|
} catch (const llm::ApiError& e) {
|
|
|
|
|
map(e);
|
|
|
|
|
}
|
|
|
|
|
if (plan.needs_confirm && !flag(body, "confirm")) return json_out(dry_run(plan.request, plan.path));
|
|
|
|
|
try {
|
|
|
|
|
return json_out(plan.call());
|
|
|
|
|
} catch (const llm::ApiError& e) {
|
|
|
|
|
map(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- planners -----------------------------------------------------------
|
|
|
|
|
Plan sonar(const Json& b) const {
|
|
|
|
|
pp::SonarParams params = sonar_params(b);
|
|
|
|
|
Plan pl{params.to_json(false), "/v1/sonar", true, nullptr};
|
|
|
|
|
auto api = std::make_shared<pp::Sonar>(p_.client);
|
|
|
|
|
pl.call = [api, params] { return sonar_json(api->create(params)); };
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan stream(const Json& b) const {
|
|
|
|
|
pp::SonarParams params = sonar_params(b);
|
|
|
|
|
Plan pl{params.to_json(true), "/v1/sonar", true, nullptr};
|
|
|
|
|
auto api = std::make_shared<pp::Sonar>(p_.client);
|
|
|
|
|
pl.call = [api, params] {
|
|
|
|
|
std::size_t chunks = 0;
|
|
|
|
|
pp::SonarResult r = api->stream(params, [&](const pp::SonarDelta&) { return ++chunks < pp::PerplexityCaps::kMaxStreamEvents; });
|
|
|
|
|
Json o = sonar_json(r);
|
|
|
|
|
o.set("chunks", Json(static_cast<int>(chunks)));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan async(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action", true);
|
|
|
|
|
auto api = std::make_shared<pp::Sonar>(p_.client);
|
|
|
|
|
auto job_json = [](const pp::AsyncJob& j) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(j.id));
|
|
|
|
|
o.set("status", Json(j.status));
|
|
|
|
|
o.set("model", Json(j.model));
|
|
|
|
|
if (!j.error_message.empty()) o.set("error_message", Json(j.error_message));
|
|
|
|
|
if (j.has_response) o.set("response", sonar_json(j.response));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
if (action == "create") {
|
|
|
|
|
pp::SonarParams params = sonar_params(b);
|
|
|
|
|
std::string key = text(b, "idempotency_key");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("request", params.to_json(false));
|
|
|
|
|
return Plan{req, "/v1/async/sonar", true, [api, params, key, job_json] { return job_json(api->create_async(params, key)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "get") {
|
|
|
|
|
std::string id = text(b, "id", true);
|
|
|
|
|
if (!llm::Service::valid_id(id)) bad("id has invalid characters");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
return Plan{req, "/v1/async/sonar/{id}", false, [api, id, job_json] { return job_json(api->get_async(id)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "list") {
|
|
|
|
|
return Plan{Json::object(), "/v1/async/sonar", false, [api] {
|
|
|
|
|
pp::AsyncList l = api->list_async();
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::AsyncSummary& s : l.requests) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("id", Json(s.id));
|
|
|
|
|
x.set("status", Json(s.status));
|
|
|
|
|
x.set("model", Json(s.model));
|
|
|
|
|
arr.push(x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("requests", arr);
|
|
|
|
|
o.set("next_token", Json(l.next_token));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
bad("action must be create, get or list");
|
|
|
|
|
}
|
|
|
|
|
Plan search(const Json& b) const {
|
|
|
|
|
pp::SearchParams sp;
|
|
|
|
|
std::string q = text(b, "query");
|
|
|
|
|
if (!q.empty()) sp.query.push_back(q);
|
|
|
|
|
for (const std::string& extra : strings(b, "queries")) sp.query.push_back(extra);
|
|
|
|
|
sp.max_results = number(b, "max_results");
|
|
|
|
|
sp.country = text(b, "country");
|
|
|
|
|
sp.search_type = text(b, "search_type");
|
|
|
|
|
sp.max_tokens_per_page = number(b, "max_tokens_per_page");
|
|
|
|
|
sp.search_domain_filter = strings(b, "search_domain_filter");
|
|
|
|
|
sp.search_language_filter = strings(b, "search_language_filter");
|
|
|
|
|
sp.search_recency_filter = text(b, "search_recency_filter");
|
|
|
|
|
Plan pl{sp.to_json(), "/search", true, nullptr};
|
|
|
|
|
auto api = std::make_shared<pp::Search>(p_.client);
|
|
|
|
|
pl.call = [api, sp] {
|
|
|
|
|
pp::SearchResponse r = api->run(sp);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::SearchPage& s : r.results) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("title", Json(s.title));
|
|
|
|
|
x.set("url", Json(s.url));
|
|
|
|
|
x.set("snippet", Json(s.snippet));
|
|
|
|
|
x.set("date", Json(s.date));
|
|
|
|
|
arr.push(x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(r.id));
|
|
|
|
|
o.set("results", arr);
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan embeddings(const Json& b) const {
|
|
|
|
|
auto api = std::make_shared<pp::Embeddings>(p_.client);
|
|
|
|
|
auto summarize = [](const pp::EmbedResult& r) {
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::Vector& v : r.vectors) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("index", Json(v.index));
|
|
|
|
|
x.set("dimensions", Json(static_cast<int>(v.int8().size())));
|
|
|
|
|
arr.push(x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("model", Json(r.model));
|
|
|
|
|
o.set("vectors", arr);
|
|
|
|
|
o.set("prompt_tokens", Json(static_cast<int>(r.usage.prompt_tokens)));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
if (const Json* docs = b.find("documents"); docs && !docs->is_null()) {
|
|
|
|
|
if (!docs->is_array()) bad("documents must be an array of arrays of strings");
|
|
|
|
|
pp::ContextEmbedParams cp;
|
|
|
|
|
std::string model = text(b, "model");
|
|
|
|
|
if (!model.empty()) cp.model = model;
|
|
|
|
|
for (const Json& d : docs->as_array()) {
|
|
|
|
|
if (!d.is_array()) bad("documents must be an array of arrays of strings");
|
|
|
|
|
std::vector<std::string> chunks;
|
|
|
|
|
for (const Json& c : d.as_array()) {
|
|
|
|
|
if (!c.is_string()) bad("documents must be an array of arrays of strings");
|
|
|
|
|
chunks.push_back(c.as_string());
|
|
|
|
|
}
|
|
|
|
|
cp.input.push_back(chunks);
|
|
|
|
|
}
|
|
|
|
|
cp.dimensions = number(b, "dimensions").value_or(0);
|
|
|
|
|
cp.encoding_format = text(b, "encoding_format");
|
|
|
|
|
Plan pl{cp.to_json(), "/v1/contextualizedembeddings", true, nullptr};
|
|
|
|
|
pl.call = [api, cp] {
|
|
|
|
|
pp::ContextEmbedResult r = api->create_contextualized(cp);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const auto& doc : r.documents) arr.push(Json(static_cast<int>(doc.size())));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("model", Json(r.model));
|
|
|
|
|
o.set("chunks_per_document", arr);
|
|
|
|
|
o.set("prompt_tokens", Json(static_cast<int>(r.usage.prompt_tokens)));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
pp::EmbedParams ep;
|
|
|
|
|
std::string model = text(b, "model");
|
|
|
|
|
if (!model.empty()) ep.model = model;
|
|
|
|
|
ep.input = strings(b, "input");
|
|
|
|
|
ep.dimensions = number(b, "dimensions").value_or(0);
|
|
|
|
|
ep.encoding_format = text(b, "encoding_format");
|
|
|
|
|
Plan pl{ep.to_json(), "/v1/embeddings", true, nullptr};
|
|
|
|
|
pl.call = [api, ep, summarize] { return summarize(api->create(ep)); };
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan agent(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action");
|
|
|
|
|
if (action.empty()) action = "create";
|
|
|
|
|
auto api = std::make_shared<pp::Agent>(p_.client);
|
|
|
|
|
if (action == "get" || action == "cancel" || action == "files") {
|
|
|
|
|
std::string id = text(b, "id", true);
|
|
|
|
|
if (!llm::Service::valid_id(id)) bad("id has invalid characters");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
if (action == "get") return Plan{req, "/v1/agent/{id}", false, [api, id] { return agent_json(api->get(id)); }};
|
|
|
|
|
if (action == "files")
|
|
|
|
|
return Plan{req, "/v1/agent/{id}/files", false, [api, id] {
|
|
|
|
|
pp::AgentFileList l = api->files(id);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::AgentFile& f : l.files) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("id", Json(f.id));
|
|
|
|
|
x.set("filename", Json(f.filename));
|
|
|
|
|
x.set("bytes", Json(static_cast<int>(f.bytes)));
|
|
|
|
|
arr.push(x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("files", arr);
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
return Plan{req, "POST /v1/agent/{id}/cancel", true, [api, id] { return api->cancel(id); }};
|
|
|
|
|
}
|
|
|
|
|
if (action != "create") bad("action must be create, get, cancel or files");
|
|
|
|
|
pp::AgentParams p;
|
|
|
|
|
p.input = Json(text(b, "input", true));
|
|
|
|
|
p.model = text(b, "model");
|
|
|
|
|
p.effort = text(b, "effort");
|
|
|
|
|
p.models = strings(b, "models");
|
|
|
|
|
p.instructions = text(b, "instructions");
|
|
|
|
|
p.max_output_tokens = number(b, "max_output_tokens");
|
|
|
|
|
p.max_steps = number(b, "max_steps");
|
|
|
|
|
p.reasoning_effort = text(b, "reasoning_effort");
|
|
|
|
|
p.background = opt_flag(b, "background");
|
|
|
|
|
for (const std::string& t : strings(b, "tools")) {
|
|
|
|
|
if (t == "web_search") p.tools.push_back(pp::AgentTools::web_search());
|
|
|
|
|
else if (t == "fetch_url") p.tools.push_back(pp::AgentTools::fetch_url());
|
|
|
|
|
else if (t == "image_search") p.tools.push_back(pp::AgentTools::image_search());
|
|
|
|
|
else if (t == "finance_search") p.tools.push_back(pp::AgentTools::finance_search());
|
|
|
|
|
else if (t == "people_search") p.tools.push_back(pp::AgentTools::people_search());
|
|
|
|
|
else if (t == "sandbox") p.tools.push_back(pp::AgentTools::sandbox());
|
|
|
|
|
else bad("tools entries are web_search, fetch_url, image_search, finance_search, people_search or sandbox");
|
|
|
|
|
}
|
|
|
|
|
for (const std::string& s : strings(b, "skills")) p.skills.push_back(pp::AgentSkills::builtin(s));
|
|
|
|
|
if (p.model.empty() && p.effort.empty() && p.models.empty()) p.effort = "fast";
|
|
|
|
|
Plan pl{p.to_json(false), "/v1/agent", true, nullptr};
|
|
|
|
|
pl.call = [api, p] { return agent_json(api->create(p)); };
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan orchestrator(const Json& b) const {
|
|
|
|
|
std::string api_name = text(b, "api");
|
|
|
|
|
if (api_name.empty()) api_name = "chat";
|
|
|
|
|
pp::Call call;
|
|
|
|
|
if (api_name == "chat") call = pp::Call::kOrchChat;
|
|
|
|
|
else if (api_name == "messages") call = pp::Call::kOrchMessages;
|
|
|
|
|
else if (api_name == "responses") call = pp::Call::kOrchResponses;
|
|
|
|
|
else bad("api must be chat, messages or responses");
|
|
|
|
|
const Json* request = b.find("request");
|
|
|
|
|
Json body;
|
|
|
|
|
if (request && request->is_object()) {
|
|
|
|
|
body = *request;
|
|
|
|
|
} else {
|
|
|
|
|
std::string model = text(b, "model", true);
|
|
|
|
|
std::string prompt = text(b, "prompt", true);
|
|
|
|
|
if (call == pp::Call::kOrchChat) body = pp::Orchestrator::chat_request(model, prompt);
|
|
|
|
|
else if (call == pp::Call::kOrchMessages) body = pp::Orchestrator::messages_request(model, prompt, number(b, "max_tokens").value_or(256));
|
|
|
|
|
else body = pp::Orchestrator::responses_request(model, prompt);
|
|
|
|
|
}
|
|
|
|
|
pp::Orchestrator::validate(call, body);
|
|
|
|
|
auto api = std::make_shared<pp::Orchestrator>(p_.client);
|
|
|
|
|
Plan pl{body, pp::Calls::spec_of(call).path, true, nullptr};
|
|
|
|
|
pl.call = [api, call, body] {
|
|
|
|
|
Json reply = call == pp::Call::kOrchChat ? api->chat(body)
|
|
|
|
|
: call == pp::Call::kOrchMessages ? api->messages(body)
|
|
|
|
|
: api->responses(body);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("text", Json(pp::Orchestrator::text_of(call, reply)));
|
|
|
|
|
o.set("id", Json(pp::J::str(reply, "id")));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan decisions(const Json& b) const {
|
|
|
|
|
pp::DecisionsParams p;
|
|
|
|
|
std::string model = text(b, "model");
|
|
|
|
|
if (!model.empty()) p.model = model;
|
|
|
|
|
p.state = Json(text(b, "state", true));
|
|
|
|
|
const Json* qs = b.find("questions");
|
|
|
|
|
if (!qs || !qs->is_object()) bad("questions must be an object of name -> {type, ...}");
|
|
|
|
|
for (const auto& [name, q] : qs->as_object()) p.questions[name] = q;
|
|
|
|
|
Plan pl{p.to_json(), "/v1/decisions", true, nullptr};
|
|
|
|
|
auto api = std::make_shared<pp::Decisions>(p_.client);
|
|
|
|
|
pl.call = [api, p] {
|
|
|
|
|
pp::DecisionsResult r = api->answer(p);
|
|
|
|
|
Json answers = Json::object();
|
|
|
|
|
for (const auto& [name, a] : r.answers) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("type", Json(a.type));
|
|
|
|
|
if (a.type == "noul") x.set("noul", Json(a.noul));
|
|
|
|
|
if (a.type == "choice") x.set("choice", Json(a.choice));
|
|
|
|
|
if (a.type == "score") x.set("score", Json(a.score));
|
|
|
|
|
answers.set(name, x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("model", Json(r.model));
|
|
|
|
|
o.set("answers", answers);
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
Plan models(const Json& b) const {
|
|
|
|
|
std::string kind = text(b, "kind");
|
|
|
|
|
if (kind.empty()) kind = "agent";
|
|
|
|
|
if (kind != "agent" && kind != "orchestrator") bad("kind must be agent or orchestrator");
|
|
|
|
|
bool orch = kind == "orchestrator";
|
|
|
|
|
auto account = std::make_shared<pp::Account>(p_.client);
|
|
|
|
|
auto orchestrator = std::make_shared<pp::Orchestrator>(p_.client);
|
|
|
|
|
return Plan{Json::object(), orch ? "/orchestrator/v1/models" : "/v1/models", false, [account, orchestrator, orch] {
|
|
|
|
|
pp::ModelList l = orch ? orchestrator->models() : account->models();
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::ModelInfo& m : l.models) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("id", Json(m.id));
|
|
|
|
|
x.set("owned_by", Json(m.owned_by));
|
|
|
|
|
if (m.pricing.present) {
|
|
|
|
|
x.set("input_per_1m", Json(m.pricing.input));
|
|
|
|
|
x.set("output_per_1m", Json(m.pricing.output));
|
|
|
|
|
}
|
|
|
|
|
arr.push(x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("models", arr);
|
|
|
|
|
o.set("truncated", Json(l.truncated));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
Plan usage(const Json& b) const {
|
|
|
|
|
pp::UsageQuery q;
|
|
|
|
|
q.dataset = text(b, "dataset", true);
|
|
|
|
|
q.start_time = number(b, "start_time").value_or(0);
|
|
|
|
|
if (auto e = number(b, "end_time")) q.end_time = *e;
|
|
|
|
|
q.bucket_width = text(b, "bucket_width");
|
|
|
|
|
q.limit = number(b, "limit");
|
|
|
|
|
q.page = text(b, "page");
|
|
|
|
|
q.user_email = text(b, "user_email");
|
|
|
|
|
bool per_member = flag(b, "per_member");
|
|
|
|
|
if (per_member) q.group_by = "user_email";
|
|
|
|
|
if (per_member) q.query_v2();
|
|
|
|
|
else q.query_v1();
|
|
|
|
|
auto api = std::make_shared<pp::Account>(p_.client);
|
|
|
|
|
return Plan{Json::object(), per_member ? "/v2/analytics/computer/usage" : "/v1/analytics/computer/usage", false,
|
|
|
|
|
[api, q, per_member] {
|
|
|
|
|
pp::UsageReport r = per_member ? api->usage_per_member(q) : api->usage(q);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::UsageBucket& u : r.buckets) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
x.set("start_time", Json(static_cast<int>(u.start_time)));
|
|
|
|
|
x.set("count", Json(static_cast<int>(u.count)));
|
|
|
|
|
if (!u.user_email.empty()) x.set("user_email", Json(u.user_email));
|
|
|
|
|
arr.push(x);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("buckets", arr);
|
|
|
|
|
o.set("has_more", Json(r.has_more));
|
|
|
|
|
o.set("next_page", Json(r.next_page));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
Plan batches(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action", true);
|
|
|
|
|
auto batches = std::make_shared<pp::Batches>(p_.client);
|
|
|
|
|
auto files = std::make_shared<pp::BatchFiles>(p_.client);
|
|
|
|
|
auto id_of = [&](const char* key) {
|
|
|
|
|
std::string id = text(b, key, true);
|
|
|
|
|
if (!llm::Service::valid_id(id)) bad(std::string(key) + " has invalid characters");
|
|
|
|
|
return id;
|
|
|
|
|
};
|
|
|
|
|
auto batch_json = [](const pp::BatchInfo& x) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(x.id));
|
|
|
|
|
o.set("status", Json(x.status));
|
|
|
|
|
o.set("output_file_id", Json(x.output_file_id));
|
|
|
|
|
o.set("error_file_id", Json(x.error_file_id));
|
|
|
|
|
o.set("total", Json(static_cast<int>(x.total)));
|
|
|
|
|
o.set("completed", Json(static_cast<int>(x.completed)));
|
|
|
|
|
o.set("failed", Json(static_cast<int>(x.failed)));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
auto file_json = [](const pp::BatchFileInfo& f) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(f.id));
|
|
|
|
|
o.set("filename", Json(f.filename));
|
|
|
|
|
o.set("purpose", Json(f.purpose));
|
|
|
|
|
o.set("bytes", Json(static_cast<int>(f.bytes)));
|
|
|
|
|
o.set("deleted", Json(f.deleted));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
if (action == "list") {
|
|
|
|
|
int limit = number(b, "limit").value_or(0);
|
|
|
|
|
std::string after = text(b, "after");
|
|
|
|
|
return Plan{Json::object(), "/v1/batches", false, [batches, limit, after, batch_json] {
|
|
|
|
|
pp::BatchList l = batches->list(limit, after);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::BatchInfo& x : l.batches) arr.push(batch_json(x));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("batches", arr);
|
|
|
|
|
o.set("has_more", Json(l.has_more));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "get") {
|
|
|
|
|
std::string id = id_of("id");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
return Plan{req, "/v1/batches/{id}", false, [batches, id, batch_json] { return batch_json(batches->get(id)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "create") {
|
|
|
|
|
std::string file_id = id_of("input_file_id");
|
|
|
|
|
std::map<std::string, std::string> meta;
|
|
|
|
|
if (const Json* m = b.find("metadata"); m && m->is_object())
|
|
|
|
|
for (const auto& [k, v] : m->as_object()) {
|
|
|
|
|
if (!v.is_string()) bad("metadata values must be strings");
|
|
|
|
|
meta[k] = v.as_string();
|
|
|
|
|
}
|
|
|
|
|
std::string key = text(b, "idempotency_key");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("input_file_id", Json(file_id));
|
|
|
|
|
req.set("endpoint", Json("/v1/responses"));
|
|
|
|
|
return Plan{req, "POST /v1/batches", true, [batches, file_id, meta, key, batch_json] { return batch_json(batches->create(file_id, meta, key)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "cancel") {
|
|
|
|
|
std::string id = id_of("id");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
return Plan{req, "POST /v1/batches/{id}/cancel", true, [batches, id, batch_json] { return batch_json(batches->cancel(id)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "files") {
|
|
|
|
|
int limit = number(b, "limit").value_or(0);
|
|
|
|
|
std::string purpose = text(b, "purpose");
|
|
|
|
|
return Plan{Json::object(), "/v1/files", false, [files, limit, purpose, file_json] {
|
|
|
|
|
pp::BatchFileList l = files->list(limit, "", "", purpose);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::BatchFileInfo& f : l.files) arr.push(file_json(f));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("files", arr);
|
|
|
|
|
o.set("has_more", Json(l.has_more));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "file_get" || action == "file_delete") {
|
|
|
|
|
std::string id = id_of("id");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
if (action == "file_get") return Plan{req, "/v1/files/{id}", false, [files, id, file_json] { return file_json(files->get(id)); }};
|
|
|
|
|
return Plan{req, "DELETE /v1/files/{id}", true, [files, id, file_json] { return file_json(files->remove(id)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "upload") {
|
|
|
|
|
pp::BatchInput in;
|
|
|
|
|
in.jsonl = text(b, "jsonl", true);
|
|
|
|
|
std::string fname = text(b, "filename");
|
|
|
|
|
if (!fname.empty()) in.filename = fname;
|
|
|
|
|
in.validate();
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("filename", Json(in.filename));
|
|
|
|
|
req.set("bytes", Json(static_cast<int>(in.jsonl.size())));
|
|
|
|
|
return Plan{req, "POST /v1/files", true, [files, in, file_json] { return file_json(files->upload(in)); }};
|
|
|
|
|
}
|
|
|
|
|
bad("action must be list, get, create, cancel, files, file_get, file_delete or upload");
|
|
|
|
|
}
|
|
|
|
|
Plan skills(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action", true);
|
|
|
|
|
auto api = std::make_shared<pp::Skills>(p_.client);
|
|
|
|
|
auto skill_json = [](const pp::SkillInfo& s) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("skill_id", Json(s.skill_id));
|
|
|
|
|
o.set("name", Json(s.name));
|
|
|
|
|
o.set("revision", Json(s.revision));
|
|
|
|
|
o.set("latest_revision", Json(s.latest_revision));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
auto id_of = [&]() {
|
|
|
|
|
std::string id = text(b, "id", true);
|
|
|
|
|
if (!llm::Service::valid_id(id)) bad("id has invalid characters");
|
|
|
|
|
return id;
|
|
|
|
|
};
|
|
|
|
|
if (action == "list") {
|
|
|
|
|
int limit = number(b, "limit").value_or(0);
|
|
|
|
|
return Plan{Json::object(), "/v1/skills", false, [api, limit, skill_json] {
|
|
|
|
|
pp::SkillList l = api->list(limit);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::SkillInfo& s : l.skills) arr.push(skill_json(s));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("skills", arr);
|
|
|
|
|
o.set("next_page_token", Json(l.next_page_token));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "get") {
|
|
|
|
|
std::string id = id_of();
|
|
|
|
|
std::string rev = text(b, "revision");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
return Plan{req, "/v1/skills/{id}", false, [api, id, rev, skill_json] { return skill_json(api->get(id, rev)); }};
|
|
|
|
|
}
|
|
|
|
|
if (action == "revisions") {
|
|
|
|
|
std::string id = id_of();
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
return Plan{req, "/v1/skills/{id}/revisions", false, [api, id] {
|
|
|
|
|
pp::SkillRevisionList l = api->revisions(id);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const pp::SkillRevision& r : l.revisions) arr.push(Json(r.revision));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("revisions", arr);
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "download") {
|
|
|
|
|
std::string id = id_of();
|
|
|
|
|
std::string rev = text(b, "revision");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
return Plan{req, "/v1/skills/{id}/download", false, [api, id, rev] {
|
|
|
|
|
pp::SkillDownload d = api->download(id, rev);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("revision", Json(d.revision));
|
|
|
|
|
o.set("download_url", Json(d.download_url));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "delete") {
|
|
|
|
|
std::string id = id_of();
|
|
|
|
|
std::string rev = text(b, "expected_revision", true);
|
|
|
|
|
Rules_opaque(rev);
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("id", Json(id));
|
|
|
|
|
req.set("expected_revision", Json(rev));
|
|
|
|
|
return Plan{req, "DELETE /v1/skills/{id}", true, [api, id, rev] {
|
|
|
|
|
api->remove(id, rev);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("deleted", Json(true));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "create" || action == "update") {
|
|
|
|
|
pp::SkillBundle bundle;
|
|
|
|
|
if (!security::crypto::Base64::decode(text(b, "data_base64", true), bundle.data)) bad("data_base64 is not valid base64");
|
|
|
|
|
std::string fname = text(b, "filename");
|
|
|
|
|
if (!fname.empty()) bundle.filename = fname;
|
|
|
|
|
bundle.validate();
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("bytes", Json(static_cast<int>(bundle.data.size())));
|
|
|
|
|
if (action == "create")
|
|
|
|
|
return Plan{req, "POST /v1/skills", true, [api, bundle, skill_json] { return skill_json(api->create(bundle)); }};
|
|
|
|
|
std::string id = id_of();
|
|
|
|
|
std::string rev = text(b, "expected_revision", true);
|
|
|
|
|
Rules_opaque(rev);
|
|
|
|
|
return Plan{req, "PUT /v1/skills/{id}", true, [api, id, rev, bundle, skill_json] { return skill_json(api->update(id, rev, bundle)); }};
|
|
|
|
|
}
|
|
|
|
|
bad("action must be list, get, revisions, download, delete, create or update");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static void Rules_opaque(const std::string& v) { pp::Rules::opaque_token(v, "expected_revision"); }
|
|
|
|
|
|
|
|
|
|
[[noreturn]] static void map(const llm::ApiError& e) {
|
|
|
|
|
if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED")
|
|
|
|
|
throw fw::HttpError(503, "perplexity_not_configured", e.message());
|
|
|
|
|
if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400 && e.request_id().empty())
|
|
|
|
|
throw fw::HttpError(422, "validation_error", e.message());
|
|
|
|
|
throw fw::HttpError(502, "perplexity_error", pp::Errors::explain(e) + " | " + e.type() + ": " + e.message());
|
|
|
|
|
}
|
|
|
|
|
pp::Platform p_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2026-10-25 09:14:26 +01:00
|
|
|
void register_ai_perplexity_demo(fw::Router& router, Services& services) {
|
2026-10-25 15:01:55 +01:00
|
|
|
auto routes = std::make_shared<PerplexityRoutes>(pp::Platform::from_env());
|
|
|
|
|
fw::Environment* env = services.templates.get();
|
|
|
|
|
services.templates->add_template("ai_perplexity.html", kPage);
|
|
|
|
|
services.add_feature("ai-perplexity", "/ai/perplexity");
|
|
|
|
|
|
|
|
|
|
router.get("/ai/perplexity", "ai-perplexity-page", [env](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
fw::TemplateCtx ctx;
|
|
|
|
|
ctx["title"] = std::string("Perplexity API");
|
|
|
|
|
return fw::Response(200, env->render("ai_perplexity.html", ctx), "text/html; charset=utf-8");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/ai/perplexity/status", "ai-perplexity-status", [routes](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("configured", Json(routes->platform().configured()));
|
|
|
|
|
o.set("credential_env", Json(pp::Platform::kKeyEnv));
|
|
|
|
|
o.set("base_url", Json(pp::Platform::kHost));
|
|
|
|
|
o.set("key_page", Json(pp::Platform::kKeyPage));
|
|
|
|
|
o.set("calls", Json(static_cast<int>(pp::Calls::count())));
|
|
|
|
|
Json models = Json::array();
|
|
|
|
|
for (std::size_t i = 0; i < pp::SonarModels::count(); ++i) {
|
|
|
|
|
Json m = Json::object();
|
|
|
|
|
m.set("id", Json(pp::SonarModels::at(i).id));
|
|
|
|
|
m.set("note", Json(pp::SonarModels::at(i).note));
|
|
|
|
|
models.push(m);
|
|
|
|
|
}
|
|
|
|
|
o.set("sonar_models", models);
|
|
|
|
|
o.set("search_max_results", Json(pp::PerplexityCaps::kSearchMaxResults));
|
|
|
|
|
o.set("embed_max_texts", Json(static_cast<int>(pp::PerplexityCaps::kEmbedTexts)));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/ai/perplexity/calls", "ai-perplexity-calls", [](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (std::size_t i = 0; i < pp::Calls::count(); ++i) {
|
|
|
|
|
const pp::CallSpec& s = pp::Calls::at(i);
|
|
|
|
|
Json row = Json::object();
|
|
|
|
|
row.set("http", Json(s.http_method));
|
|
|
|
|
row.set("path", Json(s.path));
|
|
|
|
|
row.set("operation", Json(s.operation_id));
|
|
|
|
|
row.set("spec", Json(s.spec));
|
|
|
|
|
arr.push(row);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("calls", arr);
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/ai/perplexity/errors", "ai-perplexity-errors", [](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (std::size_t i = 0; i < pp::Errors::count(); ++i) {
|
|
|
|
|
const pp::ErrorInfo& e = pp::Errors::at(i);
|
|
|
|
|
Json row = Json::object();
|
|
|
|
|
row.set("status", Json(e.status));
|
|
|
|
|
row.set("name", Json(e.name));
|
|
|
|
|
row.set("action", Json(e.action));
|
|
|
|
|
row.set("retryable", Json(e.retryable));
|
|
|
|
|
arr.push(row);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("errors", arr);
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
using Member = PerplexityRoutes::Planner;
|
|
|
|
|
auto add = [&](const char* path, const char* name, Member planner) {
|
|
|
|
|
router.post(path, name, [routes, planner](fw::Request req) { return routes->run(req, planner); });
|
|
|
|
|
};
|
|
|
|
|
PerplexityRoutes* r = routes.get();
|
|
|
|
|
auto bind = [r](Plan (PerplexityRoutes::*m)(const Json&) const) {
|
|
|
|
|
return Member([r, m](const Json& b) { return (r->*m)(b); });
|
|
|
|
|
};
|
|
|
|
|
add("/ai/perplexity/sonar", "ai-perplexity-sonar", bind(&PerplexityRoutes::sonar));
|
|
|
|
|
add("/ai/perplexity/stream", "ai-perplexity-stream", bind(&PerplexityRoutes::stream));
|
|
|
|
|
add("/ai/perplexity/async", "ai-perplexity-async", bind(&PerplexityRoutes::async));
|
|
|
|
|
add("/ai/perplexity/search", "ai-perplexity-search", bind(&PerplexityRoutes::search));
|
|
|
|
|
add("/ai/perplexity/embeddings", "ai-perplexity-embeddings", bind(&PerplexityRoutes::embeddings));
|
|
|
|
|
add("/ai/perplexity/agent", "ai-perplexity-agent", bind(&PerplexityRoutes::agent));
|
|
|
|
|
add("/ai/perplexity/orchestrator", "ai-perplexity-orchestrator", bind(&PerplexityRoutes::orchestrator));
|
|
|
|
|
add("/ai/perplexity/decisions", "ai-perplexity-decisions", bind(&PerplexityRoutes::decisions));
|
|
|
|
|
add("/ai/perplexity/models", "ai-perplexity-models", bind(&PerplexityRoutes::models));
|
|
|
|
|
add("/ai/perplexity/usage", "ai-perplexity-usage", bind(&PerplexityRoutes::usage));
|
|
|
|
|
add("/ai/perplexity/batches", "ai-perplexity-batches", bind(&PerplexityRoutes::batches));
|
|
|
|
|
add("/ai/perplexity/skills", "ai-perplexity-skills", bind(&PerplexityRoutes::skills));
|
2026-10-25 09:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace molo::example::app
|