784 lines
32 KiB
C++
784 lines
32 KiB
C++
#include "../AgentDemos.hpp"
|
|
|
|
#include <cstdlib>
|
|
|
|
#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 {
|
|
|
|
// Agent Platform: Agent Runtime (Agent Engine, reasoningEngines) over the
|
|
// real src/google/runtime part. GET routes need no credential. Every route
|
|
// that calls Google is a POST: 503 google_not_configured without a
|
|
// credential, 422 on bad input. Create never waits (deployments can take
|
|
// long): it answers the operation name; poll it with /agent/runtime/operation.
|
|
// Delete is a dry run unless the body says "confirm":true.
|
|
|
|
namespace {
|
|
|
|
namespace rt = google::runtime;
|
|
using Json = clients::body::Json;
|
|
|
|
constexpr std::size_t kMaxBody = 256 * 1024;
|
|
constexpr std::size_t kMaxStreamEventsShown = 100;
|
|
|
|
const char kPage[] = R"RUNTIME({% extends "base.html" %}
|
|
{% block content %}
|
|
<h1>Agent Runtime</h1>
|
|
<p class="hint">Deploy and call agents on Agent Engine (reasoningEngines).
|
|
Lists and queries only; create and delete stay behind explicit calls.</p>
|
|
<p id="rt-config" class="hint">Checking Google credentials...</p>
|
|
<section class="rt">
|
|
<label>Engine id <input id="rt-id" type="text" maxlength="128" placeholder="numeric engine id"></label>
|
|
<div class="rt-actions">
|
|
<button id="rt-list" type="button" class="primary">List engines</button>
|
|
<button id="rt-get" type="button">Get</button>
|
|
</div>
|
|
<label>Query input (JSON object) <textarea id="rt-input" rows="3">{"message":"hello"}</textarea></label>
|
|
<label>Class method <input id="rt-method" type="text" maxlength="64" value="query"></label>
|
|
<div class="rt-actions">
|
|
<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>
|
|
<style>
|
|
.rt label{display:block;margin:.5rem 0}
|
|
.rt input,.rt textarea{width:100%;box-sizing:border-box}
|
|
.rt-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
|
.rt-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
|
</style>
|
|
<script src="/embedded/agent_runtime.js"></script>
|
|
{% endblock %}
|
|
)RUNTIME";
|
|
|
|
fw::Response json_out(const Json& j, int status = 200) {
|
|
fw::Response res = fw::JsonResponse(j.dump(), status);
|
|
res.headers.set("Cache-Control", "no-store");
|
|
return res;
|
|
}
|
|
|
|
[[noreturn]] void 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());
|
|
}
|
|
|
|
template <typename F>
|
|
auto guarded(F&& f) -> decltype(f()) {
|
|
try {
|
|
return f();
|
|
} catch (const google::GoogleError& e) {
|
|
rethrow(e);
|
|
}
|
|
}
|
|
|
|
[[noreturn]] void bad(const std::string& why) {
|
|
throw fw::HttpError(422, "validation_error", why);
|
|
}
|
|
|
|
Json parse_body(const fw::Request& req) {
|
|
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
if (req.body.empty()) return Json::object();
|
|
try {
|
|
Json j = Json::parse(req.body);
|
|
if (!j.is_object()) bad("body must be a JSON object");
|
|
return j;
|
|
} catch (const clients::body::JsonError&) {
|
|
bad("body is not valid JSON");
|
|
}
|
|
}
|
|
|
|
std::string text(const Json& body, const char* key, bool required = false) {
|
|
const Json* v = body.find(key);
|
|
if (!v || v->is_null()) {
|
|
if (required) bad(std::string(key) + " is required");
|
|
return "";
|
|
}
|
|
if (!v->is_string()) bad(std::string(key) + " must be a string");
|
|
return v->as_string();
|
|
}
|
|
|
|
std::optional<int> number(const Json& body, const char* key) {
|
|
const Json* v = body.find(key);
|
|
if (!v || v->is_null()) return std::nullopt;
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
return static_cast<int>(v->as_int());
|
|
}
|
|
|
|
std::string engine_id(const Json& body) {
|
|
std::string id = text(body, "id", true);
|
|
if (!google::valid_segment(id)) bad("id must be 1-128 chars of A-Z a-z 0-9 . _ -");
|
|
return id;
|
|
}
|
|
|
|
Json engine_summary(const rt::Engine& e) {
|
|
Json o = Json::object();
|
|
o.set("name", Json(e.name));
|
|
o.set("id", Json(e.id()));
|
|
o.set("displayName", Json(e.display_name));
|
|
o.set("createTime", Json(e.create_time));
|
|
o.set("updateTime", Json(e.update_time));
|
|
o.set("agentFramework", Json(e.spec.agent_framework));
|
|
return o;
|
|
}
|
|
|
|
Json operation_json(const google::Operation& op) {
|
|
Json o = Json::object();
|
|
o.set("operation", Json(op.name));
|
|
o.set("done", Json(op.done));
|
|
if (op.failed()) o.set("error", Json(op.error_message));
|
|
if (op.done && op.response.is_object()) o.set("response", op.response);
|
|
return o;
|
|
}
|
|
|
|
rt::EngineDraft draft_from(const Json& body) {
|
|
rt::EngineDraft d;
|
|
d.display_name = text(body, "displayName", true);
|
|
d.description = text(body, "description");
|
|
d.spec.agent_framework = text(body, "agentFramework");
|
|
std::string image = text(body, "imageUri");
|
|
if (!image.empty()) d.spec.container = rt::ContainerSpec{image, std::nullopt};
|
|
std::string identity = text(body, "identityType");
|
|
if (!identity.empty()) {
|
|
if (identity != "SERVICE_ACCOUNT" && identity != "AGENT_IDENTITY")
|
|
bad("identityType must be SERVICE_ACCOUNT or AGENT_IDENTITY");
|
|
d.spec.identity = rt::identity_type_from(identity);
|
|
}
|
|
d.spec.service_account = text(body, "serviceAccount");
|
|
rt::DeploymentSpec dep;
|
|
bool has_dep = false;
|
|
if (auto n = number(body, "minInstances")) { dep.min_instances = n; has_dep = true; }
|
|
if (auto n = number(body, "maxInstances")) { dep.max_instances = n; has_dep = true; }
|
|
dep.cpu = text(body, "cpu");
|
|
dep.memory = text(body, "memory");
|
|
if (!dep.cpu.empty() || !dep.memory.empty()) has_dep = true;
|
|
if (has_dep) d.spec.deployment = dep;
|
|
try {
|
|
d.validate(); // local rules first: 422 before any credential check
|
|
} catch (const google::GoogleError& e) {
|
|
rethrow(e);
|
|
}
|
|
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) {
|
|
if (!services.google) services.google = google::GoogleClient::from_env();
|
|
auto client = services.google;
|
|
fw::Environment* env = services.templates.get();
|
|
services.templates->add_template("agent_runtime.html", kPage);
|
|
services.add_feature("agent-runtime", "/agent/runtime");
|
|
|
|
// Fresh per request so the environment is read when the call happens.
|
|
auto runtime = [client]() {
|
|
if (!client->configured()) throw google::NotConfiguredError("aiplatform");
|
|
return rt::AgentRuntime(client, google::CloudContext::from_env());
|
|
};
|
|
|
|
router.get("/agent/runtime", "agent-runtime-page", [env](fw::Request req) {
|
|
(void)req;
|
|
fw::TemplateCtx ctx;
|
|
ctx["title"] = std::string("Agent Runtime");
|
|
return fw::Response(200, env->render("agent_runtime.html", ctx), "text/html; charset=utf-8");
|
|
});
|
|
|
|
router.get("/agent/runtime/status", "agent-runtime-status", [client](fw::Request req) {
|
|
(void)req;
|
|
google::CloudContext ctx = google::CloudContext::from_env();
|
|
Json o = Json::object();
|
|
o.set("configured", Json(client->configured()));
|
|
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);
|
|
});
|
|
|
|
// The discovery-checked call table, for humans.
|
|
router.get("/agent/runtime/calls", "agent-runtime-calls", [](fw::Request req) {
|
|
(void)req;
|
|
Json arr = Json::array();
|
|
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));
|
|
row.set("version", Json(s.version));
|
|
row.set("rpc", Json(s.rpc_id));
|
|
arr.push(row);
|
|
}
|
|
Json o = Json::object();
|
|
o.set("calls", arr);
|
|
return json_out(o);
|
|
});
|
|
|
|
router.post("/agent/runtime/list", "agent-runtime-list", [runtime](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string filter = text(body, "filter");
|
|
if (filter.size() > rt::RuntimeCaps::kMaxFilterChars) bad("filter too long");
|
|
return guarded([&] {
|
|
Json arr = Json::array();
|
|
for (const auto& e : runtime().list(filter)) arr.push(engine_summary(e));
|
|
Json o = Json::object();
|
|
o.set("engines", arr);
|
|
return json_out(o);
|
|
});
|
|
});
|
|
|
|
router.post("/agent/runtime/get", "agent-runtime-get", [runtime](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string id = engine_id(body);
|
|
return guarded([&] { return json_out(runtime().get(id).raw); });
|
|
});
|
|
|
|
router.post("/agent/runtime/create", "agent-runtime-create", [runtime](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
rt::EngineDraft draft = draft_from(body);
|
|
return guarded([&] { return json_out(operation_json(runtime().create_start(draft)), 202); });
|
|
});
|
|
|
|
router.post("/agent/runtime/operation", "agent-runtime-operation", [runtime](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string name = text(body, "name", true);
|
|
if (name.rfind("projects/", 0) != 0 || name.size() > 512 || name.find("..") != std::string::npos)
|
|
bad("name must be an operation name starting with projects/");
|
|
return guarded([&] { return json_out(operation_json(runtime().operation(name))); });
|
|
});
|
|
|
|
auto query_args = [](const Json& body, std::string& id, Json& input, std::string& method) {
|
|
id = engine_id(body);
|
|
const Json* in = body.find("input");
|
|
input = in && in->is_object() ? *in : Json::object();
|
|
if (in && !in->is_object() && !in->is_null()) bad("input must be a JSON object");
|
|
method = text(body, "classMethod");
|
|
if (!method.empty()) {
|
|
try {
|
|
rt::AgentRuntime::query_body(input, method); // validates method and size
|
|
} catch (const google::GoogleError& e) {
|
|
rethrow(e);
|
|
}
|
|
}
|
|
};
|
|
|
|
router.post("/agent/runtime/query", "agent-runtime-query", [runtime, query_args](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string id, method;
|
|
Json input;
|
|
query_args(body, id, input, method);
|
|
return guarded([&] {
|
|
auto r = runtime();
|
|
return json_out(method.empty() ? r.query(id, input) : r.query(id, input, method));
|
|
});
|
|
});
|
|
|
|
// Streams server side, answers the first events as one JSON array.
|
|
router.post("/agent/runtime/stream", "agent-runtime-stream", [runtime, query_args](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string id, method;
|
|
Json input;
|
|
query_args(body, id, input, method);
|
|
return guarded([&] {
|
|
auto r = runtime();
|
|
Json events = Json::array();
|
|
auto sink = [&](const Json& e) {
|
|
events.push(e);
|
|
return events.as_array().size() < kMaxStreamEventsShown;
|
|
};
|
|
if (method.empty()) r.stream_query(id, input, sink);
|
|
else r.stream_query(id, input, sink, method);
|
|
Json o = Json::object();
|
|
o.set("events", events);
|
|
return json_out(o);
|
|
});
|
|
});
|
|
|
|
router.post("/agent/runtime/async", "agent-runtime-async", [runtime](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string id = engine_id(body);
|
|
std::string in = text(body, "inputGcsUri", true), out = text(body, "outputGcsUri", true);
|
|
if (in.rfind("gs://", 0) != 0 || out.rfind("gs://", 0) != 0)
|
|
bad("inputGcsUri and outputGcsUri must be gs:// URIs");
|
|
return guarded([&] { return json_out(operation_json(runtime().async_query(id, in, out)), 202); });
|
|
});
|
|
|
|
// Destructive: a dry run unless "confirm":true; force is its own field.
|
|
router.post("/agent/runtime/delete", "agent-runtime-delete", [runtime](fw::Request req) {
|
|
Json body = parse_body(req);
|
|
std::string id = engine_id(body);
|
|
const Json* f = body.find("force");
|
|
if (f && !f->is_bool()) bad("force must be true or false");
|
|
const Json* c = body.find("confirm");
|
|
if (c && !c->is_bool()) bad("confirm must be true or false");
|
|
bool force = f && f->as_bool();
|
|
if (!(c && c->as_bool())) {
|
|
Json o = Json::object();
|
|
o.set("dry_run", Json(true));
|
|
o.set("would_delete", Json(id));
|
|
o.set("force", Json(force));
|
|
o.set("hint", Json("send confirm:true to delete"));
|
|
return json_out(o);
|
|
}
|
|
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
|