feat(clients): models+runtime: close gaps (all Discovery methods wrapped, live re-verification)

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>
This commit is contained in:
2026-10-24 05:47:34 +02:00
co-authored by Claude Sonnet 5.5
parent 29d775a923
commit 8e807945a2
38 changed files with 4437 additions and 72 deletions
+335 -2
View File
@@ -9,10 +9,12 @@
#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 {
@@ -53,6 +55,19 @@ the rest needs an OAuth token and a project.</p>
<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 %}
@@ -124,6 +139,313 @@ std::string media_json(const std::vector<gm::MediaItem>& items) {
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) {
@@ -289,7 +611,7 @@ void register_agent_models_demo(fw::Router& router, Services& services) {
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);
std::string op = media.start_video(model_or(in, "veo-3.1-generate-001"), r);
return no_store(fw::JsonResponse("{\"operation\":" + quote(op) + "}", 202));
});
});
@@ -298,7 +620,7 @@ void register_agent_models_demo(fw::Router& router, Services& services) {
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"),
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");
@@ -310,6 +632,17 @@ void register_agent_models_demo(fw::Router& router, Services& services) {
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
+452 -2
View File
@@ -5,9 +5,12 @@
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "google/resource.h"
#include "google/runtime/a2a.h"
#include "google/runtime/agent_runtime.h"
#include "google/runtime/calls.h"
#include "google/runtime/caps.h"
#include "google/runtime/feedback.h"
#include "google/runtime/operation_calls.h"
namespace molo::example::app {
@@ -44,6 +47,24 @@ Lists and queries only; create and delete stay behind explicit calls.</p>
<button id="rt-query" type="button">Query</button>
<button id="rt-stream" type="button">Stream query</button>
</div>
<h2>More calls</h2>
<p class="hint">Operations, revisions, update, IAM, code, feedback, A2A. Destructive
actions are dry runs until the body says <code>"confirm":true</code>.</p>
<label>Route
<select id="rt-route">
<option value="operations">Operations (get, list, cancel, wait, delete)</option>
<option value="revisions">Revisions (get, list, delete, query, stream)</option>
<option value="update">Update engine</option>
<option value="iam">IAM (get, test, set)</option>
<option value="code">Execute code</option>
<option value="async/cancel">Cancel async query</option>
<option value="feedback">Feedback entries</option>
<option value="a2a">A2A gateway</option>
<option value="a2a/tasks">A2A tasks</option>
<option value="a2a/records">A2A task records</option>
</select></label>
<label>Body (JSON object) <textarea id="rt-body" rows="5"></textarea></label>
<div class="rt-actions"><button id="rt-send" type="button">Send</button></div>
<p id="rt-error" class="error" role="alert"></p>
<pre id="rt-out" class="rt-out"></pre>
</section>
@@ -171,6 +192,414 @@ rt::EngineDraft draft_from(const Json& body) {
return d;
}
// --- extended routes ---------------------------------------------------------
// Everything below validates its input first (422), then needs a credential
// (503 google_not_configured), then calls the runtime part. Destructive
// actions are dry runs unless the body says "confirm":true.
bool flag(const Json& body, const char* key) {
const Json* v = body.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();
}
std::string pick(const Json& body, const char* key, std::initializer_list<const char*> allowed) {
std::string v = text(body, key, true);
std::string list;
for (const char* a : allowed) {
if (v == a) return v;
list += (list.empty() ? "" : ", ") + std::string(a);
}
bad(std::string(key) + " must be one of " + list);
}
std::string segment(const Json& body, const char* key) {
std::string v = text(body, key, true);
if (!google::valid_segment(v)) bad(std::string(key) + " must be 1-128 chars of A-Z a-z 0-9 . _ -");
return v;
}
Json object_field(const Json& body, const char* key, bool required) {
const Json* v = body.find(key);
if (!v || v->is_null()) {
if (required) bad(std::string(key) + " is required");
return Json::object();
}
if (!v->is_object()) bad(std::string(key) + " must be a JSON object");
return *v;
}
Json array_field(const Json& body, const char* key, bool required) {
const Json* v = body.find(key);
if (!v || v->is_null()) {
if (required) bad(std::string(key) + " is required");
return Json::array();
}
if (!v->is_array()) bad(std::string(key) + " must be a JSON array");
return *v;
}
int bounded(const Json& body, const char* key, int lo, int hi, int fallback) {
auto n = number(body, key);
if (!n) return fallback;
if (*n < lo || *n > hi)
bad(std::string(key) + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
return *n;
}
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;
}
Json operations_json(const std::vector<google::Operation>& ops) {
Json arr = Json::array();
for (const auto& op : ops) arr.push(operation_json(op));
Json o = Json::object();
o.set("operations", arr);
return o;
}
rt::FeedbackEntry feedback_from(const Json& body) {
rt::FeedbackEntry e;
e.event_id = text(body, "eventId");
e.source = text(body, "source");
e.session_id = text(body, "sessionId");
e.user_id = text(body, "userId");
e.feedback_type = text(body, "feedbackType");
e.feedback_text = text(body, "feedbackText");
for (const Json& l : array_field(body, "feedbackLabels", false).as_array()) {
if (!l.is_string()) bad("feedbackLabels must be strings");
e.feedback_labels.push_back(l.as_string());
}
Json meta = object_field(body, "customMetadata", false);
if (!meta.as_object().empty()) e.custom_metadata = meta;
try {
e.validate();
} catch (const google::GoogleError& ex) {
rethrow(ex);
}
return e;
}
rt::A2aTaskInfo task_from(const Json& body) {
rt::A2aTaskInfo t;
t.context_id = text(body, "contextId");
t.app_id = text(body, "appId");
t.user_id = text(body, "userId");
t.ttl = text(body, "ttl");
t.expire_time = text(body, "expireTime");
Json meta = object_field(body, "metadata", false);
if (!meta.as_object().empty()) t.metadata = meta;
try {
t.validate();
} catch (const google::GoogleError& ex) {
rethrow(ex);
}
return t;
}
Json task_list_json(const std::vector<rt::A2aTaskInfo>& tasks) {
Json arr = Json::array();
for (const auto& t : tasks) arr.push(t.raw);
Json o = Json::object();
o.set("items", arr);
return o;
}
// One object per route family so the register function stays a table of routes.
class RuntimeAdminRoutes {
public:
explicit RuntimeAdminRoutes(std::shared_ptr<google::GoogleClient> client)
: client_(std::move(client)) {}
fw::Response operations(const fw::Request& req) const {
Json b = parse_body(req);
std::string action = pick(b, "action", {"get", "list", "cancel", "wait", "delete"});
std::string name = text(b, action == "list" ? "parent" : "name", true);
rt::OperationScope scope = local([&] { return rt::OperationCalls::scope_from(name); });
if (action != "list") local([&] { return rt::OperationCalls::check(scope, name); });
if (action == "list" && !rt::OperationCalls::has_list(scope))
bad(std::string(rt::OperationCalls::scope_name(scope)) + " operations cannot be listed");
int timeout = bounded(b, "timeoutSeconds", 1, rt::ScopedOperationCaps::kMaxWaitSeconds, 30);
bool confirm = flag(b, "confirm");
if (action == "delete" && !confirm) return json_out(dry_run("would_delete", name));
return guarded([&] {
rt::OperationCalls ops = runtime().operations();
if (action == "get") return json_out(operation_json(ops.get(scope, name)));
if (action == "list") return json_out(operations_json(ops.list(scope, name, text(b, "filter"))));
if (action == "wait") return json_out(operation_json(ops.wait(scope, name, timeout)));
if (action == "cancel") {
ops.cancel(scope, name);
Json o = Json::object();
o.set("cancel_requested", Json(name));
return json_out(o);
}
ops.remove(scope, name, true);
Json o = Json::object();
o.set("deleted", Json(name));
return json_out(o);
});
}
fw::Response revisions(const fw::Request& req) const {
Json b = parse_body(req);
std::string action = pick(b, "action", {"get", "list", "delete", "query", "stream"});
std::string id = engine_id(b);
std::string revision = action == "list" ? "" : segment(b, "revision");
Json input = Json::object();
std::string method = text(b, "classMethod");
if (action == "query" || action == "stream") {
input = object_field(b, "input", false);
local([&] { return rt::AgentRuntime::query_body(input, method.empty() ? "query" : method); });
}
bool confirm = flag(b, "confirm");
if (action == "delete" && !confirm) return json_out(dry_run("would_delete", id + "/" + revision));
return guarded([&] {
rt::AgentRuntime r = runtime();
if (action == "list") {
Json arr = Json::array();
for (const auto& v : r.list_revisions(id)) arr.push(v.raw);
Json o = Json::object();
o.set("revisions", arr);
return json_out(o);
}
if (action == "get") return json_out(r.get_revision(id, revision).raw);
if (action == "delete") return json_out(operation_json(r.destroy_revision_start(id, revision)), 202);
if (action == "query")
return json_out(method.empty() ? r.query_revision(id, revision, input)
: r.query_revision(id, revision, input, method));
Json events = Json::array();
auto sink = [&](const Json& e) {
events.push(e);
return events.as_array().size() < kMaxStreamEventsShown;
};
if (method.empty()) r.stream_query_revision(id, revision, input, sink);
else r.stream_query_revision(id, revision, input, sink, method);
Json o = Json::object();
o.set("events", events);
return json_out(o);
});
}
fw::Response update(const fw::Request& req) const {
Json b = parse_body(req);
std::string id = engine_id(b);
rt::EngineUpdate u;
if (b.find("displayName")) u.display_name = text(b, "displayName", true);
if (b.find("description")) u.description = text(b, "description", true);
local([&] { u.validate(); return 0; });
return guarded([&] { return json_out(operation_json(runtime().update_start(id, u)), 202); });
}
fw::Response iam(const fw::Request& req) const {
Json b = parse_body(req);
std::string action = pick(b, "action", {"get", "test", "set"});
std::string id = engine_id(b);
int version = bounded(b, "policyVersion", 0, 3, 0);
std::vector<std::string> permissions;
if (action == "test") {
for (const Json& p : array_field(b, "permissions", true).as_array()) {
if (!p.is_string()) bad("permissions must be strings");
permissions.push_back(p.as_string());
}
}
Json policy = action == "set" ? object_field(b, "policy", true) : Json::object();
if (action == "set" && !flag(b, "confirm")) return json_out(dry_run("would_set_policy_on", id));
return guarded([&] {
rt::AgentRuntime r = runtime();
if (action == "get") return json_out(r.get_iam_policy(id, version));
if (action == "set") return json_out(r.set_iam_policy(id, policy));
Json arr = Json::array();
for (const auto& p : r.test_iam_permissions(id, permissions)) arr.push(Json(p));
Json o = Json::object();
o.set("permissions", arr);
return json_out(o);
});
}
fw::Response code(const fw::Request& req) const {
Json b = parse_body(req);
std::string id = engine_id(b);
Json inputs = array_field(b, "inputs", true);
if (inputs.as_array().empty()) bad("inputs must not be empty");
return guarded([&] { return json_out(runtime().execute_code(id, inputs)); });
}
fw::Response cancel_async(const fw::Request& req) const {
Json b = parse_body(req);
std::string id = engine_id(b);
std::string name = text(b, "operation", true);
if (name.rfind("projects/", 0) != 0 || name.size() > 512 || name.find("..") != std::string::npos ||
name.find('?') != std::string::npos)
bad("operation must be an operation name starting with projects/");
return guarded([&] {
runtime().cancel_async_query(id, name);
Json o = Json::object();
o.set("cancel_requested", Json(name));
return json_out(o);
});
}
fw::Response feedback(const fw::Request& req) const {
Json b = parse_body(req);
std::string action = pick(b, "action", {"list", "get", "create", "patch", "delete", "context",
"context_update"});
std::string id = engine_id(b);
std::string entry = (action == "list" || action == "create") ? "" : segment(b, "entry");
rt::FeedbackEntry fields;
if (action == "create" || action == "patch") fields = feedback_from(b);
Json events = action == "context_update" ? array_field(b, "contextEvents", true) : Json::array();
if (action == "delete" && !flag(b, "confirm")) return json_out(dry_run("would_delete", id + "/" + entry));
return guarded([&] {
rt::RuntimeFeedback fb(client_, ctx());
need_credential();
if (action == "list") {
Json arr = Json::array();
for (const auto& e : fb.list(id, text(b, "filter"), text(b, "orderBy"))) {
Json j = e.to_json();
j.set("name", Json(e.name));
arr.push(j);
}
Json o = Json::object();
o.set("entries", arr);
return json_out(o);
}
if (action == "get") {
rt::FeedbackEntry e = fb.get(id, entry);
Json j = e.to_json();
j.set("name", Json(e.name));
return json_out(j);
}
if (action == "context") return json_out(fb.get_context(id, entry));
if (action == "create") return json_out(operation_json(fb.create(id, fields)), 202);
if (action == "patch") return json_out(operation_json(fb.patch(id, entry, fields)), 202);
if (action == "context_update")
return json_out(operation_json(fb.update_context(id, entry, events)), 202);
return json_out(operation_json(fb.destroy(id, entry, true)), 202);
});
}
fw::Response a2a(const fw::Request& req) const {
Json b = parse_body(req);
rt::A2aRequest r;
r.target = pick(b, "target", {"engine", "revision"}) == "engine" ? rt::A2aTarget::kEngine
: rt::A2aTarget::kRevision;
r.protocol = pick(b, "protocol", {"plain", "v1"}) == "v1" ? rt::A2aProtocol::kV1
: rt::A2aProtocol::kPlain;
static const std::pair<const char*, rt::A2aOp> kOps[] = {
{"card", rt::A2aOp::kCard}, {"extended_card", rt::A2aOp::kExtendedAgentCard},
{"tasks", rt::A2aOp::kTasks}, {"task", rt::A2aOp::kTaskGet},
{"push_configs", rt::A2aOp::kPushConfigs}, {"push_config", rt::A2aOp::kPushConfigGet},
{"cancel", rt::A2aOp::kTaskCancel}, {"subscribe", rt::A2aOp::kTaskSubscribe},
{"send", rt::A2aOp::kMessageSend}, {"stream", rt::A2aOp::kMessageStream}};
std::string op = text(b, "op", true);
bool known = false;
for (const auto& [name, value] : kOps)
if (op == name) {
r.op = value;
known = true;
}
if (!known) bad("op must be one of card, extended_card, tasks, task, push_configs, push_config, "
"cancel, subscribe, send, stream");
r.engine_id = engine_id(b);
if (r.target == rt::A2aTarget::kRevision) r.revision_id = segment(b, "revision");
r.task_id = text(b, "task");
r.config_id = text(b, "config");
r.history_length = bounded(b, "historyLength", 0, rt::A2aCaps::kMaxHistoryLength, 0);
if (!r.is_get()) r.body = object_field(b, "body", true);
local([&] { return rt::RuntimeA2a(client_, ctx()).path_of(r); });
return guarded([&] {
need_credential();
rt::RuntimeA2a gateway(client_, ctx());
if (r.is_stream()) {
Json events = Json::array();
gateway.stream(r, [&](const Json& e) {
events.push(e);
return events.as_array().size() < kMaxStreamEventsShown;
});
Json o = Json::object();
o.set("events", events);
return json_out(o);
}
if (r.op == rt::A2aOp::kTaskSubscribe) return json_out(gateway.subscribe(r));
return json_out(gateway.call(r));
});
}
// Tasks of an engine ("tasks") or records of a task store ("records").
fw::Response tasks(const fw::Request& req, bool records) const {
Json b = parse_body(req);
std::string action = pick(b, "action", {"create", "get", "list", "delete", "append", "events"});
std::string owner = records ? segment(b, "store") : engine_id(b);
std::string item = (action == "list") ? "" : (action == "create" && records ? text(b, "record")
: (action == "create" ? text(b, "task", true)
: segment(b, records ? "record" : "task")));
if (action == "create" && !item.empty()) {
if (records) {
if (!google::valid_segment(item)) bad("record must be 1-128 chars of A-Z a-z 0-9 . _ -");
} else {
local([&] { rt::RuntimeA2a::check_task_id(item); return 0; });
}
}
rt::A2aTaskInfo fields = action == "create" ? task_from(b) : rt::A2aTaskInfo{};
Json events = action == "append" ? array_field(b, "events", true) : Json::array();
if (action == "append" && (events.as_array().empty() || events.as_array().size() > 100))
bad("events must hold 1..100 items");
if (action == "delete" && !flag(b, "confirm")) return json_out(dry_run("would_delete", owner + "/" + item));
return guarded([&] {
need_credential();
rt::RuntimeA2a svc(client_, ctx());
std::string filter = text(b, "filter");
if (action == "create")
return json_out((records ? svc.create_record(owner, item, fields)
: svc.create_task(owner, item, fields)).raw, 201);
if (action == "get")
return json_out((records ? svc.get_record(owner, item, text(b, "view"))
: svc.get_task(owner, item, text(b, "view"))).raw);
if (action == "list")
return json_out(task_list_json(records ? svc.list_records(owner, filter, text(b, "orderBy"))
: svc.list_tasks(owner, filter, text(b, "orderBy"))));
if (action == "append")
return json_out(records ? svc.append_record_events(owner, item, events, text(b, "generation"))
: svc.append_task_events(owner, item, events, text(b, "generation")));
if (action == "events")
return json_out(records ? svc.list_record_events(owner, item, filter)
: svc.list_task_events(owner, item, filter));
if (records) return json_out(svc.delete_record(owner, item, true).raw);
svc.delete_task(owner, item, true);
Json o = Json::object();
o.set("deleted", Json(owner + "/" + item));
return json_out(o);
});
}
private:
google::CloudContext ctx() const { return google::CloudContext::from_env(); }
void need_credential() const {
if (!client_->configured()) throw google::NotConfiguredError("aiplatform");
}
rt::AgentRuntime runtime() const {
need_credential();
return rt::AgentRuntime(client_, ctx());
}
// Local validation: a library "invalid" becomes a 422.
template <typename F>
auto local(F&& f) const -> decltype(f()) {
try {
return f();
} catch (const google::GoogleError& e) {
rethrow(e);
}
}
std::shared_ptr<google::GoogleClient> client_;
};
} // namespace
void register_agent_runtime_demo(fw::Router& router, Services& services) {
@@ -201,6 +630,7 @@ void register_agent_runtime_demo(fw::Router& router, Services& services) {
o.set("project_set", Json(!ctx.project.empty()));
o.set("location", Json(ctx.location));
o.set("max_stream_events", Json(static_cast<int>(kMaxStreamEventsShown)));
o.set("calls", Json(static_cast<int>(rt::call_count())));
return json_out(o);
});
@@ -208,8 +638,8 @@ void register_agent_runtime_demo(fw::Router& router, Services& services) {
router.get("/agent/runtime/calls", "agent-runtime-calls", [](fw::Request req) {
(void)req;
Json arr = Json::array();
for (int c = static_cast<int>(rt::Call::kCreate); c <= static_cast<int>(rt::Call::kRevisionStreamQuery); ++c) {
const rt::CallSpec& s = rt::spec_of(static_cast<rt::Call>(c));
for (std::size_t i = 0; i < rt::call_count(); ++i) {
const rt::CallSpec& s = rt::spec_at(i);
Json row = Json::object();
row.set("http", Json(s.http_method));
row.set("path", Json(s.path));
@@ -330,6 +760,26 @@ void register_agent_runtime_demo(fw::Router& router, Services& services) {
}
return guarded([&] { return json_out(operation_json(runtime().destroy_start(id, force)), 202); });
});
// Extended routes (operations of every scope, revisions, update, IAM, code
// execution, feedback, A2A gateway, A2A tasks and task store records).
auto admin = std::make_shared<RuntimeAdminRoutes>(client);
using Handler = fw::Response (RuntimeAdminRoutes::*)(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/runtime/operations", "agent-runtime-operations", &RuntimeAdminRoutes::operations);
add("/agent/runtime/revisions", "agent-runtime-revisions", &RuntimeAdminRoutes::revisions);
add("/agent/runtime/update", "agent-runtime-update", &RuntimeAdminRoutes::update);
add("/agent/runtime/iam", "agent-runtime-iam", &RuntimeAdminRoutes::iam);
add("/agent/runtime/code", "agent-runtime-code", &RuntimeAdminRoutes::code);
add("/agent/runtime/async/cancel", "agent-runtime-async-cancel", &RuntimeAdminRoutes::cancel_async);
add("/agent/runtime/feedback", "agent-runtime-feedback", &RuntimeAdminRoutes::feedback);
add("/agent/runtime/a2a", "agent-runtime-a2a", &RuntimeAdminRoutes::a2a);
router.post("/agent/runtime/a2a/tasks", "agent-runtime-a2a-tasks",
[admin](fw::Request req) { return admin->tasks(req, false); });
router.post("/agent/runtime/a2a/records", "agent-runtime-a2a-records",
[admin](fw::Request req) { return admin->tasks(req, true); });
}
} // namespace molo::example::app
+30
View File
@@ -16,6 +16,36 @@
: "No Google credential configured: calls answer 503 until GOOGLE_API_KEY is set.";
}).catch(function () { $("m-config").textContent = "Status unavailable."; });
// Admin panel: one example body per route (the server validates everything).
var templates = {
"endpoints": { action: "list_beta" },
"endpoint/call": { kind: "compute_tokens", endpoint: "ENDPOINT_ID",
contents: [{ role: "user", parts: [{ text: "hello" }] }] },
"endpoint/operations": { action: "list", endpoint: "ENDPOINT_ID" },
"publisher": { action: "beta_get", publisher: "google", model: "gemini-2.5-flash" }
};
function fillAdmin() {
$("m-admin-body").value = JSON.stringify(templates[$("m-admin-route").value], null, 1);
}
$("m-admin-route").addEventListener("change", fillAdmin);
fillAdmin();
$("m-admin-run").addEventListener("click", function () {
var body;
try { body = JSON.parse($("m-admin-body").value || "{}"); }
catch (e) { $("m-error").textContent = "Body is not valid JSON"; return; }
$("m-error").textContent = "";
$("m-out").textContent = "...";
fetch("/agent/models/" + $("m-admin-route").value, {
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); });
});
$("m-run").addEventListener("click", function () {
var action = $("m-action").value;
var prompt = $("m-prompt").value;
+24
View File
@@ -40,5 +40,29 @@
$("rt-list").onclick = function () { post("/agent/runtime/list", {}); };
$("rt-get").onclick = function () { post("/agent/runtime/get", { id: $("rt-id").value.trim() }); };
$("rt-query").onclick = function () { var b = queryBody(); if (b) post("/agent/runtime/query", b); };
// Body templates for the "More calls" panel: one example per route.
var templates = {
"operations": { action: "get", name: "projects/P/locations/L/reasoningEngines/E/operations/O" },
"revisions": { action: "list", id: "ENGINE_ID" },
"update": { id: "ENGINE_ID", displayName: "New name" },
"iam": { action: "get", id: "ENGINE_ID" },
"code": { id: "ENGINE_ID", inputs: [{ code: "print(1)" }] },
"async/cancel": { id: "ENGINE_ID", operation: "projects/P/locations/L/operations/O" },
"feedback": { action: "list", id: "ENGINE_ID" },
"a2a": { target: "engine", protocol: "v1", op: "card", id: "ENGINE_ID" },
"a2a/tasks": { action: "list", id: "ENGINE_ID" },
"a2a/records": { action: "list", store: "STORE_ID" }
};
function fillTemplate() {
$("rt-body").value = JSON.stringify(templates[$("rt-route").value], null, 1);
}
$("rt-route").onchange = fillTemplate;
fillTemplate();
$("rt-send").onclick = function () {
var body;
try { body = JSON.parse($("rt-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
post("/agent/runtime/" + $("rt-route").value, body);
};
$("rt-stream").onclick = function () { var b = queryBody(); if (b) post("/agent/runtime/stream", b); };
})();