754 lines
32 KiB
C++
754 lines
32 KiB
C++
#include "../AiDemos.hpp"
|
|
|
|
#include <chrono>
|
|
#include <ctime>
|
|
|
|
#include "../../fw/Router.hpp"
|
|
#include "../../fw/Templates.hpp"
|
|
#include "llm/api_client.h"
|
|
#include "llm/config.h"
|
|
#include "security/crypto/Base64.h"
|
|
#include "xai/platform/account.h"
|
|
#include "xai/platform/api_keys.h"
|
|
#include "xai/platform/audit.h"
|
|
#include "xai/platform/billing.h"
|
|
#include "xai/platform/calls.h"
|
|
#include "xai/platform/clients.h"
|
|
#include "xai/platform/collections.h"
|
|
#include "xai/platform/documents_search.h"
|
|
#include "xai/platform/files.h"
|
|
#include "xai/platform/realtime_rest.h"
|
|
#include "xai/platform/realtime_session.h"
|
|
#include "xai/platform/stt.h"
|
|
#include "xai/platform/tts.h"
|
|
#include "xai/platform/voices.h"
|
|
#include "xai/platform/webhook.h"
|
|
|
|
namespace molo::example::app {
|
|
|
|
// xAI platform: voice (realtime agent, TTS, STT), collections, files, the
|
|
// management API (keys, billing, usage, audit) and key info, over the real
|
|
// src/xai/platform part. GET routes need no credential. Every route that
|
|
// calls xAI is a POST: 503 not_configured without the key it needs
|
|
// (XAI_API_KEY or XAI_MANAGEMENT_KEY), 422 on bad input. Destructive or
|
|
// paid calls are dry runs unless the body says "confirm":true.
|
|
|
|
namespace {
|
|
|
|
namespace xp = xai::platform;
|
|
using Json = clients::body::Json;
|
|
|
|
constexpr std::size_t kMaxBody = 4u * 1024u * 1024u;
|
|
|
|
const char kPage[] = R"XAIP({% extends "base.html" %}
|
|
{% block content %}
|
|
<h1>xAI Platform</h1>
|
|
<p class="hint">Voice, collections, files and the management API. Destructive and
|
|
paid calls are dry runs until the body says <code>"confirm":true</code>.</p>
|
|
<p id="xp-config" class="hint">Checking xAI keys...</p>
|
|
<section class="xp">
|
|
<label>Route
|
|
<select id="xp-route">
|
|
<option value="account">Account: API key info, me</option>
|
|
<option value="files">Files</option>
|
|
<option value="collections">Collections and documents</option>
|
|
<option value="search">Document search</option>
|
|
<option value="voices">Voices (built-in and custom)</option>
|
|
<option value="tts">Text to speech (paid)</option>
|
|
<option value="stt">Speech to text (paid)</option>
|
|
<option value="realtime/secret">Realtime: ephemeral client secret</option>
|
|
<option value="realtime/session">Realtime: one shot text session (paid)</option>
|
|
<option value="realtime/session/preview">Realtime: session.update preview (local)</option>
|
|
<option value="realtime/phone-numbers">Realtime: SIP phone numbers</option>
|
|
<option value="realtime/calls">Realtime: call control</option>
|
|
<option value="webhook/verify">SIP webhook verification (local)</option>
|
|
<option value="keys">Management: API keys</option>
|
|
<option value="billing">Management: billing and usage</option>
|
|
<option value="audit">Management: audit events</option>
|
|
</select></label>
|
|
<label>Body (JSON object) <textarea id="xp-body" rows="8"></textarea></label>
|
|
<div class="xp-actions"><button id="xp-send" type="button" class="primary">Send</button></div>
|
|
<p id="xp-error" class="error" role="alert"></p>
|
|
<pre id="xp-out" class="xp-out"></pre>
|
|
</section>
|
|
<style>
|
|
.xp label{display:block;margin:.5rem 0}
|
|
.xp select,.xp textarea{width:100%;box-sizing:border-box}
|
|
.xp-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
|
.xp-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
|
</style>
|
|
<script src="/embedded/ai_xai_platform.js"></script>
|
|
{% endblock %}
|
|
)XAIP";
|
|
|
|
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;
|
|
}
|
|
|
|
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
|
|
|
// Maps service errors onto the demo's status contract.
|
|
template <typename F>
|
|
auto guarded(F&& f) -> decltype(f()) {
|
|
try {
|
|
return f();
|
|
} catch (const llm::NotConfiguredError& e) {
|
|
throw fw::HttpError(503, "not_configured", e.message());
|
|
} catch (const llm::ApiError& e) {
|
|
if (e.http_status() == 400 && e.type() == "INVALID_ARGUMENT") bad(e.message());
|
|
throw fw::HttpError(502, "xai_error",
|
|
"HTTP " + std::to_string(e.http_status()) + " " + e.type() + ": " + e.message());
|
|
} catch (const clients::body::JsonError&) {
|
|
bad("body is not valid JSON");
|
|
}
|
|
}
|
|
|
|
// One request body with typed, validated accessors (422 on a wrong type).
|
|
class Body {
|
|
public:
|
|
explicit Body(const fw::Request& req) {
|
|
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
if (req.body.empty()) {
|
|
doc_ = Json::object();
|
|
return;
|
|
}
|
|
try {
|
|
doc_ = Json::parse(req.body);
|
|
} catch (const clients::body::JsonError&) {
|
|
bad("body is not valid JSON");
|
|
}
|
|
if (!doc_.is_object()) bad("body must be a JSON object");
|
|
}
|
|
|
|
std::string text(const char* key, bool required = false) const {
|
|
const Json* v = doc_.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();
|
|
}
|
|
long number(const char* key, long fallback = 0) const {
|
|
const Json* v = doc_.find(key);
|
|
if (!v || v->is_null()) return fallback;
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
return v->as_int();
|
|
}
|
|
double real(const char* key, double fallback = 0) const {
|
|
const Json* v = doc_.find(key);
|
|
if (!v || v->is_null()) return fallback;
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
return v->as_number();
|
|
}
|
|
bool flag(const char* key) const {
|
|
const Json* v = doc_.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();
|
|
}
|
|
Json object(const char* key) const {
|
|
const Json* v = doc_.find(key);
|
|
if (!v || v->is_null()) return Json();
|
|
if (!v->is_object()) bad(std::string(key) + " must be an object");
|
|
return *v;
|
|
}
|
|
Json array(const char* key) const {
|
|
const Json* v = doc_.find(key);
|
|
if (!v || v->is_null()) return Json();
|
|
if (!v->is_array()) bad(std::string(key) + " must be an array");
|
|
return *v;
|
|
}
|
|
std::vector<std::string> strings(const char* key) const {
|
|
std::vector<std::string> out;
|
|
Json a = array(key);
|
|
if (a.is_null()) return out;
|
|
for (const Json& s : a.as_array()) {
|
|
if (!s.is_string()) bad(std::string(key) + " must hold strings");
|
|
out.push_back(s.as_string());
|
|
}
|
|
return out;
|
|
}
|
|
// Base64 payload -> bytes.
|
|
std::string bytes(const char* key) const {
|
|
std::string b64 = text(key), raw;
|
|
if (b64.empty()) return raw;
|
|
if (!security::crypto::Base64::decode(b64, raw)) bad(std::string(key) + " is not valid base64");
|
|
return raw;
|
|
}
|
|
bool confirm() const { return flag("confirm"); }
|
|
const Json& doc() const { return doc_; }
|
|
|
|
private:
|
|
Json doc_ = Json::object();
|
|
};
|
|
|
|
Json dry_run(xp::Call call, std::initializer_list<std::string> segments) {
|
|
Json o = Json::object();
|
|
o.set("dry_run", Json(true));
|
|
o.set("http", Json(xp::spec_of(call).http_method));
|
|
o.set("path", Json(xp::render(call, segments)));
|
|
o.set("host", Json(xp::spec_of(call).host));
|
|
o.set("hint", Json("send \"confirm\":true to run it"));
|
|
return o;
|
|
}
|
|
|
|
std::string b64(const std::string& raw) { return security::crypto::Base64::encode(raw); }
|
|
|
|
// All handlers share the clients built from the environment at request time.
|
|
class XaiPlatformRoutes {
|
|
public:
|
|
Json account(const Body& b) const {
|
|
xp::AccountService svc(clients().api());
|
|
std::string a = b.text("action", true);
|
|
if (a == "api_key") return guarded([&] { return svc.api_key_info(); });
|
|
if (a == "me") return guarded([&] { return svc.me(); });
|
|
bad("action must be api_key or me");
|
|
}
|
|
|
|
Json files(const Body& b) const {
|
|
xp::FilesService svc(clients().api());
|
|
std::string a = b.text("action", true);
|
|
std::string id = a == "list" || a == "upload" ? "" : b.text("id", true);
|
|
if (a == "list") {
|
|
xp::FileListOptions o;
|
|
o.limit = static_cast<int>(b.number("limit"));
|
|
o.order = b.text("order");
|
|
o.sort_by = b.text("sort_by");
|
|
o.filter = b.text("filter");
|
|
o.pagination_token = b.text("pagination_token");
|
|
return guarded([&] { return svc.list(o); });
|
|
}
|
|
if (a == "list_all") {
|
|
return guarded([&] {
|
|
xp::FileListing l = svc.list_all(static_cast<std::size_t>(b.number("max_pages", 5)), b.text("filter"));
|
|
Json o = Json::object();
|
|
o.set("files", l.files);
|
|
o.set("truncated", Json(l.truncated));
|
|
o.set("pages", Json(static_cast<int>(l.pages)));
|
|
return o;
|
|
});
|
|
}
|
|
if (a == "upload") {
|
|
std::string name = b.text("filename", true);
|
|
std::string data = b.bytes("data_base64");
|
|
return guarded([&] {
|
|
return svc.upload(name, b.text("content_type") .empty() ? "application/octet-stream" : b.text("content_type"),
|
|
data, b.number("expires_after"), b.text("purpose"));
|
|
});
|
|
}
|
|
if (a == "get") return guarded([&] { return svc.get(id); });
|
|
if (a == "content") {
|
|
return guarded([&] {
|
|
llm::RawResponse r = svc.content(id, b.text("format"));
|
|
Json o = Json::object();
|
|
o.set("content_type", Json(r.content_type));
|
|
o.set("bytes", Json(static_cast<int>(r.body.size())));
|
|
// Text goes through; binary comes back base64 (bounded by the body cap).
|
|
o.set(b.text("format") == "text" ? "text" : "data_base64",
|
|
Json(b.text("format") == "text" ? r.body : b64(r.body)));
|
|
return o;
|
|
});
|
|
}
|
|
if (a == "public_url") return guarded([&] { return svc.create_public_url(id, b.number("expires_after")); });
|
|
if (a == "revoke_url") {
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("file", id); return dry_run(xp::Call::kPublicUrlRevoke, {id}); });
|
|
return guarded([&] { return svc.revoke_public_url(id, true); });
|
|
}
|
|
if (a == "delete") {
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("file", id); return dry_run(xp::Call::kFileDelete, {id}); });
|
|
return guarded([&] { return svc.remove(id, true); });
|
|
}
|
|
bad("action must be list, list_all, upload, get, content, public_url, revoke_url or delete");
|
|
}
|
|
|
|
Json collections(const Body& b) const {
|
|
xp::CollectionsService svc(clients().management());
|
|
std::string a = b.text("action", true);
|
|
auto cid = [&] { return b.text("collection_id", true); };
|
|
auto fid = [&] { return b.text("file_id", true); };
|
|
xp::ListOptions lo;
|
|
lo.team_id = b.text("team_id");
|
|
lo.limit = static_cast<int>(b.number("limit"));
|
|
lo.order = b.text("order");
|
|
lo.sort_by = b.text("sort_by");
|
|
lo.filter = b.text("filter");
|
|
lo.name = b.text("name");
|
|
lo.pagination_token = b.text("pagination_token");
|
|
if (a == "list") return guarded([&] { return svc.list(lo); });
|
|
if (a == "create" || a == "update") {
|
|
xp::CollectionSpec s;
|
|
s.name = b.text("collection_name");
|
|
s.description = b.text("collection_description");
|
|
s.embedding_model = b.text("embedding_model");
|
|
s.metric_space = b.text("metric_space");
|
|
s.chunk_configuration = b.object("chunk_configuration");
|
|
s.field_definitions = b.array("field_definitions");
|
|
s.team_id = lo.team_id;
|
|
if (a == "create") return guarded([&] { return svc.create(s); });
|
|
std::string id = cid();
|
|
return guarded([&] { return svc.update(id, s, b.array("field_definition_updates")); });
|
|
}
|
|
if (a == "get") { std::string id = cid(); return guarded([&] { return svc.get(id, lo.team_id); }); }
|
|
if (a == "delete") {
|
|
std::string id = cid();
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("collection", id); return dry_run(xp::Call::kCollectionDelete, {id}); });
|
|
return guarded([&] { return svc.remove(id, true, lo.team_id); });
|
|
}
|
|
if (a == "add_document") { std::string c = cid(), f = fid(); return guarded([&] { return svc.add_document(c, f, b.object("fields"), lo.team_id); }); }
|
|
if (a == "upload_document") {
|
|
std::string c = cid(), n = b.text("name", true), data = b.bytes("data_base64");
|
|
return guarded([&] {
|
|
return svc.upload_document(c, n, b.text("content_type").empty() ? "application/octet-stream" : b.text("content_type"),
|
|
data, b.object("fields"));
|
|
});
|
|
}
|
|
if (a == "list_documents") { std::string c = cid(); return guarded([&] { return svc.list_documents(c, lo); }); }
|
|
if (a == "get_document") { std::string c = cid(), f = fid(); return guarded([&] { return svc.get_document(c, f, lo.team_id); }); }
|
|
if (a == "reindex_document") { std::string c = cid(), f = fid(); return guarded([&] { return svc.reindex_document(c, f, lo.team_id); }); }
|
|
if (a == "remove_document") {
|
|
std::string c = cid(), f = fid();
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("collection", c); svc.check_id("file", f); return dry_run(xp::Call::kDocRemove, {c, f}); });
|
|
return guarded([&] { return svc.remove_document(c, f, true, lo.team_id); });
|
|
}
|
|
if (a == "batch_get") { std::string c = cid(); return guarded([&] { return svc.batch_get(c, b.strings("file_ids"), lo.team_id); }); }
|
|
bad("unknown collections action");
|
|
}
|
|
|
|
Json search(const Body& b) const {
|
|
xp::DocumentSearch svc(clients().api());
|
|
xp::DocumentSearchRequest q;
|
|
q.query = b.text("query", true);
|
|
q.collection_ids = b.strings("collection_ids");
|
|
q.rag_pipeline = b.text("rag_pipeline");
|
|
q.filter = b.text("filter");
|
|
q.instructions = b.text("instructions");
|
|
q.limit = static_cast<int>(b.number("limit"));
|
|
q.ranking_metric = b.text("ranking_metric");
|
|
q.retrieval_mode = b.text("retrieval_mode");
|
|
q.retrieval_options = b.object("retrieval_options");
|
|
q.group_by = b.object("group_by");
|
|
return guarded([&] { return svc.search(q); });
|
|
}
|
|
|
|
Json voices(const Body& b) const {
|
|
xp::VoicesService svc(clients().api());
|
|
std::string a = b.text("action", true);
|
|
std::string id = a == "list" || a == "custom_list" ? "" : b.text("voice_id", true);
|
|
if (a == "list") return guarded([&] { return svc.tts_voices(); });
|
|
if (a == "get") return guarded([&] { return svc.tts_voice(id); });
|
|
if (a == "custom_list") return guarded([&] { return svc.list_custom(static_cast<int>(b.number("limit")), b.text("pagination_token")); });
|
|
if (a == "custom_get") return guarded([&] { return svc.get_custom(id); });
|
|
if (a == "custom_patch") return guarded([&] { return svc.patch_custom(id, b.object("changes")); });
|
|
if (a == "custom_delete") {
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("voice", id); return dry_run(xp::Call::kCustomVoiceDelete, {id}); });
|
|
return guarded([&] { return svc.remove_custom(id, true); });
|
|
}
|
|
bad("action must be list, get, custom_list, custom_get, custom_patch or custom_delete");
|
|
}
|
|
|
|
Json tts(const Body& b) const {
|
|
xp::TextToSpeech svc(clients().api());
|
|
xp::TtsRequest q;
|
|
q.text = b.text("text", true);
|
|
q.language = b.text("language").empty() ? "en" : b.text("language");
|
|
q.voice_id = b.text("voice_id");
|
|
q.codec = b.text("codec");
|
|
q.sample_rate = static_cast<int>(b.number("sample_rate"));
|
|
q.bit_rate = static_cast<int>(b.number("bit_rate"));
|
|
q.optimize_streaming_latency = static_cast<int>(b.number("optimize_streaming_latency"));
|
|
q.speed = b.real("speed");
|
|
q.text_normalization = b.flag("text_normalization");
|
|
q.with_timestamps = b.flag("with_timestamps");
|
|
if (b.object("replace").is_object())
|
|
for (const auto& [k, v] : b.object("replace").as_object()) {
|
|
if (!v.is_string()) bad("replace values must be strings");
|
|
q.replace[k] = v.as_string();
|
|
}
|
|
Json body = guarded([&] { return svc.build(q); });
|
|
if (!b.confirm()) {
|
|
Json o = dry_run(xp::Call::kTts, {});
|
|
o.set("request", body);
|
|
return o;
|
|
}
|
|
return guarded([&] {
|
|
xp::TtsResult r = svc.synthesize(q);
|
|
Json o = Json::object();
|
|
o.set("content_type", Json(r.content_type));
|
|
o.set("bytes", Json(static_cast<int>(r.audio.size())));
|
|
o.set("audio_base64", Json(b64(r.audio)));
|
|
if (q.with_timestamps) {
|
|
o.set("duration", Json(r.duration));
|
|
o.set("audio_timestamps", r.timestamps);
|
|
}
|
|
return o;
|
|
});
|
|
}
|
|
|
|
Json stt(const Body& b) const {
|
|
xp::SpeechToText svc(clients().api());
|
|
xp::SttRequest q;
|
|
q.url = b.text("url");
|
|
q.data = b.bytes("data_base64");
|
|
q.filename = b.text("filename").empty() ? "audio" : b.text("filename");
|
|
q.content_type = b.text("content_type").empty() ? "application/octet-stream" : b.text("content_type");
|
|
q.model = b.text("model");
|
|
q.audio_format = b.text("audio_format");
|
|
q.sample_rate = static_cast<int>(b.number("sample_rate"));
|
|
q.language = b.text("language");
|
|
q.format = b.flag("format");
|
|
q.multichannel = b.flag("multichannel");
|
|
q.channels = static_cast<int>(b.number("channels"));
|
|
q.diarize = b.flag("diarize");
|
|
q.keyterms = b.strings("keyterms");
|
|
q.filler_words = b.flag("filler_words");
|
|
q.vad_threshold = b.real("vad_threshold", -1);
|
|
guarded([&] { return svc.build(q).encoded_size(); }); // validate before anything else
|
|
if (!b.confirm()) return dry_run(xp::Call::kStt, {});
|
|
return guarded([&] { return svc.transcribe(q); });
|
|
}
|
|
|
|
Json client_secret(const Body& b) const {
|
|
xp::RealtimeRest svc(clients().api());
|
|
xp::ClientSecretRequest q;
|
|
q.expires_seconds = b.number("expires_seconds");
|
|
q.model = b.text("model");
|
|
q.reasoning_effort = b.text("reasoning_effort");
|
|
Json body = guarded([&] { return svc.build_client_secret(q); });
|
|
if (!b.confirm()) {
|
|
Json o = dry_run(xp::Call::kClientSecret, {});
|
|
o.set("request", body);
|
|
return o;
|
|
}
|
|
return guarded([&] { return svc.client_secret(q); });
|
|
}
|
|
|
|
static xp::SessionConfig session_config(const Body& b) {
|
|
xp::SessionConfig c;
|
|
c.model = b.text("model");
|
|
c.instructions = b.text("instructions");
|
|
c.reasoning_effort = b.text("reasoning_effort");
|
|
c.voice = b.text("voice");
|
|
std::string turns = b.text("turn_detection");
|
|
if (turns == "server_vad") c.turns = xp::SessionConfig::Turns::kServerVad;
|
|
else if (turns == "manual") c.turns = xp::SessionConfig::Turns::kManual;
|
|
else if (!turns.empty()) bad("turn_detection must be server_vad or manual");
|
|
c.idle_timeout_ms = b.number("idle_timeout_ms");
|
|
c.input.type = b.text("input_type");
|
|
c.input.rate = static_cast<int>(b.number("input_rate"));
|
|
c.output.type = b.text("output_type");
|
|
c.output.rate = static_cast<int>(b.number("output_rate"));
|
|
c.language_hint = b.text("language_hint");
|
|
c.keyterms = b.strings("keyterms");
|
|
c.output_speed = b.real("output_speed");
|
|
Json tools = b.array("tools");
|
|
if (tools.is_array()) for (const Json& t : tools.as_array()) c.tools.push_back(t);
|
|
if (b.object("replace").is_object())
|
|
for (const auto& [k, v] : b.object("replace").as_object()) {
|
|
if (!v.is_string()) bad("replace values must be strings");
|
|
c.replace[k] = v.as_string();
|
|
}
|
|
return c;
|
|
}
|
|
|
|
Json session_preview(const Body& b) const {
|
|
xp::SessionConfig c = session_config(b);
|
|
return guarded([&] { return c.update_message(); });
|
|
}
|
|
|
|
// One shot: connect, configure, one user text turn, fold the response.
|
|
Json session(const Body& b) const {
|
|
xp::SessionConfig c = session_config(b);
|
|
std::string text = b.text("text", true);
|
|
Json update = guarded([&] { return c.update_message(); });
|
|
if (text.size() > xp::PlatformCaps::kMaxItemTextChars) bad("text too long");
|
|
if (!b.confirm()) {
|
|
Json o = Json::object();
|
|
o.set("dry_run", Json(true));
|
|
o.set("http", Json("WEBSOCKET"));
|
|
o.set("path", Json(xp::PlatformEndpoints::kRealtimeUrl));
|
|
o.set("session_update", update);
|
|
o.set("hint", Json("send \"confirm\":true to run it"));
|
|
return o;
|
|
}
|
|
return guarded([&] {
|
|
std::string key = llm::Config::get(xp::PlatformEndpoints::kApiKeyEnv);
|
|
if (key.empty()) throw llm::NotConfiguredError("xai", xp::PlatformEndpoints::kApiKeyEnv);
|
|
xp::RealtimeOptions o;
|
|
o.model = c.model;
|
|
xp::RealtimeSession s(key, o);
|
|
s.connect();
|
|
s.update_session(c);
|
|
s.add_user_text(text);
|
|
s.create_response();
|
|
xp::RealtimeResponse r = s.run_response(static_cast<int>(b.number("max_wait_ms", 30000)));
|
|
s.close();
|
|
Json out = Json::object();
|
|
out.set("status", Json(r.status));
|
|
out.set("transcript", Json(r.transcript));
|
|
out.set("text", Json(r.text));
|
|
out.set("audio_bytes", Json(static_cast<int>(r.audio.size())));
|
|
out.set("usage", r.usage);
|
|
out.set("events", Json(static_cast<int>(r.events.size())));
|
|
Json calls = Json::array();
|
|
for (const xp::FunctionCall& f : r.calls) {
|
|
Json j = Json::object();
|
|
j.set("call_id", Json(f.call_id));
|
|
j.set("name", Json(f.name));
|
|
j.set("arguments", Json(f.arguments));
|
|
calls.push(j);
|
|
}
|
|
out.set("function_calls", calls);
|
|
Json errs = Json::array();
|
|
for (const std::string& e : r.errors) errs.push(Json(e));
|
|
out.set("errors", errs);
|
|
out.set("truncated", Json(r.truncated));
|
|
return out;
|
|
});
|
|
}
|
|
|
|
Json phone_numbers(const Body& b) const {
|
|
xp::RealtimeRest svc(clients().api());
|
|
std::string a = b.text("action", true);
|
|
if (a == "list") return guarded([&] { return svc.list_phone_numbers(static_cast<int>(b.number("limit")), b.text("pagination_token"), b.text("agent_id")); });
|
|
if (a == "create") {
|
|
xp::PhoneNumberSpec s;
|
|
if (!b.text("origin").empty()) s.origin = b.text("origin");
|
|
s.name = b.text("name");
|
|
s.agent_id = b.text("agent_id");
|
|
s.area_code = b.text("area_code");
|
|
s.phone_number = b.text("phone_number");
|
|
s.sip_username = b.text("sip_username");
|
|
s.sip_password = b.text("sip_password");
|
|
s.allowed_addresses = b.strings("allowed_addresses");
|
|
s.webhook_url = b.text("webhook_url");
|
|
s.webhook_name = b.text("webhook_name");
|
|
Json body = guarded([&] { return svc.build_phone_number(s); });
|
|
if (!b.confirm()) {
|
|
Json o = dry_run(xp::Call::kPhoneCreate, {});
|
|
o.set("request", body);
|
|
return o;
|
|
}
|
|
return guarded([&] { return svc.create_phone_number(s); });
|
|
}
|
|
if (a == "delete") {
|
|
std::string id = b.text("phone_number_id", true);
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("phone number", id); return dry_run(xp::Call::kPhoneDelete, {id}); });
|
|
return guarded([&] { return svc.remove_phone_number(id, true); });
|
|
}
|
|
bad("action must be list, create or delete");
|
|
}
|
|
|
|
Json calls(const Body& b) const {
|
|
xp::RealtimeRest svc(clients().api());
|
|
std::string a = b.text("action", true), id = b.text("call_id", true);
|
|
if (a != "refer" && a != "hangup") bad("action must be refer or hangup");
|
|
std::string target = a == "refer" ? b.text("target_uri", true) : "";
|
|
if (!b.confirm()) {
|
|
return guarded([&] {
|
|
svc.check_id("call", id);
|
|
return dry_run(a == "refer" ? xp::Call::kCallRefer : xp::Call::kCallHangup, {id});
|
|
});
|
|
}
|
|
return guarded([&] { return a == "refer" ? svc.refer_call(id, target, true) : svc.hangup_call(id, true); });
|
|
}
|
|
|
|
// Local: checks a signed realtime.call.incoming webhook with the secret.
|
|
Json webhook_verify(const Body& b) const {
|
|
xp::WebhookHeaders h;
|
|
h.id = b.text("webhook_id", true);
|
|
h.timestamp = b.text("webhook_timestamp", true);
|
|
h.signature = b.text("webhook_signature", true);
|
|
std::string body = b.text("body", true);
|
|
long now = b.number("now", static_cast<long>(std::time(nullptr)));
|
|
return guarded([&] {
|
|
xp::WebhookVerifier v(b.text("secret", true));
|
|
xp::IncomingCall c = v.parse_incoming(h, body, now);
|
|
Json o = Json::object();
|
|
o.set("valid", Json(true));
|
|
o.set("call_id", Json(c.call_id));
|
|
o.set("join", Json(std::string(xp::PlatformEndpoints::kRealtimeUrl) + "?call_id=" + c.call_id));
|
|
return o;
|
|
});
|
|
}
|
|
|
|
Json keys(const Body& b) const {
|
|
xp::ApiKeysService svc(clients().management());
|
|
std::string a = b.text("action", true);
|
|
std::string team = b.text("team_id");
|
|
std::string kid = b.text("api_key_id");
|
|
if (a == "validate") return guarded([&] { return svc.validate_management_key(); });
|
|
if (a == "list") {
|
|
return guarded([&] {
|
|
return svc.list(team, static_cast<int>(b.number("page_size")), b.text("pagination_token"),
|
|
b.flag("active_only"), b.strings("acl_filters"));
|
|
});
|
|
}
|
|
if (a == "list_all") {
|
|
return guarded([&] {
|
|
xp::KeyListing l = svc.list_all(team, static_cast<std::size_t>(b.number("max_pages", 5)),
|
|
static_cast<int>(b.number("page_size", 50)));
|
|
Json o = Json::object();
|
|
o.set("keys", l.keys);
|
|
o.set("truncated", Json(l.truncated));
|
|
o.set("pages", Json(static_cast<int>(l.pages)));
|
|
return o;
|
|
});
|
|
}
|
|
if (a == "models") return guarded([&] { return svc.team_models(team); });
|
|
if (a == "endpoints") return guarded([&] { return svc.team_endpoints(team); });
|
|
if (a == "propagation") return guarded([&] { return svc.propagation(kid); });
|
|
if (a == "create") {
|
|
xp::ApiKeySpec s;
|
|
s.name = b.text("name");
|
|
s.acls = b.strings("acls");
|
|
s.qps = b.number("qps");
|
|
s.qpm = b.number("qpm");
|
|
s.tpm = b.number("tpm");
|
|
s.expire_time = b.text("expire_time");
|
|
return guarded([&] { return svc.create(team, s); });
|
|
}
|
|
if (a == "update") return guarded([&] { return svc.update(kid, b.object("changes")); });
|
|
if (a == "rotate") {
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("api key", kid); return dry_run(xp::Call::kKeyRotate, {kid}); });
|
|
return guarded([&] { return svc.rotate(kid, true, b.text("expire_time"), b.text("old_secret_expire_time")); });
|
|
}
|
|
if (a == "delete") {
|
|
if (!b.confirm()) return guarded([&] { svc.check_id("api key", kid); return dry_run(xp::Call::kKeyDelete, {kid}); });
|
|
return guarded([&] { return svc.remove(kid, true); });
|
|
}
|
|
bad("unknown keys action");
|
|
}
|
|
|
|
Json billing(const Body& b) const {
|
|
xp::BillingService svc(clients().management());
|
|
std::string a = b.text("action", true), team = b.text("team_id");
|
|
if (a == "info") return guarded([&] { return svc.billing_info(team); });
|
|
if (a == "payment_methods") return guarded([&] { return svc.payment_methods(team); });
|
|
if (a == "invoice_preview") return guarded([&] { return svc.invoice_preview(team); });
|
|
if (a == "spending_limits") return guarded([&] { return svc.spending_limits(team); });
|
|
if (a == "prepaid_balance") return guarded([&] { return svc.prepaid_balance(team); });
|
|
if (a == "invoices") {
|
|
xp::InvoiceQuery q;
|
|
q.cycle_year = static_cast<int>(b.number("cycle_year"));
|
|
q.cycle_month = static_cast<int>(b.number("cycle_month"));
|
|
q.since_year = static_cast<int>(b.number("since_year"));
|
|
q.since_month = static_cast<int>(b.number("since_month"));
|
|
q.invoice_ids = b.strings("invoice_ids");
|
|
return guarded([&] { return svc.invoices(team, q); });
|
|
}
|
|
if (a == "usage") return guarded([&] { return svc.usage(team, b.object("analytics_request")); });
|
|
// Money and billing state: dry run unless confirmed.
|
|
struct Mut { const char* name; xp::Call call; };
|
|
for (const Mut& m : {Mut{"set_info", xp::Call::kBillingInfoSet}, Mut{"set_default_payment_method", xp::Call::kPaymentDefault},
|
|
Mut{"set_spending_limit", xp::Call::kSpendingSet}, Mut{"top_up", xp::Call::kPrepaidTopUp}}) {
|
|
if (a != m.name) continue;
|
|
if (!b.confirm()) return guarded([&] { svc.require_uuid("team_id", team); return dry_run(m.call, {team}); });
|
|
return guarded([&]() -> Json {
|
|
if (a == "set_info") return svc.set_billing_info(team, b.object("billing_info"), true);
|
|
if (a == "set_default_payment_method") return svc.set_default_payment_method(team, b.text("payment_method_id"), true);
|
|
if (a == "set_spending_limit") return svc.set_soft_spending_limit(team, b.text("amount"), true);
|
|
return svc.top_up(team, b.text("amount"), true);
|
|
});
|
|
}
|
|
bad("unknown billing action");
|
|
}
|
|
|
|
Json audit(const Body& b) const {
|
|
xp::AuditService svc(clients().management());
|
|
xp::AuditQuery q;
|
|
q.page_size = static_cast<int>(b.number("page_size"));
|
|
q.page_token = b.text("page_token");
|
|
q.user_id = b.text("user_id");
|
|
q.query = b.text("query");
|
|
q.event_id = b.text("event_id");
|
|
q.time_from = b.text("time_from");
|
|
q.time_to = b.text("time_to");
|
|
q.order_by = b.text("order_by");
|
|
std::string team = b.text("team_id");
|
|
return guarded([&] { return svc.events(team, q); });
|
|
}
|
|
|
|
private:
|
|
static xp::PlatformClients clients() { return xp::PlatformClients::from_env(); }
|
|
};
|
|
|
|
} // namespace
|
|
|
|
void register_ai_xaiplatform_demo(fw::Router& router, Services& services) {
|
|
fw::Environment* env = services.templates.get();
|
|
services.templates->add_template("ai_xai_platform.html", kPage);
|
|
services.add_feature("ai-xai-platform", "/ai/xai/platform");
|
|
|
|
auto routes = std::make_shared<XaiPlatformRoutes>();
|
|
|
|
router.get("/ai/xai/platform", "ai-xai-platform-page", [env](fw::Request req) {
|
|
(void)req;
|
|
fw::TemplateCtx ctx;
|
|
ctx["title"] = std::string("xAI Platform");
|
|
return fw::Response(200, env->render("ai_xai_platform.html", ctx), "text/html; charset=utf-8");
|
|
});
|
|
|
|
router.get("/ai/xai/platform/status", "ai-xai-platform-status", [](fw::Request req) {
|
|
(void)req;
|
|
Json o = Json::object();
|
|
o.set("api_key_configured", Json(!llm::Config::get(xp::PlatformEndpoints::kApiKeyEnv).empty()));
|
|
o.set("management_key_configured", Json(!llm::Config::get(xp::PlatformEndpoints::kManagementKeyEnv).empty()));
|
|
o.set("api_base", Json(xp::PlatformEndpoints::kApiBase));
|
|
o.set("management_base", Json(xp::PlatformEndpoints::kManagementBase));
|
|
o.set("realtime_url", Json(xp::PlatformEndpoints::kRealtimeUrl));
|
|
o.set("calls", Json(static_cast<int>(xp::call_count())));
|
|
o.set("max_body_bytes", Json(static_cast<int>(kMaxBody)));
|
|
return json_out(o);
|
|
});
|
|
|
|
router.get("/ai/xai/platform/calls", "ai-xai-platform-calls", [](fw::Request req) {
|
|
(void)req;
|
|
Json arr = Json::array();
|
|
for (std::size_t i = 0; i < xp::call_count(); ++i) {
|
|
const xp::CallSpec& s = xp::spec_at(i);
|
|
Json row = Json::object();
|
|
row.set("http", Json(s.http_method));
|
|
row.set("host", Json(s.host));
|
|
row.set("path", Json(s.path));
|
|
row.set("source", Json(s.source));
|
|
arr.push(row);
|
|
}
|
|
Json o = Json::object();
|
|
o.set("calls", arr);
|
|
return json_out(o);
|
|
});
|
|
|
|
using Handler = Json (XaiPlatformRoutes::*)(const Body&) const;
|
|
struct Route { const char* path; const char* name; Handler handler; };
|
|
const Route table[] = {
|
|
{"/ai/xai/platform/account", "ai-xai-platform-account", &XaiPlatformRoutes::account},
|
|
{"/ai/xai/platform/files", "ai-xai-platform-files", &XaiPlatformRoutes::files},
|
|
{"/ai/xai/platform/collections", "ai-xai-platform-collections", &XaiPlatformRoutes::collections},
|
|
{"/ai/xai/platform/search", "ai-xai-platform-search", &XaiPlatformRoutes::search},
|
|
{"/ai/xai/platform/voices", "ai-xai-platform-voices", &XaiPlatformRoutes::voices},
|
|
{"/ai/xai/platform/tts", "ai-xai-platform-tts", &XaiPlatformRoutes::tts},
|
|
{"/ai/xai/platform/stt", "ai-xai-platform-stt", &XaiPlatformRoutes::stt},
|
|
{"/ai/xai/platform/realtime/secret", "ai-xai-platform-secret", &XaiPlatformRoutes::client_secret},
|
|
{"/ai/xai/platform/realtime/session", "ai-xai-platform-session", &XaiPlatformRoutes::session},
|
|
{"/ai/xai/platform/realtime/session/preview", "ai-xai-platform-session-preview", &XaiPlatformRoutes::session_preview},
|
|
{"/ai/xai/platform/realtime/phone-numbers", "ai-xai-platform-phones", &XaiPlatformRoutes::phone_numbers},
|
|
{"/ai/xai/platform/realtime/calls", "ai-xai-platform-calls-control", &XaiPlatformRoutes::calls},
|
|
{"/ai/xai/platform/webhook/verify", "ai-xai-platform-webhook", &XaiPlatformRoutes::webhook_verify},
|
|
{"/ai/xai/platform/keys", "ai-xai-platform-keys", &XaiPlatformRoutes::keys},
|
|
{"/ai/xai/platform/billing", "ai-xai-platform-billing", &XaiPlatformRoutes::billing},
|
|
{"/ai/xai/platform/audit", "ai-xai-platform-audit", &XaiPlatformRoutes::audit},
|
|
};
|
|
for (const Route& r : table) {
|
|
Handler h = r.handler;
|
|
router.post(r.path, r.name, [routes, h](fw::Request req) {
|
|
Body body(req);
|
|
return json_out(((*routes).*h)(body));
|
|
});
|
|
}
|
|
}
|
|
|
|
} // namespace molo::example::app
|