147 lines
4.4 KiB
C++
147 lines
4.4 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
// Request plugins (`plugins` array on chat, responses and messages requests;
|
||
|
|
// https://openrouter.ai/docs/guides/features/plugins). One class per plugin
|
||
|
|
// the spec schemas define fields for; the remaining spec plugins
|
||
|
|
// (pareto-router, fusion, switchyard-router, jev-router, alignment, armor,
|
||
|
|
// auto-beta-router) go through RawPlugin, which checks the id against the
|
||
|
|
// spec's list and passes the object through.
|
||
|
|
//
|
||
|
|
// Notes from the docs: the `transforms` request parameter is gone, the
|
||
|
|
// ContextCompressionPlugin (middle-out) replaces it; the ":online" model
|
||
|
|
// variant and the web plugin are deprecated in favour of the
|
||
|
|
// `openrouter:web_search` server tool (see server_tools.h).
|
||
|
|
|
||
|
|
#include <memory>
|
||
|
|
#include <optional>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "clients/body/json.h"
|
||
|
|
|
||
|
|
namespace molo::openrouter {
|
||
|
|
|
||
|
|
class Plugin {
|
||
|
|
public:
|
||
|
|
virtual ~Plugin() = default;
|
||
|
|
virtual std::string id() const = 0;
|
||
|
|
// Throws llm::ApiError::invalid.
|
||
|
|
virtual void validate() const {}
|
||
|
|
// The object as sent: {"id": ..., ...}. Calls validate().
|
||
|
|
clients::body::Json to_json() const;
|
||
|
|
|
||
|
|
// false disables the plugin for this request even when the account enables it.
|
||
|
|
std::optional<bool> enabled;
|
||
|
|
|
||
|
|
static const std::vector<std::string>& known_ids();
|
||
|
|
static bool is_known(const std::string& id);
|
||
|
|
|
||
|
|
protected:
|
||
|
|
virtual void fill(clients::body::Json& object) const = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
using PluginPtr = std::shared_ptr<const Plugin>;
|
||
|
|
|
||
|
|
// "web": engine native exa parallel firecrawl perplexity auto.
|
||
|
|
class WebSearchPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::string engine;
|
||
|
|
std::optional<int> max_results;
|
||
|
|
std::optional<int> max_uses;
|
||
|
|
std::string search_prompt;
|
||
|
|
std::string mode; // instant fast auto deep-lite deep deep-reasoning turbo basic advanced
|
||
|
|
std::vector<std::string> include_domains;
|
||
|
|
std::vector<std::string> exclude_domains;
|
||
|
|
std::string id() const override { return "web"; }
|
||
|
|
void validate() const override;
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json& object) const override;
|
||
|
|
};
|
||
|
|
|
||
|
|
// "file-parser": pdf engine mistral-ocr native cloudflare-ai pdf-text.
|
||
|
|
class FileParserPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::string pdf_engine;
|
||
|
|
std::string id() const override { return "file-parser"; }
|
||
|
|
void validate() const override;
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json& object) const override;
|
||
|
|
};
|
||
|
|
|
||
|
|
// "response-healing": repairs malformed JSON from structured-output requests.
|
||
|
|
class ResponseHealingPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::string id() const override { return "response-healing"; }
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json&) const override {}
|
||
|
|
};
|
||
|
|
|
||
|
|
class ModerationPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::string id() const override { return "moderation"; }
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json&) const override {}
|
||
|
|
};
|
||
|
|
|
||
|
|
// "context-compression": middle-out truncation of prompts over the context.
|
||
|
|
class ContextCompressionPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::string id() const override { return "context-compression"; }
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json& object) const override;
|
||
|
|
};
|
||
|
|
|
||
|
|
// "auto-router": model patterns may use wildcards ("anthropic/*").
|
||
|
|
class AutoRouterPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::vector<std::string> allowed_models;
|
||
|
|
std::vector<std::string> excluded_models;
|
||
|
|
std::string cost_tier; // low medium high xhigh max
|
||
|
|
std::optional<bool> pin_model;
|
||
|
|
std::string id() const override { return "auto-router"; }
|
||
|
|
void validate() const override;
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json& object) const override;
|
||
|
|
};
|
||
|
|
|
||
|
|
// "web-fetch".
|
||
|
|
class WebFetchPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
std::vector<std::string> allowed_domains;
|
||
|
|
std::vector<std::string> blocked_domains;
|
||
|
|
std::optional<int> max_content_tokens;
|
||
|
|
std::optional<int> max_uses;
|
||
|
|
std::string id() const override { return "web-fetch"; }
|
||
|
|
void validate() const override;
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json& object) const override;
|
||
|
|
};
|
||
|
|
|
||
|
|
// A spec plugin without a typed class: id plus a free-form object.
|
||
|
|
class RawPlugin : public Plugin {
|
||
|
|
public:
|
||
|
|
RawPlugin(std::string id, clients::body::Json fields);
|
||
|
|
std::string id() const override { return id_; }
|
||
|
|
void validate() const override;
|
||
|
|
|
||
|
|
protected:
|
||
|
|
void fill(clients::body::Json& object) const override;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::string id_;
|
||
|
|
clients::body::Json fields_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// The `plugins` array. Throws when over Caps::kMaxPlugins, on a null entry
|
||
|
|
// or on a repeated plugin id.
|
||
|
|
clients::body::Json plugins_to_json(const std::vector<PluginPtr>& plugins);
|
||
|
|
|
||
|
|
} // namespace molo::openrouter
|