#pragma once // CallPlanner: turns a call name from the table (calls.h) plus path // arguments, query arguments and a JSON body into a PlannedCall, and rejects // anything the table does not declare (unknown call, missing or malformed // path argument, undeclared query parameter, undeclared body field, body on a // call without one). Nothing here touches the network, so every request the // module can send is checkable against the spec-verified table. #include #include #include #include #include #include #include "clients/body/json.h" #include "clients/request.h" #include "openrouter/calls.h" namespace molo::openrouter { using Json = clients::body::Json; inline constexpr const char* kVendor = "openrouter"; using PathArgs = std::map; // Ordered query parameters (order is preserved on the wire). class Query { public: Query& add(const std::string& name, const std::string& value); Query& add(const std::string& name, const char* value) { return add(name, std::string(value)); } Query& add(const std::string& name, long value); Query& add(const std::string& name, int value) { return add(name, static_cast(value)); } Query& add(const std::string& name, double value); Query& add(const std::string& name, bool value); // Adds only when the value is non-empty. Query& add_if(const std::string& name, const std::string& value); const std::vector>& items() const { return items_; } bool empty() const { return items_.empty(); } private: std::vector> items_; }; enum class KeyKind { kApi, kManagement, kNone }; struct PlannedCall { const CallSpec* spec = nullptr; clients::Method method = clients::Method::kGet; std::string path; // placeholders substituted, segments URL-encoded std::string query; // "" or "?a=b&c=d" bool has_body = false; Json body; KeyKind key = KeyKind::kApi; std::string name() const { return spec ? spec->name : ""; } std::string fmt() const { return spec ? spec->fmt : "json"; } std::string target() const { return path + query; } }; class CallPlanner { public: static const CallSpec* find(std::string_view name); // Throws llm::ApiError::invalid for an unknown name. static const CallSpec& spec(std::string_view name); static KeyKind key_kind(const CallSpec& spec); static clients::Method method_of(const CallSpec& spec); static std::set body_fields(const CallSpec& spec); static std::set query_fields(const CallSpec& spec); static std::vector placeholders(const std::string& path_template); // Validates and builds. `body` may be null. Throws llm::ApiError::invalid. static PlannedCall plan(std::string_view name, const PathArgs& args = {}, const Query& query = {}, const Json* body = nullptr); // True when `field` may be sent in the body of `name`. static bool accepts_field(std::string_view name, const std::string& field); }; // Splits "author/slug[:variant]" into its two path segments. Throws // llm::ApiError::invalid when it is not exactly two valid segments. std::pair split_model_id(const std::string& model_id); } // namespace molo::openrouter