feat(clients): openrouter: whole public API, spec-checked call table, chat model, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#include "openrouter/server_tools.h"
|
||||
|
||||
#include "llm/error.h"
|
||||
#include "openrouter/call_plan.h"
|
||||
|
||||
namespace molo::openrouter {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid(kVendor, "tool: " + why); }
|
||||
} // namespace
|
||||
|
||||
Json ServerTools::function(const std::string& name, const std::string& description,
|
||||
const Json& parameters, bool strict) {
|
||||
if (name.empty() || name.size() > 64) bad("function name must be 1..64 characters");
|
||||
for (char c : name)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '_' || c == '-'))
|
||||
bad("function name may only use letters, digits, _ and -");
|
||||
if (!parameters.is_object()) bad("parameters must be a JSON Schema object");
|
||||
Json fn = Json::object();
|
||||
fn.set("name", Json(name));
|
||||
if (!description.empty()) fn.set("description", Json(description));
|
||||
fn.set("parameters", parameters);
|
||||
if (strict) fn.set("strict", Json(true));
|
||||
Json t = Json::object();
|
||||
t.set("type", Json("function"));
|
||||
t.set("function", fn);
|
||||
return t;
|
||||
}
|
||||
|
||||
Json ServerTools::function(const llm::ToolSpec& spec) {
|
||||
return function(spec.name, spec.description, spec.parameters);
|
||||
}
|
||||
|
||||
Json ServerTools::server(const std::string& type, const Json& parameters) {
|
||||
static const std::string kPrefix = "openrouter:";
|
||||
if (type.rfind(kPrefix, 0) != 0 || type.size() == kPrefix.size() || type.size() > 96)
|
||||
bad("server tool type must look like openrouter:<name>");
|
||||
for (std::size_t i = kPrefix.size(); i < type.size(); ++i) {
|
||||
char c = type[i];
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == ':'))
|
||||
bad("server tool type may only use a-z 0-9 _ :");
|
||||
}
|
||||
Json t = Json::object();
|
||||
t.set("type", Json(type));
|
||||
if (!parameters.is_null()) {
|
||||
if (!parameters.is_object()) bad("parameters must be an object");
|
||||
t.set("parameters", parameters);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
Json ServerTools::web_search(const Json& p) { return server("openrouter:web_search", p); }
|
||||
Json ServerTools::web_fetch(const Json& p) { return server("openrouter:web_fetch", p); }
|
||||
Json ServerTools::datetime(const Json& p) { return server("openrouter:datetime", p); }
|
||||
|
||||
const std::vector<std::string>& ServerTools::known_types() {
|
||||
static const std::vector<std::string> kTypes = {
|
||||
"openrouter:advisor", "openrouter:bash", "openrouter:datetime",
|
||||
"openrouter:files", "openrouter:fusion", "openrouter:image_generation",
|
||||
"openrouter:experimental__search_models", "openrouter:subagent",
|
||||
"openrouter:web_fetch", "openrouter:web_search"};
|
||||
return kTypes;
|
||||
}
|
||||
|
||||
} // namespace molo::openrouter
|
||||
Reference in New Issue
Block a user