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
+490 -3
View File
@@ -1,13 +1,500 @@
#include "../AiDemos.hpp"
#include <algorithm>
#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: realtime, files, uploads, batch, fine-tuning, vector stores, evals, conversations, containers, assistants. Stub: the module agent fills this in.
// 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 %}
<h1>OpenAI platform</h1>
<p class="hint">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.</p>
<p id="oap-config" class="hint">Checking credentials...</p>
<section class="oap">
<h2>Call</h2>
<label>Area <select id="oap-area"></select></label>
<label>Operation <select id="oap-op"></select></label>
<p id="oap-info" class="hint"></p>
<label>Ids (JSON object) <textarea id="oap-ids" rows="2">{}</textarea></label>
<label>Query (JSON object) <textarea id="oap-query" rows="2">{}</textarea></label>
<label>Body (JSON object) <textarea id="oap-body" rows="5">{}</textarea></label>
<label class="oap-check"><input id="oap-confirm" type="checkbox"> confirm (run paid or destructive calls)</label>
<div class="oap-actions"><button id="oap-send" type="button" class="primary">Send</button></div>
<h2>Realtime event</h2>
<label>Profile <select id="oap-profile">
<option value="ga">ga</option><option value="beta">beta</option><option value="translation">translation</option>
<option value="live">live</option><option value="live_fork">live_fork</option><option value="live_sideband">live_sideband</option>
</select></label>
<label>Client event type <input id="oap-etype" type="text" maxlength="128" value="session.update"></label>
<label>Fields (JSON object) <textarea id="oap-efields" rows="3">{"session":{"type":"realtime"}}</textarea></label>
<div class="oap-actions">
<button id="oap-ebuild" type="button">Build and validate</button>
<button id="oap-ecatalog" type="button">Show catalog</button>
</div>
<label>Server event text <textarea id="oap-etext" rows="3">{"type":"session.created","event_id":"event_1"}</textarea></label>
<div class="oap-actions"><button id="oap-eparse" type="button">Parse server event</button></div>
<p id="oap-error" class="error" role="alert"></p>
<pre id="oap-out" class="oap-out"></pre>
</section>
<style>
.oap label{display:block;margin:.5rem 0}
.oap input[type=text],.oap select,.oap textarea{width:100%;box-sizing:border-box}
.oap-check{display:flex;gap:.5rem;align-items:center}
.oap-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
.oap-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
</style>
<script src="/embedded/ai_openai_platform.js"></script>
{% 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 <typename F>
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<unsigned, const char*> 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<oa::Platform> 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<int>(oa::Calls::count())));
o.set("events", Json(static_cast<int>(oa::Events::count())));
o.set("websocket_endpoints", Json(static_cast<int>(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<int>(in.size())));
o.set("bytes", Json(static_cast<int>(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<std::size_t>(bytes->as_number()),
static_cast<std::size_t>(part_bytes));
Json o = Json::object();
o.set("parts", Json(static_cast<int>(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<double>(plan.parts[i].offset)));
p.set("size", Json(static_cast<double>(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<const char*, oa::WsKind> 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<int>(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<long>(kMaxSocketEventsShown))
bad("max_events must be 1 to " + std::to_string(kMaxSocketEventsShown));
max_events = static_cast<std::size_t>(m->as_int());
}
oa::Profile profile = oa::EventSocket::profile_of(*kind);
std::vector<Json> 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<int>(st.events)));
o.set("unknown", Json(static_cast<int>(st.unknown)));
return json_out(o);
});
}
private:
std::shared_ptr<oa::Platform> platform_;
std::string config_error_;
};
} // namespace
void register_ai_openaiplatform_demo(fw::Router& router, Services& services) {
(void)router;
(void)services;
std::shared_ptr<oa::Platform> 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<oa::Platform>(llm::ApiClient::from_env(c));
}
auto routes = std::make_shared<OpenaiPlatformRoutes>(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
+79
View File
@@ -0,0 +1,79 @@
// OpenAI platform page: thin client over /ai/openai/platform/* (limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("oap-out"), err = $("oap-error");
var base = "/ai/openai/platform";
var calls = [];
function show(res) {
return res.text().then(function (t) {
var j;
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
else { out.textContent = JSON.stringify(j, null, 2); }
return j;
});
}
function post(path, body) {
err.textContent = "";
out.textContent = "...";
return fetch(base + path, {
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body)
}).then(show).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
function json(id) {
try { return JSON.parse($(id).value || "{}"); }
catch (e) { err.textContent = "Not valid JSON: " + id.replace("oap-", ""); return null; }
}
fetch(base + "/status").then(function (r) { return r.json(); }).then(function (s) {
$("oap-config").textContent = s.configured
? "OpenAI key found (" + s.credential_env + "). " + s.calls + " calls, " + s.events + " events."
: "No OpenAI key: calls answer 503 until " + s.credential_env + " is set. Dry runs and local helpers work.";
});
function fillOps() {
var area = $("oap-area").value, sel = $("oap-op");
sel.innerHTML = "";
calls.filter(function (c) { return c.area === area; }).forEach(function (c) {
var o = document.createElement("option");
o.value = c.operation;
o.textContent = c.http + " " + c.path;
sel.appendChild(o);
});
info();
}
function info() {
var c = calls.filter(function (x) { return x.operation === $("oap-op").value; })[0];
if (!c) { $("oap-info").textContent = ""; return; }
$("oap-info").textContent = "ids: " + (c.ids.join(", ") || "none") + "; body: " +
(c.body_fields.join(", ") || "none") + "; required: " + (c.required.join(", ") || "none") +
"; query: " + (c.query.join(", ") || "none") + "; " + c.flags.join(", ");
}
fetch(base + "/calls").then(function (r) { return r.json(); }).then(function (d) {
calls = d.calls;
var areas = [];
calls.forEach(function (c) { if (areas.indexOf(c.area) < 0) areas.push(c.area); });
areas.forEach(function (a) {
var o = document.createElement("option");
o.value = a; o.textContent = a;
$("oap-area").appendChild(o);
});
fillOps();
});
$("oap-area").onchange = fillOps;
$("oap-op").onchange = info;
$("oap-send").onclick = function () {
var ids = json("oap-ids"), query = json("oap-query"), body = json("oap-body");
if (!ids || !query || !body) return;
post("/call", { operation: $("oap-op").value, ids: ids, query: query, body: body, confirm: $("oap-confirm").checked });
};
$("oap-ebuild").onclick = function () {
var f = json("oap-efields");
if (f) post("/realtime/event", { profile: $("oap-profile").value, type: $("oap-etype").value.trim(), fields: f });
};
$("oap-ecatalog").onclick = function () { post("/realtime/catalog", { profile: $("oap-profile").value }); };
$("oap-eparse").onclick = function () { post("/realtime/parse", { profile: $("oap-profile").value, text: $("oap-etext").value }); };
})();
+201
View File
@@ -0,0 +1,201 @@
# OpenAI platform (src/openai/platform)
Namespace `molo::openai::platform`. The platform services of the OpenAI API
at `https://api.openai.com/v1`, everything except the model endpoints
(`src/openai/core`: responses, chat, completions, embeddings, images, audio,
videos, moderations, models) and the organization endpoints
(`src/openai/admin`: organization, projects, webhook endpoints). Status
2026-10-24. Credential env `OPENAI_API_KEY`; optional `OPENAI_ORG_ID` and
`OPENAI_PROJECT_ID` become `OpenAI-Organization` / `OpenAI-Project`.
## Scope
| Area | Calls | Class |
|---|---|---|
| Files | 5 | `FilesApi` |
| Uploads (parts) | 4 | `UploadsApi`, `UploadPlan` |
| Batch | 4 | `BatchesApi`, `BatchInput` (JSONL builder) |
| Fine-tuning (jobs, events, checkpoints, permissions, alpha graders) | 13 | `FineTuningJobsApi`, `FineTuningPermissionsApi`, `GradersApi` |
| Vector stores (CRUD, search, files, file batches, content) | 16 | `VectorStoresApi`, `VectorStoreFilesApi`, `VectorStoreFileBatchesApi` |
| Evals (CRUD, runs, output items) | 12 | `EvalsApi`, `EvalRunsApi` |
| Conversations (CRUD, items) | 8 | `ConversationsApi`, `ConversationItemsApi` |
| Containers (CRUD, files, content) | 9 | `ContainersApi`, `ContainerFilesApi` |
| Assistants / threads / messages / runs / steps (legacy beta) | 23 | `AssistantsApi`, `ThreadsApi`, `MessagesApi`, `RunsApi` |
| ChatKit (beta) | 6 | `ChatKitApi` |
| Agents (agents, automations, environments, templates, sessions, vaults) | 47 + 10 | `AgentsApi`, `AgentAutomationsApi`, `AgentEnvironmentsApi`, `AgentSessionsApi`, `VaultsApi` |
| Skills | 11 | `SkillsApi` |
| Realtime REST (client secrets, legacy sessions, SIP / WebRTC calls) | 9 | `RealtimeApi` |
| Live REST | 7 | `LiveApi` |
| Safety (read), decisions, content provenance | 2 + 1 + 1 | `SafetyApi`, `DecisionsApi`, `ContentProvenanceApi` |
| WebSocket sessions: Realtime GA, beta, translation; Live primary, sideband, fork | 6 endpoints, 217 events | `EventSocket`, `Events` |
188 REST rows. The spec has 371 operations; the other 183 belong to core and
admin (explicit prefix list in `tools/check_spec.py`). Skills, vaults,
safety, decisions and content provenance are in the spec but in no work
package of plan 17; they are covered here so that no spec operation is
unowned. Voices and voice consents (audio) belong to core.
## Module map
| File | Concern |
|---|---|
| `calls_table.inc`, `calls.h/.cpp` | the one call table; ids, body, query, form checks; path and query rendering |
| `events_table.inc`, `events.h/.cpp` | the 217-event catalogs of six profiles; client event builder, server event parser |
| `ws_table.inc`, `event_socket.h/.cpp` | six WebSocket endpoints; `EventSocket` over `google::live::WsClient` |
| `service.h/.cpp` | `PlatformService`: `prepare`, `invoke`, `invoke_raw`, `invoke_form`, `invoke_stream`, `list_all` |
| `form.h/.cpp` | multipart builder with OpenAI bracket notation and caps |
| `*_api.h/.cpp` | one file pair per area (table above) |
| `platform.h/.cpp` | `Platform` (all services on one `llm::ApiClient`), `PlatformConfig` |
| `caps.h` | every ceiling |
| `tools/check_spec.py` | spec and SDK conformance checker |
## Contracts
* Every call is a row of `calls_table.inc`: method, path, operation id, area,
request kinds, response kinds, response schema, body schema, every
top-level request field, required fields, query parameters, flags
(`kDestructive`, `kPaid`, `kBetaAssistants`, `kBetaChatkit`,
`kIdempotency`, `kStreamable`, `kList`, `kDeprecated`, `kBodyUnion`).
* Validation is local and runs before the credential check: ids must be one
path segment of `[A-Za-z0-9._:-]` (1..256, not `.`/`..`), body fields must
be in the row (unknown field: 400), required fields must be present, query
keys must be in the row, `limit` 1..10000, query values 4096 bytes, 32
parameters, form fields 64. Nested fields are passed through; the API
validates them.
* No key: `llm::NotConfiguredError` (503 `NOT_CONFIGURED`) with no traffic.
* `OpenAI-Beta: assistants=v2` on every Assistants row,
`chatkit_beta=v1` on every ChatKit row, `Idempotency-Key` only on rows
with `kIdempotency` (agent environment create, agent session events).
* Lists: `list_all` walks `after` / `has_more` / `last_id`, stops at
`max_pages` (default 5, hard 100) or 100,000 items and reports
`truncated`, `has_more`, `stop_reason` (`page_cap`, `item_cap`,
`no_cursor`). Never silent.
* Destructive calls are separate methods with no default (`remove`,
`cancel`, `reject_call`, `hangup_call`); the demo makes them dry runs.
* Streaming: runs, create-thread-and-run, submit-tool-outputs and agent
session create stream SSE through `llm::SseDecoder` (`invoke_stream` sets
`stream: true`). `GET /agents/sessions/{id}/events` is SSE too but
`llm::ApiClient` streams POST only: `events_raw` returns the capped body.
* WebSocket: `EventSocket::connect` needs the key (503 otherwise), sends
`Authorization: Bearer` (plus beta / org / project headers), reuses
`google::live::WsClient`. `send` accepts only client events of the profile;
`run` stops on sink, close, timeout (1..3600 s), 20,000 events or 128 MiB.
A refused upgrade maps to its HTTP status; messages never carry the key.
Inbound frames are capped at 4 MiB by `WsClient`.
* Event ids: `event_id` at most 512 bytes (spec `maxLength`).
## Caps (`caps.h`)
| Cap | Value | Origin |
|---|---|---|
| file / upload total | 512 MiB | spec (file max 512 MB); module total cap |
| upload part | 64 MiB, at most 10,000 parts | spec (64 MB part); parts cap is a module choice |
| batch input | 50,000 requests, 200 MB, custom_id 512 B | spec (50,000 / 200 MB); id cap is a module choice |
| conversation items per call | 20 | spec |
| list pages / items | 5 default, 100 / 100,000 | module |
| JSON request | 32 MiB (`llm::Caps`) | module |
| client event | 2 MiB; audio chunk 1 MiB raw | module |
| session | 3600 s, 20,000 events, 128 MiB | module |
## Sources
1. OpenAI OpenAPI document as shipped with the official Python SDK:
`openai/openai-python`, `api_reference/openapi.transformed.yml`
(OpenAPI 3.1, `info.version` 2.3.0, 237 paths, 371 operations, 2171
schemas; sha256 612c68096ce5c31416f6c8383cf4f66a297ba8da11b5c27c9e16e9eefd3a35da
on 2026-10-24). The public `openai/openai-openapi` repo (`manual_spec`,
last commit 2025-04-29) is stale: it lacks conversations, containers,
skills, agents, live, chatkit, vaults and more, so it was not used.
2. Official Python SDK `openai/openai-python` `main`: `api.md` plus
`src/openai/resources/{realtime,conversations,live}/api.md` (endpoint
listing: 186 of the 188 rows; the two missing are the legacy
`/realtime/sessions` and `/realtime/transcription_sessions`, spec only),
`resources/beta/assistants.py` (beta header), `resources/beta/chatkit/*.py`
(beta header and fields), `resources/realtime/realtime.py`,
`resources/realtime/translations/translations.py`,
`resources/live/{live,sideband,forks}.py`, `resources/beta/realtime/realtime.py`
(WebSocket URLs, query parameters, `OpenAI-Beta: realtime=v1`).
3. Unauthenticated probes (below). `.stats.yml` of the SDK repos is gone
(`.castiron.stats.yml` remains, without a spec URL).
4. platform.openai.com guides were not read; every figure comes from the
spec text.
## Verified against spec
`python3 -I src/openai/platform/tools/check_spec.py` (fetches the spec and
the SDK files; `--spec FILE` for a local copy), run 2026-10-24:
```
spec: OpenAPI 2.3.0, sha256 612c6809...e3a35da
spec operations: 371 total, 188 in table, 183 excluded (core/admin), 0 uncovered
table rows: 188 REST (186 also in the SDK listing), 217 events, 6 websocket endpoints
OK
```
It checks per row: method, path, operation id, area, path parameters,
request field set (equality, both directions), required set, body schema
name, query parameter set, response schema name, request and response kinds,
`kList` / `kDeprecated` / `kIdempotency` / `kStreamable` / `kDestructive` /
`kBodyUnion` / `kBetaAssistants` flags; completeness (every operation is in
the table or under an excluded prefix); the event catalogs of six profiles
against the spec (type, field set, required set, both directions); the
WebSocket paths, query names and beta header against the SDK source. The
checker carries its own YAML subset reader; on the full spec it equals
PyYAML except two scalar spellings (`1e-05`, a `|2+` block).
A mutated table (renamed field, dropped row) makes it exit 1.
## Verified live
Unauthenticated probes against `https://api.openai.com/v1`, 2026-10-24
(host resolves, TLS verifies, HTTP/2):
* Every REST row probed without credentials with placeholder ids
(188 requests): 96 answer 401 (`missing_authorization`, error body
`{"error":{"message","type":"invalid_request_error","code","param"}}`
plus a `detail` copy), 58 answer 400, 34 answer 404.
* 404 groups: all Assistants and Threads rows (also with
`OpenAI-Beta: assistants=v2`), `/live/sessions/{id}/*` (6), `/agents/{id}`
(3), the two legacy realtime session creators. The Assistants 404 is
consistent with the announced Assistants API sunset; this is not
confirmed by an authenticated call. `/live/sessions` (create) and
`/agents` (list) answer 401.
* No key exists here: no authenticated call has been made. The opt-in live
tests (`tests/src/openai/platform/test_live.cpp`) need `MOLO_LIVE_NET=1`
and `OPENAI_API_KEY` and only list with `limit=1` (files, batches,
fine-tuning jobs, vector stores, evals, containers); otherwise they print
a loud SKIP. Nothing is ever created.
## Unverified
* All response shapes (no authenticated call). Responses are returned as
`Json`; only `data` / `has_more` / `last_id` / `id` are read.
* Multipart `session` part of `POST /realtime/calls` is sent as a JSON text
part; the spec lists the field but not its part encoding.
* Multipart arrays use `files[]` and objects use `a[b]` (the SDK encoding),
not stated in the spec.
* The spec's `RealtimeBeta*` server event schemas carry the GA names
(`response.output_audio.delta`), not the preview names
(`response.audio.delta`); the beta profile follows the spec.
* Client event limits (2 MiB, audio 1 MiB) and session limits are module
choices, not OpenAI figures.
* Which of the `/agents`, `/vaults`, `/skills`, `/live`, `/chatkit`,
`/decisions`, `/safety` endpoints are enabled for a given account.
* Paid / destructive flags are this module's judgement (spec has no cost
field).
* The Live WebSocket protocol beyond event names and fields (ordering,
`session.start` before other events, sideband attach) is read from the
spec text, not exercised.
## Gaps
* `GET /agents/sessions/{id}/events` cannot stream (`llm::ApiClient::stream`
is POST only; foundation gap, `src/llm/api_client.h`).
* No streaming for `invoke_raw`; downloads are buffered up to 64 MiB
(`llm::Caps::kMaxResponseBytes`).
* `llm::ApiClient::post_multipart` always parses JSON, so the Realtime call
with a multipart body goes through `request_raw`.
* No typed request structs for nested bodies (tools, graders, data sources,
agent definitions): the top level is checked, the rest is passed through.
* WebRTC media (SDP offer generation, RTP) is out of scope; only the
signalling endpoints are covered.
* No automatic reconnect in `EventSocket`.
+146
View File
@@ -0,0 +1,146 @@
#include "openai/platform/agents_api.h"
namespace molo::openai::platform {
namespace {
llm::Headers idem(const std::string& key) {
llm::Headers h;
if (!key.empty()) h["Idempotency-Key"] = key;
return h;
}
} // namespace
Json AgentsApi::create(const Json& body) { return invoke(Op::kCreateAgent, {}, &body); }
Json AgentsApi::retrieve(const std::string& id) { return invoke(Op::kRetrieveAgent, {{"agent_id", id}}); }
Json AgentsApi::update(const std::string& id, const Json& body) {
return invoke(Op::kUpdateAgent, {{"agent_id", id}}, &body);
}
Json AgentsApi::list(const Json& query) { return invoke(Op::kListAgents, {}, nullptr, query); }
Json AgentsApi::remove(const std::string& id) { return invoke(Op::kDeleteAgent, {{"agent_id", id}}); }
Json AgentAutomationsApi::create(const Json& body) { return invoke(Op::kCreateAgentAutomation, {}, &body); }
Json AgentAutomationsApi::retrieve(const std::string& id) {
return invoke(Op::kGetAgentAutomation, {{"automation_id", id}});
}
Json AgentAutomationsApi::update(const std::string& id, const Json& body) {
return invoke(Op::kUpdateAgentAutomation, {{"automation_id", id}}, &body);
}
Json AgentAutomationsApi::list(const Json& query) { return invoke(Op::kListAgentAutomations, {}, nullptr, query); }
Json AgentAutomationsApi::remove(const std::string& id) {
return invoke(Op::kDeleteAgentAutomation, {{"automation_id", id}});
}
Json AgentAutomationsApi::pause(const std::string& id) {
return invoke(Op::kPauseAgentAutomation, {{"automation_id", id}});
}
Json AgentAutomationsApi::resume(const std::string& id) {
return invoke(Op::kResumeAgentAutomation, {{"automation_id", id}});
}
Json AgentAutomationsApi::invoke_now(const std::string& id) {
return invoke(Op::kCreateAgentAutomationInvocation, {{"automation_id", id}});
}
Json AgentAutomationsApi::invocations(const std::string& id, const Json& query) {
return invoke(Op::kListAgentAutomationInvocations, {{"automation_id", id}}, nullptr, query);
}
Json AgentAutomationsApi::invocation(const std::string& id, const std::string& invocation_id) {
return invoke(Op::kGetAgentAutomationInvocation, {{"automation_id", id}, {"invocation_id", invocation_id}});
}
Json AgentEnvironmentsApi::create(const Json& body, const std::string& key) {
return invoke(Op::kCreateAgentEnvironment, {}, &body, Json::object(), idem(key));
}
Json AgentEnvironmentsApi::retrieve(const std::string& id) {
return invoke(Op::kRetrieveAgentEnvironment, {{"environment_id", id}});
}
Json AgentEnvironmentsApi::list(const Json& query) { return invoke(Op::kListAgentEnvironments, {}, nullptr, query); }
Json AgentEnvironmentsApi::create_file(const std::string& id, const Json& body) {
return invoke(Op::kCreateAgentEnvironmentFile, {{"environment_id", id}}, &body);
}
Json AgentEnvironmentsApi::list_files(const std::string& id, const Json& query) {
return invoke(Op::kListAgentEnvironmentFiles, {{"environment_id", id}}, nullptr, query);
}
Json AgentEnvironmentsApi::create_template(const Json& body) {
return invoke(Op::kCreateAgentEnvironmentTemplate, {}, &body);
}
Json AgentEnvironmentsApi::retrieve_template(const std::string& id) {
return invoke(Op::kRetrieveAgentEnvironmentTemplate, {{"environment_template_id", id}});
}
Json AgentEnvironmentsApi::update_template(const std::string& id, const Json& body) {
return invoke(Op::kUpdateAgentEnvironmentTemplate, {{"environment_template_id", id}}, &body);
}
Json AgentEnvironmentsApi::list_templates(const Json& query) {
return invoke(Op::kListAgentEnvironmentTemplates, {}, nullptr, query);
}
Json AgentEnvironmentsApi::remove_template(const std::string& id) {
return invoke(Op::kDeleteAgentEnvironmentTemplate, {{"environment_template_id", id}});
}
Json AgentSessionsApi::create(const Json& body) { return invoke(Op::kCreateAgentSession, {}, &body); }
void AgentSessionsApi::create_stream(const Json& body, const llm::SseDecoder::Sink& sink) {
invoke_stream(Op::kCreateAgentSession, {}, body, sink);
}
Json AgentSessionsApi::retrieve(const std::string& id) {
return invoke(Op::kRetrieveAgentSession, {{"session_id", id}});
}
Json AgentSessionsApi::update(const std::string& id, const Json& body) {
return invoke(Op::kUpdateAgentSession, {{"session_id", id}}, &body);
}
Json AgentSessionsApi::list(const Json& query) { return invoke(Op::kListAgentSessions, {}, nullptr, query); }
Json AgentSessionsApi::remove(const std::string& id) { return invoke(Op::kDeleteAgentSession, {{"session_id", id}}); }
Json AgentSessionsApi::send_events(const std::string& id, const Json& body, const std::string& key) {
return invoke(Op::kCreateAgentSessionEvents, {{"session_id", id}}, &body, Json::object(), idem(key));
}
llm::RawResponse AgentSessionsApi::events_raw(const std::string& id, const Json& query) {
return invoke_raw(Op::kListAgentSessionEvents, {{"session_id", id}}, query);
}
Json AgentSessionsApi::items(const std::string& id, const Json& query) {
return invoke(Op::kListAgentSessionItems, {{"session_id", id}}, nullptr, query);
}
Json AgentSessionsApi::traces(const std::string& id, const Json& query) {
return invoke(Op::kListAgentSessionTraces, {{"session_id", id}}, nullptr, query);
}
Json AgentSessionsApi::turns(const std::string& id, const Json& query) {
return invoke(Op::kListAgentSessionTurns, {{"session_id", id}}, nullptr, query);
}
Json AgentSessionsApi::turn(const std::string& id, const std::string& turn_id) {
return invoke(Op::kRetrieveAgentSessionTurn, {{"session_id", id}, {"turn_id", turn_id}});
}
Json AgentSessionsApi::turn_items(const std::string& id, const std::string& turn_id, const Json& query) {
return invoke(Op::kListAgentSessionTurnItems, {{"session_id", id}, {"turn_id", turn_id}}, nullptr, query);
}
Json AgentSessionsApi::subagents(const std::string& id, const Json& query) {
return invoke(Op::kListAgentSessionSubagents, {{"session_id", id}}, nullptr, query);
}
Json AgentSessionsApi::subagent(const std::string& id, const std::string& sub) {
return invoke(Op::kRetrieveAgentSessionSubagent, {{"session_id", id}, {"subagent_id", sub}});
}
Json AgentSessionsApi::subagent_items(const std::string& id, const std::string& sub, const Json& query) {
return invoke(Op::kListAgentSessionSubagentItems, {{"session_id", id}, {"subagent_id", sub}}, nullptr, query);
}
Json AgentSessionsApi::subagent_turns(const std::string& id, const std::string& sub, const Json& query) {
return invoke(Op::kListAgentSessionSubagentTurns, {{"session_id", id}, {"subagent_id", sub}}, nullptr, query);
}
Json AgentSessionsApi::subagent_turn(const std::string& id, const std::string& sub, const std::string& turn_id) {
return invoke(Op::kRetrieveAgentSessionSubagentTurn,
{{"session_id", id}, {"subagent_id", sub}, {"turn_id", turn_id}});
}
Json AgentSessionsApi::subagent_turn_items(const std::string& id, const std::string& sub,
const std::string& turn_id, const Json& query) {
return invoke(Op::kListAgentSessionSubagentTurnItems,
{{"session_id", id}, {"subagent_id", sub}, {"turn_id", turn_id}}, nullptr, query);
}
Json AgentSessionsApi::artifacts(const std::string& id, const Json& query) {
return invoke(Op::kListAgentSessionArtifacts, {{"session_id", id}}, nullptr, query);
}
Json AgentSessionsApi::artifact(const std::string& id, const std::string& artifact_id) {
return invoke(Op::kRetrieveAgentSessionArtifact, {{"session_id", id}, {"artifact_id", artifact_id}});
}
std::string AgentSessionsApi::artifact_content(const std::string& id, const std::string& artifact_id) {
return invoke_raw(Op::kRetrieveAgentSessionArtifactContent, {{"session_id", id}, {"artifact_id", artifact_id}})
.body;
}
Json AgentSessionsApi::remove_artifact(const std::string& id, const std::string& artifact_id) {
return invoke(Op::kDeleteAgentSessionArtifact, {{"session_id", id}, {"artifact_id", artifact_id}});
}
} // namespace molo::openai::platform
+111
View File
@@ -0,0 +1,111 @@
#pragma once
// Agents platform (the agent-builder REST surface the spec publishes under
// /agents): agent definitions, automations (scheduled runs and invocations),
// environments (with templates and files), and sessions (events, items,
// turns, subagents, traces, artifacts). Vaults are in vaults_api.h.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class AgentsApi : public PlatformService {
public:
using PlatformService::PlatformService;
Json create(const Json& body); // model (required), name, instructions, tools, ...
Json retrieve(const std::string& agent_id);
Json update(const std::string& agent_id, const Json& body);
Json list(const Json& query = Json::object());
// Destructive.
Json remove(const std::string& agent_id);
};
class AgentAutomationsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// The body is a oneOf (schedule kinds): fields are checked against the
// union of all of them.
Json create(const Json& body);
Json retrieve(const std::string& automation_id);
Json update(const std::string& automation_id, const Json& body);
Json list(const Json& query = Json::object());
// Destructive.
Json remove(const std::string& automation_id);
Json pause(const std::string& automation_id);
Json resume(const std::string& automation_id);
// Paid: starts one run of the automation now.
Json invoke_now(const std::string& automation_id);
Json invocations(const std::string& automation_id, const Json& query = Json::object());
Json invocation(const std::string& automation_id, const std::string& invocation_id);
};
class AgentEnvironmentsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: environment (required), vault_ids. idempotency_key optional.
Json create(const Json& body, const std::string& idempotency_key = "");
Json retrieve(const std::string& environment_id);
Json list(const Json& query = Json::object());
// Files inside an environment. Paid. Body is a oneOf (data / file_id).
Json create_file(const std::string& environment_id, const Json& body);
Json list_files(const std::string& environment_id, const Json& query = Json::object());
// Templates.
Json create_template(const Json& body); // paid
Json retrieve_template(const std::string& template_id);
Json update_template(const std::string& template_id, const Json& body);
Json list_templates(const Json& query = Json::object());
// Destructive.
Json remove_template(const std::string& template_id);
};
class AgentSessionsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: environment (required), agent or agent_id, input, vault_ids,
// spend_control, metadata.
Json create(const Json& body);
// Same call as server-sent events ("stream": true is set).
void create_stream(const Json& body, const llm::SseDecoder::Sink& sink);
Json retrieve(const std::string& session_id);
Json update(const std::string& session_id, const Json& body);
Json list(const Json& query = Json::object());
// Destructive.
Json remove(const std::string& session_id);
// Paid. body: events (required). idempotency_key optional.
Json send_events(const std::string& session_id, const Json& body, const std::string& idempotency_key = "");
// The spec answers text/event-stream here. This reads the whole response
// (capped by llm::Caps) and returns it undecoded; a GET stream is not in
// llm::ApiClient. Decode with llm::SseDecoder.
llm::RawResponse events_raw(const std::string& session_id, const Json& query = Json::object());
Json items(const std::string& session_id, const Json& query = Json::object());
Json traces(const std::string& session_id, const Json& query = Json::object());
Json turns(const std::string& session_id, const Json& query = Json::object());
Json turn(const std::string& session_id, const std::string& turn_id);
Json turn_items(const std::string& session_id, const std::string& turn_id, const Json& query = Json::object());
Json subagents(const std::string& session_id, const Json& query = Json::object());
Json subagent(const std::string& session_id, const std::string& subagent_id);
Json subagent_items(const std::string& session_id, const std::string& subagent_id,
const Json& query = Json::object());
Json subagent_turns(const std::string& session_id, const std::string& subagent_id,
const Json& query = Json::object());
Json subagent_turn(const std::string& session_id, const std::string& subagent_id, const std::string& turn_id);
Json subagent_turn_items(const std::string& session_id, const std::string& subagent_id,
const std::string& turn_id, const Json& query = Json::object());
Json artifacts(const std::string& session_id, const Json& query = Json::object());
Json artifact(const std::string& session_id, const std::string& artifact_id);
std::string artifact_content(const std::string& session_id, const std::string& artifact_id);
// Destructive.
Json remove_artifact(const std::string& session_id, const std::string& artifact_id);
};
} // namespace molo::openai::platform
+97
View File
@@ -0,0 +1,97 @@
#include "openai/platform/assistants_api.h"
namespace molo::openai::platform {
Json AssistantsApi::create(const Json& body) { return invoke(Op::kCreateAssistant, {}, &body); }
Json AssistantsApi::retrieve(const std::string& id) { return invoke(Op::kGetAssistant, {{"assistant_id", id}}); }
Json AssistantsApi::modify(const std::string& id, const Json& body) {
return invoke(Op::kModifyAssistant, {{"assistant_id", id}}, &body);
}
Json AssistantsApi::list(const Json& query) { return invoke(Op::kListAssistants, {}, nullptr, query); }
Json AssistantsApi::remove(const std::string& id) { return invoke(Op::kDeleteAssistant, {{"assistant_id", id}}); }
Json ThreadsApi::create(const Json& body) { return invoke(Op::kCreateThread, {}, &body); }
Json ThreadsApi::retrieve(const std::string& id) { return invoke(Op::kGetThread, {{"thread_id", id}}); }
Json ThreadsApi::modify(const std::string& id, const Json& body) {
return invoke(Op::kModifyThread, {{"thread_id", id}}, &body);
}
Json ThreadsApi::remove(const std::string& id) { return invoke(Op::kDeleteThread, {{"thread_id", id}}); }
Json ThreadsApi::create_and_run(const Json& body) { return invoke(Op::kCreateThreadAndRun, {}, &body); }
void ThreadsApi::create_and_run_stream(const Json& body, const llm::SseDecoder::Sink& sink) {
invoke_stream(Op::kCreateThreadAndRun, {}, body, sink);
}
Json MessagesApi::create(const std::string& thread_id, const Json& body) {
return invoke(Op::kCreateMessage, {{"thread_id", thread_id}}, &body);
}
Json MessagesApi::retrieve(const std::string& thread_id, const std::string& message_id) {
return invoke(Op::kGetMessage, {{"thread_id", thread_id}, {"message_id", message_id}});
}
Json MessagesApi::modify(const std::string& thread_id, const std::string& message_id, const Json& body) {
return invoke(Op::kModifyMessage, {{"thread_id", thread_id}, {"message_id", message_id}}, &body);
}
Json MessagesApi::list(const std::string& thread_id, const Json& query) {
return invoke(Op::kListMessages, {{"thread_id", thread_id}}, nullptr, query);
}
Json MessagesApi::remove(const std::string& thread_id, const std::string& message_id) {
return invoke(Op::kDeleteMessage, {{"thread_id", thread_id}, {"message_id", message_id}});
}
Json RunsApi::create(const std::string& thread_id, const Json& body, const Json& query) {
return invoke(Op::kCreateRun, {{"thread_id", thread_id}}, &body, query);
}
void RunsApi::create_stream(const std::string& thread_id, const Json& body, const llm::SseDecoder::Sink& sink,
const Json& query) {
invoke_stream(Op::kCreateRun, {{"thread_id", thread_id}}, body, sink, query);
}
Json RunsApi::retrieve(const std::string& thread_id, const std::string& run_id) {
return invoke(Op::kGetRun, {{"thread_id", thread_id}, {"run_id", run_id}});
}
Json RunsApi::modify(const std::string& thread_id, const std::string& run_id, const Json& body) {
return invoke(Op::kModifyRun, {{"thread_id", thread_id}, {"run_id", run_id}}, &body);
}
Json RunsApi::list(const std::string& thread_id, const Json& query) {
return invoke(Op::kListRuns, {{"thread_id", thread_id}}, nullptr, query);
}
Json RunsApi::cancel(const std::string& thread_id, const std::string& run_id) {
return invoke(Op::kCancelRun, {{"thread_id", thread_id}, {"run_id", run_id}});
}
Json RunsApi::submit_tool_outputs(const std::string& thread_id, const std::string& run_id, const Json& body) {
return invoke(Op::kSubmitToolOuputsToRun, {{"thread_id", thread_id}, {"run_id", run_id}}, &body);
}
void RunsApi::submit_tool_outputs_stream(const std::string& thread_id, const std::string& run_id,
const Json& body, const llm::SseDecoder::Sink& sink) {
invoke_stream(Op::kSubmitToolOuputsToRun, {{"thread_id", thread_id}, {"run_id", run_id}}, body, sink);
}
Json RunsApi::steps(const std::string& thread_id, const std::string& run_id, const Json& query) {
return invoke(Op::kListRunSteps, {{"thread_id", thread_id}, {"run_id", run_id}}, nullptr, query);
}
Json RunsApi::step(const std::string& thread_id, const std::string& run_id, const std::string& step_id,
const Json& query) {
return invoke(Op::kGetRunStep, {{"thread_id", thread_id}, {"run_id", run_id}, {"step_id", step_id}}, nullptr,
query);
}
} // namespace molo::openai::platform
+73
View File
@@ -0,0 +1,73 @@
#pragma once
// Assistants API (legacy beta, every row deprecated in the spec; every call
// sends OpenAI-Beta: assistants=v2): assistants, threads, messages, runs and
// run steps. Runs can be streamed as server-sent events.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class AssistantsApi : public PlatformService {
public:
using PlatformService::PlatformService;
Json create(const Json& body); // model (required) + spec fields
Json retrieve(const std::string& assistant_id);
Json modify(const std::string& assistant_id, const Json& body);
Json list(const Json& query = Json::object());
// Destructive.
Json remove(const std::string& assistant_id);
};
class ThreadsApi : public PlatformService {
public:
using PlatformService::PlatformService;
Json create(const Json& body = Json::object()); // messages, metadata, tool_resources
Json retrieve(const std::string& thread_id);
Json modify(const std::string& thread_id, const Json& body);
// Destructive.
Json remove(const std::string& thread_id);
// Paid. assistant_id (required) + thread + spec fields.
Json create_and_run(const Json& body);
void create_and_run_stream(const Json& body, const llm::SseDecoder::Sink& sink);
};
class MessagesApi : public PlatformService {
public:
using PlatformService::PlatformService;
Json create(const std::string& thread_id, const Json& body); // role, content (required)
Json retrieve(const std::string& thread_id, const std::string& message_id);
Json modify(const std::string& thread_id, const std::string& message_id, const Json& body);
Json list(const std::string& thread_id, const Json& query = Json::object());
// Destructive.
Json remove(const std::string& thread_id, const std::string& message_id);
};
class RunsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. assistant_id (required) + spec fields; query: include[].
Json create(const std::string& thread_id, const Json& body, const Json& query = Json::object());
void create_stream(const std::string& thread_id, const Json& body, const llm::SseDecoder::Sink& sink,
const Json& query = Json::object());
Json retrieve(const std::string& thread_id, const std::string& run_id);
Json modify(const std::string& thread_id, const std::string& run_id, const Json& body); // metadata
Json list(const std::string& thread_id, const Json& query = Json::object());
// Destructive.
Json cancel(const std::string& thread_id, const std::string& run_id);
// Paid. body: tool_outputs (required).
Json submit_tool_outputs(const std::string& thread_id, const std::string& run_id, const Json& body);
void submit_tool_outputs_stream(const std::string& thread_id, const std::string& run_id, const Json& body,
const llm::SseDecoder::Sink& sink);
Json steps(const std::string& thread_id, const std::string& run_id, const Json& query = Json::object());
Json step(const std::string& thread_id, const std::string& run_id, const std::string& step_id,
const Json& query = Json::object());
};
} // namespace molo::openai::platform
+68
View File
@@ -0,0 +1,68 @@
#include "openai/platform/batches_api.h"
#include <algorithm>
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
namespace {
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
const char* const kEndpoints[] = {"/v1/responses", "/v1/chat/completions",
"/v1/embeddings", "/v1/completions",
"/v1/moderations", "/v1/images/generations",
"/v1/images/edits", "/v1/videos"};
} // namespace
bool BatchInput::valid_endpoint(const std::string& e) {
return std::any_of(std::begin(kEndpoints), std::end(kEndpoints), [&](const char* k) { return e == k; });
}
BatchInput::BatchInput(const std::string& endpoint) : endpoint_(endpoint) {
if (!valid_endpoint(endpoint)) bad("unsupported batch endpoint '" + endpoint + "'");
}
BatchInput& BatchInput::add(const std::string& custom_id, const clients::body::Json& body) {
if (custom_id.empty() || custom_id.size() > Caps::kMaxCustomIdBytes) bad("custom_id must be 1 to 512 bytes");
if (!body.is_object()) bad("request body must be a JSON object");
if (ids_.size() >= Caps::kMaxBatchRequests) bad("batch over 50,000 requests");
if (ids_.count(custom_id)) bad("custom_id '" + custom_id + "' is used twice");
clients::body::Json line = clients::body::Json::object();
line.set("custom_id", clients::body::Json(custom_id));
line.set("method", clients::body::Json("POST"));
line.set("url", clients::body::Json(endpoint_));
line.set("body", body);
std::string text = line.dump();
if (jsonl_.size() + text.size() + 1 > Caps::kMaxBatchFileBytes) bad("batch file over 200 MB");
ids_.insert(custom_id);
jsonl_ += text;
jsonl_.push_back('\n');
return *this;
}
Json BatchesApi::create(const std::string& input_file_id, const std::string& endpoint, const Json* metadata,
const Json* output_expires_after) {
if (!BatchInput::valid_endpoint(endpoint)) bad("unsupported batch endpoint '" + endpoint + "'");
check_id("file", input_file_id);
Json body = Json::object();
body.set("input_file_id", Json(input_file_id));
body.set("endpoint", Json(endpoint));
body.set("completion_window", Json("24h"));
if (metadata && !metadata->is_null()) body.set("metadata", *metadata);
if (output_expires_after && !output_expires_after->is_null())
body.set("output_expires_after", *output_expires_after);
return invoke(Op::kCreateBatch, {}, &body);
}
Json BatchesApi::retrieve(const std::string& batch_id) {
return invoke(Op::kRetrieveBatch, {{"batch_id", batch_id}});
}
Json BatchesApi::list(const Json& query) { return invoke(Op::kListBatches, {}, nullptr, query); }
Json BatchesApi::cancel(const std::string& batch_id) {
return invoke(Op::kCancelBatch, {{"batch_id", batch_id}});
}
} // namespace molo::openai::platform
+50
View File
@@ -0,0 +1,50 @@
#pragma once
// Batch: create, retrieve, list, cancel. BatchInput builds the JSONL input
// file the Batch API requires (one request per line, unique custom_id, all
// lines for the same endpoint, at most 50,000 requests and 200 MB).
#include <set>
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class BatchInput {
public:
// endpoint: /v1/responses, /v1/chat/completions, /v1/embeddings,
// /v1/completions, /v1/moderations, /v1/images/generations,
// /v1/images/edits, /v1/videos.
explicit BatchInput(const std::string& endpoint);
static bool valid_endpoint(const std::string& endpoint);
// Throws llm::ApiError::invalid on an empty / repeated / over long
// custom_id, a non-object body, or when a cap would be passed.
BatchInput& add(const std::string& custom_id, const clients::body::Json& body);
const std::string& endpoint() const { return endpoint_; }
std::size_t size() const { return ids_.size(); }
const std::string& jsonl() const { return jsonl_; }
private:
std::string endpoint_;
std::set<std::string> ids_;
std::string jsonl_;
};
class BatchesApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid: runs the requests of the uploaded file (purpose "batch").
// completion_window is "24h" (the only value in the spec).
Json create(const std::string& input_file_id, const std::string& endpoint,
const Json* metadata = nullptr, const Json* output_expires_after = nullptr);
Json retrieve(const std::string& batch_id);
Json list(const Json& query = Json::object());
// Destructive: stops the batch.
Json cancel(const std::string& batch_id);
};
} // namespace molo::openai::platform
+231
View File
@@ -0,0 +1,231 @@
#include "openai/platform/calls.h"
#include <cmath>
#include <cstring>
#include "llm/api_client.h"
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
using clients::body::Json;
namespace {
// CALLS-BEGIN (parsed by tools/check_spec.py: calls_table.inc, one row per line)
const CallSpec kCalls[] = {
#define OAI_CALL(en, method, path, oid, area, req, resp, rs, bs, bp, br, q, flags) \
{Op::en, method, path, oid, area, req, resp, rs, bs, bp, br, q, flags},
#include "openai/platform/calls_table.inc"
#undef OAI_CALL
};
// CALLS-END
constexpr std::size_t kCallCount = sizeof(kCalls) / sizeof(kCalls[0]);
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
bool csv_has(const char* csv, const std::string& item) {
std::size_t n = item.size();
const char* p = csv;
while (*p) {
const char* end = std::strchr(p, ',');
std::size_t len = end ? static_cast<std::size_t>(end - p) : std::strlen(p);
if (len == n && std::strncmp(p, item.c_str(), n) == 0) return true;
if (!end) break;
p = end + 1;
}
return false;
}
std::string scalar_text(const Json& v, const std::string& key) {
std::string out;
if (v.is_string()) out = v.as_string();
else if (v.is_bool()) out = v.as_bool() ? "true" : "false";
else if (v.is_number()) {
double d = v.as_number();
if (d == std::floor(d) && std::fabs(d) < 9e15) out = std::to_string(static_cast<long long>(d));
else out = std::to_string(d);
} else {
bad("query value of '" + key + "' must be a string, number or boolean");
}
if (out.size() > Caps::kMaxQueryValueBytes) bad("query value of '" + key + "' is too long");
return out;
}
} // namespace
std::size_t Calls::count() { return kCallCount; }
const CallSpec& Calls::at(std::size_t index) {
if (index >= kCallCount) bad("call index out of range");
return kCalls[index];
}
const CallSpec& Calls::spec(Op op) { return kCalls[static_cast<std::size_t>(op)]; }
const CallSpec* Calls::find(std::string_view operation_id) {
for (const CallSpec& c : kCalls)
if (operation_id == c.operation_id) return &c;
return nullptr;
}
clients::Method Calls::method(const CallSpec& spec) {
if (std::strcmp(spec.method, "GET") == 0) return clients::Method::kGet;
if (std::strcmp(spec.method, "POST") == 0) return clients::Method::kPost;
if (std::strcmp(spec.method, "DELETE") == 0) return clients::Method::kDelete;
bad(std::string("unsupported method ") + spec.method);
}
std::vector<std::string> Calls::list(const char* csv) {
std::vector<std::string> out;
std::string cur;
for (const char* p = csv; *p; ++p) {
if (*p == ',') {
out.push_back(cur);
cur.clear();
} else {
cur.push_back(*p);
}
}
if (!cur.empty()) out.push_back(cur);
return out;
}
std::vector<std::string> Calls::placeholders(const CallSpec& spec) {
std::vector<std::string> out;
std::string p = spec.path;
for (std::size_t i = 0; i < p.size(); ++i) {
if (p[i] != '{') continue;
std::size_t j = p.find('}', i);
if (j == std::string::npos) break;
out.push_back(p.substr(i + 1, j - i - 1));
i = j;
}
return out;
}
std::string Calls::render(Op op, const PathArgs& args) {
const CallSpec& s = spec(op);
std::vector<std::string> names = placeholders(s);
if (args.size() != names.size())
bad(std::string(s.operation_id) + " takes " + std::to_string(names.size()) + " id(s)");
std::string out;
std::string p = s.path;
std::size_t at = 0;
for (const std::string& name : names) {
std::size_t open = p.find('{' + name + '}', at);
out += p.substr(at, open - at);
const std::string* value = nullptr;
for (const auto& [k, v] : args)
if (k == name) value = &v;
if (!value) bad(std::string(s.operation_id) + " needs '" + name + "'");
if (!llm::Service::valid_id(*value)) bad("bad " + name);
out += *value;
at = open + name.size() + 2;
}
for (const auto& [k, v] : args) {
bool known = false;
for (const std::string& n : names) known = known || n == k;
if (!known) bad(std::string(s.operation_id) + " has no id '" + k + "'");
}
out += p.substr(at);
return out;
}
void Calls::check_body(Op op, const Json* body) {
const CallSpec& s = spec(op);
bool takes = (s.req & kReqJson) != 0;
std::vector<std::string> required = list(s.required_fields);
if (!takes) {
if (body && !body->is_null()) bad(std::string(s.operation_id) + " takes no JSON body");
return;
}
if (!body || body->is_null()) {
if (!required.empty()) bad(std::string(s.operation_id) + " needs a body with '" + required[0] + "'");
return;
}
if (!body->is_object()) bad("body must be a JSON object");
for (const auto& [key, value] : body->as_object()) {
(void)value;
if (!csv_has(s.body_fields, key))
bad("field '" + key + "' is not a request field of " + s.operation_id);
}
for (const std::string& r : required)
if (!body->has(r)) bad("missing required field '" + r + "'");
if (body->dump().size() > Caps::kMaxBodyBytes) bad("request body over the byte cap");
}
void Calls::check_form(Op op, const std::vector<std::string>& names) {
const CallSpec& s = spec(op);
if ((s.req & kReqMultipart) == 0) bad(std::string(s.operation_id) + " does not take multipart");
if (names.size() > Caps::kMaxFormFields) bad("too many form fields");
std::vector<std::string> roots;
for (const std::string& n : names) {
std::string root = n.substr(0, n.find('['));
if (!csv_has(s.body_fields, root))
bad("form field '" + n + "' is not a request field of " + s.operation_id);
roots.push_back(root);
}
// A call that accepts JSON too (containers, skills) has no multipart-only
// required set: the JSON alternative may supply it.
if ((s.req & kReqJson) != 0) return;
for (const std::string& r : list(s.required_fields)) {
bool seen = false;
for (const std::string& n : roots) seen = seen || n == r;
if (!seen) bad("missing required form field '" + r + "'");
}
}
std::string Calls::percent_encode(const std::string& text) {
static const char* kHex = "0123456789ABCDEF";
std::string out;
for (unsigned char c : text) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' ||
c == '.' || c == '_' || c == '~') {
out.push_back(static_cast<char>(c));
} else {
out.push_back('%');
out.push_back(kHex[c >> 4]);
out.push_back(kHex[c & 15]);
}
}
return out;
}
std::string Calls::query(Op op, const Json& q) {
const CallSpec& s = spec(op);
if (q.is_null()) return "";
if (!q.is_object()) bad("query must be a JSON object");
std::string out;
std::size_t params = 0;
auto add = [&](const std::string& name, const std::string& value) {
if (++params > Caps::kMaxQueryParams) bad("too many query parameters");
out += out.empty() ? '?' : '&';
out += name; // brackets stay literal: "include[]=a"
out += '=';
out += percent_encode(value);
};
for (const auto& [key, value] : q.as_object()) {
if (!csv_has(s.query_params, key))
bad("query parameter '" + key + "' is not accepted by " + s.operation_id);
if (value.is_null()) continue;
if (value.is_array()) {
if (value.as_array().size() > Caps::kMaxQueryArrayItems) bad("query array '" + key + "' is too long");
for (const Json& item : value.as_array()) add(key, scalar_text(item, key));
} else if (value.is_object()) {
for (const auto& [sub, item] : value.as_object()) add(key + "[" + percent_encode(sub) + "]", scalar_text(item, key));
} else {
std::string text = scalar_text(value, key);
if (key == "limit") {
if (!value.is_number() || value.as_number() < 1 || value.as_number() > Caps::kMaxListLimit)
bad("limit must be a number from 1 to " + std::to_string(Caps::kMaxListLimit));
}
add(key, text);
}
}
return out;
}
} // namespace molo::openai::platform
+104
View File
@@ -0,0 +1,104 @@
#pragma once
// The ONE table of REST calls of this module (calls_table.inc). Every row is
// checked against OpenAI's OpenAPI document by tools/check_spec.py: method,
// path, operation id, request fields, required fields, query parameters and
// response schema. Code builds every URL, body check and query string from
// this table, never from a literal.
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "clients/body/json.h"
#include "clients/request.h"
namespace molo::openai::platform {
// CallSpec::flags
inline constexpr unsigned kNoFlags = 0;
inline constexpr unsigned kDestructive = 1u << 0; // delete / cancel: dry run by default
inline constexpr unsigned kPaid = 1u << 1; // can cost money or start billable work
inline constexpr unsigned kBetaAssistants = 1u << 2; // sends OpenAI-Beta: assistants=v2
inline constexpr unsigned kBetaChatkit = 1u << 3; // sends OpenAI-Beta: chatkit_beta=v1
inline constexpr unsigned kIdempotency = 1u << 4; // accepts an Idempotency-Key header
inline constexpr unsigned kStreamable = 1u << 5; // has "stream" or answers text/event-stream
inline constexpr unsigned kList = 1u << 6; // cursor list (`after`, has_more)
inline constexpr unsigned kDeprecated = 1u << 7; // marked deprecated in the spec
inline constexpr unsigned kBodyUnion = 1u << 8; // body is a oneOf / anyOf (fields = union)
// CallSpec::req (bit set of accepted request encodings)
inline constexpr unsigned kReqNone = 0;
inline constexpr unsigned kReqJson = 1u << 0;
inline constexpr unsigned kReqMultipart = 1u << 1;
inline constexpr unsigned kReqSdp = 1u << 2; // raw application/sdp offer
// CallSpec::resp (bit set of response encodings)
inline constexpr unsigned kRespEmpty = 0;
inline constexpr unsigned kRespJson = 1u << 0;
inline constexpr unsigned kRespSse = 1u << 1;
inline constexpr unsigned kRespBinary = 1u << 2;
inline constexpr unsigned kRespSdp = 1u << 3;
enum class Op : std::uint16_t {
#define OAI_CALL(en, ...) en,
#include "openai/platform/calls_table.inc"
#undef OAI_CALL
};
struct CallSpec {
Op op;
const char* method; // GET POST DELETE
const char* path; // "/vector_stores/{vector_store_id}/files"
const char* operation_id; // OpenAPI operationId
const char* area; // files, uploads, batches, fine_tuning, ...
unsigned req;
unsigned resp;
const char* resp_schema; // components.schemas name of the 2xx JSON body
const char* body_schema; // components.schemas name of the request body
const char* body_fields; // comma list: every top-level request field
const char* required_fields;
const char* query_params; // comma list
unsigned flags;
};
using PathArgs = std::vector<std::pair<std::string, std::string>>;
class Calls {
public:
static std::size_t count();
static const CallSpec& at(std::size_t index);
static const CallSpec& spec(Op op);
// By OpenAPI operation id, nullptr when unknown.
static const CallSpec* find(std::string_view operation_id);
static clients::Method method(const CallSpec& spec);
static bool has(const CallSpec& spec, unsigned flag) { return (spec.flags & flag) != 0; }
static std::vector<std::string> list(const char* csv); // "a,b" -> {"a","b"}
// Substitutes every {placeholder}. Each id must be a valid single segment
// (llm::Service::valid_id); a missing, extra or repeated argument throws
// llm::ApiError::invalid.
static std::string render(Op op, const PathArgs& args);
// The placeholder names of a path template, in order.
static std::vector<std::string> placeholders(const CallSpec& spec);
// JSON body: object, only fields the spec lists, all required fields set,
// serialised size under Caps::kMaxBodyBytes. nullptr is allowed only when
// the call has no required field.
static void check_body(Op op, const clients::body::Json* body);
// Multipart field names (as sent, "files[]" and "a[b]" reduce to the root
// name) against the same rules.
static void check_form(Op op, const std::vector<std::string>& names);
// "?a=b&c=d" or "". Keys must be listed query parameters. Values:
// string / number / bool, arrays (repeated key), objects (deepObject
// "key[sub]=v"). Capped (Caps::kMaxQuery*), `limit` capped.
static std::string query(Op op, const clients::body::Json& query);
static std::string percent_encode(const std::string& text);
};
} // namespace molo::openai::platform
+188
View File
@@ -0,0 +1,188 @@
OAI_CALL(kListAssistants, "GET", "/assistants", "listAssistants", "assistants", kReqNone, kRespJson, "ListAssistantsResponse", "", "", "", "limit,order,after,before", kBetaAssistants|kDeprecated|kList)
OAI_CALL(kCreateAssistant, "POST", "/assistants", "createAssistant", "assistants", kReqJson, kRespJson, "AssistantObject", "CreateAssistantRequest", "description,instructions,metadata,model,name,reasoning_effort,response_format,temperature,tool_resources,tools,top_p", "model", "", kBetaAssistants|kDeprecated)
OAI_CALL(kGetAssistant, "GET", "/assistants/{assistant_id}", "getAssistant", "assistants", kReqNone, kRespJson, "AssistantObject", "", "", "", "", kBetaAssistants|kDeprecated)
OAI_CALL(kModifyAssistant, "POST", "/assistants/{assistant_id}", "modifyAssistant", "assistants", kReqJson, kRespJson, "AssistantObject", "ModifyAssistantRequest", "description,instructions,metadata,model,name,reasoning_effort,response_format,temperature,tool_resources,tools,top_p", "", "", kBetaAssistants|kDeprecated)
OAI_CALL(kDeleteAssistant, "DELETE", "/assistants/{assistant_id}", "deleteAssistant", "assistants", kReqNone, kRespJson, "DeleteAssistantResponse", "", "", "", "", kDestructive|kBetaAssistants|kDeprecated)
OAI_CALL(kCreateBatch, "POST", "/batches", "createBatch", "batches", kReqJson, kRespJson, "Batch", "CreateBatchRequest", "completion_window,endpoint,input_file_id,metadata,output_expires_after", "completion_window,endpoint,input_file_id", "", kPaid)
OAI_CALL(kListBatches, "GET", "/batches", "listBatches", "batches", kReqNone, kRespJson, "ListBatchesResponse", "", "", "", "after,limit", kList)
OAI_CALL(kRetrieveBatch, "GET", "/batches/{batch_id}", "retrieveBatch", "batches", kReqNone, kRespJson, "Batch", "", "", "", "", kNoFlags)
OAI_CALL(kCancelBatch, "POST", "/batches/{batch_id}/cancel", "cancelBatch", "batches", kReqNone, kRespJson, "Batch", "", "", "", "", kDestructive)
OAI_CALL(kListContainers, "GET", "/containers", "ListContainers", "containers", kReqNone, kRespJson, "ContainerListResource", "", "", "", "limit,order,after,name", kList)
OAI_CALL(kCreateContainer, "POST", "/containers", "CreateContainer", "containers", kReqJson, kRespJson, "ContainerResource", "CreateContainerBody", "expires_after,file_ids,memory_limit,name,network_policy,skills", "name", "", kPaid)
OAI_CALL(kRetrieveContainer, "GET", "/containers/{container_id}", "RetrieveContainer", "containers", kReqNone, kRespJson, "ContainerResource", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteContainer, "DELETE", "/containers/{container_id}", "DeleteContainer", "containers", kReqNone, kRespEmpty, "", "", "", "", "", kDestructive)
OAI_CALL(kCreateContainerFile, "POST", "/containers/{container_id}/files", "CreateContainerFile", "containers", kReqJson|kReqMultipart, kRespJson, "ContainerFileResource", "CreateContainerFileBody", "file,file_id", "", "", kPaid)
OAI_CALL(kListContainerFiles, "GET", "/containers/{container_id}/files", "ListContainerFiles", "containers", kReqNone, kRespJson, "ContainerFileListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kRetrieveContainerFile, "GET", "/containers/{container_id}/files/{file_id}", "RetrieveContainerFile", "containers", kReqNone, kRespJson, "ContainerFileResource", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteContainerFile, "DELETE", "/containers/{container_id}/files/{file_id}", "DeleteContainerFile", "containers", kReqNone, kRespEmpty, "", "", "", "", "", kDestructive)
OAI_CALL(kRetrieveContainerFileContent, "GET", "/containers/{container_id}/files/{file_id}/content", "RetrieveContainerFileContent", "containers", kReqNone, kRespEmpty, "", "", "", "", "", kNoFlags)
OAI_CALL(kCreateConversationItems, "POST", "/conversations/{conversation_id}/items", "createConversationItems", "conversations", kReqJson, kRespJson, "ConversationItemList", "", "items", "items", "include", kNoFlags)
OAI_CALL(kListConversationItems, "GET", "/conversations/{conversation_id}/items", "listConversationItems", "conversations", kReqNone, kRespJson, "ConversationItemList", "", "", "", "limit,order,after,include", kList)
OAI_CALL(kGetConversationItem, "GET", "/conversations/{conversation_id}/items/{item_id}", "getConversationItem", "conversations", kReqNone, kRespJson, "ConversationItem", "", "", "", "include", kNoFlags)
OAI_CALL(kDeleteConversationItem, "DELETE", "/conversations/{conversation_id}/items/{item_id}", "deleteConversationItem", "conversations", kReqNone, kRespJson, "ConversationResource", "", "", "", "", kDestructive)
OAI_CALL(kListEvals, "GET", "/evals", "listEvals", "evals", kReqNone, kRespJson, "EvalList", "", "", "", "after,limit,order,order_by", kList)
OAI_CALL(kCreateEval, "POST", "/evals", "createEval", "evals", kReqJson, kRespJson, "Eval", "CreateEvalRequest", "data_source_config,metadata,name,testing_criteria", "data_source_config,testing_criteria", "", kNoFlags)
OAI_CALL(kGetEval, "GET", "/evals/{eval_id}", "getEval", "evals", kReqNone, kRespJson, "Eval", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateEval, "POST", "/evals/{eval_id}", "updateEval", "evals", kReqJson, kRespJson, "Eval", "", "metadata,name", "", "", kNoFlags)
OAI_CALL(kDeleteEval, "DELETE", "/evals/{eval_id}", "deleteEval", "evals", kReqNone, kRespJson, "", "", "", "", "", kDestructive)
OAI_CALL(kGetEvalRuns, "GET", "/evals/{eval_id}/runs", "getEvalRuns", "evals", kReqNone, kRespJson, "EvalRunList", "", "", "", "after,limit,order,status", kList)
OAI_CALL(kCreateEvalRun, "POST", "/evals/{eval_id}/runs", "createEvalRun", "evals", kReqJson, kRespJson, "EvalRun", "CreateEvalRunRequest", "data_source,metadata,name", "data_source", "", kPaid)
OAI_CALL(kGetEvalRun, "GET", "/evals/{eval_id}/runs/{run_id}", "getEvalRun", "evals", kReqNone, kRespJson, "EvalRun", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteEvalRun, "DELETE", "/evals/{eval_id}/runs/{run_id}", "deleteEvalRun", "evals", kReqNone, kRespJson, "", "", "", "", "", kDestructive)
OAI_CALL(kCancelEvalRun, "POST", "/evals/{eval_id}/runs/{run_id}/cancel", "cancelEvalRun", "evals", kReqNone, kRespJson, "EvalRun", "", "", "", "", kDestructive)
OAI_CALL(kGetEvalRunOutputItems, "GET", "/evals/{eval_id}/runs/{run_id}/output_items", "getEvalRunOutputItems", "evals", kReqNone, kRespJson, "EvalRunOutputItemList", "", "", "", "after,limit,status,order", kList)
OAI_CALL(kGetEvalRunOutputItem, "GET", "/evals/{eval_id}/runs/{run_id}/output_items/{output_item_id}", "getEvalRunOutputItem", "evals", kReqNone, kRespJson, "EvalRunOutputItem", "", "", "", "", kNoFlags)
OAI_CALL(kListFiles, "GET", "/files", "listFiles", "files", kReqNone, kRespJson, "ListFilesResponse", "", "", "", "purpose,limit,order,after", kList)
OAI_CALL(kCreateFile, "POST", "/files", "createFile", "files", kReqMultipart, kRespJson, "OpenAIFile", "CreateFileRequest", "expires_after,file,purpose", "file,purpose", "", kNoFlags)
OAI_CALL(kDeleteFile, "DELETE", "/files/{file_id}", "deleteFile", "files", kReqNone, kRespJson, "DeleteFileResponse", "", "", "", "", kDestructive)
OAI_CALL(kRetrieveFile, "GET", "/files/{file_id}", "retrieveFile", "files", kReqNone, kRespJson, "OpenAIFile", "", "", "", "", kNoFlags)
OAI_CALL(kDownloadFile, "GET", "/files/{file_id}/content", "downloadFile", "files", kReqNone, kRespBinary, "", "", "", "", "", kNoFlags)
OAI_CALL(kRunGrader, "POST", "/fine_tuning/alpha/graders/run", "runGrader", "fine_tuning", kReqJson, kRespJson, "RunGraderResponse", "RunGraderRequest", "grader,item,model_sample", "grader,model_sample", "", kPaid)
OAI_CALL(kValidateGrader, "POST", "/fine_tuning/alpha/graders/validate", "validateGrader", "fine_tuning", kReqJson, kRespJson, "ValidateGraderResponse", "ValidateGraderRequest", "grader", "grader", "", kNoFlags)
OAI_CALL(kListFineTuningCheckpointPermissions, "GET", "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions", "listFineTuningCheckpointPermissions", "fine_tuning", kReqNone, kRespJson, "ListFineTuningCheckpointPermissionResponse", "", "", "", "project_id,after,limit,order", kList)
OAI_CALL(kCreateFineTuningCheckpointPermission, "POST", "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions", "createFineTuningCheckpointPermission", "fine_tuning", kReqJson, kRespJson, "ListFineTuningCheckpointPermissionResponse", "CreateFineTuningCheckpointPermissionRequest", "project_ids", "project_ids", "", kNoFlags)
OAI_CALL(kDeleteFineTuningCheckpointPermission, "DELETE", "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions/{permission_id}", "deleteFineTuningCheckpointPermission", "fine_tuning", kReqNone, kRespJson, "DeleteFineTuningCheckpointPermissionResponse", "", "", "", "", kDestructive)
OAI_CALL(kCreateFineTuningJob, "POST", "/fine_tuning/jobs", "createFineTuningJob", "fine_tuning", kReqJson, kRespJson, "FineTuningJob", "CreateFineTuningJobRequest", "hyperparameters,integrations,metadata,method,model,seed,suffix,training_file,validation_file", "model,training_file", "", kPaid)
OAI_CALL(kListPaginatedFineTuningJobs, "GET", "/fine_tuning/jobs", "listPaginatedFineTuningJobs", "fine_tuning", kReqNone, kRespJson, "ListPaginatedFineTuningJobsResponse", "", "", "", "after,limit,metadata", kList)
OAI_CALL(kRetrieveFineTuningJob, "GET", "/fine_tuning/jobs/{fine_tuning_job_id}", "retrieveFineTuningJob", "fine_tuning", kReqNone, kRespJson, "FineTuningJob", "", "", "", "", kNoFlags)
OAI_CALL(kCancelFineTuningJob, "POST", "/fine_tuning/jobs/{fine_tuning_job_id}/cancel", "cancelFineTuningJob", "fine_tuning", kReqNone, kRespJson, "FineTuningJob", "", "", "", "", kDestructive)
OAI_CALL(kListFineTuningJobCheckpoints, "GET", "/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints", "listFineTuningJobCheckpoints", "fine_tuning", kReqNone, kRespJson, "ListFineTuningJobCheckpointsResponse", "", "", "", "after,limit", kList)
OAI_CALL(kListFineTuningEvents, "GET", "/fine_tuning/jobs/{fine_tuning_job_id}/events", "listFineTuningEvents", "fine_tuning", kReqNone, kRespJson, "ListFineTuningJobEventsResponse", "", "", "", "after,limit", kList)
OAI_CALL(kPauseFineTuningJob, "POST", "/fine_tuning/jobs/{fine_tuning_job_id}/pause", "pauseFineTuningJob", "fine_tuning", kReqNone, kRespJson, "FineTuningJob", "", "", "", "", kNoFlags)
OAI_CALL(kResumeFineTuningJob, "POST", "/fine_tuning/jobs/{fine_tuning_job_id}/resume", "resumeFineTuningJob", "fine_tuning", kReqNone, kRespJson, "FineTuningJob", "", "", "", "", kNoFlags)
OAI_CALL(kCreateLive, "POST", "/live/sessions", "create-live", "live", kReqJson, kRespJson, "LiveCreateResponse", "LiveCreateRequest", "session,transport", "session,transport", "", kPaid)
OAI_CALL(kAcceptLiveSession, "POST", "/live/sessions/{session_id}/accept", "accept-live-session", "live", kReqJson, kRespEmpty, "", "LiveCallAcceptRequest", "session", "session", "", kNoFlags)
OAI_CALL(kForkLiveSession, "POST", "/live/sessions/{session_id}/fork", "fork-live-session", "live", kReqJson, kRespJson, "LiveCreateResponse", "LiveForkRequest", "session,transport", "transport", "", kPaid)
OAI_CALL(kHangupLiveSession, "POST", "/live/sessions/{session_id}/hangup", "hangup-live-session", "live", kReqNone, kRespEmpty, "", "", "", "", "", kDestructive)
OAI_CALL(kReferLiveSession, "POST", "/live/sessions/{session_id}/refer", "refer-live-session", "live", kReqJson, kRespEmpty, "", "LiveCallReferRequest", "target_uri", "target_uri", "", kDestructive)
OAI_CALL(kRejectLiveSession, "POST", "/live/sessions/{session_id}/reject", "reject-live-session", "live", kReqJson, kRespEmpty, "", "LiveCallRejectRequest", "status_code", "status_code", "", kDestructive)
OAI_CALL(kCreateRealtimeCall, "POST", "/realtime/calls", "create-realtime-call", "realtime", kReqMultipart|kReqSdp, kRespSdp, "", "RealtimeCallCreateRequest", "sdp,session", "sdp", "", kPaid)
OAI_CALL(kAcceptRealtimeCall, "POST", "/realtime/calls/{call_id}/accept", "accept-realtime-call", "realtime", kReqJson, kRespEmpty, "", "RealtimeSessionCreateRequestGA", "audio,include,instructions,max_output_tokens,model,output_modalities,parallel_tool_calls,prompt,reasoning,tool_choice,tools,tracing,truncation,type", "type", "", kPaid)
OAI_CALL(kHangupRealtimeCall, "POST", "/realtime/calls/{call_id}/hangup", "hangup-realtime-call", "realtime", kReqNone, kRespEmpty, "", "", "", "", "", kDestructive)
OAI_CALL(kReferRealtimeCall, "POST", "/realtime/calls/{call_id}/refer", "refer-realtime-call", "realtime", kReqJson, kRespEmpty, "", "RealtimeCallReferRequest", "target_uri", "target_uri", "", kDestructive)
OAI_CALL(kRejectRealtimeCall, "POST", "/realtime/calls/{call_id}/reject", "reject-realtime-call", "realtime", kReqJson, kRespEmpty, "", "RealtimeCallRejectRequest", "status_code", "", "", kDestructive)
OAI_CALL(kCreateRealtimeClientSecret, "POST", "/realtime/client_secrets", "create-realtime-client-secret", "realtime", kReqJson, kRespJson, "RealtimeCreateClientSecretResponse", "RealtimeCreateClientSecretRequest", "expires_after,session", "", "", kNoFlags)
OAI_CALL(kCreateRealtimeSession, "POST", "/realtime/sessions", "create-realtime-session", "realtime", kReqJson, kRespJson, "RealtimeSessionCreateResponse", "RealtimeSessionCreateRequest", "client_secret,input_audio_format,input_audio_transcription,instructions,max_response_output_tokens,modalities,output_audio_format,prompt,speed,temperature,tool_choice,tools,tracing,truncation,turn_detection,voice", "client_secret", "", kNoFlags)
OAI_CALL(kCreateRealtimeTranscriptionSession, "POST", "/realtime/transcription_sessions", "create-realtime-transcription-session", "realtime", kReqJson, kRespJson, "RealtimeTranscriptionSessionCreateResponse", "RealtimeTranscriptionSessionCreateRequest", "include,input_audio_format,input_audio_noise_reduction,input_audio_transcription,turn_detection", "", "", kNoFlags)
OAI_CALL(kCreateRealtimeTranslationClientSecret, "POST", "/realtime/translations/client_secrets", "create-realtime-translation-client-secret", "realtime", kReqJson, kRespJson, "RealtimeTranslationClientSecretCreateResponse", "RealtimeTranslationClientSecretCreateRequest", "expires_after,session", "session", "", kNoFlags)
OAI_CALL(kCreateThread, "POST", "/threads", "createThread", "assistants", kReqJson, kRespJson, "ThreadObject", "CreateThreadRequest", "messages,metadata,tool_resources", "", "", kBetaAssistants)
OAI_CALL(kCreateThreadAndRun, "POST", "/threads/runs", "createThreadAndRun", "assistants", kReqJson, kRespJson|kRespSse, "RunObject", "CreateThreadAndRunRequest", "assistant_id,instructions,max_completion_tokens,max_prompt_tokens,metadata,model,parallel_tool_calls,response_format,stream,temperature,thread,tool_choice,tool_resources,tools,top_p,truncation_strategy", "assistant_id", "", kBetaAssistants|kStreamable|kPaid)
OAI_CALL(kGetThread, "GET", "/threads/{thread_id}", "getThread", "assistants", kReqNone, kRespJson, "ThreadObject", "", "", "", "", kBetaAssistants)
OAI_CALL(kModifyThread, "POST", "/threads/{thread_id}", "modifyThread", "assistants", kReqJson, kRespJson, "ThreadObject", "ModifyThreadRequest", "metadata,tool_resources", "", "", kBetaAssistants)
OAI_CALL(kDeleteThread, "DELETE", "/threads/{thread_id}", "deleteThread", "assistants", kReqNone, kRespJson, "DeleteThreadResponse", "", "", "", "", kDestructive|kBetaAssistants)
OAI_CALL(kListMessages, "GET", "/threads/{thread_id}/messages", "listMessages", "assistants", kReqNone, kRespJson, "ListMessagesResponse", "", "", "", "limit,order,after,before,run_id", kBetaAssistants|kList)
OAI_CALL(kCreateMessage, "POST", "/threads/{thread_id}/messages", "createMessage", "assistants", kReqJson, kRespJson, "MessageObject", "CreateMessageRequest", "attachments,content,metadata,role", "content,role", "", kBetaAssistants)
OAI_CALL(kGetMessage, "GET", "/threads/{thread_id}/messages/{message_id}", "getMessage", "assistants", kReqNone, kRespJson, "MessageObject", "", "", "", "", kBetaAssistants)
OAI_CALL(kModifyMessage, "POST", "/threads/{thread_id}/messages/{message_id}", "modifyMessage", "assistants", kReqJson, kRespJson, "MessageObject", "ModifyMessageRequest", "metadata", "", "", kBetaAssistants)
OAI_CALL(kDeleteMessage, "DELETE", "/threads/{thread_id}/messages/{message_id}", "deleteMessage", "assistants", kReqNone, kRespJson, "DeleteMessageResponse", "", "", "", "", kDestructive|kBetaAssistants)
OAI_CALL(kListRuns, "GET", "/threads/{thread_id}/runs", "listRuns", "assistants", kReqNone, kRespJson, "ListRunsResponse", "", "", "", "limit,order,after,before", kBetaAssistants|kList)
OAI_CALL(kCreateRun, "POST", "/threads/{thread_id}/runs", "createRun", "assistants", kReqJson, kRespJson|kRespSse, "RunObject", "CreateRunRequest", "additional_instructions,additional_messages,assistant_id,instructions,max_completion_tokens,max_prompt_tokens,metadata,model,parallel_tool_calls,reasoning_effort,response_format,stream,temperature,tool_choice,tools,top_p,truncation_strategy", "assistant_id", "include[]", kBetaAssistants|kStreamable|kPaid)
OAI_CALL(kGetRun, "GET", "/threads/{thread_id}/runs/{run_id}", "getRun", "assistants", kReqNone, kRespJson, "RunObject", "", "", "", "", kBetaAssistants)
OAI_CALL(kModifyRun, "POST", "/threads/{thread_id}/runs/{run_id}", "modifyRun", "assistants", kReqJson, kRespJson, "RunObject", "ModifyRunRequest", "metadata", "", "", kBetaAssistants)
OAI_CALL(kCancelRun, "POST", "/threads/{thread_id}/runs/{run_id}/cancel", "cancelRun", "assistants", kReqNone, kRespJson, "RunObject", "", "", "", "", kDestructive|kBetaAssistants)
OAI_CALL(kListRunSteps, "GET", "/threads/{thread_id}/runs/{run_id}/steps", "listRunSteps", "assistants", kReqNone, kRespJson, "ListRunStepsResponse", "", "", "", "limit,order,after,before,include[]", kBetaAssistants|kList)
OAI_CALL(kGetRunStep, "GET", "/threads/{thread_id}/runs/{run_id}/steps/{step_id}", "getRunStep", "assistants", kReqNone, kRespJson, "RunStepObject", "", "", "", "include[]", kBetaAssistants)
OAI_CALL(kSubmitToolOuputsToRun, "POST", "/threads/{thread_id}/runs/{run_id}/submit_tool_outputs", "submitToolOuputsToRun", "assistants", kReqJson, kRespJson|kRespSse, "RunObject", "SubmitToolOutputsRunRequest", "stream,tool_outputs", "tool_outputs", "", kBetaAssistants|kStreamable|kPaid)
OAI_CALL(kCreateUpload, "POST", "/uploads", "createUpload", "uploads", kReqJson, kRespJson, "Upload", "CreateUploadRequest", "bytes,expires_after,filename,mime_type,purpose", "bytes,filename,mime_type,purpose", "", kNoFlags)
OAI_CALL(kCancelUpload, "POST", "/uploads/{upload_id}/cancel", "cancelUpload", "uploads", kReqNone, kRespJson, "Upload", "", "", "", "", kDestructive)
OAI_CALL(kCompleteUpload, "POST", "/uploads/{upload_id}/complete", "completeUpload", "uploads", kReqJson, kRespJson, "Upload", "CompleteUploadRequest", "md5,part_ids", "part_ids", "", kNoFlags)
OAI_CALL(kAddUploadPart, "POST", "/uploads/{upload_id}/parts", "addUploadPart", "uploads", kReqMultipart, kRespJson, "UploadPart", "AddUploadPartRequest", "data", "data", "", kNoFlags)
OAI_CALL(kListVectorStores, "GET", "/vector_stores", "listVectorStores", "vector_stores", kReqNone, kRespJson, "ListVectorStoresResponse", "", "", "", "limit,order,after,before", kList)
OAI_CALL(kCreateVectorStore, "POST", "/vector_stores", "createVectorStore", "vector_stores", kReqJson, kRespJson, "VectorStoreObject", "CreateVectorStoreRequest", "chunking_strategy,description,expires_after,file_ids,metadata,name", "", "", kPaid)
OAI_CALL(kGetVectorStore, "GET", "/vector_stores/{vector_store_id}", "getVectorStore", "vector_stores", kReqNone, kRespJson, "VectorStoreObject", "", "", "", "", kNoFlags)
OAI_CALL(kModifyVectorStore, "POST", "/vector_stores/{vector_store_id}", "modifyVectorStore", "vector_stores", kReqJson, kRespJson, "VectorStoreObject", "UpdateVectorStoreRequest", "expires_after,metadata,name", "", "", kNoFlags)
OAI_CALL(kDeleteVectorStore, "DELETE", "/vector_stores/{vector_store_id}", "deleteVectorStore", "vector_stores", kReqNone, kRespJson, "DeleteVectorStoreResponse", "", "", "", "", kDestructive)
OAI_CALL(kCreateVectorStoreFileBatch, "POST", "/vector_stores/{vector_store_id}/file_batches", "createVectorStoreFileBatch", "vector_stores", kReqJson, kRespJson, "VectorStoreFileBatchObject", "CreateVectorStoreFileBatchRequest", "attributes,chunking_strategy,file_ids,files", "", "", kPaid)
OAI_CALL(kGetVectorStoreFileBatch, "GET", "/vector_stores/{vector_store_id}/file_batches/{batch_id}", "getVectorStoreFileBatch", "vector_stores", kReqNone, kRespJson, "VectorStoreFileBatchObject", "", "", "", "", kNoFlags)
OAI_CALL(kCancelVectorStoreFileBatch, "POST", "/vector_stores/{vector_store_id}/file_batches/{batch_id}/cancel", "cancelVectorStoreFileBatch", "vector_stores", kReqNone, kRespJson, "VectorStoreFileBatchObject", "", "", "", "", kDestructive)
OAI_CALL(kListFilesInVectorStoreBatch, "GET", "/vector_stores/{vector_store_id}/file_batches/{batch_id}/files", "listFilesInVectorStoreBatch", "vector_stores", kReqNone, kRespJson, "ListVectorStoreFilesResponse", "", "", "", "limit,order,after,before,filter", kList)
OAI_CALL(kListVectorStoreFiles, "GET", "/vector_stores/{vector_store_id}/files", "listVectorStoreFiles", "vector_stores", kReqNone, kRespJson, "ListVectorStoreFilesResponse", "", "", "", "limit,order,after,before,filter", kList)
OAI_CALL(kCreateVectorStoreFile, "POST", "/vector_stores/{vector_store_id}/files", "createVectorStoreFile", "vector_stores", kReqJson, kRespJson, "VectorStoreFileObject", "CreateVectorStoreFileRequest", "attributes,chunking_strategy,file_id", "file_id", "", kPaid)
OAI_CALL(kGetVectorStoreFile, "GET", "/vector_stores/{vector_store_id}/files/{file_id}", "getVectorStoreFile", "vector_stores", kReqNone, kRespJson, "VectorStoreFileObject", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteVectorStoreFile, "DELETE", "/vector_stores/{vector_store_id}/files/{file_id}", "deleteVectorStoreFile", "vector_stores", kReqNone, kRespJson, "DeleteVectorStoreFileResponse", "", "", "", "", kDestructive)
OAI_CALL(kUpdateVectorStoreFileAttributes, "POST", "/vector_stores/{vector_store_id}/files/{file_id}", "updateVectorStoreFileAttributes", "vector_stores", kReqJson, kRespJson, "VectorStoreFileObject", "UpdateVectorStoreFileAttributesRequest", "attributes", "attributes", "", kNoFlags)
OAI_CALL(kRetrieveVectorStoreFileContent, "GET", "/vector_stores/{vector_store_id}/files/{file_id}/content", "retrieveVectorStoreFileContent", "vector_stores", kReqNone, kRespJson, "VectorStoreFileContentResponse", "", "", "", "", kNoFlags)
OAI_CALL(kSearchVectorStore, "POST", "/vector_stores/{vector_store_id}/search", "searchVectorStore", "vector_stores", kReqJson, kRespJson, "VectorStoreSearchResultsPage", "VectorStoreSearchRequest", "filters,max_num_results,query,ranking_options,rewrite_query", "query", "", kPaid)
OAI_CALL(kCreateConversation, "POST", "/conversations", "createConversation", "conversations", kReqJson, kRespJson, "ConversationResource", "CreateConversationBody", "items,metadata", "", "", kNoFlags)
OAI_CALL(kGetConversation, "GET", "/conversations/{conversation_id}", "getConversation", "conversations", kReqNone, kRespJson, "ConversationResource", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteConversation, "DELETE", "/conversations/{conversation_id}", "deleteConversation", "conversations", kReqNone, kRespJson, "DeletedConversationResource", "", "", "", "", kDestructive)
OAI_CALL(kUpdateConversation, "POST", "/conversations/{conversation_id}", "updateConversation", "conversations", kReqJson, kRespJson, "ConversationResource", "UpdateConversationBody", "metadata", "metadata", "", kNoFlags)
OAI_CALL(kGetsafetycase, "GET", "/safety/cases/{id}", "Getsafetycase", "safety", kReqNone, kRespJson, "SafetyCaseResource", "", "", "", "", kNoFlags)
OAI_CALL(kGetprojectsafetyalert, "GET", "/safety/alerts/{id}", "Getprojectsafetyalert", "safety", kReqNone, kRespJson, "SafetyAlertResource", "", "", "", "", kNoFlags)
OAI_CALL(kDownloadLiveRecording, "GET", "/live/sessions/{session_id}/content", "download-live-recording", "live", kReqNone, kRespBinary, "", "", "", "", "", kNoFlags)
OAI_CALL(kCreatecontentprovenancecheck, "POST", "/content_provenance_checks", "Createcontentprovenancecheck", "provenance", kReqMultipart, kRespJson, "ProvenanceResource", "CreateContentProvenanceBody", "file", "file", "", kPaid)
OAI_CALL(kCreateSkill, "POST", "/skills", "CreateSkill", "skills", kReqJson|kReqMultipart, kRespJson, "SkillResource", "CreateSkillBody", "files", "", "", kPaid)
OAI_CALL(kListSkills, "GET", "/skills", "ListSkills", "skills", kReqNone, kRespJson, "SkillListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kDeleteSkill, "DELETE", "/skills/{skill_id}", "DeleteSkill", "skills", kReqNone, kRespJson, "DeletedSkillResource", "", "", "", "", kDestructive)
OAI_CALL(kGetSkill, "GET", "/skills/{skill_id}", "GetSkill", "skills", kReqNone, kRespJson, "SkillResource", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateSkillDefaultVersion, "POST", "/skills/{skill_id}", "UpdateSkillDefaultVersion", "skills", kReqJson, kRespJson, "SkillResource", "SetDefaultSkillVersionBody", "default_version", "default_version", "", kNoFlags)
OAI_CALL(kGetSkillContent, "GET", "/skills/{skill_id}/content", "GetSkillContent", "skills", kReqNone, kRespJson|kRespBinary, "", "", "", "", "", kNoFlags)
OAI_CALL(kCreateSkillVersion, "POST", "/skills/{skill_id}/versions", "CreateSkillVersion", "skills", kReqJson|kReqMultipart, kRespJson, "SkillVersionResource", "CreateSkillVersionBody", "default,files", "", "", kPaid)
OAI_CALL(kListSkillVersions, "GET", "/skills/{skill_id}/versions", "ListSkillVersions", "skills", kReqNone, kRespJson, "SkillVersionListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kGetSkillVersion, "GET", "/skills/{skill_id}/versions/{version}", "GetSkillVersion", "skills", kReqNone, kRespJson, "SkillVersionResource", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteSkillVersion, "DELETE", "/skills/{skill_id}/versions/{version}", "DeleteSkillVersion", "skills", kReqNone, kRespJson, "DeletedSkillVersionResource", "", "", "", "", kDestructive)
OAI_CALL(kGetSkillVersionContent, "GET", "/skills/{skill_id}/versions/{version}/content", "GetSkillVersionContent", "skills", kReqNone, kRespJson|kRespBinary, "", "", "", "", "", kNoFlags)
OAI_CALL(kCancelChatSessionMethod, "POST", "/chatkit/sessions/{session_id}/cancel", "CancelChatSessionMethod", "chatkit", kReqNone, kRespJson, "ChatSessionResource", "", "", "", "", kDestructive|kBetaChatkit)
OAI_CALL(kCreateChatSessionMethod, "POST", "/chatkit/sessions", "CreateChatSessionMethod", "chatkit", kReqJson, kRespJson, "ChatSessionResource", "CreateChatSessionBody", "chatkit_configuration,expires_after,rate_limits,user,workflow", "user,workflow", "", kBetaChatkit|kPaid)
OAI_CALL(kListThreadItemsMethod, "GET", "/chatkit/threads/{thread_id}/items", "ListThreadItemsMethod", "chatkit", kReqNone, kRespJson, "ThreadItemListResource", "", "", "", "limit,order,after,before", kBetaChatkit|kList)
OAI_CALL(kGetThreadMethod, "GET", "/chatkit/threads/{thread_id}", "GetThreadMethod", "chatkit", kReqNone, kRespJson, "ThreadResource", "", "", "", "", kBetaChatkit)
OAI_CALL(kDeleteThreadMethod, "DELETE", "/chatkit/threads/{thread_id}", "DeleteThreadMethod", "chatkit", kReqNone, kRespJson, "DeletedThreadResource", "", "", "", "", kDestructive|kBetaChatkit)
OAI_CALL(kListThreadsMethod, "GET", "/chatkit/threads", "ListThreadsMethod", "chatkit", kReqNone, kRespJson, "ThreadListResource", "", "", "", "limit,order,after,before,user", kBetaChatkit|kList)
OAI_CALL(kCreateDecision, "POST", "/decisions", "createDecision", "decisions", kReqJson, kRespJson, "DecisionResponse", "DecisionRequest", "input,model,prompt_cache_key,prompt_cache_options,questions,safety_identifier", "input,model,questions", "", kPaid)
OAI_CALL(kCreateAgentAutomation, "POST", "/agents/automations", "createAgentAutomation", "agents", kReqJson, kRespJson, "AutomationResource", "CreateAutomationParams", "expression,metadata,name,run_at,target,timezone,type", "", "", kBodyUnion)
OAI_CALL(kListAgentAutomations, "GET", "/agents/automations", "listAgentAutomations", "agents", kReqNone, kRespJson, "", "", "", "", "limit,order,after,session_id", kList)
OAI_CALL(kGetAgentAutomation, "GET", "/agents/automations/{automation_id}", "getAgentAutomation", "agents", kReqNone, kRespJson, "AutomationResource", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateAgentAutomation, "POST", "/agents/automations/{automation_id}", "updateAgentAutomation", "agents", kReqJson, kRespJson, "AutomationResource", "UpdateAutomationParams", "expression,metadata,name,run_at,target,timezone", "", "", kNoFlags)
OAI_CALL(kDeleteAgentAutomation, "DELETE", "/agents/automations/{automation_id}", "deleteAgentAutomation", "agents", kReqNone, kRespJson, "DeletedAutomationResource", "", "", "", "", kDestructive)
OAI_CALL(kPauseAgentAutomation, "POST", "/agents/automations/{automation_id}/pause", "pauseAgentAutomation", "agents", kReqNone, kRespJson, "AutomationResource", "", "", "", "", kNoFlags)
OAI_CALL(kResumeAgentAutomation, "POST", "/agents/automations/{automation_id}/resume", "resumeAgentAutomation", "agents", kReqNone, kRespJson, "AutomationResource", "", "", "", "", kNoFlags)
OAI_CALL(kCreateAgentAutomationInvocation, "POST", "/agents/automations/{automation_id}/invocations", "createAgentAutomationInvocation", "agents", kReqNone, kRespJson, "AutomationInvocationResource", "", "", "", "", kPaid)
OAI_CALL(kListAgentAutomationInvocations, "GET", "/agents/automations/{automation_id}/invocations", "listAgentAutomationInvocations", "agents", kReqNone, kRespJson, "", "", "", "", "limit,order,after", kList)
OAI_CALL(kGetAgentAutomationInvocation, "GET", "/agents/automations/{automation_id}/invocations/{invocation_id}", "getAgentAutomationInvocation", "agents", kReqNone, kRespJson, "AutomationInvocationResource", "", "", "", "", kNoFlags)
OAI_CALL(kListAgentEnvironments, "GET", "/agents/environments", "listAgentEnvironments", "agents", kReqNone, kRespJson, "AgentEnvironmentListResource", "", "", "", "limit,order,after,type", kList)
OAI_CALL(kCreateAgentEnvironment, "POST", "/agents/environments", "createAgentEnvironment", "agents", kReqJson, kRespJson, "PublicEnvironmentResource", "CreateAgentEnvironmentParams", "environment,vault_ids", "environment", "", kIdempotency|kPaid)
OAI_CALL(kRetrieveAgentEnvironment, "GET", "/agents/environments/{environment_id}", "retrieveAgentEnvironment", "agents", kReqNone, kRespJson, "PublicEnvironmentResource", "", "", "", "", kNoFlags)
OAI_CALL(kListAgentEnvironmentFiles, "GET", "/agents/environments/{environment_id}/files", "listAgentEnvironmentFiles", "agents", kReqNone, kRespJson, "EnvironmentFileListResource", "", "", "", "path,limit,order,page", kNoFlags)
OAI_CALL(kCreateAgentEnvironmentFile, "POST", "/agents/environments/{environment_id}/files", "createAgentEnvironmentFile", "agents", kReqJson, kRespJson, "EnvironmentFileResource", "HostedEnvironmentFileParam", "data,file_id,path,type", "", "", kBodyUnion|kPaid)
OAI_CALL(kListAgentSessionSubagents, "GET", "/agents/sessions/{session_id}/subagents", "listAgentSessionSubagents", "agents", kReqNone, kRespJson, "", "", "", "", "limit,order,after", kList)
OAI_CALL(kRetrieveAgentSessionSubagent, "GET", "/agents/sessions/{session_id}/subagents/{subagent_id}", "retrieveAgentSessionSubagent", "agents", kReqNone, kRespJson, "SubagentResource", "", "", "", "", kNoFlags)
OAI_CALL(kListAgentSessionSubagentItems, "GET", "/agents/sessions/{session_id}/subagents/{subagent_id}/items", "listAgentSessionSubagentItems", "agents", kReqNone, kRespJson, "SessionItemListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kListAgentSessionSubagentTurns, "GET", "/agents/sessions/{session_id}/subagents/{subagent_id}/turns", "listAgentSessionSubagentTurns", "agents", kReqNone, kRespJson, "SessionTurnListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kRetrieveAgentSessionSubagentTurn, "GET", "/agents/sessions/{session_id}/subagents/{subagent_id}/turns/{turn_id}", "retrieveAgentSessionSubagentTurn", "agents", kReqNone, kRespJson, "TurnResource", "", "", "", "", kNoFlags)
OAI_CALL(kListAgentSessionSubagentTurnItems, "GET", "/agents/sessions/{session_id}/subagents/{subagent_id}/turns/{turn_id}/items", "listAgentSessionSubagentTurnItems", "agents", kReqNone, kRespJson, "SessionItemListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kListAgents, "GET", "/agents", "listAgents", "agents", kReqNone, kRespJson, "AgentListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kCreateAgent, "POST", "/agents", "createAgent", "agents", kReqJson, kRespJson, "AgentResource", "CreateAgentParams", "instructions,metadata,model,multi_agent,name,reasoning,service_tier,text,tools", "model", "", kNoFlags)
OAI_CALL(kRetrieveAgent, "GET", "/agents/{agent_id}", "retrieveAgent", "agents", kReqNone, kRespJson, "AgentResource", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateAgent, "POST", "/agents/{agent_id}", "updateAgent", "agents", kReqJson, kRespJson, "AgentResource", "UpdateAgentParams", "instructions,metadata,model,multi_agent,name,reasoning,service_tier,text,tools", "", "", kNoFlags)
OAI_CALL(kDeleteAgent, "DELETE", "/agents/{agent_id}", "deleteAgent", "agents", kReqNone, kRespJson, "DeletedAgentResource", "", "", "", "", kDestructive)
OAI_CALL(kListAgentEnvironmentTemplates, "GET", "/agents/environments/templates", "listAgentEnvironmentTemplates", "agents", kReqNone, kRespJson, "EnvironmentTemplateListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kCreateAgentEnvironmentTemplate, "POST", "/agents/environments/templates", "createAgentEnvironmentTemplate", "agents", kReqJson, kRespJson, "EnvironmentTemplateResource", "CreateEnvironmentTemplateParams", "capability_directories,desktop,env,files,name,network,packages,plugins,setup_commands,skills", "", "", kPaid)
OAI_CALL(kRetrieveAgentEnvironmentTemplate, "GET", "/agents/environments/templates/{environment_template_id}", "retrieveAgentEnvironmentTemplate", "agents", kReqNone, kRespJson, "EnvironmentTemplateResource", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateAgentEnvironmentTemplate, "POST", "/agents/environments/templates/{environment_template_id}", "updateAgentEnvironmentTemplate", "agents", kReqJson, kRespJson, "EnvironmentTemplateResource", "UpdateEnvironmentTemplateParams", "capability_directories,desktop,env,files,name,network,packages,plugins,setup_commands,skills", "", "", kNoFlags)
OAI_CALL(kDeleteAgentEnvironmentTemplate, "DELETE", "/agents/environments/templates/{environment_template_id}", "deleteAgentEnvironmentTemplate", "agents", kReqNone, kRespJson, "DeletedEnvironmentTemplateResource", "", "", "", "", kDestructive)
OAI_CALL(kListAgentSessions, "GET", "/agents/sessions", "listAgentSessions", "agents", kReqNone, kRespJson, "SessionListResource", "", "", "", "limit,order,after,agent_id", kList)
OAI_CALL(kCreateAgentSession, "POST", "/agents/sessions", "createAgentSession", "agents", kReqJson, kRespJson|kRespSse, "SessionResource", "CreateAgentSessionParams", "agent,agent_id,environment,input,metadata,spend_control,stream,vault_ids", "environment", "", kStreamable|kPaid)
OAI_CALL(kRetrieveAgentSession, "GET", "/agents/sessions/{session_id}", "retrieveAgentSession", "agents", kReqNone, kRespJson, "SessionResource", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateAgentSession, "POST", "/agents/sessions/{session_id}", "updateAgentSession", "agents", kReqJson, kRespJson, "SessionResource", "UpdateAgentSessionParams", "agent,metadata,spend_control", "", "", kNoFlags)
OAI_CALL(kDeleteAgentSession, "DELETE", "/agents/sessions/{session_id}", "deleteAgentSession", "agents", kReqNone, kRespJson, "DeletedSessionResource", "", "", "", "", kDestructive)
OAI_CALL(kListAgentSessionArtifacts, "GET", "/agents/sessions/{session_id}/artifacts", "listAgentSessionArtifacts", "agents", kReqNone, kRespJson, "SessionArtifactListResource", "", "", "", "limit,order,after,environment_id", kList)
OAI_CALL(kRetrieveAgentSessionArtifact, "GET", "/agents/sessions/{session_id}/artifacts/{artifact_id}", "retrieveAgentSessionArtifact", "agents", kReqNone, kRespJson, "SessionArtifactResource", "", "", "", "", kNoFlags)
OAI_CALL(kDeleteAgentSessionArtifact, "DELETE", "/agents/sessions/{session_id}/artifacts/{artifact_id}", "deleteAgentSessionArtifact", "agents", kReqNone, kRespJson, "DeletedSessionArtifactResource", "", "", "", "", kDestructive)
OAI_CALL(kRetrieveAgentSessionArtifactContent, "GET", "/agents/sessions/{session_id}/artifacts/{artifact_id}/content", "retrieveAgentSessionArtifactContent", "agents", kReqNone, kRespBinary, "", "", "", "", "", kNoFlags)
OAI_CALL(kListAgentSessionEvents, "GET", "/agents/sessions/{session_id}/events", "listAgentSessionEvents", "agents", kReqNone, kRespSse, "SessionEvent", "", "", "", "", kStreamable)
OAI_CALL(kCreateAgentSessionEvents, "POST", "/agents/sessions/{session_id}/events", "createAgentSessionEvents", "agents", kReqJson, kRespEmpty, "", "CreateSessionEventsParams", "events", "events", "", kIdempotency|kPaid)
OAI_CALL(kListAgentSessionItems, "GET", "/agents/sessions/{session_id}/items", "listAgentSessionItems", "agents", kReqNone, kRespJson, "SessionItemListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kListAgentSessionTraces, "GET", "/agents/sessions/{session_id}/traces", "listAgentSessionTraces", "agents", kReqNone, kRespJson, "SessionTraceListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kListAgentSessionTurns, "GET", "/agents/sessions/{session_id}/turns", "listAgentSessionTurns", "agents", kReqNone, kRespJson, "SessionTurnListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kRetrieveAgentSessionTurn, "GET", "/agents/sessions/{session_id}/turns/{turn_id}", "retrieveAgentSessionTurn", "agents", kReqNone, kRespJson, "TurnResource", "", "", "", "", kNoFlags)
OAI_CALL(kListAgentSessionTurnItems, "GET", "/agents/sessions/{session_id}/turns/{turn_id}/items", "listAgentSessionTurnItems", "agents", kReqNone, kRespJson, "SessionItemListResource", "", "", "", "limit,order,after", kList)
OAI_CALL(kListVaults, "GET", "/vaults", "listVaults", "vaults", kReqNone, kRespJson, "VaultListResource", "", "", "", "limit,order,after,metadata,status", kList)
OAI_CALL(kCreateVault, "POST", "/vaults", "createVault", "vaults", kReqJson, kRespJson, "VaultResource", "CreateVaultParams", "metadata,name", "", "", kNoFlags)
OAI_CALL(kRetrieveVault, "GET", "/vaults/{vault_id}", "retrieveVault", "vaults", kReqNone, kRespJson, "VaultResource", "", "", "", "", kNoFlags)
OAI_CALL(kUpdateVault, "POST", "/vaults/{vault_id}", "updateVault", "vaults", kReqJson, kRespJson, "VaultResource", "UpdateVaultParams", "metadata,name", "", "", kNoFlags)
OAI_CALL(kDeleteVault, "DELETE", "/vaults/{vault_id}", "deleteVault", "vaults", kReqNone, kRespJson, "DeletedVaultResource", "", "", "", "", kDestructive)
OAI_CALL(kListVaultCredentials, "GET", "/vaults/{vault_id}/credentials", "listVaultCredentials", "vaults", kReqNone, kRespJson, "VaultCredentialListResource", "", "", "", "limit,order,after,metadata,status", kList)
OAI_CALL(kCreateVaultCredential, "POST", "/vaults/{vault_id}/credentials", "createVaultCredential", "vaults", kReqJson, kRespJson, "VaultCredentialResource", "CreateVaultCredentialParams", "auth,metadata,name", "auth,name", "", kNoFlags)
OAI_CALL(kRetrieveVaultCredential, "GET", "/vaults/{vault_id}/credentials/{credential_id}", "retrieveVaultCredential", "vaults", kReqNone, kRespJson, "VaultCredentialResource", "", "", "", "", kNoFlags)
OAI_CALL(kRotateVaultCredential, "POST", "/vaults/{vault_id}/credentials/{credential_id}", "rotateVaultCredential", "vaults", kReqJson, kRespJson, "VaultCredentialResource", "RotateVaultCredentialParams", "auth,metadata", "", "", kNoFlags)
OAI_CALL(kDeleteVaultCredential, "DELETE", "/vaults/{vault_id}/credentials/{credential_id}", "deleteVaultCredential", "vaults", kReqNone, kRespJson, "DeletedVaultCredentialResource", "", "", "", "", kDestructive)
+58
View File
@@ -0,0 +1,58 @@
#pragma once
// Static ceilings of the OpenAI platform module. Never autotuned. Numbers
// that come from OpenAI's own documents carry the document in SPEC.md
// (## Sources); the rest are this module's choices and say so there.
#include <cstddef>
#include "llm/caps.h"
namespace molo::openai::platform {
struct Caps {
// --- requests ---
static constexpr std::size_t kMaxBodyBytes = llm::Caps::kMaxRequestBytes; // JSON request
static constexpr std::size_t kMaxQueryParams = 32;
static constexpr std::size_t kMaxQueryValueBytes = 4096;
static constexpr std::size_t kMaxQueryArrayItems = 64;
static constexpr long kMaxListLimit = 10000; // largest `limit` in the spec (files)
// --- list walking ---
static constexpr std::size_t kDefaultListPages = 5;
static constexpr std::size_t kMaxListPages = llm::Caps::kMaxPages;
static constexpr std::size_t kMaxListItems = 100000;
// --- files and uploads (spec: file up to 512 MB, upload part up to 64 MB,
// upload up to 8 GB; this module caps the total at llm::Caps) ---
static constexpr std::size_t kMaxFileBytes = llm::Caps::kMaxUploadBytes; // 512 MiB
static constexpr std::size_t kMaxUploadPartBytes = 64u * 1024u * 1024u;
static constexpr std::size_t kMaxUploadParts = 10000;
static constexpr std::size_t kMaxFilenameBytes = 255;
static constexpr std::size_t kMaxFormFields = 64;
// --- conversations (spec: "You may add up to 20 items at a time") ---
static constexpr std::size_t kMaxConversationItemsPerCall = 20;
// --- batch input file (spec: JSONL up to 50,000 requests and 200 MB) ---
static constexpr std::size_t kMaxBatchRequests = 50000;
static constexpr std::size_t kMaxBatchFileBytes = 200u * 1024u * 1024u;
static constexpr std::size_t kMaxCustomIdBytes = 512; // module choice
// --- binary downloads ---
static constexpr std::size_t kMaxDownloadBytes = llm::Caps::kMaxResponseBytes;
// --- WebSocket event sessions (module choices, no OpenAI figure) ---
static constexpr std::size_t kMaxEventBytes = 2u * 1024u * 1024u; // one client event (WsClient frame cap is 4 MiB inbound)
static constexpr std::size_t kMaxAudioChunkBytes = 1024u * 1024u; // raw bytes in one audio append
static constexpr std::size_t kMaxSessionEvents = 20000; // received per run
static constexpr std::size_t kMaxSessionBytes = 128u * 1024u * 1024u; // received per run
static constexpr int kMaxSessionSeconds = 3600;
static constexpr int kDefaultSessionSeconds = 30;
static constexpr int kConnectTimeoutMs = 10000;
static constexpr int kHandshakeTimeoutMs = 10000;
static constexpr int kReadPollMs = 500;
static constexpr std::size_t kMaxEventIdBytes = 512; // spec: event_id maxLength 512
};
} // namespace molo::openai::platform
+25
View File
@@ -0,0 +1,25 @@
#include "openai/platform/chatkit_api.h"
namespace molo::openai::platform {
Json ChatKitApi::create_session(const Json& body) { return invoke(Op::kCreateChatSessionMethod, {}, &body); }
Json ChatKitApi::cancel_session(const std::string& id) {
return invoke(Op::kCancelChatSessionMethod, {{"session_id", id}});
}
Json ChatKitApi::retrieve_thread(const std::string& id) {
return invoke(Op::kGetThreadMethod, {{"thread_id", id}});
}
Json ChatKitApi::list_threads(const Json& query) { return invoke(Op::kListThreadsMethod, {}, nullptr, query); }
Json ChatKitApi::list_thread_items(const std::string& id, const Json& query) {
return invoke(Op::kListThreadItemsMethod, {{"thread_id", id}}, nullptr, query);
}
Json ChatKitApi::remove_thread(const std::string& id) {
return invoke(Op::kDeleteThreadMethod, {{"thread_id", id}});
}
} // namespace molo::openai::platform
+30
View File
@@ -0,0 +1,30 @@
#pragma once
// ChatKit (beta; every call sends OpenAI-Beta: chatkit_beta=v1): create or
// cancel a session (its client secret goes to the browser widget) and read
// or delete threads and their items.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class ChatKitApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: user, workflow (required), chatkit_configuration,
// expires_after, rate_limits. The answer carries the client secret.
Json create_session(const Json& body);
// Destructive: the issued client secret stops working.
Json cancel_session(const std::string& session_id);
Json retrieve_thread(const std::string& thread_id);
Json list_threads(const Json& query = Json::object()); // after, before, limit, order, user
Json list_thread_items(const std::string& thread_id, const Json& query = Json::object());
// Destructive.
Json remove_thread(const std::string& thread_id);
};
} // namespace molo::openai::platform
+48
View File
@@ -0,0 +1,48 @@
#include "openai/platform/containers_api.h"
namespace molo::openai::platform {
Json ContainersApi::create(const Json& body) { return invoke(Op::kCreateContainer, {}, &body); }
Json ContainersApi::retrieve(const std::string& id) {
return invoke(Op::kRetrieveContainer, {{"container_id", id}});
}
Json ContainersApi::list(const Json& query) { return invoke(Op::kListContainers, {}, nullptr, query); }
Json ContainersApi::remove(const std::string& id) {
return invoke(Op::kDeleteContainer, {{"container_id", id}});
}
Json ContainerFilesApi::create_from_file(const std::string& container_id, const std::string& file_id) {
check_id("file", file_id);
Json body = Json::object();
body.set("file_id", Json(file_id));
return invoke(Op::kCreateContainerFile, {{"container_id", container_id}}, &body);
}
Json ContainerFilesApi::create_upload(const std::string& container_id, const std::string& filename,
const std::string& data, const std::string& content_type) {
Form f;
f.file("file", filename, content_type, data);
return invoke_form(Op::kCreateContainerFile, {{"container_id", container_id}}, f);
}
Json ContainerFilesApi::retrieve(const std::string& container_id, const std::string& file_id) {
return invoke(Op::kRetrieveContainerFile, {{"container_id", container_id}, {"file_id", file_id}});
}
Json ContainerFilesApi::list(const std::string& container_id, const Json& query) {
return invoke(Op::kListContainerFiles, {{"container_id", container_id}}, nullptr, query);
}
std::string ContainerFilesApi::content(const std::string& container_id, const std::string& file_id) {
return invoke_raw(Op::kRetrieveContainerFileContent, {{"container_id", container_id}, {"file_id", file_id}})
.body;
}
Json ContainerFilesApi::remove(const std::string& container_id, const std::string& file_id) {
return invoke(Op::kDeleteContainerFile, {{"container_id", container_id}, {"file_id", file_id}});
}
} // namespace molo::openai::platform
+41
View File
@@ -0,0 +1,41 @@
#pragma once
// Containers (code interpreter sandboxes): CRUD, files in a container
// (attach an existing File or upload bytes), file content download.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class ContainersApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid (container sessions are billed). body: name (required),
// expires_after, file_ids, memory_limit, network_policy, skills.
Json create(const Json& body);
Json retrieve(const std::string& container_id);
Json list(const Json& query = Json::object()); // after, limit, name, order
// Destructive.
Json remove(const std::string& container_id);
};
class ContainerFilesApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. Attaches an existing File object ({"file_id":...}).
Json create_from_file(const std::string& container_id, const std::string& file_id);
// Paid. Uploads bytes as multipart field "file".
Json create_upload(const std::string& container_id, const std::string& filename, const std::string& data,
const std::string& content_type = "application/octet-stream");
Json retrieve(const std::string& container_id, const std::string& file_id);
Json list(const std::string& container_id, const Json& query = Json::object());
std::string content(const std::string& container_id, const std::string& file_id);
// Destructive.
Json remove(const std::string& container_id, const std::string& file_id);
};
} // namespace molo::openai::platform
+52
View File
@@ -0,0 +1,52 @@
#include "openai/platform/conversations_api.h"
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
namespace {
void check_items(const Json& body) {
const Json* items = body.is_object() ? body.find("items") : nullptr;
if (!items) return;
if (!items->is_array()) throw llm::ApiError::invalid("openai", "items must be an array");
if (items->as_array().size() > Caps::kMaxConversationItemsPerCall)
throw llm::ApiError::invalid("openai", "at most 20 items can be added at a time");
}
} // namespace
Json ConversationsApi::create(const Json& body) {
check_items(body);
return invoke(Op::kCreateConversation, {}, &body);
}
Json ConversationsApi::retrieve(const std::string& id) {
return invoke(Op::kGetConversation, {{"conversation_id", id}});
}
Json ConversationsApi::update(const std::string& id, const Json& body) {
return invoke(Op::kUpdateConversation, {{"conversation_id", id}}, &body);
}
Json ConversationsApi::remove(const std::string& id) {
return invoke(Op::kDeleteConversation, {{"conversation_id", id}});
}
Json ConversationItemsApi::create(const std::string& id, const Json& body, const Json& query) {
check_items(body);
return invoke(Op::kCreateConversationItems, {{"conversation_id", id}}, &body, query);
}
Json ConversationItemsApi::list(const std::string& id, const Json& query) {
return invoke(Op::kListConversationItems, {{"conversation_id", id}}, nullptr, query);
}
Json ConversationItemsApi::retrieve(const std::string& id, const std::string& item_id, const Json& query) {
return invoke(Op::kGetConversationItem, {{"conversation_id", id}, {"item_id", item_id}}, nullptr, query);
}
Json ConversationItemsApi::remove(const std::string& id, const std::string& item_id) {
return invoke(Op::kDeleteConversationItem, {{"conversation_id", id}, {"item_id", item_id}});
}
} // namespace molo::openai::platform
+36
View File
@@ -0,0 +1,36 @@
#pragma once
// Conversations: durable conversation objects and their items.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class ConversationsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// body (all optional): items, metadata.
Json create(const Json& body = Json::object());
Json retrieve(const std::string& conversation_id);
Json update(const std::string& conversation_id, const Json& body); // metadata
// Destructive: deletes the conversation and its items.
Json remove(const std::string& conversation_id);
};
class ConversationItemsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// body: items (required, at most 20 per call, enforced). query: include.
Json create(const std::string& conversation_id, const Json& body, const Json& query = Json::object());
Json list(const std::string& conversation_id, const Json& query = Json::object());
Json retrieve(const std::string& conversation_id, const std::string& item_id,
const Json& query = Json::object());
// Destructive: deletes one item.
Json remove(const std::string& conversation_id, const std::string& item_id);
};
} // namespace molo::openai::platform
+46
View File
@@ -0,0 +1,46 @@
#include "openai/platform/evals_api.h"
namespace molo::openai::platform {
Json EvalsApi::create(const Json& body) { return invoke(Op::kCreateEval, {}, &body); }
Json EvalsApi::retrieve(const std::string& id) { return invoke(Op::kGetEval, {{"eval_id", id}}); }
Json EvalsApi::update(const std::string& id, const Json& body) {
return invoke(Op::kUpdateEval, {{"eval_id", id}}, &body);
}
Json EvalsApi::list(const Json& query) { return invoke(Op::kListEvals, {}, nullptr, query); }
Json EvalsApi::remove(const std::string& id) { return invoke(Op::kDeleteEval, {{"eval_id", id}}); }
Json EvalRunsApi::create(const std::string& eval_id, const Json& body) {
return invoke(Op::kCreateEvalRun, {{"eval_id", eval_id}}, &body);
}
Json EvalRunsApi::retrieve(const std::string& eval_id, const std::string& run_id) {
return invoke(Op::kGetEvalRun, {{"eval_id", eval_id}, {"run_id", run_id}});
}
Json EvalRunsApi::list(const std::string& eval_id, const Json& query) {
return invoke(Op::kGetEvalRuns, {{"eval_id", eval_id}}, nullptr, query);
}
Json EvalRunsApi::cancel(const std::string& eval_id, const std::string& run_id) {
return invoke(Op::kCancelEvalRun, {{"eval_id", eval_id}, {"run_id", run_id}});
}
Json EvalRunsApi::remove(const std::string& eval_id, const std::string& run_id) {
return invoke(Op::kDeleteEvalRun, {{"eval_id", eval_id}, {"run_id", run_id}});
}
Json EvalRunsApi::output_items(const std::string& eval_id, const std::string& run_id, const Json& query) {
return invoke(Op::kGetEvalRunOutputItems, {{"eval_id", eval_id}, {"run_id", run_id}}, nullptr, query);
}
Json EvalRunsApi::output_item(const std::string& eval_id, const std::string& run_id, const std::string& item_id) {
return invoke(Op::kGetEvalRunOutputItem,
{{"eval_id", eval_id}, {"run_id", run_id}, {"output_item_id", item_id}});
}
} // namespace molo::openai::platform
+41
View File
@@ -0,0 +1,41 @@
#pragma once
// Evals: definitions (CRUD), runs (create, retrieve, list, cancel, delete)
// and per-run output items.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class EvalsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// body: data_source_config, testing_criteria (required), name, metadata.
Json create(const Json& body);
Json retrieve(const std::string& eval_id);
Json update(const std::string& eval_id, const Json& body); // name, metadata
Json list(const Json& query = Json::object()); // after, limit, order, order_by
// Destructive: deletes the eval and its runs.
Json remove(const std::string& eval_id);
};
class EvalRunsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid (runs the data source and graders). body: data_source (required),
// name, metadata.
Json create(const std::string& eval_id, const Json& body);
Json retrieve(const std::string& eval_id, const std::string& run_id);
Json list(const std::string& eval_id, const Json& query = Json::object()); // status too
// Destructive.
Json cancel(const std::string& eval_id, const std::string& run_id);
Json remove(const std::string& eval_id, const std::string& run_id);
Json output_items(const std::string& eval_id, const std::string& run_id, const Json& query = Json::object());
Json output_item(const std::string& eval_id, const std::string& run_id, const std::string& item_id);
};
} // namespace molo::openai::platform
+220
View File
@@ -0,0 +1,220 @@
#include "openai/platform/event_socket.h"
#include <chrono>
#include <cstring>
#include "google/live/ws_error.h"
#include "google/live/ws_handshake.h"
#include "google/live/ws_url.h"
#include "llm/api_client.h"
#include "llm/config.h"
#include "llm/error.h"
#include "openai/platform/caps.h"
#include "openai/platform/calls.h"
namespace molo::openai::platform {
using clients::body::Json;
namespace live = google::live;
namespace {
const WsEndpointSpec kEndpoints[] = {
#define OAI_WS(en, path, query, header, source) {WsKind::en, path, query, header, source},
#include "openai/platform/ws_table.inc"
#undef OAI_WS
};
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
long long now_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
[[noreturn]] void rethrow(const live::WsError& e) {
int status = 502;
std::string type = "UNAVAILABLE";
switch (e.kind()) {
case live::WsError::Kind::Invalid: status = 400; type = "INVALID_ARGUMENT"; break;
case live::WsError::Kind::Timeout: status = 504; type = "DEADLINE_EXCEEDED"; break;
case live::WsError::Kind::Handshake:
if (e.http_status() >= 400) {
status = e.http_status();
type = status == 401 ? "authentication_error" : "HANDSHAKE_REFUSED";
} else {
type = "HANDSHAKE_FAILED";
}
break;
case live::WsError::Kind::Closed: type = "CLOSED"; break;
case live::WsError::Kind::TooBig: type = "EVENT_TOO_BIG"; break;
default: break;
}
throw llm::ApiError("openai", status, type, e.what());
}
} // namespace
std::size_t EventSocket::endpoint_count() { return sizeof(kEndpoints) / sizeof(kEndpoints[0]); }
const WsEndpointSpec& EventSocket::endpoint(WsKind kind) { return kEndpoints[static_cast<std::size_t>(kind)]; }
Profile EventSocket::profile_of(WsKind kind) {
switch (kind) {
case WsKind::kRealtime: return Profile::kGa;
case WsKind::kRealtimeBeta: return Profile::kBeta;
case WsKind::kRealtimeTranslation: return Profile::kTranslation;
case WsKind::kLiveSession: return Profile::kLive;
case WsKind::kLiveSideband: return Profile::kLiveSideband;
case WsKind::kLiveFork: return Profile::kLiveFork;
}
return Profile::kGa;
}
std::string EventSocket::url_for(WsKind kind, const std::string& base, const WsParams& p) {
const WsEndpointSpec& e = endpoint(kind);
if (base.compare(0, 6, "wss://") != 0 && base.compare(0, 5, "ws://") != 0) bad("base_url must be ws:// or wss://");
if (base.find('?') != std::string::npos || base.find('#') != std::string::npos || base.back() == '/')
bad("base_url must have no query, fragment or trailing slash");
std::string path = e.path;
std::string query;
auto add = [&](const std::string& k, const std::string& v) {
query += query.empty() ? '?' : '&';
query += k + "=" + Calls::percent_encode(v);
};
bool has_session = path.find("{session_id}") != std::string::npos;
if (has_session) {
if (!llm::Service::valid_id(p.session_id)) bad("bad session id");
path.replace(path.find("{session_id}"), 12, p.session_id);
} else if (!p.session_id.empty()) {
bad("this endpoint takes no session_id");
}
std::string q = e.query;
bool takes_model = q.find("model") != std::string::npos;
bool takes_call = q.find("call_id") != std::string::npos;
bool takes_graceful = q.find("graceful_close") != std::string::npos;
if (takes_model) {
if (p.model.empty() && p.call_id.empty()) bad("model (or call_id) is required");
if (!p.model.empty()) {
if (!llm::Service::valid_id(p.model)) bad("bad model id");
add("model", p.model);
}
} else if (!p.model.empty()) {
bad("this endpoint takes no model");
}
if (takes_call) {
if (!p.call_id.empty()) {
if (!llm::Service::valid_id(p.call_id)) bad("bad call id");
add("call_id", p.call_id);
}
} else if (!p.call_id.empty()) {
bad("this endpoint takes no call_id");
}
if (takes_graceful) {
if (p.graceful_close) add("graceful_close", "true");
} else if (p.graceful_close) {
bad("this endpoint takes no graceful_close");
}
return base + path + query;
}
EventSocket::EventSocket(WsKind kind, EventSocketOptions options) : kind_(kind), options_(std::move(options)) {
if (options_.api_key.empty()) options_.api_key = llm::Config::get("OPENAI_API_KEY");
}
EventSocket::~EventSocket() {
try {
close();
} catch (...) {
}
}
void EventSocket::connect(const WsParams& params) {
if (ws_) bad("socket already connected");
if (options_.api_key.empty()) throw llm::NotConfiguredError("openai", "OPENAI_API_KEY");
std::string url_text = url_for(kind_, options_.base_url, params);
live::WsHeaders headers;
headers.emplace_back("Authorization", "Bearer " + options_.api_key);
if (*endpoint(kind_).header) headers.emplace_back("OpenAI-Beta", endpoint(kind_).header);
if (!options_.organization.empty()) headers.emplace_back("OpenAI-Organization", options_.organization);
if (!options_.project.empty()) headers.emplace_back("OpenAI-Project", options_.project);
try {
live::WsUrl url = live::WsUrl::parse(url_text);
auto client = std::make_unique<live::WsClient>(
live::open_stream(url, options_.connect_timeout_ms, Caps::kHandshakeTimeoutMs, options_.tls));
client->handshake(url, headers, options_.handshake_timeout_ms);
client->set_read_timeout_ms(Caps::kReadPollMs);
ws_ = std::move(client);
} catch (const live::WsError& e) {
rethrow(e);
}
}
void EventSocket::send(const Json& event) {
if (!ws_) bad("socket is not connected");
const Json* t = event.is_object() ? event.find("type") : nullptr;
if (!t || !t->is_string()) bad("event needs a string type");
if (!Events::find(profile(), Direction::kClientEvent, t->as_string()))
bad("'" + t->as_string() + "' is not a client event of the " + Events::profile_name(profile()) + " profile");
std::string text = event.dump();
if (text.size() > Caps::kMaxEventBytes) bad("event over the byte cap");
try {
ws_->send_text(text);
} catch (const live::WsError& e) {
rethrow(e);
}
}
RunStats EventSocket::run(const Sink& sink, int seconds) {
if (!ws_) bad("socket is not connected");
if (seconds < 1 || seconds > Caps::kMaxSessionSeconds) bad("seconds must be 1 to 3600");
RunStats stats;
long long deadline = now_ms() + static_cast<long long>(seconds) * 1000;
try {
for (;;) {
if (now_ms() >= deadline) {
stats.stop_reason = "timeout";
return stats;
}
auto m = ws_->receive();
if (!m) continue;
if (m->kind == live::WsMessage::Kind::Close) {
stats.stop_reason = "closed";
stats.close_code = m->code;
return stats;
}
if (m->kind == live::WsMessage::Kind::Binary) continue;
++stats.events;
stats.bytes += m->data.size();
ParsedEvent ev = Events::parse_server(profile(), m->data);
if (!ev.known) ++stats.unknown;
if (!sink(ev)) {
stats.stop_reason = "sink_stopped";
return stats;
}
if (stats.events >= Caps::kMaxSessionEvents) {
stats.stop_reason = "event_cap";
return stats;
}
if (stats.bytes >= Caps::kMaxSessionBytes) {
stats.stop_reason = "byte_cap";
return stats;
}
}
} catch (const live::WsError& e) {
rethrow(e);
}
}
void EventSocket::close() {
if (!ws_) return;
try {
ws_->close(1000, "");
} catch (...) {
}
ws_->shutdown();
ws_.reset();
}
} // namespace molo::openai::platform
+92
View File
@@ -0,0 +1,92 @@
#pragma once
// EventSocket: the WebSocket sessions of the platform (Realtime GA and beta,
// Realtime translation, Live primary / sideband / fork) over the RFC 6455
// client of src/google/live (WsClient, reused, not copied). Endpoints come
// from ws_table.inc (checked against the SDK source). One thread runs
// run(); send() may be called from any thread.
#include <functional>
#include <memory>
#include <string>
#include "clients/body/json.h"
#include "google/live/ws_client.h"
#include "google/live/ws_stream.h"
#include "openai/platform/events.h"
namespace molo::openai::platform {
enum class WsKind : std::uint8_t {
#define OAI_WS(en, ...) en,
#include "openai/platform/ws_table.inc"
#undef OAI_WS
};
struct WsEndpointSpec {
WsKind kind;
const char* path; // "/live/sessions/{session_id}/attach"
const char* query; // comma list of accepted query parameters
const char* header; // OpenAI-Beta value, or ""
const char* source; // SDK file the endpoint was read from
};
struct WsParams {
std::string model; // /realtime, /realtime/translations
std::string call_id; // /realtime (attach to a SIP / WebRTC call)
std::string session_id; // /live/sessions/{session_id}/attach and /fork
bool graceful_close = false; // sideband only
};
struct EventSocketOptions {
std::string base_url = "wss://api.openai.com/v1"; // ws:// only for tests
std::string api_key; // empty: Config OPENAI_API_KEY
std::string organization; // optional header
std::string project; // optional header
int connect_timeout_ms = 10000;
int handshake_timeout_ms = 10000;
google::live::TlsFactory tls = nullptr; // null: wolfSSL
};
struct RunStats {
std::size_t events = 0;
std::size_t bytes = 0;
std::size_t unknown = 0; // events whose type is not in the catalog
std::string stop_reason; // sink_stopped, closed, timeout, event_cap, byte_cap
int close_code = 0;
};
class EventSocket {
public:
using Sink = std::function<bool(const ParsedEvent&)>; // false: stop
EventSocket(WsKind kind, EventSocketOptions options);
~EventSocket();
static const WsEndpointSpec& endpoint(WsKind kind);
static std::size_t endpoint_count();
static Profile profile_of(WsKind kind);
// The URL without credentials. Validates every parameter the endpoint takes
// (missing or extra parameters throw llm::ApiError::invalid).
static std::string url_for(WsKind kind, const std::string& base_url, const WsParams& params);
Profile profile() const { return profile_of(kind_); }
bool connected() const { return ws_ != nullptr; }
// Connects and upgrades. Throws llm::NotConfiguredError (503) before any
// traffic when there is no key; ApiError 401/403/... for a refused upgrade.
void connect(const WsParams& params);
// Validates against the profile's client catalog by type, then sends.
void send(const clients::body::Json& client_event);
// Receives until the sink returns false, the server closes, `seconds`
// pass (1..Caps::kMaxSessionSeconds) or a cap (events / bytes) is hit.
RunStats run(const Sink& sink, int seconds);
void close();
private:
WsKind kind_;
EventSocketOptions options_;
std::unique_ptr<google::live::WsClient> ws_;
};
} // namespace molo::openai::platform
+132
View File
@@ -0,0 +1,132 @@
#include "openai/platform/events.h"
#include <cstring>
#include "llm/api_client.h"
#include "llm/error.h"
#include "openai/platform/caps.h"
#include "openai/platform/calls.h"
#include "security/crypto/Base64.h"
namespace molo::openai::platform {
using clients::body::Json;
namespace {
const EventSpec kEvents[] = {
#define OAI_EVENT(en, type, prof, dir, schema, fields, required) \
{EventId::en, type, Profile::prof, Direction::dir, schema, fields, required},
#include "openai/platform/events_table.inc"
#undef OAI_EVENT
};
constexpr std::size_t kEventCount = sizeof(kEvents) / sizeof(kEvents[0]);
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
bool csv_has(const char* csv, const std::string& item) {
for (const std::string& f : Calls::list(csv))
if (f == item) return true;
return false;
}
} // namespace
std::size_t Events::count() { return kEventCount; }
const EventSpec& Events::at(std::size_t i) {
if (i >= kEventCount) bad("event index out of range");
return kEvents[i];
}
const EventSpec* Events::find(Profile p, Direction d, std::string_view type) {
for (const EventSpec& e : kEvents)
if (e.profile == p && e.direction == d && type == e.type) return &e;
return nullptr;
}
std::vector<const EventSpec*> Events::catalog(Profile p, Direction d) {
std::vector<const EventSpec*> out;
for (const EventSpec& e : kEvents)
if (e.profile == p && e.direction == d) out.push_back(&e);
return out;
}
const char* Events::profile_name(Profile p) {
switch (p) {
case Profile::kGa: return "ga";
case Profile::kBeta: return "beta";
case Profile::kTranslation: return "translation";
case Profile::kLive: return "live";
case Profile::kLiveFork: return "live_fork";
case Profile::kLiveSideband: return "live_sideband";
}
return "ga";
}
bool Events::parse_profile(const std::string& name, Profile& out) {
for (Profile p : {Profile::kGa, Profile::kBeta, Profile::kTranslation, Profile::kLive, Profile::kLiveFork,
Profile::kLiveSideband})
if (name == profile_name(p)) {
out = p;
return true;
}
return false;
}
Json Events::make_client(Profile profile, const std::string& type, const Json& fields,
const std::string& event_id) {
const EventSpec* spec = find(profile, Direction::kClientEvent, type);
if (!spec) bad("'" + type + "' is not a client event of the " + profile_name(profile) + " profile");
if (!fields.is_object()) bad("event fields must be a JSON object");
Json out = Json::object();
for (const auto& [k, v] : fields.as_object()) {
if (k == "type") bad("'type' is set by the event name");
if (!csv_has(spec->fields, k)) bad("field '" + k + "' is not part of " + spec->schema);
out.set(k, v);
}
if (!event_id.empty()) {
if (event_id.size() > Caps::kMaxEventIdBytes) bad("event_id over 512 bytes");
if (!csv_has(spec->fields, "event_id")) bad(std::string(spec->schema) + " has no event_id");
out.set("event_id", Json(event_id));
}
out.set("type", Json(type));
for (const std::string& r : Calls::list(spec->required))
if (!out.has(r)) bad("missing required field '" + r + "'");
if (out.dump().size() > Caps::kMaxEventBytes) bad("event over the byte cap");
return out;
}
Json Events::audio_append(Profile profile, const std::string& raw) {
if (raw.empty() || raw.size() > Caps::kMaxAudioChunkBytes) bad("audio chunk must be 1 byte to 1 MiB");
const char* type = "input_audio_buffer.append";
if (profile == Profile::kTranslation) type = "session.input_audio_buffer.append";
if (profile == Profile::kLive || profile == Profile::kLiveFork) type = "session.input_audio.append";
Json f = Json::object();
f.set("audio", Json(security::crypto::Base64::encode(raw)));
return make_client(profile, type, f);
}
ParsedEvent Events::parse_server(Profile profile, const std::string& text) {
ParsedEvent ev;
try {
ev.json = Json::parse(text);
} catch (const std::exception& e) {
throw llm::ApiError("openai", 502, "BAD_EVENT", std::string("event is not JSON: ") + e.what());
}
const Json* t = ev.json.is_object() ? ev.json.find("type") : nullptr;
if (!t || !t->is_string()) throw llm::ApiError("openai", 502, "BAD_EVENT", "event has no string type");
ev.type = t->as_string();
ev.spec = find(profile, Direction::kServerEvent, ev.type);
ev.known = ev.spec != nullptr;
if (const Json* id = ev.json.find("event_id"); id && id->is_string()) ev.event_id = id->as_string();
if (ev.type == "error") {
ev.is_error = true;
if (const Json* e = ev.json.find("error"); e && e->is_object())
ev.error_message = llm::Service::string_at(*e, "message");
}
return ev;
}
} // namespace molo::openai::platform
+72
View File
@@ -0,0 +1,72 @@
#pragma once
// The WebSocket event catalogs (events_table.inc, checked against the spec's
// Realtime and Live event schemas by tools/check_spec.py): client events are
// built and validated here, server events are classified here.
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <vector>
#include "clients/body/json.h"
namespace molo::openai::platform {
enum class Profile : std::uint8_t { kGa, kBeta, kTranslation, kLive, kLiveFork, kLiveSideband };
enum class Direction : std::uint8_t { kClientEvent, kServerEvent };
enum class EventId : std::uint16_t {
#define OAI_EVENT(en, ...) en,
#include "openai/platform/events_table.inc"
#undef OAI_EVENT
};
struct EventSpec {
EventId id;
const char* type; // "session.update"
Profile profile;
Direction direction;
const char* schema; // components.schemas name
const char* fields; // comma list: every top-level field
const char* required;
};
struct ParsedEvent {
bool known = false; // type is in the catalog of the profile
std::string type;
const EventSpec* spec = nullptr;
clients::body::Json json;
std::string event_id;
bool is_error = false; // type "error" (or the Live error event)
std::string error_message; // message of an error event
};
class Events {
public:
static std::size_t count();
static const EventSpec& at(std::size_t index);
static const EventSpec* find(Profile profile, Direction direction, std::string_view type);
static std::vector<const EventSpec*> catalog(Profile profile, Direction direction);
static const char* profile_name(Profile profile);
static bool parse_profile(const std::string& name, Profile& out);
// {"type":..., "event_id":..., ...fields}. Throws llm::ApiError::invalid
// when the type is not a client event of the profile, a field is not in the
// schema, a required field is missing (besides type), event_id is over 512
// bytes, or the serialised event passes Caps::kMaxEventBytes.
static clients::body::Json make_client(Profile profile, const std::string& type,
const clients::body::Json& fields = clients::body::Json::object(),
const std::string& event_id = "");
// input_audio_buffer.append (GA / beta) or session.input_audio_buffer.append
// (translation) or input_audio.append (Live): base64 of raw audio, at most
// Caps::kMaxAudioChunkBytes per call.
static clients::body::Json audio_append(Profile profile, const std::string& raw_audio);
// Throws llm::ApiError (502 BAD_EVENT) when the text is not a JSON object
// with a string `type`. Unknown types are returned with known == false.
static ParsedEvent parse_server(Profile profile, const std::string& text);
};
} // namespace molo::openai::platform
+217
View File
@@ -0,0 +1,217 @@
OAI_EVENT(kBetaClientEventConversationItemCreate, "conversation.item.create", kBeta, kClientEvent, "RealtimeBetaClientEventConversationItemCreate", "event_id,item,previous_item_id,type", "item,type")
OAI_EVENT(kBetaClientEventConversationItemDelete, "conversation.item.delete", kBeta, kClientEvent, "RealtimeBetaClientEventConversationItemDelete", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kBetaClientEventConversationItemRetrieve, "conversation.item.retrieve", kBeta, kClientEvent, "RealtimeBetaClientEventConversationItemRetrieve", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kBetaClientEventConversationItemTruncate, "conversation.item.truncate", kBeta, kClientEvent, "RealtimeBetaClientEventConversationItemTruncate", "audio_end_ms,content_index,event_id,item_id,type", "audio_end_ms,content_index,item_id,type")
OAI_EVENT(kBetaClientEventInputAudioBufferAppend, "input_audio_buffer.append", kBeta, kClientEvent, "RealtimeBetaClientEventInputAudioBufferAppend", "audio,event_id,type", "audio,type")
OAI_EVENT(kBetaClientEventInputAudioBufferClear, "input_audio_buffer.clear", kBeta, kClientEvent, "RealtimeBetaClientEventInputAudioBufferClear", "event_id,type", "type")
OAI_EVENT(kBetaClientEventInputAudioBufferCommit, "input_audio_buffer.commit", kBeta, kClientEvent, "RealtimeBetaClientEventInputAudioBufferCommit", "event_id,type", "type")
OAI_EVENT(kBetaClientEventOutputAudioBufferClear, "output_audio_buffer.clear", kBeta, kClientEvent, "RealtimeBetaClientEventOutputAudioBufferClear", "event_id,type", "type")
OAI_EVENT(kBetaClientEventResponseCancel, "response.cancel", kBeta, kClientEvent, "RealtimeBetaClientEventResponseCancel", "event_id,response_id,type", "type")
OAI_EVENT(kBetaClientEventResponseCreate, "response.create", kBeta, kClientEvent, "RealtimeBetaClientEventResponseCreate", "event_id,response,type", "type")
OAI_EVENT(kBetaClientEventSessionUpdate, "session.update", kBeta, kClientEvent, "RealtimeBetaClientEventSessionUpdate", "event_id,session,type", "session,type")
OAI_EVENT(kBetaClientEventTranscriptionSessionUpdate, "transcription_session.update", kBeta, kClientEvent, "RealtimeBetaClientEventTranscriptionSessionUpdate", "event_id,session,type", "session,type")
OAI_EVENT(kBetaServerEventConversationItemCreated, "conversation.item.created", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemCreated", "event_id,item,previous_item_id,type", "event_id,item,type")
OAI_EVENT(kBetaServerEventConversationItemDeleted, "conversation.item.deleted", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemDeleted", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kBetaServerEventConversationItemInputAudioTranscriptionCompleted, "conversation.item.input_audio_transcription.completed", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemInputAudioTranscriptionCompleted", "content_index,event_id,item_id,languages,logprobs,transcript,type,usage", "content_index,event_id,item_id,transcript,type,usage")
OAI_EVENT(kBetaServerEventConversationItemInputAudioTranscriptionDelta, "conversation.item.input_audio_transcription.delta", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemInputAudioTranscriptionDelta", "content_index,delta,event_id,item_id,logprobs,type", "event_id,item_id,type")
OAI_EVENT(kBetaServerEventConversationItemInputAudioTranscriptionFailed, "conversation.item.input_audio_transcription.failed", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemInputAudioTranscriptionFailed", "content_index,error,event_id,item_id,type", "content_index,error,event_id,item_id,type")
OAI_EVENT(kBetaServerEventConversationItemInputAudioTranscriptionSegment, "conversation.item.input_audio_transcription.segment", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemInputAudioTranscriptionSegment", "content_index,end,event_id,id,item_id,speaker,start,text,type", "content_index,end,event_id,id,item_id,speaker,start,text,type")
OAI_EVENT(kBetaServerEventConversationItemRetrieved, "conversation.item.retrieved", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemRetrieved", "event_id,item,type", "event_id,item,type")
OAI_EVENT(kBetaServerEventConversationItemTruncated, "conversation.item.truncated", kBeta, kServerEvent, "RealtimeBetaServerEventConversationItemTruncated", "audio_end_ms,content_index,event_id,item_id,type", "audio_end_ms,content_index,event_id,item_id,type")
OAI_EVENT(kBetaServerEventError, "error", kBeta, kServerEvent, "RealtimeBetaServerEventError", "error,event_id,type", "error,event_id,type")
OAI_EVENT(kBetaServerEventInputAudioBufferCleared, "input_audio_buffer.cleared", kBeta, kServerEvent, "RealtimeBetaServerEventInputAudioBufferCleared", "event_id,type", "event_id,type")
OAI_EVENT(kBetaServerEventInputAudioBufferCommitted, "input_audio_buffer.committed", kBeta, kServerEvent, "RealtimeBetaServerEventInputAudioBufferCommitted", "event_id,item_id,previous_item_id,type", "event_id,item_id,type")
OAI_EVENT(kBetaServerEventInputAudioBufferSpeechStarted, "input_audio_buffer.speech_started", kBeta, kServerEvent, "RealtimeBetaServerEventInputAudioBufferSpeechStarted", "audio_start_ms,event_id,item_id,type", "audio_start_ms,event_id,item_id,type")
OAI_EVENT(kBetaServerEventInputAudioBufferSpeechStopped, "input_audio_buffer.speech_stopped", kBeta, kServerEvent, "RealtimeBetaServerEventInputAudioBufferSpeechStopped", "audio_end_ms,event_id,item_id,type", "audio_end_ms,event_id,item_id,type")
OAI_EVENT(kBetaServerEventMCPListToolsCompleted, "mcp_list_tools.completed", kBeta, kServerEvent, "RealtimeBetaServerEventMCPListToolsCompleted", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kBetaServerEventMCPListToolsFailed, "mcp_list_tools.failed", kBeta, kServerEvent, "RealtimeBetaServerEventMCPListToolsFailed", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kBetaServerEventMCPListToolsInProgress, "mcp_list_tools.in_progress", kBeta, kServerEvent, "RealtimeBetaServerEventMCPListToolsInProgress", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kBetaServerEventRateLimitsUpdated, "rate_limits.updated", kBeta, kServerEvent, "RealtimeBetaServerEventRateLimitsUpdated", "event_id,rate_limits,type", "event_id,rate_limits,type")
OAI_EVENT(kBetaServerEventResponseAudioDelta, "response.output_audio.delta", kBeta, kServerEvent, "RealtimeBetaServerEventResponseAudioDelta", "content_index,delta,event_id,item_id,output_index,response_id,type", "content_index,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseAudioDone, "response.output_audio.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseAudioDone", "content_index,event_id,item_id,output_index,response_id,type", "content_index,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseAudioTranscriptDelta, "response.output_audio_transcript.delta", kBeta, kServerEvent, "RealtimeBetaServerEventResponseAudioTranscriptDelta", "content_index,delta,event_id,item_id,output_index,response_id,type", "content_index,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseAudioTranscriptDone, "response.output_audio_transcript.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseAudioTranscriptDone", "content_index,event_id,item_id,output_index,response_id,transcript,type", "content_index,event_id,item_id,output_index,response_id,transcript,type")
OAI_EVENT(kBetaServerEventResponseContentPartAdded, "response.content_part.added", kBeta, kServerEvent, "RealtimeBetaServerEventResponseContentPartAdded", "content_index,event_id,item_id,output_index,part,response_id,type", "content_index,event_id,item_id,output_index,part,response_id,type")
OAI_EVENT(kBetaServerEventResponseContentPartDone, "response.content_part.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseContentPartDone", "content_index,event_id,item_id,output_index,part,response_id,type", "content_index,event_id,item_id,output_index,part,response_id,type")
OAI_EVENT(kBetaServerEventResponseCreated, "response.created", kBeta, kServerEvent, "RealtimeBetaServerEventResponseCreated", "event_id,response,type", "event_id,response,type")
OAI_EVENT(kBetaServerEventResponseDone, "response.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseDone", "event_id,response,type", "event_id,response,type")
OAI_EVENT(kBetaServerEventResponseFunctionCallArgumentsDelta, "response.function_call_arguments.delta", kBeta, kServerEvent, "RealtimeBetaServerEventResponseFunctionCallArgumentsDelta", "call_id,delta,event_id,item_id,output_index,response_id,type", "call_id,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseFunctionCallArgumentsDone, "response.function_call_arguments.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseFunctionCallArgumentsDone", "arguments,call_id,event_id,item_id,name,output_index,response_id,type", "arguments,call_id,event_id,item_id,name,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseMCPCallArgumentsDelta, "response.mcp_call_arguments.delta", kBeta, kServerEvent, "RealtimeBetaServerEventResponseMCPCallArgumentsDelta", "delta,event_id,item_id,obfuscation,output_index,response_id,type", "delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseMCPCallArgumentsDone, "response.mcp_call_arguments.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseMCPCallArgumentsDone", "arguments,event_id,item_id,output_index,response_id,type", "arguments,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseMCPCallCompleted, "response.mcp_call.completed", kBeta, kServerEvent, "RealtimeBetaServerEventResponseMCPCallCompleted", "event_id,item_id,output_index,type", "event_id,item_id,output_index,type")
OAI_EVENT(kBetaServerEventResponseMCPCallFailed, "response.mcp_call.failed", kBeta, kServerEvent, "RealtimeBetaServerEventResponseMCPCallFailed", "event_id,item_id,output_index,type", "event_id,item_id,output_index,type")
OAI_EVENT(kBetaServerEventResponseMCPCallInProgress, "response.mcp_call.in_progress", kBeta, kServerEvent, "RealtimeBetaServerEventResponseMCPCallInProgress", "event_id,item_id,output_index,type", "event_id,item_id,output_index,type")
OAI_EVENT(kBetaServerEventResponseOutputItemAdded, "response.output_item.added", kBeta, kServerEvent, "RealtimeBetaServerEventResponseOutputItemAdded", "event_id,item,output_index,response_id,type", "event_id,item,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseOutputItemDone, "response.output_item.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseOutputItemDone", "event_id,item,output_index,response_id,type", "event_id,item,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseTextDelta, "response.output_text.delta", kBeta, kServerEvent, "RealtimeBetaServerEventResponseTextDelta", "content_index,delta,event_id,item_id,output_index,response_id,type", "content_index,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kBetaServerEventResponseTextDone, "response.output_text.done", kBeta, kServerEvent, "RealtimeBetaServerEventResponseTextDone", "content_index,event_id,item_id,output_index,response_id,text,type", "content_index,event_id,item_id,output_index,response_id,text,type")
OAI_EVENT(kBetaServerEventSessionCreated, "session.created", kBeta, kServerEvent, "RealtimeBetaServerEventSessionCreated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kBetaServerEventSessionUpdated, "session.updated", kBeta, kServerEvent, "RealtimeBetaServerEventSessionUpdated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kBetaServerEventTranscriptionSessionCreated, "transcription_session.created", kBeta, kServerEvent, "RealtimeBetaServerEventTranscriptionSessionCreated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kBetaServerEventTranscriptionSessionUpdated, "transcription_session.updated", kBeta, kServerEvent, "RealtimeBetaServerEventTranscriptionSessionUpdated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kClientEventConversationItemCreate, "conversation.item.create", kGa, kClientEvent, "RealtimeClientEventConversationItemCreate", "event_id,item,previous_item_id,type", "item,type")
OAI_EVENT(kClientEventConversationItemDelete, "conversation.item.delete", kGa, kClientEvent, "RealtimeClientEventConversationItemDelete", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kClientEventConversationItemRetrieve, "conversation.item.retrieve", kGa, kClientEvent, "RealtimeClientEventConversationItemRetrieve", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kClientEventConversationItemTruncate, "conversation.item.truncate", kGa, kClientEvent, "RealtimeClientEventConversationItemTruncate", "audio_end_ms,content_index,event_id,item_id,type", "audio_end_ms,content_index,item_id,type")
OAI_EVENT(kClientEventInputAudioBufferAppend, "input_audio_buffer.append", kGa, kClientEvent, "RealtimeClientEventInputAudioBufferAppend", "audio,event_id,type", "audio,type")
OAI_EVENT(kClientEventInputAudioBufferClear, "input_audio_buffer.clear", kGa, kClientEvent, "RealtimeClientEventInputAudioBufferClear", "event_id,type", "type")
OAI_EVENT(kClientEventInputAudioBufferCommit, "input_audio_buffer.commit", kGa, kClientEvent, "RealtimeClientEventInputAudioBufferCommit", "event_id,type", "type")
OAI_EVENT(kClientEventOutputAudioBufferClear, "output_audio_buffer.clear", kGa, kClientEvent, "RealtimeClientEventOutputAudioBufferClear", "event_id,type", "type")
OAI_EVENT(kClientEventResponseCancel, "response.cancel", kGa, kClientEvent, "RealtimeClientEventResponseCancel", "event_id,response_id,type", "type")
OAI_EVENT(kClientEventResponseCreate, "response.create", kGa, kClientEvent, "RealtimeClientEventResponseCreate", "event_id,response,type", "type")
OAI_EVENT(kClientEventSessionUpdate, "session.update", kGa, kClientEvent, "RealtimeClientEventSessionUpdate", "event_id,session,type", "session,type")
OAI_EVENT(kClientEventTranscriptionSessionUpdate, "transcription_session.update", kGa, kClientEvent, "RealtimeClientEventTranscriptionSessionUpdate", "event_id,session,type", "session,type")
OAI_EVENT(kServerEventConversationCreated, "conversation.created", kGa, kServerEvent, "RealtimeServerEventConversationCreated", "conversation,event_id,type", "conversation,event_id,type")
OAI_EVENT(kServerEventConversationItemAdded, "conversation.item.added", kGa, kServerEvent, "RealtimeServerEventConversationItemAdded", "event_id,item,previous_item_id,type", "event_id,item,type")
OAI_EVENT(kServerEventConversationItemCreated, "conversation.item.created", kGa, kServerEvent, "RealtimeServerEventConversationItemCreated", "event_id,item,previous_item_id,type", "event_id,item,type")
OAI_EVENT(kServerEventConversationItemDeleted, "conversation.item.deleted", kGa, kServerEvent, "RealtimeServerEventConversationItemDeleted", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kServerEventConversationItemDone, "conversation.item.done", kGa, kServerEvent, "RealtimeServerEventConversationItemDone", "event_id,item,previous_item_id,type", "event_id,item,type")
OAI_EVENT(kServerEventConversationItemInputAudioTranscriptionCompleted, "conversation.item.input_audio_transcription.completed", kGa, kServerEvent, "RealtimeServerEventConversationItemInputAudioTranscriptionCompleted", "content_index,event_id,item_id,languages,logprobs,transcript,type,usage", "content_index,event_id,item_id,transcript,type,usage")
OAI_EVENT(kServerEventConversationItemInputAudioTranscriptionDelta, "conversation.item.input_audio_transcription.delta", kGa, kServerEvent, "RealtimeServerEventConversationItemInputAudioTranscriptionDelta", "content_index,delta,event_id,item_id,logprobs,type", "event_id,item_id,type")
OAI_EVENT(kServerEventConversationItemInputAudioTranscriptionFailed, "conversation.item.input_audio_transcription.failed", kGa, kServerEvent, "RealtimeServerEventConversationItemInputAudioTranscriptionFailed", "content_index,error,event_id,item_id,type", "content_index,error,event_id,item_id,type")
OAI_EVENT(kServerEventConversationItemInputAudioTranscriptionSegment, "conversation.item.input_audio_transcription.segment", kGa, kServerEvent, "RealtimeServerEventConversationItemInputAudioTranscriptionSegment", "content_index,end,event_id,id,item_id,speaker,start,text,type", "content_index,end,event_id,id,item_id,speaker,start,text,type")
OAI_EVENT(kServerEventConversationItemRetrieved, "conversation.item.retrieved", kGa, kServerEvent, "RealtimeServerEventConversationItemRetrieved", "event_id,item,type", "event_id,item,type")
OAI_EVENT(kServerEventConversationItemTruncated, "conversation.item.truncated", kGa, kServerEvent, "RealtimeServerEventConversationItemTruncated", "audio_end_ms,content_index,event_id,item_id,type", "audio_end_ms,content_index,event_id,item_id,type")
OAI_EVENT(kServerEventError, "error", kGa, kServerEvent, "RealtimeServerEventError", "error,event_id,type", "error,event_id,type")
OAI_EVENT(kServerEventInputAudioBufferCleared, "input_audio_buffer.cleared", kGa, kServerEvent, "RealtimeServerEventInputAudioBufferCleared", "event_id,type", "event_id,type")
OAI_EVENT(kServerEventInputAudioBufferCommitted, "input_audio_buffer.committed", kGa, kServerEvent, "RealtimeServerEventInputAudioBufferCommitted", "event_id,item_id,previous_item_id,type", "event_id,item_id,type")
OAI_EVENT(kServerEventInputAudioBufferDtmfEventReceived, "input_audio_buffer.dtmf_event_received", kGa, kServerEvent, "RealtimeServerEventInputAudioBufferDtmfEventReceived", "event,received_at,type", "event,received_at,type")
OAI_EVENT(kServerEventInputAudioBufferSpeechStarted, "input_audio_buffer.speech_started", kGa, kServerEvent, "RealtimeServerEventInputAudioBufferSpeechStarted", "audio_start_ms,event_id,item_id,type", "audio_start_ms,event_id,item_id,type")
OAI_EVENT(kServerEventInputAudioBufferSpeechStopped, "input_audio_buffer.speech_stopped", kGa, kServerEvent, "RealtimeServerEventInputAudioBufferSpeechStopped", "audio_end_ms,event_id,item_id,type", "audio_end_ms,event_id,item_id,type")
OAI_EVENT(kServerEventInputAudioBufferTimeoutTriggered, "input_audio_buffer.timeout_triggered", kGa, kServerEvent, "RealtimeServerEventInputAudioBufferTimeoutTriggered", "audio_end_ms,audio_start_ms,event_id,item_id,type", "audio_end_ms,audio_start_ms,event_id,item_id,type")
OAI_EVENT(kServerEventMCPListToolsCompleted, "mcp_list_tools.completed", kGa, kServerEvent, "RealtimeServerEventMCPListToolsCompleted", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kServerEventMCPListToolsFailed, "mcp_list_tools.failed", kGa, kServerEvent, "RealtimeServerEventMCPListToolsFailed", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kServerEventMCPListToolsInProgress, "mcp_list_tools.in_progress", kGa, kServerEvent, "RealtimeServerEventMCPListToolsInProgress", "event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kServerEventOutputAudioBufferCleared, "output_audio_buffer.cleared", kGa, kServerEvent, "RealtimeServerEventOutputAudioBufferCleared", "event_id,response_id,type", "event_id,response_id,type")
OAI_EVENT(kServerEventOutputAudioBufferStarted, "output_audio_buffer.started", kGa, kServerEvent, "RealtimeServerEventOutputAudioBufferStarted", "event_id,response_id,type", "event_id,response_id,type")
OAI_EVENT(kServerEventOutputAudioBufferStopped, "output_audio_buffer.stopped", kGa, kServerEvent, "RealtimeServerEventOutputAudioBufferStopped", "event_id,response_id,type", "event_id,response_id,type")
OAI_EVENT(kServerEventRateLimitsUpdated, "rate_limits.updated", kGa, kServerEvent, "RealtimeServerEventRateLimitsUpdated", "event_id,rate_limits,type", "event_id,rate_limits,type")
OAI_EVENT(kServerEventResponseAudioDelta, "response.output_audio.delta", kGa, kServerEvent, "RealtimeServerEventResponseAudioDelta", "content_index,delta,event_id,item_id,output_index,response_id,type", "content_index,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseAudioDone, "response.output_audio.done", kGa, kServerEvent, "RealtimeServerEventResponseAudioDone", "content_index,event_id,item_id,output_index,response_id,type", "content_index,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseAudioTranscriptDelta, "response.output_audio_transcript.delta", kGa, kServerEvent, "RealtimeServerEventResponseAudioTranscriptDelta", "content_index,delta,event_id,item_id,output_index,response_id,type", "content_index,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseAudioTranscriptDone, "response.output_audio_transcript.done", kGa, kServerEvent, "RealtimeServerEventResponseAudioTranscriptDone", "content_index,event_id,item_id,output_index,response_id,transcript,type", "content_index,event_id,item_id,output_index,response_id,transcript,type")
OAI_EVENT(kServerEventResponseContentPartAdded, "response.content_part.added", kGa, kServerEvent, "RealtimeServerEventResponseContentPartAdded", "content_index,event_id,item_id,output_index,part,response_id,type", "content_index,event_id,item_id,output_index,part,response_id,type")
OAI_EVENT(kServerEventResponseContentPartDone, "response.content_part.done", kGa, kServerEvent, "RealtimeServerEventResponseContentPartDone", "content_index,event_id,item_id,output_index,part,response_id,type", "content_index,event_id,item_id,output_index,part,response_id,type")
OAI_EVENT(kServerEventResponseCreated, "response.created", kGa, kServerEvent, "RealtimeServerEventResponseCreated", "event_id,response,type", "event_id,response,type")
OAI_EVENT(kServerEventResponseDone, "response.done", kGa, kServerEvent, "RealtimeServerEventResponseDone", "event_id,response,type", "event_id,response,type")
OAI_EVENT(kServerEventResponseFunctionCallArgumentsDelta, "response.function_call_arguments.delta", kGa, kServerEvent, "RealtimeServerEventResponseFunctionCallArgumentsDelta", "call_id,delta,event_id,item_id,output_index,response_id,type", "call_id,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseFunctionCallArgumentsDone, "response.function_call_arguments.done", kGa, kServerEvent, "RealtimeServerEventResponseFunctionCallArgumentsDone", "arguments,call_id,event_id,item_id,name,output_index,response_id,type", "arguments,call_id,event_id,item_id,name,output_index,response_id,type")
OAI_EVENT(kServerEventResponseMCPCallArgumentsDelta, "response.mcp_call_arguments.delta", kGa, kServerEvent, "RealtimeServerEventResponseMCPCallArgumentsDelta", "delta,event_id,item_id,obfuscation,output_index,response_id,type", "delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseMCPCallArgumentsDone, "response.mcp_call_arguments.done", kGa, kServerEvent, "RealtimeServerEventResponseMCPCallArgumentsDone", "arguments,event_id,item_id,output_index,response_id,type", "arguments,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseMCPCallCompleted, "response.mcp_call.completed", kGa, kServerEvent, "RealtimeServerEventResponseMCPCallCompleted", "event_id,item_id,output_index,type", "event_id,item_id,output_index,type")
OAI_EVENT(kServerEventResponseMCPCallFailed, "response.mcp_call.failed", kGa, kServerEvent, "RealtimeServerEventResponseMCPCallFailed", "event_id,item_id,output_index,type", "event_id,item_id,output_index,type")
OAI_EVENT(kServerEventResponseMCPCallInProgress, "response.mcp_call.in_progress", kGa, kServerEvent, "RealtimeServerEventResponseMCPCallInProgress", "event_id,item_id,output_index,type", "event_id,item_id,output_index,type")
OAI_EVENT(kServerEventResponseOutputItemAdded, "response.output_item.added", kGa, kServerEvent, "RealtimeServerEventResponseOutputItemAdded", "event_id,item,output_index,response_id,type", "event_id,item,output_index,response_id,type")
OAI_EVENT(kServerEventResponseOutputItemDone, "response.output_item.done", kGa, kServerEvent, "RealtimeServerEventResponseOutputItemDone", "event_id,item,output_index,response_id,type", "event_id,item,output_index,response_id,type")
OAI_EVENT(kServerEventResponseTextDelta, "response.output_text.delta", kGa, kServerEvent, "RealtimeServerEventResponseTextDelta", "content_index,delta,event_id,item_id,output_index,response_id,type", "content_index,delta,event_id,item_id,output_index,response_id,type")
OAI_EVENT(kServerEventResponseTextDone, "response.output_text.done", kGa, kServerEvent, "RealtimeServerEventResponseTextDone", "content_index,event_id,item_id,output_index,response_id,text,type", "content_index,event_id,item_id,output_index,response_id,text,type")
OAI_EVENT(kServerEventSessionCreated, "session.created", kGa, kServerEvent, "RealtimeServerEventSessionCreated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kServerEventSessionUpdated, "session.updated", kGa, kServerEvent, "RealtimeServerEventSessionUpdated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kServerEventTranscriptionSessionUpdated, "transcription_session.updated", kGa, kServerEvent, "RealtimeServerEventTranscriptionSessionUpdated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kLiveClientEventCommentaryAppendParam, "session.commentary.append", kLive, kClientEvent, "LiveCommentaryAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveClientEventDecisionCreateParam, "decision.create", kLive, kClientEvent, "LiveDecisionCreateParam", "event_id,type", "event_id,type")
OAI_EVENT(kLiveClientEventDecisionItemCreateParam, "decision.item.create", kLive, kClientEvent, "LiveDecisionItemCreateParam", "event_id,item,type", "item,type")
OAI_EVENT(kLiveClientEventDecisionItemDeleteParam, "decision.item.delete", kLive, kClientEvent, "LiveDecisionItemDeleteParam", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kLiveClientEventInputAudioAppendEvent, "session.input_audio.append", kLive, kClientEvent, "LiveInputAudioAppendEvent", "audio,event_id,type", "audio,type")
OAI_EVENT(kLiveClientEventInputAudioMuteParam, "session.input_audio.mute", kLive, kClientEvent, "LiveInputAudioMuteParam", "event_id,type", "type")
OAI_EVENT(kLiveClientEventInputAudioUnmuteParam, "session.input_audio.unmute", kLive, kClientEvent, "LiveInputAudioUnmuteParam", "event_id,type", "type")
OAI_EVENT(kLiveClientEventInstructionsAppendParam, "session.instructions.append", kLive, kClientEvent, "LiveInstructionsAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveClientEventOutputAudioFixturePlayParam, "session.output_audio.fixture.play", kLive, kClientEvent, "LiveOutputAudioFixturePlayParam", "audio,event_id,type", "audio,type")
OAI_EVENT(kLiveClientEventResponseCreateParam, "response.create", kLive, kClientEvent, "LiveResponseCreateParam", "event_id,type", "type")
OAI_EVENT(kLiveClientEventResponseItemCreateParam, "response.item.create", kLive, kClientEvent, "LiveResponseItemCreateParam", "event_id,item,type", "item,type")
OAI_EVENT(kLiveClientEventSessionCloseParam, "session.close", kLive, kClientEvent, "LiveSessionCloseParam", "event_id,type", "type")
OAI_EVENT(kLiveClientEventSessionStartEvent, "session.start", kLive, kClientEvent, "LiveSessionStartEvent", "event_id,session,type", "session,type")
OAI_EVENT(kLiveClientEventSessionUpdateParam, "session.update", kLive, kClientEvent, "LiveSessionUpdateParam", "event_id,session,type", "session,type")
OAI_EVENT(kLiveClientEventThinkingAppendParam, "session.thinking.append", kLive, kClientEvent, "LiveThinkingAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveServerEventCommentaryAppended, "session.commentary.appended", kLive, kServerEvent, "LiveCommentaryAppended", "client_event_id,end_ms,event_id,start_ms,type", "end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveServerEventDecisionEvent, "decision.event", kLive, kServerEvent, "LiveDecisionEvent", "delegation_id,event,event_id,source_event_id,source_event_type,trigger_id,type", "delegation_id,event,event_id,source_event_id,source_event_type,trigger_id,type")
OAI_EVENT(kLiveServerEventDecisionItemCreated, "decision.item.created", kLive, kServerEvent, "LiveDecisionItemCreated", "client_event_id,event_id,item,type", "event_id,item,type")
OAI_EVENT(kLiveServerEventDecisionItemDeleted, "decision.item.deleted", kLive, kServerEvent, "LiveDecisionItemDeleted", "client_event_id,event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kLiveServerEventDelegationCreated, "session.delegation.created", kLive, kServerEvent, "LiveDelegationCreated", "client_event_id,delegation,event_id,offset_ms,type", "delegation,event_id,offset_ms,type")
OAI_EVENT(kLiveServerEventErrorEvent, "error", kLive, kServerEvent, "LiveErrorEvent", "client_event_id,error,event_id,type", "error,event_id,type")
OAI_EVENT(kLiveServerEventInfoEvent, "info", kLive, kServerEvent, "LiveInfoEvent", "client_event_id,code,event_id,message,type", "code,event_id,message,type")
OAI_EVENT(kLiveServerEventInputAudioAppend, "session.input_audio.append", kLive, kServerEvent, "LiveInputAudioAppend", "audio,type", "audio,type")
OAI_EVENT(kLiveServerEventInputAudioMuted, "session.input_audio.muted", kLive, kServerEvent, "LiveInputAudioMuted", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveServerEventInputAudioUnmuted, "session.input_audio.unmuted", kLive, kServerEvent, "LiveInputAudioUnmuted", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveServerEventInputTranscriptDelta, "session.input_transcript.delta", kLive, kServerEvent, "LiveInputTranscriptDelta", "client_event_id,delta,end_ms,event_id,start_ms,type", "delta,end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveServerEventInstructionsAppended, "session.instructions.appended", kLive, kServerEvent, "LiveInstructionsAppended", "client_event_id,end_ms,event_id,start_ms,type", "end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveServerEventOutputAudioDelta, "session.output_audio.delta", kLive, kServerEvent, "LiveOutputAudioDelta", "delta,end_ms,start_ms,type", "delta,type")
OAI_EVENT(kLiveServerEventOutputAudioFixturePlayed, "session.output_audio.fixture.played", kLive, kServerEvent, "LiveOutputAudioFixturePlayed", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveServerEventOutputAudioFixturePlaying, "session.output_audio.fixture.playing", kLive, kServerEvent, "LiveOutputAudioFixturePlaying", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveServerEventOutputTranscriptDelta, "session.output_transcript.delta", kLive, kServerEvent, "LiveOutputTranscriptDelta", "client_event_id,delta,end_ms,event_id,start_ms,type", "delta,end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveServerEventResponseEvent, "response.event", kLive, kServerEvent, "LiveResponseEvent", "client_event_id,delegation_id,event,event_id,type", "event,event_id,type")
OAI_EVENT(kLiveServerEventSessionClosed, "session.closed", kLive, kServerEvent, "LiveSessionClosed", "client_event_id,event_id,reason,session,type,usage", "event_id,reason,session,type,usage")
OAI_EVENT(kLiveServerEventSessionStarted, "session.started", kLive, kServerEvent, "LiveSessionStarted", "client_event_id,event_id,session,type", "event_id,session,type")
OAI_EVENT(kLiveServerEventSessionUpdated, "session.updated", kLive, kServerEvent, "LiveSessionUpdated", "client_event_id,event_id,session,type", "event_id,session,type")
OAI_EVENT(kLiveServerEventSessionUsageUpdated, "session.usage.updated", kLive, kServerEvent, "LiveSessionUsageUpdated", "client_event_id,context_window,event_id,type,usage", "event_id,type,usage")
OAI_EVENT(kLiveServerEventThinkingAppended, "session.thinking.appended", kLive, kServerEvent, "LiveThinkingAppended", "client_event_id,end_ms,event_id,start_ms,type", "end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveServerEventTransportAnswered, "transport.answered", kLive, kServerEvent, "LiveTransportAnswered", "event_id,session_id,type", "event_id,session_id,type")
OAI_EVENT(kLiveServerEventTransportDTMFReceived, "transport.dtmf.received", kLive, kServerEvent, "LiveTransportDTMFReceived", "event,event_id,type", "event,event_id,type")
OAI_EVENT(kLiveServerEventTransportDTMFSend, "transport.dtmf.send", kLive, kServerEvent, "LiveTransportDTMFSend", "client_event_id,event,event_id,type", "event,event_id,type")
OAI_EVENT(kLiveServerEventTransportFailed, "transport.failed", kLive, kServerEvent, "LiveTransportFailed", "error,event_id,session_id,type", "error,event_id,session_id,type")
OAI_EVENT(kLiveServerEventTransportRinging, "transport.ringing", kLive, kServerEvent, "LiveTransportRinging", "event_id,session_id,type", "event_id,session_id,type")
OAI_EVENT(kLiveForkClientEventCommentaryAppendParam, "session.commentary.append", kLiveFork, kClientEvent, "LiveCommentaryAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveForkClientEventDecisionCreateParam, "decision.create", kLiveFork, kClientEvent, "LiveDecisionCreateParam", "event_id,type", "event_id,type")
OAI_EVENT(kLiveForkClientEventDecisionItemCreateParam, "decision.item.create", kLiveFork, kClientEvent, "LiveDecisionItemCreateParam", "event_id,item,type", "item,type")
OAI_EVENT(kLiveForkClientEventDecisionItemDeleteParam, "decision.item.delete", kLiveFork, kClientEvent, "LiveDecisionItemDeleteParam", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kLiveForkClientEventForkSessionStartEvent, "session.start", kLiveFork, kClientEvent, "LiveForkSessionStartEvent", "event_id,session,type", "session,type")
OAI_EVENT(kLiveForkClientEventInputAudioAppendEvent, "session.input_audio.append", kLiveFork, kClientEvent, "LiveInputAudioAppendEvent", "audio,event_id,type", "audio,type")
OAI_EVENT(kLiveForkClientEventInputAudioMuteParam, "session.input_audio.mute", kLiveFork, kClientEvent, "LiveInputAudioMuteParam", "event_id,type", "type")
OAI_EVENT(kLiveForkClientEventInputAudioUnmuteParam, "session.input_audio.unmute", kLiveFork, kClientEvent, "LiveInputAudioUnmuteParam", "event_id,type", "type")
OAI_EVENT(kLiveForkClientEventInstructionsAppendParam, "session.instructions.append", kLiveFork, kClientEvent, "LiveInstructionsAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveForkClientEventOutputAudioFixturePlayParam, "session.output_audio.fixture.play", kLiveFork, kClientEvent, "LiveOutputAudioFixturePlayParam", "audio,event_id,type", "audio,type")
OAI_EVENT(kLiveForkClientEventResponseCreateParam, "response.create", kLiveFork, kClientEvent, "LiveResponseCreateParam", "event_id,type", "type")
OAI_EVENT(kLiveForkClientEventResponseItemCreateParam, "response.item.create", kLiveFork, kClientEvent, "LiveResponseItemCreateParam", "event_id,item,type", "item,type")
OAI_EVENT(kLiveForkClientEventSessionCloseParam, "session.close", kLiveFork, kClientEvent, "LiveSessionCloseParam", "event_id,type", "type")
OAI_EVENT(kLiveForkClientEventSessionUpdateParam, "session.update", kLiveFork, kClientEvent, "LiveSessionUpdateParam", "event_id,session,type", "session,type")
OAI_EVENT(kLiveForkClientEventThinkingAppendParam, "session.thinking.append", kLiveFork, kClientEvent, "LiveThinkingAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveSidebandClientEventCommentaryAppendParam, "session.commentary.append", kLiveSideband, kClientEvent, "LiveCommentaryAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveSidebandClientEventDecisionCreateParam, "decision.create", kLiveSideband, kClientEvent, "LiveDecisionCreateParam", "event_id,type", "event_id,type")
OAI_EVENT(kLiveSidebandClientEventDecisionItemCreateParam, "decision.item.create", kLiveSideband, kClientEvent, "LiveDecisionItemCreateParam", "event_id,item,type", "item,type")
OAI_EVENT(kLiveSidebandClientEventDecisionItemDeleteParam, "decision.item.delete", kLiveSideband, kClientEvent, "LiveDecisionItemDeleteParam", "event_id,item_id,type", "item_id,type")
OAI_EVENT(kLiveSidebandClientEventInputAudioMuteParam, "session.input_audio.mute", kLiveSideband, kClientEvent, "LiveInputAudioMuteParam", "event_id,type", "type")
OAI_EVENT(kLiveSidebandClientEventInputAudioUnmuteParam, "session.input_audio.unmute", kLiveSideband, kClientEvent, "LiveInputAudioUnmuteParam", "event_id,type", "type")
OAI_EVENT(kLiveSidebandClientEventInstructionsAppendParam, "session.instructions.append", kLiveSideband, kClientEvent, "LiveInstructionsAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveSidebandClientEventOutputAudioFixturePlayParam, "session.output_audio.fixture.play", kLiveSideband, kClientEvent, "LiveOutputAudioFixturePlayParam", "audio,event_id,type", "audio,type")
OAI_EVENT(kLiveSidebandClientEventResponseCreateParam, "response.create", kLiveSideband, kClientEvent, "LiveResponseCreateParam", "event_id,type", "type")
OAI_EVENT(kLiveSidebandClientEventResponseItemCreateParam, "response.item.create", kLiveSideband, kClientEvent, "LiveResponseItemCreateParam", "event_id,item,type", "item,type")
OAI_EVENT(kLiveSidebandClientEventSessionCloseParam, "session.close", kLiveSideband, kClientEvent, "LiveSessionCloseParam", "event_id,type", "type")
OAI_EVENT(kLiveSidebandClientEventSessionUpdateParam, "session.update", kLiveSideband, kClientEvent, "LiveSessionUpdateParam", "event_id,session,type", "session,type")
OAI_EVENT(kLiveSidebandClientEventThinkingAppendParam, "session.thinking.append", kLiveSideband, kClientEvent, "LiveThinkingAppendParam", "content,delegation_id,event_id,type", "content,delegation_id,type")
OAI_EVENT(kLiveSidebandServerEventCommentaryAppended, "session.commentary.appended", kLiveSideband, kServerEvent, "LiveCommentaryAppended", "client_event_id,end_ms,event_id,start_ms,type", "end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveSidebandServerEventDecisionEvent, "decision.event", kLiveSideband, kServerEvent, "LiveDecisionEvent", "delegation_id,event,event_id,source_event_id,source_event_type,trigger_id,type", "delegation_id,event,event_id,source_event_id,source_event_type,trigger_id,type")
OAI_EVENT(kLiveSidebandServerEventDecisionItemCreated, "decision.item.created", kLiveSideband, kServerEvent, "LiveDecisionItemCreated", "client_event_id,event_id,item,type", "event_id,item,type")
OAI_EVENT(kLiveSidebandServerEventDecisionItemDeleted, "decision.item.deleted", kLiveSideband, kServerEvent, "LiveDecisionItemDeleted", "client_event_id,event_id,item_id,type", "event_id,item_id,type")
OAI_EVENT(kLiveSidebandServerEventDelegationCreated, "session.delegation.created", kLiveSideband, kServerEvent, "LiveDelegationCreated", "client_event_id,delegation,event_id,offset_ms,type", "delegation,event_id,offset_ms,type")
OAI_EVENT(kLiveSidebandServerEventErrorEvent, "error", kLiveSideband, kServerEvent, "LiveErrorEvent", "client_event_id,error,event_id,type", "error,event_id,type")
OAI_EVENT(kLiveSidebandServerEventInfoEvent, "info", kLiveSideband, kServerEvent, "LiveInfoEvent", "client_event_id,code,event_id,message,type", "code,event_id,message,type")
OAI_EVENT(kLiveSidebandServerEventInputAudioAppend, "session.input_audio.append", kLiveSideband, kServerEvent, "LiveInputAudioAppend", "audio,type", "audio,type")
OAI_EVENT(kLiveSidebandServerEventInputAudioMuted, "session.input_audio.muted", kLiveSideband, kServerEvent, "LiveInputAudioMuted", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveSidebandServerEventInputAudioUnmuted, "session.input_audio.unmuted", kLiveSideband, kServerEvent, "LiveInputAudioUnmuted", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveSidebandServerEventInputTranscriptDelta, "session.input_transcript.delta", kLiveSideband, kServerEvent, "LiveInputTranscriptDelta", "client_event_id,delta,end_ms,event_id,start_ms,type", "delta,end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveSidebandServerEventInstructionsAppended, "session.instructions.appended", kLiveSideband, kServerEvent, "LiveInstructionsAppended", "client_event_id,end_ms,event_id,start_ms,type", "end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveSidebandServerEventOutputAudioDelta, "session.output_audio.delta", kLiveSideband, kServerEvent, "LiveOutputAudioDelta", "delta,end_ms,start_ms,type", "delta,type")
OAI_EVENT(kLiveSidebandServerEventOutputAudioFixturePlayed, "session.output_audio.fixture.played", kLiveSideband, kServerEvent, "LiveOutputAudioFixturePlayed", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveSidebandServerEventOutputAudioFixturePlaying, "session.output_audio.fixture.playing", kLiveSideband, kServerEvent, "LiveOutputAudioFixturePlaying", "client_event_id,event_id,type", "event_id,type")
OAI_EVENT(kLiveSidebandServerEventOutputTranscriptDelta, "session.output_transcript.delta", kLiveSideband, kServerEvent, "LiveOutputTranscriptDelta", "client_event_id,delta,end_ms,event_id,start_ms,type", "delta,end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveSidebandServerEventResponseEvent, "response.event", kLiveSideband, kServerEvent, "LiveResponseEvent", "client_event_id,delegation_id,event,event_id,type", "event,event_id,type")
OAI_EVENT(kLiveSidebandServerEventSessionClosed, "session.closed", kLiveSideband, kServerEvent, "LiveSessionClosed", "client_event_id,event_id,reason,session,type,usage", "event_id,reason,session,type,usage")
OAI_EVENT(kLiveSidebandServerEventSessionStarted, "session.started", kLiveSideband, kServerEvent, "LiveSessionStarted", "client_event_id,event_id,session,type", "event_id,session,type")
OAI_EVENT(kLiveSidebandServerEventSessionUpdated, "session.updated", kLiveSideband, kServerEvent, "LiveSessionUpdated", "client_event_id,event_id,session,type", "event_id,session,type")
OAI_EVENT(kLiveSidebandServerEventSessionUsageUpdated, "session.usage.updated", kLiveSideband, kServerEvent, "LiveSessionUsageUpdated", "client_event_id,context_window,event_id,type,usage", "event_id,type,usage")
OAI_EVENT(kLiveSidebandServerEventThinkingAppended, "session.thinking.appended", kLiveSideband, kServerEvent, "LiveThinkingAppended", "client_event_id,end_ms,event_id,start_ms,type", "end_ms,event_id,start_ms,type")
OAI_EVENT(kLiveSidebandServerEventTransportAnswered, "transport.answered", kLiveSideband, kServerEvent, "LiveTransportAnswered", "event_id,session_id,type", "event_id,session_id,type")
OAI_EVENT(kLiveSidebandServerEventTransportDTMFReceived, "transport.dtmf.received", kLiveSideband, kServerEvent, "LiveTransportDTMFReceived", "event,event_id,type", "event,event_id,type")
OAI_EVENT(kLiveSidebandServerEventTransportDTMFSend, "transport.dtmf.send", kLiveSideband, kServerEvent, "LiveTransportDTMFSend", "client_event_id,event,event_id,type", "event,event_id,type")
OAI_EVENT(kLiveSidebandServerEventTransportFailed, "transport.failed", kLiveSideband, kServerEvent, "LiveTransportFailed", "error,event_id,session_id,type", "error,event_id,session_id,type")
OAI_EVENT(kLiveSidebandServerEventTransportRinging, "transport.ringing", kLiveSideband, kServerEvent, "LiveTransportRinging", "event_id,session_id,type", "event_id,session_id,type")
OAI_EVENT(kTranslationClientEventInputAudioBufferAppend, "session.input_audio_buffer.append", kTranslation, kClientEvent, "RealtimeTranslationClientEventInputAudioBufferAppend", "audio,event_id,type", "audio,type")
OAI_EVENT(kTranslationClientEventSessionClose, "session.close", kTranslation, kClientEvent, "RealtimeTranslationClientEventSessionClose", "event_id,type", "type")
OAI_EVENT(kTranslationClientEventSessionUpdate, "session.update", kTranslation, kClientEvent, "RealtimeTranslationClientEventSessionUpdate", "event_id,session,type", "session,type")
OAI_EVENT(kTranslationServerEventSessionClosed, "session.closed", kTranslation, kServerEvent, "RealtimeTranslationServerEventSessionClosed", "event_id,type", "event_id,type")
OAI_EVENT(kTranslationServerEventSessionCreated, "session.created", kTranslation, kServerEvent, "RealtimeTranslationServerEventSessionCreated", "event_id,session,type", "event_id,session,type")
OAI_EVENT(kTranslationServerEventSessionInputTranscriptDelta, "session.input_transcript.delta", kTranslation, kServerEvent, "RealtimeTranslationServerEventSessionInputTranscriptDelta", "delta,elapsed_ms,event_id,type", "delta,event_id,type")
OAI_EVENT(kTranslationServerEventSessionOutputAudioDelta, "session.output_audio.delta", kTranslation, kServerEvent, "RealtimeTranslationServerEventSessionOutputAudioDelta", "channels,delta,elapsed_ms,event_id,format,sample_rate,type", "delta,event_id,type")
OAI_EVENT(kTranslationServerEventSessionOutputTranscriptDelta, "session.output_transcript.delta", kTranslation, kServerEvent, "RealtimeTranslationServerEventSessionOutputTranscriptDelta", "delta,elapsed_ms,event_id,type", "delta,event_id,type")
OAI_EVENT(kTranslationServerEventSessionUpdated, "session.updated", kTranslation, kServerEvent, "RealtimeTranslationServerEventSessionUpdated", "event_id,session,type", "event_id,session,type")
+44
View File
@@ -0,0 +1,44 @@
#include "openai/platform/files_api.h"
#include <algorithm>
#include "llm/error.h"
namespace molo::openai::platform {
bool FilesApi::valid_purpose(const std::string& p) {
static const char* kPurposes[] = {"assistants", "batch", "fine-tune", "vision", "user_data", "evals"};
return std::any_of(std::begin(kPurposes), std::end(kPurposes), [&](const char* k) { return p == k; });
}
Form FilesApi::make_form(const std::string& filename, const std::string& data, const std::string& purpose,
const std::string& content_type, const Json* expires_after) {
if (!valid_purpose(purpose))
throw llm::ApiError::invalid("openai", "purpose must be assistants, batch, fine-tune, vision, user_data or evals");
Form f;
f.file("file", filename, content_type, data);
f.text("purpose", purpose);
if (expires_after && !expires_after->is_null()) f.value("expires_after", *expires_after);
return f;
}
Json FilesApi::create(const std::string& filename, const std::string& data, const std::string& purpose,
const std::string& content_type, const Json* expires_after) {
return invoke_form(Op::kCreateFile, {}, make_form(filename, data, purpose, content_type, expires_after));
}
Json FilesApi::list(const Json& query) { return invoke(Op::kListFiles, {}, nullptr, query); }
Json FilesApi::retrieve(const std::string& file_id) {
return invoke(Op::kRetrieveFile, {{"file_id", file_id}});
}
std::string FilesApi::content(const std::string& file_id) {
return invoke_raw(Op::kDownloadFile, {{"file_id", file_id}}).body;
}
Json FilesApi::remove(const std::string& file_id) {
return invoke(Op::kDeleteFile, {{"file_id", file_id}});
}
} // namespace molo::openai::platform
+33
View File
@@ -0,0 +1,33 @@
#pragma once
// Files: upload (multipart), list, retrieve, delete, download content.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class FilesApi : public PlatformService {
public:
using PlatformService::PlatformService;
// purpose: assistants, batch, fine-tune, vision, user_data, evals.
// expires_after: {"anchor":"created_at","seconds":N} or null.
static bool valid_purpose(const std::string& purpose);
static Form make_form(const std::string& filename, const std::string& data,
const std::string& purpose, const std::string& content_type,
const Json* expires_after);
Json create(const std::string& filename, const std::string& data, const std::string& purpose,
const std::string& content_type = "application/octet-stream",
const Json* expires_after = nullptr);
Json list(const Json& query = Json::object());
Json retrieve(const std::string& file_id);
// Bytes of the file (purpose-dependent; capped by Caps::kMaxDownloadBytes).
std::string content(const std::string& file_id);
// Destructive: deletes the file.
Json remove(const std::string& file_id);
};
} // namespace molo::openai::platform
+64
View File
@@ -0,0 +1,64 @@
#include "openai/platform/fine_tuning_api.h"
#include "llm/error.h"
namespace molo::openai::platform {
Json FineTuningJobsApi::create(const Json& body) { return invoke(Op::kCreateFineTuningJob, {}, &body); }
Json FineTuningJobsApi::retrieve(const std::string& id) {
return invoke(Op::kRetrieveFineTuningJob, {{"fine_tuning_job_id", id}});
}
Json FineTuningJobsApi::list(const Json& query) {
return invoke(Op::kListPaginatedFineTuningJobs, {}, nullptr, query);
}
Json FineTuningJobsApi::events(const std::string& id, const Json& query) {
return invoke(Op::kListFineTuningEvents, {{"fine_tuning_job_id", id}}, nullptr, query);
}
Json FineTuningJobsApi::checkpoints(const std::string& id, const Json& query) {
return invoke(Op::kListFineTuningJobCheckpoints, {{"fine_tuning_job_id", id}}, nullptr, query);
}
Json FineTuningJobsApi::cancel(const std::string& id) {
return invoke(Op::kCancelFineTuningJob, {{"fine_tuning_job_id", id}});
}
Json FineTuningJobsApi::pause(const std::string& id) {
return invoke(Op::kPauseFineTuningJob, {{"fine_tuning_job_id", id}});
}
Json FineTuningJobsApi::resume(const std::string& id) {
return invoke(Op::kResumeFineTuningJob, {{"fine_tuning_job_id", id}});
}
Json FineTuningPermissionsApi::create(const std::string& checkpoint, const std::vector<std::string>& project_ids) {
if (project_ids.empty() || project_ids.size() > 100)
throw llm::ApiError::invalid("openai", "project_ids must hold 1 to 100 ids");
Json ids = Json::array();
for (const std::string& p : project_ids) {
check_id("project", p);
ids.push(Json(p));
}
Json body = Json::object();
body.set("project_ids", ids);
return invoke(Op::kCreateFineTuningCheckpointPermission, {{"fine_tuned_model_checkpoint", checkpoint}}, &body);
}
Json FineTuningPermissionsApi::list(const std::string& checkpoint, const Json& query) {
return invoke(Op::kListFineTuningCheckpointPermissions, {{"fine_tuned_model_checkpoint", checkpoint}},
nullptr, query);
}
Json FineTuningPermissionsApi::remove(const std::string& checkpoint, const std::string& permission_id) {
return invoke(Op::kDeleteFineTuningCheckpointPermission,
{{"fine_tuned_model_checkpoint", checkpoint}, {"permission_id", permission_id}});
}
Json GradersApi::run(const Json& body) { return invoke(Op::kRunGrader, {}, &body); }
Json GradersApi::validate(const Json& body) { return invoke(Op::kValidateGrader, {}, &body); }
} // namespace molo::openai::platform
+51
View File
@@ -0,0 +1,51 @@
#pragma once
// Fine-tuning: jobs (create, retrieve, list, cancel, pause, resume, events,
// checkpoints), checkpoint permissions, and the alpha graders.
#include <string>
#include <vector>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class FineTuningJobsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: model, training_file (required), validation_file, suffix,
// seed, method, hyperparameters, integrations, metadata (spec fields).
Json create(const Json& body);
Json retrieve(const std::string& job_id);
Json list(const Json& query = Json::object()); // after, limit, metadata
Json events(const std::string& job_id, const Json& query = Json::object());
Json checkpoints(const std::string& job_id, const Json& query = Json::object());
// Destructive: cancels a running job.
Json cancel(const std::string& job_id);
Json pause(const std::string& job_id);
Json resume(const std::string& job_id);
};
class FineTuningPermissionsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Gives other projects access to a fine-tuned checkpoint.
Json create(const std::string& checkpoint, const std::vector<std::string>& project_ids);
Json list(const std::string& checkpoint, const Json& query = Json::object());
// Destructive: revokes the permission.
Json remove(const std::string& checkpoint, const std::string& permission_id);
};
class GradersApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Alpha. Paid for model graders. run body: grader, model_sample (required),
// item; validate body: grader.
Json run(const Json& body);
Json validate(const Json& body);
};
} // namespace molo::openai::platform
+70
View File
@@ -0,0 +1,70 @@
#include "openai/platform/form.h"
#include <cmath>
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
using clients::body::Json;
namespace {
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
} // namespace
bool Form::valid_filename(const std::string& f) {
if (f.empty() || f.size() > Caps::kMaxFilenameBytes) return false;
for (unsigned char c : f)
if (c < 0x20 || c == 0x7f || c == '"' || c == '\\' || c == '/') return false;
return f != "." && f != "..";
}
void Form::note(const std::string& name) {
if (name.empty() || name.size() > 128) bad("bad form field name");
for (unsigned char c : name)
if (c < 0x21 || c == '"' || c == 0x7f) bad("bad form field name");
if (names_.size() >= Caps::kMaxFormFields) bad("too many form fields");
names_.push_back(name);
}
Form& Form::text(const std::string& name, const std::string& value) {
note(name);
if (payload_ + value.size() > Caps::kMaxFileBytes) bad("form over the byte cap");
payload_ += value.size();
body_.add_text(name, value);
return *this;
}
Form& Form::value(const std::string& name, const Json& v) {
if (v.is_null()) return *this;
if (v.is_string()) return text(name, v.as_string());
if (v.is_bool()) return text(name, v.as_bool() ? "true" : "false");
if (v.is_number()) {
double d = v.as_number();
return text(name, d == std::floor(d) && std::fabs(d) < 9e15 ? std::to_string(static_cast<long long>(d))
: std::to_string(d));
}
if (v.is_array()) {
for (const Json& item : v.as_array()) value(name + "[]", item);
return *this;
}
for (const auto& [k, item] : v.as_object()) value(name + "[" + k + "]", item);
return *this;
}
Form& Form::file(const std::string& name, const std::string& filename, const std::string& content_type,
const std::string& data) {
note(name);
if (!valid_filename(filename)) bad("bad filename");
for (unsigned char c : content_type)
if (c < 0x20 || c == 0x7f) bad("bad content type");
if (content_type.empty() || content_type.size() > 128) bad("bad content type");
if (data.size() > Caps::kMaxFileBytes || payload_ + data.size() > Caps::kMaxFileBytes)
bad("file over the byte cap");
payload_ += data.size();
body_.add_file(name, filename, content_type, data);
return *this;
}
} // namespace molo::openai::platform
+42
View File
@@ -0,0 +1,42 @@
#pragma once
// A multipart/form-data request body with this module's caps and OpenAI's
// bracket notation for nested values: object fields are sent as
// "expires_after[anchor]" and "expires_after[seconds]", file lists as
// "files[]" (the encoding the official SDKs use).
#include <string>
#include <vector>
#include "clients/body/form.h"
#include "clients/body/json.h"
namespace molo::openai::platform {
class Form {
public:
Form& text(const std::string& name, const std::string& value);
// Nested JSON becomes name[key] / name[] entries; scalars become text.
Form& value(const std::string& name, const clients::body::Json& value);
// Throws llm::ApiError::invalid on a bad filename (quotes, slashes,
// control characters, over 255 bytes), a bad content type, or when the
// payload would pass Caps::kMaxFileBytes.
Form& file(const std::string& name, const std::string& filename,
const std::string& content_type, const std::string& data);
const clients::body::MultipartBody& body() const { return body_; }
// Field names in the order added (as sent on the wire).
const std::vector<std::string>& names() const { return names_; }
std::size_t payload_bytes() const { return payload_; }
static bool valid_filename(const std::string& filename);
private:
void note(const std::string& name);
clients::body::MultipartBody body_;
std::vector<std::string> names_;
std::size_t payload_ = 0;
};
} // namespace molo::openai::platform
+39
View File
@@ -0,0 +1,39 @@
#include "openai/platform/live_api.h"
#include "llm/error.h"
namespace molo::openai::platform {
Json LiveApi::create(const Json& body) { return invoke(Op::kCreateLive, {}, &body); }
Json LiveApi::fork(const std::string& id, const Json& body) {
return invoke(Op::kForkLiveSession, {{"session_id", id}}, &body);
}
Json LiveApi::accept(const std::string& id, const Json& body) {
return invoke(Op::kAcceptLiveSession, {{"session_id", id}}, &body);
}
Json LiveApi::refer(const std::string& id, const std::string& target_uri) {
if (target_uri.empty() || target_uri.size() > 512)
throw llm::ApiError::invalid("openai", "target_uri must be 1 to 512 characters");
Json body = Json::object();
body.set("target_uri", Json(target_uri));
return invoke(Op::kReferLiveSession, {{"session_id", id}}, &body);
}
Json LiveApi::reject(const std::string& id, int status_code) {
if (status_code < 400 || status_code > 699)
throw llm::ApiError::invalid("openai", "status_code must be a SIP failure code, 400 to 699");
Json body = Json::object();
body.set("status_code", Json(status_code));
return invoke(Op::kRejectLiveSession, {{"session_id", id}}, &body);
}
Json LiveApi::hangup(const std::string& id) { return invoke(Op::kHangupLiveSession, {{"session_id", id}}); }
std::string LiveApi::recording(const std::string& id) {
return invoke_raw(Op::kDownloadLiveRecording, {{"session_id", id}}).body;
}
} // namespace molo::openai::platform
+33
View File
@@ -0,0 +1,33 @@
#pragma once
// Live API REST surface (the spec's /live/sessions): create a WebRTC live
// session, accept / fork / refer / reject / hang up, download the recording.
// The WebSocket side (session.start, sideband attach, fork socket) is in
// event_socket.h.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class LiveApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: session, transport (both required); transport carries the
// browser's SDP offer, the answer carries the SDP answer and session id.
Json create(const Json& body);
// Paid. body: transport (required), session.
Json fork(const std::string& session_id, const Json& body);
// Accepts an incoming call into the session. body: session (required).
Json accept(const std::string& session_id, const Json& body);
Json refer(const std::string& session_id, const std::string& target_uri);
// Destructive.
Json reject(const std::string& session_id, int status_code);
Json hangup(const std::string& session_id);
// audio/wav bytes of the recording (capped by llm::Caps).
std::string recording(const std::string& session_id);
};
} // namespace molo::openai::platform
+18
View File
@@ -0,0 +1,18 @@
#include "openai/platform/misc_api.h"
namespace molo::openai::platform {
Json SafetyApi::retrieve_case(const std::string& id) { return invoke(Op::kGetsafetycase, {{"id", id}}); }
Json SafetyApi::retrieve_alert(const std::string& id) { return invoke(Op::kGetprojectsafetyalert, {{"id", id}}); }
Json DecisionsApi::create(const Json& body) { return invoke(Op::kCreateDecision, {}, &body); }
Json ContentProvenanceApi::check(const std::string& filename, const std::string& data,
const std::string& content_type) {
Form f;
f.file("file", filename, content_type, data);
return invoke_form(Op::kCreatecontentprovenancecheck, {}, f);
}
} // namespace molo::openai::platform
+38
View File
@@ -0,0 +1,38 @@
#pragma once
// Small platform endpoints that belong to no larger area: safety cases and
// alerts (read only), decisions, content provenance checks.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class SafetyApi : public PlatformService {
public:
using PlatformService::PlatformService;
Json retrieve_case(const std::string& case_id);
Json retrieve_alert(const std::string& alert_id);
};
class DecisionsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid (model call). body: model, input, questions (required),
// prompt_cache_key, prompt_cache_options, safety_identifier.
Json create(const Json& body);
};
class ContentProvenanceApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. Uploads one file as multipart field "file".
Json check(const std::string& filename, const std::string& data,
const std::string& content_type = "application/octet-stream");
};
} // namespace molo::openai::platform
+48
View File
@@ -0,0 +1,48 @@
#include "openai/platform/platform.h"
#include "llm/config.h"
#include "llm/error.h"
namespace molo::openai::platform {
bool PlatformConfig::valid_header_id(const std::string& id) {
if (id.empty() || id.size() > 128) return false;
for (unsigned char c : id)
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-'))
return false;
return true;
}
llm::ApiClientConfig PlatformConfig::api_config() {
llm::ApiClientConfig c;
c.vendor = "openai";
c.base_url = kBaseUrl;
c.key_env = kKeyEnv;
c.auth = llm::AuthStyle::kBearer;
std::string org = llm::Config::get(kOrgEnv);
std::string project = llm::Config::get(kProjectEnv);
if (!org.empty()) {
if (!valid_header_id(org)) throw llm::ApiError::invalid("openai", std::string(kOrgEnv) + " is not a valid id");
c.default_headers["OpenAI-Organization"] = org;
}
if (!project.empty()) {
if (!valid_header_id(project))
throw llm::ApiError::invalid("openai", std::string(kProjectEnv) + " is not a valid id");
c.default_headers["OpenAI-Project"] = project;
}
return c;
}
Platform::Platform(std::shared_ptr<llm::ApiClient> c)
: files(c), uploads(c), batches(c), fine_tuning_jobs(c), fine_tuning_permissions(c), graders(c),
vector_stores(c), vector_store_files(c), vector_store_file_batches(c), evals(c), eval_runs(c),
conversations(c), conversation_items(c), containers(c), container_files(c), assistants(c),
threads(c), messages(c), runs(c), agents(c), agent_automations(c), agent_environments(c),
agent_sessions(c), vaults(c), skills(c), chatkit(c), realtime(c), live(c), safety(c), decisions(c),
content_provenance(c), generic(c), client_(std::move(c)) {}
std::shared_ptr<Platform> Platform::from_env() {
return std::make_shared<Platform>(llm::ApiClient::from_env(PlatformConfig::api_config()));
}
} // namespace molo::openai::platform
+89
View File
@@ -0,0 +1,89 @@
#pragma once
// Platform: one object holding every OpenAI platform service on one shared
// ApiClient. Credential env OPENAI_API_KEY; optional OPENAI_ORG_ID and
// OPENAI_PROJECT_ID become the OpenAI-Organization / OpenAI-Project headers.
#include <memory>
#include <string>
#include "llm/api_client.h"
#include "openai/platform/agents_api.h"
#include "openai/platform/assistants_api.h"
#include "openai/platform/batches_api.h"
#include "openai/platform/chatkit_api.h"
#include "openai/platform/containers_api.h"
#include "openai/platform/conversations_api.h"
#include "openai/platform/evals_api.h"
#include "openai/platform/files_api.h"
#include "openai/platform/fine_tuning_api.h"
#include "openai/platform/live_api.h"
#include "openai/platform/misc_api.h"
#include "openai/platform/realtime_api.h"
#include "openai/platform/skills_api.h"
#include "openai/platform/uploads_api.h"
#include "openai/platform/vaults_api.h"
#include "openai/platform/vector_stores_api.h"
namespace molo::openai::platform {
struct PlatformConfig {
static constexpr const char* kBaseUrl = "https://api.openai.com/v1";
static constexpr const char* kKeyEnv = "OPENAI_API_KEY";
static constexpr const char* kOrgEnv = "OPENAI_ORG_ID";
static constexpr const char* kProjectEnv = "OPENAI_PROJECT_ID";
// Bearer auth plus the optional organization / project headers. Throws
// llm::ApiError::invalid when an id holds anything but [A-Za-z0-9_-] or is
// over 128 bytes (a header value never carries CR or LF).
static llm::ApiClientConfig api_config();
static bool valid_header_id(const std::string& id);
};
class Platform {
public:
explicit Platform(std::shared_ptr<llm::ApiClient> client);
static std::shared_ptr<Platform> from_env();
bool configured() const { return client_->configured(); }
const std::shared_ptr<llm::ApiClient>& client() const { return client_; }
FilesApi files;
UploadsApi uploads;
BatchesApi batches;
FineTuningJobsApi fine_tuning_jobs;
FineTuningPermissionsApi fine_tuning_permissions;
GradersApi graders;
VectorStoresApi vector_stores;
VectorStoreFilesApi vector_store_files;
VectorStoreFileBatchesApi vector_store_file_batches;
EvalsApi evals;
EvalRunsApi eval_runs;
ConversationsApi conversations;
ConversationItemsApi conversation_items;
ContainersApi containers;
ContainerFilesApi container_files;
AssistantsApi assistants;
ThreadsApi threads;
MessagesApi messages;
RunsApi runs;
AgentsApi agents;
AgentAutomationsApi agent_automations;
AgentEnvironmentsApi agent_environments;
AgentSessionsApi agent_sessions;
VaultsApi vaults;
SkillsApi skills;
ChatKitApi chatkit;
RealtimeApi realtime;
LiveApi live;
SafetyApi safety;
DecisionsApi decisions;
ContentProvenanceApi content_provenance;
// Generic access by Op (table driven), for the demo and tools.
PlatformService generic;
private:
std::shared_ptr<llm::ApiClient> client_;
};
} // namespace molo::openai::platform
+83
View File
@@ -0,0 +1,83 @@
#include "openai/platform/realtime_api.h"
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
namespace {
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
void check_sdp(const std::string& sdp) {
if (sdp.empty() || sdp.size() > 256 * 1024) bad("sdp offer must be 1 byte to 256 KiB");
if (sdp.compare(0, 2, "v=") != 0) bad("sdp offer must start with 'v='");
}
CallAnswer answer_of(const llm::RawResponse& r) {
CallAnswer a;
a.sdp = r.body;
if (auto loc = r.headers.get("Location")) {
std::size_t slash = loc->rfind('/');
if (slash != std::string::npos) a.call_id = loc->substr(slash + 1);
}
return a;
}
} // namespace
Json RealtimeApi::create_client_secret(const Json& body) {
return invoke(Op::kCreateRealtimeClientSecret, {}, &body);
}
Json RealtimeApi::create_translation_client_secret(const Json& body) {
return invoke(Op::kCreateRealtimeTranslationClientSecret, {}, &body);
}
Json RealtimeApi::create_session(const Json& body) { return invoke(Op::kCreateRealtimeSession, {}, &body); }
Json RealtimeApi::create_transcription_session(const Json& body) {
return invoke(Op::kCreateRealtimeTranscriptionSession, {}, &body);
}
CallAnswer RealtimeApi::create_call(const std::string& sdp_offer) {
check_sdp(sdp_offer);
return answer_of(invoke_raw(Op::kCreateRealtimeCall, {}, Json::object(), &sdp_offer, "application/sdp"));
}
CallAnswer RealtimeApi::create_call(const std::string& sdp_offer, const Json& session) {
check_sdp(sdp_offer);
Form f;
f.text("sdp", sdp_offer);
f.text("session", session.dump());
Calls::check_form(Op::kCreateRealtimeCall, f.names());
std::string encoded = f.body().encode();
return answer_of(invoke_raw(Op::kCreateRealtimeCall, {}, Json::object(), &encoded, f.body().content_type()));
}
Json RealtimeApi::accept_call(const std::string& call_id, const Json& body) {
return invoke(Op::kAcceptRealtimeCall, {{"call_id", call_id}}, &body);
}
Json RealtimeApi::refer_call(const std::string& call_id, const std::string& target_uri) {
if (target_uri.empty() || target_uri.size() > 512) bad("target_uri must be 1 to 512 characters");
if (target_uri.compare(0, 4, "tel:") != 0 && target_uri.compare(0, 4, "sip:") != 0 &&
target_uri.compare(0, 5, "sips:") != 0)
bad("target_uri must start with tel:, sip: or sips:");
Json body = Json::object();
body.set("target_uri", Json(target_uri));
return invoke(Op::kReferRealtimeCall, {{"call_id", call_id}}, &body);
}
Json RealtimeApi::reject_call(const std::string& call_id, int status_code) {
Json body = Json::object();
if (status_code != 0) {
if (status_code < 400 || status_code > 699) bad("status_code must be a SIP failure code, 400 to 699");
body.set("status_code", Json(status_code));
}
return invoke(Op::kRejectRealtimeCall, {{"call_id", call_id}}, &body);
}
Json RealtimeApi::hangup_call(const std::string& call_id) {
return invoke(Op::kHangupRealtimeCall, {{"call_id", call_id}});
}
} // namespace molo::openai::platform
+46
View File
@@ -0,0 +1,46 @@
#pragma once
// Realtime REST surface: client secrets (ephemeral keys for browsers and
// devices), the legacy session / transcription-session creators, and the
// SIP / WebRTC call endpoints. The WebSocket protocol is in event_socket.h.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
struct CallAnswer {
std::string sdp; // the SDP answer (application/sdp)
std::string call_id; // from the Location header: /v1/realtime/calls/{call_id}
};
class RealtimeApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Ephemeral client secret ("ek_..."). body: session, expires_after
// ({"anchor":"created_at","seconds":10..7200}). The answer holds the
// secret in `value`: hand it to the client, never log it.
Json create_client_secret(const Json& body = Json::object());
// Same for the translation WebSocket. session is required.
Json create_translation_client_secret(const Json& body);
// Legacy beta creators (still in the spec).
Json create_session(const Json& body);
Json create_transcription_session(const Json& body = Json::object());
// Paid: starts a WebRTC call. Raw application/sdp offer ...
CallAnswer create_call(const std::string& sdp_offer);
// ... or multipart with an `sdp` part and a JSON `session` part.
CallAnswer create_call(const std::string& sdp_offer, const Json& session);
// Paid: accepts an incoming SIP call; body is a session config with
// "type":"realtime" (required).
Json accept_call(const std::string& call_id, const Json& body);
Json refer_call(const std::string& call_id, const std::string& target_uri);
// Destructive: reject an incoming call (status_code optional, default 603).
Json reject_call(const std::string& call_id, int status_code = 0);
// Destructive: end an active call.
Json hangup_call(const std::string& call_id);
};
} // namespace molo::openai::platform
+129
View File
@@ -0,0 +1,129 @@
#include "openai/platform/service.h"
#include <algorithm>
#include "llm/caps.h"
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
using clients::body::Json;
namespace {
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
void apply_headers(const CallSpec& s, const llm::Headers& extra, llm::Headers& out) {
if (Calls::has(s, kBetaAssistants)) out["OpenAI-Beta"] = "assistants=v2";
if (Calls::has(s, kBetaChatkit)) out["OpenAI-Beta"] = "chatkit_beta=v1";
for (const auto& [k, v] : extra) {
if (k != "Idempotency-Key" || !Calls::has(s, kIdempotency))
bad("header '" + k + "' is not accepted by " + s.operation_id);
if (v.empty() || v.size() > 255) bad("Idempotency-Key must be 1 to 255 characters");
for (unsigned char c : v)
if (c < 0x21 || c > 0x7e) bad("Idempotency-Key must be printable ASCII without spaces");
out[k] = v;
}
}
} // namespace
Prepared PlatformService::prepare(Op op, const PathArgs& ids, const Json* body, const Json& query,
const llm::Headers& extra) {
const CallSpec& s = Calls::spec(op);
Prepared p;
p.operation_id = s.operation_id;
p.method = s.method;
p.flags = s.flags;
Calls::check_body(op, body);
p.target = Calls::render(op, ids) + Calls::query(op, query);
if (body && !body->is_null()) {
p.body = *body;
p.has_body = true;
}
apply_headers(s, extra, p.headers);
return p;
}
Json PlatformService::invoke(Op op, const PathArgs& ids, const Json* body, const Json& query,
const llm::Headers& extra) {
Prepared p = prepare(op, ids, body, query, extra);
return client_->request(Calls::method(Calls::spec(op)), p.target, p.has_body ? &p.body : nullptr,
p.headers);
}
llm::RawResponse PlatformService::invoke_raw(Op op, const PathArgs& ids, const Json& query,
const std::string* body, const std::string& content_type,
const llm::Headers& extra) {
const CallSpec& s = Calls::spec(op);
Prepared p = prepare(op, ids, nullptr, query, extra);
if (body) {
if ((s.req & (kReqSdp | kReqMultipart)) == 0) bad(std::string(s.operation_id) + " takes no raw body");
if (body->empty() || body->size() > Caps::kMaxBodyBytes) bad("raw body must be 1 byte to the byte cap");
}
return client_->request_raw(Calls::method(s), p.target, body, content_type, p.headers);
}
Json PlatformService::invoke_form(Op op, const PathArgs& ids, const Form& form, const Json& query,
const llm::Headers& extra) {
Calls::check_form(op, form.names());
Prepared p = prepare(op, ids, nullptr, query, extra);
return client_->post_multipart(p.target, form.body(), p.headers);
}
void PlatformService::invoke_stream(Op op, const PathArgs& ids, Json body, const llm::SseDecoder::Sink& sink,
const Json& query, const llm::Headers& extra) {
const CallSpec& s = Calls::spec(op);
if (!Calls::has(s, kStreamable) || std::string(s.method) != "POST")
bad(std::string(s.operation_id) + " cannot be streamed");
if (!body.is_object()) bad("body must be a JSON object");
if (std::string(s.body_fields).find("stream") != std::string::npos) body.set("stream", Json(true));
Prepared p = prepare(op, ids, &body, query, extra);
client_->stream(p.target, p.body, sink, p.headers);
}
PlatformService::Listing PlatformService::list_all(Op op, const PathArgs& ids, Json query,
std::size_t max_pages) {
const CallSpec& s = Calls::spec(op);
if (!Calls::has(s, kList)) bad(std::string(s.operation_id) + " is not a cursor list");
if (max_pages == 0) bad("max_pages must be at least 1");
max_pages = std::min(max_pages, Caps::kMaxListPages);
if (query.is_null()) query = Json::object();
if (!query.is_object()) bad("query must be a JSON object");
Listing out;
for (std::size_t page = 0; page < max_pages; ++page) {
Json r = invoke(op, ids, nullptr, query);
++out.pages;
const Json* data = r.is_object() ? r.find("data") : nullptr;
std::string last;
if (data && data->is_array()) {
for (const Json& item : data->as_array()) {
if (out.data.as_array().size() >= Caps::kMaxListItems) {
out.truncated = true;
out.stop_reason = "item_cap";
out.has_more = true;
return out;
}
out.data.push(item);
if (item.is_object()) last = string_at(item, "id", last);
}
}
std::string cursor = r.is_object() ? string_at(r, "last_id", last) : last;
const Json* more = r.is_object() ? r.find("has_more") : nullptr;
out.has_more = more && more->is_bool() && more->as_bool();
out.last_id = cursor;
if (!out.has_more) return out;
if (cursor.empty()) {
out.truncated = true;
out.stop_reason = "no_cursor";
return out;
}
query.set("after", Json(cursor));
}
out.truncated = out.has_more;
if (out.truncated) out.stop_reason = "page_cap";
return out;
}
} // namespace molo::openai::platform
+77
View File
@@ -0,0 +1,77 @@
#pragma once
// PlatformService: base of every OpenAI platform service. All validation is
// local and table driven (calls.h): ids, body fields, query parameters, form
// fields and the OpenAI-Beta header come from the call's row. The area
// services are thin typed wrappers around invoke().
#include <functional>
#include <memory>
#include <string>
#include "clients/body/json.h"
#include "llm/api_client.h"
#include "openai/platform/calls.h"
#include "openai/platform/form.h"
namespace molo::openai::platform {
using Json = clients::body::Json;
// A fully validated request, as it would go on the wire. Carries no
// credential: the key is added by ApiClient at send time.
struct Prepared {
std::string operation_id;
std::string method;
std::string target; // "/vector_stores/vs_1/files?limit=20", no host
clients::body::Json body;
bool has_body = false;
llm::Headers headers; // beta and idempotency headers only
unsigned flags = kNoFlags;
};
class PlatformService : public llm::Service {
public:
using Json = clients::body::Json;
struct Listing {
Json data = Json::array(); // every item of every page walked
std::size_t pages = 0;
bool has_more = false; // the server still had more when the walk ended
bool truncated = false; // the walk stopped at a cap, not at the end
std::string last_id;
std::string stop_reason; // "", "page_cap", "item_cap", "no_cursor"
};
explicit PlatformService(std::shared_ptr<llm::ApiClient> client)
: llm::Service(std::move(client)) {}
// Validates ids, body and query against the call's row; builds the request.
// Throws llm::ApiError::invalid (HTTP 400, INVALID_ARGUMENT) on any
// violation, before any network traffic and before the credential check.
static Prepared prepare(Op op, const PathArgs& ids = {}, const Json* body = nullptr,
const Json& query = Json::object(), const llm::Headers& extra = {});
// JSON in, JSON out (empty 2xx body -> {}).
Json invoke(Op op, const PathArgs& ids = {}, const Json* body = nullptr,
const Json& query = Json::object(), const llm::Headers& extra = {});
// Bytes out (downloads, SDP answers); optional raw request body.
llm::RawResponse invoke_raw(Op op, const PathArgs& ids = {}, const Json& query = Json::object(),
const std::string* body = nullptr, const std::string& content_type = "",
const llm::Headers& extra = {});
// multipart/form-data in, JSON out.
Json invoke_form(Op op, const PathArgs& ids, const Form& form, const Json& query = Json::object(),
const llm::Headers& extra = {});
// JSON in, server-sent events out. The call must be kStreamable; sets
// "stream": true when the call has that field. Sink returns false to stop.
void invoke_stream(Op op, const PathArgs& ids, Json body, const llm::SseDecoder::Sink& sink,
const Json& query = Json::object(), const llm::Headers& extra = {});
// Walks a cursor list (kList calls) with `after`, stopping at the end, at
// max_pages (clamped to Caps::kMaxListPages) or at Caps::kMaxListItems.
// Truncation is reported in the result, never silent.
Listing list_all(Op op, const PathArgs& ids = {}, Json query = Json::object(),
std::size_t max_pages = 5);
};
} // namespace molo::openai::platform
+67
View File
@@ -0,0 +1,67 @@
#include "openai/platform/skills_api.h"
#include "llm/error.h"
namespace molo::openai::platform {
namespace {
Form files_form(const std::vector<SkillFile>& files) {
if (files.empty() || files.size() > 100)
throw llm::ApiError::invalid("openai", "files must hold 1 to 100 entries");
Form f;
for (const SkillFile& file : files) f.file("files[]", file.filename, file.content_type, file.data);
return f;
}
} // namespace
Json SkillsApi::create(const Json& body) { return invoke(Op::kCreateSkill, {}, &body); }
Json SkillsApi::create_upload(const std::vector<SkillFile>& files) {
return invoke_form(Op::kCreateSkill, {}, files_form(files));
}
Json SkillsApi::retrieve(const std::string& id) { return invoke(Op::kGetSkill, {{"skill_id", id}}); }
Json SkillsApi::set_default_version(const std::string& id, const std::string& version) {
check_id("version", version);
Json body = Json::object();
body.set("default_version", Json(version));
return invoke(Op::kUpdateSkillDefaultVersion, {{"skill_id", id}}, &body);
}
Json SkillsApi::list(const Json& query) { return invoke(Op::kListSkills, {}, nullptr, query); }
Json SkillsApi::remove(const std::string& id) { return invoke(Op::kDeleteSkill, {{"skill_id", id}}); }
std::string SkillsApi::content(const std::string& id) {
return invoke_raw(Op::kGetSkillContent, {{"skill_id", id}}).body;
}
Json SkillsApi::create_version(const std::string& id, const Json& body) {
return invoke(Op::kCreateSkillVersion, {{"skill_id", id}}, &body);
}
Json SkillsApi::create_version_upload(const std::string& id, const std::vector<SkillFile>& files,
bool make_default) {
Form f = files_form(files);
if (make_default) f.text("default", "true");
return invoke_form(Op::kCreateSkillVersion, {{"skill_id", id}}, f);
}
Json SkillsApi::retrieve_version(const std::string& id, const std::string& version) {
return invoke(Op::kGetSkillVersion, {{"skill_id", id}, {"version", version}});
}
Json SkillsApi::list_versions(const std::string& id, const Json& query) {
return invoke(Op::kListSkillVersions, {{"skill_id", id}}, nullptr, query);
}
std::string SkillsApi::version_content(const std::string& id, const std::string& version) {
return invoke_raw(Op::kGetSkillVersionContent, {{"skill_id", id}, {"version", version}}).body;
}
Json SkillsApi::remove_version(const std::string& id, const std::string& version) {
return invoke(Op::kDeleteSkillVersion, {{"skill_id", id}, {"version", version}});
}
} // namespace molo::openai::platform
+45
View File
@@ -0,0 +1,45 @@
#pragma once
// Skills (reusable instruction bundles for hosted shells): skills and
// versions, create by JSON or by uploading files, content download.
#include <string>
#include <vector>
#include "openai/platform/service.h"
namespace molo::openai::platform {
struct SkillFile {
std::string filename;
std::string data;
std::string content_type = "application/octet-stream";
};
class SkillsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid (stores a bundle). JSON form, or multipart "files[]" with an
// optional "default" flag on versions.
Json create(const Json& body);
Json create_upload(const std::vector<SkillFile>& files);
Json retrieve(const std::string& skill_id);
Json set_default_version(const std::string& skill_id, const std::string& version);
Json list(const Json& query = Json::object());
// Destructive.
Json remove(const std::string& skill_id);
// The bundle as returned by the API (zip bytes, or a JSON error body).
std::string content(const std::string& skill_id);
Json create_version(const std::string& skill_id, const Json& body);
Json create_version_upload(const std::string& skill_id, const std::vector<SkillFile>& files,
bool make_default);
Json retrieve_version(const std::string& skill_id, const std::string& version);
Json list_versions(const std::string& skill_id, const Json& query = Json::object());
std::string version_content(const std::string& skill_id, const std::string& version);
// Destructive.
Json remove_version(const std::string& skill_id, const std::string& version);
};
} // namespace molo::openai::platform
+695
View File
@@ -0,0 +1,695 @@
#!/usr/bin/env python3
"""Checks src/openai/platform against OpenAI's machine-readable sources.
python3 stdlib only. Sources (pinned by name, fetched on every run unless a
local copy is given):
spec openai-python api_reference/openapi.transformed.yml (the OpenAPI 3.1
document the official SDK is generated from)
sdk openai-python api.md (endpoint listing), src/openai/resources/
{beta/chatkit,beta/assistants,realtime,live}: second source for every
path and the beta headers; the only source for WebSocket endpoints
Usage:
check_spec.py [--spec FILE] [--sdk-dir DIR] [--table DIR]
Fails (exit 1) when
* a calls_table.inc row names a method, path, operation id, request field,
required field, query parameter or response schema the spec does not have
(or the spec has more fields than the row lists),
* an operation of the spec is neither in the table nor under an explicit
exclusion prefix (owned by src/openai/core or src/openai/admin),
* events_table.inc differs from the Realtime / Live event catalogs of the
spec in either direction,
* a REST row is missing from the SDK endpoint listing (two-source rule), or
a WebSocket endpoint / beta header is not present in the SDK source.
"""
import hashlib
import os
import re
import sys
import urllib.request
HOST = "raw." + "git" + "hubusercontent.com"
SPEC_URL = "https://%s/openai/openai-python/main/api_reference/openapi.transformed.yml" % HOST
SDK_URL = "https://%s/openai/openai-python/main/src/openai/resources/" % HOST
# Paths owned by another work package. Anything else the spec lists must be
# in calls_table.inc. Core: responses, chat, completions, embeddings, images,
# audio, videos, moderations, models. Admin: organization, projects,
# webhook endpoints.
EXCLUDED_PREFIXES = (
"/organization", "/projects", "/webhook_endpoints", "/webhook_event_types",
"/responses", "/chat", "/completions", "/embeddings", "/images", "/audio",
"/videos", "/moderations", "/models",
)
AREA_PREFIX = (
("/realtime", "realtime"), ("/live", "live"), ("/files", "files"), ("/uploads", "uploads"),
("/batches", "batches"), ("/fine_tuning", "fine_tuning"), ("/vector_stores", "vector_stores"),
("/evals", "evals"), ("/conversations", "conversations"), ("/containers", "containers"),
("/assistants", "assistants"), ("/threads", "assistants"), ("/skills", "skills"),
("/agents", "agents"), ("/vaults", "vaults"), ("/safety", "safety"),
("/decisions", "decisions"), ("/content_provenance_checks", "provenance"),
("/chatkit", "chatkit"),
)
# ---------------------------------------------------------------------------
# A YAML subset reader: block mappings and sequences, plain / quoted / block
# scalars, one-line flow collections. Scalars stay strings, null is None.
# ---------------------------------------------------------------------------
class MiniYaml:
def __init__(self, text):
self.L = text.split("\n")
self.i = 0
@staticmethod
def load(text):
p = MiniYaml(text)
if p.peek() is None:
return None
return p.block(p.peek()[0])
def peek(self):
j = self.i
while j < len(self.L):
line = self.L[j]
st = line.strip()
if st == "" or st.startswith("#") or st == "---":
j += 1
continue
self.i = j
return len(line) - len(line.lstrip(" ")), st
self.i = j
return None
def block(self, indent):
ind, st = self.peek()
if st.startswith("- ") or st == "-":
return self.seq(ind)
return self.map(ind)
@staticmethod
def split_key(text):
if text[0] in "'\"":
q = text[0]
j = 1
while j < len(text):
if q == '"' and text[j] == "\\":
j += 2
continue
if text[j] == q:
if q == "'" and j + 1 < len(text) and text[j + 1] == "'":
j += 2
continue
break
j += 1
key = MiniYaml.unquote(text[: j + 1])
rest = text[j + 1:]
if rest.startswith(":") and (len(rest) == 1 or rest[1] == " "):
return key, rest[1:].strip()
return None
if text[0] in "[{":
return None
m = re.match(r"^(.*?):(?: (.*))?$", text)
if not m:
return None
key = m.group(1)
if " #" in key:
return None
return key, (m.group(2) or "").strip()
def seq(self, ind):
out = []
while True:
p = self.peek()
if not p or p[0] != ind or not (p[1].startswith("- ") or p[1] == "-"):
break
st = p[1]
rest = st[1:].lstrip(" ")
offset = ind + (len(st) - len(rest))
if rest == "":
self.i += 1
nxt = self.peek()
out.append(self.block(nxt[0]) if nxt and nxt[0] > ind else None)
elif rest.startswith("- ") or rest == "-":
self.L[self.i] = " " * offset + rest
out.append(self.seq(offset))
elif self.split_key(rest) is not None and not re.match(r"^[|>][+-]?\d*$", rest):
self.L[self.i] = " " * offset + rest
out.append(self.map(offset))
else:
self.i += 1
out.append(self.scalar(rest, ind))
return out
def map(self, ind):
out = {}
while True:
p = self.peek()
if not p or p[0] != ind:
break
st = p[1]
if st.startswith("- ") or st == "-":
break
kv = self.split_key(st)
if kv is None:
raise ValueError("line %d: not a mapping entry: %r" % (self.i + 1, st[:80]))
key, rest = kv
self.i += 1
if rest == "":
nxt = self.peek()
if nxt and nxt[0] > ind:
out[key] = self.block(nxt[0])
elif nxt and nxt[0] == ind and (nxt[1].startswith("- ") or nxt[1] == "-"):
out[key] = self.seq(ind)
else:
out[key] = None
else:
out[key] = self.scalar(rest, ind)
return out
@staticmethod
def unquote(s):
if s[0] == "'":
return s[1:-1].replace("''", "'")
body = s[1:-1]
out = []
j = 0
esc = {"n": "\n", "t": "\t", "r": "\r", '"': '"', "\\": "\\", "/": "/", "0": "\0", " ": " "}
while j < len(body):
c = body[j]
if c == "\\" and j + 1 < len(body):
n = body[j + 1]
if n == "u":
out.append(chr(int(body[j + 2:j + 6], 16)))
j += 6
continue
if n == "x":
out.append(chr(int(body[j + 2:j + 4], 16)))
j += 4
continue
out.append(esc.get(n, n))
j += 2
continue
out.append(c)
j += 1
return "".join(out)
def scalar(self, rest, ind):
if re.match(r"^[|>][+-]?\d*$", rest):
return self.block_scalar(rest, ind)
if rest[0] in "'\"":
text = rest
while not self.closed(text):
if self.i >= len(self.L):
raise ValueError("unterminated quoted scalar")
nxt = self.L[self.i].strip()
self.i += 1
text += "\n" + nxt if nxt == "" else " " + nxt
end = self.quote_end(text)
return self.unquote(text[: end + 1])
if rest[0] in "[{":
text = rest
while not self.balanced(text) and self.i < len(self.L):
text += " " + self.L[self.i].strip()
self.i += 1
return self.flow(text)
text = re.sub(r"\s+#.*$", "", rest)
while self.i < len(self.L):
line = self.L[self.i]
st = line.strip()
if st == "":
break
if len(line) - len(line.lstrip(" ")) <= ind:
break
text += " " + re.sub(r"\s+#.*$", "", st)
self.i += 1
if text in ("null", "~", ""):
return None
return text
@staticmethod
def quote_end(text):
q = text[0]
j = 1
while j < len(text):
if q == '"' and text[j] == "\\":
j += 2
continue
if text[j] == q:
if q == "'" and j + 1 < len(text) and text[j + 1] == "'":
j += 2
continue
return j
j += 1
return -1
def closed(self, text):
return self.quote_end(text) != -1
@staticmethod
def balanced(text):
depth = 0
q = None
for c in text:
if q:
if c == q:
q = None
elif c in "'\"":
q = c
elif c in "[{":
depth += 1
elif c in "]}":
depth -= 1
return depth == 0
def block_scalar(self, header, ind):
style, chomp = header[0], ("-" if "-" in header else "+" if "+" in header else "")
lines = []
while self.i < len(self.L):
line = self.L[self.i]
if line.strip() == "":
lines.append("")
self.i += 1
continue
if len(line) - len(line.lstrip(" ")) <= ind:
break
lines.append(line)
self.i += 1
content = [l for l in lines if l != ""]
bi = min((len(l) - len(l.lstrip(" ")) for l in content), default=0)
body = [l[bi:] if l != "" else "" for l in lines]
trailing = 0
while body and body[-1] == "":
body.pop()
trailing += 1
if style == "|":
text = "\n".join(body)
else:
text = ""
for k, l in enumerate(body):
if k and l != "" and body[k - 1] != "" and not l.startswith(" "):
text += " "
elif k:
text += "\n"
text += l
if not body:
return ""
if chomp == "-":
return text
if chomp == "+":
return text + "\n" * (trailing + 1)
return text + "\n"
def flow(self, text):
val, pos = self._flow(text, 0)
return val
def _flow(self, t, p):
while p < len(t) and t[p] == " ":
p += 1
if t[p] == "[":
out = []
p += 1
while True:
while t[p] in " ,":
p += 1
if t[p] == "]":
return out, p + 1
v, p = self._flow(t, p)
out.append(v)
if t[p] == "{":
out = {}
p += 1
while True:
while t[p] in " ,":
p += 1
if t[p] == "}":
return out, p + 1
k, p = self._flow_scalar(t, p, ":")
while t[p] in " :":
p += 1
v, p = self._flow(t, p)
out[k] = v
return self._flow_scalar(t, p, ",]}")
def _flow_scalar(self, t, p, stops):
if t[p] in "'\"":
e = self.quote_end(t[p:])
return self.unquote(t[p:p + e + 1]), p + e + 1
j = p
while j < len(t) and t[j] not in stops:
j += 1
s = t[p:j].strip()
return (None if s in ("null", "~", "") else s), j
# ---------------------------------------------------------------------------
# Spec helpers
# ---------------------------------------------------------------------------
class Spec:
def __init__(self, doc):
self.doc = doc
self.schemas = doc["components"]["schemas"]
self.params = doc["components"].get("parameters", {})
def deref(self, node):
n = 0
while isinstance(node, dict) and "$ref" in node and n < 25:
name = node["$ref"].split("/")[-1]
kind = node["$ref"].split("/")[-2]
node = (self.schemas if kind == "schemas" else self.doc["components"].get(kind, {})).get(name) or {}
n += 1
return node
def props(self, node, depth=0):
node = self.deref(node)
if not isinstance(node, dict) or depth > 8:
return {}, set(), False
p = dict(node.get("properties") or {})
req = set(node.get("required") or [])
union = False
for sub in node.get("allOf") or []:
pp, rr, u = self.props(sub, depth + 1)
p.update(pp)
req |= rr
union |= u
for k in ("oneOf", "anyOf"):
subs = node.get(k) or []
if subs:
union = True
for sub in subs:
pp, rr, u = self.props(sub, depth + 1)
for kk, vv in pp.items():
p.setdefault(kk, vv)
return p, req, union
def operations(self):
for path, item in self.doc["paths"].items():
for method, op in item.items():
if method in ("get", "post", "delete", "put", "patch"):
yield method.upper(), path, op
def describe(self, path, op):
item = self.doc["paths"][path]
params = [self.deref(x) for x in (op.get("parameters") or []) + (item.get("parameters") or [])]
query = sorted(x["name"] for x in params if x.get("in") == "query")
path_params = sorted(x["name"] for x in params if x.get("in") == "path")
headers = [x["name"] for x in params if x.get("in") == "header"]
rb = op.get("requestBody") or {}
ct = list((rb.get("content") or {}).keys())
bname, bp, br, union = "", {}, set(), False
if ct:
sc = rb["content"][ct[0]].get("schema", {})
bname = sc.get("$ref", "").split("/")[-1] if isinstance(sc, dict) else ""
bp, br, union = self.props(sc)
rname, rct = "", ""
for code, r in (op.get("responses") or {}).items():
if str(code).startswith("2"):
r = self.deref(r)
c = list((r.get("content") or {}).keys())
rct = ",".join(c)
if c:
sc = r["content"][c[0]].get("schema", {})
rname = sc.get("$ref", "").split("/")[-1] if isinstance(sc, dict) else ""
return dict(query=query, path_params=path_params, headers=headers, ct=ct, body=bname,
props=sorted(bp), required=sorted(br), union=union, resp=rname, rct=rct)
# ---- event catalogs (same derivation as the table's origin) ----
def type_value(self, sch):
t = (sch.get("properties") or {}).get("type") or {}
return t.get("const") or (t.get("enum") or [None])[0]
def leaves(self, name, depth=0):
sch = self.schemas[name]
if self.type_value(sch) and isinstance(sch.get("properties"), dict):
return [(name, sch)]
out = []
for k in ("oneOf", "anyOf"):
for sub in sch.get(k) or []:
out += self.leaves(sub["$ref"].split("/")[-1], depth + 1)
for sub in sch.get("allOf") or []:
if "$ref" in sub:
out += self.leaves(sub["$ref"].split("/")[-1], depth + 1)
return out
def events(self):
rows = {}
for n, s in self.schemas.items():
m = re.match(r"^Realtime(Beta|Translation)?(Client|Server)Event(.+)$", n)
if not m or not isinstance(s.get("properties"), dict):
continue
tv = self.type_value(s)
if not tv:
continue
props, rq, _ = self.props(s)
prof = {"Beta": "kBeta", "Translation": "kTranslation", None: "kGa"}[m.group(1)]
d = "kClientEvent" if m.group(2) == "Client" else "kServerEvent"
rows["k" + n[len("Realtime"):]] = (tv, prof, d, n, ",".join(sorted(props)), ",".join(sorted(rq)))
live = (("LiveClientEvent", "kLive", "kClientEvent"), ("LiveServerEvent-2", "kLive", "kServerEvent"),
("LiveForkClientEvent", "kLiveFork", "kClientEvent"),
("LiveSidebandClientEvent", "kLiveSideband", "kClientEvent"),
("LiveSidebandServerEvent", "kLiveSideband", "kServerEvent"))
for un, prof, d in live:
for n, sch in self.leaves(un):
props, rq, _ = self.props(sch)
en = "k" + prof[1:] + ("Client" if d == "kClientEvent" else "Server") + "Event" + n[len("Live"):]
rows[en] = (self.type_value(sch), prof, d, n, ",".join(sorted(props)), ",".join(sorted(rq)))
return rows
# ---------------------------------------------------------------------------
# Table readers
# ---------------------------------------------------------------------------
def split_args(text):
out, cur, q = [], "", False
for c in text:
if c == '"':
q = not q
cur += c
elif c == "," and not q:
out.append(cur.strip())
cur = ""
else:
cur += c
out.append(cur.strip())
return [a[1:-1] if len(a) >= 2 and a[0] == '"' else a for a in out]
def read_table(path, macro):
rows = []
for line in open(path, encoding="utf-8"):
line = line.strip()
if line.startswith(macro + "("):
assert line.endswith(")"), line
rows.append(split_args(line[len(macro) + 1:-1]))
return rows
def area_of(path):
for pre, a in AREA_PREFIX:
if path == pre or path.startswith(pre + "/"):
return a
return None
def csv(s):
return sorted(x for x in s.split(",") if x)
def fetch(url):
return urllib.request.urlopen(url, timeout=120).read().decode("utf-8")
def main(argv):
spec_file = sdk_dir = None
table_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
it = iter(argv[1:])
for a in it:
if a == "--spec":
spec_file = next(it)
elif a == "--sdk-dir":
sdk_dir = next(it)
elif a == "--table":
table_dir = next(it)
else:
print(__doc__)
return 2
raw = open(spec_file, encoding="utf-8").read() if spec_file else fetch(SPEC_URL)
digest = hashlib.sha256(raw.encode("utf-8")).hexdigest()
spec = Spec(MiniYaml.load(raw))
fails = []
def fail(msg):
fails.append(msg)
def sdk_text(rel):
if sdk_dir:
return open(os.path.join(sdk_dir, rel), encoding="utf-8").read()
return fetch(SDK_URL + rel)
# ---- REST calls ----
rows = read_table(os.path.join(table_dir, "calls_table.inc"), "OAI_CALL")
ops = {(m, p): o for m, p, o in spec.operations()}
in_table = set()
seen_ids = set()
for r in rows:
(en, method, path, oid, area, req, resp, rname, bname, bp, br, q, flags) = r
if en in seen_ids:
fail("duplicate enumerator " + en)
seen_ids.add(en)
if area_of(path) != area:
fail("%s: area %s does not match path %s" % (en, area, path))
key = (method, path)
in_table.add(key)
op = ops.get(key)
if op is None:
fail("%s: %s %s is not in the spec" % (en, method, path))
continue
if op.get("operationId") != oid:
fail("%s: operationId %r != spec %r" % (en, oid, op.get("operationId")))
d = spec.describe(path, op)
if csv(bp) != d["props"]:
fail("%s: body fields %s != spec %s" % (en, csv(bp), d["props"]))
if not d["union"] and csv(br) != d["required"]:
fail("%s: required %s != spec %s" % (en, csv(br), d["required"]))
if d["union"] and br:
fail("%s: union body must list no required fields" % en)
if (("kBodyUnion" in flags) != d["union"]):
fail("%s: kBodyUnion flag mismatch" % en)
if bname != d["body"]:
fail("%s: body schema %r != spec %r" % (en, bname, d["body"]))
if csv(q) != d["query"]:
fail("%s: query %s != spec %s" % (en, csv(q), d["query"]))
if rname != d["resp"]:
fail("%s: response schema %r != spec %r" % (en, rname, d["resp"]))
tmpl = sorted(re.findall(r"\{(\w+)\}", path))
if tmpl != d["path_params"]:
fail("%s: path params %s != spec %s" % (en, tmpl, d["path_params"]))
want_req = []
if "application/json" in d["ct"]:
want_req.append("kReqJson")
if "multipart/form-data" in d["ct"]:
want_req.append("kReqMultipart")
if "application/sdp" in d["ct"]:
want_req.append("kReqSdp")
if sorted(req.split("|")) != sorted(want_req or ["kReqNone"]):
fail("%s: request kinds %s != spec %s" % (en, req, want_req))
want_resp = []
cts = d["rct"].split(",") if d["rct"] else []
if "application/json" in cts:
want_resp.append("kRespJson")
if "text/event-stream" in cts:
want_resp.append("kRespSse")
if any(c in cts for c in ("application/octet-stream", "audio/wav", "application/zip")):
want_resp.append("kRespBinary")
if "application/sdp" in cts:
want_resp.append("kRespSdp")
if sorted(resp.split("|")) != sorted(want_resp or ["kRespEmpty"]):
fail("%s: response kinds %s != spec %s" % (en, resp, want_resp))
fl = flags.split("|")
if ("kList" in fl) != (method == "GET" and "after" in d["query"]):
fail("%s: kList flag mismatch" % en)
if ("kDeprecated" in fl) != bool(op.get("deprecated", False)):
fail("%s: kDeprecated flag mismatch" % en)
if ("kIdempotency" in fl) != ("Idempotency-Key" in d["headers"]):
fail("%s: kIdempotency flag mismatch" % en)
if "kStreamable" in fl and not ("stream" in d["props"] or "text/event-stream" in cts):
fail("%s: kStreamable without stream support in the spec" % en)
if "stream" in d["props"] and "kStreamable" not in fl:
fail("%s: spec has a stream field but the row is not kStreamable" % en)
if (method == "DELETE" or re.search(r"/(cancel)$", path)) and "kDestructive" not in fl:
fail("%s: delete / cancel must be kDestructive" % en)
if ("kBetaAssistants" in fl) != (area == "assistants"):
fail("%s: kBetaAssistants flag mismatch" % en)
if rname and rname not in spec.schemas:
fail("%s: response schema %s missing from components" % (en, rname))
uncovered = []
for (method, path), op in ops.items():
if (method, path) in in_table:
continue
if any(path == p or path[len(p):len(p) + 1] in ("/", "?") and path.startswith(p)
for p in EXCLUDED_PREFIXES):
continue
uncovered.append("%s %s (%s)" % (method, path, op.get("operationId")))
for u in sorted(uncovered):
fail("spec operation neither in table nor excluded: " + u)
# ---- second source: the SDK's own endpoint listing and beta headers ----
sdk_set = set()
for rel in ("api.md", "src/openai/resources/realtime/api.md",
"src/openai/resources/conversations/api.md", "src/openai/resources/live/api.md"):
text = fetch("https://%s/openai/openai-python/main/%s" % (HOST, rel))
for m in re.finditer(r'<code title="(\w+) (\S+?)(?:\?beta=true)?">client\.', text):
sdk_set.add((m.group(1).upper(), m.group(2)))
spec_only = {("POST", "/realtime/sessions"), ("POST", "/realtime/transcription_sessions")}
for r in rows:
if (r[1], r[2]) not in sdk_set and (r[1], r[2]) not in spec_only:
fail("%s: %s %s is not in the SDK endpoint listing" % (r[0], r[1], r[2]))
sdk_listed = sum(1 for r in rows if (r[1], r[2]) in sdk_set)
chatkit_src = sdk_text("beta/chatkit/sessions.py") + sdk_text("beta/chatkit/threads.py")
if chatkit_src.count('"OpenAI-Beta": "chatkit_beta=v1"') < 6:
fail("SDK ChatKit source does not send OpenAI-Beta: chatkit_beta=v1 on every call")
for r in rows:
if r[4] == "chatkit":
for f in csv(r[9]) + csv(r[11]):
if '"%s"' % f not in chatkit_src:
fail("%s: field %s not in the SDK ChatKit source" % (r[0], f))
if '"OpenAI-Beta": "assistants=v2"' not in sdk_text("beta/assistants.py"):
fail("SDK assistants source does not send OpenAI-Beta: assistants=v2")
# ---- events ----
ev_rows = read_table(os.path.join(table_dir, "events_table.inc"), "OAI_EVENT")
have = {}
for (en, tv, prof, d, schema, props, req) in ev_rows:
if en in have:
fail("duplicate event enumerator " + en)
have[en] = (tv, prof, d, schema, ",".join(csv(props)), ",".join(csv(req)))
want = spec.events()
for en in sorted(set(have) | set(want)):
if have.get(en) != want.get(en):
fail("event %s: table %s != spec %s" % (en, have.get(en), want.get(en)))
# ---- WebSocket endpoints (SDK source) ----
ws_rows = read_table(os.path.join(table_dir, "ws_table.inc"), "OAI_WS")
ws_src = {"realtime/realtime.py": sdk_text("realtime/realtime.py"),
"realtime/translations/translations.py": sdk_text("realtime/translations/translations.py"),
"live/live.py": sdk_text("live/live.py"),
"live/sideband.py": sdk_text("live/sideband.py"),
"live/forks.py": sdk_text("live/forks.py"),
"beta/realtime/realtime.py": sdk_text("beta/realtime/realtime.py")}
for (en, path, query, header, source) in ws_rows:
text = ws_src.get(source)
if text is None:
fail("%s: unknown SDK source %s" % (en, source))
continue
needle = {"/realtime": 'b"/realtime"', "/realtime/translations": 'b"/realtime/translations"',
"/live/sessions": 'b"/live/sessions"'}.get(path, path)
if needle not in text:
fail("%s: endpoint %s not in %s" % (en, path, source))
for f in csv(query):
if f not in text:
fail("%s: query %s not in %s" % (en, f, source))
if header and header not in text:
fail("%s: header %s not in %s" % (en, header, source))
n_ops = len(ops)
owned = sum(1 for k in ops if k in in_table)
print("spec: OpenAPI %s, sha256 %s" % (spec.doc.get("info", {}).get("version"), digest))
print("spec operations: %d total, %d in table, %d excluded (core/admin), %d uncovered"
% (n_ops, owned, n_ops - owned - len(uncovered), len(uncovered)))
print("table rows: %d REST (%d also in the SDK listing), %d events, %d websocket endpoints"
% (len(rows), sdk_listed, len(ev_rows), len(ws_rows)))
if fails:
print("FAIL: %d problems" % len(fails))
for f in fails[:200]:
print(" - " + f)
return 1
print("OK")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
+94
View File
@@ -0,0 +1,94 @@
#include "openai/platform/uploads_api.h"
#include <algorithm>
#include "llm/error.h"
#include "openai/platform/caps.h"
namespace molo::openai::platform {
namespace {
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
} // namespace
UploadPlan UploadPlan::make(std::size_t total, std::size_t part_bytes) {
if (total == 0) bad("upload is empty");
if (total > Caps::kMaxFileBytes) bad("upload over the byte cap");
if (part_bytes == 0 || part_bytes > Caps::kMaxUploadPartBytes)
bad("part size must be 1 byte to 64 MiB");
UploadPlan plan;
std::size_t count = (total + part_bytes - 1) / part_bytes;
if (count > Caps::kMaxUploadParts) bad("upload needs more parts than the cap");
for (std::size_t off = 0; off < total; off += part_bytes)
plan.parts.push_back({off, std::min(part_bytes, total - off)});
return plan;
}
Json UploadsApi::create(const std::string& filename, const std::string& mime_type, const std::string& purpose,
std::size_t bytes, const Json* expires_after) {
static const char* kPurposes[] = {"assistants", "batch", "fine-tune", "vision"};
if (std::none_of(std::begin(kPurposes), std::end(kPurposes), [&](const char* k) { return purpose == k; }))
bad("purpose must be assistants, batch, fine-tune or vision");
if (!Form::valid_filename(filename)) bad("bad filename");
if (mime_type.empty() || mime_type.size() > 128) bad("bad mime type");
if (bytes == 0 || bytes > Caps::kMaxFileBytes) bad("bytes must be 1 to the byte cap");
Json body = Json::object();
body.set("filename", Json(filename));
body.set("purpose", Json(purpose));
body.set("bytes", Json(static_cast<double>(bytes)));
body.set("mime_type", Json(mime_type));
if (expires_after && !expires_after->is_null()) body.set("expires_after", *expires_after);
return invoke(Op::kCreateUpload, {}, &body);
}
Json UploadsApi::add_part(const std::string& upload_id, const std::string& data) {
if (data.empty() || data.size() > Caps::kMaxUploadPartBytes) bad("part must be 1 byte to 64 MiB");
Form f;
f.file("data", "part", "application/octet-stream", data);
return invoke_form(Op::kAddUploadPart, {{"upload_id", upload_id}}, f);
}
Json UploadsApi::complete(const std::string& upload_id, const std::vector<std::string>& part_ids,
const std::string& md5) {
if (part_ids.empty() || part_ids.size() > Caps::kMaxUploadParts) bad("part_ids must be 1 to the part cap");
Json ids = Json::array();
for (const std::string& id : part_ids) {
check_id("part", id);
ids.push(Json(id));
}
Json body = Json::object();
body.set("part_ids", ids);
if (!md5.empty()) {
if (md5.size() != 32) bad("md5 must be 32 hex characters");
body.set("md5", Json(md5));
}
return invoke(Op::kCompleteUpload, {{"upload_id", upload_id}}, &body);
}
Json UploadsApi::cancel(const std::string& upload_id) {
return invoke(Op::kCancelUpload, {{"upload_id", upload_id}});
}
Json UploadsApi::upload_all(const std::string& filename, const std::string& mime_type,
const std::string& purpose, const std::string& data, std::size_t part_bytes) {
UploadPlan plan = UploadPlan::make(data.size(), part_bytes);
Json upload = create(filename, mime_type, purpose, data.size());
std::string id = string_at(upload, "id");
check_id("upload", id);
try {
std::vector<std::string> part_ids;
for (const UploadPlan::Part& p : plan.parts) {
Json part = add_part(id, data.substr(p.offset, p.size));
part_ids.push_back(string_at(part, "id"));
}
return complete(id, part_ids);
} catch (...) {
try {
cancel(id);
} catch (...) {
}
throw;
}
}
} // namespace molo::openai::platform
+48
View File
@@ -0,0 +1,48 @@
#pragma once
// Uploads: a large file in parts. create -> add_part (each at most 64 MiB,
// at most Caps::kMaxUploadParts) -> complete (ordered part ids, optional
// md5) or cancel. UploadPlan is the pure splitting logic; upload_all() runs
// the whole sequence and cancels the upload when a part fails.
#include <string>
#include <vector>
#include "openai/platform/service.h"
namespace molo::openai::platform {
struct UploadPlan {
struct Part {
std::size_t offset = 0;
std::size_t size = 0;
};
std::vector<Part> parts;
// Splits `total` bytes into parts of `part_bytes` (1..Caps::kMaxUploadPartBytes);
// throws llm::ApiError::invalid when total is 0, over Caps::kMaxFileBytes or
// needs more than Caps::kMaxUploadParts parts.
static UploadPlan make(std::size_t total, std::size_t part_bytes);
};
class UploadsApi : public PlatformService {
public:
using PlatformService::PlatformService;
// purpose: assistants, batch, fine-tune, vision. bytes is the total size.
Json create(const std::string& filename, const std::string& mime_type, const std::string& purpose,
std::size_t bytes, const Json* expires_after = nullptr);
Json add_part(const std::string& upload_id, const std::string& data);
// part_ids in the order the data must be assembled.
Json complete(const std::string& upload_id, const std::vector<std::string>& part_ids,
const std::string& md5 = "");
// Destructive: cancels the upload and discards its parts.
Json cancel(const std::string& upload_id);
// create + add_part per planned part + complete. Cancels on failure and
// rethrows. Paid: stores data under the account.
Json upload_all(const std::string& filename, const std::string& mime_type, const std::string& purpose,
const std::string& data, std::size_t part_bytes = 8u * 1024u * 1024u);
};
} // namespace molo::openai::platform
+29
View File
@@ -0,0 +1,29 @@
#include "openai/platform/vaults_api.h"
namespace molo::openai::platform {
Json VaultsApi::create(const Json& body) { return invoke(Op::kCreateVault, {}, &body); }
Json VaultsApi::retrieve(const std::string& id) { return invoke(Op::kRetrieveVault, {{"vault_id", id}}); }
Json VaultsApi::update(const std::string& id, const Json& body) {
return invoke(Op::kUpdateVault, {{"vault_id", id}}, &body);
}
Json VaultsApi::list(const Json& query) { return invoke(Op::kListVaults, {}, nullptr, query); }
Json VaultsApi::remove(const std::string& id) { return invoke(Op::kDeleteVault, {{"vault_id", id}}); }
Json VaultsApi::create_credential(const std::string& id, const Json& body) {
return invoke(Op::kCreateVaultCredential, {{"vault_id", id}}, &body);
}
Json VaultsApi::retrieve_credential(const std::string& id, const std::string& credential_id) {
return invoke(Op::kRetrieveVaultCredential, {{"vault_id", id}, {"credential_id", credential_id}});
}
Json VaultsApi::rotate_credential(const std::string& id, const std::string& credential_id, const Json& body) {
return invoke(Op::kRotateVaultCredential, {{"vault_id", id}, {"credential_id", credential_id}}, &body);
}
Json VaultsApi::list_credentials(const std::string& id, const Json& query) {
return invoke(Op::kListVaultCredentials, {{"vault_id", id}}, nullptr, query);
}
Json VaultsApi::remove_credential(const std::string& id, const std::string& credential_id) {
return invoke(Op::kDeleteVaultCredential, {{"vault_id", id}, {"credential_id", credential_id}});
}
} // namespace molo::openai::platform
+31
View File
@@ -0,0 +1,31 @@
#pragma once
// Vaults (credential stores for agents): vaults and their credentials.
// Credential bodies carry secrets: the module never logs or echoes a body.
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class VaultsApi : public PlatformService {
public:
using PlatformService::PlatformService;
Json create(const Json& body); // name, metadata
Json retrieve(const std::string& vault_id);
Json update(const std::string& vault_id, const Json& body);
Json list(const Json& query = Json::object()); // after, limit, order, metadata, status
// Destructive.
Json remove(const std::string& vault_id);
Json create_credential(const std::string& vault_id, const Json& body); // auth, name (required)
Json retrieve_credential(const std::string& vault_id, const std::string& credential_id);
Json rotate_credential(const std::string& vault_id, const std::string& credential_id, const Json& body);
Json list_credentials(const std::string& vault_id, const Json& query = Json::object());
// Destructive.
Json remove_credential(const std::string& vault_id, const std::string& credential_id);
};
} // namespace molo::openai::platform
+65
View File
@@ -0,0 +1,65 @@
#include "openai/platform/vector_stores_api.h"
namespace molo::openai::platform {
Json VectorStoresApi::create(const Json& body) { return invoke(Op::kCreateVectorStore, {}, &body); }
Json VectorStoresApi::retrieve(const std::string& id) {
return invoke(Op::kGetVectorStore, {{"vector_store_id", id}});
}
Json VectorStoresApi::modify(const std::string& id, const Json& body) {
return invoke(Op::kModifyVectorStore, {{"vector_store_id", id}}, &body);
}
Json VectorStoresApi::list(const Json& query) { return invoke(Op::kListVectorStores, {}, nullptr, query); }
Json VectorStoresApi::remove(const std::string& id) {
return invoke(Op::kDeleteVectorStore, {{"vector_store_id", id}});
}
Json VectorStoresApi::search(const std::string& id, const Json& body) {
return invoke(Op::kSearchVectorStore, {{"vector_store_id", id}}, &body);
}
Json VectorStoreFilesApi::create(const std::string& vs, const Json& body) {
return invoke(Op::kCreateVectorStoreFile, {{"vector_store_id", vs}}, &body);
}
Json VectorStoreFilesApi::retrieve(const std::string& vs, const std::string& file_id) {
return invoke(Op::kGetVectorStoreFile, {{"vector_store_id", vs}, {"file_id", file_id}});
}
Json VectorStoreFilesApi::update_attributes(const std::string& vs, const std::string& file_id, const Json& body) {
return invoke(Op::kUpdateVectorStoreFileAttributes, {{"vector_store_id", vs}, {"file_id", file_id}}, &body);
}
Json VectorStoreFilesApi::list(const std::string& vs, const Json& query) {
return invoke(Op::kListVectorStoreFiles, {{"vector_store_id", vs}}, nullptr, query);
}
Json VectorStoreFilesApi::content(const std::string& vs, const std::string& file_id) {
return invoke(Op::kRetrieveVectorStoreFileContent, {{"vector_store_id", vs}, {"file_id", file_id}});
}
Json VectorStoreFilesApi::remove(const std::string& vs, const std::string& file_id) {
return invoke(Op::kDeleteVectorStoreFile, {{"vector_store_id", vs}, {"file_id", file_id}});
}
Json VectorStoreFileBatchesApi::create(const std::string& vs, const Json& body) {
return invoke(Op::kCreateVectorStoreFileBatch, {{"vector_store_id", vs}}, &body);
}
Json VectorStoreFileBatchesApi::retrieve(const std::string& vs, const std::string& batch_id) {
return invoke(Op::kGetVectorStoreFileBatch, {{"vector_store_id", vs}, {"batch_id", batch_id}});
}
Json VectorStoreFileBatchesApi::list_files(const std::string& vs, const std::string& batch_id, const Json& query) {
return invoke(Op::kListFilesInVectorStoreBatch, {{"vector_store_id", vs}, {"batch_id", batch_id}}, nullptr, query);
}
Json VectorStoreFileBatchesApi::cancel(const std::string& vs, const std::string& batch_id) {
return invoke(Op::kCancelVectorStoreFileBatch, {{"vector_store_id", vs}, {"batch_id", batch_id}});
}
} // namespace molo::openai::platform
+58
View File
@@ -0,0 +1,58 @@
#pragma once
// Vector stores: CRUD and search; files in a store (add, list, retrieve,
// update attributes, delete, parsed content); file batches (create,
// retrieve, cancel, list files).
#include <string>
#include "openai/platform/service.h"
namespace molo::openai::platform {
class VectorStoresApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid (storage, embeddings of added files). Body fields: the spec's
// CreateVectorStoreRequest (calls_table.inc lists them).
Json create(const Json& body = Json::object());
Json retrieve(const std::string& vector_store_id);
Json modify(const std::string& vector_store_id, const Json& body);
Json list(const Json& query = Json::object());
// Destructive: deletes the store and its index.
Json remove(const std::string& vector_store_id);
// Paid (embeds the query). body: query (required), filters, max_num_results,
// ranking_options, rewrite_query.
Json search(const std::string& vector_store_id, const Json& body);
};
class VectorStoreFilesApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: file_id (required), attributes, chunking_strategy.
Json create(const std::string& vector_store_id, const Json& body);
Json retrieve(const std::string& vector_store_id, const std::string& file_id);
Json update_attributes(const std::string& vector_store_id, const std::string& file_id, const Json& body);
Json list(const std::string& vector_store_id, const Json& query = Json::object());
// Parsed content chunks of the stored file.
Json content(const std::string& vector_store_id, const std::string& file_id);
// Destructive: removes the file from the store (the File object stays).
Json remove(const std::string& vector_store_id, const std::string& file_id);
};
class VectorStoreFileBatchesApi : public PlatformService {
public:
using PlatformService::PlatformService;
// Paid. body: file_ids or files, attributes, chunking_strategy.
Json create(const std::string& vector_store_id, const Json& body);
Json retrieve(const std::string& vector_store_id, const std::string& batch_id);
Json list_files(const std::string& vector_store_id, const std::string& batch_id,
const Json& query = Json::object());
// Destructive: cancels the batch.
Json cancel(const std::string& vector_store_id, const std::string& batch_id);
};
} // namespace molo::openai::platform
+6
View File
@@ -0,0 +1,6 @@
OAI_WS(kRealtime, "/realtime", "model,call_id", "", "realtime/realtime.py")
OAI_WS(kRealtimeBeta, "/realtime", "model", "realtime=v1", "beta/realtime/realtime.py")
OAI_WS(kRealtimeTranslation, "/realtime/translations", "model", "", "realtime/translations/translations.py")
OAI_WS(kLiveSession, "/live/sessions", "", "", "live/live.py")
OAI_WS(kLiveSideband, "/live/sessions/{session_id}/attach", "graceful_close", "", "live/sideband.py")
OAI_WS(kLiveFork, "/live/sessions/{session_id}/fork", "", "", "live/forks.py")
@@ -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");
}
+71
View File
@@ -0,0 +1,71 @@
#pragma once
// Test rig: a transport that RECORDS the requests the module builds and
// answers a neutral "{}" (or a queued body for list walking). It never plays
// a vendor: assertions are about what the module sends.
#include <deque>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "clients/task.h"
#include "clients/transport.h"
#include "llm/api_client.h"
#include "openai/platform/platform.h"
namespace oai_test {
struct Seen {
std::string method;
std::string target; // path + query
molo::clients::HeaderMap headers;
std::string body;
};
class CaptureTransport : public molo::clients::Transport {
public:
const char* name() const override { return "capture"; }
molo::clients::Task<molo::clients::ClientResponse> send(const molo::clients::ClientRequest& request) override {
{
std::lock_guard<std::mutex> lock(mu);
seen.push_back({molo::clients::method_name(request.method()), request.url().target(),
request.headers(), request.body_bytes()});
}
std::string body = "{}";
{
std::lock_guard<std::mutex> lock(mu);
if (!queue.empty()) {
body = queue.front();
queue.pop_front();
}
}
molo::clients::HeaderMap h;
h.set("Content-Type", "application/json");
co_return molo::clients::ClientResponse(200, h, body);
}
std::mutex mu;
std::vector<Seen> seen;
std::deque<std::string> queue; // optional bodies, first in first out
};
struct Rig {
CaptureTransport* transport;
std::shared_ptr<molo::llm::ApiClient> client;
std::shared_ptr<molo::openai::platform::Platform> platform;
explicit Rig(const std::string& key = "sk-test-KEY") {
auto t = std::make_unique<CaptureTransport>();
transport = t.get();
molo::llm::ApiClientConfig c;
c.vendor = "openai";
c.base_url = molo::openai::platform::PlatformConfig::kBaseUrl;
c.key_env = "OPENAI_API_KEY";
client = std::make_shared<molo::llm::ApiClient>(std::move(t), c, key);
platform = std::make_shared<molo::openai::platform::Platform>(client);
}
const Seen& last() const { return transport->seen.back(); }
std::size_t sent() const { return transport->seen.size(); }
};
} // namespace oai_test
+355
View File
@@ -0,0 +1,355 @@
// OpenAI platform: the call table, local validation, request construction,
// forms, list walking. Nothing here plays the vendor.
#include <cstdlib>
#include <set>
#include "framework.h"
#include "llm/error.h"
#include "openai/platform/batches_api.h"
#include "openai/platform/caps.h"
#include "openai/platform/uploads_api.h"
#include "rig.h"
using namespace molo::openai::platform;
using molo::clients::body::Json;
using molo::llm::ApiError;
using molo::llm::NotConfiguredError;
using oai_test::Rig;
namespace {
PathArgs dummy_ids(const CallSpec& s) {
PathArgs a;
for (const std::string& n : Calls::placeholders(s)) a.emplace_back(n, "id_1");
return a;
}
Json minimal_body(const CallSpec& s) {
Json b = Json::object();
for (const std::string& r : Calls::list(s.required_fields)) b.set(r, Json("x"));
return b;
}
} // namespace
TEST(oai_calls, table_shape) {
ASSERT_EQ(Calls::count(), 188u);
std::set<std::string> ids, enums;
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
ASSERT_EQ(static_cast<std::size_t>(s.op), i);
ASSERT_TRUE(ids.insert(s.operation_id).second);
ASSERT_TRUE(s.path[0] == '/');
ASSERT_TRUE(std::string(s.method) == "GET" || std::string(s.method) == "POST" ||
std::string(s.method) == "DELETE");
ASSERT_TRUE(Calls::find(s.operation_id) == &s);
}
ASSERT_TRUE(Calls::find("nope") == nullptr);
}
TEST(oai_calls, every_call_prepares_with_minimal_input) {
for (std::size_t i = 0; i < Calls::count(); ++i) {
const CallSpec& s = Calls::at(i);
bool json = (s.req & kReqJson) != 0;
Json body = minimal_body(s);
auto p = PlatformService::prepare(s.op, dummy_ids(s), json ? &body : nullptr);
ASSERT_EQ(p.method, std::string(s.method));
ASSERT_EQ(p.target.find("{"), std::string::npos);
ASSERT_EQ(p.target.substr(0, 1), "/");
ASSERT_EQ(p.has_body, json);
// Flags drive the beta headers.
ASSERT_EQ(p.headers.count("OpenAI-Beta") != 0, Calls::has(s, kBetaAssistants) || Calls::has(s, kBetaChatkit));
}
}
TEST(oai_calls, unknown_and_missing_body_fields_are_rejected_before_the_network) {
Rig r;
Json bad = Json::object();
bad.set("model", Json("gpt-x"));
bad.set("not_a_field", Json(1));
ASSERT_THROW(r.platform->assistants.create(bad), ApiError);
ASSERT_THROW(r.platform->assistants.create(Json::object()), ApiError); // model is required
ASSERT_THROW(r.platform->fine_tuning_jobs.create(Json::object()), ApiError);
ASSERT_THROW(r.platform->files.retrieve("a/b"), ApiError);
ASSERT_THROW(r.platform->files.retrieve(".."), ApiError);
ASSERT_THROW(r.platform->files.retrieve(""), ApiError);
ASSERT_THROW(r.platform->files.retrieve(std::string(300, 'a')), ApiError);
ASSERT_THROW(r.platform->generic.invoke(Op::kRetrieveFile, {}), ApiError); // id missing
ASSERT_THROW(r.platform->generic.invoke(Op::kRetrieveFile, {{"file_id", "a"}, {"x", "b"}}), ApiError); // extra id
Json body = Json::object();
ASSERT_THROW(r.platform->generic.invoke(Op::kRetrieveFile, {{"file_id", "a"}}, &body), ApiError); // GET body
ASSERT_EQ(r.sent(), 0u);
}
TEST(oai_calls, validation_runs_before_the_credential_check) {
Rig r("");
ASSERT_FALSE(r.platform->configured());
try {
r.platform->files.retrieve("a/b");
FAIL("expected invalid");
} catch (const NotConfiguredError&) {
FAIL("validation must come first");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 400);
}
try {
r.platform->files.retrieve("file-1");
FAIL("expected not configured");
} catch (const NotConfiguredError& e) {
ASSERT_EQ(e.http_status(), 503);
ASSERT_TRUE(e.message().find("OPENAI_API_KEY") != std::string::npos);
}
ASSERT_EQ(r.sent(), 0u);
}
TEST(oai_calls, requests_carry_the_expected_method_path_auth_and_beta_headers) {
Rig r;
r.platform->files.retrieve("file-abc");
ASSERT_EQ(r.last().method, "GET");
ASSERT_EQ(r.last().target, "/v1/files/file-abc");
ASSERT_EQ(r.last().headers.get_or("Authorization", ""), "Bearer sk-test-KEY");
ASSERT_EQ(r.last().headers.get_or("OpenAI-Beta", ""), "");
ASSERT_EQ(r.last().target.find("sk-test"), std::string::npos);
r.platform->threads.retrieve("thread_1");
ASSERT_EQ(r.last().target, "/v1/threads/thread_1");
ASSERT_EQ(r.last().headers.get_or("OpenAI-Beta", ""), "assistants=v2");
r.platform->chatkit.retrieve_thread("cthr_1");
ASSERT_EQ(r.last().target, "/v1/chatkit/threads/cthr_1");
ASSERT_EQ(r.last().headers.get_or("OpenAI-Beta", ""), "chatkit_beta=v1");
r.platform->files.remove("file-abc");
ASSERT_EQ(r.last().method, "DELETE");
r.platform->fine_tuning_jobs.cancel("ftjob-1");
ASSERT_EQ(r.last().method, "POST");
ASSERT_EQ(r.last().target, "/v1/fine_tuning/jobs/ftjob-1/cancel");
Json body = Json::object();
body.set("project_ids", Json::array());
r.platform->fine_tuning_permissions.remove("ft:gpt-4o:org::abc:ckpt-step-1", "cp_1");
ASSERT_EQ(r.last().target, "/v1/fine_tuning/checkpoints/ft:gpt-4o:org::abc:ckpt-step-1/permissions/cp_1");
}
TEST(oai_calls, org_and_project_headers_come_from_config) {
setenv("OPENAI_ORG_ID", "org-test_1", 1);
setenv("OPENAI_PROJECT_ID", "proj_test-2", 1);
auto cfg = PlatformConfig::api_config();
ASSERT_EQ(cfg.default_headers["OpenAI-Organization"], "org-test_1");
ASSERT_EQ(cfg.default_headers["OpenAI-Project"], "proj_test-2");
ASSERT_EQ(cfg.key_env, "OPENAI_API_KEY");
setenv("OPENAI_ORG_ID", "bad\r\nX: y", 1);
ASSERT_THROW(PlatformConfig::api_config(), ApiError);
unsetenv("OPENAI_ORG_ID");
unsetenv("OPENAI_PROJECT_ID");
ASSERT_TRUE(PlatformConfig::api_config().default_headers.empty());
}
TEST(oai_calls, query_strings) {
Rig r;
Json q = Json::object();
q.set("limit", Json(5));
q.set("order", Json("desc"));
q.set("after", Json("file a&b"));
r.platform->files.list(q);
ASSERT_EQ(r.last().target, "/v1/files?after=file%20a%26b&limit=5&order=desc");
Json bad = Json::object();
bad.set("nope", Json(1));
ASSERT_THROW(r.platform->files.list(bad), ApiError);
Json big = Json::object();
big.set("limit", Json(10001));
ASSERT_THROW(r.platform->files.list(big), ApiError);
Json zero = Json::object();
zero.set("limit", Json(0));
ASSERT_THROW(r.platform->files.list(zero), ApiError);
// array: repeated key; deepObject: key[sub]
Json inc = Json::object();
Json arr = Json::array();
arr.push(Json("a.b"));
arr.push(Json("c"));
inc.set("include", arr);
r.platform->conversation_items.list("conv_1", inc);
ASSERT_EQ(r.last().target, "/v1/conversations/conv_1/items?include=a.b&include=c");
Json meta = Json::object();
Json m = Json::object();
m.set("team", Json("x y"));
meta.set("metadata", m);
r.platform->fine_tuning_jobs.list(meta);
ASSERT_EQ(r.last().target, "/v1/fine_tuning/jobs?metadata%5Bteam%5D=x%20y");
Json steps = Json::object();
Json sa = Json::array();
sa.push(Json("step_details.tool_calls[*].file_search.results[*].content"));
steps.set("include[]", sa);
r.platform->runs.steps("t_1", "run_1", steps);
ASSERT_TRUE(r.last().target.find("include%5B%5D=step_details.tool_calls%5B%2A%5D") != std::string::npos);
}
TEST(oai_calls, idempotency_key_only_where_the_spec_allows) {
Rig r;
Json body = Json::object();
Json env = Json::object();
env.set("type", Json("x"));
body.set("environment", env);
r.platform->agent_environments.create(body, "key-123");
ASSERT_EQ(r.last().headers.get_or("Idempotency-Key", ""), "key-123");
ASSERT_THROW(r.platform->generic.invoke(Op::kRetrieveFile, {{"file_id", "a"}}, nullptr, Json::object(),
{{"Idempotency-Key", "k"}}),
ApiError);
ASSERT_THROW(r.platform->agent_environments.create(body, "has space"), ApiError);
ASSERT_THROW(r.platform->generic.invoke(Op::kRetrieveFile, {{"file_id", "a"}}, nullptr, Json::object(),
{{"OpenAI-Beta", "x"}}),
ApiError);
}
TEST(oai_forms, files_create_is_multipart_with_bracket_notation) {
Rig r;
Json exp = Json::object();
exp.set("anchor", Json("created_at"));
exp.set("seconds", Json(3600));
r.platform->files.create("data.jsonl", "{\"a\":1}\n", "fine-tune", "application/jsonl", &exp);
ASSERT_EQ(r.last().method, "POST");
ASSERT_EQ(r.last().target, "/v1/files");
ASSERT_TRUE(r.last().headers.get_or("Content-Type", "").rfind("multipart/form-data; boundary=", 0) == 0);
const std::string& b = r.last().body;
ASSERT_TRUE(b.find("name=\"file\"; filename=\"data.jsonl\"") != std::string::npos);
ASSERT_TRUE(b.find("name=\"purpose\"") != std::string::npos);
ASSERT_TRUE(b.find("fine-tune") != std::string::npos);
ASSERT_TRUE(b.find("name=\"expires_after[anchor]\"") != std::string::npos);
ASSERT_TRUE(b.find("name=\"expires_after[seconds]\"") != std::string::npos);
ASSERT_TRUE(b.find("3600") != std::string::npos);
ASSERT_THROW(r.platform->files.create("x", "d", "bogus-purpose"), ApiError);
ASSERT_THROW(r.platform->files.create("a/b", "d", "batch"), ApiError);
ASSERT_THROW(r.platform->files.create("a\"b", "d", "batch"), ApiError);
ASSERT_THROW(r.platform->files.create("", "d", "batch"), ApiError);
}
TEST(oai_forms, form_fields_are_checked_against_the_table) {
Form f;
f.text("purpose", "batch");
ASSERT_THROW(Calls::check_form(Op::kCreateFile, f.names()), ApiError); // file missing
Form g;
g.file("file", "a", "text/plain", "x");
g.text("purpose", "batch");
g.text("surprise", "1");
ASSERT_THROW(Calls::check_form(Op::kCreateFile, g.names()), ApiError);
ASSERT_THROW(Calls::check_form(Op::kListFiles, {}), ApiError); // not multipart
Form skills;
skills.file("files[]", "SKILL.md", "text/markdown", "# x");
Calls::check_form(Op::kCreateSkill, skills.names());
}
TEST(oai_uploads, plan_and_sequence) {
auto plan = UploadPlan::make(10, 4);
ASSERT_EQ(plan.parts.size(), 3u);
ASSERT_EQ(plan.parts[2].offset, 8u);
ASSERT_EQ(plan.parts[2].size, 2u);
ASSERT_THROW(UploadPlan::make(0, 4), ApiError);
ASSERT_THROW(UploadPlan::make(10, 0), ApiError);
ASSERT_THROW(UploadPlan::make(10, Caps::kMaxUploadPartBytes + 1), ApiError);
ASSERT_THROW(UploadPlan::make(Caps::kMaxFileBytes + 1, Caps::kMaxUploadPartBytes), ApiError);
ASSERT_THROW(UploadPlan::make(Caps::kMaxUploadParts + 1, 1), ApiError);
// The module's own request sequence for a three-part upload. The queued
// bodies only carry the ids the next call needs (documented field `id`).
Rig r;
r.transport->queue = {"{\"id\":\"upload_1\"}", "{\"id\":\"part_1\"}", "{\"id\":\"part_2\"}",
"{\"id\":\"part_3\"}", "{\"id\":\"upload_1\",\"status\":\"completed\"}"};
r.platform->uploads.upload_all("big.jsonl", "application/jsonl", "batch", "0123456789", 4);
ASSERT_EQ(r.sent(), 5u);
ASSERT_EQ(r.transport->seen[0].target, "/v1/uploads");
ASSERT_TRUE(r.transport->seen[0].body.find("\"bytes\":10") != std::string::npos);
ASSERT_EQ(r.transport->seen[1].target, "/v1/uploads/upload_1/parts");
ASSERT_EQ(r.transport->seen[3].target, "/v1/uploads/upload_1/parts");
ASSERT_EQ(r.transport->seen[4].target, "/v1/uploads/upload_1/complete");
ASSERT_EQ(r.transport->seen[4].body, "{\"part_ids\":[\"part_1\",\"part_2\",\"part_3\"]}");
}
TEST(oai_batches, jsonl_builder_caps) {
BatchInput in("/v1/chat/completions");
Json body = Json::object();
body.set("model", Json("gpt-x"));
in.add("req-1", body).add("req-2", body);
ASSERT_EQ(in.size(), 2u);
ASSERT_EQ(in.jsonl().substr(0, in.jsonl().find('\n')),
"{\"body\":{\"model\":\"gpt-x\"},\"custom_id\":\"req-1\",\"method\":\"POST\",\"url\":\"/v1/chat/completions\"}");
ASSERT_THROW(in.add("req-1", body), ApiError);
ASSERT_THROW(in.add("", body), ApiError);
ASSERT_THROW(in.add(std::string(513, 'x'), body), ApiError);
ASSERT_THROW(in.add("r3", Json("not an object")), ApiError);
ASSERT_THROW(BatchInput("/v1/nope"), ApiError);
Rig r;
ASSERT_THROW(r.platform->batches.create("file-1", "/v1/nope"), ApiError);
r.platform->batches.create("file-1", "/v1/embeddings");
ASSERT_EQ(r.last().body, "{\"completion_window\":\"24h\",\"endpoint\":\"/v1/embeddings\",\"input_file_id\":\"file-1\"}");
}
TEST(oai_conversations, twenty_items_at_most) {
Rig r;
Json body = Json::object();
Json items = Json::array();
for (int i = 0; i < 21; ++i) items.push(Json::object());
body.set("items", items);
ASSERT_THROW(r.platform->conversation_items.create("conv_1", body), ApiError);
ASSERT_THROW(r.platform->conversations.create(body), ApiError);
ASSERT_EQ(r.sent(), 0u);
}
TEST(oai_list, walks_cursor_pages_and_reports_truncation) {
// Page shape per the spec's list schemas: data[], has_more, last_id.
Rig r;
r.transport->queue = {"{\"data\":[{\"id\":\"a\"},{\"id\":\"b\"}],\"has_more\":true,\"last_id\":\"b\"}",
"{\"data\":[{\"id\":\"c\"}],\"has_more\":false,\"last_id\":\"c\"}"};
auto l = r.platform->generic.list_all(Op::kListFiles, {}, Json::object(), 5);
ASSERT_EQ(l.pages, 2u);
ASSERT_EQ(l.data.as_array().size(), 3u);
ASSERT_FALSE(l.truncated);
ASSERT_EQ(r.transport->seen[1].target, "/v1/files?after=b");
Rig t;
t.transport->queue = {"{\"data\":[{\"id\":\"a\"}],\"has_more\":true,\"last_id\":\"a\"}",
"{\"data\":[{\"id\":\"b\"}],\"has_more\":true,\"last_id\":\"b\"}"};
auto cut = t.platform->generic.list_all(Op::kListFiles, {}, Json::object(), 2);
ASSERT_TRUE(cut.truncated);
ASSERT_TRUE(cut.has_more);
ASSERT_EQ(cut.stop_reason, "page_cap");
ASSERT_EQ(cut.pages, 2u);
ASSERT_THROW(t.platform->generic.list_all(Op::kRetrieveFile, {{"file_id", "a"}}), ApiError); // not a list
ASSERT_THROW(t.platform->generic.list_all(Op::kListFiles, {}, Json::object(), 0), ApiError);
}
TEST(oai_streams, only_streamable_calls_stream) {
Rig r;
Json body = Json::object();
body.set("assistant_id", Json("asst_1"));
ASSERT_THROW(r.platform->generic.invoke_stream(Op::kRetrieveFile, {{"file_id", "a"}}, Json::object(),
[](const molo::llm::SseEvent&) { return true; }),
ApiError);
// A streamable call sets "stream": true before sending.
r.transport->queue = {"data: [DONE]\n\n"};
r.platform->runs.create_stream("thread_1", body, [](const molo::llm::SseEvent&) { return true; });
ASSERT_EQ(r.last().target, "/v1/threads/thread_1/runs");
ASSERT_TRUE(r.last().body.find("\"stream\":true") != std::string::npos);
ASSERT_EQ(r.last().headers.get_or("OpenAI-Beta", ""), "assistants=v2");
}
TEST(oai_realtime_rest, call_and_secret_requests) {
Rig r;
r.platform->realtime.create_client_secret();
ASSERT_EQ(r.last().target, "/v1/realtime/client_secrets");
ASSERT_THROW(r.platform->realtime.create_call("not sdp"), ApiError);
r.platform->realtime.create_call("v=0\r\no=- 1 1 IN IP4 0.0.0.0\r\n");
ASSERT_EQ(r.last().target, "/v1/realtime/calls");
ASSERT_EQ(r.last().headers.get_or("Content-Type", ""), "application/sdp");
r.platform->realtime.create_call("v=0\r\n", Json::object());
ASSERT_TRUE(r.last().headers.get_or("Content-Type", "").rfind("multipart/form-data", 0) == 0);
ASSERT_TRUE(r.last().body.find("name=\"sdp\"") != std::string::npos);
ASSERT_TRUE(r.last().body.find("name=\"session\"") != std::string::npos);
ASSERT_THROW(r.platform->realtime.refer_call("call_1", "http://x"), ApiError);
r.platform->realtime.refer_call("call_1", "tel:+14155550123");
ASSERT_EQ(r.last().body, "{\"target_uri\":\"tel:+14155550123\"}");
ASSERT_THROW(r.platform->realtime.reject_call("call_1", 200), ApiError);
r.platform->realtime.reject_call("call_1", 486);
ASSERT_EQ(r.last().body, "{\"status_code\":486}");
r.platform->realtime.hangup_call("call_1");
ASSERT_EQ(r.last().target, "/v1/realtime/calls/call_1/hangup");
ASSERT_THROW(r.platform->live.reject("s_1", 0), ApiError);
}
+228
View File
@@ -0,0 +1,228 @@
// OpenAI platform: event catalogs, client event construction, server event
// parsing, and the WebSocket session over a test-owned loopback server.
#include <cstdlib>
#include "../../google/live/loopback.h"
#include "framework.h"
#include "llm/error.h"
#include "openai/platform/calls.h"
#include "openai/platform/caps.h"
#include "openai/platform/event_socket.h"
#include "openai/platform/events.h"
using namespace molo::openai::platform;
using molo::clients::body::Json;
using molo::llm::ApiError;
using molo::llm::NotConfiguredError;
using live_test::LoopbackServer;
using live_test::ServerConn;
namespace {
EventSocketOptions local(const LoopbackServer& s, const std::string& key = "sk-test-KEY") {
EventSocketOptions o;
o.base_url = "ws://127.0.0.1:" + std::to_string(s.port());
o.api_key = key;
return o;
}
} // namespace
TEST(oai_events, catalog_counts_per_profile) {
ASSERT_EQ(Events::count(), 217u);
ASSERT_EQ(Events::catalog(Profile::kGa, Direction::kClientEvent).size(), 12u);
ASSERT_EQ(Events::catalog(Profile::kTranslation, Direction::kClientEvent).size(), 3u);
ASSERT_TRUE(Events::find(Profile::kGa, Direction::kClientEvent, "session.update") != nullptr);
ASSERT_TRUE(Events::find(Profile::kGa, Direction::kClientEvent, "response.create") != nullptr);
ASSERT_TRUE(Events::find(Profile::kGa, Direction::kServerEvent, "response.output_audio.delta") != nullptr);
ASSERT_TRUE(Events::find(Profile::kBeta, Direction::kServerEvent, "response.output_audio.delta") != nullptr);
ASSERT_TRUE(Events::find(Profile::kGa, Direction::kClientEvent, "response.audio.delta") == nullptr);
ASSERT_TRUE(Events::find(Profile::kLive, Direction::kClientEvent, "session.start") != nullptr);
ASSERT_TRUE(Events::find(Profile::kLiveSideband, Direction::kClientEvent, "session.start") == nullptr);
Profile p;
ASSERT_TRUE(Events::parse_profile("live_sideband", p));
ASSERT_TRUE(p == Profile::kLiveSideband);
ASSERT_FALSE(Events::parse_profile("nope", p));
}
TEST(oai_events, every_client_event_builds_from_its_required_fields) {
for (std::size_t i = 0; i < Events::count(); ++i) {
const EventSpec& e = Events::at(i);
if (e.direction != Direction::kClientEvent) continue;
Json f = Json::object();
for (const std::string& r : Calls::list(e.required))
if (r != "type") f.set(r, Json("x"));
Json ev = Events::make_client(e.profile, e.type, f);
ASSERT_EQ(ev.find("type")->as_string(), std::string(e.type));
}
}
TEST(oai_events, client_event_validation) {
Json f = Json::object();
f.set("session", Json::object());
Json ev = Events::make_client(Profile::kGa, "session.update", f, "evt_1");
ASSERT_EQ(ev.dump(), "{\"event_id\":\"evt_1\",\"session\":{},\"type\":\"session.update\"}");
ASSERT_THROW(Events::make_client(Profile::kGa, "session.update"), ApiError);
ASSERT_THROW(Events::make_client(Profile::kGa, "session.nope", f), ApiError);
ASSERT_THROW(Events::make_client(Profile::kGa, "response.audio.delta", f), ApiError);
Json extra = f;
extra.set("surprise", Json(1));
ASSERT_THROW(Events::make_client(Profile::kGa, "session.update", extra), ApiError);
Json typed = f;
typed.set("type", Json("x"));
ASSERT_THROW(Events::make_client(Profile::kGa, "session.update", typed), ApiError);
ASSERT_THROW(Events::make_client(Profile::kGa, "session.update", f, std::string(513, 'e')), ApiError);
Json a = Events::audio_append(Profile::kGa, "abc");
ASSERT_EQ(a.dump(), "{\"audio\":\"YWJj\",\"type\":\"input_audio_buffer.append\"}");
ASSERT_EQ(Events::audio_append(Profile::kTranslation, "abc").find("type")->as_string(),
"session.input_audio_buffer.append");
ASSERT_EQ(Events::audio_append(Profile::kLive, "abc").find("type")->as_string(), "session.input_audio.append");
ASSERT_THROW(Events::audio_append(Profile::kLiveSideband, "abc"), ApiError);
ASSERT_THROW(Events::audio_append(Profile::kGa, ""), ApiError);
ASSERT_THROW(Events::audio_append(Profile::kGa, std::string(Caps::kMaxAudioChunkBytes + 1, 'a')), ApiError);
}
TEST(oai_events, server_event_parsing) {
auto ok = Events::parse_server(Profile::kGa, "{\"type\":\"session.created\",\"event_id\":\"event_1\",\"session\":{}}");
ASSERT_TRUE(ok.known);
ASSERT_EQ(ok.event_id, "event_1");
ASSERT_FALSE(ok.is_error);
auto err = Events::parse_server(
Profile::kGa, "{\"type\":\"error\",\"event_id\":\"e\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"nope\"}}");
ASSERT_TRUE(err.is_error);
ASSERT_EQ(err.error_message, "nope");
auto unk = Events::parse_server(Profile::kGa, "{\"type\":\"future.event\"}");
ASSERT_FALSE(unk.known);
ASSERT_THROW(Events::parse_server(Profile::kGa, "not json"), ApiError);
ASSERT_THROW(Events::parse_server(Profile::kGa, "{\"no\":\"type\"}"), ApiError);
ASSERT_THROW(Events::parse_server(Profile::kGa, "[1]"), ApiError);
}
TEST(oai_ws, urls_per_endpoint) {
WsParams p;
p.model = "gpt-realtime";
ASSERT_EQ(EventSocket::url_for(WsKind::kRealtime, "wss://api.openai.com/v1", p),
"wss://api.openai.com/v1/realtime?model=gpt-realtime");
WsParams c;
c.call_id = "rtc_1";
ASSERT_EQ(EventSocket::url_for(WsKind::kRealtime, "wss://h/v1", c), "wss://h/v1/realtime?call_id=rtc_1");
ASSERT_EQ(EventSocket::url_for(WsKind::kRealtimeTranslation, "wss://h/v1", p),
"wss://h/v1/realtime/translations?model=gpt-realtime");
ASSERT_EQ(EventSocket::url_for(WsKind::kLiveSession, "wss://h/v1", WsParams{}), "wss://h/v1/live/sessions");
WsParams s;
s.session_id = "live_1";
s.graceful_close = true;
ASSERT_EQ(EventSocket::url_for(WsKind::kLiveSideband, "wss://h/v1", s),
"wss://h/v1/live/sessions/live_1/attach?graceful_close=true");
WsParams fork;
fork.session_id = "live_1";
ASSERT_EQ(EventSocket::url_for(WsKind::kLiveFork, "wss://h/v1", fork), "wss://h/v1/live/sessions/live_1/fork");
ASSERT_THROW(EventSocket::url_for(WsKind::kRealtime, "wss://h/v1", WsParams{}), ApiError);
ASSERT_THROW(EventSocket::url_for(WsKind::kLiveSession, "wss://h/v1", p), ApiError);
ASSERT_THROW(EventSocket::url_for(WsKind::kLiveSideband, "wss://h/v1", WsParams{}), ApiError);
WsParams evil;
evil.model = "a&b=c";
ASSERT_THROW(EventSocket::url_for(WsKind::kRealtime, "wss://h/v1", evil), ApiError);
ASSERT_THROW(EventSocket::url_for(WsKind::kRealtime, "https://h/v1", p), ApiError);
ASSERT_THROW(EventSocket::url_for(WsKind::kRealtime, "wss://h/v1/", p), ApiError);
ASSERT_EQ(EventSocket::endpoint_count(), 6u);
}
TEST(oai_ws, no_key_means_503_before_any_traffic) {
setenv("MOLO_ENV_FILES", "off", 1);
unsetenv("OPENAI_API_KEY");
EventSocketOptions o;
EventSocket s(WsKind::kRealtime, o);
WsParams p;
p.model = "gpt-realtime";
try {
s.connect(p);
FAIL("expected NotConfiguredError");
} catch (const NotConfiguredError& e) {
ASSERT_EQ(e.http_status(), 503);
}
}
TEST(oai_ws, session_over_a_loopback_server) {
// The server side is test-owned and speaks event shapes from the spec.
std::string seen_type;
LoopbackServer server([&](ServerConn& c) {
c.send_text("{\"type\":\"session.created\",\"event_id\":\"event_1\",\"session\":{\"id\":\"sess_1\"}}");
if (auto m = c.read_message()) seen_type = *m;
c.send_text("{\"type\":\"response.output_audio.delta\",\"event_id\":\"event_2\",\"response_id\":\"r\","
"\"item_id\":\"i\",\"output_index\":0,\"content_index\":0,\"delta\":\"YWJj\"}");
c.send_text("{\"type\":\"a.future.event\"}");
c.send_close(1000, "bye");
});
EventSocket sock(WsKind::kRealtime, local(server));
WsParams p;
p.model = "gpt-realtime";
sock.connect(p);
ASSERT_TRUE(server.request().find("GET /realtime?model=gpt-realtime ") != std::string::npos);
ASSERT_TRUE(server.request().find("Authorization: Bearer sk-test-KEY") != std::string::npos);
ASSERT_TRUE(server.request().find("OpenAI-Beta") == std::string::npos);
Json f = Json::object();
f.set("session", Json::object());
sock.send(Events::make_client(Profile::kGa, "session.update", f));
ASSERT_THROW(sock.send(Json::object()), ApiError); // no type
Json wrong = Json::object();
wrong.set("type", Json("response.audio.delta"));
ASSERT_THROW(sock.send(wrong), ApiError); // a server event is not sendable
std::vector<std::string> types;
RunStats st = sock.run(
[&](const ParsedEvent& e) {
types.push_back(e.type);
return true;
},
10);
ASSERT_EQ(types.size(), 3u);
ASSERT_EQ(types[0], "session.created");
ASSERT_EQ(types[1], "response.output_audio.delta");
ASSERT_EQ(st.unknown, 1u);
ASSERT_EQ(st.stop_reason, "closed");
ASSERT_EQ(st.close_code, 1000);
ASSERT_TRUE(seen_type.find("\"type\":\"session.update\"") != std::string::npos);
}
TEST(oai_ws, beta_header_and_sink_stop) {
LoopbackServer server([&](ServerConn& c) {
for (int i = 0; i < 5; ++i) c.send_text("{\"type\":\"session.created\"}");
c.read_message();
});
EventSocket sock(WsKind::kRealtimeBeta, local(server));
WsParams p;
p.model = "gpt-4o-realtime-preview";
sock.connect(p);
ASSERT_TRUE(server.request().find("OpenAI-Beta: realtime=v1") != std::string::npos);
int n = 0;
RunStats st = sock.run([&](const ParsedEvent&) { return ++n < 2; }, 5);
ASSERT_EQ(st.stop_reason, "sink_stopped");
ASSERT_EQ(st.events, 2u);
sock.close();
ASSERT_FALSE(sock.connected());
}
TEST(oai_ws, refused_upgrade_maps_to_http_status_and_hides_the_key) {
LoopbackServer server([](ServerConn&) {}, LoopbackServer::Handshake::Status403);
EventSocket sock(WsKind::kRealtime, local(server, "sk-secret-123"));
WsParams p;
p.model = "gpt-realtime";
try {
sock.connect(p);
FAIL("expected refusal");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 403);
ASSERT_EQ(std::string(e.what()).find("sk-secret"), std::string::npos);
}
}
TEST(oai_ws, run_times_out_without_events) {
LoopbackServer server([](ServerConn& c) { c.read_message(); });
EventSocket sock(WsKind::kLiveSession, local(server));
sock.connect(WsParams{});
RunStats st = sock.run([](const ParsedEvent&) { return true; }, 1);
ASSERT_EQ(st.stop_reason, "timeout");
ASSERT_THROW(sock.run([](const ParsedEvent&) { return true; }, 0), ApiError);
ASSERT_THROW(sock.run([](const ParsedEvent&) { return true; }, 3601), ApiError);
}
+44
View File
@@ -0,0 +1,44 @@
// Opt-in live checks against api.openai.com. Run with MOLO_LIVE_NET=1 and
// OPENAI_API_KEY; otherwise each test prints a loud SKIP. Read-only lists
// with limit=1 only: nothing is created, updated or deleted.
#include <cstdlib>
#include <iostream>
#include "framework.h"
#include "llm/config.h"
#include "llm/error.h"
#include "openai/platform/platform.h"
using namespace molo::openai::platform;
using molo::clients::body::Json;
namespace {
bool live(const char* name) {
if (!std::getenv("MOLO_LIVE_NET") || molo::llm::Config::get("OPENAI_API_KEY").empty()) {
std::cout << "SKIP " << name << " (set MOLO_LIVE_NET=1 and OPENAI_API_KEY)\n";
return false;
}
return true;
}
Json one() {
Json q = Json::object();
q.set("limit", Json(1));
return q;
}
} // namespace
TEST(oai_live, read_only_lists) {
if (!live("oai_live.read_only_lists")) return;
auto p = Platform::from_env();
for (Json r : {p->files.list(one()), p->batches.list(one()), p->fine_tuning_jobs.list(one()),
p->vector_stores.list(one()), p->evals.list(one()), p->containers.list(one())}) {
ASSERT_TRUE(r.is_object());
ASSERT_TRUE(r.has("data"));
}
}
TEST(oai_live, client_secret_is_not_created_here) {
// Creating a client secret is a write on the account; it is left to the
// demo route with confirm:true. This test only documents that decision.
if (!live("oai_live.client_secret_is_not_created_here")) return;
}