119 lines
4.4 KiB
C++
119 lines
4.4 KiB
C++
#include "xai/platform/platform_service.h"
|
|||
|
|
|
||
|
|
#include <cctype>
|
||
|
|
|
||
|
|
#include "xai/platform/clients.h"
|
||
|
|
|
||
|
|
namespace molo::xai::platform {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::string encode(const std::string& s) {
|
||
|
|
static const char* hex = "0123456789ABCDEF";
|
||
|
|
std::string out;
|
||
|
|
for (unsigned char c : s) {
|
||
|
|
if (std::isalnum(c) || c == '-' || c == '_' || c == '~' || c == '.') {
|
||
|
|
out.push_back(static_cast<char>(c));
|
||
|
|
} else {
|
||
|
|
out.push_back('%');
|
||
|
|
out.push_back(hex[c >> 4]);
|
||
|
|
out.push_back(hex[c & 15]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
Query& Query::add(const std::string& name, const std::string& value) {
|
||
|
|
pairs_.emplace_back(name, value);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
Query& Query::add_int(const std::string& name, long value) {
|
||
|
|
return add(name, std::to_string(value));
|
||
|
|
}
|
||
|
|
Query& Query::add_bool(const std::string& name, bool value) {
|
||
|
|
return add(name, value ? "true" : "false");
|
||
|
|
}
|
||
|
|
Query& Query::add_if(const std::string& name, const std::string& value) {
|
||
|
|
if (!value.empty()) add(name, value);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
std::string Query::str() const {
|
||
|
|
std::string out;
|
||
|
|
for (const auto& [k, v] : pairs_) {
|
||
|
|
out += out.empty() ? '?' : '&';
|
||
|
|
out += encode(k) + "=" + encode(v);
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
PlatformService::PlatformService(std::shared_ptr<llm::ApiClient> client, Host host)
|
||
|
|
: llm::Service(std::move(client)), host_(host) {}
|
||
|
|
|
||
|
|
void PlatformService::bad(const std::string& why) const {
|
||
|
|
throw llm::ApiError::invalid("xai", why);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool PlatformService::valid_uuid(const std::string& s) {
|
||
|
|
if (s.size() != 36) return false;
|
||
|
|
for (std::size_t i = 0; i < s.size(); ++i) {
|
||
|
|
bool dash = i == 8 || i == 13 || i == 18 || i == 23;
|
||
|
|
if (dash ? s[i] != '-' : !std::isxdigit(static_cast<unsigned char>(s[i]))) return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void PlatformService::require_uuid(const std::string& what, const std::string& s) const {
|
||
|
|
if (!valid_uuid(s)) bad(what + " must be a UUID");
|
||
|
|
}
|
||
|
|
|
||
|
|
void PlatformService::require_text(const std::string& what, const std::string& s,
|
||
|
|
std::size_t max_chars, bool required) const {
|
||
|
|
if (required && s.empty()) bad(what + " is required");
|
||
|
|
if (s.size() > max_chars) bad(what + " is over " + std::to_string(max_chars) + " characters");
|
||
|
|
for (unsigned char c : s)
|
||
|
|
if (c == 0) bad(what + " has a NUL byte");
|
||
|
|
}
|
||
|
|
|
||
|
|
void PlatformService::require_range(const std::string& what, long v, long lo, long hi) const {
|
||
|
|
if (v < lo || v > hi)
|
||
|
|
bad(what + " must be in " + std::to_string(lo) + ".." + std::to_string(hi));
|
||
|
|
}
|
||
|
|
|
||
|
|
void PlatformService::require_confirm(const std::string& what, bool confirm) const {
|
||
|
|
if (!confirm) bad(what + " is destructive or costs money: pass confirm=true");
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string PlatformService::path_of(Call call, std::initializer_list<std::string> segments,
|
||
|
|
const Query& query) const {
|
||
|
|
if (host_of(call) != host_) bad("call belongs to the other xAI host");
|
||
|
|
// The client must carry this host's credential: never send the management
|
||
|
|
// key to the inference host or the API key to the management host.
|
||
|
|
const char* want = host_ == Host::kApi ? PlatformEndpoints::kApiKeyEnv
|
||
|
|
: PlatformEndpoints::kManagementKeyEnv;
|
||
|
|
if (client().config().key_env != want) bad(std::string("client is not configured for ") + want);
|
||
|
|
return render(call, segments) + query.str();
|
||
|
|
}
|
||
|
|
|
||
|
|
Json PlatformService::invoke(Call call, std::initializer_list<std::string> segments,
|
||
|
|
const Json* body, const Query& query) {
|
||
|
|
std::string path = path_of(call, segments, query);
|
||
|
|
return client().request(method_of(call), path, body);
|
||
|
|
}
|
||
|
|
|
||
|
|
llm::RawResponse PlatformService::invoke_raw(Call call,
|
||
|
|
std::initializer_list<std::string> segments,
|
||
|
|
const std::string* body,
|
||
|
|
const std::string& content_type,
|
||
|
|
const Query& query) {
|
||
|
|
std::string path = path_of(call, segments, query);
|
||
|
|
return client().request_raw(method_of(call), path, body, content_type);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json PlatformService::invoke_multipart(Call call, std::initializer_list<std::string> segments,
|
||
|
|
const clients::body::MultipartBody& form) {
|
||
|
|
if (method_of(call) != clients::Method::kPost) bad("multipart needs a POST call");
|
||
|
|
return client().post_multipart(path_of(call, segments, Query()), form);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::xai::platform
|