models: EndpointAdmin and PublisherAdmin wrap every remaining method of the owned resources (84 call table rows, 132 Discovery methods covered, v1beta1 twins, IAM, operations, responses, export, config, enableModel); planner rejects undeclared body keys; VertexModel::TurnBuilder replays model turns with every thoughtSignature; Lyria WAV mime from bytes; veo-3.1 default. runtime: v1beta1 feedbackEntries, A2A gateway (40 rpcs), a2aTasks, task store records, scoped operations, beta twin flag (103 rows, 214 methods covered); checker verifies twins, a2aEndpoint patterns and coverage. example: admin routes for both parts. SPECs: live results moved from Unverified to Verified live (Veo, Lyria, thought signatures, v1beta1 routes). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
649 lines
29 KiB
C++
649 lines
29 KiB
C++
#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/endpoint_admin.h"
|
|
#include "google/models/gemini.h"
|
|
#include "google/models/media.h"
|
|
#include "google/models/model_garden.h"
|
|
#include "google/models/open_models.h"
|
|
#include "google/models/publisher_admin.h"
|
|
#include "security/crypto/Base64.h"
|
|
|
|
namespace molo::example::app {
|
|
|
|
// 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>
|
|
<h2>Admin calls</h2>
|
|
<p class="hint">Endpoint lifecycle, endpoint inference surfaces, operations and
|
|
publisher extras. Gated actions are dry runs until the body says
|
|
<code>"confirm":true</code>.</p>
|
|
<label>Route
|
|
<select id="m-admin-route">
|
|
<option value="endpoints">Endpoints (create, patch, deploy, IAM, ...)</option>
|
|
<option value="endpoint/call">Endpoint inference (tokens, explain, invoke, chat, ...)</option>
|
|
<option value="endpoint/operations">Endpoint operations</option>
|
|
<option value="publisher">Publisher models (export, config, responses, v1beta1)</option>
|
|
</select></label>
|
|
<textarea id="m-admin-body" rows="5" style="width:100%;box-sizing:border-box"></textarea>
|
|
<button id="m-admin-run" type="button">Send</button>
|
|
</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 + "]}";
|
|
}
|
|
|
|
// --- admin routes ---------------------------------------------------------------
|
|
// Endpoint lifecycle and inference surfaces, endpoint operations, publisher
|
|
// extras. Like the routes above: no credential is 503 first, bad input is
|
|
// 422 before any network traffic. Gated actions (delete, deploy, undeploy,
|
|
// mutate, IAM set, export, config, enable, record removal) are dry runs
|
|
// unless the body says "confirm":true.
|
|
|
|
constexpr std::size_t kMaxAdminBody = 256 * 1024;
|
|
constexpr std::size_t kMaxEventsShown = 100;
|
|
|
|
[[noreturn]] void bad_input(const std::string& why) {
|
|
throw fw::HttpError(422, "validation_error", why);
|
|
}
|
|
|
|
Json_t parse_admin(const fw::Request& req) {
|
|
if (req.body.size() > kMaxAdminBody)
|
|
throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
if (req.body.empty()) return Json_t::object();
|
|
try {
|
|
Json_t j = Json_t::parse(req.body);
|
|
if (!j.is_object()) bad_input("body must be a JSON object");
|
|
return j;
|
|
} catch (const clients::body::JsonError&) {
|
|
bad_input("body is not valid JSON");
|
|
}
|
|
}
|
|
|
|
std::string str_of(const Json_t& b, const char* key, bool required = false) {
|
|
const Json_t* v = b.find(key);
|
|
if (!v || v->is_null()) {
|
|
if (required) bad_input(std::string(key) + " is required");
|
|
return "";
|
|
}
|
|
if (!v->is_string()) bad_input(std::string(key) + " must be a string");
|
|
return v->as_string();
|
|
}
|
|
|
|
Json_t obj_of(const Json_t& b, const char* key, bool required = false) {
|
|
const Json_t* v = b.find(key);
|
|
if (!v || v->is_null()) {
|
|
if (required) bad_input(std::string(key) + " is required");
|
|
return Json_t::object();
|
|
}
|
|
if (!v->is_object()) bad_input(std::string(key) + " must be a JSON object");
|
|
return *v;
|
|
}
|
|
|
|
Json_t arr_of(const Json_t& b, const char* key, bool required = false) {
|
|
const Json_t* v = b.find(key);
|
|
if (!v || v->is_null()) {
|
|
if (required) bad_input(std::string(key) + " is required");
|
|
return Json_t::array();
|
|
}
|
|
if (!v->is_array()) bad_input(std::string(key) + " must be a JSON array");
|
|
return *v;
|
|
}
|
|
|
|
bool flag_of(const Json_t& b, const char* key) {
|
|
const Json_t* v = b.find(key);
|
|
if (!v || v->is_null()) return false;
|
|
if (!v->is_bool()) bad_input(std::string(key) + " must be true or false");
|
|
return v->as_bool();
|
|
}
|
|
|
|
int int_of(const Json_t& b, const char* key, int lo, int hi, int fallback) {
|
|
const Json_t* v = b.find(key);
|
|
if (!v || v->is_null()) return fallback;
|
|
if (!v->is_number()) bad_input(std::string(key) + " must be a number");
|
|
long n = v->as_int();
|
|
if (n < lo || n > hi)
|
|
bad_input(std::string(key) + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
|
|
return static_cast<int>(n);
|
|
}
|
|
|
|
std::string pick_of(const Json_t& b, const char* key, std::initializer_list<const char*> allowed) {
|
|
std::string v = str_of(b, key, true);
|
|
std::string list;
|
|
for (const char* a : allowed) {
|
|
if (v == a) return v;
|
|
list += (list.empty() ? "" : ", ") + std::string(a);
|
|
}
|
|
bad_input(std::string(key) + " must be one of " + list);
|
|
}
|
|
|
|
fw::Response json_response(const Json_t& j, int status = 200) {
|
|
return no_store(fw::JsonResponse(j.dump(), status));
|
|
}
|
|
|
|
Json_t dry_run_json(const char* what, const std::string& target) {
|
|
Json_t o = Json_t::object();
|
|
o.set("dry_run", Json_t(true));
|
|
o.set(what, Json_t(target));
|
|
o.set("hint", Json_t("send confirm:true to run it"));
|
|
return o;
|
|
}
|
|
|
|
Json_t operation_out(const google::Operation& op) {
|
|
Json_t o = Json_t::object();
|
|
o.set("operation", Json_t(op.name));
|
|
o.set("done", Json_t(op.done));
|
|
if (op.failed()) o.set("error", Json_t(op.error_message));
|
|
if (op.done && op.response.is_object()) o.set("response", op.response);
|
|
return o;
|
|
}
|
|
|
|
class ModelsAdminRoutes {
|
|
public:
|
|
explicit ModelsAdminRoutes(std::shared_ptr<google::GoogleClient> client)
|
|
: client_(std::move(client)) {}
|
|
|
|
fw::Response endpoints(const fw::Request& req) const {
|
|
return guarded(client_, [&] {
|
|
Json_t b = parse_admin(req);
|
|
std::string action = pick_of(b, "action",
|
|
{"create", "patch", "update", "delete", "deploy", "undeploy",
|
|
"mutate", "list_beta", "get_iam", "set_iam", "test_iam"});
|
|
gm::EndpointAdmin a(client_, target_now(), flag_of(b, "beta"));
|
|
std::string id = action == "create" || action == "list_beta" ? "" : str_of(b, "endpoint", true);
|
|
bool confirm = flag_of(b, "confirm");
|
|
if (action == "create")
|
|
return json_response(operation_out(a.create(obj_of(b, "body", true), str_of(b, "endpointId"))), 202);
|
|
if (action == "patch")
|
|
return json_response(a.patch(id, obj_of(b, "body", true), str_of(b, "updateMask", true)));
|
|
if (action == "update")
|
|
return json_response(operation_out(a.update(id, obj_of(b, "body", true))), 202);
|
|
if (action == "list_beta")
|
|
return json_response(a.list_beta(str_of(b, "readMask"), str_of(b, "gdcZone")));
|
|
if (action == "get_iam")
|
|
return json_response(a.get_iam_policy(id, int_of(b, "policyVersion", 0, 3, 0)));
|
|
if (action == "test_iam") {
|
|
std::vector<std::string> perms;
|
|
for (const Json_t& p : arr_of(b, "permissions", true).as_array()) {
|
|
if (!p.is_string()) bad_input("permissions must be strings");
|
|
perms.push_back(p.as_string());
|
|
}
|
|
Json_t out = Json_t::array();
|
|
for (const auto& p : a.test_iam_permissions(id, perms)) out.push(Json_t(p));
|
|
Json_t o = Json_t::object();
|
|
o.set("permissions", out);
|
|
return json_response(o);
|
|
}
|
|
if (action == "set_iam") {
|
|
Json_t policy = obj_of(b, "policy", true);
|
|
if (!confirm) return json_response(dry_run_json("would_set_policy_on", id));
|
|
return json_response(a.set_iam_policy(id, policy, true));
|
|
}
|
|
if (action == "delete") {
|
|
if (!confirm) return json_response(dry_run_json("would_delete", id));
|
|
return json_response(operation_out(a.destroy(id, true)), 202);
|
|
}
|
|
if (action == "deploy") {
|
|
Json_t dm = obj_of(b, "deployedModel", true), split = obj_of(b, "trafficSplit");
|
|
if (!confirm) return json_response(dry_run_json("would_deploy_to", id));
|
|
return json_response(operation_out(a.deploy_model(id, dm, split, true)), 202);
|
|
}
|
|
if (action == "undeploy") {
|
|
std::string dm = str_of(b, "deployedModelId", true);
|
|
Json_t split = obj_of(b, "trafficSplit");
|
|
if (!confirm) return json_response(dry_run_json("would_undeploy", id + "/" + dm));
|
|
return json_response(operation_out(a.undeploy_model(id, dm, split, true)), 202);
|
|
}
|
|
Json_t dm = obj_of(b, "deployedModel", true);
|
|
std::string mask = str_of(b, "updateMask", true);
|
|
if (!confirm) return json_response(dry_run_json("would_mutate", id));
|
|
return json_response(operation_out(a.mutate_deployed_model(id, dm, mask, true)), 202);
|
|
});
|
|
}
|
|
|
|
fw::Response endpoint_call(const fw::Request& req) const {
|
|
return guarded(client_, [&] {
|
|
Json_t b = parse_admin(req);
|
|
std::string kind = pick_of(b, "kind",
|
|
{"compute_tokens", "direct_predict", "direct_raw_predict", "explain",
|
|
"invoke", "deployed_invoke", "chat", "openapi", "science",
|
|
"response_get", "response_delete"});
|
|
gm::EndpointAdmin a(client_, target_now(), flag_of(b, "beta"));
|
|
std::string id = (kind == "openapi" || kind == "science") ? "" : str_of(b, "endpoint", true);
|
|
if (kind == "compute_tokens")
|
|
return json_response(a.compute_tokens(id, arr_of(b, "contents", true), str_of(b, "model")));
|
|
if (kind == "direct_predict")
|
|
return json_response(a.direct_predict(id, arr_of(b, "inputs", true), obj_of(b, "parameters")));
|
|
if (kind == "direct_raw_predict")
|
|
return json_response(a.direct_raw_predict(id, str_of(b, "methodName", true), str_of(b, "input", true)));
|
|
if (kind == "explain")
|
|
return json_response(a.explain(id, arr_of(b, "instances", true), obj_of(b, "parameters"),
|
|
str_of(b, "deployedModelId")));
|
|
if (kind == "invoke")
|
|
return json_response(a.invoke(id, str_of(b, "invokeId", true), obj_of(b, "body", true)));
|
|
if (kind == "deployed_invoke")
|
|
return json_response(a.invoke_deployed_model(id, str_of(b, "deployedModelId", true),
|
|
str_of(b, "invokeId", true), obj_of(b, "body", true)));
|
|
if (kind == "chat") return json_response(a.chat_completions(id, obj_of(b, "body", true)));
|
|
if (kind == "openapi")
|
|
return json_response(a.openapi(pick_of(b, "openapiKind", {"completions", "embeddings", "responses"}),
|
|
obj_of(b, "body", true)));
|
|
if (kind == "science") return json_response(a.science_inference(obj_of(b, "body", true)));
|
|
std::string response = str_of(b, "responseId", true);
|
|
if (kind == "response_get") return json_response(a.get_response(id, response));
|
|
if (!flag_of(b, "confirm")) return json_response(dry_run_json("would_delete", id + "/" + response));
|
|
return json_response(a.delete_response(id, response, true));
|
|
});
|
|
}
|
|
|
|
fw::Response endpoint_operations(const fw::Request& req) const {
|
|
return guarded(client_, [&] {
|
|
Json_t b = parse_admin(req);
|
|
std::string action = pick_of(b, "action", {"get", "list", "cancel", "wait", "delete"});
|
|
gm::EndpointAdmin a(client_, target_now(), flag_of(b, "beta"));
|
|
std::string id = str_of(b, "endpoint", true);
|
|
std::string op = action == "list" ? "" : str_of(b, "operation", true);
|
|
if (action == "get") return json_response(operation_out(a.operation(id, op)));
|
|
if (action == "list") {
|
|
Json_t arr = Json_t::array();
|
|
for (const auto& o : a.list_operations(id, 1, str_of(b, "filter"))) arr.push(operation_out(o));
|
|
Json_t out = Json_t::object();
|
|
out.set("operations", arr);
|
|
return json_response(out);
|
|
}
|
|
if (action == "wait")
|
|
return json_response(operation_out(a.wait_operation(id, op, int_of(b, "timeoutSeconds", 1, 600, 30))));
|
|
if (action == "cancel") {
|
|
a.cancel_operation(id, op);
|
|
return json_response(dry_run_json("cancel_requested", id + "/" + op));
|
|
}
|
|
if (!flag_of(b, "confirm")) return json_response(dry_run_json("would_delete", id + "/" + op));
|
|
a.delete_operation(id, op, true);
|
|
return json_response(dry_run_json("deleted", id + "/" + op));
|
|
});
|
|
}
|
|
|
|
fw::Response publisher(const fw::Request& req) const {
|
|
return guarded(client_, [&] {
|
|
Json_t b = parse_admin(req);
|
|
std::string action = pick_of(b, "action",
|
|
{"stream_predict", "responses_create", "responses_compact",
|
|
"responses_get", "responses_delete", "export", "set_config",
|
|
"enable", "iam", "beta_get", "beta_generate", "beta_count",
|
|
"beta_post", "beta_invoke"});
|
|
gm::PublisherAdmin p(client_, target_now());
|
|
std::string publisher = str_of(b, "publisher").empty() ? "google" : str_of(b, "publisher");
|
|
std::string model = (action.rfind("responses_", 0) == 0) ? "" : str_of(b, "model", true);
|
|
bool beta = flag_of(b, "beta");
|
|
bool confirm = flag_of(b, "confirm");
|
|
if (action == "stream_predict") {
|
|
Json_t events = Json_t::array();
|
|
p.server_streaming_predict(publisher, model, arr_of(b, "inputs", true), obj_of(b, "parameters"),
|
|
[&](const Json_t& e) {
|
|
events.push(e);
|
|
return events.as_array().size() < kMaxEventsShown;
|
|
});
|
|
Json_t o = Json_t::object();
|
|
o.set("events", events);
|
|
return json_response(o);
|
|
}
|
|
if (action == "responses_create")
|
|
return json_response(p.responses_create(publisher, obj_of(b, "body", true), beta));
|
|
if (action == "responses_compact")
|
|
return json_response(p.responses_compact(publisher, obj_of(b, "body", true), beta));
|
|
if (action == "responses_get")
|
|
return json_response(p.responses_get(publisher, str_of(b, "responseId", true), beta));
|
|
if (action == "responses_delete") {
|
|
std::string r = str_of(b, "responseId", true);
|
|
if (!confirm) return json_response(dry_run_json("would_delete", publisher + "/" + r));
|
|
return json_response(p.responses_delete(publisher, r, true, beta));
|
|
}
|
|
if (action == "export") {
|
|
std::string dest = str_of(b, "destination", true);
|
|
if (!confirm) return json_response(dry_run_json("would_export_to", dest));
|
|
return json_response(operation_out(p.export_model(publisher, model, dest, true)), 202);
|
|
}
|
|
if (action == "set_config") {
|
|
Json_t cfg = obj_of(b, "publisherModelConfig", true);
|
|
std::string mask = str_of(b, "updateMask", true);
|
|
if (!confirm) return json_response(dry_run_json("would_configure", publisher + "/" + model));
|
|
return json_response(operation_out(p.set_config(publisher, model, cfg, mask, true)), 202);
|
|
}
|
|
if (action == "enable") {
|
|
if (!confirm) return json_response(dry_run_json("would_enable", publisher + "/" + model));
|
|
return json_response(p.enable_model(publisher, model, str_of(b, "service"), true));
|
|
}
|
|
if (action == "iam")
|
|
return json_response(p.get_iam_policy(publisher, model, int_of(b, "policyVersion", 0, 3, 0)));
|
|
if (action == "beta_get") return json_response(p.beta_get(publisher, model, flag_of(b, "fullView")));
|
|
if (action == "beta_generate" || action == "beta_count") {
|
|
google::GenerateRequest r;
|
|
r.contents.push_back(google::Content::user(str_of(b, "prompt", true)));
|
|
r.config.max_output_tokens = int_of(b, "max_tokens", 1, gm::ModelsCaps::kMaxOutputTokens, 256);
|
|
Json_t o = Json_t::object();
|
|
if (action == "beta_count") {
|
|
o.set("total_tokens", Json_t(p.beta_count_tokens(publisher, model, r).total_tokens));
|
|
} else {
|
|
google::GenerateResponse out = p.beta_generate(publisher, model, r);
|
|
o.set("text", Json_t(out.text()));
|
|
o.set("total_tokens", Json_t(out.usage.total_tokens));
|
|
}
|
|
return json_response(o);
|
|
}
|
|
if (action == "beta_invoke")
|
|
return json_response(p.beta_invoke(publisher, model, str_of(b, "invokeId", true), obj_of(b, "body", true)));
|
|
return json_response(p.beta_post(str_of(b, "callId", true), publisher, model, obj_of(b, "body", true)));
|
|
});
|
|
}
|
|
|
|
private:
|
|
std::shared_ptr<google::GoogleClient> client_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
void register_agent_models_demo(fw::Router& router, Services& 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.1-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.1-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));
|
|
});
|
|
});
|
|
|
|
auto admin = std::make_shared<ModelsAdminRoutes>(client);
|
|
using Handler = fw::Response (ModelsAdminRoutes::*)(const fw::Request&) const;
|
|
auto add = [&](const char* path, const char* name, Handler h) {
|
|
router.post(path, name, [admin, h](fw::Request req) { return ((*admin).*h)(req); });
|
|
};
|
|
add("/agent/models/endpoints", "agent-models-endpoints", &ModelsAdminRoutes::endpoints);
|
|
add("/agent/models/endpoint/call", "agent-models-endpoint-call", &ModelsAdminRoutes::endpoint_call);
|
|
add("/agent/models/endpoint/operations", "agent-models-endpoint-operations",
|
|
&ModelsAdminRoutes::endpoint_operations);
|
|
add("/agent/models/publisher", "agent-models-publisher", &ModelsAdminRoutes::publisher);
|
|
}
|
|
|
|
} // namespace molo::example::app
|