#pragma once // RequestPlan: turns one row of the call table plus caller arguments into a // validated request target (path and query). Every id is checked before it is // placed in a path, every query parameter and body field must be one the table // lists, every size is capped (caps.h). Nothing here touches the network. #include #include #include #include "clients/body/json.h" #include "clients/request.h" #include "vendors/huggingface/calls.h" namespace molo::huggingface { // Hub repository kinds the module reads. Kernels exist on the Hub but are // outside this module (SPEC.md ## Gaps). enum class RepoType { kModel, kDataset, kSpace }; const char* repo_type_plural(RepoType type); // models, datasets, spaces bool parse_repo_type(const std::string& text, RepoType& out); // accepts singular or plural // "owner/name" validated once. Legacy ids without an owner (gpt2) are refused // loudly: the Hub answers them with a redirect to the canonical id. class RepoRef { public: static RepoRef parse(const std::string& id); // throws ApiError::invalid const std::string& owner() const { return owner_; } const std::string& name() const { return name_; } std::string id() const { return owner_ + "/" + name_; } private: std::string owner_; std::string name_; }; class PathArgs { public: PathArgs& set(const std::string& name, std::string value); PathArgs& repo(const RepoRef& ref); // namespace + repo PathArgs& repo_type(RepoType type); // repoType + typePrefix const std::string* find(const std::string& name) const; const std::map& all() const { return values_; } private: std::map values_; }; class Query { public: Query& add(const std::string& name, const std::string& value); // repeats allowed Query& add(const std::string& name, long value); Query& flag(const std::string& name, bool value); bool has(const std::string& name) const; // Replaces every value of `name` (page walks set the cursor). Query& replace(const std::string& name, const std::string& value); const std::vector>& items() const { return items_; } private: std::vector> items_; }; struct Plan { Call call; clients::Method method; std::string target; // "/path?query", percent-encoded }; struct RequestPlan { // Throws ApiError::invalid (400 INVALID_ARGUMENT) naming the offending part. static Plan make(Call call, const PathArgs& args, const Query& query, const clients::body::Json* body = nullptr); static std::string encode(const std::string& text); // RFC 3986 unreserved kept // True for a Hub-style name: 1..256 of [A-Za-z0-9._-] and not "." or "..". static bool valid_name(const std::string& text); // Validates a repo-relative file path and returns it percent-encoded per segment. static std::string encode_file_path(const std::string& path); // Validates "owner/name" or a bare "name" model id (router, hf-inference). static std::string check_model(const std::string& model); }; } // namespace molo::huggingface