Files
moloapp/src/openrouter/call_plan.h
T

88 lines
3.2 KiB
C++
Raw Normal View History

#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 <map>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#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<std::string, std::string>;
// 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<long>(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<std::pair<std::string, std::string>>& items() const { return items_; }
bool empty() const { return items_.empty(); }
private:
std::vector<std::pair<std::string, std::string>> 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<std::string> body_fields(const CallSpec& spec);
static std::set<std::string> query_fields(const CallSpec& spec);
static std::vector<std::string> 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<std::string, std::string> split_model_id(const std::string& model_id);
} // namespace molo::openrouter