feat(clients): openrouter: whole public API, spec-checked call table, chat model, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Route: POST /ai/openrouter/call (generic table call; dry run unless confirm)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& body) { return f.client->post("/ai/openrouter/call", body, "application/json"); };
|
||||
auto get = post("{\"call\":\"getModels\"}");
|
||||
check(get.status == 503 && contains(get.body, "not_configured"), "read without key is 503");
|
||||
check(contains(get.body, "OPENROUTER_API_KEY"), "names the variable to set");
|
||||
auto mgmt = post("{\"call\":\"getCredits\"}");
|
||||
check(mgmt.status == 503 && contains(mgmt.body, "OPENROUTER_MANAGEMENT_KEY"), "management read needs the management key");
|
||||
auto dry = post("{\"call\":\"deleteKeys\",\"args\":{\"hash\":\"abc\"}}");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"), "delete is a dry run");
|
||||
check(contains(dry.body, "DELETE") && contains(dry.body, "/keys/abc"), "dry run shows the request");
|
||||
auto secret = post("{\"call\":\"createBYOKKey\",\"body\":{\"provider\":\"openai\",\"key\":\"sk-verysecret\",\"name\":\"n\"}}");
|
||||
check(secret.status == 200 && !contains(secret.body, "sk-verysecret"), "secrets are redacted in the dry run");
|
||||
auto confirmed = post("{\"call\":\"deleteKeys\",\"args\":{\"hash\":\"abc\"},\"confirm\":true}");
|
||||
check(confirmed.status == 503, "confirmed delete without key is 503");
|
||||
check(post("{\"call\":\"uploadFile\"}").status == 422, "uploads are refused 422");
|
||||
check(post("{\"call\":\"getKey\"}").status == 422, "validation precedes credentials");
|
||||
check(post("{\"call\":\"getModels\",\"confirm\":\"yes\"}").status == 422, "confirm must be a boolean");
|
||||
check(f.client->get("/ai/openrouter/call").status == 405, "get is 405");
|
||||
return finish("ai/openrouter/call");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Route: POST /ai/openrouter/chat (paid: dry run unless confirm)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& body) { return f.client->post("/ai/openrouter/chat", body, "application/json"); };
|
||||
auto dry = post("{\"model\":\"openai/gpt-4o:nitro\",\"prompt\":\"hi\",\"provider\":{\"sort\":\"price\"},\"models\":[\"anthropic/claude-3.5-sonnet\"]}");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"), "chat is a dry run");
|
||||
check(contains(dry.body, "\"sort\":\"price\"") && contains(dry.body, "\"max_tokens\":64"), "dry run shows routing and the token cap");
|
||||
check(post("{\"model\":\"openai/gpt-4o\",\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed chat without key is 503");
|
||||
check(post("{\"prompt\":\"hi\"}").status == 422, "missing model 422");
|
||||
check(post("{\"model\":\"nonsense\",\"prompt\":\"hi\"}").status == 422, "bad model id 422");
|
||||
check(post("{\"model\":\"a/b:bogus\",\"prompt\":\"hi\"}").status == 422, "unknown variant 422");
|
||||
check(post("{\"model\":\"a/b\"}").status == 422, "missing prompt 422");
|
||||
check(post("{\"model\":\"a/b\",\"prompt\":\"x\",\"max_tokens\":100000}").status == 422, "token cap 422");
|
||||
check(post("{\"model\":\"a/b\",\"prompt\":\"x\",\"provider\":{\"quantizations\":[\"fp99\"]}}").status == 422, "bad provider 422");
|
||||
check(post("{\"model\":\"a/b\",\"prompt\":\"x\",\"provider\":[1]}").status == 422, "provider must be an object 422");
|
||||
check(post("oops").status == 422, "invalid JSON 422");
|
||||
check(f.client->get("/ai/openrouter/chat").status == 405, "get is 405");
|
||||
return finish("ai/openrouter/chat");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Routes: POST /ai/openrouter/responses, /messages, /model-ref
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& path, const std::string& body) { return f.client->post(path, body, "application/json"); };
|
||||
auto dry = post("/ai/openrouter/responses", "{\"body\":{\"model\":\"openai/gpt-5.2\",\"input\":\"hi\"}}");
|
||||
check(dry.status == 200 && contains(dry.body, "createResponses"), "responses dry run");
|
||||
check(post("/ai/openrouter/responses", "{\"body\":{\"model\":\"m/x\",\"input\":\"hi\"},\"confirm\":true}").status == 503, "responses without key 503");
|
||||
check(post("/ai/openrouter/responses", "{\"body\":{\"bogus\":1}}").status == 422, "responses undeclared field 422");
|
||||
auto msg = post("/ai/openrouter/messages", "{\"body\":{\"model\":\"anthropic/claude-sonnet\",\"max_tokens\":8,\"messages\":[]}}");
|
||||
check(msg.status == 200 && contains(msg.body, "createMessages"), "messages dry run");
|
||||
check(post("/ai/openrouter/messages", "{\"body\":{\"model\":\"a/b\",\"messages\":[]},\"confirm\":true}").status == 503, "messages without key 503");
|
||||
check(post("/ai/openrouter/messages", "{\"body\":{\"zzz\":1}}").status == 422, "messages undeclared field 422");
|
||||
auto ref = post("/ai/openrouter/model-ref", "{\"model\":\"poolside/laguna-s-2.1:free:nitro\"}");
|
||||
check(ref.status == 200 && contains(ref.body, "\"catalog_id\":\"poolside/laguna-s-2.1:free\""), "model ref resolves the catalog id");
|
||||
check(contains(ref.body, "\"nitro\""), "routing variants listed");
|
||||
check(post("/ai/openrouter/model-ref", "{\"model\":\"x:y:z\"}").status == 422, "bad model ref 422");
|
||||
check(f.client->get("/ai/openrouter/responses").status == 405, "get is 405");
|
||||
check(f.client->get("/ai/openrouter/messages").status == 405, "messages get is 405");
|
||||
return finish("ai/openrouter/inference");
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Route: GET /ai/openrouter (page, no credential needed)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto res = f.client->get("/ai/openrouter");
|
||||
check(res.status == 200, "status 200");
|
||||
check(contains(res.headers.get("Content-Type"), "text/html"), "html");
|
||||
check(contains(res.body, "OpenRouter") && contains(res.body, "viewport"), "page and mobile viewport");
|
||||
check(contains(res.body, "/embedded/ai_openrouter.js"), "script wired");
|
||||
check(f.client->post("/ai/openrouter", "{}", "application/json").status == 405, "post is 405");
|
||||
return finish("ai/openrouter/page");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Route: POST /ai/openrouter/plan (validation only, no credential, no network)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& body) { return f.client->post("/ai/openrouter/plan", body, "application/json"); };
|
||||
auto ok = post("{\"call\":\"getModels\",\"query\":{\"limit\":5,\"sort\":\"newest\"}}");
|
||||
check(ok.status == 200, "valid plan 200");
|
||||
check(contains(ok.body, "/models?limit=5&sort=newest"), "target carries the query");
|
||||
auto key = post("{\"call\":\"getKey\",\"args\":{\"hash\":\"abc\"}}");
|
||||
check(key.status == 200 && contains(key.body, "/keys/abc") && contains(key.body, "\"key\":\"mgmt\""), "management call planned");
|
||||
check(post("{\"call\":\"nope\"}").status == 422, "unknown call 422");
|
||||
check(post("{}").status == 422, "missing call 422");
|
||||
check(post("{\"call\":\"getKey\"}").status == 422, "missing path argument 422");
|
||||
check(post("{\"call\":\"getKey\",\"args\":{\"hash\":\"a/b\"}}").status == 422, "bad path argument 422");
|
||||
check(post("{\"call\":\"getModels\",\"query\":{\"bogus\":1}}").status == 422, "undeclared query 422");
|
||||
check(post("{\"call\":\"sendChatCompletionRequest\",\"body\":{\"nonsense\":1}}").status == 422, "undeclared body field 422");
|
||||
check(post("not json").status == 422, "invalid JSON 422");
|
||||
check(post("[1]").status == 422, "non-object body 422");
|
||||
check(f.client->get("/ai/openrouter/plan").status == 405, "get is 405");
|
||||
return finish("ai/openrouter/plan");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Routes: GET /ai/openrouter/status and /ai/openrouter/calls
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto res = f.client->get("/ai/openrouter/status");
|
||||
check(res.status == 200, "status 200");
|
||||
check(contains(res.body, "\"configured\":false"), "reports no API key");
|
||||
check(contains(res.body, "\"management_configured\":false"), "reports no management key");
|
||||
check(contains(res.body, "OPENROUTER_MANAGEMENT_KEY"), "names the management key variable");
|
||||
check(contains(res.body, "\"calls\":140"), "reports the call count");
|
||||
auto calls = f.client->get("/ai/openrouter/calls");
|
||||
check(calls.status == 200, "calls 200");
|
||||
check(contains(calls.body, "sendChatCompletionRequest") && contains(calls.body, "/chat/completions"), "table lists chat");
|
||||
check(contains(calls.body, "\"key\":\"mgmt\""), "table lists management calls");
|
||||
check(f.client->post("/ai/openrouter/status", "{}", "application/json").status == 405, "post is 405");
|
||||
check(f.client->post("/ai/openrouter/calls", "{}", "application/json").status == 405, "calls post is 405");
|
||||
return finish("ai/openrouter/status");
|
||||
}
|
||||
Reference in New Issue
Block a user