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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user