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,238 @@
|
||||
#include "openrouter/platform.h"
|
||||
|
||||
#include "clients/body/form.h"
|
||||
#include "clients/url.h"
|
||||
#include "llm/error.h"
|
||||
#include "openrouter/admin.h"
|
||||
#include "openrouter/caps.h"
|
||||
#include "openrouter/errors.h"
|
||||
|
||||
namespace molo::openrouter {
|
||||
|
||||
namespace {
|
||||
|
||||
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid(kVendor, why); }
|
||||
|
||||
void need(const Json& body, std::initializer_list<const char*> keys, const char* what) {
|
||||
if (!body.is_object()) bad(std::string(what) + " needs a JSON object");
|
||||
for (const char* k : keys)
|
||||
if (!body.has(k)) bad(std::string(what) + " needs " + k);
|
||||
}
|
||||
|
||||
std::string pick_after(const std::string& after) {
|
||||
if (after.size() > 256) bad("cursor too long");
|
||||
return after;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Json EmbeddingsService::create(const Json& body) const {
|
||||
need(body, {"model", "input"}, "embeddings");
|
||||
const Json& in = *body.find("input");
|
||||
if (in.is_string() ? in.as_string().empty()
|
||||
: (!in.is_array() || in.as_array().empty() || in.as_array().size() > Caps::kMaxEmbeddingInputs))
|
||||
bad("input must be a non-empty string or an array of at most 2048 entries");
|
||||
if (const Json* e = body.find("encoding_format"); e && e->is_string() && e->as_string() != "float" && e->as_string() != "base64")
|
||||
bad("encoding_format must be float or base64");
|
||||
if (const Json* d = body.find("dimensions"); d && (!d->is_number() || d->as_int() < 1)) bad("dimensions must be >= 1");
|
||||
Json doc = run("createEmbeddings", {}, {}, body);
|
||||
throw_if_error_body(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
Json EmbeddingsService::rerank(const std::string& model, const std::string& query,
|
||||
const std::vector<std::string>& documents, std::optional<int> top_n) const {
|
||||
if (model.empty() || query.empty()) bad("rerank needs model and query");
|
||||
if (documents.empty() || documents.size() > Caps::kMaxRerankDocuments) bad("rerank needs 1..1000 documents");
|
||||
if (top_n && *top_n < 1) bad("top_n must be >= 1");
|
||||
Json docs = Json::array();
|
||||
for (const std::string& d : documents) docs.push(Json(d));
|
||||
Json b = Json::object();
|
||||
b.set("model", Json(model));
|
||||
b.set("query", Json(query));
|
||||
b.set("documents", docs);
|
||||
if (top_n) b.set("top_n", Json(*top_n));
|
||||
Json doc = run("createRerank", {}, {}, b);
|
||||
throw_if_error_body(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
std::vector<std::vector<double>> EmbeddingsService::vectors(const Json& response) {
|
||||
std::vector<std::vector<double>> out;
|
||||
const Json* data = response.is_object() ? response.find("data") : nullptr;
|
||||
if (!data || !data->is_array()) bad("embeddings response has no data array");
|
||||
for (const Json& item : data->as_array()) {
|
||||
const Json* e = item.is_object() ? item.find("embedding") : nullptr;
|
||||
if (!e || !e->is_array()) bad("embedding is not a float array (use encoding_format float)");
|
||||
std::vector<double> v;
|
||||
for (const Json& x : e->as_array()) v.push_back(x.is_number() ? x.as_number() : 0);
|
||||
out.push_back(std::move(v));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Json MediaService::generate_image(const Json& body) const {
|
||||
need(body, {"model", "prompt"}, "image generation");
|
||||
return run("createImages", {}, {}, body);
|
||||
}
|
||||
Json MediaService::submit_video(const Json& body) const {
|
||||
need(body, {"model", "prompt"}, "video generation");
|
||||
return run("createVideos", {}, {}, body);
|
||||
}
|
||||
Json MediaService::video_status(const std::string& id) const { return run("getVideos", {{"jobId", id}}); }
|
||||
llm::RawResponse MediaService::video_content(const std::string& id, std::optional<int> index) const {
|
||||
Query q;
|
||||
if (index) {
|
||||
if (*index < 0) bad("index must be >= 0");
|
||||
q.add("index", *index);
|
||||
}
|
||||
return run_raw("listVideosContent", {{"jobId", id}}, q);
|
||||
}
|
||||
llm::RawResponse MediaService::speech(const Json& body) const {
|
||||
need(body, {"model", "input", "voice"}, "speech");
|
||||
return gateway_->call_raw(CallPlanner::plan("createAudioSpeech", {}, {}, &body));
|
||||
}
|
||||
Json MediaService::transcribe(const Json& body) const {
|
||||
need(body, {"model", "input_audio"}, "transcription");
|
||||
return run("createAudioTranscriptions", {}, {}, body);
|
||||
}
|
||||
Json MediaService::decisions(const Json& body) const {
|
||||
need(body, {"questions"}, "decisions");
|
||||
return run("createApiAlphaDecisions", {}, {}, body);
|
||||
}
|
||||
Json MediaService::system_one(const Json& body) const {
|
||||
need(body, {"questions"}, "systemone");
|
||||
return run("createSystemone", {}, {}, body);
|
||||
}
|
||||
|
||||
Json BatchesService::list(std::optional<int> limit, const std::string& after, const std::string& status) const {
|
||||
Query q;
|
||||
if (limit) {
|
||||
if (*limit < 1 || *limit > 1000) bad("limit must be 1..1000");
|
||||
q.add("limit", *limit);
|
||||
}
|
||||
q.add_if("after", pick_after(after));
|
||||
q.add_if("status", status);
|
||||
return run("listBatches", {}, q);
|
||||
}
|
||||
Json BatchesService::get(const std::string& id) const { return run("getBatches", {{"id", id}}); }
|
||||
Json BatchesService::create(const Json& body) const {
|
||||
need(body, {"endpoint", "model", "requests"}, "batch");
|
||||
const Json& r = *body.find("requests");
|
||||
if (!r.is_array() || r.as_array().empty() || r.as_array().size() > Caps::kMaxBatchRequests)
|
||||
bad("requests must be an array of 1..10000");
|
||||
return run("createBatches", {}, {}, body);
|
||||
}
|
||||
Json BatchesService::remove(const std::string& id) const { return run("deleteBatch", {{"id", id}}); }
|
||||
|
||||
Json FilesService::list(std::optional<int> limit, const std::string& cursor) const {
|
||||
Query q;
|
||||
if (limit) {
|
||||
if (*limit < 1 || *limit > 1000) bad("limit must be 1..1000");
|
||||
q.add("limit", *limit);
|
||||
}
|
||||
q.add_if("cursor", pick_after(cursor));
|
||||
return run("listFiles", {}, q);
|
||||
}
|
||||
Json FilesService::metadata(const std::string& id) const { return run("getFileMetadata", {{"file_id", id}}); }
|
||||
llm::RawResponse FilesService::content(const std::string& id) const {
|
||||
return run_raw("downloadFileContent", {{"file_id", id}});
|
||||
}
|
||||
Json FilesService::upload(const std::string& filename, const std::string& content_type,
|
||||
const std::string& data) const {
|
||||
if (filename.empty() || filename.size() > 255 || filename.find_first_of("/\\\"\r\n") != std::string::npos)
|
||||
bad("bad file name");
|
||||
if (data.empty()) bad("file is empty");
|
||||
clients::body::MultipartBody form;
|
||||
form.add_file("file", filename, content_type.empty() ? "application/octet-stream" : content_type, data);
|
||||
return gateway_->call_multipart(CallPlanner::plan("uploadFile"), form);
|
||||
}
|
||||
Json FilesService::remove(const std::string& id) const { return run("deleteFile", {{"file_id", id}}); }
|
||||
Json FilesService::container_files(const std::string& c, std::optional<int> limit, const std::string& after) const {
|
||||
Query q;
|
||||
if (limit) {
|
||||
if (*limit < 1 || *limit > 1000) bad("limit must be 1..1000");
|
||||
q.add("limit", *limit);
|
||||
}
|
||||
q.add_if("after", pick_after(after));
|
||||
return run("listContainerFiles", {{"container_id", c}}, q);
|
||||
}
|
||||
Json FilesService::container_file(const std::string& c, const std::string& f) const {
|
||||
return run("getContainerFile", {{"container_id", c}, {"file_id", f}});
|
||||
}
|
||||
llm::RawResponse FilesService::container_file_content(const std::string& c, const std::string& f) const {
|
||||
return run_raw("downloadContainerFileContent", {{"container_id", c}, {"file_id", f}});
|
||||
}
|
||||
Json FilesService::promote_container_file(const std::string& c, const std::string& f) const {
|
||||
return run("promoteContainerFile", {{"container_id", c}, {"file_id", f}});
|
||||
}
|
||||
|
||||
Json PresetsService::list(std::optional<int> o, std::optional<int> l) const {
|
||||
return run("listPresets", {}, page_query(o, l));
|
||||
}
|
||||
Json PresetsService::get(const std::string& slug) const { return run("getPreset", {{"slug", slug}}); }
|
||||
Json PresetsService::versions(const std::string& slug, std::optional<int> o, std::optional<int> l) const {
|
||||
return run("listPresetVersions", {{"slug", slug}}, page_query(o, l));
|
||||
}
|
||||
Json PresetsService::version(const std::string& slug, const std::string& v) const {
|
||||
return run("getPresetVersion", {{"slug", slug}, {"version", v}});
|
||||
}
|
||||
Json PresetsService::create_from_chat(const std::string& slug, const Json& b) const {
|
||||
return run("createPresetsChatCompletions", {{"slug", slug}}, {}, b);
|
||||
}
|
||||
Json PresetsService::create_from_messages(const std::string& slug, const Json& b) const {
|
||||
return run("createPresetsMessages", {{"slug", slug}}, {}, b);
|
||||
}
|
||||
Json PresetsService::create_from_responses(const std::string& slug, const Json& b) const {
|
||||
return run("createPresetsResponses", {{"slug", slug}}, {}, b);
|
||||
}
|
||||
|
||||
Json CatalogService::tools(std::optional<int> o, std::optional<int> l, const std::string& fmt) const {
|
||||
Query q = page_query(o, l);
|
||||
q.add_if("api_format", fmt);
|
||||
return run("listTools", {}, q);
|
||||
}
|
||||
Json CatalogService::tool(const std::string& name) const { return run("getTool", {{"name", name}}); }
|
||||
Json CatalogService::benchmarks(const Query& q) const { return run("getBenchmarks", {}, q); }
|
||||
Json CatalogService::task_classifications(const std::string& w) const {
|
||||
return run("getTaskClassifications", {}, Query().add_if("window", w));
|
||||
}
|
||||
Json CatalogService::app_rankings(const Query& q) const { return run("getAppRankings", {}, q); }
|
||||
Json CatalogService::rankings_daily(const Query& q) const { return run("getRankingsDaily", {}, q); }
|
||||
Json CatalogService::session_cost(const Query& q) const { return run("getSessionCost", {}, q); }
|
||||
|
||||
std::string AuthService::authorization_url(const std::string& callback_url, const PkcePair& pkce) {
|
||||
if (callback_url.rfind("https://", 0) != 0 && callback_url.rfind("http://localhost", 0) != 0 &&
|
||||
callback_url.rfind("http://127.0.0.1", 0) != 0)
|
||||
bad("callback_url must be https, or localhost / 127.0.0.1");
|
||||
if (pkce.challenge.empty()) bad("PKCE challenge is empty");
|
||||
return "https://openrouter.ai/auth?callback_url=" + clients::Url::encode_component(callback_url) +
|
||||
"&code_challenge=" + clients::Url::encode_component(pkce.challenge) + "&code_challenge_method=S256";
|
||||
}
|
||||
|
||||
Json AuthService::exchange_code(const std::string& code, const std::string& verifier) const {
|
||||
if (code.empty() || code.size() > 512) bad("code must be 1..512 characters");
|
||||
PkcePair::from_verifier(verifier);
|
||||
Json b = Json::object();
|
||||
b.set("code", Json(code));
|
||||
b.set("code_verifier", Json(verifier));
|
||||
b.set("code_challenge_method", Json("S256"));
|
||||
return run("exchangeAuthCodeForAPIKey", {}, {}, b);
|
||||
}
|
||||
Json AuthService::create_code(const Json& body) const {
|
||||
need(body, {"callback_url", "code_challenge"}, "auth code");
|
||||
return run("createAuthKeysCode", {}, {}, body);
|
||||
}
|
||||
Json AuthService::jwks() const { return run("listOauthJwks"); }
|
||||
Json AuthService::exchange_token(const std::string& policy, const std::string& subject_token,
|
||||
const std::string& subject_token_type) const {
|
||||
if (policy.empty() || subject_token.empty() || subject_token_type.empty()) bad("token exchange needs all three values");
|
||||
Json b = Json::object();
|
||||
b.set("grant_type", Json("urn:ietf:params:oauth:grant-type:token-exchange"));
|
||||
b.set("federation_policy_id", Json(policy));
|
||||
b.set("subject_token", Json(subject_token));
|
||||
b.set("subject_token_type", Json(subject_token_type));
|
||||
return run("createOauthToken", {}, {}, b);
|
||||
}
|
||||
|
||||
} // namespace molo::openrouter
|
||||
Reference in New Issue
Block a user