Merge xai platform package
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
// Destructive and paid routes are dry runs unless the body says "confirm":true.
|
||||
// A dry run needs no key and sends nothing; confirm without a key is 503; a
|
||||
// non-boolean confirm is 422.
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("XAI_API_KEY");
|
||||
unsetenv("XAI_MANAGEMENT_KEY");
|
||||
Fixture f;
|
||||
const std::string team = "5ea6f6bd-7815-4b8a-9135-28b2d7ba6722";
|
||||
const struct {
|
||||
const char* path;
|
||||
std::string body; // without the closing brace; confirm is appended
|
||||
std::string expect_path;
|
||||
} cases[] = {
|
||||
{"/ai/xai/platform/files", "{\"action\":\"delete\",\"id\":\"file_abc\"", "/v1/files/file_abc"},
|
||||
{"/ai/xai/platform/files", "{\"action\":\"revoke_url\",\"id\":\"file_abc\"", "/v1/files/file_abc/public-url/revoke"},
|
||||
{"/ai/xai/platform/collections", "{\"action\":\"delete\",\"collection_id\":\"collection_x\"", "/v1/collections/collection_x"},
|
||||
{"/ai/xai/platform/collections", "{\"action\":\"remove_document\",\"collection_id\":\"collection_x\",\"file_id\":\"file_y\"",
|
||||
"/v1/collections/collection_x/documents/file_y"},
|
||||
{"/ai/xai/platform/voices", "{\"action\":\"custom_delete\",\"voice_id\":\"abcd1234\"", "/v1/custom-voices/abcd1234"},
|
||||
{"/ai/xai/platform/keys", "{\"action\":\"delete\",\"api_key_id\":\"" + team + "\"", "/auth/api-keys/" + team},
|
||||
{"/ai/xai/platform/keys", "{\"action\":\"rotate\",\"api_key_id\":\"" + team + "\"", "/auth/api-keys/" + team + "/rotate"},
|
||||
{"/ai/xai/platform/billing", "{\"action\":\"top_up\",\"team_id\":\"" + team + "\",\"amount\":\"10\"",
|
||||
"/v1/billing/teams/" + team + "/prepaid/top-up"},
|
||||
{"/ai/xai/platform/billing", "{\"action\":\"set_spending_limit\",\"team_id\":\"" + team + "\",\"amount\":\"100\"",
|
||||
"/v1/billing/teams/" + team + "/postpaid/spending-limits"},
|
||||
{"/ai/xai/platform/realtime/phone-numbers", "{\"action\":\"delete\",\"phone_number_id\":\"phone_1\"", "/v2/phone-numbers/phone_1"},
|
||||
{"/ai/xai/platform/realtime/calls", "{\"action\":\"refer\",\"call_id\":\"c1\",\"target_uri\":\"tel:+15551234567\"", "/v1/realtime/calls/c1/refer"},
|
||||
{"/ai/xai/platform/tts", "{\"text\":\"hi\"", "/v1/tts"},
|
||||
{"/ai/xai/platform/stt", "{\"url\":\"https://example.com/a.mp3\"", "/v1/stt"},
|
||||
{"/ai/xai/platform/realtime/secret", "{\"expires_seconds\":60", "/v1/realtime/client_secrets"},
|
||||
{"/ai/xai/platform/realtime/session", "{\"text\":\"hi\"", "wss://api.x.ai/v1/realtime"},
|
||||
};
|
||||
for (const auto& c : cases) {
|
||||
std::string p = c.path;
|
||||
auto dry = f.client->post(p, c.body + "}", "application/json");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, c.expect_path),
|
||||
p + " " + c.expect_path + ": default is a dry run");
|
||||
auto off = f.client->post(p, c.body + ",\"confirm\":false}", "application/json");
|
||||
check(off.status == 200 && contains(off.body, "\"dry_run\":true"), p + ": confirm false is a dry run");
|
||||
auto real = f.client->post(p, c.body + ",\"confirm\":true}", "application/json");
|
||||
check(real.status == 503 && contains(real.body, "not_configured"), p + ": confirmed without a key is 503");
|
||||
check(f.client->post(p, c.body + ",\"confirm\":\"yes\"}", "application/json").status == 422,
|
||||
p + ": non-bool confirm is 422");
|
||||
}
|
||||
// Dry runs still validate their input.
|
||||
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"delete\",\"id\":\"../x\"}", "application/json").status == 422,
|
||||
"bad id is 422 even on a dry run");
|
||||
check(f.client->post("/ai/xai/platform/billing", "{\"action\":\"top_up\",\"team_id\":\"" + team + "\",\"amount\":\"-5\",\"confirm\":true}", "application/json").status == 422,
|
||||
"a bad amount is 422 before the key is needed");
|
||||
check(f.client->post("/ai/xai/platform/tts", "{\"text\":\"hi\",\"speed\":9}", "application/json").status == 422,
|
||||
"tts speed out of range is 422 on a dry run");
|
||||
return finish("ai/xai/platform/confirm");
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Local routes (no key, no network): POST /ai/xai/platform/realtime/session/preview
|
||||
// and /ai/xai/platform/webhook/verify, plus body and limit cases.
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
#include "xai/platform/webhook.h"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("XAI_API_KEY");
|
||||
unsetenv("XAI_MANAGEMENT_KEY");
|
||||
Fixture f;
|
||||
auto pre = f.client->post("/ai/xai/platform/realtime/session/preview",
|
||||
"{\"voice\":\"eve\",\"turn_detection\":\"server_vad\",\"instructions\":\"Be brief.\","
|
||||
"\"input_type\":\"audio/pcm\",\"input_rate\":16000,"
|
||||
"\"tools\":[{\"type\":\"web_search\"}]}",
|
||||
"application/json");
|
||||
check(pre.status == 200 && contains(pre.body, "\"type\":\"session.update\""), "preview builds session.update");
|
||||
check(contains(pre.body, "\"rate\":16000") && contains(pre.body, "\"type\":\"server_vad\""), "preview carries audio and turns");
|
||||
auto manual = f.client->post("/ai/xai/platform/realtime/session/preview", "{\"turn_detection\":\"manual\"}", "application/json");
|
||||
check(manual.status == 200 && contains(manual.body, "\"type\":null"), "manual turns send a null type");
|
||||
for (const char* bad : {"{\"model\":\"gpt-realtime\"}", "{\"input_type\":\"audio/mp3\"}", "{\"output_speed\":3}",
|
||||
"{\"turn_detection\":\"loud\"}", "{\"language_hint\":\"es\"}", "{\"tools\":[{\"type\":\"nope\"}]}",
|
||||
"{\"keyterms\":[1]}", "{\"idle_timeout_ms\":\"soon\"}"})
|
||||
check(f.client->post("/ai/xai/platform/realtime/session/preview", bad, "application/json").status == 422,
|
||||
std::string("preview refuses ") + bad);
|
||||
|
||||
// Webhook verification with a locally signed SIP event (shape from the docs).
|
||||
const std::string secret = "whsec_MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=";
|
||||
const std::string event =
|
||||
"{\"object\":\"event\",\"id\":\"evt_123\",\"type\":\"realtime.call.incoming\",\"created_at\":1750000000,"
|
||||
"\"data\":{\"call_id\":\"00000000-0000-0000-0000-000000000000\",\"metadata\":{}}}";
|
||||
molo::xai::platform::WebhookVerifier v(secret);
|
||||
molo::xai::platform::WebhookHeaders h{"msg_1", "1750000000", ""};
|
||||
h.signature = v.sign(h, event);
|
||||
auto esc = [](const std::string& s) {
|
||||
std::string o;
|
||||
for (char c : s) {
|
||||
if (c == '"' || c == '\\') o.push_back('\\');
|
||||
o.push_back(c);
|
||||
}
|
||||
return o;
|
||||
};
|
||||
auto req = [&](const std::string& sig, const std::string& body, long now) {
|
||||
return "{\"secret\":\"" + secret + "\",\"webhook_id\":\"msg_1\",\"webhook_timestamp\":\"1750000000\","
|
||||
"\"webhook_signature\":\"" + sig + "\",\"body\":\"" + esc(body) + "\",\"now\":" + std::to_string(now) + "}";
|
||||
};
|
||||
auto ok = f.client->post("/ai/xai/platform/webhook/verify", req(h.signature, event, 1750000010), "application/json");
|
||||
check(ok.status == 200 && contains(ok.body, "\"valid\":true") &&
|
||||
contains(ok.body, "call_id=00000000-0000-0000-0000-000000000000"),
|
||||
"valid signature opens the call");
|
||||
auto tamper = f.client->post("/ai/xai/platform/webhook/verify", req(h.signature, event + " ", 1750000010), "application/json");
|
||||
check(tamper.status == 502 || tamper.status == 422, "tampered body is refused (loud)");
|
||||
auto old = f.client->post("/ai/xai/platform/webhook/verify", req(h.signature, event, 1750001000), "application/json");
|
||||
check(old.status != 200, "stale timestamp is refused");
|
||||
check(f.client->post("/ai/xai/platform/webhook/verify", "{}", "application/json").status == 422, "missing fields is 422");
|
||||
|
||||
// Limits and bodies.
|
||||
std::string big(5 * 1024 * 1024, 'a');
|
||||
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"upload\",\"filename\":\"a\",\"data_base64\":\"" + big + "\"}",
|
||||
"application/json").status == 413, "body over the cap is 413");
|
||||
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"upload\",\"filename\":\"a.txt\",\"data_base64\":\"!!!\"}",
|
||||
"application/json").status == 422, "bad base64 is 422");
|
||||
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"upload\",\"filename\":\"a/b\",\"data_base64\":\"QUJD\"}",
|
||||
"application/json").status == 422, "path in filename is 422");
|
||||
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"list\",\"limit\":5000}", "application/json").status == 422,
|
||||
"limit over the cap is 422");
|
||||
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"list_all\",\"max_pages\":1000}", "application/json").status == 422,
|
||||
"page walk over the cap is 422");
|
||||
check(f.client->post("/ai/xai/platform/tts", "{\"text\":123}", "application/json").status == 422, "wrong type is 422");
|
||||
check(f.client->post("/ai/xai/platform/tts", "{\"text\":\"" + std::string(60001, 'a') + "\"}", "application/json").status == 422,
|
||||
"tts text over 60000 characters is 422");
|
||||
check(f.client->post("/ai/xai/platform/stt", "{\"keyterms\":[\"a\"],\"url\":\"ftp://x\"}", "application/json").status == 422,
|
||||
"stt url scheme is checked");
|
||||
check(f.client->post("/ai/xai/platform/collections", "{\"action\":\"batch_get\",\"collection_id\":\"c\",\"file_ids\":[]}",
|
||||
"application/json").status == 422, "empty batch is 422");
|
||||
check(f.client->post("/ai/xai/platform/search", "{\"query\":\"q\",\"collection_ids\":[\"c\"],\"retrieval_mode\":\"fuzzy\"}",
|
||||
"application/json").status == 422, "unknown retrieval mode is 422");
|
||||
return finish("ai/xai/platform/local");
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Route: GET /ai/xai/platform (page, no credential needed)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("XAI_API_KEY");
|
||||
unsetenv("XAI_MANAGEMENT_KEY");
|
||||
Fixture f;
|
||||
auto res = f.client->get("/ai/xai/platform");
|
||||
check(res.status == 200, "status 200");
|
||||
check(contains(res.headers.get("Content-Type"), "text/html"), "html");
|
||||
check(contains(res.body, "xAI Platform") && contains(res.body, "viewport"), "page and mobile viewport");
|
||||
check(contains(res.body, "/embedded/ai_xai_platform.js"), "script wired");
|
||||
check(contains(res.body, "<select id=\"xp-route\""), "route picker is a dropdown, not a button group");
|
||||
check(f.client->post("/ai/xai/platform", "{}", "application/json").status == 405, "post is 405");
|
||||
auto js = f.client->get("/embedded/ai_xai_platform.js");
|
||||
check(js.status == 200 && contains(js.body, "/ai/xai/platform/"), "script is served");
|
||||
auto idx = f.client->get("/");
|
||||
check(idx.status == 200 && contains(idx.body, "/ai/xai/platform"), "matrix lists the page");
|
||||
return finish("ai/xai/platform");
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Routes: POST /ai/xai/platform/{account,files,collections,search,voices,tts,stt,
|
||||
// realtime/secret,realtime/session,realtime/phone-numbers,realtime/calls,keys,
|
||||
// billing,audit}
|
||||
// Without a key a well formed request is 503 not_configured; bad input is 422
|
||||
// and never reaches xAI; GET is 405. Nothing here touches the network.
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
namespace {
|
||||
struct Route {
|
||||
const char* path;
|
||||
const char* valid;
|
||||
const char* bad;
|
||||
};
|
||||
|
||||
const char* kTeam = "5ea6f6bd-7815-4b8a-9135-28b2d7ba6722";
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
unsetenv("XAI_API_KEY");
|
||||
unsetenv("XAI_MANAGEMENT_KEY");
|
||||
Fixture f;
|
||||
const std::string team = std::string("\"team_id\":\"") + kTeam + "\"";
|
||||
const std::string billing_ok = "{\"action\":\"prepaid_balance\"," + team + "}";
|
||||
const std::string audit_ok = "{" + team + "}";
|
||||
const Route routes[] = {
|
||||
{"/ai/xai/platform/account", "{\"action\":\"api_key\"}", "{\"action\":\"x\"}"},
|
||||
{"/ai/xai/platform/files", "{\"action\":\"list\"}", "{\"action\":\"get\"}"},
|
||||
{"/ai/xai/platform/collections", "{\"action\":\"list\"}", "{\"action\":\"get\"}"},
|
||||
{"/ai/xai/platform/search", "{\"query\":\"q\",\"collection_ids\":[\"collection_x\"]}", "{\"query\":\"q\"}"},
|
||||
{"/ai/xai/platform/voices", "{\"action\":\"list\"}", "{\"action\":\"get\"}"},
|
||||
{"/ai/xai/platform/tts", "{\"text\":\"hi\",\"confirm\":true}", "{\"text\":\"\"}"},
|
||||
{"/ai/xai/platform/stt", "{\"url\":\"https://example.com/a.mp3\",\"confirm\":true}", "{\"confirm\":true}"},
|
||||
{"/ai/xai/platform/realtime/secret", "{\"confirm\":true}", "{\"expires_seconds\":9999}"},
|
||||
{"/ai/xai/platform/realtime/session", "{\"text\":\"hi\",\"confirm\":true}", "{\"confirm\":true}"},
|
||||
{"/ai/xai/platform/realtime/phone-numbers", "{\"action\":\"list\"}",
|
||||
"{\"action\":\"create\",\"name\":\"n\",\"phone_number\":\"123\"}"},
|
||||
{"/ai/xai/platform/realtime/calls", "{\"action\":\"hangup\",\"call_id\":\"c1\",\"confirm\":true}",
|
||||
"{\"action\":\"dial\",\"call_id\":\"c1\"}"},
|
||||
{"/ai/xai/platform/keys", "{\"action\":\"validate\"}", "{\"action\":\"list\",\"team_id\":\"x\"}"},
|
||||
{"/ai/xai/platform/billing", billing_ok.c_str(), "{\"action\":\"prepaid_balance\",\"team_id\":\"x\"}"},
|
||||
{"/ai/xai/platform/audit", audit_ok.c_str(), "{\"team_id\":\"x\"}"},
|
||||
};
|
||||
for (const Route& r : routes) {
|
||||
std::string p = r.path;
|
||||
auto ok = f.client->post(p, r.valid, "application/json");
|
||||
check(ok.status == 503 && contains(ok.body, "not_configured"), p + ": no key is 503 not_configured");
|
||||
auto bad = f.client->post(p, r.bad, "application/json");
|
||||
check(bad.status == 422 && contains(bad.body, "validation_error"), p + ": bad input is 422");
|
||||
check(f.client->post(p, "{not json", "application/json").status == 422, p + ": broken json is 422");
|
||||
check(f.client->post(p, "[1]", "application/json").status == 422, p + ": non-object json is 422");
|
||||
check(f.client->get(p).status == 405, p + ": get is 405");
|
||||
}
|
||||
// The keys decide the host: the API key alone does not open management routes.
|
||||
setenv("XAI_API_KEY", "k-test", 1);
|
||||
Fixture g;
|
||||
auto m = g.client->post("/ai/xai/platform/collections", "{\"action\":\"list\"}", "application/json");
|
||||
check(m.status == 503 && contains(m.body, "XAI_MANAGEMENT_KEY"), "collections need the management key");
|
||||
unsetenv("XAI_API_KEY");
|
||||
return finish("ai/xai/platform/routes");
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Routes: GET /ai/xai/platform/status and /ai/xai/platform/calls
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("XAI_API_KEY");
|
||||
unsetenv("XAI_MANAGEMENT_KEY");
|
||||
Fixture f;
|
||||
auto res = f.client->get("/ai/xai/platform/status");
|
||||
check(res.status == 200, "status 200");
|
||||
check(contains(res.body, "\"api_key_configured\":false"), "reports no api key");
|
||||
check(contains(res.body, "\"management_key_configured\":false"), "reports no management key");
|
||||
check(contains(res.body, "https://management-api.x.ai"), "management host is separate");
|
||||
check(!contains(res.body, "Bearer"), "no credential in the body");
|
||||
auto calls = f.client->get("/ai/xai/platform/calls");
|
||||
check(calls.status == 200, "calls 200");
|
||||
check(contains(calls.body, "/v1/realtime/client_secrets"), "table lists client secrets");
|
||||
check(contains(calls.body, "/auth/teams/{teamId}/api-keys"), "table lists key management");
|
||||
check(contains(calls.body, "\"host\":\"management\""), "table names the host");
|
||||
setenv("XAI_API_KEY", "k-test", 1);
|
||||
Fixture g;
|
||||
check(contains(g.client->get("/ai/xai/platform/status").body, "\"api_key_configured\":true"), "reports api key");
|
||||
check(!contains(g.client->get("/ai/xai/platform/status").body, "k-test"), "never echoes the key");
|
||||
unsetenv("XAI_API_KEY");
|
||||
check(f.client->post("/ai/xai/platform/status", "{}", "application/json").status == 405, "post is 405");
|
||||
check(f.client->post("/ai/xai/platform/calls", "{}", "application/json").status == 405, "calls post is 405");
|
||||
return finish("ai/xai/platform/status");
|
||||
}
|
||||
Reference in New Issue
Block a user