From a2c1186036eaaee53514ed0e71b60029e23a69e3 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 25 Oct 2026 15:04:27 +0100 Subject: [PATCH] feat(clients): azure: Azure OpenAI and AI Foundry package, 147-row spec table, Entra auth, demo Co-Authored-By: Claude Sonnet 5.5 --- example/app/ai/AzureDemo.cpp | 666 +++++++++++++++++- example/assets/ai_azure.js | 72 ++ src/vendors/azure/SPEC.md | 274 +++++++ src/vendors/azure/api_version.cpp | 32 + src/vendors/azure/api_version.h | 39 + src/vendors/azure/assistants.cpp | 146 ++++ src/vendors/azure/assistants.h | 63 ++ src/vendors/azure/audio.cpp | 49 ++ src/vendors/azure/audio.h | 38 + src/vendors/azure/batches.cpp | 38 + src/vendors/azure/batches.h | 27 + src/vendors/azure/calls.cpp | 114 +++ src/vendors/azure/calls.h | 76 ++ src/vendors/azure/calls_table.inc | 152 ++++ src/vendors/azure/caps.h | 48 ++ src/vendors/azure/chat.cpp | 38 + src/vendors/azure/chat.h | 42 ++ src/vendors/azure/chat_model.cpp | 83 +++ src/vendors/azure/chat_model.h | 57 ++ src/vendors/azure/check.cpp | 63 ++ src/vendors/azure/check.h | 34 + src/vendors/azure/client.cpp | 67 ++ src/vendors/azure/client.h | 51 ++ src/vendors/azure/embeddings.cpp | 53 ++ src/vendors/azure/embeddings.h | 25 + src/vendors/azure/entra.cpp | 146 ++++ src/vendors/azure/entra.h | 76 ++ src/vendors/azure/files.cpp | 87 +++ src/vendors/azure/files.h | 39 + src/vendors/azure/fine_tuning.cpp | 89 +++ src/vendors/azure/fine_tuning.h | 40 ++ src/vendors/azure/foundry.cpp | 55 ++ src/vendors/azure/foundry.h | 39 + src/vendors/azure/images.cpp | 54 ++ src/vendors/azure/images.h | 35 + src/vendors/azure/models.cpp | 30 + src/vendors/azure/models.h | 26 + src/vendors/azure/operations.cpp | 90 +++ src/vendors/azure/operations.h | 53 ++ src/vendors/azure/realtime.cpp | 209 ++++++ src/vendors/azure/realtime.h | 85 +++ src/vendors/azure/responses.cpp | 58 ++ src/vendors/azure/responses.h | 37 + src/vendors/azure/safety.cpp | 121 ++++ src/vendors/azure/safety.h | 47 ++ src/vendors/azure/service.cpp | 291 ++++++++ src/vendors/azure/service.h | 117 +++ src/vendors/azure/settings.cpp | 112 +++ src/vendors/azure/settings.h | 82 +++ .../tools/__pycache__/specdoc.cpython-314.pyc | Bin 0 -> 6786 bytes src/vendors/azure/tools/check_spec.py | 151 ++++ src/vendors/azure/tools/gen_calls.py | 109 +++ src/vendors/azure/tools/specdoc.py | 119 ++++ src/vendors/azure/upload_file.h | 15 + src/vendors/azure/vector_stores.cpp | 107 +++ src/vendors/azure/vector_stores.h | 41 ++ tests/example/ai/azure/page_test.cpp | 39 + tests/example/ai/azure/routes_test.cpp | 111 +++ tests/src/vendors/azure/rig.h | 109 +++ tests/src/vendors/azure/test_calls.cpp | 105 +++ tests/src/vendors/azure/test_chat_model.cpp | 152 ++++ tests/src/vendors/azure/test_entra.cpp | 161 +++++ tests/src/vendors/azure/test_gateway.cpp | 239 +++++++ tests/src/vendors/azure/test_live.cpp | 108 +++ tests/src/vendors/azure/test_realtime.cpp | 145 ++++ tests/src/vendors/azure/test_services.cpp | 419 +++++++++++ tests/src/vendors/azure/test_settings.cpp | 115 +++ 67 files changed, 6607 insertions(+), 3 deletions(-) create mode 100644 example/assets/ai_azure.js create mode 100644 src/vendors/azure/SPEC.md create mode 100644 src/vendors/azure/api_version.cpp create mode 100644 src/vendors/azure/api_version.h create mode 100644 src/vendors/azure/assistants.cpp create mode 100644 src/vendors/azure/assistants.h create mode 100644 src/vendors/azure/audio.cpp create mode 100644 src/vendors/azure/audio.h create mode 100644 src/vendors/azure/batches.cpp create mode 100644 src/vendors/azure/batches.h create mode 100644 src/vendors/azure/calls.cpp create mode 100644 src/vendors/azure/calls.h create mode 100644 src/vendors/azure/calls_table.inc create mode 100644 src/vendors/azure/caps.h create mode 100644 src/vendors/azure/chat.cpp create mode 100644 src/vendors/azure/chat.h create mode 100644 src/vendors/azure/chat_model.cpp create mode 100644 src/vendors/azure/chat_model.h create mode 100644 src/vendors/azure/check.cpp create mode 100644 src/vendors/azure/check.h create mode 100644 src/vendors/azure/client.cpp create mode 100644 src/vendors/azure/client.h create mode 100644 src/vendors/azure/embeddings.cpp create mode 100644 src/vendors/azure/embeddings.h create mode 100644 src/vendors/azure/entra.cpp create mode 100644 src/vendors/azure/entra.h create mode 100644 src/vendors/azure/files.cpp create mode 100644 src/vendors/azure/files.h create mode 100644 src/vendors/azure/fine_tuning.cpp create mode 100644 src/vendors/azure/fine_tuning.h create mode 100644 src/vendors/azure/foundry.cpp create mode 100644 src/vendors/azure/foundry.h create mode 100644 src/vendors/azure/images.cpp create mode 100644 src/vendors/azure/images.h create mode 100644 src/vendors/azure/models.cpp create mode 100644 src/vendors/azure/models.h create mode 100644 src/vendors/azure/operations.cpp create mode 100644 src/vendors/azure/operations.h create mode 100644 src/vendors/azure/realtime.cpp create mode 100644 src/vendors/azure/realtime.h create mode 100644 src/vendors/azure/responses.cpp create mode 100644 src/vendors/azure/responses.h create mode 100644 src/vendors/azure/safety.cpp create mode 100644 src/vendors/azure/safety.h create mode 100644 src/vendors/azure/service.cpp create mode 100644 src/vendors/azure/service.h create mode 100644 src/vendors/azure/settings.cpp create mode 100644 src/vendors/azure/settings.h create mode 100644 src/vendors/azure/tools/__pycache__/specdoc.cpython-314.pyc create mode 100644 src/vendors/azure/tools/check_spec.py create mode 100644 src/vendors/azure/tools/gen_calls.py create mode 100644 src/vendors/azure/tools/specdoc.py create mode 100644 src/vendors/azure/upload_file.h create mode 100644 src/vendors/azure/vector_stores.cpp create mode 100644 src/vendors/azure/vector_stores.h create mode 100644 tests/example/ai/azure/page_test.cpp create mode 100644 tests/example/ai/azure/routes_test.cpp create mode 100644 tests/src/vendors/azure/rig.h create mode 100644 tests/src/vendors/azure/test_calls.cpp create mode 100644 tests/src/vendors/azure/test_chat_model.cpp create mode 100644 tests/src/vendors/azure/test_entra.cpp create mode 100644 tests/src/vendors/azure/test_gateway.cpp create mode 100644 tests/src/vendors/azure/test_live.cpp create mode 100644 tests/src/vendors/azure/test_realtime.cpp create mode 100644 tests/src/vendors/azure/test_services.cpp create mode 100644 tests/src/vendors/azure/test_settings.cpp diff --git a/example/app/ai/AzureDemo.cpp b/example/app/ai/AzureDemo.cpp index e33aef9..aaae170 100644 --- a/example/app/ai/AzureDemo.cpp +++ b/example/app/ai/AzureDemo.cpp @@ -1,13 +1,673 @@ #include "../AiDemos.hpp" +#include +#include +#include +#include + #include "../../fw/Router.hpp" +#include "clients/transports/http_transport.h" +#include "../../fw/Templates.hpp" +#include "llm/error.h" +#include "security/crypto/Base64.h" +#include "vendors/azure/audio.h" +#include "vendors/azure/chat_model.h" +#include "vendors/azure/check.h" +#include "vendors/azure/client.h" +#include "vendors/azure/embeddings.h" +#include "vendors/azure/foundry.h" +#include "vendors/azure/images.h" +#include "vendors/azure/models.h" +#include "vendors/azure/operations.h" +#include "vendors/azure/realtime.h" +#include "vendors/azure/safety.h" namespace molo::example::app { -// azure. Stub: the package agent fills this in. +// Azure OpenAI / AI Foundry over the real src/vendors/azure part. GET routes +// need no credential. Every route that calls Azure is a POST: 422 on bad +// input, 503 not_configured without AZURE_OPENAI_ENDPOINT plus a credential +// (AZURE_OPENAI_API_KEY, or AZURE_TENANT_ID / AZURE_CLIENT_ID / +// AZURE_CLIENT_SECRET). Routes that cost money or change resources are dry +// runs that show the exact request (path, api-version, body) unless the body +// has "confirm":true. + +namespace { + +namespace az = azure; +using Json = clients::body::Json; + +constexpr std::size_t kMaxBody = 8 * 1024 * 1024; +constexpr std::size_t kMaxAudioInline = 256 * 1024; // bytes returned inline as base64 + +const char kPage[] = R"AZURE({% extends "base.html" %} +{% block content %} +

Azure OpenAI and AI Foundry

+

Deployments, v1 and dated API versions, key or Microsoft Entra auth, chat, responses, +embeddings, images, audio, realtime, assistants, batch, fine-tuning, files, vector stores, Foundry +model inference, content safety. Paid and changing calls are dry runs until the body says +"confirm":true.

+

Checking credential...

+
+ + + +
+ + +
+ +

+
+ + +{% endblock %} +)AZURE"; + +[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); } + +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; +} + +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& b, const char* key, bool required = false) { + const Json* v = b.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"); + if (required && v->as_string().empty()) bad(std::string(key) + " is required"); + return v->as_string(); +} + +std::optional number(const Json& b, const char* key) { + const Json* v = b.find(key); + if (!v || v->is_null()) return std::nullopt; + if (!v->is_number()) bad(std::string(key) + " must be a number"); + return static_cast(v->as_int()); +} + +bool flag(const Json& b, const char* key) { + const Json* v = b.find(key); + if (!v || v->is_null()) return false; + if (!v->is_bool()) bad(std::string(key) + " must be true or false"); + return v->as_bool(); +} + +std::vector strings(const Json& b, const char* key) { + std::vector out; + const Json* v = b.find(key); + if (!v || v->is_null()) return out; + if (v->is_string()) { + out.push_back(v->as_string()); + return out; + } + if (!v->is_array()) bad(std::string(key) + " must be a string or an array of strings"); + for (const Json& s : v->as_array()) { + if (!s.is_string()) bad(std::string(key) + " must be an array of strings"); + out.push_back(s.as_string()); + } + return out; +} + +Json usage_json(const llm::Usage& u) { + Json o = Json::object(); + o.set("input_tokens", Json(u.input_tokens)); + o.set("output_tokens", Json(u.output_tokens)); + o.set("total_tokens", Json(u.total_tokens)); + return o; +} + +Json dry_run(const Json& request, const std::string& path, const std::string& method) { + Json o = Json::object(); + o.set("dry_run", Json(true)); + o.set("would_send_to", Json(method + " " + path)); + o.set("request", request); + o.set("hint", Json("send confirm:true to run it")); + return o; +} + +std::string auth_name(az::AuthKind k) { + switch (k) { + case az::AuthKind::kApiKey: return "api_key"; + case az::AuthKind::kEntra: return "entra"; + case az::AuthKind::kNone: break; + } + return "none"; +} + +// One route = validate into a Plan, then the generic runner decides dry run, +// credential, call and error mapping. +struct Plan { + Json request; // what would be sent (shown in a dry run) + std::string path; // path with api-version, for the dry-run message + std::string method = "POST"; + bool needs_confirm = false; // paid or changes a resource + std::function call; +}; + +class AzureRoutes { + public: + AzureRoutes(std::shared_ptr client, std::string config_error) + : c_(std::move(client)), config_error_(std::move(config_error)) {} + + const az::AzureClient& client() const { return *c_; } + const std::string& config_error() const { return config_error_; } + + using Planner = std::function; + + fw::Response run(const fw::Request& req, const Planner& planner) const { + Json body = parse_body(req); + Plan plan; + try { + plan = planner(body); + } catch (const llm::ApiError& e) { + map(e); + } + if (plan.needs_confirm && !flag(body, "confirm")) return json_out(dry_run(plan.request, plan.path, plan.method)); + try { + return json_out(plan.call()); + } catch (const llm::ApiError& e) { + map(e); + } + } + + // ---- planners ----------------------------------------------------------- + Plan chat(const Json& b) const { + std::string dep = text(b, "deployment", true); + az::ChatModelConfig cfg; + std::string dialect = text(b, "dialect"); + if (dialect == "responses") cfg.dialect = az::Dialect::kResponses; + else if (dialect == "foundry") cfg.dialect = az::Dialect::kFoundry; + else if (!dialect.empty() && dialect != "chat") bad("dialect must be chat, responses or foundry"); + cfg.extra_parameters = text(b, "extra_parameters"); + llm::ChatRequest r; + r.model = dep; + r.system = text(b, "system"); + r.messages.push_back(llm::Message::user(text(b, "prompt", true))); + r.max_tokens = number(b, "max_tokens"); + if (const Json* t = b.find("temperature"); t && t->is_number()) r.temperature = t->as_number(); + auto model = std::make_shared(c_, cfg); + Json body = model->wire_body(r, false); + Plan pl; + pl.request = body; + if (cfg.dialect == az::Dialect::kResponses) pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateResponse, {}); + else if (cfg.dialect == az::Dialect::kFoundry) + pl.path = path_of(az::Surface::kFoundry, az::Op::kFoundryGetChatCompletions, {}); + else pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateChatCompletion, {dep, {}}); + pl.needs_confirm = true; + pl.call = [model, r] { + llm::ChatResponse out = model->generate(r); + Json o = Json::object(); + o.set("id", Json(out.id)); + o.set("model", Json(out.model)); + o.set("finish_reason", Json(out.finish_reason)); + o.set("text", Json(out.text())); + o.set("usage", usage_json(out.usage)); + return o; + }; + return pl; + } + + Plan embeddings(const Json& b) const { + std::string dep = text(b, "deployment", true); + std::vector input = strings(b, "input"); + if (input.empty()) bad("input is required"); + int dims = number(b, "dimensions").value_or(0); + Json req = Json::object(); + Json arr = Json::array(); + for (const std::string& s : input) arr.push(Json(s)); + req.set("input", arr); + if (dims > 0) req.set("dimensions", Json(dims)); + az::EmbeddingsService svc(c_); + Plan pl; + pl.request = svc.prepare_body(az::Op::kCreateEmbedding, {dep, {}}, req); + pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateEmbedding, {dep, {}}); + pl.needs_confirm = true; + pl.call = [this, dep, input, dims] { + az::EmbeddingsService s(c_); + auto vectors = s.embed(dep, input, dims); + Json o = Json::object(); + o.set("count", Json(static_cast(vectors.size()))); + o.set("dimensions", Json(static_cast(vectors.empty() ? 0 : vectors[0].size()))); + Json head = Json::array(); + if (!vectors.empty()) + for (std::size_t i = 0; i < vectors[0].size() && i < 8; ++i) head.push(Json(vectors[0][i])); + o.set("first_values", head); + return o; + }; + return pl; + } + + Plan images(const Json& b) const { + std::string dep = text(b, "deployment", true); + Json req = Json::object(); + req.set("prompt", Json(text(b, "prompt", true))); + if (std::string size = text(b, "size"); !size.empty()) req.set("size", Json(size)); + if (auto n = number(b, "n")) req.set("n", Json(*n)); + if (std::string q = text(b, "quality"); !q.empty()) req.set("quality", Json(q)); + az::ImagesService svc(c_); + Plan pl; + pl.request = svc.prepare_body(az::Op::kCreateImage, {dep, {}}, req); + pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateImage, {dep, {}}); + pl.needs_confirm = true; + pl.call = [this, dep, req] { + az::ImagesService s(c_); + Json doc = s.generate(dep, req); + Json o = Json::object(); + const Json* data = doc.find("data"); + o.set("count", Json(data && data->is_array() ? static_cast(data->as_array().size()) : 0)); + Json items = Json::array(); + if (data && data->is_array()) + for (const Json& d : data->as_array()) { + Json x = Json::object(); + if (const Json* u = d.find("url"); u && u->is_string()) x.set("url", *u); + if (const Json* p = d.find("revised_prompt"); p && p->is_string()) x.set("revised_prompt", *p); + if (const Json* j = d.find("b64_json"); j && j->is_string()) + x.set("b64_json_chars", Json(static_cast(j->as_string().size()))); + items.push(x); + } + o.set("images", items); + return o; + }; + return pl; + } + + Plan speech(const Json& b) const { + std::string dep = text(b, "deployment", true); + Json req = Json::object(); + req.set("input", Json(text(b, "input", true))); + req.set("voice", Json(text(b, "voice", true))); + if (std::string f = text(b, "response_format"); !f.empty()) req.set("response_format", Json(f)); + az::AudioService svc(c_); + Plan pl; + pl.request = svc.prepare_body(az::Op::kCreateSpeech, {dep, {}}, req); + pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateSpeech, {dep, {}}); + pl.needs_confirm = true; + pl.call = [this, dep, req] { + az::AudioService s(c_); + llm::RawResponse r = s.speech(dep, req); + Json o = Json::object(); + o.set("content_type", Json(r.content_type)); + o.set("bytes", Json(static_cast(r.body.size()))); + if (r.body.size() <= kMaxAudioInline) + o.set("audio_base64", Json(security::crypto::Base64::encode(r.body))); + return o; + }; + return pl; + } + + Plan models(const Json&) const { + return Plan{Json::object(), path_of(az::Surface::kOpenAi, az::Op::kListModels, {}), "GET", false, [this] { + az::ModelsService s(c_); + Json arr = Json::array(); + for (const std::string& id : s.ids()) arr.push(Json(id)); + Json o = Json::object(); + o.set("models", arr); + return o; + }}; + } + + Plan foundry(const Json& b) const { + std::string action = text(b, "action", true); + if (action == "info") { + return Plan{Json::object(), path_of(az::Surface::kFoundry, az::Op::kFoundryGetModelInfo, {}), "GET", false, [this] { + az::FoundryService s(c_); + return s.info(); + }}; + } + std::string extra = text(b, "extra_parameters"); + if (action == "chat") { + Json req = Json::object(); + Json msgs = Json::array(); + if (std::string sys = text(b, "system"); !sys.empty()) { + Json m = Json::object(); + m.set("role", Json("system")); + m.set("content", Json(sys)); + msgs.push(m); + } + Json m = Json::object(); + m.set("role", Json("user")); + m.set("content", Json(text(b, "prompt", true))); + msgs.push(m); + req.set("messages", msgs); + if (std::string model = text(b, "model"); !model.empty()) req.set("model", Json(model)); + if (auto n = number(b, "max_tokens")) req.set("max_tokens", Json(*n)); + az::FoundryService svc(c_); + Plan pl; + pl.request = svc.prepare_body(az::Op::kFoundryGetChatCompletions, {}, req); + pl.path = path_of(az::Surface::kFoundry, az::Op::kFoundryGetChatCompletions, {}); + pl.needs_confirm = true; + pl.call = [this, req, extra] { + az::FoundryService s(c_); + Json doc = s.chat(req, extra); + Json o = Json::object(); + o.set("model", doc.find("model") ? *doc.find("model") : Json("")); + std::string t; + if (const Json* ch = doc.find("choices"); ch && ch->is_array() && !ch->as_array().empty()) + if (const Json* msg = ch->as_array()[0].find("message")) + if (const Json* c = msg->find("content"); c && c->is_string()) t = c->as_string(); + o.set("text", Json(t)); + if (const Json* u = doc.find("usage")) o.set("usage", *u); + return o; + }; + return pl; + } + if (action == "embeddings") { + std::vector input = strings(b, "input"); + if (input.empty()) bad("input is required"); + Json req = Json::object(); + Json arr = Json::array(); + for (const std::string& s : input) arr.push(Json(s)); + req.set("input", arr); + if (std::string model = text(b, "model"); !model.empty()) req.set("model", Json(model)); + az::FoundryService svc(c_); + Plan pl; + pl.request = svc.prepare_body(az::Op::kFoundryGetEmbeddings, {}, req); + pl.path = path_of(az::Surface::kFoundry, az::Op::kFoundryGetEmbeddings, {}); + pl.needs_confirm = true; + pl.call = [this, req, extra] { + az::FoundryService s(c_); + Json doc = s.embeddings(req, extra); + Json o = Json::object(); + const Json* data = doc.find("data"); + o.set("count", Json(data && data->is_array() ? static_cast(data->as_array().size()) : 0)); + if (const Json* u = doc.find("usage")) o.set("usage", *u); + return o; + }; + return pl; + } + bad("action must be info, chat or embeddings"); + } + + Plan safety(const Json& b) const { + std::string action = text(b, "action", true); + az::SafetyService svc(c_); + if (action == "blocklists") { + return Plan{Json::object(), path_of(az::Surface::kSafety, az::Op::kSafetyTextBlocklistsListTextBlocklists, {}), + "GET", false, [this] { + az::SafetyService s(c_); + return s.list_blocklists(); + }}; + } + Json req = Json::object(); + az::Op op; + if (action == "text") { + req.set("text", Json(text(b, "text", true))); + std::vector cats = strings(b, "categories"); + if (!cats.empty()) { + Json arr = Json::array(); + for (const std::string& c : cats) arr.push(Json(c)); + req.set("categories", arr); + } + op = az::Op::kSafetyTextOperationsAnalyzeText; + } else if (action == "image") { + Json img = Json::object(); + img.set("content", Json(text(b, "content_base64", true))); + req.set("image", img); + op = az::Op::kSafetyImageOperationsAnalyzeImage; + } else if (action == "shield") { + if (std::string p = text(b, "user_prompt"); !p.empty()) req.set("userPrompt", Json(p)); + std::vector docs = strings(b, "documents"); + if (!docs.empty()) { + Json arr = Json::array(); + for (const std::string& d : docs) arr.push(Json(d)); + req.set("documents", arr); + } + if (!req.has("userPrompt") && !req.has("documents")) bad("user_prompt or documents is required"); + op = az::Op::kSafetyTextOperationsShieldPrompt; + } else if (action == "protected") { + req.set("text", Json(text(b, "text", true))); + op = az::Op::kSafetyTextOperationsDetectTextProtectedMaterial; + } else { + bad("action must be text, image, shield, protected or blocklists"); + } + Plan pl; + pl.request = svc.prepare_body(op, {}, req); + pl.path = path_of(az::Surface::kSafety, op, {}); + pl.needs_confirm = true; + pl.call = [this, action, req] { + az::SafetyService s(c_); + if (action == "text") return s.analyze_text(req); + if (action == "image") return s.analyze_image(req); + if (action == "shield") return s.shield_prompt(req); + return s.detect_protected_material(req.find("text")->as_string()); + }; + return pl; + } + + Plan entra(const Json&) const { + return Plan{Json::object(), c_->settings().entra.complete() ? c_->settings().entra.token_url() : "", "POST", false, + [this] { + if (!c_->tokens()) + throw llm::NotConfiguredError("azure", "AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET"); + c_->tokens()->token(); // the token itself is never returned + std::int64_t now = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + Json o = Json::object(); + o.set("ok", Json(true)); + o.set("expires_in_seconds", Json(static_cast(c_->tokens()->expires_at() - now))); + o.set("exchanges", Json(static_cast(c_->tokens()->exchanges()))); + o.set("scope", Json(c_->settings().entra.scope)); + return o; + }}; + } + + Plan realtime(const Json& b) const { + std::string action = text(b, "action", true); + std::string dep = text(b, "deployment"); + if (action == "url") { + if (dep.empty()) bad("deployment is required"); + az::Check::deployment(dep); + return Plan{Json::object(), "", "GET", false, [this, dep] { + if (c_->settings().endpoint.empty()) + throw llm::NotConfiguredError("azure", az::Settings::kEndpointEnv); + Json o = Json::object(); + o.set("url", Json(az::RealtimeUrl::build(c_->settings(), dep))); + o.set("auth_header", Json(c_->auth() == az::AuthKind::kEntra ? "Authorization: Bearer " + : "api-key: ")); + o.set("note", Json("the credential travels in a header, never in the URL")); + return o; + }}; + } + if (action == "secret") { + Json req = Json::object(); + Json session = Json::object(); + session.set("type", Json("realtime")); + if (!dep.empty()) session.set("model", Json(dep)); + req.set("session", session); + if (auto s = number(b, "seconds")) { + Json ea = Json::object(); + ea.set("anchor", Json("created_at")); + ea.set("seconds", Json(*s)); + req.set("expires_after", ea); + } + az::RealtimeService svc(c_); + Plan pl; + pl.request = svc.prepare_body(az::Op::kCreateRealtimeClientSecret, {}, req); + pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateRealtimeClientSecret, {}); + pl.needs_confirm = true; + pl.call = [this, req] { + az::RealtimeService s(c_); + Json doc = s.create_client_secret(req); + // The secret is handed to the caller exactly once; report that it exists, not its value. + Json o = Json::object(); + o.set("created", Json(true)); + o.set("expires_at", doc.find("expires_at") ? *doc.find("expires_at") : Json(0)); + o.set("secret_chars", Json(doc.find("value") && doc.find("value")->is_string() + ? static_cast(doc.find("value")->as_string().size()) + : 0)); + return o; + }; + return pl; + } + bad("action must be url or secret"); + } + + // Any table operation: assistants, threads, batch, fine-tuning, files (JSON), + // vector stores, containers, conversations, evals, models, ... + Plan call(const Json& b) const { + std::string name = text(b, "operation", true); + az::Target t; + t.deployment = text(b, "deployment"); + t.ids = strings(b, "ids"); + std::map query; + if (const Json* q = b.find("query"); q && q->is_object()) + for (const auto& [k, v] : q->as_object()) { + if (!v.is_string()) bad("query values must be strings"); + query[k] = v.as_string(); + } + Json body; + const Json* bp = nullptr; + if (const Json* x = b.find("body"); x && !x->is_null()) { + if (!x->is_object()) bad("body must be a JSON object"); + body = *x; + bp = &body; + } + auto ops = std::make_shared(c_); + az::OperationsService::Plan p = ops->plan(name, t, bp, query); + Plan pl; + pl.request = p.has_body ? p.body : Json::object(); + pl.path = p.path; + pl.method = p.method; + pl.needs_confirm = p.method != "GET"; + pl.call = [ops, name, t, body, bp, query] { return ops->invoke(name, t, bp ? &body : nullptr, query); }; + return pl; + } + + private: + std::string path_of(az::Surface s, az::Op op, const az::Target& t) const { + az::AzureService g(c_, s); + return g.request_path(op, t); + } + + [[noreturn]] static void map(const llm::ApiError& e) { + if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED") + throw fw::HttpError(503, "not_configured", e.message()); + if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400 && e.request_id().empty()) + throw fw::HttpError(422, "validation_error", e.message()); + throw fw::HttpError(502, "azure_error", e.type() + ": " + e.message()); + } + + std::shared_ptr c_; + std::string config_error_; +}; + +} // namespace + void register_ai_azure_demo(fw::Router& router, Services& services) { - (void)router; - (void)services; + std::shared_ptr client; + std::string config_error; + try { + client = az::AzureClient::from_env(); + } catch (const llm::ApiError& e) { + // A malformed AZURE_* value must not stop the app; the status route says so. + config_error = e.message(); + client = az::AzureClient::create(az::Settings{}, [](az::Channel) -> std::unique_ptr { + return std::make_unique(); + }); + } + auto routes = std::make_shared(client, config_error); + fw::Environment* env = services.templates.get(); + services.templates->add_template("ai_azure.html", kPage); + services.add_feature("ai-azure", "/ai/azure"); + + router.get("/ai/azure", "ai-azure-page", [env](fw::Request req) { + (void)req; + fw::TemplateCtx ctx; + ctx["title"] = std::string("Azure OpenAI and AI Foundry"); + return fw::Response(200, env->render("ai_azure.html", ctx), "text/html; charset=utf-8"); + }); + + router.get("/ai/azure/status", "ai-azure-status", [routes](fw::Request req) { + (void)req; + const az::Settings& s = routes->client().settings(); + Json o = Json::object(); + o.set("configured", Json(routes->client().configured())); + o.set("auth", Json(auth_name(routes->client().auth()))); + o.set("api_version", Json(s.api_version.value())); + o.set("endpoint", Json(s.endpoint)); + o.set("config_error", Json(routes->config_error())); + Json env_names = Json::array(); + for (const char* n : {az::Settings::kEndpointEnv, az::Settings::kKeyEnv, az::Settings::kVersionEnv, + az::Settings::kTenantEnv, az::Settings::kClientIdEnv, az::Settings::kClientSecretEnv}) + env_names.push(Json(n)); + o.set("credential_env", env_names); + Json surfaces = Json::object(); + surfaces.set("openai", Json(routes->client().configured(az::Surface::kOpenAi))); + surfaces.set("foundry", Json(routes->client().configured(az::Surface::kFoundry))); + surfaces.set("safety", Json(routes->client().configured(az::Surface::kSafety))); + o.set("surfaces", surfaces); + o.set("calls", Json(static_cast(az::Calls::count()))); + o.set("dated_fallback_version", Json(az::ApiVersion::kDatedFallback)); + o.set("foundry_version", Json(s.foundry_version)); + o.set("safety_version", Json(s.safety_version)); + return json_out(o); + }); + + router.get("/ai/azure/calls", "ai-azure-calls", [](fw::Request req) { + (void)req; + Json arr = Json::array(); + for (std::size_t i = 0; i < az::Calls::count(); ++i) { + const az::CallSpec& c = az::Calls::at(i); + Json row = Json::object(); + row.set("name", Json(c.name)); + row.set("surface", Json(c.surface == az::Surface::kOpenAi ? "openai" + : c.surface == az::Surface::kFoundry ? "foundry" : "safety")); + row.set("http", Json(c.method)); + row.set("v1_path", Json(c.v1.path)); + row.set("dated_path", Json(c.dated.path)); + row.set("deployment", Json(c.deployment)); + row.set("destructive", Json(az::OperationsService::destructive(c))); + row.set("generic", Json(az::OperationsService::generic(c))); + arr.push(row); + } + Json o = Json::object(); + o.set("calls", arr); + return json_out(o); + }); + + using Member = AzureRoutes::Planner; + auto add = [&](const char* path, const char* name, Member planner) { + router.post(path, name, [routes, planner](fw::Request req) { return routes->run(req, planner); }); + }; + AzureRoutes* r = routes.get(); + auto bind = [r](Plan (AzureRoutes::*m)(const Json&) const) { + return Member([r, m](const Json& b) { return (r->*m)(b); }); + }; + add("/ai/azure/chat", "ai-azure-chat", bind(&AzureRoutes::chat)); + add("/ai/azure/embeddings", "ai-azure-embeddings", bind(&AzureRoutes::embeddings)); + add("/ai/azure/images", "ai-azure-images", bind(&AzureRoutes::images)); + add("/ai/azure/speech", "ai-azure-speech", bind(&AzureRoutes::speech)); + add("/ai/azure/models", "ai-azure-models", bind(&AzureRoutes::models)); + add("/ai/azure/foundry", "ai-azure-foundry", bind(&AzureRoutes::foundry)); + add("/ai/azure/safety", "ai-azure-safety", bind(&AzureRoutes::safety)); + add("/ai/azure/entra", "ai-azure-entra", bind(&AzureRoutes::entra)); + add("/ai/azure/realtime", "ai-azure-realtime", bind(&AzureRoutes::realtime)); + add("/ai/azure/call", "ai-azure-call", bind(&AzureRoutes::call)); } } // namespace molo::example::app diff --git a/example/assets/ai_azure.js b/example/assets/ai_azure.js new file mode 100644 index 0000000..acbe804 --- /dev/null +++ b/example/assets/ai_azure.js @@ -0,0 +1,72 @@ +// Azure page: thin client over /ai/azure/* (all limits server side). +(function () { + "use strict"; + var $ = function (id) { return document.getElementById(id); }; + var out = $("az-out"), err = $("az-error"); + var templates = { + "chat": { deployment: "gpt-4o", prompt: "Hello!", system: "Be brief.", max_tokens: 128, dialect: "chat" }, + "embeddings": { deployment: "text-embedding-3-small", input: ["hello world"], dimensions: 256 }, + "images": { deployment: "gpt-image-1", prompt: "A lighthouse at dawn", size: "1024x1024", n: 1 }, + "speech": { deployment: "tts", input: "Hello from Azure.", voice: "alloy" }, + "models": {}, + "foundry": { action: "chat", model: "Phi-4", prompt: "Hello!", max_tokens: 64, extra_parameters: "drop" }, + "safety": { action: "text", text: "I want to hurt myself.", categories: ["SelfHarm"] }, + "entra": {}, + "realtime": { action: "url", deployment: "gpt-realtime" }, + "call": { operation: "ListVectorStores", deployment: "", ids: [], query: {}, body: null } + }; + var sel = $("az-route"), opSel = $("az-op"), opLabel = $("az-op-label"); + Object.keys(templates).forEach(function (k) { + var o = document.createElement("option"); + o.value = k; o.textContent = k; + sel.appendChild(o); + }); + function fill() { + var t = templates[sel.value]; + if (sel.value === "call") t.operation = opSel.value || t.operation; + $("az-body").value = JSON.stringify(t, null, 1); + opLabel.style.display = sel.value === "call" ? "block" : "none"; + } + sel.onchange = fill; + opSel.onchange = function () { templates.call.operation = opSel.value; fill(); }; + fill(); + + fetch("/ai/azure/calls").then(function (r) { return r.json(); }).then(function (d) { + d.calls.filter(function (c) { return c.generic; }).forEach(function (c) { + var o = document.createElement("option"); + o.value = c.name; + o.textContent = c.name + " (" + c.http + ")"; + opSel.appendChild(o); + }); + opSel.value = templates.call.operation; + }); + + function send(confirm) { + var body; + try { body = JSON.parse($("az-body").value || "{}"); } + catch (e) { err.textContent = "Body is not valid JSON"; return; } + if (confirm) body.confirm = true; + err.textContent = ""; + out.textContent = "..."; + fetch("/ai/azure/" + sel.value, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body) + }).then(function (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); } + }); + }).catch(function (e) { err.textContent = String(e); out.textContent = ""; }); + } + $("az-send").onclick = function () { send(false); }; + $("az-confirm").onclick = function () { send(true); }; + + fetch("/ai/azure/status").then(function (r) { return r.json(); }).then(function (s) { + $("az-config").textContent = s.configured + ? "Credential found (" + s.auth + ", api version " + s.api_version + "). Calls are real; paid ones need confirm." + : "No endpoint or credential (AZURE_OPENAI_ENDPOINT plus AZURE_OPENAI_API_KEY or AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET): calls answer 503 until configured; dry runs still work."; + }); +})(); diff --git a/src/vendors/azure/SPEC.md b/src/vendors/azure/SPEC.md new file mode 100644 index 0000000..d0c9b44 --- /dev/null +++ b/src/vendors/azure/SPEC.md @@ -0,0 +1,274 @@ +# Azure OpenAI and AI Foundry - Specification + +Owned directory: `src/vendors/azure/`. Namespace `molo::azure`. Depends on +`molo::llm` (`ApiClient`, `Service`, `SseDecoder`, `ChatModel`, `Config`), +`molo::clients`, the WebSocket client of `src/google/live` (realtime) and the +OpenAI neutral wire mapping `openai::core::ChatWire` / `ResponsesWire` (the +chat model only: Azure OpenAI speaks the same shapes). Plan: +`plans/18-breadth.md` section 2, method of `plans/17-llm-vendor-platforms.md`. +Demo: `example/app/ai/AzureDemo.cpp`, routes `/ai/azure`. + +## 1. Scope + +One resource, three surfaces, one credential: + +| Surface | Host (from the specs) | Prefix | Covers | +|---|---|---|---| +| OpenAI | `https://.openai.azure.com` | `/openai/v1` (v1 API) or `/openai` (dated API) | chat, completions, responses, embeddings, images (generate, edit, variation), audio (speech, transcription, translation), realtime, assistants / threads / messages / runs / steps, batch, fine-tuning (jobs, events, checkpoints, permissions, copy), files, uploads, vector stores, models, containers, conversations, evals, graders, videos | +| Foundry | `https://.services.ai.azure.com` | `/models` | Azure AI model inference for catalog models: chat completions, embeddings, image embeddings, model info | +| Safety | `https://.cognitiveservices.azure.com` | `/contentsafety` | Content Safety: analyze text / image, Prompt Shields, protected material, text blocklists | + +Not covered, and said so: the Azure management plane (ARM: creating +resources, deployments, keys, quotas), Azure AI Agents / Projects +(`ai/data-plane/AIAgents`, `AIProjects`), Voice Live, Speech / Translator / +Language services. Deployments are therefore not listable here: `GET /models` +lists models, a deployment name must be known to the caller. + +## 2. Addressing and api-version (the core rule) + +`AZURE_OPENAI_API_VERSION` selects one of three modes (`ApiVersion`): + +| Value | Mode | OpenAI-surface URL | Deployment goes | +|---|---|---|---| +| unset / `v1` | v1 | `/openai/v1/` (no api-version query) | body `model` | +| `preview` | v1 preview | `/openai/v1/?api-version=preview` | body `model` | +| `YYYY-MM-DD[-preview]` | dated | `/openai/?api-version=` | URL `/deployments/{d}/...` (8 operations), body `model` for the rest | + +The 8 deployment-scoped operations are exactly the SDK list +(openai-python `lib/azure.py` `_deployments_endpoints`): completions, chat +completions, embeddings, audio transcriptions / translations / speech, image +generations / edits. The caller always passes the deployment once; the gateway +(`AzureService`) places it. A body `model` that differs from the deployment is +rejected; on the dated API it is removed. + +An operation that exists on one API only is sent to that API with its +fallback version: dated-only operations (assistants, uploads, files import, +realtime sessions under `/realtimeapi`, fine-tuning job delete) under a v1 +config use `2025-04-01-preview`; v1-only operations (containers, +conversations, evals, graders, videos, realtime client secrets and calls, +checkpoint permissions, image variations) under a dated config use +`/openai/v1` with `api-version=preview`. Foundry uses `2024-05-01-preview`, +Safety `2024-09-01` (both overridable in `Settings`, independent of the OpenAI +version). + +## 3. Authentication + +| Mode | Chosen when | Header | +|---|---|---| +| API key | `AZURE_OPENAI_API_KEY` set (wins) | `api-key: ` (OpenAI, Foundry); `Ocp-Apim-Subscription-Key: ` (Safety, key `AZURE_CONTENT_SAFETY_KEY` or the same key) | +| Microsoft Entra | tenant, client id and secret set, no key | `Authorization: Bearer ` on every surface | + +Entra is its own class pair (`entra.h`): `EntraTokenSource` posts +`client_id`, `client_secret`, `scope`, `grant_type=client_credentials` (form +encoded) to `//oauth2/v2.0/token` (authority default +`https://login.microsoftonline.com`, `AZURE_AUTHORITY_HOST` for sovereign +clouds), scope `https://cognitiveservices.azure.com/.default` (the scope the +specs' OAuth2 schemes name), caches the token, refreshes it 300 s before +expiry, and after a failed exchange refuses (without network) for 5 s +(`TOKEN_BACKOFF`, a floor). `EntraTransport` decorates the transport so the +bearer reaches every request, including streams and the realtime handshake. +Secrets never appear in URLs, errors or the demo output (the demo's `entra` +route reports expiry only). + +## 4. Module map (one concern per file) + +| File | Concern | +|---|---| +| `caps.h` | Static ceilings | +| `api_version.*` | `ApiVersion`: v1 / preview / dated parsing | +| `settings.*` | `Settings`, `EntraConfig`: env, endpoint rules, auth kind | +| `calls.*`, `calls_table.inc` | The ONE call table (generated), `Calls::render/accepts_*` | +| `entra.*` | `EntraTokenSource`, `EntraTransport` | +| `client.*` | `AzureClient`: one `llm::ApiClient` per surface, shared credential | +| `service.*` | `AzureService` gateway: side choice, paths, field/query checks, JSON / multipart / raw / SSE, list walk | +| `check.*` | Validation helpers | +| `chat.*` | `ChatService`, `CompletionsService` | +| `responses.*` | `ResponsesService` | +| `embeddings.*` | `EmbeddingsService` | +| `images.*` | `ImagesService` | +| `audio.*` | `AudioService` | +| `realtime.*` | `RealtimeService` (REST), `RealtimeUrl`, `RealtimeSocket` (WebSocket) | +| `assistants.*` | `AssistantsService` | +| `batches.*`, `fine_tuning.*`, `files.*`, `vector_stores.*`, `models.*` | one service each | +| `foundry.*` | `FoundryService` (model inference) | +| `safety.*` | `SafetyService` (Content Safety) | +| `operations.*` | `OperationsService`: every table operation by name | +| `chat_model.*` | `AzureChatModel : llm::ChatModel` (dialects chat completions, responses, Foundry) | +| `upload_file.h` | `UploadFile` | +| `tools/specdoc.py`, `gen_calls.py`, `check_spec.py` | spec reader, table generator, checker | + +## 5. Static caps (see caps.h) + +| Knob | Value | +|---|---| +| Deployment name | 1..64 of `[A-Za-z0-9._-]` | +| Endpoint | 256 chars, scheme and host only | +| Page limit / page walk | 100 / 20 pages | +| Stream events | 200000 | +| Embedding inputs | 2048 | +| Audio file / image file / speech text / image prompt | 25 MiB / 50 MiB / 4096 / 32000 chars | +| Single file upload | 512 MiB (`llm::Caps`), upload part 64 MiB | +| Content Safety text / image / blocklist items / Shield documents | 10000 chars / 7.5 MiB / 100 / 5 | +| Entra token | refresh skew 300 s, failure floor 5 s, lifetime <= 24 h, answer <= 16 KiB | +| Realtime event / events per session / bytes / seconds | 1 MiB / 100000 / 64 MiB / 3600 | +| Client secret lifetime | 10..7200 s | + +## 6. Contracts + +- No endpoint, or neither key nor complete Entra credentials: every call + throws `NotConfiguredError` (503 `NOT_CONFIGURED`) before any network + traffic. A malformed `AZURE_*` value throws `ApiError::invalid` from + `Settings::from_env`. +- Every request field and query name must be listed by the spec of the API + side in use (`Calls::accepts_field/accepts_query`), else 400 before traffic. + Ids are single URL path segments (`Service::valid_id`). +- Destructive calls (delete, cancel, remove, hangup, reject, pause) are + explicit methods with no defaults; the demo asks for `confirm:true` for them + and for everything that costs money or changes a resource. +- Lists report truncation (`Listing::truncated`, `next_after`); a server that + says `has_more` without a usable cursor is reported truncated, not looped. +- Error bodies of the form `{"error":{"code","message"}}` keep the code as + `ApiError::type()`. + +## 7. Sources + +All machine readable, pinned to azure-rest-api-specs commit +`ba3eef39e5be55cd5ed1a2e5c6c9bb1a17c36b9a` (2026-10-23): + +| Short name | Document | +|---|---| +| `v1`, `v1p` | `specification/ai/data-plane/OpenAI.v1/azure-v1-v1-generated.json`, `azure-v1-preview-generated.json` (OpenAPI 3, server `{endpoint}/openai/v1`) | +| `inf25` | `cognitiveservices/data-plane/OpenAIInference/preview/2025-04-01-preview/inference.json` | +| `auth24` | `cognitiveservices/data-plane/OpenAIAuthoring/stable/2024-10-21/azureopenai.json` (files, batches, fine-tuning, models, uploads) | +| `cs24` | `cognitiveservices/data-plane/ContentSafety/stable/2024-09-01/contentsafety.json` | +| `mi` | `ai/data-plane/ModelInference/preview/2024-05-01-preview/openapi.json` | + +Cross-checks: openai-python `src/openai/lib/azure.py` (deployment endpoint +list, `api-key` / `Authorization: Bearer` headers, `/openai` and +`/openai/deployments/...` base URLs, `AZURE_OPENAI_ENDPOINT` / +`AZURE_OPENAI_API_KEY`; the SDK reads the version from `OPENAI_API_VERSION`, +this package from `AZURE_OPENAI_API_VERSION` as the plan names it); the specs' +own security schemes (header names, OAuth scope) and `info.version`; the +Microsoft identity platform client-credentials documentation (token request +and response bodies). +The older dated revisions (2022-12-01 ... 2025-03-01-preview, 2024-02-15 +etc.) are not read: the table covers the newest stable authoring revision and +the newest preview inference revision; a field only older versions accept is +rejected by the gateway. + +## 8. Verified against spec + +`python3 -I src/vendors/azure/tools/check_spec.py` (2026-10-25): +`spec ok: 147 rows, 209 table sides verified, 315 spec operations seen in 6 documents`. +Per side it checks method, path, operationId, request content kind, the full +top-level request property list and the query list; it fails on any spec +operation of a covered document missing from the table, duplicate names, the +deployment-scoped set against the SDK list, the api-version constants against +each document's `info.version`, the v1 `api-version` enum (`v1`, `preview`), +the key header names and the Entra scope. The table is generated by +`tools/gen_calls.py` from the same documents (`calls_table.inc` is never +hand edited). + +## 9. Verified live + +Unauthenticated probes, 2026-10-25 (`MOLO_LIVE_NET=1 make ai-test`, suite +`azure_live`, passing): + +- `POST https://login.microsoftonline.com//oauth2/v2.0/token` with the + documented form and a client id that does not exist: HTTP 400, + `{"error":"unauthorized_client","error_description":"AADSTS700038: ...","error_codes":[700038],"trace_id":...}`. + `EntraTokenSource` maps it to `ApiError(400, "unauthorized_client")` with the + trace id as request id. The same body is the parse fixture in `test_entra.cpp`. +- `GET https://eastus.api.cognitive.microsoft.com/openai/v1/models` with a bad + `api-key`: HTTP 401 `{"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint..."}}`. + `POST .../contentsafety/text:analyze?api-version=2024-09-01` with a bad + `Ocp-Apim-Subscription-Key`: the same 401 body. So the v1 path, the + Content Safety path and both key headers are the right shape on the real host. +- `.openai.azure.com`, `.services.ai.azure.com`, `.cognitiveservices.azure.com` + do not resolve for an unknown resource name (NXDOMAIN); a resource host + cannot be probed without a resource. + +Opt-in authenticated live tests (`azure_live.models_list_and_one_tiny_completion`): +need `MOLO_LIVE_NET=1`, `AZURE_OPENAI_ENDPOINT` and a key or Entra triple; +models list is read-only; one smallest completion additionally needs +`MOLO_LIVE_PAID=1` and `AZURE_OPENAI_DEPLOYMENT`. Loud SKIP otherwise. +Nothing is created, updated or deleted. + +## 10. Unverified + +No Azure resource or key exists in this environment: no success response body +from any Azure host was seen. Everything below is read from specs, the SDK or +documentation but never confirmed against a live resource. + +- Success shapes: chat completion, embeddings, models, responses and stream + parsing use the documented OpenAI-shaped examples (labelled in the tests). +- v1 `api-version`: omitted for `v1` per the Microsoft Learn v1 guide (the spec + says optional, default `v1`); whether a real resource accepts both is unseen. +- Multipart vs JSON: the v1 spec lists `application/json` for audio + transcriptions / translations and image edits / variations (generator + artefact: a `file` property typed `object`), and the dated 2025-04-01-preview + spec lists JSON for image edits. This package sends multipart (the dated + spec says multipart for transcription; every official SDK sends multipart + for these). Field names are checked against the spec lists. +- Assistants: the v1 documents contain threads, messages and runs but no + `/assistants` path; assistants are sent to the dated API (fallback version), + threads to the configured API. Microsoft has announced the retirement of the + Assistants API; whether a given resource still serves it is unseen. +- Realtime WebSocket URLs (`/openai/v1/realtime?model=`, dated + `/openai/realtime?api-version=&deployment=`) and the `api-key` / + Bearer handshake headers come from the SDK shape and Microsoft Learn, not + from the OpenAPI documents (WebSocket is not in them); the session over a + loopback server proves the client, not the service. The realtime event + vocabulary is not catalogued (events are JSON objects with a `type`). +- Foundry: `https://.services.ai.azure.com/models` host and the + bearer scope `https://cognitiveservices.azure.com/.default` for it (the spec + names this scope; the Learn page for Foundry models also mentions + `https://ai.azure.com/.default` for some flows). `AZURE_AI_ENDPOINT` and + `AZURE_CONTENT_SAFETY_ENDPOINT` default to the OpenAI endpoint, which only + works on a multi-service Azure AI Services resource that serves all three + hosts under one name. How `model` picks a deployment on a multi-model + endpoint is not described by the spec. +- Content Safety: Entra scope (spec), `application/merge-patch+json` for the + blocklist PATCH (spec `consumes`), response shapes of analyze / Shield. +- Entra: the `expires_in` number form (documented); sovereign authority hosts + (`login.microsoftonline.us`, `.cn`) via `AZURE_AUTHORITY_HOST` (documented + hosts, never called). Managed identity, certificates and workload identity + federation are not implemented. +- Rate limits, `Retry-After` and quota errors (429) are passed through as + `ApiError` without any retry (the `llm` foundation drops `Retry-After`). + +## 11. Gaps + +- No ARM (management plane): cannot list or create deployments, keys, quotas + (needs `management.azure.com` and a different token scope). +- No AAD token for Foundry-only resources via `https://ai.azure.com/.default`. +- Realtime has no event catalogue and no audio helpers (owner: this package, + `realtime.h`); the `openai/platform` catalogue could be reused once Azure + event differences are known. +- `llm::ApiClient` has no per-call timeout, no GET stream (response replay), + and no unauthenticated request (`src/llm/SPEC.md` section 4); the Entra + exchange therefore uses its own `clients::Client`. +- `clients::Url` percent-encodes `[` and `]` in query names (`include[]` + goes out as `include%5B%5D`); servers decode it, not proven on Azure. +- Batch input blobs (`input_blob`, `output_folder` of the v1 batch API) are + passed through as JSON fields only. +- `ListParams::before` exists, but the spec lists `before` for some lists only; + the gateway rejects it where the spec does not. +- Newer dated revisions than 2025-04-01-preview (if Microsoft ships them) need + `gen_calls.py` re-run; `check_spec.py` fails on drift of the pinned documents. + +## 12. Credentials + +| Variable | Meaning | +|---|---| +| `AZURE_OPENAI_ENDPOINT` | `https://.openai.azure.com` (scheme and host only) | +| `AZURE_OPENAI_API_KEY` | resource key (Azure portal, Keys and Endpoint) | +| `AZURE_OPENAI_API_VERSION` | `v1` (default), `preview`, or a dated version | +| `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET` | Entra app registration with the "Cognitive Services OpenAI User" (or Contributor) role on the resource | +| `AZURE_AI_ENDPOINT` | optional, Foundry model inference host | +| `AZURE_CONTENT_SAFETY_ENDPOINT`, `AZURE_CONTENT_SAFETY_KEY` | optional, Content Safety host and key | +| `AZURE_AUTHORITY_HOST` | optional, default `https://login.microsoftonline.com` | +| `AZURE_OPENAI_DEPLOYMENT` | live tests only: the deployment for the one paid completion | + +Console: https://portal.azure.com (resource, Keys and Endpoint; Entra: App +registrations). Not read: the portal needs a sign-in. diff --git a/src/vendors/azure/api_version.cpp b/src/vendors/azure/api_version.cpp new file mode 100644 index 0000000..c8583a4 --- /dev/null +++ b/src/vendors/azure/api_version.cpp @@ -0,0 +1,32 @@ +#include "vendors/azure/api_version.h" + +#include "llm/error.h" + +namespace molo::azure { + +bool ApiVersion::is_date(const std::string& t) { + auto digit = [&](std::size_t i) { return i < t.size() && t[i] >= '0' && t[i] <= '9'; }; + if (t.size() < 10) return false; + for (std::size_t i : {0u, 1u, 2u, 3u, 5u, 6u, 8u, 9u}) + if (!digit(i)) return false; + if (t[4] != '-' || t[7] != '-') return false; + if (t.size() == 10) return true; + return t.substr(10) == "-preview"; +} + +ApiVersion ApiVersion::parse(const std::string& text) { + ApiVersion v; + if (text.empty() || text == "v1") return v; + if (text == "preview") { + v.mode_ = Mode::kPreview; + v.value_ = "preview"; + return v; + } + if (!is_date(text)) + throw llm::ApiError::invalid("azure", "api version must be v1, preview or YYYY-MM-DD[-preview]"); + v.mode_ = Mode::kDated; + v.value_ = text; + return v; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/api_version.h b/src/vendors/azure/api_version.h new file mode 100644 index 0000000..6e19c9c --- /dev/null +++ b/src/vendors/azure/api_version.h @@ -0,0 +1,39 @@ +#pragma once + +// ApiVersion: the three ways an Azure OpenAI resource is addressed. +// v1 /openai/v1/... no api-version query (the GA v1 API) +// preview /openai/v1/... ?api-version=preview (v1 preview features) +// dated /openai/... ?api-version=YYYY-MM-DD[-preview], deployment URLs +// Env AZURE_OPENAI_API_VERSION selects one; unset means v1. + +#include + +namespace molo::azure { + +class ApiVersion { + public: + enum class Mode { kV1, kPreview, kDated }; + + static constexpr const char* kDatedFallback = "2025-04-01-preview"; // inference doc revision + static constexpr const char* kFoundryDefault = "2024-05-01-preview"; + static constexpr const char* kSafetyDefault = "2024-09-01"; + + ApiVersion() = default; + // "", "v1", "preview", or a date; anything else throws ApiError::invalid. + static ApiVersion parse(const std::string& text); + // True for YYYY-MM-DD and YYYY-MM-DD-preview. + static bool is_date(const std::string& text); + + Mode mode() const { return mode_; } + const std::string& value() const { return value_; } + bool is_dated() const { return mode_ == Mode::kDated; } + bool is_v1_family() const { return mode_ != Mode::kDated; } + // Query value to send on the v1 path: "" for v1 (omitted), "preview". + std::string v1_query() const { return mode_ == Mode::kPreview ? "preview" : ""; } + + private: + Mode mode_ = Mode::kV1; + std::string value_ = "v1"; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/assistants.cpp b/src/vendors/azure/assistants.cpp new file mode 100644 index 0000000..8af74a7 --- /dev/null +++ b/src/vendors/azure/assistants.cpp @@ -0,0 +1,146 @@ +#include "vendors/azure/assistants.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +namespace { +Query page(const ListParams& p) { + p.validate(); + Query q; + p.apply(q); + return q; +} +} // namespace + +Json AssistantsService::create_assistant(const Json& body) { + Check::has_string(body, "model"); + Check::deployment(body.find("model")->as_string()); + return call(Op::kCreateAssistant, {}, &body); +} +Json AssistantsService::get_assistant(const std::string& id) { + Check::id("assistant", id); + return call(Op::kGetAssistant, {"", {id}}); +} +Json AssistantsService::modify_assistant(const std::string& id, const Json& body) { + Check::id("assistant", id); + return call(Op::kModifyAssistant, {"", {id}}, &body); +} +Json AssistantsService::list_assistants(const ListParams& params) { + Query q = page(params); + return call(Op::kListAssistants, {}, nullptr, &q); +} +Json AssistantsService::remove_assistant(const std::string& id) { + Check::id("assistant", id); + return call(Op::kDeleteAssistant, {"", {id}}); +} + +Json AssistantsService::create_thread(const Json& body) { return call(Op::kCreateThread, {}, &body); } +Json AssistantsService::get_thread(const std::string& id) { + Check::id("thread", id); + return call(Op::kRetrieveThread, {"", {id}}); +} +Json AssistantsService::modify_thread(const std::string& id, const Json& body) { + Check::id("thread", id); + return call(Op::kModifyThread, {"", {id}}, &body); +} +Json AssistantsService::remove_thread(const std::string& id) { + Check::id("thread", id); + return call(Op::kDeleteThread, {"", {id}}); +} +Json AssistantsService::create_thread_and_run(const Json& body) { + Check::has_string(body, "assistant_id"); + return call(Op::kCreateThreadAndRun, {}, &body); +} + +Json AssistantsService::create_message(const std::string& thread, const Json& body) { + Check::id("thread", thread); + Check::has_string(body, "role"); + Check::has_field(body, "content"); + return call(Op::kCreateMessage, {"", {thread}}, &body); +} +Json AssistantsService::get_message(const std::string& thread, const std::string& message) { + Check::id("thread", thread); + Check::id("message", message); + return call(Op::kRetrieveMessage, {"", {thread, message}}); +} +Json AssistantsService::modify_message(const std::string& thread, const std::string& message, + const Json& body) { + Check::id("thread", thread); + Check::id("message", message); + return call(Op::kModifyMessage, {"", {thread, message}}, &body); +} +Json AssistantsService::list_messages(const std::string& thread, const ListParams& params) { + Check::id("thread", thread); + Query q = page(params); + return call(Op::kListMessages, {"", {thread}}, nullptr, &q); +} +Listing AssistantsService::list_messages_all(const std::string& thread, const ListParams& params, + std::size_t max_pages) { + Check::id("thread", thread); + return walk(Op::kListMessages, {"", {thread}}, params, Query(), max_pages); +} +Json AssistantsService::remove_message(const std::string& thread, const std::string& message) { + Check::id("thread", thread); + Check::id("message", message); + return call(Op::kDeleteMessage, {"", {thread, message}}); +} + +Json AssistantsService::create_run(const std::string& thread, const Json& body) { + Check::id("thread", thread); + Check::has_string(body, "assistant_id"); + return call(Op::kCreateRun, {"", {thread}}, &body); +} +void AssistantsService::stream_run(const std::string& thread, const Json& body, const EventHandler& handler) { + Check::id("thread", thread); + Check::has_string(body, "assistant_id"); + call_stream(Op::kCreateRun, {"", {thread}}, body, handler); +} +Json AssistantsService::get_run(const std::string& thread, const std::string& run) { + Check::id("thread", thread); + Check::id("run", run); + return call(Op::kRetrieveRun, {"", {thread, run}}); +} +Json AssistantsService::modify_run(const std::string& thread, const std::string& run, const Json& body) { + Check::id("thread", thread); + Check::id("run", run); + return call(Op::kModifyRun, {"", {thread, run}}, &body); +} +Json AssistantsService::list_runs(const std::string& thread, const ListParams& params) { + Check::id("thread", thread); + Query q = page(params); + return call(Op::kListRuns, {"", {thread}}, nullptr, &q); +} +Json AssistantsService::cancel_run(const std::string& thread, const std::string& run) { + Check::id("thread", thread); + Check::id("run", run); + return call(Op::kCancelRun, {"", {thread, run}}); +} +Json AssistantsService::submit_tool_outputs(const std::string& thread, const std::string& run, + const Json& tool_outputs) { + Check::id("thread", thread); + Check::id("run", run); + Check::require(tool_outputs.is_array() && !tool_outputs.as_array().empty(), + "tool_outputs must be a non-empty array"); + Json body = Json::object(); + body.set("tool_outputs", tool_outputs); + return call(Op::kSubmitToolOutputsToRun, {"", {thread, run}}, &body); +} +Json AssistantsService::list_run_steps(const std::string& thread, const std::string& run, + const ListParams& params) { + Check::id("thread", thread); + Check::id("run", run); + Query q = page(params); + return call(Op::kListRunSteps, {"", {thread, run}}, nullptr, &q); +} +Json AssistantsService::get_run_step(const std::string& thread, const std::string& run, + const std::string& step) { + Check::id("thread", thread); + Check::id("run", run); + Check::id("step", step); + return call(Op::kGetRunStep, {"", {thread, run, step}}); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/assistants.h b/src/vendors/azure/assistants.h new file mode 100644 index 0000000..7984f96 --- /dev/null +++ b/src/vendors/azure/assistants.h @@ -0,0 +1,63 @@ +#pragma once + +// Assistants, threads, messages, runs, run steps (OpenAI Assistants API on +// Azure). The v1 documents have threads / runs but no /assistants path; when +// an operation exists on one API only, the gateway uses that API with its +// fallback api-version (dated: 2025-04-01-preview, v1: preview). Microsoft +// announced the retirement of this API in favour of Responses / Agents. + +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class AssistantsService : public AzureService { + public: + using EventHandler = std::function; // false: stop + + explicit AssistantsService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // Assistants. create needs `model` (the deployment name). + clients::body::Json create_assistant(const clients::body::Json& body); + clients::body::Json get_assistant(const std::string& id); + clients::body::Json modify_assistant(const std::string& id, const clients::body::Json& body); + clients::body::Json list_assistants(const ListParams& params = {}); + clients::body::Json remove_assistant(const std::string& id); // destructive + + // Threads. + clients::body::Json create_thread(const clients::body::Json& body = clients::body::Json::object()); + clients::body::Json get_thread(const std::string& id); + clients::body::Json modify_thread(const std::string& id, const clients::body::Json& body); + clients::body::Json remove_thread(const std::string& id); // destructive + // Thread and run in one call; body needs assistant_id. + clients::body::Json create_thread_and_run(const clients::body::Json& body); + + // Messages. + clients::body::Json create_message(const std::string& thread, const clients::body::Json& body); + clients::body::Json get_message(const std::string& thread, const std::string& message); + clients::body::Json modify_message(const std::string& thread, const std::string& message, + const clients::body::Json& body); + clients::body::Json list_messages(const std::string& thread, const ListParams& params = {}); + Listing list_messages_all(const std::string& thread, const ListParams& params = {}, + std::size_t max_pages = 5); + clients::body::Json remove_message(const std::string& thread, const std::string& message); // destructive + + // Runs. body needs assistant_id. + clients::body::Json create_run(const std::string& thread, const clients::body::Json& body); + void stream_run(const std::string& thread, const clients::body::Json& body, const EventHandler& handler); + clients::body::Json get_run(const std::string& thread, const std::string& run); + clients::body::Json modify_run(const std::string& thread, const std::string& run, + const clients::body::Json& body); + clients::body::Json list_runs(const std::string& thread, const ListParams& params = {}); + clients::body::Json cancel_run(const std::string& thread, const std::string& run); + clients::body::Json submit_tool_outputs(const std::string& thread, const std::string& run, + const clients::body::Json& tool_outputs); + clients::body::Json list_run_steps(const std::string& thread, const std::string& run, + const ListParams& params = {}); + clients::body::Json get_run_step(const std::string& thread, const std::string& run, + const std::string& step); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/audio.cpp b/src/vendors/azure/audio.cpp new file mode 100644 index 0000000..c9c8316 --- /dev/null +++ b/src/vendors/azure/audio.cpp @@ -0,0 +1,49 @@ +#include "vendors/azure/audio.h" + +#include "vendors/azure/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +llm::RawResponse AudioService::speech(const std::string& deployment, const Json& body) { + Check::deployment(deployment); + Check::object("body", body); + Check::has_string(body, "input"); + Check::has_string(body, "voice"); + Check::max_chars("input", body.find("input")->as_string(), AzureCaps::kMaxSpeechChars); + return call_json_raw(Op::kCreateSpeech, {deployment, {}}, body); +} + +llm::RawResponse AudioService::upload(Op op, const std::string& deployment, const UploadFile& file, + const std::map& fields) { + Check::deployment(deployment); + Check::non_empty("audio filename", file.filename); + Check::no_control("filename", file.filename); + Check::require(!file.data.empty(), "audio file is empty"); + Check::require(file.data.size() <= AzureCaps::kMaxAudioFileBytes, "audio file over 25 MiB"); + clients::body::MultipartBody form; + form.add_file("file", file.filename, file.content_type, file.data); + for (const auto& [k, v] : fields) form.add_text(k, v); + return call_multipart_raw(op, {deployment, {}}, form); +} + +llm::RawResponse AudioService::transcribe(const std::string& deployment, const UploadFile& file, + const std::map& fields) { + return upload(Op::kCreateTranscription, deployment, file, fields); +} + +llm::RawResponse AudioService::translate(const std::string& deployment, const UploadFile& file, + const std::map& fields) { + return upload(Op::kCreateTranslation, deployment, file, fields); +} + +Json AudioService::as_json(const llm::RawResponse& answer) { + if (answer.content_type.find("json") != std::string::npos) return Json::parse(answer.body); + Json o = Json::object(); + o.set("text", Json(answer.body)); + return o; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/audio.h b/src/vendors/azure/audio.h new file mode 100644 index 0000000..e1dcfcd --- /dev/null +++ b/src/vendors/azure/audio.h @@ -0,0 +1,38 @@ +#pragma once + +// Audio on a deployment: speech (tts, tts-hd, gpt-4o-mini-tts), transcription +// (whisper, gpt-4o-transcribe) and translation. Transcription and translation +// are multipart on both APIs (the v1 document lists JSON for them, see SPEC +// Unverified). Answers may be text (srt, vtt, text), so they come back raw. + +#include +#include + +#include "vendors/azure/service.h" +#include "vendors/azure/upload_file.h" + +namespace molo::azure { + +class AudioService : public AzureService { + public: + explicit AudioService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body: input (<= 4096 chars) and voice (required), plus response_format, + // speed, instructions. The answer is audio bytes (content_type tells which). + llm::RawResponse speech(const std::string& deployment, const clients::body::Json& body); + // fields: language, prompt, response_format, temperature, ... (spec names). + // The file is capped at 25 MiB. + llm::RawResponse transcribe(const std::string& deployment, const UploadFile& file, + const std::map& fields = {}); + llm::RawResponse translate(const std::string& deployment, const UploadFile& file, + const std::map& fields = {}); + // The answer parsed as JSON when its content type says so, else {"text": body}. + static clients::body::Json as_json(const llm::RawResponse& answer); + + private: + llm::RawResponse upload(Op op, const std::string& deployment, const UploadFile& file, + const std::map& fields); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/batches.cpp b/src/vendors/azure/batches.cpp new file mode 100644 index 0000000..43b5c0d --- /dev/null +++ b/src/vendors/azure/batches.cpp @@ -0,0 +1,38 @@ +#include "vendors/azure/batches.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +Json BatchesService::create(const Json& body) { + Check::has_string(body, "endpoint"); + Check::has_string(body, "completion_window"); + Check::require(body.find("input_file_id") || body.find("input_blob"), + "input_file_id (or input_blob) is required"); + return call(Op::kCreateBatch, {}, &body); +} + +Json BatchesService::get(const std::string& id) { + Check::id("batch", id); + return call(Op::kRetrieveBatch, {"", {id}}); +} + +Json BatchesService::list(const ListParams& params) { + params.validate(); + Query q; + params.apply(q); + return call(Op::kListBatches, {}, nullptr, &q); +} + +Listing BatchesService::list_all(const ListParams& params, std::size_t max_pages) { + return walk(Op::kListBatches, {}, params, Query(), max_pages); +} + +Json BatchesService::cancel(const std::string& id) { + Check::id("batch", id); + return call(Op::kCancelBatch, {"", {id}}); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/batches.h b/src/vendors/azure/batches.h new file mode 100644 index 0000000..bd5e583 --- /dev/null +++ b/src/vendors/azure/batches.h @@ -0,0 +1,27 @@ +#pragma once + +// Batch (Global Batch): jobs over an uploaded JSONL file of requests. +// Creating a batch is paid work: the demo runs it as a dry run unless +// confirmed. + +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class BatchesService : public AzureService { + public: + explicit BatchesService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body: endpoint (/chat/completions, /embeddings, /v1/chat/completions ...), + // completion_window ("24h"), and input_file_id (or input_blob on the v1 API). + clients::body::Json create(const clients::body::Json& body); + clients::body::Json get(const std::string& id); + clients::body::Json list(const ListParams& params = {}); + Listing list_all(const ListParams& params = {}, std::size_t max_pages = 5); + clients::body::Json cancel(const std::string& id); // destructive +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/calls.cpp b/src/vendors/azure/calls.cpp new file mode 100644 index 0000000..d85769d --- /dev/null +++ b/src/vendors/azure/calls.cpp @@ -0,0 +1,114 @@ +#include "vendors/azure/calls.h" + +#include "llm/api_client.h" +#include "llm/error.h" + +namespace molo::azure { + +namespace { +const CallSpec kCalls[] = { +#define AZ_OP(name, surface, method, dep, d1, p1, o1, c1, b1, q1, d2, p2, o2, c2, b2, q2) \ + {Op::k##name, #name, surface, method, dep != 0, {d1, p1, o1, c1, b1, q1}, {d2, p2, o2, c2, b2, q2}}, +#include "vendors/azure/calls_table.inc" +#undef AZ_OP +}; +constexpr std::size_t kCallCount = sizeof(kCalls) / sizeof(kCalls[0]); + +bool listed(const std::string& csv, const std::string& item) { + std::size_t pos = 0; + while (pos <= csv.size()) { + std::size_t end = csv.find(',', pos); + if (end == std::string::npos) end = csv.size(); + if (csv.compare(pos, end - pos, item) == 0 && end - pos == item.size()) return true; + pos = end + 1; + } + return false; +} + +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("azure", why); } +} // namespace + +const CallSpec& Calls::spec(Op op) { return kCalls[static_cast(op)]; } +const CallSpec& Calls::at(std::size_t index) { return kCalls[index]; } +std::size_t Calls::count() { return kCallCount; } + +const CallSpec* Calls::find(const std::string& name) { + for (const CallSpec& c : kCalls) + if (name == c.name) return &c; + return nullptr; +} + +clients::Method Calls::method(Op op) { + std::string m = spec(op).method; + if (m == "GET") return clients::Method::kGet; + if (m == "POST") return clients::Method::kPost; + if (m == "DELETE") return clients::Method::kDelete; + if (m == "PATCH") return clients::Method::kPatch; + if (m == "PUT") return clients::Method::kPut; + bad("unknown method in table: " + m); +} + +bool Calls::accepts_field(Op op, Side side, const std::string& field) { + return listed(spec(op).side(side).body, field); +} + +bool Calls::accepts_query(Op op, Side side, const std::string& name) { + std::string n = name; + if (n.size() > 2 && n.compare(n.size() - 2, 2, "[]") == 0) { + // The v1 spec names array parameters include[]; the dated one include. + if (listed(spec(op).side(side).query, n)) return true; + n.resize(n.size() - 2); + } + return listed(spec(op).side(side).query, n) || listed(spec(op).side(side).query, n + "[]"); +} + +std::size_t Calls::id_count(Op op, Side side) { + std::string path = spec(op).side(side).path; + std::size_t n = 0; + for (std::size_t i = 0; i < path.size(); ++i) + if (path[i] == '{') ++n; + if (side == Side::kDated && spec(op).deployment && n > 0) --n; + return n; +} + +std::string Calls::render(Op op, Side side, const std::vector& ids, + const std::string& deployment) { + const CallSpec& c = spec(op); + const SideSpec& s = c.side(side); + if (!s.present()) bad(std::string(c.name) + " is not available on this API side"); + std::string path = s.path; + std::string out; + std::size_t used = 0; + bool first_placeholder = true; + for (std::size_t i = 0; i < path.size();) { + if (path[i] != '{') { + out += path[i++]; + continue; + } + std::size_t close = path.find('}', i); + if (close == std::string::npos) bad("malformed table path"); + if (side == Side::kDated && c.deployment && first_placeholder) { + if (!llm::Service::valid_id(deployment)) bad("bad deployment name"); + out += deployment; + } else { + if (used >= ids.size()) bad(std::string(c.name) + " needs more ids"); + if (!llm::Service::valid_id(ids[used])) bad("bad id"); + out += ids[used++]; + } + first_placeholder = false; + i = close + 1; + } + if (used != ids.size()) bad(std::string(c.name) + " got too many ids"); + return out; +} + +std::string Calls::prefix(Op op, Side side) { + switch (spec(op).surface) { + case Surface::kFoundry: return "/models"; + case Surface::kSafety: return "/contentsafety"; + case Surface::kOpenAi: break; + } + return side == Side::kV1 ? "/openai/v1" : "/openai"; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/calls.h b/src/vendors/azure/calls.h new file mode 100644 index 0000000..d8b2652 --- /dev/null +++ b/src/vendors/azure/calls.h @@ -0,0 +1,76 @@ +#pragma once + +// The ONE table of REST operations this package makes (calls_table.inc, +// generated by tools/gen_calls.py from the pinned azure-rest-api-specs and +// verified by tools/check_spec.py). Code builds every path from this table. +// +// An OpenAI-surface operation has up to two sides: the v1 API (/openai/v1, +// document "v1" / "v1p") and the dated API (/openai, documents "inf25" / +// "auth24", deployment URLs). Foundry model inference and Content Safety are +// single-sided (the dated slot). + +#include +#include +#include + +#include "clients/request.h" + +namespace molo::azure { + +enum class Surface { kOpenAi, kFoundry, kSafety }; +enum class Side { kV1, kDated }; + +enum class Op { +#define AZ_OP(name, ...) k##name, +#include "vendors/azure/calls_table.inc" +#undef AZ_OP +}; + +struct SideSpec { + const char* doc; // spec document short name, "" when the side is absent + const char* path; // spec path under the server base + const char* op_id; // spec operationId ("" when the spec has none) + const char* content; // json, multipart, json+multipart, none (as the spec lists it) + const char* body; // every top-level request property, comma separated + const char* query; // query parameters except api-version, comma separated + bool present() const { return path[0] != '\0'; } +}; + +struct CallSpec { + Op op; + const char* name; + Surface surface; + const char* method; + bool deployment; // dated path is /deployments/{deployment-id}/... + SideSpec v1; + SideSpec dated; + const SideSpec& side(Side s) const { return s == Side::kV1 ? v1 : dated; } +}; + +class Calls { + public: + static const CallSpec& spec(Op op); + static const CallSpec& at(std::size_t index); + static std::size_t count(); + // By table name ("CreateChatCompletion"); nullptr when unknown. + static const CallSpec* find(const std::string& name); + static clients::Method method(Op op); + + static bool has(Op op, Side side) { return spec(op).side(side).present(); } + // True when the spec of that side lists `field` as a request property. + static bool accepts_field(Op op, Side side, const std::string& field); + // True when the spec lists `name` as a query parameter ("include[]" matches "include"). + static bool accepts_query(Op op, Side side, const std::string& name); + // Number of {placeholders} the caller supplies as ids (the deployment of a + // dated deployment-scoped path is not one of them). + static std::size_t id_count(Op op, Side side); + // Spec path with ids substituted (each validated as one URL path segment), + // without the surface prefix and without the deployment segment. + // `deployment` is inserted for a dated deployment-scoped side. + static std::string render(Op op, Side side, const std::vector& ids, + const std::string& deployment = ""); + // Server base path of a side: "/openai/v1", "/openai", "/models", "/contentsafety". + static std::string prefix(Op op, Side side); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/calls_table.inc b/src/vendors/azure/calls_table.inc new file mode 100644 index 0000000..77aaa24 --- /dev/null +++ b/src/vendors/azure/calls_table.inc @@ -0,0 +1,152 @@ +// GENERATED by tools/gen_calls.py from azure-rest-api-specs ba3eef39e5be55cd5ed1a2e5c6c9bb1a17c36b9a. Do not edit. +// AZ_OP(name, surface, method, deployment_scoped, then twice (v1 side, dated side): +// doc, path, operation_id, content, body_fields, query_params) +// AZ-ROWS-BEGIN +AZ_OP(DeleteAssistant, Surface::kOpenAi, "DELETE", 0, "", "", "", "", "", "", "inf25", "/assistants/{assistant_id}", "Delete_Assistant", "none", "", "") +AZ_OP(DeleteContainer, Surface::kOpenAi, "DELETE", 0, "v1p", "/containers/{container_id}", "deleteContainer", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteContainerFile, Surface::kOpenAi, "DELETE", 0, "v1p", "/containers/{container_id}/files/{file_id}", "deleteContainerFile", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteConversation, Surface::kOpenAi, "DELETE", 0, "v1p", "/conversations/{conversation_id}", "deleteConversation", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteConversationItem, Surface::kOpenAi, "DELETE", 0, "v1p", "/conversations/{conversation_id}/items/{item_id}", "deleteConversationItem", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteEval, Surface::kOpenAi, "DELETE", 0, "v1p", "/evals/{eval_id}", "deleteEval", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteEvalRun, Surface::kOpenAi, "DELETE", 0, "v1p", "/evals/{eval_id}/runs/{run_id}", "deleteEvalRun", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteFile, Surface::kOpenAi, "DELETE", 0, "v1p", "/files/{file_id}", "deleteFile", "none", "", "", "auth24", "/files/{file-id}", "Files_Delete", "none", "", "") +AZ_OP(DeleteFineTuningCheckpointPermission, Surface::kOpenAi, "DELETE", 0, "v1p", "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions/{permission_id}", "deleteFineTuningCheckpointPermission", "none", "", "", "", "", "", "", "", "") +AZ_OP(FineTuningDelete, Surface::kOpenAi, "DELETE", 0, "", "", "", "", "", "", "auth24", "/fine_tuning/jobs/{fine-tuning-job-id}", "FineTuning_Delete", "none", "", "") +AZ_OP(DeleteModel, Surface::kOpenAi, "DELETE", 0, "v1p", "/models/{model}", "deleteModel", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteResponse, Surface::kOpenAi, "DELETE", 0, "v1p", "/responses/{response_id}", "deleteResponse", "none", "", "", "inf25", "/responses/{response_id}", "deleteResponse", "none", "", "") +AZ_OP(DeleteThread, Surface::kOpenAi, "DELETE", 0, "v1p", "/threads/{thread_id}", "deleteThread", "none", "", "", "inf25", "/threads/{thread_id}", "Delete_Thread", "none", "", "") +AZ_OP(DeleteMessage, Surface::kOpenAi, "DELETE", 0, "v1p", "/threads/{thread_id}/messages/{message_id}", "deleteMessage", "none", "", "", "", "", "", "", "", "") +AZ_OP(DeleteVectorStore, Surface::kOpenAi, "DELETE", 0, "v1p", "/vector_stores/{vector_store_id}", "deleteVectorStore", "none", "", "", "inf25", "/vector_stores/{vector_store_id}", "Delete_Vector_Store", "none", "", "") +AZ_OP(DeleteVectorStoreFile, Surface::kOpenAi, "DELETE", 0, "v1p", "/vector_stores/{vector_store_id}/files/{file_id}", "deleteVectorStoreFile", "none", "", "", "inf25", "/vector_stores/{vector_store_id}/files/{file_id}", "Delete_Vector_Store_File", "none", "", "") +AZ_OP(VideosDelete, Surface::kOpenAi, "DELETE", 0, "v1p", "/videos/{video-id}", "Videos_Delete", "none", "", "", "", "", "", "", "", "") +AZ_OP(ListAssistants, Surface::kOpenAi, "GET", 0, "", "", "", "", "", "", "inf25", "/assistants", "List_Assistants", "none", "", "after,before,limit,order") +AZ_OP(GetAssistant, Surface::kOpenAi, "GET", 0, "", "", "", "", "", "", "inf25", "/assistants/{assistant_id}", "Get_Assistant", "none", "", "") +AZ_OP(ListBatches, Surface::kOpenAi, "GET", 0, "v1p", "/batches", "listBatches", "none", "", "$filter,$orderby,after,limit", "auth24", "/batches", "Batch_List", "none", "", "after,limit") +AZ_OP(RetrieveBatch, Surface::kOpenAi, "GET", 0, "v1p", "/batches/{batch_id}", "retrieveBatch", "none", "", "", "auth24", "/batches/{batch-id}", "Batch_Get", "none", "", "") +AZ_OP(ListContainers, Surface::kOpenAi, "GET", 0, "v1p", "/containers", "listContainers", "none", "", "after,before,limit,order", "", "", "", "", "", "") +AZ_OP(RetrieveContainer, Surface::kOpenAi, "GET", 0, "v1p", "/containers/{container_id}", "retrieveContainer", "none", "", "", "", "", "", "", "", "") +AZ_OP(ListContainerFiles, Surface::kOpenAi, "GET", 0, "v1p", "/containers/{container_id}/files", "listContainerFiles", "none", "", "after,before,limit,order", "", "", "", "", "", "") +AZ_OP(RetrieveContainerFile, Surface::kOpenAi, "GET", 0, "v1p", "/containers/{container_id}/files/{file_id}", "retrieveContainerFile", "none", "", "", "", "", "", "", "", "") +AZ_OP(RetrieveContainerFileContent, Surface::kOpenAi, "GET", 0, "v1p", "/containers/{container_id}/files/{file_id}/content", "retrieveContainerFileContent", "none", "", "", "", "", "", "", "", "") +AZ_OP(RetrieveConversation, Surface::kOpenAi, "GET", 0, "v1p", "/conversations/{conversation_id}", "retrieveConversation", "none", "", "", "", "", "", "", "", "") +AZ_OP(ListConversationItems, Surface::kOpenAi, "GET", 0, "v1p", "/conversations/{conversation_id}/items", "listConversationItems", "none", "", "after,include,limit,order", "", "", "", "", "", "") +AZ_OP(RetrieveConversationItem, Surface::kOpenAi, "GET", 0, "v1p", "/conversations/{conversation_id}/items/{item_id}", "retrieveConversationItem", "none", "", "include", "", "", "", "", "", "") +AZ_OP(ListEvals, Surface::kOpenAi, "GET", 0, "v1p", "/evals", "listEvals", "none", "", "after,limit,order,order_by", "", "", "", "", "", "") +AZ_OP(GetEval, Surface::kOpenAi, "GET", 0, "v1p", "/evals/{eval_id}", "getEval", "none", "", "", "", "", "", "", "", "") +AZ_OP(GetEvalRuns, Surface::kOpenAi, "GET", 0, "v1p", "/evals/{eval_id}/runs", "getEvalRuns", "none", "", "after,limit,order,status", "", "", "", "", "", "") +AZ_OP(GetEvalRun, Surface::kOpenAi, "GET", 0, "v1p", "/evals/{eval_id}/runs/{run_id}", "getEvalRun", "none", "", "", "", "", "", "", "", "") +AZ_OP(GetEvalRunOutputItems, Surface::kOpenAi, "GET", 0, "v1p", "/evals/{eval_id}/runs/{run_id}/output_items", "getEvalRunOutputItems", "none", "", "after,limit,order,status", "", "", "", "", "", "") +AZ_OP(GetEvalRunOutputItem, Surface::kOpenAi, "GET", 0, "v1p", "/evals/{eval_id}/runs/{run_id}/output_items/{output_item_id}", "getEvalRunOutputItem", "none", "", "", "", "", "", "", "", "") +AZ_OP(ListFiles, Surface::kOpenAi, "GET", 0, "v1p", "/files", "listFiles", "none", "", "after,limit,order,purpose", "auth24", "/files", "Files_List", "none", "", "purpose") +AZ_OP(RetrieveFile, Surface::kOpenAi, "GET", 0, "v1p", "/files/{file_id}", "retrieveFile", "none", "", "", "auth24", "/files/{file-id}", "Files_Get", "none", "", "") +AZ_OP(DownloadFile, Surface::kOpenAi, "GET", 0, "v1p", "/files/{file_id}/content", "downloadFile", "none", "", "", "auth24", "/files/{file-id}/content", "Files_GetContent", "none", "", "") +AZ_OP(ListFineTuningCheckpointPermissions, Surface::kOpenAi, "GET", 0, "v1p", "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions", "listFineTuningCheckpointPermissions", "none", "", "after,limit,order,project_id", "", "", "", "", "", "") +AZ_OP(ListPaginatedFineTuningJobs, Surface::kOpenAi, "GET", 0, "v1p", "/fine_tuning/jobs", "listPaginatedFineTuningJobs", "none", "", "after,limit", "auth24", "/fine_tuning/jobs", "FineTuning_List", "none", "", "after,limit") +AZ_OP(RetrieveFineTuningJob, Surface::kOpenAi, "GET", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}", "retrieveFineTuningJob", "none", "", "", "auth24", "/fine_tuning/jobs/{fine-tuning-job-id}", "FineTuning_Get", "none", "", "") +AZ_OP(ListFineTuningJobCheckpoints, Surface::kOpenAi, "GET", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints", "listFineTuningJobCheckpoints", "none", "", "after,limit", "auth24", "/fine_tuning/jobs/{fine-tuning-job-id}/checkpoints", "FineTuning_GetCheckpoints", "none", "", "after,limit") +AZ_OP(FineTuningGetCheckpoint, Surface::kOpenAi, "GET", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints/{fine_tuning_checkpoint_id}/copy", "FineTuning_GetCheckpoint", "none", "", "", "", "", "", "", "", "") +AZ_OP(ListFineTuningEvents, Surface::kOpenAi, "GET", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/events", "listFineTuningEvents", "none", "", "after,limit", "auth24", "/fine_tuning/jobs/{fine-tuning-job-id}/events", "FineTuning_GetEvents", "none", "", "after,limit") +AZ_OP(ListModels, Surface::kOpenAi, "GET", 0, "v1p", "/models", "listModels", "none", "", "", "auth24", "/models", "Models_List", "none", "", "") +AZ_OP(RetrieveModel, Surface::kOpenAi, "GET", 0, "v1p", "/models/{model}", "retrieveModel", "none", "", "", "auth24", "/models/{model-id}", "Models_Get", "none", "", "") +AZ_OP(GetResponse, Surface::kOpenAi, "GET", 0, "v1p", "/responses/{response_id}", "getResponse", "none", "", "include[],include_obfuscation,starting_after,stream", "inf25", "/responses/{response_id}", "", "none", "", "include") +AZ_OP(ListInputItems, Surface::kOpenAi, "GET", 0, "v1p", "/responses/{response_id}/input_items", "listInputItems", "none", "", "after,before,limit,order", "inf25", "/responses/{response_id}/input_items", "", "none", "", "after,before,limit,order") +AZ_OP(RetrieveThread, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}", "retrieveThread", "none", "", "", "inf25", "/threads/{thread_id}", "Get_Thread", "none", "", "") +AZ_OP(ListMessages, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}/messages", "listMessages", "none", "", "after,before,limit,order,run_id", "inf25", "/threads/{thread_id}/messages", "List_Messages", "none", "", "after,before,limit,order,run_id") +AZ_OP(RetrieveMessage, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}/messages/{message_id}", "retrieveMessage", "none", "", "", "inf25", "/threads/{thread_id}/messages/{message_id}", "Get_Message", "none", "", "") +AZ_OP(ListRuns, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}/runs", "listRuns", "none", "", "after,before,limit,order", "inf25", "/threads/{thread_id}/runs", "List_Runs", "none", "", "after,before,limit,order") +AZ_OP(RetrieveRun, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}/runs/{run_id}", "retrieveRun", "none", "", "", "inf25", "/threads/{thread_id}/runs/{run_id}", "Get_Run", "none", "", "") +AZ_OP(ListRunSteps, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}/runs/{run_id}/steps", "listRunSteps", "none", "", "after,before,limit,order", "inf25", "/threads/{thread_id}/runs/{run_id}/steps", "List_Run_Steps", "none", "", "after,before,include[],limit,order") +AZ_OP(GetRunStep, Surface::kOpenAi, "GET", 0, "v1p", "/threads/{thread_id}/runs/{run_id}/steps/{step_id}", "getRunStep", "none", "", "", "inf25", "/threads/{thread_id}/runs/{run_id}/steps/{step_id}", "Get_Run_Step", "none", "", "include[]") +AZ_OP(ListVectorStores, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores", "listVectorStores", "none", "", "after,before,limit,order", "inf25", "/vector_stores", "List_Vector_Stores", "none", "", "after,before,limit,order") +AZ_OP(GetVectorStore, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores/{vector_store_id}", "getVectorStore", "none", "", "", "inf25", "/vector_stores/{vector_store_id}", "Get_Vector_Store", "none", "", "") +AZ_OP(GetVectorStoreFileBatch, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores/{vector_store_id}/file_batches/{batch_id}", "getVectorStoreFileBatch", "none", "", "", "inf25", "/vector_stores/{vector_store_id}/file_batches/{batch_id}", "Get_Vector_Store_File_Batch", "none", "", "") +AZ_OP(ListFilesInVectorStoreBatch, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores/{vector_store_id}/file_batches/{batch_id}/files", "listFilesInVectorStoreBatch", "none", "", "after,before,filter,limit,order", "inf25", "/vector_stores/{vector_store_id}/file_batches/{batch_id}/files", "List_Vector_Store_File_Batch_Files", "none", "", "after,before,filter,limit,order") +AZ_OP(ListVectorStoreFiles, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores/{vector_store_id}/files", "listVectorStoreFiles", "none", "", "after,before,filter,limit,order", "inf25", "/vector_stores/{vector_store_id}/files", "List_Vector_Store_Files", "none", "", "after,before,filter,limit,order") +AZ_OP(GetVectorStoreFile, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores/{vector_store_id}/files/{file_id}", "getVectorStoreFile", "none", "", "", "inf25", "/vector_stores/{vector_store_id}/files/{file_id}", "Get_Vector_Store_File", "none", "", "") +AZ_OP(RetrieveVectorStoreFileContent, Surface::kOpenAi, "GET", 0, "v1p", "/vector_stores/{vector_store_id}/files/{file_id}/content", "retrieveVectorStoreFileContent", "none", "", "", "inf25", "/vector_stores/{vector_store_id}/files/{file_id}/content", "retrieveVectorStoreFileContent", "none", "", "") +AZ_OP(VideosList, Surface::kOpenAi, "GET", 0, "v1p", "/videos", "Videos_List", "none", "", "after,limit,order", "", "", "", "", "", "") +AZ_OP(VideosGet, Surface::kOpenAi, "GET", 0, "v1p", "/videos/{video-id}", "Videos_Get", "none", "", "", "", "", "", "", "", "") +AZ_OP(VideosRetrieveContent, Surface::kOpenAi, "GET", 0, "v1p", "/videos/{video-id}/content", "Videos_RetrieveContent", "none", "", "variant", "", "", "", "", "", "") +AZ_OP(CreateAssistant, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "inf25", "/assistants", "Create_Assistant", "json", "description,instructions,metadata,model,name,response_format,temperature,tool_resources,tools,top_p", "") +AZ_OP(ModifyAssistant, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "inf25", "/assistants/{assistant_id}", "Modify_Assistant", "json", "description,instructions,metadata,model,name,response_format,temperature,tool_resources,tools,top_p", "") +AZ_OP(CreateSpeech, Surface::kOpenAi, "POST", 1, "v1p", "/audio/speech", "createSpeech", "json", "input,instructions,model,response_format,speed,stream_format,voice", "", "inf25", "/deployments/{deployment-id}/audio/speech", "Speech_Create", "multipart", "input,response_format,speed,voice", "") +AZ_OP(CreateTranscription, Surface::kOpenAi, "POST", 1, "v1p", "/audio/transcriptions", "createTranscription", "json", "chunking_strategy,file,filename,include,known_speaker_names,known_speaker_references,language,model,prompt,response_format,stream,temperature,timestamp_granularities", "", "inf25", "/deployments/{deployment-id}/audio/transcriptions", "Transcriptions_Create", "multipart", "file,language,prompt,response_format,temperature,timestamp_granularities[]", "") +AZ_OP(CreateTranslation, Surface::kOpenAi, "POST", 1, "v1p", "/audio/translations", "createTranslation", "json", "file,filename,model,prompt,response_format,temperature", "", "inf25", "/deployments/{deployment-id}/audio/translations", "Translations_Create", "multipart", "file,prompt,response_format,temperature", "") +AZ_OP(CreateBatch, Surface::kOpenAi, "POST", 0, "v1p", "/batches", "createBatch", "json", "completion_window,endpoint,input_blob,input_file_id,output_expires_after,output_folder", "", "auth24", "/batches", "Batch_Create", "json", "completion_window,endpoint,input_file_id,metadata", "") +AZ_OP(CancelBatch, Surface::kOpenAi, "POST", 0, "v1p", "/batches/{batch_id}/cancel", "cancelBatch", "none", "", "", "auth24", "/batches/{batch-id}/cancel", "Batch_Cancel", "none", "", "") +AZ_OP(CreateChatCompletion, Surface::kOpenAi, "POST", 1, "v1p", "/chat/completions", "createChatCompletion", "json", "audio,frequency_penalty,function_call,functions,logit_bias,logprobs,max_completion_tokens,max_tokens,messages,metadata,modalities,model,n,parallel_tool_calls,prediction,presence_penalty,prompt_cache_key,prompt_cache_retention,reasoning_effort,response_format,safety_identifier,seed,stop,store,stream,stream_options,temperature,tool_choice,tools,top_logprobs,top_p,user,user_security_context,verbosity", "", "inf25", "/deployments/{deployment-id}/chat/completions", "ChatCompletions_Create", "json", "audio,data_sources,frequency_penalty,function_call,functions,logit_bias,logprobs,max_completion_tokens,max_tokens,messages,metadata,modalities,n,parallel_tool_calls,prediction,presence_penalty,reasoning_effort,response_format,seed,stop,store,stream,stream_options,temperature,tool_choice,tools,top_logprobs,top_p,user,user_security_context", "") +AZ_OP(CreateCompletion, Surface::kOpenAi, "POST", 1, "v1p", "/completions", "createCompletion", "json", "best_of,echo,frequency_penalty,logit_bias,logprobs,max_tokens,model,n,presence_penalty,prompt,seed,stop,stream,stream_options,suffix,temperature,top_p,user", "", "inf25", "/deployments/{deployment-id}/completions", "Completions_Create", "json", "best_of,echo,frequency_penalty,logit_bias,logprobs,max_tokens,n,presence_penalty,prompt,seed,stop,stream,suffix,temperature,top_p,user", "") +AZ_OP(CreateContainer, Surface::kOpenAi, "POST", 0, "v1p", "/containers", "createContainer", "json", "expires_after,file_ids,memory_limit,name,network_policy,skills", "", "", "", "", "", "", "") +AZ_OP(CreateContainerFile, Surface::kOpenAi, "POST", 0, "v1p", "/containers/{container_id}/files", "createContainerFile", "multipart", "file,file_id", "", "", "", "", "", "", "") +AZ_OP(CreateConversation, Surface::kOpenAi, "POST", 0, "v1p", "/conversations", "createConversation", "json", "items,metadata", "", "", "", "", "", "", "") +AZ_OP(UpdateConversation, Surface::kOpenAi, "POST", 0, "v1p", "/conversations/{conversation_id}", "updateConversation", "json", "metadata", "", "", "", "", "", "", "") +AZ_OP(CreateConversationItems, Surface::kOpenAi, "POST", 0, "v1p", "/conversations/{conversation_id}/items", "createConversationItems", "json", "items", "include", "", "", "", "", "", "") +AZ_OP(CreateEmbedding, Surface::kOpenAi, "POST", 1, "v1p", "/embeddings", "createEmbedding", "json", "dimensions,encoding_format,input,model,user", "", "inf25", "/deployments/{deployment-id}/embeddings", "embeddings_create", "json", "dimensions,encoding_format,input,input_type,user", "") +AZ_OP(CreateEval, Surface::kOpenAi, "POST", 0, "v1p", "/evals", "createEval", "json", "data_source_config,metadata,name,statusCode,testing_criteria", "", "", "", "", "", "", "") +AZ_OP(UpdateEval, Surface::kOpenAi, "POST", 0, "v1p", "/evals/{eval_id}", "updateEval", "json", "metadata,name", "", "", "", "", "", "", "") +AZ_OP(CreateEvalRun, Surface::kOpenAi, "POST", 0, "v1p", "/evals/{eval_id}/runs", "createEvalRun", "json", "data_source,metadata,name", "", "", "", "", "", "", "") +AZ_OP(CancelEvalRun, Surface::kOpenAi, "POST", 0, "v1p", "/evals/{eval_id}/runs/{run_id}", "cancelEvalRun", "none", "", "", "", "", "", "", "", "") +AZ_OP(CreateFile, Surface::kOpenAi, "POST", 0, "v1p", "/files", "createFile", "multipart", "expires_after,file,purpose", "", "auth24", "/files", "Files_Upload", "multipart", "file,purpose", "") +AZ_OP(FilesImport, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "auth24", "/files/import", "Files_Import", "json", "content_url,filename,purpose", "") +AZ_OP(RunGrader, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/alpha/graders/run", "runGrader", "json", "grader,item,model_sample", "", "", "", "", "", "", "") +AZ_OP(ValidateGrader, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/alpha/graders/validate", "validateGrader", "json", "grader", "", "", "", "", "", "", "") +AZ_OP(CreateFineTuningCheckpointPermission, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/checkpoints/{fine_tuned_model_checkpoint}/permissions", "createFineTuningCheckpointPermission", "json", "project_ids", "", "", "", "", "", "", "") +AZ_OP(CreateFineTuningJob, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/jobs", "createFineTuningJob", "json", "hyperparameters,integrations,metadata,method,model,seed,suffix,training_file,validation_file", "", "auth24", "/fine_tuning/jobs", "FineTuning_Create", "json", "hyperparameters,model,seed,suffix,training_file,validation_file", "") +AZ_OP(CancelFineTuningJob, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/cancel", "cancelFineTuningJob", "none", "", "", "auth24", "/fine_tuning/jobs/{fine-tuning-job-id}/cancel", "FineTuning_Cancel", "none", "", "") +AZ_OP(FineTuningCopyCheckpoint, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints/{fine_tuning_checkpoint_id}/copy", "FineTuning_CopyCheckpoint", "json", "destinationResourceId,region", "", "", "", "", "", "", "") +AZ_OP(PauseFineTuningJob, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/pause", "pauseFineTuningJob", "none", "", "", "", "", "", "", "", "") +AZ_OP(ResumeFineTuningJob, Surface::kOpenAi, "POST", 0, "v1p", "/fine_tuning/jobs/{fine_tuning_job_id}/resume", "resumeFineTuningJob", "none", "", "", "", "", "", "", "", "") +AZ_OP(CreateImageEdit, Surface::kOpenAi, "POST", 1, "v1p", "/images/edits", "createImageEdit", "json", "background,image,input_fidelity,mask,model,n,output_compression,output_format,partial_images,prompt,quality,response_format,size,stream,user", "", "inf25", "/deployments/{deployment-id}/images/edits", "ImageGenerations_Edit", "json", "image,mask,n,prompt,quality,response_format,size,user", "") +AZ_OP(CreateImage, Surface::kOpenAi, "POST", 1, "v1p", "/images/generations", "createImage", "json", "background,model,moderation,n,output_compression,output_format,partial_images,prompt,quality,response_format,size,stream,style,user", "", "inf25", "/deployments/{deployment-id}/images/generations", "ImageGenerations_Create", "json", "background,n,output_compression,output_format,prompt,quality,response_format,size,style,user", "") +AZ_OP(CreateImageVariation, Surface::kOpenAi, "POST", 0, "v1p", "/images/variations", "createImageVariation", "json", "image,model,n,response_format,size,user", "", "", "", "", "", "", "") +AZ_OP(CreateRealtimeCall, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/calls", "createRealtimeCall", "multipart", "sdp,session", "", "", "", "", "", "", "") +AZ_OP(AcceptRealtimeCall, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/calls/{call_id}/accept", "acceptRealtimeCall", "json", "audio,include,instructions,max_output_tokens,model,output_modalities,parallel_tool_calls,prompt,reasoning,tool_choice,tools,tracing,truncation,type", "", "", "", "", "", "", "") +AZ_OP(HangupRealtimeCall, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/calls/{call_id}/hangup", "hangupRealtimeCall", "none", "", "", "", "", "", "", "", "") +AZ_OP(ReferRealtimeCall, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/calls/{call_id}/refer", "referRealtimeCall", "json", "target_uri", "", "", "", "", "", "", "") +AZ_OP(RejectRealtimeCall, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/calls/{call_id}/reject", "rejectRealtimeCall", "json", "status_code", "", "", "", "", "", "", "") +AZ_OP(CreateRealtimeClientSecret, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/client_secrets", "createRealtimeClientSecret", "json", "expires_after,session", "", "", "", "", "", "", "") +AZ_OP(CreateRealtimeSession, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/sessions", "createRealtimeSession", "json", "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", "", "", "", "", "", "", "") +AZ_OP(CreateRealtimeTranscriptionSession, Surface::kOpenAi, "POST", 0, "v1p", "/realtime/transcription_sessions", "createRealtimeTranscriptionSession", "json", "include,input_audio_format,input_audio_noise_reduction,input_audio_transcription,turn_detection", "", "", "", "", "", "", "") +AZ_OP(CreateRealtimeSessionDated, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "inf25", "/realtimeapi/sessions", "create_realtimeSession", "json", "input_audio_format,input_audio_noise_reduction,input_audio_transcription,instructions,max_response_output_tokens,modalities,model,output_audio_format,temperature,tool_choice,tools,turn_detection,voice", "") +AZ_OP(CreateTranscriptionRealtimeSession, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "inf25", "/realtimeapi/transcription_sessions", "create_transcriptionRealtimeSession", "json", "include,input_audio_format,input_audio_noise_reduction,input_audio_transcription,modalities,turn_detection", "") +AZ_OP(CreateResponse, Surface::kOpenAi, "POST", 0, "v1p", "/responses", "createResponse", "json", "background,context_management,conversation,include,input,instructions,max_output_tokens,max_tool_calls,metadata,model,parallel_tool_calls,previous_response_id,prompt,prompt_cache_key,prompt_cache_retention,reasoning,safety_identifier,store,stream,stream_options,temperature,text,tool_choice,tools,top_logprobs,top_p,truncation,user", "", "inf25", "/responses", "", "json", "include,input,instructions,max_output_tokens,metadata,model,parallel_tool_calls,previous_response_id,reasoning,store,stream,temperature,text,tool_choice,tools,top_p,truncation,user", "") +AZ_OP(CancelResponse, Surface::kOpenAi, "POST", 0, "v1p", "/responses/{response_id}/cancel", "cancelResponse", "none", "", "", "", "", "", "", "", "") +AZ_OP(CreateThread, Surface::kOpenAi, "POST", 0, "v1p", "/threads", "createThread", "json", "messages,metadata,tool_resources", "", "inf25", "/threads", "Create_Thread", "json", "messages,metadata,tool_resources", "") +AZ_OP(CreateThreadAndRun, Surface::kOpenAi, "POST", 0, "v1p", "/threads/runs", "createThreadAndRun", "json", "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", "", "inf25", "/threads/runs", "Create_Thread_And_Run", "json", "assistant_id,instructions,max_completion_tokens,max_prompt_tokens,metadata,model,parallel_tool_calls,response_format,stream,stream_options,temperature,thread,tool_choice,tool_resources,tools,top_p,truncation_strategy", "") +AZ_OP(ModifyThread, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}", "modifyThread", "json", "metadata,tool_resources", "", "inf25", "/threads/{thread_id}", "Modify_Thread", "json", "metadata,tool_resources", "") +AZ_OP(CreateMessage, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}/messages", "createMessage", "json", "attachments,content,metadata,role", "", "inf25", "/threads/{thread_id}/messages", "Create_Message", "json", "attachments,content,metadata,role", "") +AZ_OP(ModifyMessage, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}/messages/{message_id}", "modifyMessage", "json", "metadata", "", "inf25", "/threads/{thread_id}/messages/{message_id}", "Modify_Message", "json", "metadata", "") +AZ_OP(CreateRun, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}/runs", "createRun", "json", "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", "", "inf25", "/threads/{thread_id}/runs", "Create_Run", "json", "additional_instructions,additional_messages,assistant_id,instructions,max_completion_tokens,max_prompt_tokens,metadata,model,parallel_tool_calls,response_format,stream,temperature,tool_choice,tools,top_p,truncation_strategy", "include[]") +AZ_OP(ModifyRun, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}/runs/{run_id}", "modifyRun", "json", "metadata", "", "inf25", "/threads/{thread_id}/runs/{run_id}", "Modify_Run", "json", "metadata", "") +AZ_OP(CancelRun, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}/runs/{run_id}/cancel", "cancelRun", "none", "", "", "inf25", "/threads/{thread_id}/runs/{run_id}/cancel", "Cancel_Run", "none", "", "") +AZ_OP(SubmitToolOutputsToRun, Surface::kOpenAi, "POST", 0, "v1p", "/threads/{thread_id}/runs/{run_id}/submit_tool_outputs", "submitToolOutputsToRun", "json", "stream,tool_outputs", "", "inf25", "/threads/{thread_id}/runs/{run_id}/submit_tool_outputs", "Submit_Tool_Outputs_To_Run", "json", "stream,tool_outputs", "") +AZ_OP(UploadFileStart, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "auth24", "/uploads", "UploadFile_Start", "json", "bytes,filename,mime_type,purpose", "") +AZ_OP(UploadFileCancel, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "auth24", "/uploads/{upload-id}/cancel", "UploadFile_Cancel", "none", "", "") +AZ_OP(UploadFileComplete, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "auth24", "/uploads/{upload-id}/complete", "UploadFile_Complete", "json", "md5,part_ids", "") +AZ_OP(UploadFilePart, Surface::kOpenAi, "POST", 0, "", "", "", "", "", "", "auth24", "/uploads/{upload-id}/parts", "UploadFile_Part", "multipart", "data", "") +AZ_OP(CreateVectorStore, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores", "createVectorStore", "json", "chunking_strategy,description,expires_after,file_ids,metadata,name", "", "inf25", "/vector_stores", "Create_Vector_Store", "json", "chunking_strategy,expires_after,file_ids,metadata,name", "") +AZ_OP(ModifyVectorStore, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores/{vector_store_id}", "modifyVectorStore", "json", "expires_after,metadata,name", "", "inf25", "/vector_stores/{vector_store_id}", "Modify_Vector_Store", "json", "expires_after,metadata,name", "") +AZ_OP(CreateVectorStoreFileBatch, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores/{vector_store_id}/file_batches", "createVectorStoreFileBatch", "json", "attributes,chunking_strategy,file_ids,files", "", "inf25", "/vector_stores/{vector_store_id}/file_batches", "Create_Vector_Store_File_Batch", "json", "chunking_strategy,file_ids", "") +AZ_OP(CancelVectorStoreFileBatch, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores/{vector_store_id}/file_batches/{batch_id}/cancel", "cancelVectorStoreFileBatch", "none", "", "", "inf25", "/vector_stores/{vector_store_id}/file_batches/{batch_id}/cancel", "Cancel_Vector_Store_File_Batch", "none", "", "") +AZ_OP(CreateVectorStoreFile, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores/{vector_store_id}/files", "createVectorStoreFile", "json", "attributes,chunking_strategy,file_id", "", "inf25", "/vector_stores/{vector_store_id}/files", "Create_Vector_Store_File", "json", "chunking_strategy,file_id", "") +AZ_OP(UpdateVectorStoreFileAttributes, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores/{vector_store_id}/files/{file_id}", "updateVectorStoreFileAttributes", "json", "attributes", "", "inf25", "/vector_stores/{vector_store_id}/files/{file_id}", "updateVectorStoreFileAttributes", "json", "attributes", "") +AZ_OP(SearchVectorStore, Surface::kOpenAi, "POST", 0, "v1p", "/vector_stores/{vector_store_id}/search", "searchVectorStore", "json", "filters,max_num_results,query,ranking_options,rewrite_query", "", "inf25", "/vector_stores/{vector_store_id}/search", "searchVectorStore", "json", "filters,max_num_results,query,ranking_options,rewrite_query", "") +AZ_OP(VideosCreate, Surface::kOpenAi, "POST", 0, "v1p", "/videos", "Videos_Create", "json+multipart", "input_reference,model,prompt,seconds,size", "", "", "", "", "", "", "") +AZ_OP(VideosRemix, Surface::kOpenAi, "POST", 0, "v1p", "/videos/{video-id}/remix", "Videos_Remix", "json", "prompt", "", "", "", "", "", "", "") +AZ_OP(FoundryGetModelInfo, Surface::kFoundry, "GET", 0, "", "", "", "", "", "", "mi", "/info", "GetModelInfo", "none", "", "") +AZ_OP(FoundryGetChatCompletions, Surface::kFoundry, "POST", 0, "", "", "", "", "", "", "mi", "/chat/completions", "GetChatCompletions", "json", "frequency_penalty,max_tokens,messages,modalities,model,presence_penalty,response_format,seed,stop,stream,temperature,tool_choice,tools,top_p", "") +AZ_OP(FoundryGetEmbeddings, Surface::kFoundry, "POST", 0, "", "", "", "", "", "", "mi", "/embeddings", "GetEmbeddings", "json", "dimensions,encoding_format,input,input_type,model", "") +AZ_OP(FoundryGetImageEmbeddings, Surface::kFoundry, "POST", 0, "", "", "", "", "", "", "mi", "/images/embeddings", "GetImageEmbeddings", "json", "dimensions,encoding_format,input,input_type,model", "") +AZ_OP(SafetyTextBlocklistsDeleteTextBlocklist, Surface::kSafety, "DELETE", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}", "TextBlocklists_DeleteTextBlocklist", "none", "", "") +AZ_OP(SafetyTextBlocklistsListTextBlocklists, Surface::kSafety, "GET", 0, "", "", "", "", "", "", "cs24", "/text/blocklists", "TextBlocklists_ListTextBlocklists", "none", "", "") +AZ_OP(SafetyTextBlocklistsGetTextBlocklist, Surface::kSafety, "GET", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}", "TextBlocklists_GetTextBlocklist", "none", "", "") +AZ_OP(SafetyTextBlocklistsListTextBlocklistItems, Surface::kSafety, "GET", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}/blocklistItems", "TextBlocklists_ListTextBlocklistItems", "none", "", "maxpagesize,skip,top") +AZ_OP(SafetyTextBlocklistsGetTextBlocklistItem, Surface::kSafety, "GET", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}/blocklistItems/{blocklistItemId}", "TextBlocklists_GetTextBlocklistItem", "none", "", "") +AZ_OP(SafetyTextBlocklistsCreateOrUpdateTextBlocklist, Surface::kSafety, "PATCH", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}", "TextBlocklists_CreateOrUpdateTextBlocklist", "json", "blocklistName,description", "") +AZ_OP(SafetyImageOperationsAnalyzeImage, Surface::kSafety, "POST", 0, "", "", "", "", "", "", "cs24", "/image:analyze", "ImageOperations_AnalyzeImage", "json", "categories,image,outputType", "") +AZ_OP(SafetyTextBlocklistsAddOrUpdateBlocklistItems, Surface::kSafety, "POST", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", "TextBlocklists_AddOrUpdateBlocklistItems", "json", "blocklistItems", "") +AZ_OP(SafetyTextBlocklistsRemoveBlocklistItems, Surface::kSafety, "POST", 0, "", "", "", "", "", "", "cs24", "/text/blocklists/{blocklistName}:removeBlocklistItems", "TextBlocklists_RemoveBlocklistItems", "json", "blocklistItemIds", "") +AZ_OP(SafetyTextOperationsAnalyzeText, Surface::kSafety, "POST", 0, "", "", "", "", "", "", "cs24", "/text:analyze", "TextOperations_AnalyzeText", "json", "blocklistNames,categories,haltOnBlocklistHit,outputType,text", "") +AZ_OP(SafetyTextOperationsDetectTextProtectedMaterial, Surface::kSafety, "POST", 0, "", "", "", "", "", "", "cs24", "/text:detectProtectedMaterial", "TextOperations_DetectTextProtectedMaterial", "json", "text", "") +AZ_OP(SafetyTextOperationsShieldPrompt, Surface::kSafety, "POST", 0, "", "", "", "", "", "", "cs24", "/text:shieldPrompt", "TextOperations_ShieldPrompt", "json", "documents,userPrompt", "") +// AZ-ROWS-END diff --git a/src/vendors/azure/caps.h b/src/vendors/azure/caps.h new file mode 100644 index 0000000..0aefc4e --- /dev/null +++ b/src/vendors/azure/caps.h @@ -0,0 +1,48 @@ +#pragma once + +// Static ceilings for the Azure package. Never autotuned. Numbers come from +// Microsoft Learn limits pages and the OpenAPI documents (see SPEC.md). + +#include + +namespace molo::azure { + +struct AzureCaps { + // Names and endpoints. + static constexpr std::size_t kMaxEndpointChars = 256; + static constexpr std::size_t kMaxDeploymentChars = 64; // Azure deployment name limit + static constexpr std::size_t kMaxIdChars = 256; + static constexpr std::size_t kMaxSecretChars = 1024; + // Lists. + static constexpr int kMaxPageLimit = 100; + static constexpr std::size_t kMaxListPages = 20; + // Streams. + static constexpr std::size_t kMaxStreamEvents = 200000; + // Request content. + static constexpr std::size_t kMaxEmbeddingInputs = 2048; + static constexpr std::size_t kMaxAudioFileBytes = 25u * 1024u * 1024u; + static constexpr std::size_t kMaxImageFileBytes = 50u * 1024u * 1024u; + static constexpr std::size_t kMaxSpeechChars = 4096; + static constexpr std::size_t kMaxImagePromptChars = 32000; + // Content Safety (documented limits: 10k chars per text, 7.5 MiB image). + static constexpr std::size_t kMaxSafetyTextChars = 10000; + static constexpr std::size_t kMaxSafetyImageBytes = 7864320; + static constexpr std::size_t kMaxBlocklistItems = 100; + static constexpr std::size_t kMaxBlocklistItemChars = 128; + static constexpr std::size_t kMaxShieldDocuments = 5; + // Entra token exchange. The skew refreshes before expiry; the gap is the + // floor between two exchanges after a failure (the source throws without + // network traffic until it passes). Responses over the byte cap are refused. + static constexpr long kTokenSkewSeconds = 300; + static constexpr long kMinExchangeGapSeconds = 5; + static constexpr long kMaxTokenLifetimeSeconds = 24 * 3600; + static constexpr std::size_t kMaxTokenBytes = 16 * 1024; + // Realtime client secrets. + static constexpr int kMaxClientSecretSeconds = 7200; + // Waiting on background work. + static constexpr int kMinPollSeconds = 1; + static constexpr int kMaxPollSeconds = 60; + static constexpr int kMaxWaitSeconds = 600; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/chat.cpp b/src/vendors/azure/chat.cpp new file mode 100644 index 0000000..218f63a --- /dev/null +++ b/src/vendors/azure/chat.cpp @@ -0,0 +1,38 @@ +#include "vendors/azure/chat.h" + +#include "llm/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +void ChatService::validate(const std::string& deployment, const Json& body) const { + Check::deployment(deployment); + Check::object("body", body); + const Json* m = body.find("messages"); + Check::require(m && m->is_array() && !m->as_array().empty(), "messages is required"); + Check::max_count("messages", m->as_array().size(), llm::Caps::kMaxMessages); +} + +Json ChatService::create(const std::string& deployment, const Json& body) { + validate(deployment, body); + const Json* s = body.find("stream"); + Check::require(!s || (s->is_bool() && !s->as_bool()), "use stream() for stream=true"); + return call(Op::kCreateChatCompletion, {deployment, {}}, &body); +} + +void ChatService::stream(const std::string& deployment, const Json& body, const ChunkHandler& handler) { + validate(deployment, body); + call_stream(Op::kCreateChatCompletion, {deployment, {}}, body, + [&](const llm::SseEvent& ev) { return handler(ev.data); }); +} + +Json CompletionsService::create(const std::string& deployment, const Json& body) { + Check::deployment(deployment); + Check::object("body", body); + Check::has_field(body, "prompt"); + return call(Op::kCreateCompletion, {deployment, {}}, &body); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/chat.h b/src/vendors/azure/chat.h new file mode 100644 index 0000000..f45833f --- /dev/null +++ b/src/vendors/azure/chat.h @@ -0,0 +1,42 @@ +#pragma once + +// Chat Completions and legacy Completions on a deployment. +// v1 API POST /openai/v1/chat/completions (deployment = body model) +// dated API POST /openai/deployments/{d}/chat/completions?api-version=... +// The caller passes the deployment name once; the gateway puts it where the +// configured API needs it. + +#include +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class ChatService : public AzureService { + public: + using ChunkHandler = std::function; // false: stop + + explicit ChatService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body: messages (required, 1..4096) plus any field of the spec. `stream` + // must be absent or false here; use stream(). + clients::body::Json create(const std::string& deployment, const clients::body::Json& body); + // Every `chat.completion.chunk` document reaches the handler; [DONE] ends. + void stream(const std::string& deployment, const clients::body::Json& body, + const ChunkHandler& handler); + + private: + void validate(const std::string& deployment, const clients::body::Json& body) const; +}; + +class CompletionsService : public AzureService { + public: + explicit CompletionsService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + // body: prompt (required) plus any field of the spec. + clients::body::Json create(const std::string& deployment, const clients::body::Json& body); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/chat_model.cpp b/src/vendors/azure/chat_model.cpp new file mode 100644 index 0000000..1fd1de3 --- /dev/null +++ b/src/vendors/azure/chat_model.cpp @@ -0,0 +1,83 @@ +#include "vendors/azure/chat_model.h" + +#include "openai/core/wire/response_events.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; +namespace oc = openai::core; + +AzureChatModel::AzureChatModel(std::shared_ptr azure, ChatModelConfig config) + : azure_(azure), config_(std::move(config)), chat_(azure), responses_(azure), foundry_(azure) {} + +std::shared_ptr AzureChatModel::from_env(ChatModelConfig config) { + return std::make_shared(AzureClient::from_env(), std::move(config)); +} + +void AzureChatModel::check(const llm::ChatRequest& request) const { + Check::deployment(request.model); + Check::require(!request.messages.empty(), "messages is required"); +} + +Json AzureChatModel::wire_body(const llm::ChatRequest& request, bool stream) const { + check(request); + if (config_.dialect == Dialect::kResponses) + return oc::ResponsesWire::request(request, config_.wire).to_json(stream); + oc::ChatCompletionRequest r = oc::ChatWire::request(request, config_.wire); + if (stream) r.include_usage = true; + Json body = r.to_json(stream); + if (config_.dialect == Dialect::kFoundry) { + // The model inference API names the cap max_tokens. + Json::Object& o = body.as_object(); + if (auto it = o.find("max_completion_tokens"); it != o.end()) { + o["max_tokens"] = it->second; + o.erase("max_completion_tokens"); + } + if (!stream) o.erase("stream"); + } + return body; +} + +llm::ChatResponse AzureChatModel::generate(const llm::ChatRequest& request) { + Json body = wire_body(request, false); + switch (config_.dialect) { + case Dialect::kResponses: + return oc::ResponsesWire::response(responses_.create(body)); + case Dialect::kFoundry: + return oc::ChatWire::response(foundry_.chat(body, config_.extra_parameters)); + case Dialect::kChatCompletions: + break; + } + return oc::ChatWire::response(chat_.create(request.model, body)); +} + +void AzureChatModel::stream(const llm::ChatRequest& request, + const std::function& on_chunk) { + Json body = wire_body(request, true); + if (config_.dialect == Dialect::kResponses) { + oc::ResponsesChunkDecoder decoder; + responses_.stream(body, [&](const llm::SseEvent& ev) { + oc::ResponseEvent e = oc::ResponseEvent::from_sse(ev); + llm::ChatChunk chunk; + if (!decoder.decode(e, chunk)) return true; + return on_chunk(chunk); + }); + return; + } + oc::ChatChunkDecoder decoder; + auto handle = [&](const Json& doc) { + std::vector chunks; + decoder.decode(doc, chunks); + for (const llm::ChatChunk& c : chunks) + if (!on_chunk(c)) return false; + return true; + }; + if (config_.dialect == Dialect::kFoundry) { + foundry_.chat_stream(body, handle, config_.extra_parameters); + return; + } + chat_.stream(request.model, body, handle); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/chat_model.h b/src/vendors/azure/chat_model.h new file mode 100644 index 0000000..588c6c4 --- /dev/null +++ b/src/vendors/azure/chat_model.h @@ -0,0 +1,57 @@ +#pragma once + +// AzureChatModel: llm::ChatModel over a deployment of an Azure resource. +// The request's `model` is the deployment name. Three dialects: +// kChatCompletions /chat/completions (default; the widest model support) +// kResponses /responses +// kFoundry Azure AI model inference /models/chat/completions +// The neutral <-> wire mapping is the OpenAI one (openai::core::ChatWire / +// ResponsesWire): Azure OpenAI speaks the same shapes. + +#include +#include +#include + +#include "llm/chat_types.h" +#include "openai/core/wire/chat_wire.h" +#include "vendors/azure/chat.h" +#include "vendors/azure/foundry.h" +#include "vendors/azure/responses.h" + +namespace molo::azure { + +enum class Dialect { kChatCompletions, kResponses, kFoundry }; + +struct ChatModelConfig { + Dialect dialect = Dialect::kChatCompletions; + openai::core::WireOptions wire; + std::string extra_parameters; // Foundry dialect: error, drop, pass-through +}; + +class AzureChatModel : public llm::ChatModel { + public: + explicit AzureChatModel(std::shared_ptr azure, ChatModelConfig config = {}); + // Settings from the environment; not configured without endpoint and credential. + static std::shared_ptr from_env(ChatModelConfig config = {}); + + std::string vendor() const override { return "azure"; } + llm::ChatResponse generate(const llm::ChatRequest& request) override; + void stream(const llm::ChatRequest& request, + const std::function& on_chunk) override; + + Dialect dialect() const { return config_.dialect; } + bool configured() const { return azure_->configured(); } + // The wire body generate() would send for a request (no traffic). + clients::body::Json wire_body(const llm::ChatRequest& request, bool stream) const; + + private: + void check(const llm::ChatRequest& request) const; + + std::shared_ptr azure_; + ChatModelConfig config_; + ChatService chat_; + ResponsesService responses_; + FoundryService foundry_; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/check.cpp b/src/vendors/azure/check.cpp new file mode 100644 index 0000000..273276a --- /dev/null +++ b/src/vendors/azure/check.cpp @@ -0,0 +1,63 @@ +#include "vendors/azure/check.h" + +#include "llm/api_client.h" +#include "llm/error.h" +#include "vendors/azure/caps.h" + +namespace molo::azure { + +using clients::body::Json; + +void Check::fail(const std::string& why) { throw llm::ApiError::invalid("azure", why); } +void Check::require(bool ok, const std::string& why) { + if (!ok) fail(why); +} +void Check::non_empty(const std::string& what, const std::string& value) { + if (value.empty()) fail(what + " is required"); +} +void Check::max_chars(const std::string& what, const std::string& value, std::size_t cap) { + if (value.size() > cap) fail(what + " is over " + std::to_string(cap) + " characters"); +} +void Check::max_count(const std::string& what, std::size_t count, std::size_t cap) { + if (count > cap) fail(what + " has more than " + std::to_string(cap) + " entries"); +} +void Check::range(const std::string& what, double value, double lo, double hi) { + if (value < lo || value > hi) + fail(what + " must be " + std::to_string(static_cast(lo)) + ".." + std::to_string(static_cast(hi))); +} +void Check::in_set(const std::string& what, const std::string& value, + const std::vector& allowed) { + for (const std::string& a : allowed) + if (a == value) return; + std::string list; + for (const std::string& a : allowed) list += (list.empty() ? "" : ", ") + a; + fail(what + " must be one of: " + list); +} +void Check::deployment(const std::string& value) { + if (value.empty() || value.size() > AzureCaps::kMaxDeploymentChars || value == "." || value == "..") + fail("deployment must be 1.." + std::to_string(AzureCaps::kMaxDeploymentChars) + " characters"); + for (char c : value) + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || + c == '_' || c == '.')) + fail("deployment has invalid characters"); +} +void Check::id(const std::string& what, const std::string& value) { + if (!llm::Service::valid_id(value)) fail("bad " + what + " id"); +} +void Check::object(const std::string& what, const Json& value) { + if (!value.is_object()) fail(what + " must be a JSON object"); +} +void Check::has_string(const Json& body, const std::string& key) { + const Json* v = body.is_object() ? body.find(key) : nullptr; + if (!v || !v->is_string() || v->as_string().empty()) fail(key + " is required"); +} +void Check::has_field(const Json& body, const std::string& key) { + const Json* v = body.is_object() ? body.find(key) : nullptr; + if (!v || v->is_null()) fail(key + " is required"); +} +void Check::no_control(const std::string& what, const std::string& value) { + for (char c : value) + if (static_cast(c) < 0x20) fail(what + " has control characters"); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/check.h b/src/vendors/azure/check.h new file mode 100644 index 0000000..57981c1 --- /dev/null +++ b/src/vendors/azure/check.h @@ -0,0 +1,34 @@ +#pragma once + +// Request validation helpers. Every failure is llm::ApiError::invalid +// ("azure", 400 INVALID_ARGUMENT), thrown before any network traffic. + +#include +#include + +#include "clients/body/json.h" + +namespace molo::azure { + +class Check { + public: + [[noreturn]] static void fail(const std::string& why); + static void require(bool ok, const std::string& why); + static void non_empty(const std::string& what, const std::string& value); + static void max_chars(const std::string& what, const std::string& value, std::size_t cap); + static void max_count(const std::string& what, std::size_t count, std::size_t cap); + static void range(const std::string& what, double value, double lo, double hi); + static void in_set(const std::string& what, const std::string& value, + const std::vector& allowed); + // A deployment name: 1..64 of [A-Za-z0-9._-], not "." or "..". + static void deployment(const std::string& value); + // One URL path segment id (llm::Service::valid_id). + static void id(const std::string& what, const std::string& value); + static void object(const std::string& what, const clients::body::Json& value); + // The body object has the string field `key` (non-empty). + static void has_string(const clients::body::Json& body, const std::string& key); + static void has_field(const clients::body::Json& body, const std::string& key); + static void no_control(const std::string& what, const std::string& value); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/client.cpp b/src/vendors/azure/client.cpp new file mode 100644 index 0000000..dcd93fa --- /dev/null +++ b/src/vendors/azure/client.cpp @@ -0,0 +1,67 @@ +#include "vendors/azure/client.h" + +#include "clients/transports/http_transport.h" + +namespace molo::azure { + +namespace { +constexpr const char* kPlaceholderKey = "entra-managed"; // never sent: EntraTransport overwrites it + +llm::ApiClientConfig base(const std::string& endpoint, const char* key_env, bool entra, + const char* key_header) { + llm::ApiClientConfig c; + c.vendor = Settings::kVendor; + // An unset endpoint cannot be parsed as a URL; the empty key stops every call first. + c.base_url = endpoint.empty() ? "https://unset.invalid" : endpoint; + c.key_env = endpoint.empty() ? Settings::kEndpointEnv : key_env; + if (entra) { + c.auth = llm::AuthStyle::kBearer; + } else { + c.auth = llm::AuthStyle::kHeader; + c.auth_header = key_header; + } + return c; +} +} // namespace + +std::shared_ptr AzureClient::from_env() { + return create(Settings::from_env(), [](Channel) -> std::unique_ptr { + return std::make_unique(); + }); +} + +std::shared_ptr AzureClient::create(Settings settings, const TransportFactory& factory, + EntraTokenSource::Clock clock) { + settings.validate(); + std::shared_ptr self(new AzureClient()); + self->settings_ = std::move(settings); + const Settings& s = self->settings_; + bool entra = s.auth() == AuthKind::kEntra; + if (entra) self->tokens_ = std::make_shared(s.entra, factory(Channel::kToken), clock); + + auto wire = [&](Channel ch, const std::string& endpoint, const char* key_env, const char* header, + const std::string& key) { + std::unique_ptr t = factory(ch); + if (entra) t = std::make_unique(std::move(t), self->tokens_); + std::string k = entra ? (endpoint.empty() ? "" : kPlaceholderKey) : (endpoint.empty() ? "" : key); + return std::make_shared(std::move(t), base(endpoint, key_env, entra, header), k); + }; + self->openai_ = wire(Channel::kOpenAi, s.endpoint, Settings::kKeyEnv, "api-key", s.api_key); + self->foundry_ = wire(Channel::kFoundry, s.foundry_endpoint, Settings::kKeyEnv, "api-key", s.api_key); + self->safety_ = wire(Channel::kSafety, s.safety_endpoint, Settings::kSafetyKeyEnv, + "Ocp-Apim-Subscription-Key", s.safety_key); + return self; +} + +std::shared_ptr AzureClient::api(Surface surface) const { + switch (surface) { + case Surface::kFoundry: return foundry_; + case Surface::kSafety: return safety_; + case Surface::kOpenAi: break; + } + return openai_; +} + +bool AzureClient::configured(Surface surface) const { return api(surface)->configured(); } + +} // namespace molo::azure diff --git a/src/vendors/azure/client.h b/src/vendors/azure/client.h new file mode 100644 index 0000000..0bfa512 --- /dev/null +++ b/src/vendors/azure/client.h @@ -0,0 +1,51 @@ +#pragma once + +// AzureClient: one llm::ApiClient per surface (OpenAI resource, Foundry model +// inference, Content Safety) plus the shared credential. Key mode sends the +// api-key header (Ocp-Apim-Subscription-Key for Content Safety); Entra mode +// puts a managed bearer token on every request through EntraTransport. +// Not configured (no endpoint, or neither key nor complete Entra client +// credentials): every call throws NotConfiguredError before any traffic. + +#include +#include +#include + +#include "llm/api_client.h" +#include "vendors/azure/calls.h" +#include "vendors/azure/entra.h" +#include "vendors/azure/settings.h" + +namespace molo::azure { + +// Which wire a transport serves. kToken is the Entra token exchange. +enum class Channel { kOpenAi, kFoundry, kSafety, kToken }; + +class AzureClient { + public: + using TransportFactory = std::function(Channel)>; + + // Real HTTPS transports, settings from the environment. + static std::shared_ptr from_env(); + // Any transports (tests). + static std::shared_ptr create(Settings settings, const TransportFactory& factory, + EntraTokenSource::Clock clock = nullptr); + + const Settings& settings() const { return settings_; } + bool configured() const { return settings_.configured(); } + AuthKind auth() const { return settings_.auth(); } + std::shared_ptr api(Surface surface) const; + std::shared_ptr tokens() const { return tokens_; } + // The configured credential for a surface is present (Safety may have its own key). + bool configured(Surface surface) const; + + private: + AzureClient() = default; + Settings settings_; + std::shared_ptr tokens_; + std::shared_ptr openai_; + std::shared_ptr foundry_; + std::shared_ptr safety_; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/embeddings.cpp b/src/vendors/azure/embeddings.cpp new file mode 100644 index 0000000..418fc3f --- /dev/null +++ b/src/vendors/azure/embeddings.cpp @@ -0,0 +1,53 @@ +#include "vendors/azure/embeddings.h" + +#include "vendors/azure/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +Json EmbeddingsService::create(const std::string& deployment, const Json& body) { + Check::deployment(deployment); + Check::object("body", body); + const Json* in = body.find("input"); + Check::require(in && !in->is_null(), "input is required"); + if (in->is_array()) { + Check::require(!in->as_array().empty(), "input must not be empty"); + Check::max_count("input", in->as_array().size(), AzureCaps::kMaxEmbeddingInputs); + } else { + Check::require(in->is_string() && !in->as_string().empty(), "input must be a non-empty string or an array"); + } + return call(Op::kCreateEmbedding, {deployment, {}}, &body); +} + +std::vector> EmbeddingsService::embed(const std::string& deployment, + const std::vector& texts, + int dimensions) { + Check::require(!texts.empty(), "texts must not be empty"); + Json body = Json::object(); + Json arr = Json::array(); + for (const std::string& t : texts) { + Check::non_empty("text", t); + arr.push(Json(t)); + } + body.set("input", arr); + body.set("encoding_format", Json("float")); + if (dimensions > 0) body.set("dimensions", Json(dimensions)); + Json doc = create(deployment, body); + std::vector> out; + const Json* data = doc.find("data"); + if (!data || !data->is_array()) + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "embeddings answer has no data array"); + for (const Json& item : data->as_array()) { + std::vector v; + const Json* e = item.find("embedding"); + if (!e || !e->is_array()) + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "embedding item has no vector"); + for (const Json& x : e->as_array()) v.push_back(x.as_number()); + out.push_back(std::move(v)); + } + return out; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/embeddings.h b/src/vendors/azure/embeddings.h new file mode 100644 index 0000000..164b64d --- /dev/null +++ b/src/vendors/azure/embeddings.h @@ -0,0 +1,25 @@ +#pragma once + +// Embeddings on a deployment (text-embedding-3-*, ada-002). + +#include +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class EmbeddingsService : public AzureService { + public: + explicit EmbeddingsService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body.input: a string or an array of 1..2048 strings / token arrays. + clients::body::Json create(const std::string& deployment, const clients::body::Json& body); + // Convenience: texts in, body built, vectors out (encoding_format float). + std::vector> embed(const std::string& deployment, + const std::vector& texts, + int dimensions = 0); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/entra.cpp b/src/vendors/azure/entra.cpp new file mode 100644 index 0000000..71ceb9c --- /dev/null +++ b/src/vendors/azure/entra.cpp @@ -0,0 +1,146 @@ +#include "vendors/azure/entra.h" + +#include + +#include "clients/body/form.h" +#include "clients/body/json.h" +#include "llm/error.h" +#include "vendors/azure/caps.h" + +namespace molo::azure { + +using clients::body::Json; + +namespace { +clients::ClientConfig make_config() { + clients::ClientConfig c; + c.persist_cookies = false; + return c; +} + +std::int64_t system_now() { + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); +} + +std::string first_line(const std::string& s, std::size_t cap) { + std::string out = s.substr(0, s.find_first_of("\r\n")); + if (out.size() > cap) out.resize(cap); + return out; +} +} // namespace + +EntraTokenSource::EntraTokenSource(EntraConfig config, std::unique_ptr transport, + Clock clock) + : config_(std::move(config)), + client_(std::move(transport), make_config()), + clock_(clock ? std::move(clock) : Clock(system_now)) { + config_.validate(); +} + +std::string EntraTokenSource::request_body() const { + clients::body::UrlEncodedForm f; + f.add("client_id", config_.client_id); + f.add("client_secret", config_.client_secret); + f.add("scope", config_.scope); + f.add("grant_type", "client_credentials"); + return f.encode(); +} + +EntraTokenSource::Parsed EntraTokenSource::parse(int status, const std::string& body) { + if (body.size() > AzureCaps::kMaxTokenBytes) + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "token response over the byte cap"); + Json doc; + try { + doc = Json::parse(body); + } catch (const std::exception&) { + throw llm::ApiError("azure", status >= 400 ? status : 502, "BAD_RESPONSE", + "token endpoint answered with a non-JSON body"); + } + if (status < 200 || status >= 300 || !doc.is_object() || doc.has("error")) { + std::string code = "token_error"; + std::string desc = "HTTP " + std::to_string(status); + std::string trace; + if (doc.is_object()) { + if (const Json* e = doc.find("error"); e && e->is_string()) code = e->as_string(); + if (const Json* d = doc.find("error_description"); d && d->is_string()) + desc = first_line(d->as_string(), 300); + if (const Json* t = doc.find("trace_id"); t && t->is_string()) trace = t->as_string(); + } + throw llm::ApiError("azure", status >= 400 ? status : 502, code, desc, trace); + } + Parsed out; + const Json* t = doc.find("access_token"); + if (!t || !t->is_string() || t->as_string().empty()) + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "token response has no access_token"); + out.access_token = t->as_string(); + const Json* ex = doc.find("expires_in"); + // The v2.0 endpoint documents expires_in as a number; some stacks send a string. + if (ex && ex->is_number()) out.expires_in = ex->as_int(); + else if (ex && ex->is_string()) out.expires_in = std::atol(ex->as_string().c_str()); + if (out.expires_in <= 0 || out.expires_in > AzureCaps::kMaxTokenLifetimeSeconds) + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "token response has an unusable expires_in"); + return out; +} + +std::string EntraTokenSource::token() { + std::lock_guard lock(mutex_); + std::int64_t now = clock_(); + if (!token_.empty() && now < expires_at_ - AzureCaps::kTokenSkewSeconds) return token_; + if (last_failure_at_ != 0 && now - last_failure_at_ < AzureCaps::kMinExchangeGapSeconds) + throw llm::ApiError("azure", 503, "TOKEN_BACKOFF", + "token exchange failed " + std::to_string(now - last_failure_at_) + + "s ago (" + last_failure_ + "), not retrying yet"); + clients::ClientRequest req(clients::Method::kPost, clients::Url::parse(config_.token_url())); + req.form(clients::body::UrlEncodedForm::decode(request_body())); + ++exchanges_; + try { + clients::ClientResponse resp = client_.send_blocking(std::move(req)); + Parsed p = parse(resp.status(), resp.body()); + token_ = p.access_token; + expires_at_ = now + p.expires_in; + last_failure_at_ = 0; + return token_; + } catch (const llm::ApiError& e) { + last_failure_at_ = now; + last_failure_ = e.type(); + token_.clear(); + throw; + } catch (const std::exception& e) { + last_failure_at_ = now; + last_failure_ = "UNAVAILABLE"; + token_.clear(); + throw llm::ApiError("azure", 502, "UNAVAILABLE", e.what()); + } +} + +std::size_t EntraTokenSource::exchanges() const { + std::lock_guard lock(mutex_); + return exchanges_; +} + +std::int64_t EntraTokenSource::expires_at() const { + std::lock_guard lock(mutex_); + return expires_at_; +} + +EntraTransport::EntraTransport(std::unique_ptr inner, + std::shared_ptr source) + : inner_(std::move(inner)), source_(std::move(source)) {} + +clients::Task EntraTransport::send(const clients::ClientRequest& request) { + clients::ClientRequest copy = request; + copy.bearer(source_->token()); + co_return co_await inner_->send(copy); +} + +clients::Task EntraTransport::send_stream(const clients::ClientRequest& request, + clients::body::ChunkSink sink) { + clients::ClientRequest copy = request; + copy.bearer(source_->token()); + co_await inner_->send_stream(copy, std::move(sink)); + co_return; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/entra.h b/src/vendors/azure/entra.h new file mode 100644 index 0000000..6de5bdd --- /dev/null +++ b/src/vendors/azure/entra.h @@ -0,0 +1,76 @@ +#pragma once + +// Microsoft Entra ID client-credentials flow (OAuth2, RFC 6749 section 4.4): +// POST //oauth2/v2.0/token with client_id, client_secret, +// scope and grant_type=client_credentials. EntraTokenSource caches the token +// and refreshes it AzureCaps::kTokenSkewSeconds before expiry; after a failed +// exchange it refuses to call again for AzureCaps::kMinExchangeGapSeconds +// (a floor: a broken secret must not hammer the tenant). EntraTransport puts +// the current token on every request that passes through a transport. + +#include +#include +#include +#include +#include + +#include "clients/client.h" +#include "clients/transport.h" +#include "vendors/azure/settings.h" + +namespace molo::azure { + +class EntraTokenSource { + public: + using Clock = std::function; // unix seconds + + EntraTokenSource(EntraConfig config, std::unique_ptr transport, + Clock clock = nullptr); + + // A valid access token, exchanging when none is cached or it is about to + // expire. Throws llm::ApiError (type = the AAD error code, e.g. + // "invalid_client") on a failed exchange. Thread safe. + std::string token(); + // The form body of the exchange request (contains the secret: for tests of + // the wire shape only, never log it). + std::string request_body() const; + const EntraConfig& config() const { return config_; } + std::size_t exchanges() const; + std::int64_t expires_at() const; + + // Parses a token endpoint answer (documented success and error bodies). + struct Parsed { + std::string access_token; + long expires_in = 0; + }; + static Parsed parse(int status, const std::string& body); + + private: + EntraConfig config_; + clients::Client client_; + Clock clock_; + mutable std::mutex mutex_; + std::string token_; + std::int64_t expires_at_ = 0; + std::int64_t last_failure_at_ = 0; + std::string last_failure_; + std::size_t exchanges_ = 0; +}; + +// Transport decorator: sets `Authorization: Bearer ` from the source on +// every request, whatever the caller put there. +class EntraTransport : public clients::Transport { + public: + EntraTransport(std::unique_ptr inner, std::shared_ptr source); + const char* name() const override { return "entra"; } + clients::Task send(const clients::ClientRequest& request) override; + clients::Task send_stream(const clients::ClientRequest& request, + clients::body::ChunkSink sink) override; + void close() override { inner_->close(); } + + private: + std::unique_ptr inner_; + std::shared_ptr source_; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/files.cpp b/src/vendors/azure/files.cpp new file mode 100644 index 0000000..5eb95b0 --- /dev/null +++ b/src/vendors/azure/files.cpp @@ -0,0 +1,87 @@ +#include "vendors/azure/files.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +Json FilesService::upload(const UploadFile& file, const std::string& purpose) { + Check::non_empty("filename", file.filename); + Check::no_control("filename", file.filename); + Check::non_empty("purpose", purpose); + Check::require(!file.data.empty(), "file is empty"); + Check::require(file.data.size() <= kMaxFileBytes, "file over 512 MiB"); + clients::body::MultipartBody form; + form.add_text("purpose", purpose); + form.add_file("file", file.filename, file.content_type, file.data); + return call_multipart(Op::kCreateFile, {}, form); +} + +Json FilesService::get(const std::string& id) { + Check::id("file", id); + return call(Op::kRetrieveFile, {"", {id}}); +} + +Json FilesService::list(const std::string& purpose, const ListParams& params) { + params.validate(); + Query q; + q.add("purpose", purpose); + params.apply(q); + return call(Op::kListFiles, {}, nullptr, &q); +} + +llm::RawResponse FilesService::content(const std::string& id) { + Check::id("file", id); + return call_raw(Op::kDownloadFile, {"", {id}}); +} + +Json FilesService::remove(const std::string& id) { + Check::id("file", id); + return call(Op::kDeleteFile, {"", {id}}); +} + +Json FilesService::import_from_url(const Json& body) { + Check::has_string(body, "purpose"); + Check::has_string(body, "filename"); + Check::has_string(body, "content_url"); + return call(Op::kFilesImport, {}, &body); +} + +Json FilesService::start_upload(const Json& body) { + Check::has_string(body, "purpose"); + Check::has_string(body, "filename"); + Check::has_field(body, "bytes"); + return call(Op::kUploadFileStart, {}, &body); +} + +Json FilesService::add_part(const std::string& upload_id, const UploadFile& part) { + Check::id("upload", upload_id); + Check::require(!part.data.empty(), "part is empty"); + Check::require(part.data.size() <= 64u * 1024u * 1024u, "part over 64 MiB"); + clients::body::MultipartBody form; + form.add_file("data", part.filename.empty() ? "part" : part.filename, part.content_type, part.data); + return call_multipart(Op::kUploadFilePart, {"", {upload_id}}, form); +} + +Json FilesService::complete_upload(const std::string& upload_id, const std::vector& part_ids, + const std::string& md5) { + Check::id("upload", upload_id); + Check::require(!part_ids.empty() && part_ids.size() <= 10000, "part_ids must hold 1..10000 ids"); + Json body = Json::object(); + Json ids = Json::array(); + for (const std::string& p : part_ids) { + Check::id("part", p); + ids.push(Json(p)); + } + body.set("part_ids", ids); + if (!md5.empty()) body.set("md5", Json(md5)); + return call(Op::kUploadFileComplete, {"", {upload_id}}, &body); +} + +Json FilesService::cancel_upload(const std::string& upload_id) { + Check::id("upload", upload_id); + return call(Op::kUploadFileCancel, {"", {upload_id}}); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/files.h b/src/vendors/azure/files.h new file mode 100644 index 0000000..54d6bb2 --- /dev/null +++ b/src/vendors/azure/files.h @@ -0,0 +1,39 @@ +#pragma once + +// Files and multipart Uploads. Upload purposes: fine-tune, assistants, batch, +// user_data, vision, evals (the spec lists the accepted ones per API). + +#include +#include + +#include "vendors/azure/service.h" +#include "vendors/azure/upload_file.h" + +namespace molo::azure { + +class FilesService : public AzureService { + public: + static constexpr std::size_t kMaxFileBytes = 512u * 1024u * 1024u; // llm::Caps::kMaxUploadBytes + + explicit FilesService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // Uploads one file (single request, capped at 512 MiB). purpose is required. + clients::body::Json upload(const UploadFile& file, const std::string& purpose); + clients::body::Json get(const std::string& id); + // GET /files; the dated API takes `purpose`, the v1 API also after/limit/order. + clients::body::Json list(const std::string& purpose = "", const ListParams& params = {}); + llm::RawResponse content(const std::string& id); + clients::body::Json remove(const std::string& id); // destructive + // Dated API only: import from a URL (body: purpose, filename, content_url). + clients::body::Json import_from_url(const clients::body::Json& body); + + // Multipart uploads of large files (dated API). + clients::body::Json start_upload(const clients::body::Json& body); + clients::body::Json add_part(const std::string& upload_id, const UploadFile& part); + clients::body::Json complete_upload(const std::string& upload_id, const std::vector& part_ids, + const std::string& md5 = ""); + clients::body::Json cancel_upload(const std::string& upload_id); // destructive +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/fine_tuning.cpp b/src/vendors/azure/fine_tuning.cpp new file mode 100644 index 0000000..163dc2d --- /dev/null +++ b/src/vendors/azure/fine_tuning.cpp @@ -0,0 +1,89 @@ +#include "vendors/azure/fine_tuning.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +namespace { +Query page(const ListParams& p) { + p.validate(); + Query q; + p.apply(q); + return q; +} +} // namespace + +Json FineTuningService::create(const Json& body) { + Check::has_string(body, "model"); + Check::has_string(body, "training_file"); + return call(Op::kCreateFineTuningJob, {}, &body); +} +Json FineTuningService::get(const std::string& job) { + Check::id("job", job); + return call(Op::kRetrieveFineTuningJob, {"", {job}}); +} +Json FineTuningService::list(const ListParams& params) { + Query q = page(params); + return call(Op::kListPaginatedFineTuningJobs, {}, nullptr, &q); +} +Listing FineTuningService::list_all(const ListParams& params, std::size_t max_pages) { + return walk(Op::kListPaginatedFineTuningJobs, {}, params, Query(), max_pages); +} +Json FineTuningService::events(const std::string& job, const ListParams& params) { + Check::id("job", job); + Query q = page(params); + return call(Op::kListFineTuningEvents, {"", {job}}, nullptr, &q); +} +Json FineTuningService::checkpoints(const std::string& job, const ListParams& params) { + Check::id("job", job); + Query q = page(params); + return call(Op::kListFineTuningJobCheckpoints, {"", {job}}, nullptr, &q); +} +Json FineTuningService::cancel(const std::string& job) { + Check::id("job", job); + return call(Op::kCancelFineTuningJob, {"", {job}}); +} +Json FineTuningService::pause(const std::string& job) { + Check::id("job", job); + return call(Op::kPauseFineTuningJob, {"", {job}}); +} +Json FineTuningService::resume(const std::string& job) { + Check::id("job", job); + return call(Op::kResumeFineTuningJob, {"", {job}}); +} +Json FineTuningService::remove(const std::string& job) { + Check::id("job", job); + return call(Op::kFineTuningDelete, {"", {job}}); +} +Json FineTuningService::copy_checkpoint(const std::string& job, const std::string& checkpoint, + const Json& body) { + Check::id("job", job); + Check::id("checkpoint", checkpoint); + Check::has_string(body, "destinationResourceId"); + Check::has_string(body, "region"); + return call(Op::kFineTuningCopyCheckpoint, {"", {job, checkpoint}}, &body); +} +Json FineTuningService::get_checkpoint_copy(const std::string& job, const std::string& checkpoint) { + Check::id("job", job); + Check::id("checkpoint", checkpoint); + return call(Op::kFineTuningGetCheckpoint, {"", {job, checkpoint}}); +} +Json FineTuningService::list_permissions(const std::string& model, const ListParams& params) { + Check::id("checkpoint model", model); + Query q = page(params); + return call(Op::kListFineTuningCheckpointPermissions, {"", {model}}, nullptr, &q); +} +Json FineTuningService::create_permission(const std::string& model, const Json& body) { + Check::id("checkpoint model", model); + Check::has_field(body, "project_ids"); + return call(Op::kCreateFineTuningCheckpointPermission, {"", {model}}, &body); +} +Json FineTuningService::remove_permission(const std::string& model, const std::string& permission) { + Check::id("checkpoint model", model); + Check::id("permission", permission); + return call(Op::kDeleteFineTuningCheckpointPermission, {"", {model, permission}}); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/fine_tuning.h b/src/vendors/azure/fine_tuning.h new file mode 100644 index 0000000..83d91ed --- /dev/null +++ b/src/vendors/azure/fine_tuning.h @@ -0,0 +1,40 @@ +#pragma once + +// Fine-tuning jobs, events, checkpoints, checkpoint permissions and the +// cross-resource checkpoint copy. Creating a job costs money. + +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class FineTuningService : public AzureService { + public: + explicit FineTuningService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body: model and training_file (required) plus any spec field. + clients::body::Json create(const clients::body::Json& body); + clients::body::Json get(const std::string& job); + clients::body::Json list(const ListParams& params = {}); + Listing list_all(const ListParams& params = {}, std::size_t max_pages = 5); + clients::body::Json events(const std::string& job, const ListParams& params = {}); + clients::body::Json checkpoints(const std::string& job, const ListParams& params = {}); + clients::body::Json cancel(const std::string& job); // destructive + clients::body::Json pause(const std::string& job); + clients::body::Json resume(const std::string& job); + // Dated API only: DELETE a job record. + clients::body::Json remove(const std::string& job); // destructive + // v1 API: copy a checkpoint to another resource (body: destinationResourceId, region). + clients::body::Json copy_checkpoint(const std::string& job, const std::string& checkpoint, + const clients::body::Json& body); + clients::body::Json get_checkpoint_copy(const std::string& job, const std::string& checkpoint); + // Checkpoint permissions (v1 API). + clients::body::Json list_permissions(const std::string& checkpoint_model, const ListParams& params = {}); + clients::body::Json create_permission(const std::string& checkpoint_model, const clients::body::Json& body); + clients::body::Json remove_permission(const std::string& checkpoint_model, + const std::string& permission); // destructive +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/foundry.cpp b/src/vendors/azure/foundry.cpp new file mode 100644 index 0000000..f0848fb --- /dev/null +++ b/src/vendors/azure/foundry.cpp @@ -0,0 +1,55 @@ +#include "vendors/azure/foundry.h" + +#include "llm/caps.h" +#include "vendors/azure/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +llm::Headers FoundryService::headers(const std::string& extra) { + llm::Headers h; + if (extra.empty()) return h; + Check::in_set("extra_parameters", extra, {"error", "drop", "pass-through"}); + h["extra-parameters"] = extra; + return h; +} + +Json FoundryService::info() { return call(Op::kFoundryGetModelInfo); } + +Json FoundryService::chat(const Json& body, const std::string& extra) { + Check::object("body", body); + const Json* m = body.find("messages"); + Check::require(m && m->is_array() && !m->as_array().empty(), "messages is required"); + Check::max_count("messages", m->as_array().size(), llm::Caps::kMaxMessages); + const Json* s = body.find("stream"); + Check::require(!s || (s->is_bool() && !s->as_bool()), "use chat_stream() for stream=true"); + return call(Op::kFoundryGetChatCompletions, {}, &body, nullptr, headers(extra)); +} + +void FoundryService::chat_stream(const Json& body, const ChunkHandler& handler, const std::string& extra) { + Check::object("body", body); + const Json* m = body.find("messages"); + Check::require(m && m->is_array() && !m->as_array().empty(), "messages is required"); + call_stream(Op::kFoundryGetChatCompletions, {}, body, + [&](const llm::SseEvent& ev) { return handler(ev.data); }, headers(extra)); +} + +Json FoundryService::embeddings(const Json& body, const std::string& extra) { + Check::object("body", body); + const Json* in = body.find("input"); + Check::require(in && in->is_array() && !in->as_array().empty(), "input must be a non-empty array of strings"); + Check::max_count("input", in->as_array().size(), AzureCaps::kMaxEmbeddingInputs); + return call(Op::kFoundryGetEmbeddings, {}, &body, nullptr, headers(extra)); +} + +Json FoundryService::image_embeddings(const Json& body, const std::string& extra) { + Check::object("body", body); + const Json* in = body.find("input"); + Check::require(in && in->is_array() && !in->as_array().empty(), "input must be a non-empty array of images"); + Check::max_count("input", in->as_array().size(), AzureCaps::kMaxEmbeddingInputs); + return call(Op::kFoundryGetImageEmbeddings, {}, &body, nullptr, headers(extra)); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/foundry.h b/src/vendors/azure/foundry.h new file mode 100644 index 0000000..dc9c17a --- /dev/null +++ b/src/vendors/azure/foundry.h @@ -0,0 +1,39 @@ +#pragma once + +// Azure AI Foundry model inference (the Azure AI Model Inference API, +// https://.services.ai.azure.com/models, api-version +// 2024-05-01-preview): chat completions, text embeddings, image embeddings +// and model info for the model catalog models (Llama, Mistral, Phi, Cohere, +// DeepSeek, ...). Same credential as the OpenAI resource (api-key header or +// Entra bearer). The `model` body field picks the deployment on a multi-model +// endpoint. + +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class FoundryService : public AzureService { + public: + using ChunkHandler = std::function; // false: stop + + explicit FoundryService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kFoundry) {} + + // GET /info: model_name, model_type, model_provider_name of the endpoint's model. + clients::body::Json info(); + // extra_parameters: "" (service default: error), "error", "drop" or "pass-through" + // (the extra-parameters header of the spec). + clients::body::Json chat(const clients::body::Json& body, const std::string& extra_parameters = ""); + void chat_stream(const clients::body::Json& body, const ChunkHandler& handler, + const std::string& extra_parameters = ""); + clients::body::Json embeddings(const clients::body::Json& body, const std::string& extra_parameters = ""); + clients::body::Json image_embeddings(const clients::body::Json& body, + const std::string& extra_parameters = ""); + + private: + static llm::Headers headers(const std::string& extra_parameters); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/images.cpp b/src/vendors/azure/images.cpp new file mode 100644 index 0000000..9e2b949 --- /dev/null +++ b/src/vendors/azure/images.cpp @@ -0,0 +1,54 @@ +#include "vendors/azure/images.h" + +#include "vendors/azure/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; +using clients::body::MultipartBody; + +namespace { +void add_file(MultipartBody& form, const std::string& field, const UploadFile& f) { + Check::non_empty("image filename", f.filename); + Check::no_control("filename", f.filename); + Check::require(!f.data.empty(), "image file is empty"); + Check::require(f.data.size() <= AzureCaps::kMaxImageFileBytes, "image file over 50 MiB"); + form.add_file(field, f.filename, f.content_type, f.data); +} +} // namespace + +Json ImagesService::generate(const std::string& deployment, const Json& body) { + Check::deployment(deployment); + Check::object("body", body); + Check::has_string(body, "prompt"); + Check::max_chars("prompt", body.find("prompt")->as_string(), AzureCaps::kMaxImagePromptChars); + return call(Op::kCreateImage, {deployment, {}}, &body); +} + +Json ImagesService::edit(const std::string& deployment, const std::string& prompt, + const std::vector& images, const std::optional& mask, + const std::map& fields) { + Check::deployment(deployment); + Check::non_empty("prompt", prompt); + Check::max_chars("prompt", prompt, AzureCaps::kMaxImagePromptChars); + Check::require(!images.empty() && images.size() <= 16, "images must hold 1..16 files"); + MultipartBody form; + form.add_text("prompt", prompt); + for (const UploadFile& f : images) add_file(form, images.size() > 1 ? "image[]" : "image", f); + if (mask) add_file(form, "mask", *mask); + for (const auto& [k, v] : fields) form.add_text(k, v); + return call_multipart(Op::kCreateImageEdit, {deployment, {}}, form); +} + +Json ImagesService::variation(const std::string& deployment, const UploadFile& image, + const std::map& fields) { + Check::deployment(deployment); + MultipartBody form; + add_file(form, "image", image); + for (const auto& [k, v] : fields) form.add_text(k, v); + form.add_text("model", deployment); + return call_multipart(Op::kCreateImageVariation, {}, form); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/images.h b/src/vendors/azure/images.h new file mode 100644 index 0000000..c7bc5aa --- /dev/null +++ b/src/vendors/azure/images.h @@ -0,0 +1,35 @@ +#pragma once + +// Images on a deployment (gpt-image-1, dall-e-3): generations (JSON), edits +// and variations (multipart; the spec of the dated API lists JSON for edits, +// the service and every official SDK send multipart, see SPEC Unverified). + +#include +#include +#include +#include + +#include "vendors/azure/service.h" +#include "vendors/azure/upload_file.h" + +namespace molo::azure { + +class ImagesService : public AzureService { + public: + explicit ImagesService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body: prompt (required, <= 32000) plus any field of the spec (n, size, quality...). + clients::body::Json generate(const std::string& deployment, const clients::body::Json& body); + // fields: text form fields of the spec (n, size, quality, response_format...). + // images: 1..16 files (sent as image[] when more than one); mask optional. + clients::body::Json edit(const std::string& deployment, const std::string& prompt, + const std::vector& images, + const std::optional& mask = std::nullopt, + const std::map& fields = {}); + // v1 API only (the dated documents have no variations operation). + clients::body::Json variation(const std::string& deployment, const UploadFile& image, + const std::map& fields = {}); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/models.cpp b/src/vendors/azure/models.cpp new file mode 100644 index 0000000..69d1aa0 --- /dev/null +++ b/src/vendors/azure/models.cpp @@ -0,0 +1,30 @@ +#include "vendors/azure/models.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +Json ModelsService::list() { return call(Op::kListModels); } + +std::vector ModelsService::ids() { + std::vector out; + Json doc = list(); + if (const Json* data = doc.find("data"); data && data->is_array()) + for (const Json& m : data->as_array()) + if (const Json* id = m.find("id"); id && id->is_string()) out.push_back(id->as_string()); + return out; +} + +Json ModelsService::get(const std::string& id) { + Check::id("model", id); + return call(Op::kRetrieveModel, {"", {id}}); +} + +Json ModelsService::remove(const std::string& id) { + Check::id("model", id); + return call(Op::kDeleteModel, {"", {id}}); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/models.h b/src/vendors/azure/models.h new file mode 100644 index 0000000..d843b0b --- /dev/null +++ b/src/vendors/azure/models.h @@ -0,0 +1,26 @@ +#pragma once + +// Models available to the resource (base models and fine-tuned ones). On +// Azure this lists models, not deployments: deployments are managed through +// the Azure management plane (ARM), which this package does not call. + +#include +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class ModelsService : public AzureService { + public: + explicit ModelsService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + clients::body::Json list(); + std::vector ids(); + clients::body::Json get(const std::string& id); + // v1 API only: deletes a fine-tuned model. Destructive, no default. + clients::body::Json remove(const std::string& id); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/operations.cpp b/src/vendors/azure/operations.cpp new file mode 100644 index 0000000..b377c31 --- /dev/null +++ b/src/vendors/azure/operations.cpp @@ -0,0 +1,90 @@ +#include "vendors/azure/operations.h" + +#include + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +OperationsService::OperationsService(std::shared_ptr azure) + : azure_(azure), + openai_(azure, Surface::kOpenAi), + foundry_(azure, Surface::kFoundry), + safety_(azure, Surface::kSafety) {} + +AzureService& OperationsService::gate(Surface s) const { + switch (s) { + case Surface::kFoundry: return foundry_; + case Surface::kSafety: return safety_; + case Surface::kOpenAi: break; + } + return openai_; +} + +const CallSpec& OperationsService::lookup(const std::string& name) const { + const CallSpec* c = Calls::find(name); + if (!c) Check::fail("unknown operation '" + name + "'"); + return *c; +} + +bool OperationsService::destructive(const CallSpec& c) { + if (std::strcmp(c.method, "DELETE") == 0) return true; + std::string n = c.name; + for (const char* p : {"Cancel", "Delete", "Reject", "Hangup", "Pause", "Refer", "Remove"}) + if (n.find(p) != std::string::npos) return true; + return false; +} + +bool OperationsService::generic(const CallSpec& c) { + for (const SideSpec* s : {&c.v1, &c.dated}) { + if (!s->present()) continue; + std::string content = s->content; + if (content == "multipart") return false; + } + std::string n = c.name; + // Binary or streaming answers belong to the typed services. + return n != "CreateSpeech" && n != "DownloadFile" && n != "VideosRetrieveContent" && + n != "RetrieveContainerFileContent" && n != "CreateRealtimeCall"; +} + +Query OperationsService::to_query(const std::map& query) const { + Query q; + for (const auto& [k, v] : query) q.add(k, v); + return q; +} + +OperationsService::Plan OperationsService::plan(const std::string& name, const Target& target, + const Json* body, + const std::map& query) const { + const CallSpec& c = lookup(name); + if (!generic(c)) Check::fail(name + " needs its typed service (multipart or binary)"); + AzureService& g = gate(c.surface); + Plan p; + p.method = c.method; + p.destructive = destructive(c); + Route r = g.route(c.op, target); + p.side = r.side == Side::kV1 ? "v1" : "dated"; + Query q = to_query(query); + p.path = g.request_path(c.op, target, &q); + if (body) { + if (std::strcmp(c.side(r.side).content, "none") == 0) Check::fail(name + " takes no request body"); + p.body = g.prepare_body(c.op, target, *body); + p.has_body = true; + } else if (c.deployment && std::strstr(c.side(r.side).content, "json") && std::strcmp(c.method, "POST") == 0) { + p.body = g.prepare_body(c.op, target, Json::object()); + p.has_body = true; + } + return p; +} + +Json OperationsService::invoke(const std::string& name, const Target& target, const Json* body, + const std::map& query) { + const CallSpec& c = lookup(name); + if (!generic(c)) Check::fail(name + " needs its typed service (multipart or binary)"); + Query q = to_query(query); + return gate(c.surface).call(c.op, target, body, &q); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/operations.h b/src/vendors/azure/operations.h new file mode 100644 index 0000000..52b0ce9 --- /dev/null +++ b/src/vendors/azure/operations.h @@ -0,0 +1,53 @@ +#pragma once + +// OperationsService: every operation of the call table by name, for the ones +// that have no typed class (containers, conversations, evals, graders, +// videos, checkpoint tools) and for the demo's generic route. Same gateway as +// the typed services: ids validated, fields and query names checked against +// the spec, api side and api-version chosen from the settings. + +#include +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class OperationsService { + public: + explicit OperationsService(std::shared_ptr azure); + + // JSON in, JSON out. `body` may be null. `query` values are strings. + // Throws ApiError::invalid for an unknown name, wrong ids, unknown fields or + // query names, or an operation that needs multipart / streams / answers bytes + // (use the typed service for those). + clients::body::Json invoke(const std::string& name, const Target& target, + const clients::body::Json* body = nullptr, + const std::map& query = {}); + // Route and body exactly as invoke() would send them; no traffic. + struct Plan { + std::string method; + std::string path; // with query and api-version + clients::body::Json body; + bool has_body = false; + bool destructive = false; + std::string side; // "v1" or "dated" + }; + Plan plan(const std::string& name, const Target& target, const clients::body::Json* body = nullptr, + const std::map& query = {}) const; + + // DELETE, cancel, reject, hangup, pause, remove: the demo asks for confirm. + static bool destructive(const CallSpec& spec); + // Calls the generic JSON path can make (not multipart-only, not binary). + static bool generic(const CallSpec& spec); + + private: + AzureService& gate(Surface s) const; + const CallSpec& lookup(const std::string& name) const; + Query to_query(const std::map& query) const; + + std::shared_ptr azure_; + mutable AzureService openai_, foundry_, safety_; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/realtime.cpp b/src/vendors/azure/realtime.cpp new file mode 100644 index 0000000..a8a9c72 --- /dev/null +++ b/src/vendors/azure/realtime.cpp @@ -0,0 +1,209 @@ +#include "vendors/azure/realtime.h" + +#include + +#include "clients/url.h" +#include "google/live/ws_error.h" +#include "google/live/ws_handshake.h" +#include "google/live/ws_url.h" +#include "llm/error.h" +#include "vendors/azure/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; +namespace live = google::live; + +namespace { +long long now_ms() { + return std::chrono::duration_cast( + 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("azure", status, type, e.what()); +} +} // namespace + +Json RealtimeService::create_client_secret(const Json& body) { + if (body.is_object() && body.find("expires_after")) { + const Json* ea = body.find("expires_after"); + const Json* secs = ea->is_object() ? ea->find("seconds") : nullptr; + Check::require(secs && secs->is_number(), "expires_after.seconds is required"); + Check::range("expires_after.seconds", secs->as_number(), 10, AzureCaps::kMaxClientSecretSeconds); + } + return call(Op::kCreateRealtimeClientSecret, {}, &body); +} + +Json RealtimeService::create_session(const Json& body) { + Op op = azure_->settings().api_version.is_dated() ? Op::kCreateRealtimeSessionDated : Op::kCreateRealtimeSession; + return call(op, {}, &body); +} + +Json RealtimeService::create_transcription_session(const Json& body) { + Op op = azure_->settings().api_version.is_dated() ? Op::kCreateTranscriptionRealtimeSession + : Op::kCreateRealtimeTranscriptionSession; + return call(op, {}, &body); +} + +Json RealtimeService::accept_call(const std::string& call_id, const Json& body) { + Check::id("call", call_id); + Check::has_string(body, "type"); + return call(Op::kAcceptRealtimeCall, {"", {call_id}}, &body); +} + +Json RealtimeService::refer_call(const std::string& call_id, const std::string& target_uri) { + Check::id("call", call_id); + Check::non_empty("target_uri", target_uri); + Check::no_control("target_uri", target_uri); + Json body = Json::object(); + body.set("target_uri", Json(target_uri)); + return call(Op::kReferRealtimeCall, {"", {call_id}}, &body); +} + +Json RealtimeService::reject_call(const std::string& call_id, int status_code) { + Check::id("call", call_id); + Check::range("status_code", status_code, 400, 699); + Json body = Json::object(); + body.set("status_code", Json(status_code)); + return call(Op::kRejectRealtimeCall, {"", {call_id}}, &body); +} + +Json RealtimeService::hangup_call(const std::string& call_id) { + Check::id("call", call_id); + return call(Op::kHangupRealtimeCall, {"", {call_id}}); +} + +std::string RealtimeUrl::build(const Settings& s, const std::string& deployment) { + Check::deployment(deployment); + Check::require(!s.endpoint.empty(), "endpoint is not set"); + std::string host = s.endpoint; + if (host.rfind("https://", 0) == 0) host = "wss://" + host.substr(8); + else if (host.rfind("http://", 0) == 0) host = "ws://" + host.substr(7); + std::string dep = clients::Url::encode_component(deployment); + if (s.api_version.is_dated()) + return host + "/openai/realtime?api-version=" + clients::Url::encode_component(s.api_version.value()) + + "&deployment=" + dep; + std::string url = host + "/openai/v1/realtime?model=" + dep; + if (!s.api_version.v1_query().empty()) url += "&api-version=" + s.api_version.v1_query(); + return url; +} + +RealtimeSocket::RealtimeSocket(std::shared_ptr azure, live::TlsFactory tls) + : azure_(std::move(azure)), tls_(std::move(tls)) {} + +RealtimeSocket::~RealtimeSocket() { + try { + close(); + } catch (...) { + } +} + +void RealtimeSocket::connect(const std::string& deployment) { + Check::require(!ws_, "socket already connected"); + const Settings& s = azure_->settings(); + if (s.endpoint.empty()) throw llm::NotConfiguredError("azure", Settings::kEndpointEnv); + if (s.auth() == AuthKind::kNone) throw llm::NotConfiguredError("azure", Settings::kKeyEnv); + std::string url_text = RealtimeUrl::build(s, deployment); + live::WsHeaders headers; + if (s.auth() == AuthKind::kApiKey) headers.emplace_back("api-key", s.api_key); + else headers.emplace_back("Authorization", "Bearer " + azure_->tokens()->token()); + try { + live::WsUrl url = live::WsUrl::parse(url_text); + auto client = std::make_unique(live::open_stream(url, 10000, 10000, tls_)); + client->handshake(url, headers, 10000); + client->set_read_timeout_ms(250); + ws_ = std::move(client); + } catch (const live::WsError& e) { + rethrow(e); + } +} + +void RealtimeSocket::send(const Json& event) { + Check::require(ws_ != nullptr, "socket is not connected"); + const Json* t = event.is_object() ? event.find("type") : nullptr; + Check::require(t && t->is_string() && !t->as_string().empty(), "event needs a string type"); + std::string text = event.dump(); + Check::require(text.size() <= kMaxEventBytes, "event over the byte cap"); + try { + ws_->send_text(text); + } catch (const live::WsError& e) { + rethrow(e); + } +} + +RealtimeStats RealtimeSocket::run(const Sink& sink, int seconds) { + Check::require(ws_ != nullptr, "socket is not connected"); + Check::range("seconds", seconds, 1, kMaxSessionSeconds); + RealtimeStats stats; + long long deadline = now_ms() + static_cast(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(); + Json ev; + try { + ev = Json::parse(m->data); + } catch (const std::exception&) { + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "server event is not JSON"); + } + if (!sink(ev)) { + stats.stop_reason = "sink_stopped"; + return stats; + } + if (stats.events >= kMaxSessionEvents) { + stats.stop_reason = "event_cap"; + return stats; + } + if (stats.bytes >= kMaxSessionBytes) { + stats.stop_reason = "byte_cap"; + return stats; + } + } + } catch (const live::WsError& e) { + rethrow(e); + } +} + +void RealtimeSocket::close() { + if (!ws_) return; + try { + ws_->close(1000, ""); + } catch (...) { + } + ws_->shutdown(); + ws_.reset(); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/realtime.h b/src/vendors/azure/realtime.h new file mode 100644 index 0000000..a829b71 --- /dev/null +++ b/src/vendors/azure/realtime.h @@ -0,0 +1,85 @@ +#pragma once + +// Realtime (gpt-realtime, gpt-4o-realtime-preview): the REST creators of +// sessions and ephemeral client secrets, the WebSocket URL builder, and a +// small WebSocket session over the RFC 6455 client of src/google/live +// (WsClient, reused, not copied). +// +// v1 wss:///openai/v1/realtime?model= +// dated wss:///openai/realtime?api-version=&deployment= +// The credential travels in a header (api-key, or Authorization: Bearer for +// Entra), never in the URL. + +#include +#include +#include + +#include "google/live/ws_client.h" +#include "google/live/ws_stream.h" +#include "vendors/azure/service.h" + +namespace molo::azure { + +class RealtimeService : public AzureService { + public: + explicit RealtimeService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // Ephemeral client secret for browsers and devices (v1 API). The answer + // holds the secret in `value`: hand it to the client, never log it. + // body: session, expires_after ({"anchor":"created_at","seconds":10..7200}). + clients::body::Json create_client_secret(const clients::body::Json& body = clients::body::Json::object()); + // Session creator: /realtime/sessions (v1) or /realtimeapi/sessions (dated). + clients::body::Json create_session(const clients::body::Json& body); + clients::body::Json create_transcription_session( + const clients::body::Json& body = clients::body::Json::object()); + // SIP / WebRTC call control (v1 API). Destructive ones have no defaults. + clients::body::Json accept_call(const std::string& call_id, const clients::body::Json& body); + clients::body::Json refer_call(const std::string& call_id, const std::string& target_uri); + clients::body::Json reject_call(const std::string& call_id, int status_code); + clients::body::Json hangup_call(const std::string& call_id); +}; + +class RealtimeUrl { + public: + // The WebSocket URL without credentials. Throws on a bad deployment. + static std::string build(const Settings& settings, const std::string& deployment); +}; + +struct RealtimeStats { + std::size_t events = 0; + std::size_t bytes = 0; + std::string stop_reason; // sink_stopped, closed, timeout, event_cap, byte_cap + int close_code = 0; +}; + +class RealtimeSocket { + public: + static constexpr std::size_t kMaxEventBytes = 1024 * 1024; + static constexpr std::size_t kMaxSessionEvents = 100000; + static constexpr std::size_t kMaxSessionBytes = 64u * 1024u * 1024u; + static constexpr int kMaxSessionSeconds = 3600; + + using Sink = std::function; // false: stop + + explicit RealtimeSocket(std::shared_ptr azure, google::live::TlsFactory tls = nullptr); + ~RealtimeSocket(); + + bool connected() const { return ws_ != nullptr; } + // Connects and upgrades. Throws NotConfiguredError (503) before any traffic + // when there is no credential or endpoint. + void connect(const std::string& deployment); + // Sends one client event (a JSON object with a string `type`). + void send(const clients::body::Json& event); + // Receives until the sink returns false, the server closes, `seconds` pass + // (1..3600) or a cap is hit. + RealtimeStats run(const Sink& sink, int seconds); + void close(); + + private: + std::shared_ptr azure_; + google::live::TlsFactory tls_; + std::unique_ptr ws_; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/responses.cpp b/src/vendors/azure/responses.cpp new file mode 100644 index 0000000..1792f2d --- /dev/null +++ b/src/vendors/azure/responses.cpp @@ -0,0 +1,58 @@ +#include "vendors/azure/responses.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +void ResponsesService::validate(const Json& body) { + Check::object("body", body); + Check::has_string(body, "model"); + Check::deployment(body.find("model")->as_string()); +} + +Json ResponsesService::create(const Json& body) { + validate(body); + const Json* s = body.find("stream"); + Check::require(!s || (s->is_bool() && !s->as_bool()), "use stream() for stream=true"); + return call(Op::kCreateResponse, {}, &body); +} + +void ResponsesService::stream(const Json& body, const EventHandler& handler) { + validate(body); + call_stream(Op::kCreateResponse, {}, body, handler); +} + +Json ResponsesService::retrieve(const std::string& id, const std::vector& include) { + Check::id("response", id); + Query q; + q.add_list("include", include); + return call(Op::kGetResponse, {"", {id}}, nullptr, &q); +} + +Json ResponsesService::remove(const std::string& id) { + Check::id("response", id); + return call(Op::kDeleteResponse, {"", {id}}); +} + +Json ResponsesService::cancel(const std::string& id) { + Check::id("response", id); + return call(Op::kCancelResponse, {"", {id}}); +} + +Json ResponsesService::input_items(const std::string& id, const ListParams& params) { + Check::id("response", id); + params.validate(); + Query q; + params.apply(q); + return call(Op::kListInputItems, {"", {id}}, nullptr, &q); +} + +Listing ResponsesService::input_items_all(const std::string& id, const ListParams& params, + std::size_t max_pages) { + Check::id("response", id); + return walk(Op::kListInputItems, {"", {id}}, params, Query(), max_pages); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/responses.h b/src/vendors/azure/responses.h new file mode 100644 index 0000000..bc4bd23 --- /dev/null +++ b/src/vendors/azure/responses.h @@ -0,0 +1,37 @@ +#pragma once + +// Responses API. On Azure the `model` field of the body is the deployment +// name on both APIs (v1: /openai/v1/responses, dated: /openai/responses). + +#include +#include +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class ResponsesService : public AzureService { + public: + using EventHandler = std::function; // false: stop + + explicit ResponsesService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + // body: model (the deployment, required), input, plus any spec field. + clients::body::Json create(const clients::body::Json& body); + void stream(const clients::body::Json& body, const EventHandler& handler); + clients::body::Json retrieve(const std::string& id, const std::vector& include = {}); + // DELETE: removes the stored response. No default. + clients::body::Json remove(const std::string& id); + // Only responses created with background=true can be cancelled. + clients::body::Json cancel(const std::string& id); + clients::body::Json input_items(const std::string& id, const ListParams& params = {}); + Listing input_items_all(const std::string& id, const ListParams& params = {}, + std::size_t max_pages = 5); + + private: + static void validate(const clients::body::Json& body); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/safety.cpp b/src/vendors/azure/safety.cpp new file mode 100644 index 0000000..9546ae0 --- /dev/null +++ b/src/vendors/azure/safety.cpp @@ -0,0 +1,121 @@ +#include "vendors/azure/safety.h" + +#include "vendors/azure/caps.h" +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +void SafetyService::check_blocklist_name(const std::string& name) { + Check::require(!name.empty() && name.size() <= 64, "blocklist name must be 1..64 characters"); + for (char c : name) + if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '.' || + c == '_' || c == '~' || c == '-')) + Check::fail("blocklist name allows only 0-9 A-Z a-z . _ ~ -"); +} + +Json SafetyService::analyze_text(const Json& body) { + Check::has_string(body, "text"); + Check::max_chars("text", body.find("text")->as_string(), AzureCaps::kMaxSafetyTextChars); + return call(Op::kSafetyTextOperationsAnalyzeText, {}, &body); +} + +Json SafetyService::analyze_image(const Json& body) { + Check::object("body", body); + const Json* img = body.find("image"); + Check::require(img && img->is_object(), "image is required"); + const Json* content = img->find("content"); + Check::require(content || img->find("blobUrl"), "image needs content (base64) or blobUrl"); + if (content && content->is_string()) + Check::require(content->as_string().size() <= AzureCaps::kMaxSafetyImageBytes * 4 / 3 + 8, + "image over 7.5 MiB"); + return call(Op::kSafetyImageOperationsAnalyzeImage, {}, &body); +} + +Json SafetyService::shield_prompt(const Json& body) { + Check::object("body", body); + Check::require(body.find("userPrompt") || body.find("documents"), "userPrompt or documents is required"); + if (const Json* d = body.find("documents"); d && d->is_array()) + Check::max_count("documents", d->as_array().size(), AzureCaps::kMaxShieldDocuments); + return call(Op::kSafetyTextOperationsShieldPrompt, {}, &body); +} + +Json SafetyService::detect_protected_material(const std::string& text) { + Check::non_empty("text", text); + Check::max_chars("text", text, AzureCaps::kMaxSafetyTextChars); + Json body = Json::object(); + body.set("text", Json(text)); + return call(Op::kSafetyTextOperationsDetectTextProtectedMaterial, {}, &body); +} + +Json SafetyService::list_blocklists() { return call(Op::kSafetyTextBlocklistsListTextBlocklists); } + +Json SafetyService::get_blocklist(const std::string& name) { + check_blocklist_name(name); + return call(Op::kSafetyTextBlocklistsGetTextBlocklist, {"", {name}}); +} + +Json SafetyService::create_or_update_blocklist(const std::string& name, const std::string& description) { + check_blocklist_name(name); + Check::max_chars("description", description, 1024); + Json body = Json::object(); + if (!description.empty()) body.set("description", Json(description)); + Op op = Op::kSafetyTextBlocklistsCreateOrUpdateTextBlocklist; + Json prepared = prepare_body(op, {"", {name}}, body); + std::string text = prepared.dump(); + llm::RawResponse r = client_->request_raw(Calls::method(op), request_path(op, {"", {name}}), &text, + "application/merge-patch+json"); + return r.body.find_first_not_of(" \t\r\n") == std::string::npos ? Json::object() : Json::parse(r.body); +} + +Json SafetyService::remove_blocklist(const std::string& name) { + check_blocklist_name(name); + return call(Op::kSafetyTextBlocklistsDeleteTextBlocklist, {"", {name}}); +} + +Json SafetyService::add_blocklist_items(const std::string& name, const Json& items) { + check_blocklist_name(name); + Check::require(items.is_array() && !items.as_array().empty(), "items must be a non-empty array"); + Check::max_count("items", items.as_array().size(), AzureCaps::kMaxBlocklistItems); + for (const Json& it : items.as_array()) { + Check::has_string(it, "text"); + Check::max_chars("item text", it.find("text")->as_string(), AzureCaps::kMaxBlocklistItemChars); + } + Json body = Json::object(); + body.set("blocklistItems", items); + return call(Op::kSafetyTextBlocklistsAddOrUpdateBlocklistItems, {"", {name}}, &body); +} + +Json SafetyService::remove_blocklist_items(const std::string& name, const std::vector& item_ids) { + check_blocklist_name(name); + Check::require(!item_ids.empty(), "item_ids must not be empty"); + Check::max_count("item_ids", item_ids.size(), AzureCaps::kMaxBlocklistItems); + Json ids = Json::array(); + for (const std::string& i : item_ids) { + Check::id("item", i); + ids.push(Json(i)); + } + Json body = Json::object(); + body.set("blocklistItemIds", ids); + return call(Op::kSafetyTextBlocklistsRemoveBlocklistItems, {"", {name}}, &body); +} + +Json SafetyService::list_blocklist_items(const std::string& name, std::optional maxpagesize, + std::optional skip) { + check_blocklist_name(name); + if (maxpagesize) Check::range("maxpagesize", *maxpagesize, 1, 1000); + if (skip) Check::range("skip", *skip, 0, 1000000); + Query q; + q.add_int("maxpagesize", maxpagesize); + q.add_int("skip", skip); + return call(Op::kSafetyTextBlocklistsListTextBlocklistItems, {"", {name}}, nullptr, &q); +} + +Json SafetyService::get_blocklist_item(const std::string& name, const std::string& item_id) { + check_blocklist_name(name); + Check::id("item", item_id); + return call(Op::kSafetyTextBlocklistsGetTextBlocklistItem, {"", {name, item_id}}); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/safety.h b/src/vendors/azure/safety.h new file mode 100644 index 0000000..8ae26fb --- /dev/null +++ b/src/vendors/azure/safety.h @@ -0,0 +1,47 @@ +#pragma once + +// Azure AI Content Safety (api-version 2024-09-01): analyze text and images +// for harm categories (Hate, SelfHarm, Sexual, Violence), Prompt Shields, +// protected material detection, and custom text blocklists. Own endpoint +// (https://.cognitiveservices.azure.com/contentsafety) and key +// header (Ocp-Apim-Subscription-Key), or an Entra bearer token. + +#include +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class SafetyService : public AzureService { + public: + explicit SafetyService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kSafety) {} + + // body: text (required, <= 10000 chars), categories, blocklistNames, + // haltOnBlocklistHit, outputType. + clients::body::Json analyze_text(const clients::body::Json& body); + // body: image ({content: base64} or {blobUrl}), categories, outputType. + clients::body::Json analyze_image(const clients::body::Json& body); + // Prompt Shields: userPrompt and/or documents (<= 5). + clients::body::Json shield_prompt(const clients::body::Json& body); + clients::body::Json detect_protected_material(const std::string& text); + + // Blocklists. Names: 1..64 of [0-9A-Za-z._~-]. + clients::body::Json list_blocklists(); + clients::body::Json get_blocklist(const std::string& name); + // PATCH (merge-patch) creates or updates; description <= 1024 chars. + clients::body::Json create_or_update_blocklist(const std::string& name, const std::string& description); + clients::body::Json remove_blocklist(const std::string& name); // destructive + // items: [{description, text}] (1..100, text <= 128 chars). + clients::body::Json add_blocklist_items(const std::string& name, const clients::body::Json& items); + clients::body::Json remove_blocklist_items(const std::string& name, const std::vector& item_ids); + // One page; `truncated` stays visible in the answer as nextLink. + clients::body::Json list_blocklist_items(const std::string& name, std::optional maxpagesize = std::nullopt, + std::optional skip = std::nullopt); + clients::body::Json get_blocklist_item(const std::string& name, const std::string& item_id); + + static void check_blocklist_name(const std::string& name); +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/service.cpp b/src/vendors/azure/service.cpp new file mode 100644 index 0000000..d01b59b --- /dev/null +++ b/src/vendors/azure/service.cpp @@ -0,0 +1,291 @@ +#include "vendors/azure/service.h" + +#include + +#include "clients/url.h" +#include "llm/error.h" +#include "vendors/azure/caps.h" + +namespace molo::azure { + +using clients::body::Json; + +namespace { +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("azure", why); } + +std::string strip_brackets(const std::string& name) { + if (name.size() > 2 && name.compare(name.size() - 2, 2, "[]") == 0) return name.substr(0, name.size() - 2); + return name; +} + +std::string encode_name(const std::string& k) { + std::string name; + for (char c : k) + name += (c == '[' || c == ']') ? std::string(1, c) : clients::Url::encode_component(std::string(1, c)); + return name; +} + +bool has_json(const char* content) { return std::strstr(content, "json") != nullptr; } +} // namespace + +Query& Query::add(const std::string& name, const std::string& value) { + if (value.empty()) return *this; + for (char c : value) + if (static_cast(c) < 0x20) bad("query value has control characters"); + if (value.size() > 1024) bad("query value is too long"); + pairs_.emplace_back(name, value); + return *this; +} + +Query& Query::add_int(const std::string& name, const std::optional& value) { + if (value) add(name, std::to_string(*value)); + return *this; +} + +Query& Query::add_bool(const std::string& name, const std::optional& value) { + if (value) add(name, *value ? "true" : "false"); + return *this; +} + +Query& Query::add_list(const std::string& name, const std::vector& values) { + if (values.size() > 16) bad(name + ": too many values"); + for (const std::string& v : values) { + if (v.empty()) bad(name + ": empty value"); + pairs_.emplace_back(name + "[]", v); + } + return *this; +} + +void ListParams::validate() const { + if (limit && (*limit < 1 || *limit > AzureCaps::kMaxPageLimit)) + bad("limit must be 1.." + std::to_string(AzureCaps::kMaxPageLimit)); + if (!order.empty() && order != "asc" && order != "desc") bad("order must be asc or desc"); + if (!after.empty() && !llm::Service::valid_id(after)) bad("bad after cursor"); + if (!before.empty() && !llm::Service::valid_id(before)) bad("bad before cursor"); +} + +void ListParams::apply(Query& q) const { + q.add_int("limit", limit); + q.add("order", order); + q.add("after", after); + q.add("before", before); +} + +AzureService::AzureService(std::shared_ptr azure, Surface surface) + : llm::Service(azure->api(surface)), azure_(azure), surface_(surface) {} + +void AzureService::guard(Op op) const { + if (Calls::spec(op).surface != surface_) + bad(std::string(Calls::spec(op).name) + " does not belong to this service"); +} + +Route AzureService::route(Op op, const Target& target) const { + const CallSpec& c = Calls::spec(op); + const Settings& s = azure_->settings(); + Route r; + switch (c.surface) { + case Surface::kFoundry: + r.side = Side::kDated; + r.version = s.foundry_version; + break; + case Surface::kSafety: + r.side = Side::kDated; + r.version = s.safety_version; + break; + case Surface::kOpenAi: { + const ApiVersion& v = s.api_version; + Side pref = v.is_dated() ? Side::kDated : Side::kV1; + Side other = pref == Side::kDated ? Side::kV1 : Side::kDated; + r.side = Calls::has(op, pref) ? pref : other; + if (r.side == Side::kV1) r.version = v.is_v1_family() ? v.v1_query() : "preview"; + else r.version = v.is_dated() ? v.value() : ApiVersion::kDatedFallback; + break; + } + } + if (c.deployment && r.side == Side::kDated) { + if (target.deployment.empty() || target.deployment.size() > AzureCaps::kMaxDeploymentChars) + bad("a deployment name of 1.." + std::to_string(AzureCaps::kMaxDeploymentChars) + " characters is required"); + } + r.path = Calls::prefix(op, r.side) + Calls::render(op, r.side, target.ids, target.deployment); + return r; +} + +std::string AzureService::request_path(Op op, const Target& target, const Query* query) const { + Route r = route(op, target); + std::string out = r.path; + char sep = '?'; + if (query) { + for (const auto& [k, v] : query->pairs()) { + if (!Calls::accepts_query(op, r.side, k)) + bad("query '" + k + "' is not a parameter of " + Calls::spec(op).side(r.side).op_id + + " on the " + (r.side == Side::kV1 ? "v1" : "dated") + " API"); + out += sep; + out += encode_name(k) + "=" + clients::Url::encode_component(v); + sep = '&'; + } + } + if (!r.version.empty()) { + out += sep; + out += "api-version=" + clients::Url::encode_component(r.version); + } + return out; +} + +Json AzureService::prepare_body(Op op, const Target& target, const Json& body) const { + if (!body.is_object()) bad("body must be a JSON object"); + Route r = route(op, target); + const CallSpec& c = Calls::spec(op); + Json out = body; + Json::Object& obj = out.as_object(); + if (c.deployment) { + auto it = obj.find("model"); + if (it != obj.end()) { + if (!it->second.is_string() || it->second.as_string() != target.deployment) + bad("model must equal the deployment name (or be left out)"); + if (r.side == Side::kDated) obj.erase(it); + } else if (r.side == Side::kV1) { + if (!llm::Service::valid_id(target.deployment) || + target.deployment.size() > AzureCaps::kMaxDeploymentChars) + bad("a deployment name is required"); + obj["model"] = Json(target.deployment); + } + } + for (const auto& [k, v] : obj) { + (void)v; + if (!Calls::accepts_field(op, r.side, k)) + bad("field '" + k + "' is not accepted by " + c.side(r.side).op_id + " on the " + + (r.side == Side::kV1 ? "v1" : "dated") + " API"); + } + return out; +} + +clients::body::MultipartBody AzureService::prepare_form( + Op op, const Target& target, const clients::body::MultipartBody& form) const { + Route r = route(op, target); + const CallSpec& c = Calls::spec(op); + clients::body::MultipartBody out = form; + bool has_model = false; + for (const auto& part : form.parts()) { + std::string name = strip_brackets(part.name); + if (name == "model" && c.deployment) { + has_model = true; + if (part.data != target.deployment) bad("model must equal the deployment name (or be left out)"); + if (r.side == Side::kDated) bad("model is not a form field of the dated API (the deployment is in the URL)"); + continue; + } + if (!Calls::accepts_field(op, r.side, name)) + bad("form field '" + name + "' is not accepted by " + c.side(r.side).op_id); + } + if (c.deployment && r.side == Side::kV1 && !has_model) out.add_text("model", target.deployment); + return out; +} + +Json AzureService::call(Op op, const Target& target, const Json* body, const Query* query, + const llm::Headers& headers) { + guard(op); + const CallSpec& c = Calls::spec(op); + Route r = route(op, target); + std::string path = request_path(op, target, query); + Json prepared; + const Json* send = nullptr; + if (body) { + if (std::strcmp(c.side(r.side).content, "none") == 0) + bad(std::string(c.name) + " takes no request body"); + prepared = prepare_body(op, target, *body); + send = &prepared; + } else if (has_json(c.side(r.side).content) && std::strcmp(c.method, "POST") == 0 && + c.deployment) { + prepared = prepare_body(op, target, Json::object()); + send = &prepared; + } + return client_->request(Calls::method(op), path, send, headers); +} + +llm::RawResponse AzureService::call_raw(Op op, const Target& target, const Query* query, + const llm::Headers& headers) { + guard(op); + return client_->request_raw(Calls::method(op), request_path(op, target, query), nullptr, "", + headers); +} + +llm::RawResponse AzureService::call_json_raw(Op op, const Target& target, const Json& body, + const llm::Headers& headers) { + guard(op); + Json prepared = prepare_body(op, target, body); + std::string text = prepared.dump(); + return client_->request_raw(Calls::method(op), request_path(op, target), &text, + "application/json", headers); +} + +Json AzureService::call_multipart(Op op, const Target& target, + const clients::body::MultipartBody& form, + const llm::Headers& headers) { + guard(op); + clients::body::MultipartBody prepared = prepare_form(op, target, form); + return client_->post_multipart(request_path(op, target), prepared, headers); +} + +llm::RawResponse AzureService::call_multipart_raw(Op op, const Target& target, + const clients::body::MultipartBody& form, + const llm::Headers& headers) { + guard(op); + clients::body::MultipartBody prepared = prepare_form(op, target, form); + std::string encoded = prepared.encode(); + return client_->request_raw(clients::Method::kPost, request_path(op, target), &encoded, + prepared.content_type(), headers); +} + +void AzureService::call_stream(Op op, const Target& target, const Json& body, const EventSink& sink, + const llm::Headers& headers) { + guard(op); + Route r = route(op, target); + Json prepared = prepare_body(op, target, body); + if (!Calls::accepts_field(op, r.side, "stream")) + bad(std::string(Calls::spec(op).name) + " does not stream"); + prepared.set("stream", Json(true)); + std::size_t seen = 0; + client_->stream(request_path(op, target), prepared, + [&](const llm::SseEvent& ev) { + if (++seen > AzureCaps::kMaxStreamEvents) + throw llm::ApiError("azure", 502, "BAD_RESPONSE", "stream over the event cap"); + return sink(ev); + }, + headers); +} + +Listing AzureService::walk(Op op, const Target& target, const ListParams& params, const Query& base, + std::size_t max_pages) { + guard(op); + params.validate(); + if (max_pages < 1 || max_pages > AzureCaps::kMaxListPages) + bad("max_pages must be 1.." + std::to_string(AzureCaps::kMaxListPages)); + Listing out; + std::string after = params.after; + for (std::size_t page = 0; page < max_pages; ++page) { + Query q = base; + q.add_int("limit", params.limit); + q.add("order", params.order); + q.add("before", params.before); + q.add("after", after); + Json doc = call(op, target, nullptr, &q); + ++out.pages; + if (const Json* data = doc.find("data"); data && data->is_array()) + for (const Json& item : data->as_array()) out.items.push_back(item); + const Json* more = doc.find("has_more"); + bool has_more = more && more->is_bool() && more->as_bool(); + std::string last = string_at(doc, "last_id"); + if (!has_more) return out; + if (last.empty() || last == after) { + out.truncated = true; // more pages, no usable cursor: stop loudly + return out; + } + after = last; + if (page + 1 == max_pages) { + out.truncated = true; + out.next_after = last; + } + } + return out; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/service.h b/src/vendors/azure/service.h new file mode 100644 index 0000000..aedbcb2 --- /dev/null +++ b/src/vendors/azure/service.h @@ -0,0 +1,117 @@ +#pragma once + +// AzureService: the gateway every service class goes through. It +// - picks the API side for an operation (v1 or dated) from the configured +// api version, and the api-version query value that side needs; +// - builds the path from the call table (ids validated as single path +// segments, the deployment placed in the URL on the dated API and in the +// body `model` on the v1 API); +// - rejects request fields and query names the spec of that side does not +// list (loud, before any traffic); +// - sends JSON / multipart / raw / SSE through llm::ApiClient; +// - walks cursor lists with a page cap and reports truncation. + +#include +#include +#include +#include + +#include "clients/body/form.h" +#include "clients/body/json.h" +#include "llm/api_client.h" +#include "llm/sse.h" +#include "vendors/azure/calls.h" +#include "vendors/azure/client.h" + +namespace molo::azure { + +// Where an operation goes: the deployment (or empty) and the path ids in order. +struct Target { + std::string deployment; + std::vector ids; +}; + +// Query builder; names are checked against the call row when the path is built. +class Query { + public: + Query& add(const std::string& name, const std::string& value); // skipped when empty + Query& add_int(const std::string& name, const std::optional& value); + Query& add_bool(const std::string& name, const std::optional& value); + Query& add_list(const std::string& name, const std::vector& values); + const std::vector>& pairs() const { return pairs_; } + bool empty() const { return pairs_.empty(); } + + private: + std::vector> pairs_; +}; + +struct ListParams { + std::optional limit; // 1..100 + std::string after; // cursor (an object id) + std::string before; + std::string order; // "", "asc", "desc" + void validate() const; + void apply(Query& q) const; +}; + +struct Listing { + std::vector items; + bool truncated = false; // the page cap was hit while the server had more + std::string next_after; // cursor to continue from when truncated + std::size_t pages = 0; +}; + +struct Route { + Side side = Side::kV1; + std::string path; // prefix + rendered spec path + std::string version; // api-version query value, empty = omitted +}; + +using EventSink = std::function; // false: stop + +class AzureService : public llm::Service { + public: + AzureService(std::shared_ptr azure, Surface surface); + + const AzureClient& azure() const { return *azure_; } + Surface surface() const { return surface_; } + + // Side, path and api-version of an operation for a target (no traffic). + Route route(Op op, const Target& target = {}) const; + // Route path plus ?query&api-version=... exactly as sent. + std::string request_path(Op op, const Target& target = {}, const Query* query = nullptr) const; + // The JSON body as sent (deployment moved, fields checked). Throws on a bad body. + clients::body::Json prepare_body(Op op, const Target& target, const clients::body::Json& body) const; + + // Any operation of the surface by table entry. Used by the typed services + // and by the generic route of the demo. + clients::body::Json call(Op op, const Target& target = {}, + const clients::body::Json* body = nullptr, const Query* query = nullptr, + const llm::Headers& headers = {}); + llm::RawResponse call_raw(Op op, const Target& target = {}, const Query* query = nullptr, + const llm::Headers& headers = {}); + clients::body::Json call_multipart(Op op, const Target& target, + const clients::body::MultipartBody& form, + const llm::Headers& headers = {}); + // Multipart POST whose answer may be text (srt, vtt) or binary. + llm::RawResponse call_multipart_raw(Op op, const Target& target, + const clients::body::MultipartBody& form, + const llm::Headers& headers = {}); + // POST with `stream: true` decoded as SSE; at most AzureCaps::kMaxStreamEvents events. + void call_stream(Op op, const Target& target, const clients::body::Json& body, + const EventSink& sink, const llm::Headers& headers = {}); + // Binary-answer POST (speech): the body as JSON, the answer as bytes. + llm::RawResponse call_json_raw(Op op, const Target& target, const clients::body::Json& body, + const llm::Headers& headers = {}); + Listing walk(Op op, const Target& target, const ListParams& params, const Query& base, + std::size_t max_pages); + + protected: + void guard(Op op) const; + clients::body::MultipartBody prepare_form(Op op, const Target& target, + const clients::body::MultipartBody& form) const; + std::shared_ptr azure_; + Surface surface_; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/settings.cpp b/src/vendors/azure/settings.cpp new file mode 100644 index 0000000..176d25c --- /dev/null +++ b/src/vendors/azure/settings.cpp @@ -0,0 +1,112 @@ +#include "vendors/azure/settings.h" + +#include "llm/config.h" +#include "llm/error.h" +#include "vendors/azure/caps.h" + +namespace molo::azure { + +namespace { +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("azure", why); } + +bool name_char(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || + c == '.' || c == '_'; +} + +void check_name(const std::string& what, const std::string& value) { + if (value.empty() || value.size() > 128) bad(what + " must be 1..128 characters"); + for (char c : value) + if (!name_char(c)) bad(what + " has invalid characters"); +} + +std::string trimmed(std::string s) { + while (!s.empty() && (s.back() == ' ' || s.back() == '\t' || s.back() == '\n' || s.back() == '\r')) + s.pop_back(); + std::size_t i = 0; + while (i < s.size() && (s[i] == ' ' || s[i] == '\t')) ++i; + return s.substr(i); +} +} // namespace + +void EntraConfig::validate() const { + check_name("tenant id", tenant_id); + check_name("client id", client_id); + if (client_secret.empty() || client_secret.size() > AzureCaps::kMaxSecretChars) + bad("client secret must be 1.." + std::to_string(AzureCaps::kMaxSecretChars) + " characters"); + for (char c : client_secret) + if (static_cast(c) < 0x20) bad("client secret has control characters"); + Settings::normalize_endpoint(authority); + if (scope.empty() || scope.size() > 256) bad("scope must be 1..256 characters"); + for (char c : scope) + if (static_cast(c) <= 0x20) bad("scope has invalid characters"); +} + +std::string EntraConfig::token_url() const { + return Settings::normalize_endpoint(authority) + "/" + tenant_id + "/oauth2/v2.0/token"; +} + +std::string Settings::normalize_endpoint(const std::string& raw) { + std::string e = trimmed(raw); + if (e.empty()) return e; + if (e.size() > AzureCaps::kMaxEndpointChars) bad("endpoint is too long"); + std::string scheme, rest; + if (e.rfind("https://", 0) == 0) { + scheme = "https://"; + } else if (e.rfind("http://127.0.0.1", 0) == 0 || e.rfind("http://localhost", 0) == 0) { + scheme = "http://"; + } else { + bad("endpoint must start with https://"); + } + rest = e.substr(scheme.size()); + while (!rest.empty() && rest.back() == '/') rest.pop_back(); + if (rest.empty()) bad("endpoint has no host"); + e = scheme + rest; + for (char c : rest) { + if (static_cast(c) <= 0x20) bad("endpoint has invalid characters"); + if (c == '/' || c == '?' || c == '#' || c == '@' || c == '\\') + bad("endpoint must be scheme and host only (no path, query or credentials)"); + } + return e; +} + +AuthKind Settings::auth() const { + if (!api_key.empty()) return AuthKind::kApiKey; + if (entra.complete()) return AuthKind::kEntra; + return AuthKind::kNone; +} + +void Settings::validate() const { + normalize_endpoint(endpoint); + normalize_endpoint(foundry_endpoint); + normalize_endpoint(safety_endpoint); + for (const std::string* k : {&api_key, &safety_key}) { + if (k->size() > AzureCaps::kMaxSecretChars) bad("api key is too long"); + for (char c : *k) + if (static_cast(c) <= 0x20) bad("api key has invalid characters"); + } + if (entra.complete()) entra.validate(); +} + +Settings Settings::from_env() { + using llm::Config; + Settings s; + s.endpoint = normalize_endpoint(Config::get(kEndpointEnv)); + s.foundry_endpoint = normalize_endpoint(Config::get(kFoundryEndpointEnv)); + if (s.foundry_endpoint.empty()) s.foundry_endpoint = s.endpoint; + s.safety_endpoint = normalize_endpoint(Config::get(kSafetyEndpointEnv)); + if (s.safety_endpoint.empty()) s.safety_endpoint = s.endpoint; + s.api_key = trimmed(Config::get(kKeyEnv)); + s.safety_key = trimmed(Config::get(kSafetyKeyEnv)); + if (s.safety_key.empty()) s.safety_key = s.api_key; + s.api_version = ApiVersion::parse(trimmed(Config::get(kVersionEnv))); + s.entra.tenant_id = trimmed(Config::get(kTenantEnv)); + s.entra.client_id = trimmed(Config::get(kClientIdEnv)); + s.entra.client_secret = Config::get(kClientSecretEnv); + std::string authority = trimmed(Config::get(kAuthorityEnv)); + if (!authority.empty()) s.entra.authority = authority; + s.validate(); + return s; +} + +} // namespace molo::azure diff --git a/src/vendors/azure/settings.h b/src/vendors/azure/settings.h new file mode 100644 index 0000000..5be5a13 --- /dev/null +++ b/src/vendors/azure/settings.h @@ -0,0 +1,82 @@ +#pragma once + +// Settings: everything the package needs to address and authenticate against +// an Azure resource, read from llm::Config (environment, then the .env.json +// files). Validation is loud and happens before any network traffic. +// +// AZURE_OPENAI_ENDPOINT https://.openai.azure.com (no path) +// AZURE_OPENAI_API_KEY resource key, sent as the api-key header +// AZURE_OPENAI_API_VERSION v1 (default), preview, or YYYY-MM-DD[-preview] +// AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET +// Microsoft Entra client credentials (used when +// no api key is set) +// Optional extras (same style): +// AZURE_AI_ENDPOINT Foundry model inference host, default = endpoint +// AZURE_CONTENT_SAFETY_ENDPOINT / AZURE_CONTENT_SAFETY_KEY +// default = endpoint / api key +// AZURE_AUTHORITY_HOST default https://login.microsoftonline.com + +#include + +#include "vendors/azure/api_version.h" + +namespace molo::azure { + +enum class AuthKind { kNone, kApiKey, kEntra }; + +struct EntraConfig { + static constexpr const char* kDefaultAuthority = "https://login.microsoftonline.com"; + static constexpr const char* kDefaultScope = "https://cognitiveservices.azure.com/.default"; + + std::string tenant_id; + std::string client_id; + std::string client_secret; + std::string authority = kDefaultAuthority; + std::string scope = kDefaultScope; + + bool complete() const { + return !tenant_id.empty() && !client_id.empty() && !client_secret.empty(); + } + // Throws ApiError::invalid on a malformed tenant / client id / authority. + void validate() const; + // POST target: //oauth2/v2.0/token + std::string token_url() const; +}; + +struct Settings { + static constexpr const char* kVendor = "azure"; + static constexpr const char* kEndpointEnv = "AZURE_OPENAI_ENDPOINT"; + static constexpr const char* kKeyEnv = "AZURE_OPENAI_API_KEY"; + static constexpr const char* kVersionEnv = "AZURE_OPENAI_API_VERSION"; + static constexpr const char* kTenantEnv = "AZURE_TENANT_ID"; + static constexpr const char* kClientIdEnv = "AZURE_CLIENT_ID"; + static constexpr const char* kClientSecretEnv = "AZURE_CLIENT_SECRET"; + static constexpr const char* kFoundryEndpointEnv = "AZURE_AI_ENDPOINT"; + static constexpr const char* kSafetyEndpointEnv = "AZURE_CONTENT_SAFETY_ENDPOINT"; + static constexpr const char* kSafetyKeyEnv = "AZURE_CONTENT_SAFETY_KEY"; + static constexpr const char* kAuthorityEnv = "AZURE_AUTHORITY_HOST"; + + std::string endpoint; // OpenAI resource + std::string foundry_endpoint; // model inference (services.ai.azure.com) + std::string safety_endpoint; // content safety (cognitiveservices.azure.com) + std::string api_key; + std::string safety_key; + ApiVersion api_version; + std::string foundry_version = ApiVersion::kFoundryDefault; + std::string safety_version = ApiVersion::kSafetyDefault; + EntraConfig entra; + + // Reads the environment. Malformed values throw ApiError::invalid; missing + // ones leave the field empty (the clients then report not configured). + static Settings from_env(); + // Validates and normalizes: https only (http for loopback), host only, no + // path, query, fragment or userinfo, trailing slash removed. Empty stays empty. + static std::string normalize_endpoint(const std::string& endpoint); + + AuthKind auth() const; + bool configured() const { return !endpoint.empty() && auth() != AuthKind::kNone; } + // Throws ApiError::invalid when a field is malformed (used by from_env and tests). + void validate() const; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/tools/__pycache__/specdoc.cpython-314.pyc b/src/vendors/azure/tools/__pycache__/specdoc.cpython-314.pyc new file mode 100644 index 0000000000000000000000000000000000000000..48adead0d9acc12a203c950c990edba72fe6490b GIT binary patch literal 6786 zcma)AeQX;?cAx#=_fn)pO0pf>T3NAdTB2y`!Cr8$;9O^D7OQ6W5v^GU< zW|xvhqyVotppuQN=!;zoO@LE?1g8U9)OQU~7bwv5`bUETEojrWcpDWtpaqKj=U4%X z>tB7do@=UpVxlrLg>H9gK=@C&h8d;GDtuw znn40JN~x57r&U_NGb*FsS=A)as###Jn%m6=6NFTYzzQb8ELa4qU=!?uL*N9bP$Rek zX4NXVh1Y~y;h<0_911Y1O*pLDg(kt%h~kv$5WI~@F41N`K2czzz< zN0X7%qL@$>{ui{ zr1HsFQP4n`i0j9qqWZ(K4`*p#AqYY)&%(@ zVO2^d0vf#(Om~K*Ks2m|{c=2<5Ci&dj&>{sOFWi>e)xy|5^-1+qaD|jWI|(>f^z!w zD<{fWT$04)muH)#M69zX9f~C96Ot+|iSP+aQbbg~EyrjgCSoxW5d$S&fzFeiJ^qv3 z{*yt!F$C<2L`C7u;gq`2*`2=pJ@^j*X%b6``GBJ9ov`or2T%GtOW&Ua;|fbf#%x4+ zCELNGDkj4*QC)d~>B&BjIZ#rSqQ5V)r5C02(2INdRx&EatA0CtIDG!zx9jrBB>~Mv zX*EkMnMy?Qie@gZ6QTEf7g?asAjIze2SqR-L>()=j9y3RAet_dPE^h+jZT=#ugje& zw?MU*pAhOU&6DX@@FJ4DbcN_{kcwPosd!x~t3C+LgpO{;Q}N!6rk%#f9tVR{3!1Jm z-cGoU-rjw}v)4Cz)82Bg!RUoh`vIemP~{iTd@9hVOJ9A4nx#^7ikU^-C1RPGeueZQ z#*j6|q-cR5f8C%Xay6kA#G|(Ie$2x<2a!ZKp+oSF{0U48I>Q`766IslBbSolr~=?) zU?4#|1#Dd96X8X%osZ$!!JL1V8arG#b{Z%B#?V>&3XaadYoi4AJz6pjP#Y8EWMn*zYoEHI$I zVb2dt4r}bt<-tiGtFdHx%~qC2qhyW33JV|J+RF3VwZKAhQ4C;FO(rp5K=PUxQI)`A z64sLCKu07VPDRDQauQ!xu?Pqf2BZRb?LOJv(|1Y?&UHuUPR;d10}74=;KQOx3^W9X z1Hds}1Vq>>z;;Z`KyMC1NBJo<8T3CE_YPOHeqrsx`lYo?8~e9Te=?rux>l(jhjab( z+UfOkYv(qaw;cx`IS%f48t?V}qVK~io6R3gEc?_IVnYHIqWWNF^eyrfqG!Cl zb?`Joo~|I72~ptJ6&7F3_l2^*PE_$`u;P+DZZzURNM@(HD>=%{82GIX9Y$9enF0x@ z_Oc%XH9aPg?|=+|{R-qnY4&w2#?o|4K%)X+Ja$Qu5`fzf9yB&8MO4ikS5z#?nn{u4 z5TH#O1G)tx131-KMFhiSA}LI47^@0_GGb&rCJNU~kXeW^+y?#Q@KYktWKhAuWrlX_ z-t3hRySMH9BRjtt+EPEBdB8rv|7a`J)UGlG@4k(OyO*}T#~yl*ZLxW;f7{#j(A#x? z@Ts?Vb*NBRf7f?&@M{Lu_x-_Dx5^f*T$cK|?Iv4rxH4nAundZnoSpPxKT2xd2b%`~ zP0w(>cMzIyB#QZd3Z93}2HG-M8&CvLSXR>%WtfFb?NNrP39|l6mp}!|ux_YSW&|44 zdWxvE`m7M!QJw{qhk())Vx~d1;x%Q`Rc<`O-2lCI@7^F7NmJ2C#(InQ5CEQ!`HJ|4 zW(Q1=MXW+f=3_Jyq;!{KnkktO$t|2%xg7JE@e!cJErgs=L{(R@#>|VV#wn2AErw?a z69ouc2_f+kVhu9j90SOwG1tWvjk=+b47hdAVsHq8Chv zC|O5_-|0eifHzkf&P1iT0R_1Pw5KdX#T9et?>nb5E|!$ehL~#DQDKU#VT#7eO~_EX zu51eQ9L5kTR1qsAY{Vh%U}Dvp8R7uox~^28N!OreBQa_ga{5IGSu#Eibod}NC6@=E zFSvM$UGBlt@JC_;Uan@#i_x!j0o7~o1zAl(|c7A&0!I6Jz{fE}i z4t|dF{X)KHa@#qXb55>O1*>D-zGlDEv*CIFPo7$vK|wWFs6RMr4E>am85SV>uj}XE z^wmOWqVg?_kxM|vZV-zk*Ze->)<9iNf7Hv=QJ?~pr(4L3YW&OSZ16A5V83&Q={wdi zlNsQYmb4qx5jW}!k`|;*ZRCa_w4k(oF%?%Og1!OGR_>>pD<|FXFE20qp~PGCL(TxX zKY%x+6=c9iYj(hU!0u(JeKafRUGG7fLPZqviLFJ&atWZGE#=zy0=yW8p8|nScTww~ zzxUJka^BW$%dtn6V_U($?f-TE{rT;-bC252{oYkyu;>KYOWT&_N0#Q_yAG5_a^BP1 zmflB}UP1(c;o`SFuLR!g?r&lDGZk<>1@X;*H}J-?21AS+z0IcRlPE=x0Fb;pRLxg} zvUUZ!_eWp}x^BlHI)_++sf?)t+q+zTf8yKkuhU}yStRDBXqh$iDVz4Z(Fhi@xyqO& zL|2usY#FjOWG&m%yLuQZ2b3wR;pc>^Dj)M+pqVHK3Q$|=GGqf}89-CQ@Y z7ln@iFRm)JP?q&28LM-CrHrBK^Y$dNS6EXNtfjhCj>?^@w%adT;U4|idU2No?mH~U zY0#c2SE5DhhRUV}&X`

$5%ou^97>dlf!z!CK*QW`k0oL#6+_t;3{dC15uMXEOLaFd-$C6xAH%Wk#bK z(x^6)*Cs@tU5{PB25LMpss}|(5|Uz?VFs^efWX(3gx&rwWwNYuk`kaf(( zaHxk$`~xyPPKHg_CBRvY#&Z~I1B6M)ZAQ9(n^$y}`91l2O&JkFJg0iEjqqPd?^a3!MAT zrA^PT#E-(`z^|6C5ammIW zbBDf*17!Xcvras9yl%|8{+K%gv(_)JUCjP4yL9)*Pr0VAov_d|H?q01^gZ)!bM{a3 z?xWl8ww$|dtFBPo{DW?T_8F!(dyfQ7o>2+a$7Ny;sPOS_*!1F;|4;#KV=tr>PXR4$KXsI_G}M8VGW zlAQ}NMhq?O9z?jQmYEmzQZDa|b)bMMbwf~`RqC)ZD}SlR?k`0#$P;}`+WC{20slK; zfBK}q?`j(sfM5St#9+q9p}wNAh>ya9W=cWUjY%f1iZ@iEBS^|B5^WZUDoX1Jr0#LN z0E3VbAY~Mq3?c_B7jIt7)gRqd^ISVPG)cO@aMrC-Uplz8AQV5@AKvZ0*MGbJ{j;0R zo990{`oweW33m*NX%njPR8~J=4`fM9wjYyy!K7I8QJ9>(!Ht+XR0m{xp!!e3ffOS{ zWk+l)vY?sav=vGVA2kl7JR$XHmf2Z2JD8o-5gC&ML>wivYl)k z9^w7uk(+#57#SEGo+YPTc!rF7NYh3dy=r`u^e&KwAf;}e`u6-9=_zF~i?yCzOh!|2 z@f^lLM3kU>0L?RoqNuM~mST79h~mCLHvPXI_QMj)Fa^t0?wzYoELSt^Gb=LLa`tlt z)|PV&6<9~kDHd2RS2J5+?K$phfwkuB6VIF$D-FbTsCIwuK>vf$&z5r&Kgzum$+@B# v0G43J^o`j<9enP!QtodWtd#3{0}D-!g>pXk?xW1lPqCE$n;J9a(uMzjHj|Nh literal 0 HcmV?d00001 diff --git a/src/vendors/azure/tools/check_spec.py b/src/vendors/azure/tools/check_spec.py new file mode 100644 index 0000000..c946773 --- /dev/null +++ b/src/vendors/azure/tools/check_spec.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python3 +"""Check src/vendors/azure against the pinned azure-rest-api-specs documents. + + python3 -I src/vendors/azure/tools/check_spec.py [local_spec_dir] + +Fails (exit 1) when a row of calls_table.inc has a wrong method, path, +operationId, request content kind, request property list or query list on +either side; when a spec operation of a covered document is not in the table; +when the table lists a name twice; or when the constants in the C++ sources +(api versions, header names, Entra scope) disagree with the documents. +Python 3 stdlib only. local_spec_dir may hold .json copies of the +documents (v1.json, v1p.json, inf25.json, auth24.json, cs24.json, mi.json); +without it the pinned revision is fetched from raw.githubusercontent.com. +""" +import os +import re +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import specdoc # noqa: E402 + +PKG = os.path.join(HERE, "..") +STR = re.compile(r'"([^"]*)"') +DEP = re.compile(r"^/deployments/\{[^}]+\}") + +# Spec operations deliberately not in the table: none. (Older dated revisions +# than the ones in specdoc.DOCS are not read at all; see SPEC.md.) +EXCLUDED = set() + + +def norm(method, path): + return method.upper() + " " + re.sub(r"\{[^}]*\}", "{}", DEP.sub("", path)) + + +def read_rows(): + text = open(os.path.join(PKG, "calls_table.inc")).read() + body = text[text.index("// AZ-ROWS-BEGIN"):text.index("// AZ-ROWS-END")] + rows = [] + for line in body.splitlines()[1:]: + if not line.startswith("AZ_OP("): + continue + name = line[len("AZ_OP("):].split(",")[0] + surface = re.search(r"Surface::(\w+)", line).group(1) + strs = STR.findall(line) + dep = int(re.search(r'"[A-Z]+", (\d),', line).group(1)) + method = strs[0] + sides = [strs[1:7], strs[7:13]] + rows.append({"name": name, "surface": surface, "method": method, "dep": dep, "sides": sides}) + return rows + + +def main(): + local = sys.argv[1] if len(sys.argv) > 1 else None + docs = {n: specdoc.load(n, local) for n in specdoc.DOCS} + rows = read_rows() + bad = [] + names = set() + covered = set() # (doc, method, path) + sides_checked = 0 + for r in rows: + if r["name"] in names: + bad.append("duplicate row name " + r["name"]) + names.add(r["name"]) + for idx, s in enumerate(r["sides"]): + doc_name, path, opid, content, body, query = s + if not path: + continue + sides_checked += 1 + doc = docs.get(doc_name) + if doc is None: + bad.append("%s: unknown document %s" % (r["name"], doc_name)) + continue + item = doc["paths"].get(path) + op = item.get(r["method"].lower()) if item else None + if op is None: + bad.append("%s: %s %s missing in %s" % (r["name"], r["method"], path, doc_name)) + continue + covered.add((doc_name, r["method"].lower(), path)) + f = specdoc.op_facts(doc, item, op) + if f["opid"] != opid: + bad.append("%s: operationId %r != %r" % (r["name"], f["opid"], opid)) + if f["content"] != content: + bad.append("%s: content %s != %s" % (r["name"], f["content"], content)) + if sorted(filter(None, body.split(","))) != f["body"]: + bad.append("%s: body fields differ: table-only %s spec-only %s" % ( + r["name"], sorted(set(filter(None, body.split(","))) - set(f["body"])), + sorted(set(f["body"]) - set(filter(None, body.split(",")))))) + if sorted(filter(None, query.split(","))) != f["query"]: + bad.append("%s: query differs: table %s spec %s" % (r["name"], query, f["query"])) + if r["surface"] != "kOpenAi" and idx == 0: + bad.append("%s: single-sided surface uses the v1 slot" % r["name"]) + if r["dep"] and idx == 1 and not DEP.match(path): + bad.append("%s: deployment flag but no deployment path" % r["name"]) + # Every spec operation must be reachable through the table. + keys = set() + for r in rows: + for s in r["sides"]: + if s[1]: + keys.add((r["surface"], norm(r["method"], s[1]))) + total_ops = 0 + for surface, names_ in specdoc.SURFACE_DOCS.items(): + sk = {"openai": "kOpenAi", "foundry": "kFoundry", "safety": "kSafety"}[surface] + for dn in names_: + for m, path, item, op in specdoc.operations(docs[dn]): + total_ops += 1 + if (sk, norm(m, path)) not in keys and (dn, m, path) not in EXCLUDED: + bad.append("unwrapped spec operation %s %s %s (%s)" % (dn, m.upper(), path, op.get("operationId"))) + # Second source: openai-python src/openai/lib/azure.py `_deployments_endpoints` + # (read 2026-10-25): the operations the official SDK moves under /deployments/{model}. + sdk_deployment_paths = {"/completions", "/chat/completions", "/embeddings", "/audio/transcriptions", + "/audio/translations", "/audio/speech", "/images/generations", "/images/edits"} + table_deployment_paths = {DEP.sub("", s[1]) for r in rows if r["dep"] for s in r["sides"][1:] if s[1]} + if table_deployment_paths != sdk_deployment_paths: + bad.append("deployment-scoped operations differ from the SDK: %s / %s" % ( + sorted(table_deployment_paths - sdk_deployment_paths), sorted(sdk_deployment_paths - table_deployment_paths))) + if len([r for r in rows if r["dep"]]) != len(sdk_deployment_paths): + bad.append("deployment flag count differs from the SDK list") + # Constants in C++ sources. + read = lambda p: open(os.path.join(PKG, p)).read() + ver = read("api_version.h") + for const, doc_name in (("kDatedFallback", "inf25"), ("kFoundryDefault", "mi"), ("kSafetyDefault", "cs24")): + v = re.search(const + r' = "([^"]+)"', ver).group(1) + spec_v = docs[doc_name]["info"]["version"] + if v != spec_v: + bad.append("%s %s != info.version %s of %s" % (const, v, spec_v, doc_name)) + enum = docs["v1p"]["components"]["schemas"]["AzureAIFoundryModelsApiVersion"]["enum"] + if sorted(enum) != ["preview", "v1"]: + bad.append("v1 api-version enum is now %s (ApiVersion knows v1, preview)" % enum) + sec = docs["v1p"]["components"]["securitySchemes"] + if sec["ApiKeyAuth"]["name"] != "api-key": + bad.append("OpenAI api key header is no longer api-key") + if docs["cs24"]["securityDefinitions"]["ApiKeyAuth"]["name"] != "Ocp-Apim-Subscription-Key": + bad.append("Content Safety key header changed") + scope = list(docs["cs24"]["securityDefinitions"]["OAuth2Auth"]["scopes"])[0] + if scope not in read("settings.h"): + bad.append("Entra scope %s not in settings.h" % scope) + if "Ocp-Apim-Subscription-Key" not in read("client.cpp"): + bad.append("client.cpp does not use Ocp-Apim-Subscription-Key") + if bad: + print("SPEC CHECK FAILED") + for b in bad: + print(" " + b) + return 1 + print("spec ok: %d rows, %d table sides verified, %d spec operations seen in %d documents (rev %s)" + % (len(rows), sides_checked, total_ops, len(docs), specdoc.REV[:10])) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/vendors/azure/tools/gen_calls.py b/src/vendors/azure/tools/gen_calls.py new file mode 100644 index 0000000..2e1d3a7 --- /dev/null +++ b/src/vendors/azure/tools/gen_calls.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Generates src/vendors/azure/calls_table.inc from the pinned Azure specs. + + python3 -I src/vendors/azure/tools/gen_calls.py [local_spec_dir] + +One row per logical operation. The OpenAI surface has two sides, "v1" (the +/openai/v1 API) and "dated" (the api-version=YYYY-MM-DD[-preview] API); +operations present in both are one row. Foundry model inference and Content +Safety are single-sided (dated slot). check_spec.py re-verifies every row. +""" +import os +import re +import sys + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import specdoc # noqa: E402 + +HERE = os.path.dirname(os.path.abspath(__file__)) +OUT = os.path.join(HERE, "..", "calls_table.inc") +DEP = re.compile(r"^/deployments/\{[^}]+\}") + + +def norm_key(method, path): + p = DEP.sub("", path) + return method + " " + re.sub(r"\{[^}]*\}", "{}", p) + + +def collect(names, local): + """(method, path) -> (doc name, facts), first document wins.""" + out = {} + for n in names: + doc = specdoc.load(n, local) + for m, path, item, op in specdoc.operations(doc): + if (m, path) in out: + continue + f = specdoc.op_facts(doc, item, op) + f["doc"] = n + f["path"] = path + out[(m, path)] = f + return out + + +def side_args(f): + if f is None: + return ['""'] * 6 + return ['"%s"' % f["doc"], '"%s"' % f["path"], '"%s"' % f["opid"], '"%s"' % f["content"], + '"%s"' % ",".join(f["body"]), '"%s"' % ",".join(f["query"])] + + +def main(): + local = sys.argv[1] if len(sys.argv) > 1 else None + rows = [] # (surface, method, v1, dated) + v1 = collect(("v1p", "v1"), local) + dated = collect(("inf25", "auth24"), local) + groups = {} + for (m, path), f in v1.items(): + groups.setdefault(norm_key(m, path), [None, None, m])[0] = f + for (m, path), f in dated.items(): + k = norm_key(m, path) + g = groups.setdefault(k, [None, None, m]) + if g[1] is not None: + sys.exit("two dated operations map to %s" % k) + g[1] = f + for k in sorted(groups): + a, b, m = groups[k] + rows.append(("kOpenAi", m, a, b)) + for surface, doc in (("kFoundry", "mi"), ("kSafety", "cs24")): + for (m, path), f in sorted(collect((doc,), local).items()): + rows.append((surface, m, None, f)) + names = {} + lines = [] + for surface, m, a, b in rows: + pick = a if a is not None and a["opid"] else (b if b is not None and b["opid"] else None) + if pick is not None: + name = specdoc.camel(pick["opid"]) + else: + name = specdoc.camel(m + " " + (a or b)["path"]) + if surface == "kFoundry": + name = "Foundry" + name + if surface == "kSafety": + name = "Safety" + name + if name in names: + alt = specdoc.camel(b["opid"]) if b is not None and b["opid"] else None + name = alt if alt and alt not in names else name + "Dated" + names[name] = 1 + if a is not None and b is not None: + na = len(re.findall(r"\{", DEP.sub("", a["path"]))) + nb = len(re.findall(r"\{", DEP.sub("", b["path"]))) + if na != nb: + sys.exit("placeholder count differs for %s" % name) + dep = 1 if (b is not None and DEP.match(b["path"])) else 0 + if a is not None and DEP.match(a["path"]): + sys.exit("v1 path with deployment: " + name) + args = [name, "Surface::" + surface, '"%s"' % m.upper(), str(dep)] + side_args(a) + side_args(b) + lines.append("AZ_OP(" + ", ".join(args) + ")") + with open(OUT, "w") as fh: + fh.write("// GENERATED by tools/gen_calls.py from azure-rest-api-specs %s. Do not edit.\n" + % specdoc.REV) + fh.write("// AZ_OP(name, surface, method, deployment_scoped, then twice (v1 side, dated side):\n") + fh.write("// doc, path, operation_id, content, body_fields, query_params)\n") + fh.write("// AZ-ROWS-BEGIN\n") + for ln in lines: + fh.write(ln + "\n") + fh.write("// AZ-ROWS-END\n") + print("wrote %d rows" % len(lines)) + + +if __name__ == "__main__": + main() diff --git a/src/vendors/azure/tools/specdoc.py b/src/vendors/azure/tools/specdoc.py new file mode 100644 index 0000000..a65a47f --- /dev/null +++ b/src/vendors/azure/tools/specdoc.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python3 +"""Shared reader for the Azure OpenAPI / Swagger documents (stdlib only). + +Used by gen_calls.py (writes calls_table.inc) and check_spec.py (verifies it). +Every document is pinned to a commit of Azure/azure-rest-api-specs. +""" +import json +import os +import re +import urllib.request + +# Pinned revision of Azure/azure-rest-api-specs (main at the time of reading). +REV = "ba3eef39e5be55cd5ed1a2e5c6c9bb1a17c36b9a" # committed 2026-10-23T22:56:13Z +BASE = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/%s/specification/" % REV + +# Short name -> path under specification/ +DOCS = { + "v1": "ai/data-plane/OpenAI.v1/azure-v1-v1-generated.json", + "v1p": "ai/data-plane/OpenAI.v1/azure-v1-preview-generated.json", + "inf25": "cognitiveservices/data-plane/OpenAIInference/preview/2025-04-01-preview/inference.json", + "auth24": "cognitiveservices/data-plane/OpenAIAuthoring/stable/2024-10-21/azureopenai.json", + "cs24": "cognitiveservices/data-plane/ContentSafety/stable/2024-09-01/contentsafety.json", + "mi": "ai/data-plane/ModelInference/preview/2024-05-01-preview/openapi.json", +} +# Documents per surface: a spec operation in one of these must be in the table. +SURFACE_DOCS = {"openai": ("v1p", "v1", "inf25", "auth24"), "foundry": ("mi",), "safety": ("cs24",)} +VERBS = ("get", "post", "put", "patch", "delete") + + +def load(name, local_dir=None): + """Loads a document by short name, from local_dir/.json when given.""" + if local_dir: + p = os.path.join(local_dir, name + ".json") + if os.path.exists(p): + return json.load(open(p)) + with urllib.request.urlopen(BASE + DOCS[name], timeout=180) as r: + return json.load(r) + + +def deref(doc, node): + seen = 0 + while isinstance(node, dict) and "$ref" in node and seen < 20: + cur = doc + for part in node["$ref"].lstrip("#/").split("/"): + cur = cur[part] + node = cur + seen += 1 + return node + + +def schema_props(doc, node, depth=0): + if depth > 12 or not isinstance(node, dict): + return set() + node = deref(doc, node) + out = set(node.get("properties", {})) + for key in ("allOf", "oneOf", "anyOf"): + for x in node.get(key, []): + out |= schema_props(doc, x, depth + 1) + return out + + +def params_of(doc, path_item, op): + out = [] + for p in list(path_item.get("parameters", [])) + list(op.get("parameters", [])): + out.append(deref(doc, p)) + return out + + +def norm_content(c): + if c.startswith("application/json") or c.endswith("+json"): + return "json" + if c.startswith("multipart/"): + return "multipart" + if c.startswith("application/x-www-form-urlencoded"): + return "form" + return c + + +def op_facts(doc, path_item, op): + """Returns dict(opid, content, body, query) for one operation.""" + params = params_of(doc, path_item, op) + query = sorted({p["name"] for p in params if p.get("in") == "query" and p["name"] != "api-version"}) + body = set() + kinds = set() + rb = op.get("requestBody") + if rb: + rb = deref(doc, rb) + for c, x in rb.get("content", {}).items(): + kinds.add(norm_content(c)) + body |= schema_props(doc, x.get("schema", {})) + for p in params: + if p.get("in") == "body": + kinds.add("json") + body |= schema_props(doc, p.get("schema", {})) + if p.get("in") == "formData": + kinds.add("multipart") + body.add(p["name"]) + return { + "opid": op.get("operationId") or "", + "content": "+".join(sorted(kinds)) or "none", + "body": sorted(body), + "query": query, + } + + +def operations(doc): + for path, item in doc["paths"].items(): + for m in VERBS: + if m in item: + yield m, path, item, item[m] + + +def camel(text): + parts = re.split(r"[^A-Za-z0-9]+", text) + out = "" + for p in parts: + if p: + out += p[0].upper() + p[1:] + return out diff --git a/src/vendors/azure/upload_file.h b/src/vendors/azure/upload_file.h new file mode 100644 index 0000000..2409b11 --- /dev/null +++ b/src/vendors/azure/upload_file.h @@ -0,0 +1,15 @@ +#pragma once + +// A file for a multipart form: name, media type, bytes. + +#include + +namespace molo::azure { + +struct UploadFile { + std::string filename; + std::string content_type = "application/octet-stream"; + std::string data; +}; + +} // namespace molo::azure diff --git a/src/vendors/azure/vector_stores.cpp b/src/vendors/azure/vector_stores.cpp new file mode 100644 index 0000000..9781e8b --- /dev/null +++ b/src/vendors/azure/vector_stores.cpp @@ -0,0 +1,107 @@ +#include "vendors/azure/vector_stores.h" + +#include "vendors/azure/check.h" + +namespace molo::azure { + +using clients::body::Json; + +namespace { +Query page(const ListParams& p, const std::string& filter = "") { + p.validate(); + Query q; + p.apply(q); + if (!filter.empty()) { + Check::in_set("filter", filter, {"in_progress", "completed", "failed", "cancelled"}); + q.add("filter", filter); + } + return q; +} +} // namespace + +Json VectorStoresService::create(const Json& body) { return call(Op::kCreateVectorStore, {}, &body); } +Json VectorStoresService::get(const std::string& s) { + Check::id("vector store", s); + return call(Op::kGetVectorStore, {"", {s}}); +} +Json VectorStoresService::modify(const std::string& s, const Json& body) { + Check::id("vector store", s); + return call(Op::kModifyVectorStore, {"", {s}}, &body); +} +Json VectorStoresService::list(const ListParams& params) { + Query q = page(params); + return call(Op::kListVectorStores, {}, nullptr, &q); +} +Listing VectorStoresService::list_all(const ListParams& params, std::size_t max_pages) { + return walk(Op::kListVectorStores, {}, params, Query(), max_pages); +} +Json VectorStoresService::remove(const std::string& s) { + Check::id("vector store", s); + return call(Op::kDeleteVectorStore, {"", {s}}); +} +Json VectorStoresService::search(const std::string& s, const Json& body) { + Check::id("vector store", s); + Check::has_field(body, "query"); + return call(Op::kSearchVectorStore, {"", {s}}, &body); +} + +Json VectorStoresService::add_file(const std::string& s, const Json& body) { + Check::id("vector store", s); + Check::has_string(body, "file_id"); + return call(Op::kCreateVectorStoreFile, {"", {s}}, &body); +} +Json VectorStoresService::get_file(const std::string& s, const std::string& f) { + Check::id("vector store", s); + Check::id("file", f); + return call(Op::kGetVectorStoreFile, {"", {s, f}}); +} +Json VectorStoresService::list_files(const std::string& s, const ListParams& params, + const std::string& filter) { + Check::id("vector store", s); + Query q = page(params, filter); + return call(Op::kListVectorStoreFiles, {"", {s}}, nullptr, &q); +} +Json VectorStoresService::update_file_attributes(const std::string& s, const std::string& f, + const Json& attributes) { + Check::id("vector store", s); + Check::id("file", f); + Check::object("attributes", attributes); + Json body = Json::object(); + body.set("attributes", attributes); + return call(Op::kUpdateVectorStoreFileAttributes, {"", {s, f}}, &body); +} +Json VectorStoresService::file_content(const std::string& s, const std::string& f) { + Check::id("vector store", s); + Check::id("file", f); + return call(Op::kRetrieveVectorStoreFileContent, {"", {s, f}}); +} +Json VectorStoresService::remove_file(const std::string& s, const std::string& f) { + Check::id("vector store", s); + Check::id("file", f); + return call(Op::kDeleteVectorStoreFile, {"", {s, f}}); +} + +Json VectorStoresService::create_file_batch(const std::string& s, const Json& body) { + Check::id("vector store", s); + Check::require(body.find("file_ids") || body.find("files"), "file_ids (or files) is required"); + return call(Op::kCreateVectorStoreFileBatch, {"", {s}}, &body); +} +Json VectorStoresService::get_file_batch(const std::string& s, const std::string& b) { + Check::id("vector store", s); + Check::id("batch", b); + return call(Op::kGetVectorStoreFileBatch, {"", {s, b}}); +} +Json VectorStoresService::cancel_file_batch(const std::string& s, const std::string& b) { + Check::id("vector store", s); + Check::id("batch", b); + return call(Op::kCancelVectorStoreFileBatch, {"", {s, b}}); +} +Json VectorStoresService::list_batch_files(const std::string& s, const std::string& b, + const ListParams& params, const std::string& filter) { + Check::id("vector store", s); + Check::id("batch", b); + Query q = page(params, filter); + return call(Op::kListFilesInVectorStoreBatch, {"", {s, b}}, nullptr, &q); +} + +} // namespace molo::azure diff --git a/src/vendors/azure/vector_stores.h b/src/vendors/azure/vector_stores.h new file mode 100644 index 0000000..347a797 --- /dev/null +++ b/src/vendors/azure/vector_stores.h @@ -0,0 +1,41 @@ +#pragma once + +// Vector stores: stores, files, file batches, search, file content. + +#include + +#include "vendors/azure/service.h" + +namespace molo::azure { + +class VectorStoresService : public AzureService { + public: + explicit VectorStoresService(std::shared_ptr azure) + : AzureService(std::move(azure), Surface::kOpenAi) {} + + clients::body::Json create(const clients::body::Json& body = clients::body::Json::object()); + clients::body::Json get(const std::string& store); + clients::body::Json modify(const std::string& store, const clients::body::Json& body); + clients::body::Json list(const ListParams& params = {}); + Listing list_all(const ListParams& params = {}, std::size_t max_pages = 5); + clients::body::Json remove(const std::string& store); // destructive + // body: query (required) plus filters, max_num_results, ranking_options, rewrite_query. + clients::body::Json search(const std::string& store, const clients::body::Json& body); + + clients::body::Json add_file(const std::string& store, const clients::body::Json& body); // file_id required + clients::body::Json get_file(const std::string& store, const std::string& file); + clients::body::Json list_files(const std::string& store, const ListParams& params = {}, + const std::string& filter = ""); + clients::body::Json update_file_attributes(const std::string& store, const std::string& file, + const clients::body::Json& attributes); + clients::body::Json file_content(const std::string& store, const std::string& file); + clients::body::Json remove_file(const std::string& store, const std::string& file); // destructive + + clients::body::Json create_file_batch(const std::string& store, const clients::body::Json& body); + clients::body::Json get_file_batch(const std::string& store, const std::string& batch); + clients::body::Json cancel_file_batch(const std::string& store, const std::string& batch); // destructive + clients::body::Json list_batch_files(const std::string& store, const std::string& batch, + const ListParams& params = {}, const std::string& filter = ""); +}; + +} // namespace molo::azure diff --git a/tests/example/ai/azure/page_test.cpp b/tests/example/ai/azure/page_test.cpp new file mode 100644 index 0000000..0b87b73 --- /dev/null +++ b/tests/example/ai/azure/page_test.cpp @@ -0,0 +1,39 @@ +// Routes: GET /ai/azure (page), /status, /calls (no credential needed) +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + for (const char* v : {"AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_API_KEY", "AZURE_OPENAI_API_VERSION", "AZURE_TENANT_ID", + "AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_AI_ENDPOINT", "AZURE_CONTENT_SAFETY_ENDPOINT", + "AZURE_CONTENT_SAFETY_KEY"}) + unsetenv(v); + Fixture f; + auto page = f.client->get("/ai/azure"); + check(page.status == 200, "page 200"); + check(contains(page.headers.get("Content-Type"), "text/html"), "html"); + check(contains(page.body, "Azure OpenAI and AI Foundry") && contains(page.body, "viewport"), "title and mobile viewport"); + check(contains(page.body, "/embedded/ai_azure.js"), "script wired"); + check(f.client->post("/ai/azure", "{}", "application/json").status == 405, "post page is 405"); + + auto st = f.client->get("/ai/azure/status"); + check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without a key"); + check(contains(st.body, "\"auth\":\"none\"") && contains(st.body, "\"api_version\":\"v1\""), "auth none, version v1"); + check(contains(st.body, "AZURE_OPENAI_ENDPOINT") && contains(st.body, "AZURE_OPENAI_API_KEY") && + contains(st.body, "AZURE_TENANT_ID") && contains(st.body, "AZURE_CLIENT_SECRET"), + "status names the credential variables"); + check(contains(st.body, "\"calls\":147"), "status counts the call table"); + check(f.client->post("/ai/azure/status", "{}", "application/json").status == 405, "post status is 405"); + + auto calls = f.client->get("/ai/azure/calls"); + check(calls.status == 200 && contains(calls.body, "CreateChatCompletion") && + contains(calls.body, "/deployments/{deployment-id}/chat/completions") && + contains(calls.body, "\"v1_path\":\"/chat/completions\"") && + contains(calls.body, "SafetyTextOperationsAnalyzeText"), + "call table listed with both api paths"); + check(f.client->post("/ai/azure/calls", "{}", "application/json").status == 405, "post calls is 405"); + return finish("ai/azure/page"); +} diff --git a/tests/example/ai/azure/routes_test.cpp b/tests/example/ai/azure/routes_test.cpp new file mode 100644 index 0000000..81d2caa --- /dev/null +++ b/tests/example/ai/azure/routes_test.cpp @@ -0,0 +1,111 @@ +// Routes: POST /ai/azure/* (405, 422, dry runs, 503 without a credential) +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + for (const char* v : {"AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_API_KEY", "AZURE_OPENAI_API_VERSION", "AZURE_TENANT_ID", + "AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_AI_ENDPOINT", "AZURE_CONTENT_SAFETY_ENDPOINT", + "AZURE_CONTENT_SAFETY_KEY"}) + unsetenv(v); + Fixture f; + auto post = [&](const std::string& route, const std::string& body) { + return f.client->post("/ai/azure/" + route, body, "application/json"); + }; + const char* all[] = {"chat", "embeddings", "images", "speech", "models", "foundry", "safety", "entra", "realtime", "call"}; + for (const char* r : all) { + std::string route = std::string("/ai/azure/") + r; + check(f.client->get(route).status == 405, route + " get is 405"); + check(post(r, "not json").status == 422, route + " bad json is 422"); + check(post(r, "[1]").status == 422, route + " non-object is 422"); + } + + // 422: input rules run before anything else + check(post("chat", "{}").status == 422, "chat needs deployment"); + check(post("chat", "{\"deployment\":\"gpt4o\"}").status == 422, "chat needs prompt"); + check(post("chat", "{\"deployment\":\"bad name\",\"prompt\":\"hi\"}").status == 422, "bad deployment name"); + check(post("chat", "{\"deployment\":\"" + std::string(65, 'a') + "\",\"prompt\":\"hi\"}").status == 422, "deployment over 64"); + check(post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\",\"dialect\":\"soap\"}").status == 422, "bad dialect"); + check(post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\",\"max_tokens\":\"x\"}").status == 422, "max_tokens type"); + check(post("embeddings", "{\"deployment\":\"e\"}").status == 422, "embeddings need input"); + check(post("images", "{\"deployment\":\"i\"}").status == 422, "images need a prompt"); + check(post("images", "{\"deployment\":\"i\",\"prompt\":\"x\",\"n\":\"two\"}").status == 422, "image n must be a number"); + check(post("speech", "{\"deployment\":\"t\",\"input\":\"x\"}").status == 422, "speech needs a voice"); + check(post("foundry", "{}").status == 422, "foundry needs an action"); + check(post("foundry", "{\"action\":\"fly\"}").status == 422, "bad foundry action"); + check(post("foundry", "{\"action\":\"chat\"}").status == 422, "foundry chat needs a prompt"); + check(post("foundry", "{\"action\":\"embeddings\"}").status == 422, "foundry embeddings need input"); + check(post("safety", "{\"action\":\"text\"}").status == 422, "safety text needs text"); + check(post("safety", "{\"action\":\"shield\"}").status == 422, "shield needs a prompt or documents"); + check(post("safety", "{\"action\":\"fly\"}").status == 422, "bad safety action"); + check(post("realtime", "{\"action\":\"url\"}").status == 422, "realtime url needs a deployment"); + check(post("realtime", "{\"action\":\"url\",\"deployment\":\"bad name\"}").status == 422, "realtime bad deployment"); + check(post("realtime", "{\"action\":\"fly\"}").status == 422, "bad realtime action"); + check(post("call", "{}").status == 422, "call needs an operation"); + check(post("call", "{\"operation\":\"NoSuchCall\"}").status == 422, "unknown operation"); + check(post("call", "{\"operation\":\"CreateFile\"}").status == 422, "multipart operation is refused"); + check(post("call", "{\"operation\":\"RetrieveFile\",\"ids\":[\"../x\"]}").status == 422, "path traversal id"); + check(post("call", "{\"operation\":\"RetrieveFile\"}").status == 422, "missing id"); + check(post("call", "{\"operation\":\"CreateConversation\",\"body\":{\"bogus\":1}}").status == 422, "field not in the spec"); + check(post("call", "{\"operation\":\"ListContainers\",\"query\":{\"zzz\":\"1\"}}").status == 422, "query not in the spec"); + check(post("call", "{\"operation\":\"ListContainers\",\"query\":{\"limit\":5}}").status == 422, "query values are strings"); + + // dry runs: paid or resource-changing routes show the exact request + auto dry = post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\"}"); + check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "/openai/v1/chat/completions") && + contains(dry.body, "\"model\":\"gpt4o\""), + "chat is a dry run on the v1 api"); + check(contains(post("chat", "{\"deployment\":\"gpt41\",\"prompt\":\"hi\",\"dialect\":\"responses\"}").body, "/openai/v1/responses"), + "responses dialect preview"); + check(contains(post("chat", "{\"deployment\":\"Phi-4\",\"prompt\":\"hi\",\"dialect\":\"foundry\"}").body, + "/models/chat/completions?api-version=2024-05-01-preview"), + "foundry dialect preview"); + check(contains(post("embeddings", "{\"deployment\":\"e\",\"input\":\"hi\"}").body, "/openai/v1/embeddings"), "embeddings dry run"); + check(contains(post("images", "{\"deployment\":\"i\",\"prompt\":\"a cat\"}").body, "/openai/v1/images/generations"), "images dry run"); + check(contains(post("speech", "{\"deployment\":\"t\",\"voice\":\"alloy\",\"input\":\"hi\"}").body, "/openai/v1/audio/speech"), + "speech dry run"); + check(contains(post("foundry", "{\"action\":\"chat\",\"prompt\":\"hi\"}").body, "\"dry_run\":true"), "foundry chat dry run"); + check(contains(post("safety", "{\"action\":\"text\",\"text\":\"hello\"}").body, + "/contentsafety/text:analyze?api-version=2024-09-01"), + "safety text dry run"); + check(contains(post("safety", "{\"action\":\"protected\",\"text\":\"hello\"}").body, "detectProtectedMaterial"), "protected dry run"); + check(contains(post("realtime", "{\"action\":\"secret\",\"deployment\":\"rt\",\"seconds\":60}").body, + "/openai/v1/realtime/client_secrets"), + "client secret dry run"); + auto del = post("call", "{\"operation\":\"DeleteFile\",\"ids\":[\"file-1\"]}"); + check(del.status == 200 && contains(del.body, "\"dry_run\":true") && contains(del.body, "DELETE /openai/v1/files/file-1"), + "delete is a dry run"); + check(contains(post("call", "{\"operation\":\"CreateVectorStore\",\"body\":{\"name\":\"docs\"}}").body, "\"dry_run\":true"), + "create is a dry run"); + check(contains(post("call", "{\"operation\":\"CreateAssistant\",\"body\":{\"model\":\"gpt4o\"}}").body, + "/openai/assistants?api-version=2025-04-01-preview"), + "dated-only operation shows the fallback api version"); + check(contains(post("call", "{\"operation\":\"CreateChatCompletion\",\"deployment\":\"gpt4o\",\"body\":{\"messages\":[]}}").body, + "/openai/v1/chat/completions"), + "generic chat preview"); + + // 503: free calls and confirmed calls need the credential + for (const char* r : {"models"}) { + auto res = post(r, "{}"); + check(res.status == 503 && contains(res.body, "not_configured"), std::string(r) + " without key is 503"); + } + check(post("entra", "{}").status == 503, "entra check without client credentials is 503"); + check(post("realtime", "{\"action\":\"url\",\"deployment\":\"rt\"}").status == 503, "realtime url without an endpoint is 503"); + check(post("foundry", "{\"action\":\"info\"}").status == 503, "foundry info without key is 503"); + check(post("safety", "{\"action\":\"blocklists\"}").status == 503, "blocklists without key is 503"); + check(post("call", "{\"operation\":\"ListVectorStores\"}").status == 503, "generic GET without key is 503"); + check(post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed chat without key is 503"); + check(post("embeddings", "{\"deployment\":\"e\",\"input\":\"hi\",\"confirm\":true}").status == 503, "confirmed embeddings without key is 503"); + check(post("images", "{\"deployment\":\"i\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed images without key is 503"); + check(post("speech", "{\"deployment\":\"t\",\"voice\":\"alloy\",\"input\":\"x\",\"confirm\":true}").status == 503, + "confirmed speech without key is 503"); + check(post("safety", "{\"action\":\"text\",\"text\":\"hi\",\"confirm\":true}").status == 503, "confirmed safety without key is 503"); + check(post("call", "{\"operation\":\"DeleteFile\",\"ids\":[\"file-1\"],\"confirm\":true}").status == 503, + "confirmed delete without key is 503"); + check(post("realtime", "{\"action\":\"secret\",\"deployment\":\"rt\",\"confirm\":true}").status == 503, + "confirmed client secret without key is 503"); + return finish("ai/azure/routes"); +} diff --git a/tests/src/vendors/azure/rig.h b/tests/src/vendors/azure/rig.h new file mode 100644 index 0000000..1375577 --- /dev/null +++ b/tests/src/vendors/azure/rig.h @@ -0,0 +1,109 @@ +#pragma once + +// Shared test rig: an AzureClient over RecordingTransports. A recorder only +// records what was sent and answers a fixed body (a documented example where +// a call parses its answer, "{}" otherwise); no vendor behavior is scripted. + +#include +#include +#include +#include +#include + +#include "clients/transport.h" +#include "framework.h" +#include "llm/error.h" +#include "vendors/azure/client.h" + +namespace az_test { + +using molo::azure::AzureClient; +using molo::azure::Channel; +using molo::azure::Settings; +using molo::clients::body::Json; + +struct Seen { + std::string method; + std::string target; // path?query + std::string body; + molo::clients::HeaderMap headers; +}; + +class Recorder : public molo::clients::Transport { + public: + const char* name() const override { return "recorder"; } + molo::clients::Task send(const molo::clients::ClientRequest& r) override { + Seen s; + s.method = molo::clients::method_name(r.method()); + s.target = r.url().target(); + s.headers = r.headers(); + if (r.has_body() && !r.is_stream_body()) s.body = r.body_bytes(); + { + std::lock_guard lock(mu); + seen.push_back(std::move(s)); + } + molo::clients::HeaderMap h; + h.set("Content-Type", content_type); + co_return molo::clients::ClientResponse(status, h, answer); + } + std::mutex mu; + std::vector seen; + std::string answer = "{}"; + std::string content_type = "application/json"; + int status = 200; +}; + +struct Rig { + std::shared_ptr client; + std::map rec; + + explicit Rig(Settings s, molo::azure::EntraTokenSource::Clock clock = nullptr) { + client = AzureClient::create( + std::move(s), + [this](Channel ch) -> std::unique_ptr { + auto r = std::make_unique(); + rec[ch] = r.get(); + return r; + }, + std::move(clock)); + } + Recorder& openai() { return *rec.at(Channel::kOpenAi); } + Recorder& foundry() { return *rec.at(Channel::kFoundry); } + Recorder& safety() { return *rec.at(Channel::kSafety); } + Recorder& token() { return *rec.at(Channel::kToken); } + const Seen& last() { return openai().seen.back(); } + Json last_body() { return Json::parse(openai().seen.back().body); } +}; + +inline Settings key_settings(const std::string& version = "") { + Settings s; + s.endpoint = "https://res.openai.azure.com"; + s.foundry_endpoint = "https://res.services.ai.azure.com"; + s.safety_endpoint = "https://res.cognitiveservices.azure.com"; + s.api_key = "az-test-KEY"; + s.safety_key = "az-safety-KEY"; + s.api_version = molo::azure::ApiVersion::parse(version); + return s; +} + +inline Settings entra_settings(const std::string& version = "") { + Settings s = key_settings(version); + s.api_key.clear(); + s.safety_key.clear(); + s.entra.tenant_id = "contoso.onmicrosoft.com"; + s.entra.client_id = "11111111-2222-3333-4444-555555555555"; + s.entra.client_secret = "s3cr3t-value"; + return s; +} + +template +bool rejects(F&& fn, const std::string& needle = "") { + try { + fn(); + } catch (const molo::llm::ApiError& e) { + return needle.empty() || e.message().find(needle) != std::string::npos; + } + return false; +} + +} // namespace az_test diff --git a/tests/src/vendors/azure/test_calls.cpp b/tests/src/vendors/azure/test_calls.cpp new file mode 100644 index 0000000..345430d --- /dev/null +++ b/tests/src/vendors/azure/test_calls.cpp @@ -0,0 +1,105 @@ +// Azure: the call table (shape, rendering, ids, fields, queries). +#include + +#include "rig.h" +#include "vendors/azure/calls.h" + +using namespace molo::azure; +using namespace az_test; + +TEST(azure_calls, table_shape) { + ASSERT_EQ(Calls::count(), static_cast(147)); + std::set names; + std::size_t openai = 0, foundry = 0, safety = 0, both = 0; + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& c = Calls::at(i); + ASSERT_TRUE(names.insert(c.name).second); + ASSERT_EQ(static_cast(c.op), i); // enum order is table order + ASSERT_TRUE(c.v1.present() || c.dated.present()); + for (Side s : {Side::kV1, Side::kDated}) { + if (!c.side(s).present()) continue; + ASSERT_TRUE(std::string(c.side(s).path).rfind("/", 0) == 0); + ASSERT_TRUE(std::string(c.side(s).doc).size() >= 2); + } + (void)Calls::method(c.op); + if (c.surface == Surface::kOpenAi) { + ++openai; + if (c.v1.present() && c.dated.present()) ++both; + } + if (c.surface == Surface::kFoundry) ++foundry; + if (c.surface == Surface::kSafety) ++safety; + if (c.surface != Surface::kOpenAi) ASSERT_FALSE(c.v1.present()); // single-sided + } + ASSERT_EQ(foundry, static_cast(4)); + ASSERT_EQ(safety, static_cast(12)); + ASSERT_EQ(openai, static_cast(131)); + ASSERT_TRUE(both > 50); +} + +TEST(azure_calls, every_side_renders_with_dummy_ids) { + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& c = Calls::at(i); + for (Side s : {Side::kV1, Side::kDated}) { + if (!c.side(s).present()) continue; + std::vector ids(Calls::id_count(c.op, s), "id1"); + std::string path = Calls::render(c.op, s, ids, "dep1"); + ASSERT_TRUE(path.find('{') == std::string::npos); + ASSERT_TRUE(path.find("dep1") != std::string::npos || !c.deployment || s == Side::kV1); + // one id too many and one too few are both rejected + auto more = ids; + more.push_back("x"); + ASSERT_TRUE(rejects([&] { Calls::render(c.op, s, more, "dep1"); })); + if (!ids.empty()) { + auto fewer = ids; + fewer.pop_back(); + ASSERT_TRUE(rejects([&] { Calls::render(c.op, s, fewer, "dep1"); })); + } + } + } +} + +TEST(azure_calls, deployment_paths) { + ASSERT_EQ(Calls::render(Op::kCreateChatCompletion, Side::kDated, {}, "gpt4o"), + std::string("/deployments/gpt4o/chat/completions")); + ASSERT_EQ(Calls::render(Op::kCreateChatCompletion, Side::kV1, {}, "gpt4o"), std::string("/chat/completions")); + ASSERT_EQ(Calls::prefix(Op::kCreateChatCompletion, Side::kV1), std::string("/openai/v1")); + ASSERT_EQ(Calls::prefix(Op::kCreateChatCompletion, Side::kDated), std::string("/openai")); + ASSERT_EQ(Calls::prefix(Op::kFoundryGetChatCompletions, Side::kDated), std::string("/models")); + ASSERT_EQ(Calls::prefix(Op::kSafetyTextOperationsAnalyzeText, Side::kDated), std::string("/contentsafety")); + ASSERT_EQ(Calls::render(Op::kSafetyTextOperationsAnalyzeText, Side::kDated, {}), std::string("/text:analyze")); + ASSERT_EQ(Calls::render(Op::kSafetyTextBlocklistsAddOrUpdateBlocklistItems, Side::kDated, {"my-list"}), + std::string("/text/blocklists/my-list:addOrUpdateBlocklistItems")); + ASSERT_TRUE(rejects([] { Calls::render(Op::kCreateChatCompletion, Side::kDated, {}, "../x"); }, "deployment")); + ASSERT_TRUE(rejects([] { Calls::render(Op::kCreateChatCompletion, Side::kDated, {}, ""); }, "deployment")); +} + +TEST(azure_calls, ids_are_single_segments) { + ASSERT_EQ(Calls::render(Op::kRetrieveModel, Side::kV1, {"ft:gpt-4o:org::abc"}), std::string("/models/ft:gpt-4o:org::abc")); + for (const char* bad : {"../x", "a/b", "", "a b", "a?x=1", "%2e%2e", ".."}) + ASSERT_TRUE(rejects([&] { Calls::render(Op::kRetrieveFile, Side::kV1, {bad}); }, "bad id")); +} + +TEST(azure_calls, fields_and_queries_come_from_the_spec) { + ASSERT_TRUE(Calls::accepts_field(Op::kCreateChatCompletion, Side::kV1, "messages")); + ASSERT_TRUE(Calls::accepts_field(Op::kCreateChatCompletion, Side::kV1, "model")); + ASSERT_FALSE(Calls::accepts_field(Op::kCreateChatCompletion, Side::kDated, "model")); + ASSERT_FALSE(Calls::accepts_field(Op::kCreateChatCompletion, Side::kV1, "nonsense")); + ASSERT_FALSE(Calls::accepts_field(Op::kCreateChatCompletion, Side::kV1, "mess")); // whole-name match + ASSERT_TRUE(Calls::accepts_query(Op::kListFiles, Side::kV1, "after")); + ASSERT_FALSE(Calls::accepts_query(Op::kListFiles, Side::kDated, "after")); + ASSERT_TRUE(Calls::accepts_query(Op::kListFiles, Side::kDated, "purpose")); + ASSERT_TRUE(Calls::accepts_query(Op::kGetResponse, Side::kV1, "include[]")); + ASSERT_TRUE(Calls::accepts_query(Op::kGetResponse, Side::kDated, "include[]")); + ASSERT_FALSE(Calls::accepts_query(Op::kGetResponse, Side::kV1, "api-version")); // the gateway owns it + ASSERT_TRUE(Calls::find("CreateImage") != nullptr); + ASSERT_TRUE(Calls::find("NoSuchCall") == nullptr); +} + +TEST(azure_calls, sides_that_exist_only_once) { + ASSERT_FALSE(Calls::has(Op::kCreateAssistant, Side::kV1)); // the v1 documents have no /assistants + ASSERT_TRUE(Calls::has(Op::kCreateAssistant, Side::kDated)); + ASSERT_TRUE(Calls::has(Op::kCreateConversation, Side::kV1)); + ASSERT_FALSE(Calls::has(Op::kCreateConversation, Side::kDated)); + ASSERT_TRUE(Calls::has(Op::kCreateChatCompletion, Side::kV1) && Calls::has(Op::kCreateChatCompletion, Side::kDated)); + ASSERT_TRUE(rejects([] { Calls::render(Op::kCreateAssistant, Side::kV1, {}); }, "not available")); +} diff --git a/tests/src/vendors/azure/test_chat_model.cpp b/tests/src/vendors/azure/test_chat_model.cpp new file mode 100644 index 0000000..ce3a34d --- /dev/null +++ b/tests/src/vendors/azure/test_chat_model.cpp @@ -0,0 +1,152 @@ +// AzureChatModel: wire bodies per dialect and parsing of the documented +// examples (chat completion, stream transcript, response object). +#include "rig.h" +#include "vendors/azure/chat_model.h" + +using namespace molo::azure; +using namespace az_test; + +namespace { +// Documented chat completion example (OpenAI / Azure OpenAI reference). +const char kChat[] = + R"({"id":"chatcmpl-123","object":"chat.completion","created":1677652288,"model":"gpt-4o-mini","choices":[{"index":0,"message":{"role":"assistant","content":"Hello there, how may I assist you today?"},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":12,"total_tokens":21}})"; +// Documented stream transcript (chat.completion.chunk). +const char kStream[] = + "data: {\"id\":\"chatcmpl-123\",\"object\":\"chat.completion.chunk\",\"created\":1694268190,\"model\":\"gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}\n\n" + "data: {\"id\":\"chatcmpl-123\",\"object\":\"chat.completion.chunk\",\"created\":1694268190,\"model\":\"gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"},\"finish_reason\":null}]}\n\n" + "data: {\"id\":\"chatcmpl-123\",\"object\":\"chat.completion.chunk\",\"created\":1694268190,\"model\":\"gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" + "data: [DONE]\n\n"; +// Documented Responses example. +const char kResponse[] = + R"({"id":"resp_67ccd2bed1ec8190b14f964abc0542670bb6a6b452d3795b","object":"response","created_at":1741476542,"status":"completed","model":"gpt-4.1-2025-04-14","output":[{"type":"message","id":"msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b","status":"completed","role":"assistant","content":[{"type":"output_text","text":"In a peaceful grove beneath a silver moon, a unicorn named Lumina discovered a hidden pool.","annotations":[]}]}],"usage":{"input_tokens":36,"output_tokens":87,"total_tokens":123}})"; + +molo::llm::ChatRequest req(const std::string& model = "gpt4o") { + molo::llm::ChatRequest r; + r.model = model; + r.system = "Be brief."; + r.messages.push_back(molo::llm::Message::user("Say hi")); + r.max_tokens = 32; + return r; +} +} // namespace + +TEST(azure_chat_model, generate_over_chat_completions_v1) { + Rig rig(key_settings()); + rig.openai().answer = kChat; + AzureChatModel m(rig.client); + ASSERT_EQ(m.vendor(), std::string("azure")); + auto out = m.generate(req()); + ASSERT_EQ(out.text(), std::string("Hello there, how may I assist you today?")); + ASSERT_EQ(out.finish_reason, std::string("stop")); + ASSERT_EQ(out.usage.total_tokens, 21); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/chat/completions")); + Json b = rig.last_body(); + ASSERT_EQ(b.find("model")->as_string(), std::string("gpt4o")); + ASSERT_TRUE(b.has("messages")); + ASSERT_FALSE(b.has("stream")); +} + +TEST(azure_chat_model, generate_over_the_dated_api_uses_the_deployment_url) { + Rig rig(key_settings("2024-10-21")); + rig.openai().answer = kChat; + AzureChatModel m(rig.client); + m.generate(req()); + ASSERT_EQ(rig.last().target, std::string("/openai/deployments/gpt4o/chat/completions?api-version=2024-10-21")); + ASSERT_FALSE(rig.last_body().has("model")); +} + +TEST(azure_chat_model, stream_decodes_the_documented_transcript) { + Rig rig(key_settings()); + rig.openai().content_type = "text/event-stream"; + rig.openai().answer = kStream; + AzureChatModel m(rig.client); + std::string text, finish; + m.stream(req(), [&](const molo::llm::ChatChunk& c) { + text += c.text_delta; + if (!c.finish_reason.empty()) finish = c.finish_reason; + return true; + }); + ASSERT_EQ(text, std::string("Hello")); + ASSERT_EQ(finish, std::string("stop")); + Json b = rig.last_body(); + ASSERT_TRUE(b.find("stream")->as_bool()); + ASSERT_TRUE(b.find("stream_options")->find("include_usage")->as_bool()); +} + +TEST(azure_chat_model, stream_stops_when_the_consumer_says_so) { + Rig rig(key_settings()); + rig.openai().content_type = "text/event-stream"; + rig.openai().answer = kStream; + AzureChatModel m(rig.client); + int calls = 0; + m.stream(req(), [&](const molo::llm::ChatChunk&) { return ++calls < 2; }); + ASSERT_EQ(calls, 2); +} + +TEST(azure_chat_model, responses_dialect) { + Rig rig(key_settings()); + rig.openai().answer = kResponse; + ChatModelConfig cfg; + cfg.dialect = Dialect::kResponses; + AzureChatModel m(rig.client, cfg); + auto out = m.generate(req("gpt41")); + ASSERT_TRUE(out.text().rfind("In a peaceful grove", 0) == 0); + ASSERT_EQ(out.usage.total_tokens, 123); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/responses")); + ASSERT_EQ(rig.last_body().find("model")->as_string(), std::string("gpt41")); +} + +TEST(azure_chat_model, foundry_dialect_goes_to_the_model_inference_host) { + Rig rig(key_settings()); + rig.foundry().answer = kChat; + ChatModelConfig cfg; + cfg.dialect = Dialect::kFoundry; + cfg.extra_parameters = "drop"; + AzureChatModel m(rig.client, cfg); + auto out = m.generate(req("Phi-4")); + ASSERT_TRUE(out.text().rfind("Hello there", 0) == 0); + const Seen& s = rig.foundry().seen.back(); + ASSERT_EQ(s.target, std::string("/models/chat/completions?api-version=2024-05-01-preview")); + ASSERT_EQ(s.headers.get_or("extra-parameters", ""), std::string("drop")); + Json b = Json::parse(s.body); + ASSERT_TRUE(b.has("max_tokens")); + ASSERT_FALSE(b.has("max_completion_tokens")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_chat_model, validation_before_traffic) { + Rig rig(key_settings()); + AzureChatModel m(rig.client); + molo::llm::ChatRequest r = req(); + r.model = ""; + ASSERT_TRUE(rejects([&] { m.generate(r); }, "deployment")); + r.model = "bad name"; + ASSERT_TRUE(rejects([&] { m.generate(r); }, "deployment")); + r = req(); + r.messages.clear(); + ASSERT_TRUE(rejects([&] { m.generate(r); }, "messages")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_chat_model, not_configured_is_a_503_before_traffic) { + Settings none; + none.endpoint = "https://res.openai.azure.com"; + Rig rig(none); + AzureChatModel m(rig.client); + ASSERT_FALSE(m.configured()); + try { + m.generate(req()); + FAIL("expected not configured"); + } catch (const molo::llm::NotConfiguredError& e) { + ASSERT_EQ(e.http_status(), 503); + } + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_chat_model, wire_body_is_exposed_without_traffic) { + Rig rig(key_settings()); + AzureChatModel m(rig.client); + Json b = m.wire_body(req(), false); + ASSERT_EQ(b.find("model")->as_string(), std::string("gpt4o")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} diff --git a/tests/src/vendors/azure/test_entra.cpp b/tests/src/vendors/azure/test_entra.cpp new file mode 100644 index 0000000..8fc8ffc --- /dev/null +++ b/tests/src/vendors/azure/test_entra.cpp @@ -0,0 +1,161 @@ +// Microsoft Entra client-credentials flow: wire shape, parsing of the +// documented success body and of an error body observed live (2026-10-25), +// caching, refresh, failure floor, bearer on every surface. +#include "clients/body/form.h" +#include "rig.h" +#include "vendors/azure/entra.h" +#include "vendors/azure/files.h" +#include "vendors/azure/safety.h" + +using namespace molo::azure; +using namespace az_test; + +namespace { +// Documented success body (Microsoft identity platform, client credentials flow). +const char kOk[] = + R"({"token_type":"Bearer","expires_in":3599,"ext_expires_in":3599,"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Imk2bEdrM0ZaenhSY1ViMkMzbkVRN3N5SEpsWSJ9"})"; +// Observed live from login.microsoftonline.com for a client id that does not exist. +const char kErr[] = + R"({"error":"unauthorized_client","error_description":"AADSTS700038: 00000000-0000-0000-0000-000000000000 is not a valid application identifier. Trace ID: 856e791a-6d26-41ce-a6d4-be68759f9101 Correlation ID: 93a74863-808b-4409-88a1-c2563d0fb599 Timestamp: 2026-10-25 09:24:16Z","error_codes":[700038],"timestamp":"2026-10-25 09:24:16Z","trace_id":"856e791a-6d26-41ce-a6d4-be68759f9101","correlation_id":"93a74863-808b-4409-88a1-c2563d0fb599"})"; + +struct Clock { + std::int64_t now = 1000000; + EntraTokenSource::Clock fn() { + return [this] { return now; }; + } +}; +} // namespace + +TEST(azure_entra, parses_the_documented_success_body) { + auto p = EntraTokenSource::parse(200, kOk); + ASSERT_TRUE(p.access_token.rfind("eyJ0eXAi", 0) == 0); + ASSERT_EQ(p.expires_in, 3599L); + ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, R"({"token_type":"Bearer","expires_in":3599})"); }, "access_token")); + ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, R"({"access_token":"x","expires_in":0})"); }, "expires_in")); + ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, R"({"access_token":"x","expires_in":999999999})"); }, "expires_in")); + ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, ""); }, "non-JSON")); + ASSERT_EQ(EntraTokenSource::parse(200, R"({"access_token":"x","expires_in":"3600"})").expires_in, 3600L); +} + +TEST(azure_entra, parses_the_observed_error_body) { + try { + EntraTokenSource::parse(400, kErr); + FAIL("expected an error"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 400); + ASSERT_EQ(e.type(), std::string("unauthorized_client")); + ASSERT_TRUE(e.message().rfind("AADSTS700038", 0) == 0); + ASSERT_EQ(e.request_id(), std::string("856e791a-6d26-41ce-a6d4-be68759f9101")); + } +} + +TEST(azure_entra, exchange_request_has_the_documented_shape) { + Clock clock; + Rig rig(entra_settings(), clock.fn()); + rig.token().answer = kOk; + std::string tok = rig.client->tokens()->token(); + ASSERT_TRUE(tok.rfind("eyJ0eXAi", 0) == 0); + const Seen& s = rig.token().seen.back(); + ASSERT_EQ(s.method, std::string("POST")); + ASSERT_EQ(s.target, std::string("/contoso.onmicrosoft.com/oauth2/v2.0/token")); + ASSERT_EQ(s.headers.get_or("Content-Type", ""), std::string("application/x-www-form-urlencoded")); + auto form = molo::clients::body::UrlEncodedForm::decode(s.body); + std::map f(form.fields().begin(), form.fields().end()); + ASSERT_EQ(f["grant_type"], std::string("client_credentials")); + ASSERT_EQ(f["client_id"], std::string("11111111-2222-3333-4444-555555555555")); + ASSERT_EQ(f["client_secret"], std::string("s3cr3t-value")); + ASSERT_EQ(f["scope"], std::string("https://cognitiveservices.azure.com/.default")); + ASSERT_EQ(f.size(), static_cast(4)); +} + +TEST(azure_entra, token_is_cached_and_refreshed_before_expiry) { + Clock clock; + Rig rig(entra_settings(), clock.fn()); + rig.token().answer = kOk; + auto src = rig.client->tokens(); + src->token(); + clock.now += 3000; // 599 s left, above the 300 s skew + src->token(); + ASSERT_EQ(src->exchanges(), static_cast(1)); + clock.now += 400; // 199 s left: inside the skew, refresh + src->token(); + ASSERT_EQ(src->exchanges(), static_cast(2)); + ASSERT_EQ(src->expires_at(), clock.now + 3599); +} + +TEST(azure_entra, failed_exchange_has_a_floor_and_never_leaks_the_secret) { + Clock clock; + Rig rig(entra_settings(), clock.fn()); + rig.token().status = 400; + rig.token().answer = kErr; + auto src = rig.client->tokens(); + try { + src->token(); + FAIL("expected failure"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.type(), std::string("unauthorized_client")); + ASSERT_TRUE(std::string(e.what()).find("s3cr3t-value") == std::string::npos); + } + // Within the floor: no second network call. + try { + src->token(); + FAIL("expected backoff"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.type(), std::string("TOKEN_BACKOFF")); + ASSERT_EQ(e.http_status(), 503); + ASSERT_TRUE(std::string(e.what()).find("s3cr3t-value") == std::string::npos); + } + ASSERT_EQ(src->exchanges(), static_cast(1)); + clock.now += 5; // floor passed: one more try + rig.token().status = 200; + rig.token().answer = kOk; + ASSERT_FALSE(src->token().empty()); + ASSERT_EQ(src->exchanges(), static_cast(2)); +} + +TEST(azure_entra, every_surface_sends_the_bearer_token) { + Clock clock; + Rig rig(entra_settings(), clock.fn()); + rig.token().answer = kOk; + FilesService f(rig.client); + f.get("file-1"); + std::string want = "Bearer " + rig.client->tokens()->token(); + ASSERT_EQ(rig.last().headers.get_or("Authorization", ""), want); + ASSERT_FALSE(rig.last().headers.has("api-key")); + SafetyService s(rig.client); + s.list_blocklists(); + ASSERT_EQ(rig.safety().seen.back().headers.get_or("Authorization", ""), want); + ASSERT_FALSE(rig.safety().seen.back().headers.has("Ocp-Apim-Subscription-Key")); + ASSERT_EQ(rig.client->tokens()->exchanges(), static_cast(1)); // one token serves all + ASSERT_TRUE(rig.last().target.find("eyJ") == std::string::npos); +} + +TEST(azure_entra, a_failed_exchange_fails_the_api_call_without_sending_it) { + Clock clock; + Rig rig(entra_settings(), clock.fn()); + rig.token().status = 400; + rig.token().answer = kErr; + FilesService f(rig.client); + ASSERT_TRUE(rejects([&] { f.get("file-1"); }, "AADSTS700038")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_entra, key_wins_and_no_exchange_happens_in_key_mode) { + Settings s = entra_settings(); + s.api_key = "k"; + s.safety_key = "k"; + Rig rig(s); + ASSERT_TRUE(rig.client->tokens() == nullptr); + ASSERT_TRUE(rig.client->auth() == AuthKind::kApiKey); + ASSERT_EQ(rig.rec.count(Channel::kToken), static_cast(0)); // no token transport at all +} + +TEST(azure_entra, sovereign_authority_hosts) { + Settings s = entra_settings(); + s.entra.authority = "https://login.microsoftonline.us"; + Rig rig(s); + rig.token().answer = kOk; + rig.client->tokens()->token(); + ASSERT_EQ(rig.token().seen.back().target, std::string("/contoso.onmicrosoft.com/oauth2/v2.0/token")); + ASSERT_EQ(s.entra.token_url(), std::string("https://login.microsoftonline.us/contoso.onmicrosoft.com/oauth2/v2.0/token")); +} diff --git a/tests/src/vendors/azure/test_gateway.cpp b/tests/src/vendors/azure/test_gateway.cpp new file mode 100644 index 0000000..2b569b2 --- /dev/null +++ b/tests/src/vendors/azure/test_gateway.cpp @@ -0,0 +1,239 @@ +// Azure gateway: api side and api-version choice, path building, body and +// query rules, credentials on the wire, caps. Recorders only record. +#include "rig.h" +#include "vendors/azure/assistants.h" +#include "vendors/azure/chat.h" +#include "vendors/azure/files.h" +#include "vendors/azure/foundry.h" +#include "vendors/azure/responses.h" +#include "vendors/azure/safety.h" + +using namespace molo::azure; +using namespace az_test; + +namespace { +Json chat_body() { + return Json::parse(R"({"messages":[{"role":"user","content":"hi"}],"max_tokens":16})"); +} +} // namespace + +TEST(azure_gateway, v1_chat_puts_deployment_in_the_body_and_omits_api_version) { + Rig rig(key_settings()); + ChatService chat(rig.client); + chat.create("gpt4o", chat_body()); + ASSERT_EQ(rig.last().method, std::string("POST")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/chat/completions")); + ASSERT_EQ(rig.last_body().find("model")->as_string(), std::string("gpt4o")); +} + +TEST(azure_gateway, preview_adds_api_version_preview) { + Rig rig(key_settings("preview")); + ChatService chat(rig.client); + chat.create("gpt4o", chat_body()); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/chat/completions?api-version=preview")); +} + +TEST(azure_gateway, dated_chat_puts_deployment_in_the_url_and_drops_model) { + Rig rig(key_settings("2024-10-21")); + ChatService chat(rig.client); + Json body = chat_body(); + body.set("model", Json("gpt4o")); // allowed when equal to the deployment, then dropped + chat.create("gpt4o", body); + ASSERT_EQ(rig.last().target, std::string("/openai/deployments/gpt4o/chat/completions?api-version=2024-10-21")); + ASSERT_FALSE(rig.last_body().has("model")); + ASSERT_TRUE(rig.last_body().has("messages")); +} + +TEST(azure_gateway, model_must_match_the_deployment) { + Rig rig(key_settings()); + ChatService chat(rig.client); + Json body = chat_body(); + body.set("model", Json("other")); + ASSERT_TRUE(rejects([&] { chat.create("gpt4o", body); }, "deployment")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_gateway, unknown_fields_are_rejected_before_traffic) { + Rig rig(key_settings()); + ChatService chat(rig.client); + Json body = chat_body(); + body.set("not_a_field", Json(1)); + ASSERT_TRUE(rejects([&] { chat.create("gpt4o", body); }, "not_a_field")); + ASSERT_TRUE(rejects([&] { chat.create("gpt4o", Json::object()); }, "messages")); + ASSERT_TRUE(rejects([&] { chat.create("bad name", chat_body()); }, "deployment")); + ASSERT_TRUE(rejects([&] { chat.create("", chat_body()); }, "deployment")); + ASSERT_TRUE(rejects([&] { chat.create(std::string(65, 'a'), chat_body()); }, "deployment")); + ASSERT_TRUE(rejects([&] { chat.create("gpt4o", Json::parse("[1]")); }, "object")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_gateway, dated_only_operations_use_the_fallback_version_under_v1) { + Rig rig(key_settings()); + AssistantsService a(rig.client); + a.get_assistant("asst_1"); + ASSERT_EQ(rig.last().target, std::string("/openai/assistants/asst_1?api-version=2025-04-01-preview")); + Rig dated(key_settings("2024-10-21")); + AssistantsService b(dated.client); + b.get_assistant("asst_1"); + ASSERT_EQ(dated.last().target, std::string("/openai/assistants/asst_1?api-version=2024-10-21")); +} + +TEST(azure_gateway, v1_only_operations_use_preview_under_a_dated_version) { + Rig rig(key_settings("2024-10-21")); + molo::azure::Route r = AzureService(rig.client, Surface::kOpenAi).route(Op::kCreateConversation); + ASSERT_TRUE(r.side == Side::kV1); + ASSERT_EQ(r.version, std::string("preview")); + ASSERT_EQ(r.path, std::string("/openai/v1/conversations")); +} + +TEST(azure_gateway, shared_operations_follow_the_configured_api) { + Rig v1(key_settings()); + Rig dated(key_settings("2025-04-01-preview")); + AzureService a(v1.client, Surface::kOpenAi), b(dated.client, Surface::kOpenAi); + ASSERT_EQ(a.request_path(Op::kRetrieveFile, {"", {"file-1"}}), std::string("/openai/v1/files/file-1")); + ASSERT_EQ(b.request_path(Op::kRetrieveFile, {"", {"file-1"}}), + std::string("/openai/files/file-1?api-version=2025-04-01-preview")); + ASSERT_EQ(b.request_path(Op::kListModels), std::string("/openai/models?api-version=2025-04-01-preview")); +} + +TEST(azure_gateway, query_names_come_from_the_spec_and_are_encoded) { + Rig rig(key_settings()); + FilesService f(rig.client); + f.list("fine-tune", [] { + ListParams p; + p.limit = 5; + p.order = "desc"; + return p; + }()); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/files?purpose=fine-tune&limit=5&order=desc")); + Rig dated(key_settings("2024-10-21")); + FilesService g(dated.client); + ListParams p; + p.limit = 5; + ASSERT_TRUE(rejects([&] { g.list("", p); }, "limit")); // the dated spec only lists purpose + ASSERT_EQ(dated.openai().seen.size(), static_cast(0)); + ResponsesService r(rig.client); + r.retrieve("resp_1", {"file_search_call.results", "a b"}); + // clients::Url percent-encodes the brackets of query names (src/llm/SPEC.md gap); servers decode them. + std::string t = rig.last().target; + ASSERT_TRUE(t.find("include%5B%5D=file_search_call.results") != std::string::npos || + t.find("include[]=file_search_call.results") != std::string::npos); + ASSERT_TRUE(t.find("=a%20b") != std::string::npos); + ListParams bad; + bad.limit = 101; + ASSERT_TRUE(rejects([&] { f.list("", bad); }, "limit")); + bad.limit = 1; + bad.order = "sideways"; + ASSERT_TRUE(rejects([&] { f.list("", bad); }, "order")); +} + +TEST(azure_gateway, ids_are_validated_before_a_path_is_built) { + Rig rig(key_settings()); + FilesService f(rig.client); + for (const char* bad : {"../x", "a/b", "", "a b"}) ASSERT_TRUE(rejects([&] { f.get(bad); }, "bad file id")); + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); +} + +TEST(azure_gateway, key_goes_in_a_header_only) { + Rig rig(key_settings()); + FilesService f(rig.client); + f.get("file-1"); + ASSERT_EQ(rig.last().headers.get_or("api-key", ""), std::string("az-test-KEY")); + ASSERT_FALSE(rig.last().headers.has("Authorization")); + ASSERT_TRUE(rig.last().target.find("az-test-KEY") == std::string::npos); + SafetyService s(rig.client); + s.list_blocklists(); + const Seen& seen = rig.safety().seen.back(); + ASSERT_EQ(seen.headers.get_or("Ocp-Apim-Subscription-Key", ""), std::string("az-safety-KEY")); + ASSERT_FALSE(seen.headers.has("api-key")); + FoundryService fy(rig.client); + fy.info(); + ASSERT_EQ(rig.foundry().seen.back().headers.get_or("api-key", ""), std::string("az-test-KEY")); +} + +TEST(azure_gateway, errors_never_carry_the_key) { + Rig rig(key_settings()); + rig.openai().status = 401; + rig.openai().answer = + R"({"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint."}})"; + FilesService f(rig.client); + try { + f.get("file-1"); + FAIL("expected 401"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 401); + ASSERT_EQ(e.type(), std::string("401")); + ASSERT_TRUE(std::string(e.what()).find("az-test-KEY") == std::string::npos); + } +} + +TEST(azure_gateway, not_configured_means_no_traffic) { + Settings none; + none.endpoint = "https://res.openai.azure.com"; + Rig rig(none); + FilesService f(rig.client); + ASSERT_FALSE(f.configured()); + try { + f.get("file-1"); + FAIL("expected not configured"); + } catch (const molo::llm::NotConfiguredError& e) { + ASSERT_EQ(e.http_status(), 503); + ASSERT_EQ(e.type(), std::string("NOT_CONFIGURED")); + } + ASSERT_EQ(rig.openai().seen.size(), static_cast(0)); + Settings no_endpoint; + no_endpoint.api_key = "k"; + Rig rig2(no_endpoint); + FilesService g(rig2.client); + try { + g.get("file-1"); + FAIL("expected not configured"); + } catch (const molo::llm::NotConfiguredError& e) { + ASSERT_TRUE(e.message().find("AZURE_OPENAI_ENDPOINT") != std::string::npos); + } +} + +TEST(azure_gateway, a_service_refuses_operations_of_another_surface) { + Rig rig(key_settings()); + AzureService openai(rig.client, Surface::kOpenAi); + ASSERT_TRUE(rejects([&] { openai.call(Op::kFoundryGetModelInfo); }, "does not belong")); +} + +TEST(azure_gateway, list_walk_is_capped_and_reports_truncation) { + Rig rig(key_settings()); + rig.openai().answer = R"({"object":"list","data":[{"id":"a"}],"has_more":true,"last_id":"a"})"; + AzureService s(rig.client, Surface::kOpenAi); + Listing l = s.walk(Op::kListVectorStores, {}, ListParams(), Query(), 3); + // last_id == the cursor after the first page: the walk stops loudly instead of looping + ASSERT_TRUE(l.truncated); + ASSERT_TRUE(l.pages <= 3); + ASSERT_TRUE(rejects([&] { s.walk(Op::kListVectorStores, {}, ListParams(), Query(), 0); }, "max_pages")); + ASSERT_TRUE(rejects([&] { s.walk(Op::kListVectorStores, {}, ListParams(), Query(), 21); }, "max_pages")); +} + +TEST(azure_gateway, list_walk_follows_cursors_and_stops_at_the_cap) { + Rig rig(key_settings()); + AzureService s(rig.client, Surface::kOpenAi); + rig.openai().answer = R"({"data":[{"id":"x"}],"has_more":false})"; + Listing l = s.walk(Op::kListVectorStores, {}, ListParams(), Query(), 5); + ASSERT_FALSE(l.truncated); + ASSERT_EQ(l.items.size(), static_cast(1)); + ASSERT_EQ(l.pages, static_cast(1)); +} + +TEST(azure_gateway, stream_requires_a_streaming_operation_and_sets_stream_true) { + Rig rig(key_settings()); + rig.openai().content_type = "text/event-stream"; + rig.openai().answer = "data: {\"id\":\"1\"}\n\ndata: [DONE]\n\n"; + ChatService chat(rig.client); + std::size_t n = 0; + chat.stream("gpt4o", chat_body(), [&](const Json& c) { + ++n; + ASSERT_EQ(c.find("id")->as_string(), std::string("1")); + return true; + }); + ASSERT_EQ(n, static_cast(1)); + ASSERT_TRUE(rig.last_body().find("stream")->as_bool()); + AzureService s(rig.client, Surface::kOpenAi); + ASSERT_TRUE(rejects([&] { s.call_stream(Op::kRetrieveFile, {"", {"f"}}, Json::object(), [](const molo::llm::SseEvent&) { return true; }); })); +} diff --git a/tests/src/vendors/azure/test_live.cpp b/tests/src/vendors/azure/test_live.cpp new file mode 100644 index 0000000..259a7fd --- /dev/null +++ b/tests/src/vendors/azure/test_live.cpp @@ -0,0 +1,108 @@ +// Azure: opt-in live checks. MOLO_LIVE_NET=1 enables them. +// - Unauthenticated probes (no credential needed): the Entra token endpoint +// answers the documented error for a client id that does not exist, and the +// regional Azure OpenAI / Content Safety host answers 401 with the +// documented error body for a bad key. +// - With AZURE_OPENAI_ENDPOINT + a credential (key, or tenant / client id / +// secret): read-only models list. One smallest chat completion needs +// MOLO_LIVE_PAID=1 and AZURE_OPENAI_DEPLOYMENT. +// Everything else prints a loud SKIP. Nothing is created, updated or deleted. +#include +#include + +#include "clients/transports/http_transport.h" +#include "llm/config.h" +#include "rig.h" +#include "vendors/azure/chat_model.h" +#include "vendors/azure/models.h" +#include "vendors/azure/safety.h" + +using namespace molo::azure; +using namespace az_test; + +namespace { +// Read once at static init: other suites in this binary edit the environment. +const bool kNet = std::getenv("MOLO_LIVE_NET") && std::string(std::getenv("MOLO_LIVE_NET")) == "1"; +const bool kPaid = std::getenv("MOLO_LIVE_PAID") && std::string(std::getenv("MOLO_LIVE_PAID")) == "1"; +const std::string kEndpoint = molo::llm::Config::get("AZURE_OPENAI_ENDPOINT"); +const std::string kKey = molo::llm::Config::get("AZURE_OPENAI_API_KEY"); +const std::string kTenant = molo::llm::Config::get("AZURE_TENANT_ID"); +const std::string kDeployment = molo::llm::Config::get("AZURE_OPENAI_DEPLOYMENT"); + +std::shared_ptr http_client(Settings s) { + return AzureClient::create(std::move(s), [](Channel) -> std::unique_ptr { + return std::make_unique(); + }); +} +} // namespace + +TEST(azure_live, entra_token_endpoint_answers_the_documented_error) { + if (!kNet) { + std::cout << "SKIP azure_live entra probe: MOLO_LIVE_NET=1 not set\n"; + return; + } + Settings s = entra_settings(); + s.entra.client_id = "00000000-0000-0000-0000-000000000000"; + auto client = http_client(s); + try { + client->tokens()->token(); + FAIL("expected an AAD error"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 400); + ASSERT_EQ(e.type(), std::string("unauthorized_client")); + ASSERT_TRUE(e.message().rfind("AADSTS", 0) == 0); + std::cout << " probe: " << e.http_status() << " " << e.type() << " " << e.message().substr(0, 40) << "\n"; + } +} + +TEST(azure_live, regional_host_answers_401_for_a_bad_key) { + if (!kNet) { + std::cout << "SKIP azure_live host probe: MOLO_LIVE_NET=1 not set\n"; + return; + } + Settings s; + s.endpoint = "https://eastus.api.cognitive.microsoft.com"; + s.safety_endpoint = s.endpoint; + s.api_key = "invalid-probe-key"; + s.safety_key = "invalid-probe-key"; + auto client = http_client(s); + ModelsService models(client); + try { + models.list(); + FAIL("expected 401"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 401); + ASSERT_EQ(e.type(), std::string("401")); + ASSERT_TRUE(e.message().find("invalid subscription key") != std::string::npos); + } + SafetyService safety(client); + try { + safety.analyze_text(Json::parse(R"({"text":"hello"})")); + FAIL("expected 401"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 401); + } +} + +TEST(azure_live, models_list_and_one_tiny_completion) { + bool have_cred = !kKey.empty() || !kTenant.empty(); + if (!kNet || kEndpoint.empty() || !have_cred) { + std::cout << "SKIP azure_live models: needs MOLO_LIVE_NET=1, AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY" + " (or AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET)\n"; + return; + } + auto client = AzureClient::from_env(); + ModelsService models(client); + ASSERT_TRUE(!models.ids().empty()); + if (!kPaid || kDeployment.empty()) { + std::cout << "SKIP azure_live completion: needs MOLO_LIVE_PAID=1 and AZURE_OPENAI_DEPLOYMENT\n"; + return; + } + AzureChatModel model(client); + molo::llm::ChatRequest in; + in.model = kDeployment; + in.messages.push_back(molo::llm::Message::user("Reply with the single word: ok")); + in.max_tokens = 16; + auto out = model.generate(in); + ASSERT_TRUE(!out.text().empty()); +} diff --git a/tests/src/vendors/azure/test_realtime.cpp b/tests/src/vendors/azure/test_realtime.cpp new file mode 100644 index 0000000..3ab2b4b --- /dev/null +++ b/tests/src/vendors/azure/test_realtime.cpp @@ -0,0 +1,145 @@ +// Azure realtime: URL builder, header choice, and a WebSocket session over a +// test-owned loopback server (the protocol is not Microsoft's to script; the +// server only speaks RFC 6455). +#include "../../google/live/loopback.h" +#include "rig.h" +#include "vendors/azure/realtime.h" + +using namespace molo::azure; +using namespace az_test; +using live_test::LoopbackServer; +using live_test::ServerConn; + +namespace { +Settings local(const LoopbackServer& s, const std::string& version = "") { + Settings st = key_settings(version); + st.endpoint = "http://127.0.0.1:" + std::to_string(s.port()); + return st; +} +bool contains(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; } +} // namespace + +TEST(azure_realtime, urls_per_api) { + Settings s = key_settings(); + ASSERT_EQ(RealtimeUrl::build(s, "gpt-rt"), std::string("wss://res.openai.azure.com/openai/v1/realtime?model=gpt-rt")); + s = key_settings("preview"); + ASSERT_EQ(RealtimeUrl::build(s, "gpt-rt"), + std::string("wss://res.openai.azure.com/openai/v1/realtime?model=gpt-rt&api-version=preview")); + s = key_settings("2025-04-01-preview"); + ASSERT_EQ(RealtimeUrl::build(s, "gpt-rt"), + std::string("wss://res.openai.azure.com/openai/realtime?api-version=2025-04-01-preview&deployment=gpt-rt")); + ASSERT_TRUE(rejects([&] { RealtimeUrl::build(s, "bad name"); }, "deployment")); + ASSERT_TRUE(rejects([&] { RealtimeUrl::build(s, ""); }, "deployment")); + ASSERT_TRUE(RealtimeUrl::build(s, "gpt-rt").find("az-test-KEY") == std::string::npos); + Settings none; + ASSERT_TRUE(rejects([&] { RealtimeUrl::build(none, "d"); }, "endpoint")); +} + +TEST(azure_realtime, not_configured_means_no_connection) { + Settings none; + none.endpoint = "https://res.openai.azure.com"; + Rig rig(none); + RealtimeSocket sock(rig.client); + try { + sock.connect("gpt-rt"); + FAIL("expected not configured"); + } catch (const molo::llm::NotConfiguredError& e) { + ASSERT_EQ(e.http_status(), 503); + } + ASSERT_FALSE(sock.connected()); + ASSERT_TRUE(rejects([&] { sock.send(Json::parse(R"({"type":"x"})")); }, "not connected")); +} + +TEST(azure_realtime, session_over_a_loopback_server) { + std::string got; + LoopbackServer server([&](ServerConn& c) { + c.send_text(R"({"type":"session.created","session":{"id":"sess_1"}})"); + auto m = c.read_message(); + if (m) got = *m; + c.send_text(R"({"type":"response.done"})"); + c.send_close(1000); + }); + Rig rig(local(server)); + RealtimeSocket sock(rig.client); + sock.connect("gpt-rt"); + ASSERT_TRUE(sock.connected()); + sock.send(Json::parse(R"({"type":"response.create"})")); + std::vector types; + RealtimeStats st = sock.run( + [&](const Json& ev) { + types.push_back(ev.find("type")->as_string()); + return true; + }, + 5); + ASSERT_EQ(types.size(), static_cast(2)); + ASSERT_EQ(types[0], std::string("session.created")); + ASSERT_EQ(types[1], std::string("response.done")); + ASSERT_EQ(st.stop_reason, std::string("closed")); + ASSERT_EQ(st.close_code, 1000); + ASSERT_EQ(got, std::string(R"({"type":"response.create"})")); + std::string req = server.request(); + ASSERT_TRUE(contains(req, "GET /openai/v1/realtime?model=gpt-rt HTTP/1.1")); + ASSERT_TRUE(contains(req, "api-key: az-test-KEY")); + ASSERT_FALSE(contains(req, "Authorization")); + sock.close(); + ASSERT_FALSE(sock.connected()); +} + +TEST(azure_realtime, dated_url_reaches_the_server) { + LoopbackServer server([](ServerConn& c) { c.send_close(1000); }); + Rig rig(local(server, "2025-04-01-preview")); + RealtimeSocket sock(rig.client); + sock.connect("gpt-rt"); + ASSERT_TRUE(contains(server.request(), "GET /openai/realtime?api-version=2025-04-01-preview&deployment=gpt-rt ")); + RealtimeStats st = sock.run([](const Json&) { return true; }, 2); + ASSERT_EQ(st.stop_reason, std::string("closed")); +} + +TEST(azure_realtime, entra_uses_a_bearer_header) { + LoopbackServer server([](ServerConn& c) { c.send_close(1000); }); + Settings s = entra_settings(); + s.endpoint = "http://127.0.0.1:" + std::to_string(server.port()); + Rig rig(s); + rig.token().answer = R"({"token_type":"Bearer","expires_in":3599,"access_token":"tok-rt"})"; + RealtimeSocket sock(rig.client); + sock.connect("gpt-rt"); + std::string req = server.request(); + ASSERT_TRUE(contains(req, "Authorization: Bearer tok-rt")); + ASSERT_FALSE(contains(req, "api-key")); +} + +TEST(azure_realtime, caps_and_validation) { + LoopbackServer server([](ServerConn& c) { + for (int i = 0; i < 5; ++i) c.send_text(R"({"type":"x"})"); + c.read_message(); + }); + Rig rig(local(server)); + RealtimeSocket sock(rig.client); + sock.connect("gpt-rt"); + ASSERT_TRUE(rejects([&] { sock.send(Json::object()); }, "type")); + ASSERT_TRUE(rejects([&] { sock.send(Json::parse(R"({"type":""})")); }, "type")); + Json big = Json::object(); + big.set("type", Json("x")); + big.set("pad", Json(std::string(RealtimeSocket::kMaxEventBytes + 1, 'a'))); + ASSERT_TRUE(rejects([&] { sock.send(big); }, "byte cap")); + ASSERT_TRUE(rejects([&] { sock.run([](const Json&) { return true; }, 0); }, "seconds")); + ASSERT_TRUE(rejects([&] { sock.run([](const Json&) { return true; }, 3601); }, "seconds")); + std::size_t seen = 0; + RealtimeStats st = sock.run([&](const Json&) { return ++seen < 3; }, 5); + ASSERT_EQ(st.stop_reason, std::string("sink_stopped")); + ASSERT_EQ(seen, static_cast(3)); + ASSERT_TRUE(rejects([&] { sock.connect("gpt-rt"); }, "already connected")); +} + +TEST(azure_realtime, refused_upgrade_is_an_api_error) { + LoopbackServer server([](ServerConn&) {}, LoopbackServer::Handshake::Status403); + Rig rig(local(server)); + RealtimeSocket sock(rig.client); + try { + sock.connect("gpt-rt"); + FAIL("expected a refused upgrade"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 403); + ASSERT_EQ(e.type(), std::string("HANDSHAKE_REFUSED")); + } +} diff --git a/tests/src/vendors/azure/test_services.cpp b/tests/src/vendors/azure/test_services.cpp new file mode 100644 index 0000000..54c991a --- /dev/null +++ b/tests/src/vendors/azure/test_services.cpp @@ -0,0 +1,419 @@ +// Azure services: request shapes per concern (paths, methods, bodies, content +// types) over recorders, plus validation and caps. Answers that a call parses +// are the documented examples (labelled), nothing else is scripted. +#include + +#include "rig.h" +#include "vendors/azure/assistants.h" +#include "vendors/azure/audio.h" +#include "vendors/azure/batches.h" +#include "vendors/azure/chat.h" +#include "vendors/azure/embeddings.h" +#include "vendors/azure/files.h" +#include "vendors/azure/fine_tuning.h" +#include "vendors/azure/foundry.h" +#include "vendors/azure/images.h" +#include "vendors/azure/models.h" +#include "vendors/azure/operations.h" +#include "vendors/azure/realtime.h" +#include "vendors/azure/responses.h" +#include "vendors/azure/safety.h" +#include "vendors/azure/vector_stores.h" + +using namespace molo::azure; +using namespace az_test; + +namespace { +bool contains(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; } +// A text part exactly as MultipartBody encodes it. +std::string part(const std::string& name, const std::string& value) { + return "name=\"" + name + "\"\r\nContent-Type: text/plain\r\n\r\n" + value; +} +} // namespace + +TEST(azure_services, embeddings_parse_the_documented_example) { + // Documented example: Azure OpenAI embeddings reference (shape: data[].embedding). + Rig rig(key_settings("2024-10-21")); + rig.openai().answer = + R"({"object":"list","data":[{"object":"embedding","index":0,"embedding":[0.0023064255,-0.009327292,0.5]}],"model":"text-embedding-ada-002","usage":{"prompt_tokens":5,"total_tokens":5}})"; + EmbeddingsService e(rig.client); + auto v = e.embed("emb", {"hello"}, 3); + ASSERT_EQ(v.size(), static_cast(1)); + ASSERT_EQ(v[0].size(), static_cast(3)); + ASSERT_EQ(rig.last().target, std::string("/openai/deployments/emb/embeddings?api-version=2024-10-21")); + ASSERT_EQ(rig.last_body().find("dimensions")->as_int(), 3L); + ASSERT_TRUE(rejects([&] { e.embed("emb", {}); }, "texts")); + ASSERT_TRUE(rejects([&] { e.embed("emb", {""}); }, "text")); + Json many = Json::array(); + for (int i = 0; i < 2049; ++i) many.push(Json("x")); + Json b = Json::object(); + b.set("input", many); + ASSERT_TRUE(rejects([&] { e.create("emb", b); }, "2048")); +} + +TEST(azure_services, completions_need_a_prompt) { + Rig rig(key_settings()); + CompletionsService c(rig.client); + ASSERT_TRUE(rejects([&] { c.create("d", Json::object()); }, "prompt")); + Json b = Json::object(); + b.set("prompt", Json("hi")); + c.create("d", b); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/completions")); +} + +TEST(azure_services, images_generate_and_edit) { + Rig rig(key_settings()); + ImagesService im(rig.client); + Json b = Json::object(); + b.set("prompt", Json("a cat")); + b.set("size", Json("1024x1024")); + im.generate("img", b); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/images/generations")); + ASSERT_EQ(rig.last_body().find("model")->as_string(), std::string("img")); + ASSERT_TRUE(rejects([&] { im.generate("img", Json::object()); }, "prompt")); + Json big = Json::object(); + big.set("prompt", Json(std::string(32001, 'x'))); + ASSERT_TRUE(rejects([&] { im.generate("img", big); }, "32000")); + + UploadFile png{"a.png", "image/png", "PNGDATA"}; + im.edit("img", "add a hat", {png}, std::nullopt, {{"n", "1"}}); + ASSERT_TRUE(contains(rig.last().headers.get_or("Content-Type", ""), "multipart/form-data")); + ASSERT_TRUE(contains(rig.last().body, part("model", "img"))); + ASSERT_TRUE(contains(rig.last().body, "name=\"image\"; filename=\"a.png\"")); + ASSERT_TRUE(contains(rig.last().body, "name=\"prompt\"")); + im.edit("img", "x", {png, png}, UploadFile{"m.png", "image/png", "M"}); + ASSERT_TRUE(contains(rig.last().body, "name=\"image[]\"")); + ASSERT_TRUE(contains(rig.last().body, "name=\"mask\"")); + ASSERT_TRUE(rejects([&] { im.edit("img", "x", {}); }, "1..16")); + ASSERT_TRUE(rejects([&] { im.edit("img", "x", {UploadFile{"e.png", "image/png", ""}}); }, "empty")); + ASSERT_TRUE(rejects([&] { im.edit("img", "x", {png}, std::nullopt, {{"bogus", "1"}}); }, "bogus")); + + Rig dated(key_settings("2025-04-01-preview")); + ImagesService di(dated.client); + di.edit("img", "x", {png}); + ASSERT_EQ(dated.last().target, std::string("/openai/deployments/img/images/edits?api-version=2025-04-01-preview")); + ASSERT_FALSE(contains(dated.last().body, "name=\"model\"")); + di.variation("img", png); // v1-only operation, preview fallback + ASSERT_EQ(dated.last().target, std::string("/openai/v1/images/variations?api-version=preview")); + ASSERT_TRUE(contains(dated.last().body, part("model", "img"))); +} + +TEST(azure_services, audio_speech_transcription_translation) { + Rig rig(key_settings()); + rig.openai().content_type = "audio/mpeg"; + rig.openai().answer = "ID3BYTES"; + AudioService a(rig.client); + Json b = Json::object(); + b.set("input", Json("Hello")); + b.set("voice", Json("alloy")); + auto sp = a.speech("tts", b); + ASSERT_EQ(sp.body, std::string("ID3BYTES")); + ASSERT_EQ(sp.content_type, std::string("audio/mpeg")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/audio/speech")); + ASSERT_EQ(rig.last().headers.get_or("Content-Type", ""), std::string("application/json")); + ASSERT_TRUE(rejects([&] { a.speech("tts", Json::parse(R"({"voice":"alloy"})")); }, "input")); + Json longb = b; + longb.set("input", Json(std::string(4097, 'x'))); + ASSERT_TRUE(rejects([&] { a.speech("tts", longb); }, "4096")); + + rig.openai().content_type = "application/json"; + rig.openai().answer = R"({"text":"Hello world"})"; + UploadFile wav{"a.wav", "audio/wav", "RIFF"}; + auto t = a.transcribe("whisper", wav, {{"language", "en"}, {"response_format", "json"}}); + ASSERT_EQ(AudioService::as_json(t).find("text")->as_string(), std::string("Hello world")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/audio/transcriptions")); + ASSERT_TRUE(contains(rig.last().body, "name=\"file\"; filename=\"a.wav\"")); + ASSERT_TRUE(contains(rig.last().body, part("language", "en"))); + rig.openai().content_type = "text/plain"; + rig.openai().answer = "1\n00:00:00,000 --> 00:00:01,000\nHi\n"; + auto srt = a.transcribe("whisper", wav, {{"response_format", "srt"}}); + ASSERT_EQ(AudioService::as_json(srt).find("text")->as_string(), srt.body); + a.translate("whisper", wav); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/audio/translations")); + UploadFile huge{"h.wav", "audio/wav", std::string(25u * 1024u * 1024u + 1, 'x')}; + ASSERT_TRUE(rejects([&] { a.transcribe("whisper", huge); }, "25 MiB")); + ASSERT_TRUE(rejects([&] { a.transcribe("whisper", UploadFile{"", "audio/wav", "x"}); }, "filename")); + ASSERT_TRUE(rejects([&] { a.transcribe("whisper", wav, {{"nonsense", "1"}}); }, "nonsense")); + + Rig dated(key_settings("2024-10-21")); + AudioService d(dated.client); + d.transcribe("whisper", wav); + ASSERT_EQ(dated.last().target, std::string("/openai/deployments/whisper/audio/transcriptions?api-version=2024-10-21")); +} + +TEST(azure_services, responses_calls) { + Rig rig(key_settings()); + ResponsesService r(rig.client); + Json b = Json::parse(R"({"model":"gpt41","input":"hi"})"); + r.create(b); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/responses")); + ASSERT_TRUE(rejects([&] { r.create(Json::parse(R"({"input":"hi"})")); }, "model")); + ASSERT_TRUE(rejects([&] { r.create(Json::parse(R"({"model":"x","input":"hi","stream":true})")); }, "stream()")); + r.retrieve("resp_1"); + ASSERT_EQ(rig.last().method, std::string("GET")); + r.cancel("resp_1"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/responses/resp_1/cancel")); + r.remove("resp_1"); + ASSERT_EQ(rig.last().method, std::string("DELETE")); + ListParams p; + p.limit = 10; + p.order = "asc"; + r.input_items("resp_1", p); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/responses/resp_1/input_items?limit=10&order=asc")); + Rig dated(key_settings("2025-04-01-preview")); + ResponsesService d(dated.client); + d.create(b); + ASSERT_EQ(dated.last().target, std::string("/openai/responses?api-version=2025-04-01-preview")); +} + +TEST(azure_services, assistants_threads_runs) { + Rig rig(key_settings("2025-04-01-preview")); + AssistantsService a(rig.client); + a.create_assistant(Json::parse(R"({"model":"gpt4o","name":"x"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/assistants?api-version=2025-04-01-preview")); + ASSERT_TRUE(rejects([&] { a.create_assistant(Json::object()); }, "model")); + a.create_thread(); + ASSERT_EQ(rig.last().target, std::string("/openai/threads?api-version=2025-04-01-preview")); + a.create_message("thread_1", Json::parse(R"({"role":"user","content":"hi"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/threads/thread_1/messages?api-version=2025-04-01-preview")); + a.create_run("thread_1", Json::parse(R"({"assistant_id":"asst_1"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/threads/thread_1/runs?api-version=2025-04-01-preview")); + a.submit_tool_outputs("thread_1", "run_1", Json::parse(R"([{"tool_call_id":"c1","output":"4"}])")); + ASSERT_EQ(rig.last().target, + std::string("/openai/threads/thread_1/runs/run_1/submit_tool_outputs?api-version=2025-04-01-preview")); + a.get_run_step("thread_1", "run_1", "step_1"); + ASSERT_TRUE(contains(rig.last().target, "/steps/step_1")); + ASSERT_TRUE(rejects([&] { a.create_run("thread_1", Json::object()); }, "assistant_id")); + ASSERT_TRUE(rejects([&] { a.create_message("../x", Json::object()); }, "bad thread id")); + ASSERT_TRUE(rejects([&] { a.submit_tool_outputs("t", "r", Json::array()); }, "non-empty")); + a.remove_thread("thread_1"); + ASSERT_EQ(rig.last().method, std::string("DELETE")); + a.cancel_run("thread_1", "run_1"); + ASSERT_EQ(rig.last().method, std::string("POST")); +} + +TEST(azure_services, batches_and_fine_tuning) { + Rig rig(key_settings()); + BatchesService b(rig.client); + b.create(Json::parse(R"({"input_file_id":"file-1","endpoint":"/chat/completions","completion_window":"24h"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/batches")); + ASSERT_TRUE(rejects([&] { b.create(Json::parse(R"({"endpoint":"/chat/completions","completion_window":"24h"})")); }, "input_file_id")); + b.cancel("batch_1"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/batches/batch_1/cancel")); + FineTuningService f(rig.client); + f.create(Json::parse(R"({"model":"gpt-4o-mini","training_file":"file-1"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/fine_tuning/jobs")); + ASSERT_TRUE(rejects([&] { f.create(Json::parse(R"({"model":"gpt-4o-mini"})")); }, "training_file")); + f.events("ftjob-1"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/fine_tuning/jobs/ftjob-1/events")); + f.pause("ftjob-1"); + f.resume("ftjob-1"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/fine_tuning/jobs/ftjob-1/resume")); + f.remove("ftjob-1"); // dated only: fallback version + ASSERT_EQ(rig.last().method, std::string("DELETE")); + ASSERT_EQ(rig.last().target, std::string("/openai/fine_tuning/jobs/ftjob-1?api-version=2025-04-01-preview")); + f.copy_checkpoint("ftjob-1", "ftckpt-1", Json::parse(R"({"destinationResourceId":"/subscriptions/x","region":"eastus"})")); + ASSERT_TRUE(contains(rig.last().target, "/checkpoints/ftckpt-1/copy")); + ASSERT_TRUE(rejects([&] { f.copy_checkpoint("j", "c", Json::object()); }, "destinationResourceId")); +} + +TEST(azure_services, files_and_uploads) { + Rig rig(key_settings()); + FilesService f(rig.client); + f.upload(UploadFile{"train.jsonl", "application/jsonl", "{}\n"}, "fine-tune"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/files")); + ASSERT_TRUE(contains(rig.last().body, part("purpose", "fine-tune"))); + ASSERT_TRUE(contains(rig.last().body, "filename=\"train.jsonl\"")); + ASSERT_TRUE(rejects([&] { f.upload(UploadFile{"a", "x", ""}, "batch"); }, "empty")); + ASSERT_TRUE(rejects([&] { f.upload(UploadFile{"a", "x", "d"}, ""); }, "purpose")); + ASSERT_TRUE(rejects([&] { f.upload(UploadFile{"a\nb", "x", "d"}, "batch"); }, "control")); + rig.openai().content_type = "application/octet-stream"; + rig.openai().answer = "RAW"; + ASSERT_EQ(f.content("file-1").body, std::string("RAW")); + rig.openai().content_type = "application/json"; + rig.openai().answer = "{}"; + f.remove("file-1"); + ASSERT_EQ(rig.last().method, std::string("DELETE")); + f.start_upload(Json::parse(R"({"purpose":"batch","filename":"big.jsonl","bytes":123,"mime_type":"application/jsonl"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/uploads?api-version=2025-04-01-preview")); + f.add_part("upload_1", UploadFile{"p", "application/octet-stream", "abc"}); + ASSERT_TRUE(contains(rig.last().target, "/uploads/upload_1/parts")); + f.complete_upload("upload_1", {"part_1", "part_2"}, "d41d8cd98f00b204e9800998ecf8427e"); + ASSERT_TRUE(contains(rig.last().body, "\"part_ids\":[\"part_1\",\"part_2\"]")); + ASSERT_TRUE(rejects([&] { f.complete_upload("upload_1", {}); }, "part_ids")); + f.cancel_upload("upload_1"); + ASSERT_TRUE(contains(rig.last().target, "/uploads/upload_1/cancel")); +} + +TEST(azure_services, vector_stores) { + Rig rig(key_settings()); + VectorStoresService v(rig.client); + v.create(Json::parse(R"({"name":"docs"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/vector_stores")); + v.search("vs_1", Json::parse(R"({"query":"what"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/vector_stores/vs_1/search")); + ASSERT_TRUE(rejects([&] { v.search("vs_1", Json::object()); }, "query")); + v.add_file("vs_1", Json::parse(R"({"file_id":"file-1"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/vector_stores/vs_1/files")); + v.list_files("vs_1", ListParams(), "completed"); + ASSERT_TRUE(contains(rig.last().target, "filter=completed")); + ASSERT_TRUE(rejects([&] { v.list_files("vs_1", ListParams(), "weird"); }, "filter")); + v.create_file_batch("vs_1", Json::parse(R"({"file_ids":["a","b"]})")); + ASSERT_TRUE(rejects([&] { v.create_file_batch("vs_1", Json::object()); }, "file_ids")); + v.update_file_attributes("vs_1", "file-1", Json::parse(R"({"team":"x"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/vector_stores/vs_1/files/file-1")); + v.remove_file("vs_1", "file-1"); + ASSERT_EQ(rig.last().method, std::string("DELETE")); + v.cancel_file_batch("vs_1", "vsfb_1"); + ASSERT_TRUE(contains(rig.last().target, "/file_batches/vsfb_1/cancel")); +} + +TEST(azure_services, models) { + Rig rig(key_settings()); + // Documented example: models list shape (data[].id). + rig.openai().answer = R"({"object":"list","data":[{"id":"gpt-4o","object":"model"},{"id":"text-embedding-3-small","object":"model"}]})"; + ModelsService m(rig.client); + auto ids = m.ids(); + ASSERT_EQ(ids.size(), static_cast(2)); + ASSERT_EQ(ids[0], std::string("gpt-4o")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/models")); + m.get("gpt-4o"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/models/gpt-4o")); + m.remove("ft:gpt-4o:org::abc"); + ASSERT_EQ(rig.last().method, std::string("DELETE")); +} + +TEST(azure_services, foundry_model_inference) { + Rig rig(key_settings()); + FoundryService f(rig.client); + f.info(); + ASSERT_EQ(rig.foundry().seen.back().target, std::string("/models/info?api-version=2024-05-01-preview")); + Json body = Json::parse(R"({"messages":[{"role":"user","content":"hi"}],"max_tokens":8,"model":"Phi-4"})"); + f.chat(body, "drop"); + const Seen& s = rig.foundry().seen.back(); + ASSERT_EQ(s.target, std::string("/models/chat/completions?api-version=2024-05-01-preview")); + ASSERT_EQ(s.headers.get_or("extra-parameters", ""), std::string("drop")); + ASSERT_EQ(s.headers.get_or("api-key", ""), std::string("az-test-KEY")); + f.chat(body); + ASSERT_FALSE(rig.foundry().seen.back().headers.has("extra-parameters")); + ASSERT_TRUE(rejects([&] { f.chat(body, "maybe"); }, "extra_parameters")); + ASSERT_TRUE(rejects([&] { f.chat(Json::object()); }, "messages")); + f.embeddings(Json::parse(R"({"input":["a","b"],"dimensions":256})")); + ASSERT_TRUE(contains(rig.foundry().seen.back().target, "/models/embeddings")); + ASSERT_TRUE(rejects([&] { f.embeddings(Json::parse(R"({"input":"a"})")); }, "array")); + f.image_embeddings(Json::parse(R"({"input":[{"image":"data:image/png;base64,AA=="}]})")); + ASSERT_TRUE(contains(rig.foundry().seen.back().target, "/models/images/embeddings")); + Rig dated(key_settings("2024-10-21")); // the OpenAI api version does not move the Foundry one + FoundryService g(dated.client); + g.info(); + ASSERT_EQ(dated.foundry().seen.back().target, std::string("/models/info?api-version=2024-05-01-preview")); +} + +TEST(azure_services, content_safety) { + Rig rig(key_settings()); + SafetyService s(rig.client); + s.analyze_text(Json::parse(R"({"text":"hello","categories":["Hate"],"outputType":"FourSeverityLevels"})")); + ASSERT_EQ(rig.safety().seen.back().target, std::string("/contentsafety/text:analyze?api-version=2024-09-01")); + ASSERT_TRUE(rejects([&] { s.analyze_text(Json::object()); }, "text")); + Json big = Json::object(); + big.set("text", Json(std::string(10001, 'x'))); + ASSERT_TRUE(rejects([&] { s.analyze_text(big); }, "10000")); + s.analyze_image(Json::parse(R"({"image":{"content":"AAAA"}})")); + ASSERT_TRUE(contains(rig.safety().seen.back().target, "/contentsafety/image:analyze")); + ASSERT_TRUE(rejects([&] { s.analyze_image(Json::parse(R"({"image":{}})")); }, "content")); + s.shield_prompt(Json::parse(R"({"userPrompt":"ignore previous","documents":["d"]})")); + ASSERT_TRUE(contains(rig.safety().seen.back().target, "text:shieldPrompt")); + ASSERT_TRUE(rejects([&] { s.shield_prompt(Json::parse(R"({"documents":["1","2","3","4","5","6"]})")); }, "5")); + s.detect_protected_material("some text"); + ASSERT_TRUE(contains(rig.safety().seen.back().target, "text:detectProtectedMaterial")); + + s.create_or_update_blocklist("my.list_1", "desc"); + const Seen& p = rig.safety().seen.back(); + ASSERT_EQ(p.method, std::string("PATCH")); + ASSERT_EQ(p.headers.get_or("Content-Type", ""), std::string("application/merge-patch+json")); + ASSERT_EQ(p.target, std::string("/contentsafety/text/blocklists/my.list_1?api-version=2024-09-01")); + for (const char* bad : {"", "a b", "a/b", "a:b"}) + ASSERT_TRUE(rejects([&] { s.get_blocklist(bad); }, "blocklist name")); + ASSERT_TRUE(rejects([&] { s.get_blocklist(std::string(65, 'a')); }, "1..64")); + s.add_blocklist_items("my-list", Json::parse(R"([{"description":"d","text":"bad word"}])")); + ASSERT_TRUE(contains(rig.safety().seen.back().target, "my-list:addOrUpdateBlocklistItems")); + ASSERT_TRUE(contains(rig.safety().seen.back().body, "blocklistItems")); + ASSERT_TRUE(rejects([&] { s.add_blocklist_items("my-list", Json::array()); }, "non-empty")); + Json many = Json::array(); + for (int i = 0; i < 101; ++i) many.push(Json::parse(R"({"text":"x"})")); + ASSERT_TRUE(rejects([&] { s.add_blocklist_items("my-list", many); }, "100")); + ASSERT_TRUE(rejects([&] { s.add_blocklist_items("my-list", Json::parse(R"([{"text":")" + std::string(129, 'x') + R"("}])")); }, "128")); + s.remove_blocklist_items("my-list", {"id1"}); + ASSERT_TRUE(contains(rig.safety().seen.back().body, "blocklistItemIds")); + s.list_blocklist_items("my-list", 50, 10); + ASSERT_TRUE(contains(rig.safety().seen.back().target, "maxpagesize=50&skip=10&api-version=2024-09-01")); + s.remove_blocklist("my-list"); + ASSERT_EQ(rig.safety().seen.back().method, std::string("DELETE")); +} + +TEST(azure_services, realtime_rest) { + Rig rig(key_settings()); + RealtimeService r(rig.client); + r.create_client_secret(Json::parse(R"({"expires_after":{"anchor":"created_at","seconds":600},"session":{"type":"realtime","model":"gpt-realtime"}})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/realtime/client_secrets")); + ASSERT_TRUE(rejects([&] { r.create_client_secret(Json::parse(R"({"expires_after":{"seconds":5}})")); }, "10")); + ASSERT_TRUE(rejects([&] { r.create_client_secret(Json::parse(R"({"expires_after":{"seconds":9999}})")); }, "7200")); + r.create_session(Json::parse(R"({"instructions":"be brief"})")); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/realtime/sessions")); + r.hangup_call("call_1"); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/realtime/calls/call_1/hangup")); + r.reject_call("call_1", 603); + ASSERT_TRUE(rejects([&] { r.reject_call("call_1", 200); }, "400")); + ASSERT_TRUE(rejects([&] { r.accept_call("call_1", Json::object()); }, "type")); + Rig dated(key_settings("2025-04-01-preview")); + RealtimeService d(dated.client); + d.create_session(Json::parse(R"({"model":"gpt-4o-realtime-preview"})")); + ASSERT_EQ(dated.last().target, std::string("/openai/realtimeapi/sessions?api-version=2025-04-01-preview")); + d.create_transcription_session(); + ASSERT_EQ(dated.last().target, std::string("/openai/realtimeapi/transcription_sessions?api-version=2025-04-01-preview")); +} + +TEST(azure_services, generic_operations_plan_every_op_in_every_mode) { + for (const char* version : {"", "preview", "2024-10-21", "2025-04-01-preview"}) { + Rig rig(key_settings(version)); + OperationsService ops(rig.client); + std::size_t planned = 0; + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& c = Calls::at(i); + if (!OperationsService::generic(c)) continue; + AzureService g(rig.client, c.surface); + // Both sides of an operation take the same number of ids (checked by gen_calls.py). + Side any = c.v1.present() ? Side::kV1 : Side::kDated; + Target t{"dep1", std::vector(Calls::id_count(c.op, any), "id1")}; + Side side = g.route(c.op, t).side; + auto plan = ops.plan(c.name, t); + ASSERT_TRUE(plan.path.rfind(Calls::prefix(c.op, side), 0) == 0); + ASSERT_EQ(plan.method, std::string(c.method)); + ++planned; + } + ASSERT_TRUE(planned > 120); + } +} + +TEST(azure_services, generic_operations_reject_what_the_spec_rejects) { + Rig rig(key_settings()); + OperationsService ops(rig.client); + ASSERT_TRUE(rejects([&] { ops.invoke("NoSuchCall", {}); }, "unknown operation")); + ASSERT_TRUE(rejects([&] { ops.invoke("CreateFile", {}); }, "typed service")); + ASSERT_TRUE(rejects([&] { ops.invoke("CreateSpeech", {"d", {}}); }, "typed service")); + Json bad = Json::parse(R"({"bogus":1})"); + ASSERT_TRUE(rejects([&] { ops.invoke("CreateConversation", {}, &bad); }, "bogus")); + ASSERT_TRUE(rejects([&] { ops.invoke("RetrieveConversation", {"", {"../x"}}); }, "bad id")); + Json body = Json::parse(R"({"metadata":{"a":"b"}})"); + ops.invoke("CreateConversation", {}, &body); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/conversations")); + ASSERT_TRUE(rejects([&] { ops.invoke("RetrieveConversation", {"", {"c1"}}, &body); }, "no request body")); + ops.invoke("ListContainers", {}, nullptr, {{"limit", "5"}}); + ASSERT_EQ(rig.last().target, std::string("/openai/v1/containers?limit=5")); + ASSERT_TRUE(rejects([&] { ops.invoke("ListContainers", {}, nullptr, {{"zzz", "5"}}); }, "zzz")); + ASSERT_TRUE(OperationsService::destructive(*Calls::find("DeleteContainer"))); + ASSERT_TRUE(OperationsService::destructive(*Calls::find("CancelEvalRun"))); + ASSERT_TRUE(OperationsService::destructive(*Calls::find("HangupRealtimeCall"))); + ASSERT_FALSE(OperationsService::destructive(*Calls::find("ListContainers"))); + ASSERT_FALSE(OperationsService::destructive(*Calls::find("CreateChatCompletion"))); +} diff --git a/tests/src/vendors/azure/test_settings.cpp b/tests/src/vendors/azure/test_settings.cpp new file mode 100644 index 0000000..3af0bba --- /dev/null +++ b/tests/src/vendors/azure/test_settings.cpp @@ -0,0 +1,115 @@ +// Azure: api version parsing, endpoint rules, settings from the environment. +#include + +#include "rig.h" +#include "vendors/azure/api_version.h" +#include "vendors/azure/settings.h" + +using namespace molo::azure; +using namespace az_test; + +TEST(azure_settings, api_version_modes) { + ASSERT_TRUE(ApiVersion::parse("").mode() == ApiVersion::Mode::kV1); + ASSERT_TRUE(ApiVersion::parse("v1").mode() == ApiVersion::Mode::kV1); + ASSERT_EQ(ApiVersion::parse("v1").v1_query(), std::string("")); + ASSERT_TRUE(ApiVersion::parse("preview").mode() == ApiVersion::Mode::kPreview); + ASSERT_EQ(ApiVersion::parse("preview").v1_query(), std::string("preview")); + ApiVersion d = ApiVersion::parse("2024-10-21"); + ASSERT_TRUE(d.is_dated()); + ASSERT_EQ(d.value(), std::string("2024-10-21")); + ASSERT_TRUE(ApiVersion::parse("2025-04-01-preview").is_dated()); + ASSERT_TRUE(ApiVersion::parse("2025-04-01-preview").is_dated() && !ApiVersion::parse("preview").is_dated()); +} + +TEST(azure_settings, api_version_rejects_junk) { + for (const char* bad : {"latest", "2024-10", "2024-10-21-beta", "20241021", "v2", "2024-10-21 ", "../x"}) + ASSERT_TRUE(rejects([&] { ApiVersion::parse(bad); }, "api version")); +} + +TEST(azure_settings, endpoint_normalization) { + ASSERT_EQ(Settings::normalize_endpoint("https://a.openai.azure.com/"), std::string("https://a.openai.azure.com")); + ASSERT_EQ(Settings::normalize_endpoint(" https://a.openai.azure.com "), std::string("https://a.openai.azure.com")); + ASSERT_EQ(Settings::normalize_endpoint(""), std::string("")); + ASSERT_EQ(Settings::normalize_endpoint("http://127.0.0.1:8080"), std::string("http://127.0.0.1:8080")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("http://a.openai.azure.com"); }, "https")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("a.openai.azure.com"); }, "https")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("https://a.openai.azure.com/openai"); }, "no path")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("https://a.openai.azure.com?x=1"); }, "no path")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("https://user:pw@a.openai.azure.com"); }, "no path")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("https://"); }, "no host")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("https://a b.openai.azure.com"); }, "invalid")); + ASSERT_TRUE(rejects([] { Settings::normalize_endpoint("https://" + std::string(300, 'a') + ".com"); }, "too long")); +} + +TEST(azure_settings, auth_kind_priority) { + Settings s; + s.endpoint = "https://a.openai.azure.com"; + ASSERT_TRUE(s.auth() == AuthKind::kNone); + ASSERT_FALSE(s.configured()); + s.entra.tenant_id = "t"; + s.entra.client_id = "c"; + ASSERT_TRUE(s.auth() == AuthKind::kNone); // secret missing + s.entra.client_secret = "s"; + ASSERT_TRUE(s.auth() == AuthKind::kEntra); + s.api_key = "k"; + ASSERT_TRUE(s.auth() == AuthKind::kApiKey); // a key wins over Entra + ASSERT_TRUE(s.configured()); +} + +TEST(azure_settings, entra_validation) { + EntraConfig e; + e.tenant_id = "contoso.onmicrosoft.com"; + e.client_id = "11111111-2222-3333-4444-555555555555"; + e.client_secret = "x"; + e.validate(); + ASSERT_EQ(e.token_url(), std::string("https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token")); + EntraConfig bad = e; + bad.tenant_id = "../common"; + ASSERT_TRUE(rejects([&] { bad.validate(); }, "tenant")); + bad = e; + bad.client_id = "a b"; + ASSERT_TRUE(rejects([&] { bad.validate(); }, "client id")); + bad = e; + bad.client_secret = std::string("a\nb"); + ASSERT_TRUE(rejects([&] { bad.validate(); }, "control")); + bad = e; + bad.authority = "https://login.microsoftonline.us/"; + ASSERT_EQ(bad.token_url(), std::string("https://login.microsoftonline.us/contoso.onmicrosoft.com/oauth2/v2.0/token")); + bad.authority = "http://evil.example"; + ASSERT_TRUE(rejects([&] { bad.validate(); }, "https")); +} + +TEST(azure_settings, from_env_reads_every_variable) { + setenv("MOLO_ENV_FILES", "off", 1); + setenv("AZURE_OPENAI_ENDPOINT", "https://r1.openai.azure.com/", 1); + setenv("AZURE_OPENAI_API_KEY", "key-1", 1); + setenv("AZURE_OPENAI_API_VERSION", "2024-10-21", 1); + setenv("AZURE_TENANT_ID", "tenant-1", 1); + setenv("AZURE_CLIENT_ID", "client-1", 1); + setenv("AZURE_CLIENT_SECRET", "secret-1", 1); + Settings s = Settings::from_env(); + ASSERT_EQ(s.endpoint, std::string("https://r1.openai.azure.com")); + ASSERT_EQ(s.foundry_endpoint, s.endpoint); // fallback + ASSERT_EQ(s.safety_endpoint, s.endpoint); + ASSERT_EQ(s.api_key, std::string("key-1")); + ASSERT_EQ(s.safety_key, std::string("key-1")); + ASSERT_TRUE(s.api_version.is_dated()); + ASSERT_EQ(s.entra.tenant_id, std::string("tenant-1")); + ASSERT_EQ(s.entra.client_secret, std::string("secret-1")); + setenv("AZURE_AI_ENDPOINT", "https://r1.services.ai.azure.com", 1); + setenv("AZURE_CONTENT_SAFETY_ENDPOINT", "https://r1.cognitiveservices.azure.com", 1); + setenv("AZURE_CONTENT_SAFETY_KEY", "key-safety", 1); + Settings t = Settings::from_env(); + ASSERT_EQ(t.foundry_endpoint, std::string("https://r1.services.ai.azure.com")); + ASSERT_EQ(t.safety_endpoint, std::string("https://r1.cognitiveservices.azure.com")); + ASSERT_EQ(t.safety_key, std::string("key-safety")); + setenv("AZURE_OPENAI_API_VERSION", "nonsense", 1); + ASSERT_TRUE(rejects([] { Settings::from_env(); }, "api version")); + for (const char* v : {"AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_API_KEY", "AZURE_OPENAI_API_VERSION", + "AZURE_TENANT_ID", "AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_AI_ENDPOINT", + "AZURE_CONTENT_SAFETY_ENDPOINT", "AZURE_CONTENT_SAFETY_KEY"}) + unsetenv(v); + Settings none = Settings::from_env(); + ASSERT_FALSE(none.configured()); + ASSERT_TRUE(none.api_version.mode() == ApiVersion::Mode::kV1); +}