#include "../AiDemos.hpp" #include #include "../../fw/Router.hpp" #include "../../fw/Templates.hpp" #include "llm/config.h" #include "llm/error.h" #include "openai/platform/batches_api.h" #include "openai/platform/caps.h" #include "openai/platform/event_socket.h" #include "openai/platform/platform.h" #include "openai/platform/uploads_api.h" namespace molo::example::app { // OpenAI platform: realtime, files, uploads, batch, fine-tuning, vector // stores, evals, conversations, containers, assistants, ChatKit, agents, // skills, vaults, live. One generic POST (/ai/openai/platform/call) runs any // call of the table (src/openai/platform/calls_table.inc); the other POSTs // are local helpers (event builder / parser, batch file, upload plan) or the // WebSocket session. Paid and destructive calls are dry runs unless the body // says "confirm":true. No credential: 503 not_configured on every route that // would call OpenAI; GET page, /status, /calls and /events answer 200. namespace { namespace oa = openai::platform; using Json = clients::body::Json; constexpr std::size_t kMaxBody = 256 * 1024; constexpr int kMaxSocketSeconds = 30; constexpr std::size_t kMaxSocketEventsShown = 200; constexpr std::size_t kMaxClientEvents = 20; const char kPage[] = R"OAIP({% extends "base.html" %} {% block content %}

OpenAI platform

Files, uploads, batch, fine-tuning, vector stores, evals, conversations, containers, assistants, ChatKit, agents, skills, vaults, realtime and live. Paid and destructive calls are dry runs until you tick confirm.

Checking credentials...

Call

Realtime event


{% endblock %} )OAIP"; 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); } [[noreturn]] void rethrow(const llm::ApiError& e) { if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED") throw fw::HttpError(503, "not_configured", e.message()); if (e.http_status() == 400 && e.type() == "INVALID_ARGUMENT") bad(e.message()); throw fw::HttpError(502, "openai_error", e.type() + ": " + e.message()); } template auto guarded(F&& f) -> decltype(f()) { try { return f(); } catch (const llm::ApiError& e) { rethrow(e); } } 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& body, const char* key, bool required = false) { const Json* v = body.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(); } Json object_field(const Json& body, const char* key) { const Json* v = body.find(key); if (!v || v->is_null()) return Json::object(); if (!v->is_object()) bad(std::string(key) + " must be an object"); return *v; } bool flag(const Json& body, const char* key) { const Json* v = body.find(key); return v && v->is_bool() && v->as_bool(); } oa::Profile profile_of(const Json& body) { oa::Profile p = oa::Profile::kGa; std::string name = text(body, "profile"); if (!name.empty() && !oa::Events::parse_profile(name, p)) bad("profile must be ga, beta, translation, live, live_fork or live_sideband"); return p; } Json flags_json(unsigned flags) { Json arr = Json::array(); static const std::pair kNames[] = { {oa::kDestructive, "destructive"}, {oa::kPaid, "paid"}, {oa::kBetaAssistants, "beta_assistants"}, {oa::kBetaChatkit, "beta_chatkit"}, {oa::kIdempotency, "idempotency_key"}, {oa::kStreamable, "streamable"}, {oa::kList, "list"}, {oa::kDeprecated, "deprecated"}, {oa::kBodyUnion, "body_union"}}; for (const auto& [bit, name] : kNames) if (flags & bit) arr.push(Json(name)); return arr; } Json csv_json(const char* csv) { Json arr = Json::array(); for (const std::string& s : oa::Calls::list(csv)) arr.push(Json(s)); return arr; } Json prepared_json(const oa::Prepared& p, const char* what = "request") { Json o = Json::object(); o.set("dry_run", Json(true)); o.set("operation", Json(p.operation_id)); o.set(what, Json(p.method + " " + p.target)); Json h = Json::object(); for (const auto& [k, v] : p.headers) h.set(k, Json(v)); o.set("headers", h); if (p.has_body) o.set("body", p.body); o.set("flags", flags_json(p.flags)); o.set("hint", Json("send confirm:true to run it")); return o; } class OpenaiPlatformRoutes { public: explicit OpenaiPlatformRoutes(std::shared_ptr platform, std::string config_error) : platform_(std::move(platform)), config_error_(std::move(config_error)) {} fw::Response status(const fw::Request&) const { Json o = Json::object(); o.set("configured", Json(platform_->configured())); o.set("org_set", Json(!llm::Config::get(oa::PlatformConfig::kOrgEnv).empty())); o.set("project_set", Json(!llm::Config::get(oa::PlatformConfig::kProjectEnv).empty())); o.set("base_url", Json(oa::PlatformConfig::kBaseUrl)); o.set("credential_env", Json(oa::PlatformConfig::kKeyEnv)); if (!config_error_.empty()) o.set("config_error", Json(config_error_)); o.set("calls", Json(static_cast(oa::Calls::count()))); o.set("events", Json(static_cast(oa::Events::count()))); o.set("websocket_endpoints", Json(static_cast(oa::EventSocket::endpoint_count()))); o.set("max_socket_seconds", Json(kMaxSocketSeconds)); return json_out(o); } fw::Response calls(const fw::Request&) const { Json arr = Json::array(); for (std::size_t i = 0; i < oa::Calls::count(); ++i) { const oa::CallSpec& s = oa::Calls::at(i); Json row = Json::object(); row.set("operation", Json(s.operation_id)); row.set("area", Json(s.area)); row.set("http", Json(s.method)); row.set("path", Json(s.path)); Json ids = Json::array(); for (const std::string& n : oa::Calls::placeholders(s)) ids.push(Json(n)); row.set("ids", ids); row.set("body_fields", csv_json(s.body_fields)); row.set("required", csv_json(s.required_fields)); row.set("query", csv_json(s.query_params)); row.set("json", Json((s.req & oa::kReqJson) != 0)); row.set("flags", flags_json(s.flags)); arr.push(row); } Json o = Json::object(); o.set("calls", arr); return json_out(o); } fw::Response events(const fw::Request&) const { Json arr = Json::array(); for (std::size_t i = 0; i < oa::Events::count(); ++i) { const oa::EventSpec& e = oa::Events::at(i); Json row = Json::object(); row.set("profile", Json(oa::Events::profile_name(e.profile))); row.set("direction", Json(e.direction == oa::Direction::kClientEvent ? "client" : "server")); row.set("type", Json(e.type)); row.set("schema", Json(e.schema)); row.set("required", csv_json(e.required)); arr.push(row); } Json o = Json::object(); o.set("events", arr); return json_out(o); } // Generic table-driven call. fw::Response call(const fw::Request& req) const { Json b = parse_body(req); std::string op_name = text(b, "operation", true); const oa::CallSpec* spec = oa::Calls::find(op_name); if (!spec) bad("unknown operation '" + op_name + "'"); if ((spec->req & oa::kReqJson) == 0 && (spec->req != oa::kReqNone)) bad(op_name + " uploads a file or SDP; use its dedicated route or the library"); if (spec->resp != oa::kRespJson && spec->resp != oa::kRespEmpty) bad(op_name + " answers non-JSON data; use the library for it"); oa::PathArgs ids; for (const auto& [k, v] : object_field(b, "ids").as_object()) { if (!v.is_string()) bad("ids must be strings"); ids.emplace_back(k, v.as_string()); } Json query = object_field(b, "query"); Json body = object_field(b, "body"); const Json* body_ptr = (spec->req & oa::kReqJson) != 0 ? &body : nullptr; std::string idem = text(b, "idempotency_key"); llm::Headers extra; if (!idem.empty()) extra["Idempotency-Key"] = idem; oa::Prepared p = guarded([&] { return oa::PlatformService::prepare(spec->op, ids, body_ptr, query, extra); }); if ((spec->flags & (oa::kDestructive | oa::kPaid)) && !flag(b, "confirm")) return json_out(prepared_json(p)); return guarded([&] { if (!platform_->configured()) throw llm::NotConfiguredError("openai", oa::PlatformConfig::kKeyEnv); Json out = platform_->generic.invoke(spec->op, ids, body_ptr, query, extra); return json_out(out); }); } fw::Response event_build(const fw::Request& req) const { Json b = parse_body(req); oa::Profile profile = profile_of(b); std::string type = text(b, "type", true); Json fields = object_field(b, "fields"); return guarded([&] { return json_out(oa::Events::make_client(profile, type, fields, text(b, "event_id"))); }); } fw::Response event_parse(const fw::Request& req) const { Json b = parse_body(req); oa::Profile profile = profile_of(b); std::string raw = text(b, "text", true); return guarded([&] { oa::ParsedEvent ev; try { ev = oa::Events::parse_server(profile, raw); } catch (const llm::ApiError& e) { if (e.type() == "BAD_EVENT") bad(e.message()); // the caller's text, not a server reply throw; } Json o = Json::object(); o.set("type", Json(ev.type)); o.set("known", Json(ev.known)); o.set("is_error", Json(ev.is_error)); if (!ev.event_id.empty()) o.set("event_id", Json(ev.event_id)); if (ev.spec) o.set("schema", Json(ev.spec->schema)); if (ev.is_error) o.set("error_message", Json(ev.error_message)); return json_out(o); }); } fw::Response event_catalog(const fw::Request& req) const { Json b = parse_body(req); oa::Profile profile = profile_of(b); std::string dir = text(b, "direction"); if (!dir.empty() && dir != "client" && dir != "server") bad("direction must be client or server"); Json arr = Json::array(); for (oa::Direction d : {oa::Direction::kClientEvent, oa::Direction::kServerEvent}) { if (!dir.empty() && (dir == "client") != (d == oa::Direction::kClientEvent)) continue; for (const oa::EventSpec* e : oa::Events::catalog(profile, d)) { Json row = Json::object(); row.set("direction", Json(d == oa::Direction::kClientEvent ? "client" : "server")); row.set("type", Json(e->type)); row.set("fields", csv_json(e->fields)); row.set("required", csv_json(e->required)); arr.push(row); } } Json o = Json::object(); o.set("profile", Json(oa::Events::profile_name(profile))); o.set("events", arr); return json_out(o); } fw::Response batch_file(const fw::Request& req) const { Json b = parse_body(req); std::string endpoint = text(b, "endpoint", true); const Json* reqs = b.find("requests"); if (!reqs || !reqs->is_array() || reqs->as_array().empty()) bad("requests must be a non-empty array"); return guarded([&] { oa::BatchInput in(endpoint); for (const Json& r : reqs->as_array()) { if (!r.is_object()) bad("each request must be an object"); in.add(text(r, "custom_id", true), object_field(r, "body")); } Json o = Json::object(); o.set("requests", Json(static_cast(in.size()))); o.set("bytes", Json(static_cast(in.jsonl().size()))); o.set("jsonl", Json(in.jsonl())); return json_out(o); }); } fw::Response upload_plan(const fw::Request& req) const { Json b = parse_body(req); const Json* bytes = b.find("bytes"); if (!bytes || !bytes->is_number() || bytes->as_number() < 1) bad("bytes must be a positive number"); const Json* part = b.find("part_bytes"); double part_bytes = part && part->is_number() ? part->as_number() : 8.0 * 1024 * 1024; return guarded([&] { oa::UploadPlan plan = oa::UploadPlan::make(static_cast(bytes->as_number()), static_cast(part_bytes)); Json o = Json::object(); o.set("parts", Json(static_cast(plan.parts.size()))); Json first = Json::array(); for (std::size_t i = 0; i < plan.parts.size() && i < 5; ++i) { Json p = Json::object(); p.set("offset", Json(static_cast(plan.parts[i].offset))); p.set("size", Json(static_cast(plan.parts[i].size))); first.push(p); } o.set("first_parts", first); return json_out(o); }); } // Runs a short WebSocket session: connect, send events, collect. fw::Response socket(const fw::Request& req) const { Json b = parse_body(req); static const std::pair kKinds[] = { {"realtime", oa::WsKind::kRealtime}, {"realtime_beta", oa::WsKind::kRealtimeBeta}, {"translation", oa::WsKind::kRealtimeTranslation}, {"live", oa::WsKind::kLiveSession}, {"live_sideband", oa::WsKind::kLiveSideband}, {"live_fork", oa::WsKind::kLiveFork}}; std::string kind_name = text(b, "kind", true); const oa::WsKind* kind = nullptr; for (const auto& [n, k] : kKinds) if (kind_name == n) kind = &k; if (!kind) bad("kind must be realtime, realtime_beta, translation, live, live_sideband or live_fork"); oa::WsParams params; params.model = text(b, "model"); params.call_id = text(b, "call_id"); params.session_id = text(b, "session_id"); params.graceful_close = flag(b, "graceful_close"); int seconds = 5; if (const Json* s = b.find("seconds"); s && !s->is_null()) { if (!s->is_number() || s->as_int() < 1 || s->as_int() > kMaxSocketSeconds) bad("seconds must be 1 to " + std::to_string(kMaxSocketSeconds)); seconds = static_cast(s->as_int()); } std::size_t max_events = 50; if (const Json* m = b.find("max_events"); m && !m->is_null()) { if (!m->is_number() || m->as_int() < 1 || m->as_int() > static_cast(kMaxSocketEventsShown)) bad("max_events must be 1 to " + std::to_string(kMaxSocketEventsShown)); max_events = static_cast(m->as_int()); } oa::Profile profile = oa::EventSocket::profile_of(*kind); std::vector to_send; if (const Json* ev = b.find("events"); ev && !ev->is_null()) { if (!ev->is_array() || ev->as_array().size() > kMaxClientEvents) bad("events must be an array of up to 20"); for (const Json& e : ev->as_array()) { if (!e.is_object()) bad("each event must be an object"); Json fields = object_field(e, "fields"); to_send.push_back(guarded([&] { return oa::Events::make_client(profile, text(e, "type", true), fields, text(e, "event_id")); })); } } std::string url = guarded([&] { return oa::EventSocket::url_for(*kind, "wss://api.openai.com/v1", params); }); if (!flag(b, "confirm")) { Json o = Json::object(); o.set("dry_run", Json(true)); o.set("url", Json(url)); Json arr = Json::array(); for (const Json& e : to_send) arr.push(e); o.set("events", arr); o.set("hint", Json("send confirm:true to open the session (a session can cost money)")); return json_out(o); } return guarded([&] { if (!platform_->configured()) throw llm::NotConfiguredError("openai", oa::PlatformConfig::kKeyEnv); oa::EventSocketOptions opts; opts.organization = llm::Config::get(oa::PlatformConfig::kOrgEnv); opts.project = llm::Config::get(oa::PlatformConfig::kProjectEnv); oa::EventSocket sock(*kind, opts); sock.connect(params); for (const Json& e : to_send) sock.send(e); Json shown = Json::array(); oa::RunStats st = sock.run( [&](const oa::ParsedEvent& ev) { Json row = Json::object(); row.set("type", Json(ev.type)); row.set("known", Json(ev.known)); if (ev.is_error) row.set("error", Json(ev.error_message)); shown.push(row); return shown.as_array().size() < max_events; }, seconds); Json o = Json::object(); o.set("events", shown); o.set("stop_reason", Json(st.stop_reason)); o.set("received", Json(static_cast(st.events))); o.set("unknown", Json(static_cast(st.unknown))); return json_out(o); }); } private: std::shared_ptr platform_; std::string config_error_; }; } // namespace void register_ai_openaiplatform_demo(fw::Router& router, Services& services) { std::shared_ptr platform; std::string config_error; try { platform = oa::Platform::from_env(); } catch (const llm::ApiError& e) { // A bad OPENAI_ORG_ID / OPENAI_PROJECT_ID must not take the demo app down. config_error = e.message(); llm::ApiClientConfig c; c.vendor = "openai"; c.base_url = oa::PlatformConfig::kBaseUrl; c.key_env = oa::PlatformConfig::kKeyEnv; platform = std::make_shared(llm::ApiClient::from_env(c)); } auto routes = std::make_shared(platform, config_error); fw::Environment* env = services.templates.get(); services.templates->add_template("ai_openai_platform.html", kPage); services.add_feature("openai-platform", "/ai/openai/platform"); router.get("/ai/openai/platform", "ai-openai-platform-page", [env](fw::Request req) { (void)req; fw::TemplateCtx ctx; ctx["title"] = std::string("OpenAI platform"); return fw::Response(200, env->render("ai_openai_platform.html", ctx), "text/html; charset=utf-8"); }); router.get("/ai/openai/platform/status", "ai-openai-platform-status", [routes](fw::Request req) { return routes->status(req); }); router.get("/ai/openai/platform/calls", "ai-openai-platform-calls", [routes](fw::Request req) { return routes->calls(req); }); router.get("/ai/openai/platform/events", "ai-openai-platform-events", [routes](fw::Request req) { return routes->events(req); }); router.post("/ai/openai/platform/call", "ai-openai-platform-call", [routes](fw::Request req) { return routes->call(req); }); router.post("/ai/openai/platform/realtime/event", "ai-openai-platform-event-build", [routes](fw::Request req) { return routes->event_build(req); }); router.post("/ai/openai/platform/realtime/parse", "ai-openai-platform-event-parse", [routes](fw::Request req) { return routes->event_parse(req); }); router.post("/ai/openai/platform/realtime/catalog", "ai-openai-platform-event-catalog", [routes](fw::Request req) { return routes->event_catalog(req); }); router.post("/ai/openai/platform/realtime/socket", "ai-openai-platform-socket", [routes](fw::Request req) { return routes->socket(req); }); router.post("/ai/openai/platform/batch/file", "ai-openai-platform-batch-file", [routes](fw::Request req) { return routes->batch_file(req); }); router.post("/ai/openai/platform/uploads/plan", "ai-openai-platform-upload-plan", [routes](fw::Request req) { return routes->upload_plan(req); }); } } // namespace molo::example::app