feat(clients): compat: OpenAI-compatible provider registry (39 rows), chat model, services, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// Routes: GET /ai/compat (page), /status, /providers, /calls (no credential needed)
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
#include "vendors/compat/registry.h"
|
||||
|
||||
using namespace molo::example::test;
|
||||
namespace cp = molo::compat;
|
||||
|
||||
int main() {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
for (std::size_t i = 0; i < cp::Registry::count(); ++i) {
|
||||
const cp::ProviderRow& r = cp::Registry::at(i);
|
||||
unsetenv(r.key_env);
|
||||
if (r.base_env[0]) unsetenv(r.base_env);
|
||||
if (r.extra_env[0]) unsetenv(r.extra_env);
|
||||
}
|
||||
Fixture f;
|
||||
auto page = f.client->get("/ai/compat");
|
||||
check(page.status == 200, "page 200");
|
||||
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
|
||||
check(contains(page.body, "OpenAI-compatible providers") && contains(page.body, "viewport"), "title and mobile viewport");
|
||||
check(contains(page.body, "/embedded/ai_compat.js"), "script wired");
|
||||
check(f.client->post("/ai/compat", "{}", "application/json").status == 405, "post page is 405");
|
||||
|
||||
auto st = f.client->get("/ai/compat/status");
|
||||
check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without a key");
|
||||
check(contains(st.body, "GROQ_API_KEY") && contains(st.body, "CLOUDFLARE_API_TOKEN"), "status names credential envs");
|
||||
check(contains(st.body, "\"configured_providers\":0"), "no provider configured");
|
||||
check(f.client->post("/ai/compat/status", "{}", "application/json").status == 405, "post status is 405");
|
||||
|
||||
auto prov = f.client->get("/ai/compat/providers");
|
||||
check(prov.status == 200, "providers 200");
|
||||
for (std::size_t i = 0; i < cp::Registry::count(); ++i) {
|
||||
const cp::ProviderRow& r = cp::Registry::at(i);
|
||||
check(contains(prov.body, std::string("\"id\":\"") + r.id + "\""), std::string("providers lists ") + r.id);
|
||||
}
|
||||
check(contains(prov.body, "https://api.groq.com/openai/v1") && contains(prov.body, "docs+probe") &&
|
||||
contains(prov.body, "needs GROQ_API_KEY") == false && contains(prov.body, "\"missing_env\":\"GROQ_API_KEY\""),
|
||||
"provider rows carry base url, evidence and missing env");
|
||||
check(contains(prov.body, "\"missing_env\":\"OLLAMA_BASE_URL\""), "local row names its switch");
|
||||
check(contains(prov.body, "\"missing_env\":\"CLOUDFLARE_ACCOUNT_ID\"") || contains(prov.body, "\"missing_env\":\"CLOUDFLARE_API_TOKEN\""),
|
||||
"cloudflare names a missing env");
|
||||
|
||||
auto calls = f.client->get("/ai/compat/calls");
|
||||
check(calls.status == 200 && contains(calls.body, "/chat/completions") && contains(calls.body, "createEmbedding") &&
|
||||
contains(calls.body, "/audio/speech"),
|
||||
"call table listed");
|
||||
return finish("ai/compat/page");
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// Routes: POST /ai/compat/* (405, 422, dry runs, 503 without a credential)
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
#include "vendors/compat/registry.h"
|
||||
|
||||
using namespace molo::example::test;
|
||||
namespace cp = molo::compat;
|
||||
|
||||
int main() {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
for (std::size_t i = 0; i < cp::Registry::count(); ++i) {
|
||||
const cp::ProviderRow& r = cp::Registry::at(i);
|
||||
unsetenv(r.key_env);
|
||||
if (r.base_env[0]) unsetenv(r.base_env);
|
||||
if (r.extra_env[0]) unsetenv(r.extra_env);
|
||||
}
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& route, const std::string& body) {
|
||||
return f.client->post("/ai/compat/" + route, body, "application/json");
|
||||
};
|
||||
const char* all[] = {"chat", "models", "embeddings", "images", "transcribe", "speech"};
|
||||
for (const char* r : all) {
|
||||
std::string route = std::string("/ai/compat/") + 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");
|
||||
check(post(r, "{}").status == 422, route + " without provider is 422");
|
||||
check(post(r, "{\"provider\":\"nope\"}").status == 422, route + " unknown provider is 422");
|
||||
check(post(r, "{\"provider\":5}").status == 422, route + " non-string provider is 422");
|
||||
}
|
||||
|
||||
// 422: input rules run before anything else
|
||||
check(post("chat", "{\"provider\":\"groq\"}").status == 422, "chat needs prompt");
|
||||
check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\"}").status == 422, "chat needs a model");
|
||||
check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"max_tokens\":-1}").status == 422, "max_tokens -1");
|
||||
check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"extra\":{\"logprobs\":true}}").status == 422,
|
||||
"groq quirk logprobs is 422");
|
||||
check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"extra\":{\"n\":2}}").status == 422, "groq quirk n is 422");
|
||||
check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"temperature\":3}").status == 422, "temperature 3");
|
||||
check(post("models", "{\"provider\":\"dashscope\"}").status == 422, "row without a list endpoint is 422");
|
||||
check(post("embeddings", "{\"provider\":\"cerebras\",\"model\":\"m\",\"input\":\"x\"}").status == 422, "cerebras has no embeddings");
|
||||
check(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\"}").status == 422, "embeddings need input");
|
||||
check(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\",\"input\":[1]}").status == 422, "input must be strings");
|
||||
check(post("images", "{\"provider\":\"together\",\"model\":\"m\",\"prompt\":\"x\",\"n\":11}").status == 422, "n over 10");
|
||||
check(post("images", "{\"provider\":\"groq\",\"model\":\"m\",\"prompt\":\"x\"}").status == 422, "groq has no images");
|
||||
check(post("transcribe", "{\"provider\":\"groq\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"@@\"}").status == 422, "bad base64");
|
||||
check(post("transcribe", "{\"provider\":\"cerebras\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"aGk=\"}").status == 422,
|
||||
"cerebras has no transcription");
|
||||
check(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\"}").status == 422, "speech needs a voice");
|
||||
check(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\",\"voice\":\"v\",\"format\":\"ogg\"}").status == 422, "bad format");
|
||||
|
||||
// dry runs: paid routes show the exact request and the URL
|
||||
auto dry = post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"llama\",\"max_tokens\":8}");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"model\":\"llama\"") &&
|
||||
contains(dry.body, "https://api.groq.com/openai/v1/chat/completions"),
|
||||
"chat is a dry run without confirm");
|
||||
check(contains(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\",\"input\":\"x\"}").body, "/embeddings"), "embeddings dry run");
|
||||
check(contains(post("images", "{\"provider\":\"together\",\"model\":\"m\",\"prompt\":\"x\"}").body, "/images/generations"), "images dry run");
|
||||
check(contains(post("transcribe", "{\"provider\":\"groq\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"aGk=\"}").body,
|
||||
"\"bytes\":2"),
|
||||
"transcribe dry run");
|
||||
check(contains(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\",\"voice\":\"v\"}").body, "/audio/speech"),
|
||||
"speech dry run");
|
||||
check(contains(post("chat", "{\"provider\":\"ollama\",\"prompt\":\"hi\",\"model\":\"m\"}").body, "http://localhost:11434/v1/chat/completions"),
|
||||
"local provider dry run shows its default URL");
|
||||
|
||||
// 503: the free route and confirmed routes need the provider's credential
|
||||
auto models = post("models", "{\"provider\":\"groq\"}");
|
||||
check(models.status == 503 && contains(models.body, "compat_not_configured") && contains(models.body, "GROQ_API_KEY"),
|
||||
"models without key is 503 and names the env");
|
||||
check(post("chat", "{\"provider\":\"groq\",\"prompt\":\"hi\",\"model\":\"m\",\"confirm\":true}").status == 503, "confirmed chat without key is 503");
|
||||
check(post("embeddings", "{\"provider\":\"nvidia\",\"model\":\"m\",\"input\":\"x\",\"confirm\":true}").status == 503, "confirmed embeddings without key is 503");
|
||||
check(post("images", "{\"provider\":\"together\",\"model\":\"m\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed images without key is 503");
|
||||
check(post("transcribe", "{\"provider\":\"groq\",\"model\":\"m\",\"filename\":\"a.wav\",\"data_base64\":\"aGk=\",\"confirm\":true}").status == 503,
|
||||
"confirmed transcribe without key is 503");
|
||||
check(post("speech", "{\"provider\":\"together\",\"model\":\"m\",\"input\":\"x\",\"voice\":\"v\",\"confirm\":true}").status == 503,
|
||||
"confirmed speech without key is 503");
|
||||
auto local = post("models", "{\"provider\":\"ollama\"}");
|
||||
check(local.status == 503 && contains(local.body, "OLLAMA_BASE_URL"), "local provider without base env is 503");
|
||||
auto cf = post("chat", "{\"provider\":\"cloudflare\",\"prompt\":\"hi\",\"model\":\"m\",\"confirm\":true}");
|
||||
check(cf.status == 503, "cloudflare without account is 503");
|
||||
return finish("ai/compat/routes");
|
||||
}
|
||||
Reference in New Issue
Block a user