Merge openai platform package

This commit is contained in:
2026-10-24 08:00:17 +02:00
60 changed files with 5788 additions and 3 deletions
@@ -0,0 +1,35 @@
// Route: POST /ai/openai/platform/call (table driven)
#include "support/Assert.hpp"
using namespace molo::example::test;
static const char* kJson = "application/json";
int main() {
setenv("MOLO_ENV_FILES", "off", 1);
unsetenv("OPENAI_API_KEY");
Fixture f;
const std::string url = "/ai/openai/platform/call";
auto list = f.client->post(url, "{\"operation\":\"listFiles\",\"query\":{\"limit\":1}}", kJson);
check(list.status == 503 && contains(list.body, "not_configured"), "read-only call without key is 503");
check(f.client->post(url, "{}", kJson).status == 422, "operation required is 422");
check(f.client->post(url, "{\"operation\":\"nope\"}", kJson).status == 422, "unknown operation is 422");
check(f.client->post(url, "{\"operation\":\"retrieveFile\"}", kJson).status == 422, "missing id is 422");
check(f.client->post(url, "{\"operation\":\"retrieveFile\",\"ids\":{\"file_id\":\"a/b\"}}", kJson).status == 422, "bad id is 422");
check(f.client->post(url, "{\"operation\":\"listFiles\",\"query\":{\"bogus\":1}}", kJson).status == 422, "unknown query is 422");
check(f.client->post(url, "{\"operation\":\"createAssistant\",\"body\":{}}", kJson).status == 422, "missing required field is 422");
check(f.client->post(url, "{\"operation\":\"createFile\"}", kJson).status == 422, "multipart-only call is 422");
check(f.client->post(url, "{\"operation\":\"downloadFile\",\"ids\":{\"file_id\":\"f\"}}", kJson).status == 422, "binary call is 422");
check(f.client->post(url, "not json", kJson).status == 422, "bad json is 422");
check(f.client->post(url, "[1]", kJson).status == 422, "non-object is 422");
auto paid = f.client->post(url, "{\"operation\":\"createBatch\",\"body\":{\"input_file_id\":\"file-1\",\"endpoint\":\"/v1/embeddings\",\"completion_window\":\"24h\"}}", kJson);
check(paid.status == 200 && contains(paid.body, "\"dry_run\":true") && contains(paid.body, "POST /batches"), "paid call is a dry run");
auto del = f.client->post(url, "{\"operation\":\"deleteFile\",\"ids\":{\"file_id\":\"file-1\"}}", kJson);
check(del.status == 200 && contains(del.body, "\"dry_run\":true") && contains(del.body, "destructive"), "delete is a dry run");
auto beta = f.client->post(url, "{\"operation\":\"deleteThread\",\"ids\":{\"thread_id\":\"t\"}}", kJson);
check(contains(beta.body, "assistants=v2"), "dry run shows the beta header");
auto run = f.client->post(url, "{\"operation\":\"deleteFile\",\"ids\":{\"file_id\":\"file-1\"},\"confirm\":true}", kJson);
check(run.status == 503, "confirmed call without key is 503");
check(f.client->get(url).status == 405, "get is 405");
return finish("ai/openai/platform/call");
}
@@ -0,0 +1,40 @@
// Routes: POST /ai/openai/platform/realtime/{event,parse,catalog,socket}, /batch/file, /uploads/plan
#include "support/Assert.hpp"
using namespace molo::example::test;
static const char* kJson = "application/json";
int main() {
setenv("MOLO_ENV_FILES", "off", 1);
unsetenv("OPENAI_API_KEY");
Fixture f;
const std::string b = "/ai/openai/platform";
auto ev = f.client->post(b + "/realtime/event", "{\"type\":\"session.update\",\"fields\":{\"session\":{\"type\":\"realtime\"}}}", kJson);
check(ev.status == 200 && contains(ev.body, "\"type\":\"session.update\""), "event builds without a key");
check(f.client->post(b + "/realtime/event", "{\"type\":\"session.update\"}", kJson).status == 422, "missing required field is 422");
check(f.client->post(b + "/realtime/event", "{\"type\":\"nope\"}", kJson).status == 422, "unknown event is 422");
check(f.client->post(b + "/realtime/event", "{\"profile\":\"x\",\"type\":\"a\"}", kJson).status == 422, "unknown profile is 422");
auto parse = f.client->post(b + "/realtime/parse", "{\"text\":\"{\\\"type\\\":\\\"error\\\",\\\"error\\\":{\\\"message\\\":\\\"boom\\\"}}\"}", kJson);
check(parse.status == 200 && contains(parse.body, "\"is_error\":true") && contains(parse.body, "boom"), "error event parses");
check(f.client->post(b + "/realtime/parse", "{\"text\":\"nope\"}", kJson).status == 422, "bad event text is 422");
auto cat = f.client->post(b + "/realtime/catalog", "{\"profile\":\"live\",\"direction\":\"client\"}", kJson);
check(cat.status == 200 && contains(cat.body, "session.start"), "catalog lists live client events");
check(f.client->post(b + "/realtime/catalog", "{\"direction\":\"x\"}", kJson).status == 422, "bad direction is 422");
auto batch = f.client->post(b + "/batch/file", "{\"endpoint\":\"/v1/embeddings\",\"requests\":[{\"custom_id\":\"a\",\"body\":{\"model\":\"m\",\"input\":\"x\"}}]}", kJson);
check(batch.status == 200 && contains(batch.body, "\"requests\":1"), "batch file builds");
check(f.client->post(b + "/batch/file", "{\"endpoint\":\"/v1/embeddings\",\"requests\":[{\"custom_id\":\"a\",\"body\":{}},{\"custom_id\":\"a\",\"body\":{}}]}", kJson).status == 422, "duplicate custom_id is 422");
check(f.client->post(b + "/batch/file", "{\"endpoint\":\"/v1/nope\",\"requests\":[{\"custom_id\":\"a\"}]}", kJson).status == 422, "bad endpoint is 422");
auto plan = f.client->post(b + "/uploads/plan", "{\"bytes\":100,\"part_bytes\":30}", kJson);
check(plan.status == 200 && contains(plan.body, "\"parts\":4"), "upload plan");
check(f.client->post(b + "/uploads/plan", "{\"bytes\":0}", kJson).status == 422, "empty upload is 422");
check(f.client->post(b + "/uploads/plan", "{\"bytes\":100,\"part_bytes\":999999999999}", kJson).status == 422, "part over 64 MiB is 422");
auto dry = f.client->post(b + "/realtime/socket", "{\"kind\":\"realtime\",\"model\":\"gpt-realtime\"}", kJson);
check(dry.status == 200 && contains(dry.body, "wss://api.openai.com/v1/realtime?model=gpt-realtime"), "socket dry run");
check(f.client->post(b + "/realtime/socket", "{\"kind\":\"realtime\",\"model\":\"gpt-realtime\",\"confirm\":true}", kJson).status == 503, "confirmed socket without key is 503");
check(f.client->post(b + "/realtime/socket", "{\"kind\":\"realtime\"}", kJson).status == 422, "model required is 422");
check(f.client->post(b + "/realtime/socket", "{\"kind\":\"x\"}", kJson).status == 422, "bad kind is 422");
check(f.client->post(b + "/realtime/socket", "{\"kind\":\"live\",\"seconds\":999}", kJson).status == 422, "seconds over cap is 422");
check(f.client->get(b + "/realtime/socket").status == 405, "get socket is 405");
return finish("ai/openai/platform/helpers");
}
@@ -0,0 +1,25 @@
// Routes: GET /ai/openai/platform, /status, /calls, /events
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
setenv("MOLO_ENV_FILES", "off", 1);
unsetenv("OPENAI_API_KEY");
Fixture f;
auto page = f.client->get("/ai/openai/platform");
check(page.status == 200 && contains(page.body, "OpenAI platform"), "page 200");
check(contains(page.body, "ai_openai_platform.js"), "page loads its script");
auto st = f.client->get("/ai/openai/platform/status");
check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without key");
check(contains(st.body, "\"credential_env\":\"OPENAI_API_KEY\""), "status names the env var");
check(contains(st.body, "\"calls\":188"), "status counts calls");
auto calls = f.client->get("/ai/openai/platform/calls");
check(calls.status == 200 && contains(calls.body, "createVectorStoreFileBatch"), "calls lists the table");
auto ev = f.client->get("/ai/openai/platform/events");
check(ev.status == 200 && contains(ev.body, "input_audio_buffer.append"), "events lists the catalog");
check(f.client->post("/ai/openai/platform/status", "{}", "application/json").status == 405, "post status is 405");
check(f.client->post("/ai/openai/platform", "{}", "application/json").status == 405, "post page is 405");
check(f.client->get("/ai/openai/platform/call").status == 405, "get call is 405");
return finish("ai/openai/platform/status");
}