513 lines
20 KiB
C++
513 lines
20 KiB
C++
#include "../AiDemos.hpp"
|
|
|
|
#include "../../fw/Router.hpp"
|
|
#include "../../fw/Templates.hpp"
|
|
#include "anthropic/core/anthropic.h"
|
|
#include "anthropic/core/caps.h"
|
|
|
|
namespace molo::example::app {
|
|
|
|
// Anthropic Claude (api.anthropic.com) over src/anthropic/core. GET routes need
|
|
// no credential. Routes that call the vendor are POST: 422 on bad input first,
|
|
// then 503 anthropic_not_configured without ANTHROPIC_API_KEY. Anything that
|
|
// costs money (messages, stream, batch create) or deletes is a dry run unless
|
|
// the body has "confirm":true. /ai/anthropic/build only builds and validates
|
|
// the request offline.
|
|
|
|
namespace {
|
|
|
|
namespace core = anthropic::core;
|
|
using Json = clients::body::Json;
|
|
|
|
constexpr std::size_t kMaxBody = 512 * 1024;
|
|
constexpr std::size_t kMaxStreamEventsShown = 200;
|
|
|
|
const char kPage[] = R"ANTHROPIC({% extends "base.html" %}
|
|
{% block content %}
|
|
<h1>Anthropic Claude</h1>
|
|
<p class="hint">Messages, streaming, tools, batches, token counting, models, files,
|
|
skills. Calls that cost money or delete are dry runs until the body says
|
|
<code>"confirm":true</code>.</p>
|
|
<p id="an-config" class="hint">Checking credentials...</p>
|
|
<section class="an">
|
|
<label>Route
|
|
<select id="an-route">
|
|
<option value="build">Build request (offline check)</option>
|
|
<option value="count">Count tokens</option>
|
|
<option value="messages">Messages (paid)</option>
|
|
<option value="stream">Stream (paid)</option>
|
|
<option value="models">Models</option>
|
|
<option value="batches">Batches</option>
|
|
<option value="files">Files</option>
|
|
<option value="skills">Skills</option>
|
|
<option value="profiles">User profiles</option>
|
|
</select></label>
|
|
<label>Body (JSON object) <textarea id="an-body" rows="10"></textarea></label>
|
|
<div class="an-actions">
|
|
<button id="an-send" type="button" class="primary">Send</button>
|
|
<button id="an-reset" type="button">Example body</button>
|
|
</div>
|
|
<p id="an-error" class="error" role="alert"></p>
|
|
<pre id="an-out" class="an-out"></pre>
|
|
</section>
|
|
<style>
|
|
.an label{display:block;margin:.5rem 0}
|
|
.an select,.an textarea{width:100%;box-sizing:border-box}
|
|
.an-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
|
.an-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
|
</style>
|
|
<script src="/embedded/ai_anthropic_core.js"></script>
|
|
{% endblock %}
|
|
)ANTHROPIC";
|
|
|
|
fw::Response json_out(const Json& j, int status = 200) {
|
|
fw::Response res = fw::JsonResponse(j.dump(), status);
|
|
res.headers.set("Cache-Control", "no-store");
|
|
return res;
|
|
}
|
|
|
|
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
|
|
|
template <typename F>
|
|
auto guarded(F&& f) -> decltype(f()) {
|
|
try {
|
|
return f();
|
|
} catch (const core::AnthropicError& e) {
|
|
if (e.kind() == core::ErrorKind::kNotConfigured)
|
|
throw fw::HttpError(503, "anthropic_not_configured", e.message());
|
|
if (e.kind() == core::ErrorKind::kInvalidArgument) throw fw::HttpError(422, "validation_error", e.message());
|
|
throw fw::HttpError(502, "anthropic_error", e.type() + ": " + e.message());
|
|
} catch (const llm::ApiError& e) {
|
|
if (e.type() == "NOT_CONFIGURED") throw fw::HttpError(503, "anthropic_not_configured", e.message());
|
|
if (e.type() == "INVALID_ARGUMENT") throw fw::HttpError(422, "validation_error", e.message());
|
|
throw fw::HttpError(502, "anthropic_error", e.type() + ": " + e.message());
|
|
}
|
|
}
|
|
|
|
Json parse_body(const fw::Request& req) {
|
|
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
if (req.body.empty()) return Json::object();
|
|
try {
|
|
Json j = Json::parse(req.body);
|
|
if (!j.is_object()) bad("body must be a JSON object");
|
|
return j;
|
|
} catch (const clients::body::JsonError&) {
|
|
bad("body is not valid JSON");
|
|
}
|
|
}
|
|
|
|
std::string text(const Json& body, const char* key, bool required = false) {
|
|
const Json* v = body.find(key);
|
|
if (!v || v->is_null()) {
|
|
if (required) bad(std::string(key) + " is required");
|
|
return "";
|
|
}
|
|
if (!v->is_string()) bad(std::string(key) + " must be a string");
|
|
return v->as_string();
|
|
}
|
|
|
|
int number(const Json& body, const char* key, int fallback = 0) {
|
|
const Json* v = body.find(key);
|
|
if (!v || v->is_null()) return fallback;
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
return static_cast<int>(v->as_int());
|
|
}
|
|
|
|
bool confirmed(const Json& body) {
|
|
const Json* c = body.find("confirm");
|
|
if (c && !c->is_bool()) bad("confirm must be true or false");
|
|
return c && c->as_bool();
|
|
}
|
|
|
|
Json dry_run(const std::string& what, const Json& detail) {
|
|
Json o = Json::object();
|
|
o.set("dry_run", Json(true));
|
|
o.set("would", Json(what));
|
|
o.set("request", detail);
|
|
o.set("hint", Json("send confirm:true to run it"));
|
|
return o;
|
|
}
|
|
|
|
// Maps the demo's JSON body onto a typed request; every rule is the module's.
|
|
core::MessageRequest request_from(const Json& body) {
|
|
core::MessageRequest r;
|
|
r.model = text(body, "model", true);
|
|
r.set_max_tokens(number(body, "max_tokens", 256));
|
|
r.system_text = text(body, "system");
|
|
const Json* ms = body.find("messages");
|
|
if (ms && ms->is_array()) {
|
|
for (const Json& m : ms->as_array()) {
|
|
std::string role = text(m, "role", true);
|
|
const Json* c = m.find("content");
|
|
if (!c) bad("every message needs content");
|
|
core::MessageParam p;
|
|
if (role == "user") p.role = core::MessageParam::Role::kUser;
|
|
else if (role == "assistant") p.role = core::MessageParam::Role::kAssistant;
|
|
else if (role == "system") p.role = core::MessageParam::Role::kSystem;
|
|
else bad("role must be user, assistant or system");
|
|
if (c->is_string()) {
|
|
p.text = c->as_string();
|
|
} else if (c->is_array()) {
|
|
for (const Json& b : c->as_array()) p.blocks.push_back(core::ContentBlock::from_json(b));
|
|
} else {
|
|
bad("content must be a string or an array of blocks");
|
|
}
|
|
r.add(std::move(p));
|
|
}
|
|
} else if (!text(body, "prompt").empty()) {
|
|
r.add(core::MessageParam::user(text(body, "prompt")));
|
|
} else {
|
|
bad("messages (array) or prompt (string) is required");
|
|
}
|
|
if (const Json* ts = body.find("tools")) {
|
|
if (!ts->is_array()) bad("tools must be an array");
|
|
for (const Json& t : ts->as_array()) r.add_tool(core::Tool::from_json(t));
|
|
}
|
|
std::string tc = text(body, "tool_choice");
|
|
if (tc == "auto") r.tool_choice = core::ToolChoice::auto_();
|
|
else if (tc == "any") r.tool_choice = core::ToolChoice::any();
|
|
else if (tc == "none") r.tool_choice = core::ToolChoice::none();
|
|
else if (!tc.empty()) r.tool_choice = core::ToolChoice::tool(tc);
|
|
if (const Json* th = body.find("thinking"); th && th->is_object()) {
|
|
std::string t = text(*th, "type", true);
|
|
if (t == "enabled") r.thinking = core::ThinkingConfig::enabled(number(*th, "budget_tokens"));
|
|
else if (t == "adaptive") r.thinking = core::ThinkingConfig::adaptive();
|
|
else if (t == "disabled") r.thinking = core::ThinkingConfig::disabled();
|
|
else if (t == "between_tools") r.thinking = core::ThinkingConfig::between_tools();
|
|
else bad("thinking.type is not valid");
|
|
}
|
|
r.output.effort = text(body, "effort");
|
|
r.speed = text(body, "speed");
|
|
r.user_profile_id = text(body, "user_profile_id");
|
|
if (const Json* ss = body.find("stop_sequences"); ss && ss->is_array())
|
|
for (const Json& s : ss->as_array()) {
|
|
if (!s.is_string()) bad("stop_sequences must be strings");
|
|
r.stop_sequences.push_back(s.as_string());
|
|
}
|
|
if (const Json* b = body.find("betas")) {
|
|
if (!b->is_array()) bad("betas must be an array of strings");
|
|
for (const Json& n : b->as_array()) {
|
|
if (!n.is_string()) bad("betas must be an array of strings");
|
|
r.betas.add_name(n.as_string());
|
|
}
|
|
}
|
|
if (const Json* t = body.find("temperature")) r.temperature = t->is_number() ? t->as_number() : (bad("temperature must be a number"), 0.0);
|
|
return r;
|
|
}
|
|
|
|
core::ApiVersion version_from(const Json& body) {
|
|
std::string v = text(body, "version");
|
|
return v.empty() ? core::kDefaultVersion : core::parse_version(v);
|
|
}
|
|
|
|
Json built(const core::MessageRequest& r, core::ApiVersion v, bool stream) {
|
|
r.validate(v);
|
|
Json o = Json::object();
|
|
o.set("anthropic_version", Json(core::version_name(v)));
|
|
o.set("anthropic_beta", Json(r.effective_betas().header_value()));
|
|
o.set("body", r.to_json(stream, v));
|
|
return o;
|
|
}
|
|
|
|
std::vector<std::string> id_list(const Json& body, const char* key) {
|
|
std::vector<std::string> out;
|
|
if (const Json* a = body.find(key)) {
|
|
if (!a->is_array()) bad(std::string(key) + " must be an array of strings");
|
|
for (const Json& s : a->as_array()) {
|
|
if (!s.is_string()) bad(std::string(key) + " must be an array of strings");
|
|
out.push_back(s.as_string());
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Json page_json(const core::Page& p) {
|
|
Json o = Json::object();
|
|
o.set("data", Json(p.data));
|
|
o.set("has_more", Json(p.has_more));
|
|
o.set("truncated", Json(p.truncated));
|
|
if (!p.next_page.empty()) o.set("next_page", Json(p.next_page));
|
|
if (!p.last_id.empty()) o.set("last_id", Json(p.last_id));
|
|
return o;
|
|
}
|
|
|
|
void need_id(const std::string& id) {
|
|
if (!llm::Service::valid_id(id)) bad("id must be one segment of A-Z a-z 0-9 . _ : -");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void register_ai_anthropic_demo(fw::Router& router, Services& services) {
|
|
auto api = std::make_shared<core::Anthropic>(core::Anthropic::from_env());
|
|
fw::Environment* env = services.templates.get();
|
|
services.templates->add_template("ai_anthropic.html", kPage);
|
|
services.add_feature("ai-anthropic", "/ai/anthropic");
|
|
|
|
router.get("/ai/anthropic", "ai-anthropic-page", [env](fw::Request req) {
|
|
(void)req;
|
|
fw::TemplateCtx ctx;
|
|
ctx["title"] = std::string("Anthropic Claude");
|
|
return fw::Response(200, env->render("ai_anthropic.html", ctx), "text/html; charset=utf-8");
|
|
});
|
|
|
|
router.get("/ai/anthropic/status", "ai-anthropic-status", [api](fw::Request req) {
|
|
(void)req;
|
|
Json o = Json::object();
|
|
o.set("configured", Json(api->configured()));
|
|
o.set("key_env", Json(api->session().options().key_env));
|
|
o.set("base_url", Json(api->session().options().base_url));
|
|
o.set("default_version", Json(core::version_name(core::kDefaultVersion)));
|
|
o.set("calls", Json(static_cast<int>(core::call_count())));
|
|
o.set("betas", Json(static_cast<int>(core::beta_count())));
|
|
o.set("tool_types", Json(static_cast<int>(core::tool_schema_count())));
|
|
o.set("block_types", Json(static_cast<int>(core::block_schema_count())));
|
|
o.set("max_stream_events", Json(static_cast<int>(kMaxStreamEventsShown)));
|
|
return json_out(o);
|
|
});
|
|
|
|
router.get("/ai/anthropic/calls", "ai-anthropic-calls", [](fw::Request req) {
|
|
(void)req;
|
|
Json arr = Json::array();
|
|
for (std::size_t i = 0; i < core::call_count(); ++i) {
|
|
const core::CallSpec& s = core::spec_at(i);
|
|
Json row = Json::object();
|
|
row.set("key", Json(s.key));
|
|
row.set("http", Json(s.http_method));
|
|
row.set("path", Json(s.path));
|
|
row.set("body_fields", Json(s.body_fields));
|
|
row.set("query_params", Json(s.query_params));
|
|
arr.push(row);
|
|
}
|
|
Json o = Json::object();
|
|
o.set("calls", arr);
|
|
return json_out(o);
|
|
});
|
|
|
|
router.get("/ai/anthropic/betas", "ai-anthropic-betas", [](fw::Request req) {
|
|
(void)req;
|
|
Json arr = Json::array();
|
|
for (std::size_t i = 0; i < core::beta_count(); ++i) {
|
|
const core::BetaInfo& b = core::beta_at(i);
|
|
Json row = Json::object();
|
|
row.set("name", Json(b.wire));
|
|
row.set("status", Json(b.status == core::BetaStatus::kCurrent ? "current"
|
|
: b.status == core::BetaStatus::kLegacy ? "legacy"
|
|
: b.status == core::BetaStatus::kOtherPackage ? "admin_package"
|
|
: "unknown"));
|
|
row.set("meaning", Json(b.meaning));
|
|
arr.push(row);
|
|
}
|
|
Json o = Json::object();
|
|
o.set("betas", arr);
|
|
return json_out(o);
|
|
});
|
|
|
|
router.get("/ai/anthropic/tools", "ai-anthropic-tools", [](fw::Request req) {
|
|
(void)req;
|
|
Json arr = Json::array();
|
|
for (std::size_t i = 0; i < core::tool_schema_count(); ++i) {
|
|
const core::ToolSchema& t = core::tool_schema_at(i);
|
|
Json row = Json::object();
|
|
row.set("type", Json(t.type));
|
|
row.set("name", Json(t.name));
|
|
row.set("beta", Json(t.beta));
|
|
row.set("ga", Json(t.ga));
|
|
arr.push(row);
|
|
}
|
|
Json o = Json::object();
|
|
o.set("tools", arr);
|
|
return json_out(o);
|
|
});
|
|
|
|
// Offline: builds and validates, never calls the vendor.
|
|
router.post("/ai/anthropic/build", "ai-anthropic-build", [](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
return guarded([&] { return json_out(built(request_from(body), version_from(body), false)); });
|
|
});
|
|
|
|
router.post("/ai/anthropic/count", "ai-anthropic-count", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
return guarded([&] {
|
|
core::MessageRequest r = request_from(body);
|
|
r.validate(version_from(body));
|
|
core::TokenCount t = api->messages->count_tokens(r);
|
|
return json_out(t.raw);
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/messages", "ai-anthropic-messages", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
return guarded([&] {
|
|
core::MessageRequest r = request_from(body);
|
|
Json plan = built(r, version_from(body), false);
|
|
if (!confirmed(body)) return json_out(dry_run("create a message", plan));
|
|
core::MessageResult res = api->messages->create(r);
|
|
Json o = Json::object();
|
|
o.set("message", res.message.raw);
|
|
o.set("text", Json(res.message.text()));
|
|
o.set("betas_sent", Json(res.betas.header_value()));
|
|
o.set("request_id", Json(res.limits.request_id));
|
|
return json_out(o);
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/stream", "ai-anthropic-stream", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
return guarded([&] {
|
|
core::MessageRequest r = request_from(body);
|
|
Json plan = built(r, version_from(body), true);
|
|
if (!confirmed(body)) return json_out(dry_run("stream a message", plan));
|
|
Json events = Json::array();
|
|
core::StreamResult res = api->messages->stream(r, [&](const core::StreamEvent& e) {
|
|
events.push(e.data);
|
|
return events.as_array().size() < kMaxStreamEventsShown;
|
|
});
|
|
Json o = Json::object();
|
|
o.set("events", events);
|
|
o.set("complete", Json(res.complete));
|
|
o.set("message", res.message.raw);
|
|
return json_out(o);
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/models", "ai-anthropic-models", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string id = text(body, "id");
|
|
if (!id.empty()) need_id(id);
|
|
int limit = number(body, "limit", 0);
|
|
return guarded([&] {
|
|
if (!id.empty()) return json_out(api->models->retrieve(id).raw);
|
|
return json_out(page_json(api->models->list(limit, "", "", id_list(body, "lifecycle"))));
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/batches", "ai-anthropic-batches", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string action = text(body, "action", true);
|
|
std::string id = text(body, "id");
|
|
if (action != "create" && action != "get" && action != "list" && action != "cancel" && action != "delete" &&
|
|
action != "results")
|
|
bad("action must be create, get, list, cancel, delete or results");
|
|
if (action != "create" && action != "list") {
|
|
if (id.empty()) bad("id is required");
|
|
need_id(id);
|
|
}
|
|
return guarded([&] {
|
|
if (action == "list") return json_out(page_json(api->batches->list(number(body, "limit", 0))));
|
|
if (action == "get") return json_out(api->batches->retrieve(id).raw);
|
|
if (action == "create") {
|
|
std::vector<core::BatchRequest> reqs;
|
|
const Json* rs = body.find("requests");
|
|
if (!rs || !rs->is_array()) bad("requests must be an array of {custom_id, params}");
|
|
for (const Json& r : rs->as_array()) {
|
|
const Json* p = r.find("params");
|
|
if (!p || !p->is_object()) bad("every request needs params");
|
|
reqs.push_back({text(r, "custom_id", true), request_from(*p)});
|
|
}
|
|
if (!confirmed(body)) {
|
|
Json o = Json::object();
|
|
o.set("requests", Json(static_cast<int>(reqs.size())));
|
|
for (const core::BatchRequest& r : reqs) r.params.validate(core::kDefaultVersion);
|
|
return json_out(dry_run("create a batch", o));
|
|
}
|
|
return json_out(api->batches->create(reqs, text(body, "user_profile_id")).raw);
|
|
}
|
|
if (action == "cancel" || action == "delete") {
|
|
if (!confirmed(body)) {
|
|
Json o = Json::object();
|
|
o.set("id", Json(id));
|
|
return json_out(dry_run(action + " the batch", o));
|
|
}
|
|
return json_out(action == "cancel" ? api->batches->cancel(id).raw : api->batches->destroy(id));
|
|
}
|
|
core::BatchResults res = api->batches->results(id);
|
|
Json arr = Json::array();
|
|
for (const core::BatchResultItem& it : res.items) arr.push(it.raw);
|
|
Json o = Json::object();
|
|
o.set("results", arr);
|
|
o.set("truncated", Json(res.truncated));
|
|
o.set("skipped_lines", Json(static_cast<int>(res.skipped_lines)));
|
|
return json_out(o);
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/files", "ai-anthropic-files", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string action = text(body, "action", true);
|
|
std::string id = text(body, "id");
|
|
if (action != "list" && action != "get" && action != "delete" && action != "upload")
|
|
bad("action must be list, get, delete or upload");
|
|
if (action == "get" || action == "delete") {
|
|
if (id.empty()) bad("id is required");
|
|
need_id(id);
|
|
}
|
|
return guarded([&] {
|
|
if (action == "list") return json_out(page_json(api->files->list(number(body, "limit", 0), text(body, "page"))));
|
|
if (action == "get") return json_out(api->files->metadata(id).raw);
|
|
if (action == "delete") {
|
|
if (!confirmed(body)) {
|
|
Json o = Json::object();
|
|
o.set("id", Json(id));
|
|
return json_out(dry_run("delete the file", o));
|
|
}
|
|
return json_out(api->files->destroy(id));
|
|
}
|
|
std::string name = text(body, "filename", true), data = text(body, "text", true);
|
|
long expires = number(body, "expires_in_seconds", 0);
|
|
if (!confirmed(body)) {
|
|
Json o = Json::object();
|
|
o.set("filename", Json(name));
|
|
o.set("bytes", Json(static_cast<int>(data.size())));
|
|
return json_out(dry_run("upload a text file", o));
|
|
}
|
|
return json_out(api->files->upload(name, data, "text/plain", expires).raw);
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/skills", "ai-anthropic-skills", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string action = text(body, "action", true);
|
|
std::string id = text(body, "id");
|
|
if (action != "list" && action != "get" && action != "versions" && action != "delete")
|
|
bad("action must be list, get, versions or delete");
|
|
if (action != "list") {
|
|
if (id.empty()) bad("id is required");
|
|
need_id(id);
|
|
}
|
|
return guarded([&] {
|
|
if (action == "list") return json_out(page_json(api->skills->list(number(body, "limit", 0), text(body, "page"), text(body, "source"))));
|
|
if (action == "get") return json_out(api->skills->retrieve(id));
|
|
if (action == "versions") return json_out(page_json(api->skills->list_versions(id, number(body, "limit", 0))));
|
|
if (!confirmed(body)) {
|
|
Json o = Json::object();
|
|
o.set("id", Json(id));
|
|
return json_out(dry_run("delete the skill and all its versions", o));
|
|
}
|
|
return json_out(api->skills->destroy(id));
|
|
});
|
|
});
|
|
|
|
router.post("/ai/anthropic/profiles", "ai-anthropic-profiles", [api](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string action = text(body, "action", true);
|
|
std::string id = text(body, "id");
|
|
if (action != "list" && action != "get" && action != "delete") bad("action must be list, get or delete");
|
|
if (action != "list") {
|
|
if (id.empty()) bad("id is required");
|
|
need_id(id);
|
|
}
|
|
return guarded([&] {
|
|
if (action == "list") return json_out(page_json(api->user_profiles->list(number(body, "limit", 0), text(body, "page"))));
|
|
if (action == "get") return json_out(api->user_profiles->retrieve(id));
|
|
if (!confirmed(body)) {
|
|
Json o = Json::object();
|
|
o.set("id", Json(id));
|
|
return json_out(dry_run("delete the user profile", o));
|
|
}
|
|
return json_out(api->user_profiles->destroy(id));
|
|
});
|
|
});
|
|
}
|
|
|
|
} // namespace molo::example::app
|