feat(clients): models: Gemini on Vertex, Model Garden, Claude, endpoint models, media, VertexModel

One call table verified against Discovery (29 calls), live-verified read-only
with an express key and a service account token, /agent/models example routes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 04:40:00 +02:00
co-authored by Claude Sonnet 5.5
parent 6d744a433f
commit 6b731643bf
30 changed files with 3200 additions and 3 deletions
+305 -3
View File
@@ -1,13 +1,315 @@
#include "../AgentDemos.hpp"
#include <cstdlib>
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "../../fw/Validate.hpp"
#include "google/generative.h"
#include "google/models/calls.h"
#include "google/models/caps.h"
#include "google/models/claude_models.h"
#include "google/models/gemini.h"
#include "google/models/media.h"
#include "google/models/model_garden.h"
#include "google/models/open_models.h"
#include "security/crypto/Base64.h"
namespace molo::example::app {
// Agent Platform: models. Stub: the module agent fills this in.
// Agent Platform: models (src/google/models). Gemini on Vertex, Model Garden,
// Claude partner models, endpoint models (Gemma), media generation.
// Routes live under /agent/models. GET routes need no credential; every route
// that calls Google is a POST and answers 503 google_not_configured without
// one, 422 on bad input (validated before any network traffic).
namespace {
namespace gm = google::models;
using Fields = std::map<std::string, std::string>;
using Json_t = clients::body::Json;
const char kPage[] = R"MODELS({% extends "base.html" %}
{% block content %}
<h1>Models</h1>
<p class="hint">Gemini on Vertex AI, Model Garden, Claude, Gemma endpoints and
media generation. Express keys cover Gemini generate, tokens and embeddings;
the rest needs an OAuth token and a project.</p>
<p id="m-config" class="hint">Checking Google credentials...</p>
<section class="models">
<label>Action
<select id="m-action">
<option value="generate">Generate (Gemini)</option>
<option value="count">Count tokens</option>
<option value="embed">Embed text</option>
<option value="claude">Claude (partner)</option>
<option value="list">Model Garden list</option>
<option value="image">Image</option>
<option value="music">Music</option>
<option value="video">Video (start)</option>
</select></label>
<label>Model <input id="m-model" type="text" maxlength="128" placeholder="default"></label>
<textarea id="m-prompt" rows="4" style="width:100%;box-sizing:border-box" maxlength="4000" placeholder="Prompt"></textarea>
<button id="m-run" type="button" class="primary">Run</button>
<p id="m-error" class="error" role="alert"></p>
<pre id="m-out" style="white-space:pre-wrap;word-break:break-word"></pre>
</section>
<script src="/embedded/agent_models.js"></script>
{% endblock %}
)MODELS";
std::string quote(const std::string& s) { return "\"" + fw::json_escape(s) + "\""; }
[[noreturn]] void rethrow(const google::GoogleError& e) {
if (e.http_status() == 503 && e.status() == "NOT_CONFIGURED")
throw fw::HttpError(503, "google_not_configured", e.message());
if (e.service().empty() && e.status() == "INVALID_ARGUMENT")
throw fw::HttpError(422, "validation_error", e.message());
throw fw::HttpError(502, "google_error", e.status() + ": " + e.message());
}
// No credential is always 503, whatever the input: checked before parsing
// the call so a missing project cannot masquerade as a 422.
template <typename F>
auto guarded(const std::shared_ptr<google::GoogleClient>& client, F&& f) -> decltype(f()) {
if (!client->configured())
throw fw::HttpError(503, "google_not_configured",
"no Google credential configured (set GOOGLE_API_KEY)");
try {
return f();
} catch (const google::GoogleError& e) {
rethrow(e);
}
}
fw::Response no_store(fw::Response res) {
res.headers.set("Cache-Control", "no-store");
return res;
}
std::string field(Fields& in, const char* key) {
auto it = in.find(key);
return it == in.end() ? "" : it->second;
}
int int_field(Fields& in, const char* key, int fallback) {
std::string v = field(in, key);
if (v.empty()) return fallback;
char* end = nullptr;
long n = std::strtol(v.c_str(), &end, 10);
if (*end != '\0' || n < -1000000 || n > 1000000)
throw fw::HttpError(422, "validation_error", std::string(key) + " must be an integer");
return static_cast<int>(n);
}
std::string model_or(Fields& in, const char* fallback) {
std::string m = field(in, "model");
return m.empty() ? fallback : m;
}
// Chosen per request so the environment (project, token) is honored.
gm::VertexTarget target_now() { return gm::VertexTarget::from_env(); }
std::string media_json(const std::vector<gm::MediaItem>& items) {
std::string out = "{\"items\":[";
for (std::size_t i = 0; i < items.size(); ++i) {
out += std::string(i ? "," : "") + "{\"mime\":" + quote(items[i].mime) +
",\"uri\":" + quote(items[i].uri) + ",\"bytes\":" +
std::to_string(items[i].bytes.size()) + ",\"data_b64\":\"" +
(items[i].bytes.size() <= 2u * 1024u * 1024u
? security::crypto::Base64::encode(items[i].bytes)
: std::string()) +
"\"}";
}
return out + "]}";
}
} // namespace
void register_agent_models_demo(fw::Router& router, Services& services) {
(void)router;
(void)services;
if (!services.google) services.google = google::GoogleClient::from_env();
auto client = services.google;
services.templates->add_template("agent_models.html", kPage);
fw::Environment* env = services.templates.get();
services.add_feature("agent-models", "/agent/models");
router.get("/agent/models", "agent-models-page", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Models");
return fw::Response(200, env->render("agent_models.html", ctx),
"text/html; charset=utf-8");
});
router.get("/agent/models/status", "agent-models-status", [client](fw::Request req) {
(void)req;
gm::VertexTarget t = target_now();
std::string out = std::string("{\"configured\":") + (client->configured() ? "true" : "false") +
",\"mode\":" + quote(t.is_express() ? "express" : "project") +
",\"default_model\":" + quote(google::Generative::default_model()) +
",\"calls\":" + std::to_string(gm::kCallCount) + ",\"caps\":{" +
"\"max_output_tokens\":" + std::to_string(gm::ModelsCaps::kMaxOutputTokens) +
",\"max_embed_batch\":" + std::to_string(gm::ModelsCaps::kMaxEmbedBatch) +
",\"max_prompt_chars\":" + std::to_string(gm::ModelsCaps::kMaxPromptChars) +
",\"max_images\":" + std::to_string(gm::ModelsCaps::kMaxImages) + "}}";
return no_store(fw::JsonResponse(out, 200));
});
// The declared call table (local, no network): what this module can call.
router.get("/agent/models/calls", "agent-models-calls", [](fw::Request req) {
(void)req;
std::string out = "{\"calls\":[";
for (std::size_t i = 0; i < gm::kCallCount; ++i) {
const gm::CallSpec& c = gm::kCalls[i];
out += std::string(i ? "," : "") + "{\"id\":" + quote(c.id) + ",\"http\":" + quote(c.http) +
",\"version\":" + quote(c.version) + ",\"express\":" + (c.express ? "true" : "false") +
",\"rpc\":" + quote(c.rpc) + "}";
}
return no_store(fw::JsonResponse(out + "]}", 200));
});
router.post("/agent/models/generate", "agent-models-generate", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::Gemini gemini(client, target_now());
google::GenerateRequest r;
r.contents.push_back(google::Content::user(field(in, "prompt")));
r.config.max_output_tokens = int_field(in, "max_tokens", 256);
r.config.thinking_budget = 0;
google::GenerateResponse out =
gemini.generate(model_or(in, google::Generative::default_model().c_str()), r);
return no_store(fw::JsonResponse(
"{\"text\":" + quote(out.text()) + ",\"total_tokens\":" +
std::to_string(out.usage.total_tokens) + "}", 200));
});
});
router.post("/agent/models/count", "agent-models-count", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::Gemini gemini(client, target_now());
google::GenerateRequest r;
r.contents.push_back(google::Content::user(field(in, "prompt")));
gm::CountTokensResult n =
gemini.count_tokens(model_or(in, google::Generative::default_model().c_str()), r);
return no_store(fw::JsonResponse(
"{\"total_tokens\":" + std::to_string(n.total_tokens) + ",\"billable_characters\":" +
std::to_string(n.billable_characters) + "}", 200));
});
});
router.post("/agent/models/embed", "agent-models-embed", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::Gemini gemini(client, target_now());
auto out = gemini.embed(model_or(in, "text-embedding-005"), {field(in, "text")});
return no_store(fw::JsonResponse(
"{\"dimensions\":" + std::to_string(out[0].values.size()) + ",\"token_count\":" +
std::to_string(out[0].token_count) + "}", 200));
});
});
router.post("/agent/models/claude", "agent-models-claude", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::ClaudeModels claude(client, target_now());
gm::ClaudeRequest r;
r.model = model_or(in, "claude-sonnet-4-6");
r.messages.push_back({"user", field(in, "prompt")});
r.max_tokens = int_field(in, "max_tokens", 256);
gm::ClaudeResponse out = claude.generate(r);
return no_store(fw::JsonResponse(
"{\"text\":" + quote(out.text) + ",\"stop_reason\":" + quote(out.stop_reason) +
",\"input_tokens\":" + std::to_string(out.input_tokens) + ",\"output_tokens\":" +
std::to_string(out.output_tokens) + "}", 200));
});
});
router.post("/agent/models/list", "agent-models-list", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::ModelGarden garden(client, target_now());
gm::ListModelsOptions o;
o.page_size = int_field(in, "page_size", 20);
o.max_pages = 1;
std::string publisher = field(in, "publisher");
auto models = garden.list(publisher.empty() ? "google" : publisher, o);
std::string out = "{\"models\":[";
for (std::size_t i = 0; i < models.size(); ++i)
out += std::string(i ? "," : "") + "{\"name\":" + quote(models[i].name) +
",\"launch_stage\":" + quote(models[i].launch_stage) + "}";
return no_store(fw::JsonResponse(out + "]}", 200));
});
});
router.post("/agent/models/endpoint", "agent-models-endpoint", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::OpenModels open(client, target_now());
Json_t instances = Json_t::array();
instances.push(gm::OpenModels::gemma_instance(field(in, "prompt"),
int_field(in, "max_tokens", 128)));
gm::PredictResult r = open.predict(field(in, "endpoint"), instances);
return no_store(fw::JsonResponse("{\"predictions\":" + r.predictions.dump() + "}", 200));
});
});
router.post("/agent/models/image", "agent-models-image", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::MediaGenerator media(client, target_now());
gm::ImageRequest r;
r.prompt = field(in, "prompt");
r.count = int_field(in, "count", 1);
r.aspect_ratio = field(in, "aspect_ratio");
return no_store(fw::JsonResponse(
media_json(media.generate_image(model_or(in, "imagen-4.0-generate-001"), r)), 200));
});
});
router.post("/agent/models/music", "agent-models-music", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::MediaGenerator media(client, target_now());
gm::MusicRequest r;
r.prompt = field(in, "prompt");
r.count = int_field(in, "count", 1);
return no_store(fw::JsonResponse(
media_json(media.generate_music(model_or(in, "lyria-002"), r)), 200));
});
});
router.post("/agent/models/video", "agent-models-video", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::MediaGenerator media(client, target_now());
gm::VideoRequest r;
r.prompt = field(in, "prompt");
r.count = int_field(in, "count", 1);
r.duration_seconds = int_field(in, "duration_seconds", 0);
r.aspect_ratio = field(in, "aspect_ratio");
std::string op = media.start_video(model_or(in, "veo-3.0-generate-001"), r);
return no_store(fw::JsonResponse("{\"operation\":" + quote(op) + "}", 202));
});
});
router.post("/agent/models/video/status", "agent-models-video-status", [client](fw::Request req) {
Fields in = fw::parse_json_object_flat(req.body);
return guarded(client, [&] {
gm::MediaGenerator media(client, target_now());
google::Operation op = media.video_status(model_or(in, "veo-3.0-generate-001"),
field(in, "operation"));
std::string out = std::string("{\"done\":") + (op.done ? "true" : "false") +
",\"failed\":" + (op.failed() ? "true" : "false");
if (op.done && !op.failed()) {
auto items = gm::MediaGenerator::items_from(
op.response.find("videos") ? *op.response.find("videos") : Json_t());
out += ",\"videos\":" + std::to_string(items.size());
}
return no_store(fw::JsonResponse(out + "}", 200));
});
});
}
} // namespace molo::example::app
+37
View File
@@ -0,0 +1,37 @@
// Models page: posts the chosen action to /agent/models/<action>.
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var routes = {
generate: "/agent/models/generate", count: "/agent/models/count",
embed: "/agent/models/embed", claude: "/agent/models/claude",
list: "/agent/models/list", image: "/agent/models/image",
music: "/agent/models/music", video: "/agent/models/video"
};
var MAX_OUT = 20000;
fetch("/agent/models/status").then(function (r) { return r.json(); }).then(function (s) {
$("m-config").textContent = s.configured
? "Credential found. Mode: " + s.mode + ". Default model: " + s.default_model + "."
: "No Google credential configured: calls answer 503 until GOOGLE_API_KEY is set.";
}).catch(function () { $("m-config").textContent = "Status unavailable."; });
$("m-run").addEventListener("click", function () {
var action = $("m-action").value;
var prompt = $("m-prompt").value;
var body = { prompt: prompt };
if (action === "embed") body = { text: prompt };
if ($("m-model").value) body.model = $("m-model").value;
$("m-error").textContent = "";
$("m-out").textContent = "...";
fetch(routes[action], {
method: "POST", headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (r) {
return r.text().then(function (t) { return { ok: r.ok, text: t }; });
}).then(function (res) {
if (!res.ok) { $("m-error").textContent = res.text.slice(0, 300); $("m-out").textContent = ""; return; }
$("m-out").textContent = res.text.slice(0, MAX_OUT);
}).catch(function (e) { $("m-error").textContent = String(e); });
});
})();