Files
moloapp/example/app/ai/MetaDemo.cpp
T

475 lines
18 KiB
C++
Raw Normal View History

#include "../AiDemos.hpp"
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "meta/calls.h"
#include "meta/check.h"
#include "meta/response_input.h"
#include "meta/catalog.h"
#include "meta/chat_completions.h"
#include "meta/chat_model.h"
#include "meta/client.h"
#include "meta/files.h"
#include "meta/images.h"
#include "meta/messages.h"
#include "meta/models.h"
#include "meta/muse_code.h"
#include "meta/responses.h"
#include "meta/sam.h"
#include "meta/speech.h"
#include "security/crypto/Base64.h"
namespace molo::example::app {
// Meta Model API (api.meta.ai/v1: Muse Spark, Muse Wonder, Muse Image, Muse
// Voice Transcribe, SAM 3.1) and the Muse Code session protocol, over the
// real src/meta module. GET routes need no credential and make no upstream
// call. Every route that calls Meta is a POST: 422 on bad input first, then
// 503 not_configured without META_API_KEY / MODEL_API_KEY. Paid and
// destructive calls (images, segmentation, transcription, deletes, spawning
// muse) are dry runs unless the body says "confirm":true.
namespace {
namespace mt = meta;
using Json = clients::body::Json;
constexpr std::size_t kMaxBody = 8 * 1024 * 1024;
const char kPage[] = R"META({% extends "base.html" %}
{% block content %}
<h1>Meta Model API</h1>
<p class="hint">Muse Spark and Muse Wonder (Responses, Chat Completions, Messages), Muse Image,
Muse Voice Transcribe, SAM 3.1, files, models, and the Muse Code session protocol.
Paid and destructive calls stay dry runs until you tick confirm.</p>
<p id="meta-config" class="hint">Checking credentials...</p>
<section class="meta">
<label>Route
<select id="meta-route">
<option value="responses">Responses: create</option>
<option value="responses/count">Responses: count input tokens</option>
<option value="responses/get">Responses: retrieve</option>
<option value="responses/cancel">Responses: cancel</option>
<option value="responses/delete">Responses: delete (confirm)</option>
<option value="chat">Chat completions</option>
<option value="messages">Messages (Anthropic format)</option>
<option value="models">Models</option>
<option value="files">Files (list, get, delete)</option>
<option value="images">Muse Image (confirm)</option>
<option value="segment">SAM 3.1 (confirm)</option>
<option value="transcribe">Transcribe WAV (confirm)</option>
<option value="platform">Service status (upstream)</option>
<option value="code">Muse Code protocol (confirm)</option>
</select></label>
<label>Body (JSON object)
<textarea id="meta-body" rows="7">{"model":"muse-spark-1.3","input":"Say hi in five words."}</textarea></label>
<label class="meta-check"><input id="meta-confirm" type="checkbox"> confirm (run paid or destructive calls)</label>
<div class="meta-actions"><button id="meta-send" type="button" class="primary">Send</button></div>
<p id="meta-error" class="error" role="alert"></p>
<pre id="meta-out" class="meta-out"></pre>
<h2>Models</h2>
<div id="meta-models" class="hint"></div>
</section>
<style>
.meta label{display:block;margin:.5rem 0}
.meta select,.meta textarea{width:100%;box-sizing:border-box}
.meta-check input{width:auto}
.meta-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
.meta-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
</style>
<script src="/embedded/ai_meta.js"></script>
{% endblock %}
)META";
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);
}
[[noreturn]] void rethrow(const llm::ApiError& e) {
if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED")
throw fw::HttpError(503, "not_configured", e.message());
if (e.type() == "INVALID_ARGUMENT") throw fw::HttpError(422, "validation_error", e.message());
throw fw::HttpError(502, "meta_error", e.type() + ": " + e.message());
}
template <typename F>
auto guarded(F&& f) -> decltype(f()) {
try {
return f();
} catch (const llm::ApiError& e) {
rethrow(e);
}
}
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& b, const char* key, bool required = false) {
const Json* v = b.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();
}
std::optional<double> number(const Json& b, const char* key) {
const Json* v = b.find(key);
if (!v || v->is_null()) return std::nullopt;
if (!v->is_number()) bad(std::string(key) + " must be a number");
return v->as_number();
}
bool flag(const Json& b, const char* key) {
const Json* v = b.find(key);
if (!v || v->is_null()) return false;
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
return v->as_bool();
}
Json dry_run(const char* what, const std::string& target) {
Json o = Json::object();
o.set("dry_run", Json(true));
o.set(what, Json(target));
o.set("hint", Json("send confirm:true to run it"));
return o;
}
void need_key(const std::shared_ptr<llm::ApiClient>& c) {
if (!c->configured()) throw fw::HttpError(503, "not_configured", "set META_API_KEY or MODEL_API_KEY");
}
mt::ResponseRequest response_from(const Json& b) {
mt::ResponseRequest r;
r.model = text(b, "model", true);
const Json* in = b.find("input");
if (!in || !(in->is_string() || in->is_array())) bad("input must be a string or an array");
r.input = *in;
r.instructions = text(b, "instructions");
r.previous_response_id = text(b, "previous_response_id");
r.reasoning_effort = text(b, "reasoning_effort");
if (auto n = number(b, "max_output_tokens")) r.max_output_tokens = static_cast<int>(*n);
if (auto t = number(b, "temperature")) r.temperature = *t;
if (flag(b, "web_search")) r.tools.push_back(mt::ToolBuilder::web_search());
return r;
}
} // namespace
void register_ai_meta_demo(fw::Router& router, Services& services) {
auto client = mt::MetaClient::from_env();
fw::Environment* env = services.templates.get();
services.templates->add_template("ai_meta.html", kPage);
services.add_feature("ai-meta", "/ai/meta");
router.get("/ai/meta", "ai-meta-page", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Meta Model API");
return fw::Response(200, env->render("ai_meta.html", ctx), "text/html; charset=utf-8");
});
router.get("/ai/meta/status", "ai-meta-status", [client](fw::Request req) {
(void)req;
Json o = Json::object();
o.set("configured", Json(client->configured()));
o.set("base_url", Json(mt::MetaEndpoints::kBaseUrl));
o.set("key_env", Json(std::string(mt::MetaEndpoints::kKeyEnv) + " or " + mt::MetaEndpoints::kAltKeyEnv));
o.set("calls", Json(static_cast<int>(mt::call_count())));
Json models = Json::array();
for (std::size_t i = 0; i < mt::ModelCatalog::size(); ++i) {
const mt::ModelInfo& m = mt::ModelCatalog::at(i);
Json row = Json::object();
row.set("id", Json(m.id));
row.set("family", Json(m.family));
row.set("kind", Json(mt::ModelCatalog::kind_name(m.kind)));
row.set("hosted", Json(m.hosted));
row.set("note", Json(m.note));
models.push(row);
}
o.set("models", models);
return json_out(o);
});
router.get("/ai/meta/calls", "ai-meta-calls", [](fw::Request req) {
(void)req;
Json arr = Json::array();
for (std::size_t i = 0; i < mt::call_count(); ++i) {
const mt::CallSpec& s = mt::spec_at(i);
Json row = Json::object();
row.set("http", Json(s.http_method));
row.set("path", Json(s.path));
row.set("operation", Json(s.operation));
arr.push(row);
}
Json o = Json::object();
o.set("calls", arr);
return json_out(o);
});
router.post("/ai/meta/responses", "ai-meta-responses", [client](fw::Request req) {
Json body = parse_body(req);
mt::ResponseRequest r = response_from(body);
return guarded([&] {
r.validate();
need_key(client);
mt::ResponseResult res = mt::ResponsesService(client).create(r);
Json o = Json::object();
o.set("id", Json(res.id()));
o.set("status", Json(res.status()));
o.set("output_text", Json(res.output_text()));
o.set("usage", res.body.find("usage") ? *res.body.find("usage") : Json::object());
return json_out(o);
});
});
router.post("/ai/meta/responses/count", "ai-meta-responses-count", [client](fw::Request req) {
mt::ResponseRequest r = response_from(parse_body(req));
return guarded([&] {
r.validate();
need_key(client);
Json o = Json::object();
o.set("input_tokens", Json(static_cast<int>(mt::ResponsesService(client).count_input_tokens(r))));
return json_out(o);
});
});
auto id_route = [&](const char* path, const char* name, int mode) {
router.post(path, name, [client, mode](fw::Request req) {
Json body = parse_body(req);
std::string id = text(body, "id", true);
bool confirm = flag(body, "confirm");
return guarded([&] {
mt::Check::segment("id", id);
if (mode == 2 && !confirm) return json_out(dry_run("delete", id));
need_key(client);
mt::ResponsesService svc(client);
Json out = mode == 0 ? svc.retrieve(id).body : mode == 1 ? svc.cancel(id) : svc.remove(id);
return json_out(out);
});
});
};
id_route("/ai/meta/responses/get", "ai-meta-responses-get", 0);
id_route("/ai/meta/responses/cancel", "ai-meta-responses-cancel", 1);
id_route("/ai/meta/responses/delete", "ai-meta-responses-delete", 2);
router.post("/ai/meta/chat", "ai-meta-chat", [client](fw::Request req) {
Json body = parse_body(req);
mt::ChatCompletionRequest r;
r.model = text(body, "model", true);
r.messages = {mt::ChatMessage::user(text(body, "message", true))};
r.reasoning_effort = text(body, "reasoning_effort");
if (auto n = number(body, "max_completion_tokens")) r.max_completion_tokens = static_cast<int>(*n);
return guarded([&] {
r.validate();
need_key(client);
Json out = mt::ChatCompletionsService(client).create(r);
llm::ChatResponse cr = mt::MetaChatModel::from_chat(out);
Json o = Json::object();
o.set("text", Json(cr.text()));
o.set("finish_reason", Json(cr.finish_reason));
o.set("total_tokens", Json(cr.usage.total_tokens));
return json_out(o);
});
});
router.post("/ai/meta/messages", "ai-meta-messages", [client](fw::Request req) {
Json body = parse_body(req);
mt::MessageRequest r;
r.model = text(body, "model", true);
r.messages = {mt::AnthropicBlock::message_text("user", text(body, "message", true))};
r.max_tokens = static_cast<int>(number(body, "max_tokens").value_or(0));
return guarded([&] {
r.validate();
need_key(client);
Json out = mt::MessagesService(client).create(r);
Json o = Json::object();
o.set("text", Json(mt::MessagesService::text_of(out)));
o.set("stop_reason", Json(mt::JsonX::str(out, "stop_reason")));
return json_out(o);
});
});
router.post("/ai/meta/models", "ai-meta-models", [client](fw::Request req) {
Json body = parse_body(req);
std::string id = text(body, "id");
return guarded([&] {
if (!id.empty()) mt::Check::model_id(id);
need_key(client);
mt::ModelsService svc(client);
Json arr = Json::array();
if (id.empty()) {
for (const auto& m : svc.list()) {
Json row = Json::object();
row.set("id", Json(m.id));
row.set("created", Json(static_cast<int>(m.created)));
arr.push(row);
}
} else {
auto m = svc.retrieve(id);
Json row = Json::object();
row.set("id", Json(m.id));
row.set("created", Json(static_cast<int>(m.created)));
arr.push(row);
}
Json o = Json::object();
o.set("models", arr);
return json_out(o);
});
});
router.post("/ai/meta/files", "ai-meta-files", [client](fw::Request req) {
Json body = parse_body(req);
std::string action = text(body, "action", true);
if (action != "list" && action != "get" && action != "delete") bad("action must be list, get or delete");
std::string id = text(body, "id");
if (action != "list" && id.empty()) bad("id is required");
bool confirm = flag(body, "confirm");
return guarded([&] {
if (!id.empty()) mt::Check::segment("id", id);
if (action == "delete" && !confirm) return json_out(dry_run("delete", id));
need_key(client);
mt::FilesService svc(client);
Json o = Json::object();
if (action == "list") {
mt::FileList l = svc.list_all(text(body, "purpose"), 100, text(body, "order"));
Json arr = Json::array();
for (const auto& f : l.files) {
Json row = Json::object();
row.set("id", Json(f.id));
row.set("filename", Json(f.filename));
row.set("bytes", Json(static_cast<int>(f.bytes)));
arr.push(row);
}
o.set("files", arr);
o.set("truncated", Json(l.truncated));
} else if (action == "get") {
mt::FileObject f = svc.retrieve(id);
o.set("id", Json(f.id));
o.set("filename", Json(f.filename));
o.set("status", Json(f.status));
} else {
o = svc.remove(id);
}
return json_out(o);
});
});
router.post("/ai/meta/images", "ai-meta-images", [client](fw::Request req) {
Json body = parse_body(req);
mt::ImageRequest r;
r.prompt = text(body, "prompt", true);
r.size = text(body, "size");
if (auto n = number(body, "n")) r.n = static_cast<int>(*n);
r.reasoning_strength = text(body, "reasoning_strength");
bool confirm = flag(body, "confirm");
return guarded([&] {
r.validate(false);
if (!confirm) return json_out(dry_run("prompt", r.prompt));
need_key(client);
mt::ImageResult res = mt::ImagesService(client).generate(r);
Json o = Json::object();
o.set("images", Json(static_cast<int>(res.data.size())));
o.set("output_format", Json(res.output_format));
if (!res.data.empty()) o.set("first_b64_prefix", Json(res.data[0].b64_json.substr(0, 64)));
return json_out(o);
});
});
router.post("/ai/meta/segment", "ai-meta-segment", [client](fw::Request req) {
Json body = parse_body(req);
mt::SamRequest s;
std::string phrase = text(body, "prompt", true);
s.image_url = text(body, "image_url");
s.video_url = text(body, "video_url");
s.include_confidence = flag(body, "include_confidence");
s.mask_encoding = text(body, "mask_encoding");
bool confirm = flag(body, "confirm");
return guarded([&] {
s.prompt = mt::SamPrompt::concept_text(phrase);
mt::ResponseRequest rr = s.to_response_request();
rr.validate(true);
if (!confirm) return json_out(dry_run("prompt", s.prompt));
need_key(client);
mt::ResponseAccumulator acc = mt::ResponsesService(client).stream(rr);
auto frames = mt::SamOutput::parse(acc.text());
Json o = Json::object();
o.set("frames", Json(static_cast<int>(frames.size())));
o.set("records", Json(static_cast<int>(mt::SamOutput::record_count(frames))));
return json_out(o);
});
});
router.post("/ai/meta/transcribe", "ai-meta-transcribe", [client](fw::Request req) {
Json body = parse_body(req);
std::string b64 = text(body, "audio_b64", true);
std::string wav;
if (!security::crypto::Base64::decode(b64, wav)) bad("audio_b64 is not valid base64");
mt::SpeechOptions o;
o.mode = text(body, "mode");
bool confirm = flag(body, "confirm");
return guarded([&] {
o.validate();
mt::WavInfo info = mt::WavInfo::parse(wav);
if (!confirm) return json_out(dry_run("duration_ms", std::to_string(info.duration_ms)));
need_key(client);
mt::TranscribeResult r = mt::TranscribeService(client).transcribe(o, wav);
Json out = Json::object();
out.set("transcript", Json(r.transcript));
out.set("turns", Json(static_cast<int>(r.turns.size())));
return json_out(out);
});
});
// GET /status is unauthenticated upstream; still a POST here (it leaves the host).
router.post("/ai/meta/platform", "ai-meta-platform", [](fw::Request req) {
(void)req;
return guarded([&] {
mt::ServiceStatus st = mt::StatusService(mt::MetaClient::unauthenticated()).get();
Json o = Json::object();
o.set("is_alive", Json(st.is_alive));
o.set("service_status", Json(st.service_status));
return json_out(o);
});
});
router.post("/ai/meta/code", "ai-meta-code", [](fw::Request req) {
Json body = parse_body(req);
bool confirm = flag(body, "confirm");
bool available = mt::ProcessTransport::binary_available("muse");
if (!confirm) {
Json o = dry_run("command", "muse serve");
o.set("muse_on_path", Json(available));
return json_out(o);
}
if (!available) throw fw::HttpError(503, "not_configured", "the muse binary is not on PATH");
return guarded([&] {
mt::MspClient c(std::make_unique<mt::ProcessTransport>());
Json init = c.initialize("moloapp-demo", "0.1");
Json o = Json::object();
o.set("schema_fingerprint", Json(c.schema_fingerprint()));
o.set("server", init.find("serverInfo") ? *init.find("serverInfo") : Json::object());
c.close();
return json_out(o);
});
});
}
} // namespace molo::example::app