Merge registry module (Agent Registry, Gateway, Identity)
This commit is contained in:
@@ -1,13 +1,442 @@
|
||||
#include "../AgentDemos.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../../fw/Router.hpp"
|
||||
#include "../../fw/Templates.hpp"
|
||||
#include "../../fw/Validate.hpp"
|
||||
#include "clients/body/json.h"
|
||||
#include "google/registry/agent_gateway.h"
|
||||
#include "google/registry/agent_identity.h"
|
||||
#include "google/registry/agent_principal.h"
|
||||
#include "google/registry/agent_registry.h"
|
||||
#include "google/registry/approvals.h"
|
||||
#include "google/registry/caps.h"
|
||||
#include "google/registry/gateway_policy.h"
|
||||
#include "google/registry/ids.h"
|
||||
|
||||
namespace molo::example::app {
|
||||
|
||||
// Agent Platform: registry. Stub: the module agent fills this in.
|
||||
// Agent Platform: registry (Agent Registry, Agent Gateway, Agent Identity)
|
||||
// over src/google/registry. GET routes never touch Google. Every route that
|
||||
// would call Google is a POST (or DELETE), validates input first (422) and
|
||||
// answers 503 google_not_configured without a credential. Approval state is
|
||||
// LOCAL (the registry API has no approve method, see src/google/registry/SPEC.md).
|
||||
|
||||
namespace {
|
||||
|
||||
using Json = clients::body::Json;
|
||||
namespace reg = google::registry;
|
||||
|
||||
[[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());
|
||||
if (e.status() == "ALREADY_EXISTS")
|
||||
throw fw::HttpError(409, "conflict", e.message());
|
||||
throw fw::HttpError(502, "google_error", e.status() + ": " + e.message());
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
fw::Response guarded(F&& f) {
|
||||
try {
|
||||
return f();
|
||||
} catch (const google::GoogleError& e) {
|
||||
rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
fw::Response json_reply(const Json& j, int status = 200) {
|
||||
fw::Response res = fw::JsonResponse(j.dump(), status);
|
||||
res.headers.set("Cache-Control", "no-store");
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string field(std::map<std::string, std::string>& in, const char* key) {
|
||||
auto it = in.find(key);
|
||||
return it == in.end() ? "" : it->second;
|
||||
}
|
||||
|
||||
[[noreturn]] void bad(const std::string& message) {
|
||||
throw fw::HttpError(422, "validation_error", message);
|
||||
}
|
||||
|
||||
reg::AssetKind kind_of(const std::string& value, std::initializer_list<reg::AssetKind> allowed) {
|
||||
reg::AssetKind kind;
|
||||
if (!reg::asset_kind_from(value, kind)) bad("kind is not a registry collection");
|
||||
for (auto a : allowed)
|
||||
if (a == kind) return kind;
|
||||
bad("kind " + value + " is not allowed here");
|
||||
}
|
||||
|
||||
// One object owns everything the routes share.
|
||||
class RegistryDemo {
|
||||
public:
|
||||
explicit RegistryDemo(Services& services) {
|
||||
if (!services.google) services.google = google::GoogleClient::from_env();
|
||||
client_ = services.google;
|
||||
}
|
||||
|
||||
reg::ApprovalLedger& ledger() { return ledger_; }
|
||||
bool configured() const { return client_->configured(); }
|
||||
|
||||
// Context is read per request so the environment can change at runtime.
|
||||
google::CloudContext context() const { return google::CloudContext::from_env(); }
|
||||
|
||||
void require_credential() const {
|
||||
if (!configured()) throw google::NotConfiguredError("registry");
|
||||
}
|
||||
reg::AgentRegistry registry() const { return reg::AgentRegistry(client_, ready()); }
|
||||
reg::AgentGateway gateway() const { return reg::AgentGateway(client_, ready()); }
|
||||
reg::GatewayPolicies policies() const { return reg::GatewayPolicies(client_, ready()); }
|
||||
reg::AgentIdentity identity() const { return reg::AgentIdentity(client_, ready()); }
|
||||
|
||||
private:
|
||||
google::CloudContext ready() const {
|
||||
require_credential();
|
||||
google::CloudContext c = context();
|
||||
c.parent(); // Throws GoogleError (invalid) for a missing project.
|
||||
return c;
|
||||
}
|
||||
|
||||
std::shared_ptr<google::GoogleClient> client_;
|
||||
reg::ApprovalLedger ledger_;
|
||||
};
|
||||
|
||||
const char kPage[] = R"REGISTRY({% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>Agent registry</h1>
|
||||
<p class="hint">Browse the Agent Registry (agents, MCP servers and tools,
|
||||
endpoints, skills), the Agent Gateways and policies that govern them, and
|
||||
Agent Identity. Needs a Google credential and GOOGLE_CLOUD_PROJECT. Approval
|
||||
here is a local list: the registry API has no approve method.</p>
|
||||
<p id="r-config" class="hint">Checking configuration...</p>
|
||||
<section class="registry">
|
||||
<label class="inline">Collection
|
||||
<select id="r-kind">
|
||||
<option value="agents">Agents</option>
|
||||
<option value="mcpServers">MCP servers</option>
|
||||
<option value="endpoints">Endpoints</option>
|
||||
<option value="services">Services</option>
|
||||
<option value="skills">Skills</option>
|
||||
<option value="bindings">Bindings</option>
|
||||
</select></label>
|
||||
<input id="r-q" type="text" maxlength="200" placeholder="Search text or asset id" autocomplete="off">
|
||||
<div class="registry-actions">
|
||||
<button id="r-list" type="button" class="primary">List</button>
|
||||
<button id="r-search" type="button">Search</button>
|
||||
<button id="r-get" type="button">Get</button>
|
||||
</div>
|
||||
<label class="inline">Governance
|
||||
<select id="r-gov">
|
||||
<option value="gateways">Agent gateways</option>
|
||||
<option value="policies">Gateway policies</option>
|
||||
<option value="identity">Agent identity (engine id)</option>
|
||||
<option value="approvals">Approvals (local)</option>
|
||||
<option value="audit">Approval audit (local)</option>
|
||||
</select></label>
|
||||
<button id="r-gov-go" type="button">Show</button>
|
||||
<p id="r-error" class="error" role="alert"></p>
|
||||
<pre id="r-out" class="scroll"></pre>
|
||||
</section>
|
||||
<script src="/embedded/agent_registry.js"></script>
|
||||
{% endblock %}
|
||||
)REGISTRY";
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_agent_registry_demo(fw::Router& router, Services& services) {
|
||||
(void)router;
|
||||
(void)services;
|
||||
auto demo = std::make_shared<RegistryDemo>(services);
|
||||
fw::Environment* env = services.templates.get();
|
||||
env->add_template("agent_registry.html", kPage);
|
||||
services.add_feature("agent-registry", "/agent/registry");
|
||||
|
||||
// ---- Pages and status (never call Google).
|
||||
router.get("/agent/registry", "agent-registry-page", [env](fw::Request) {
|
||||
fw::TemplateCtx ctx;
|
||||
ctx["title"] = std::string("Agent registry");
|
||||
return fw::Response(200, env->render("agent_registry.html", ctx), "text/html; charset=utf-8");
|
||||
});
|
||||
|
||||
router.get("/agent/registry/status", "agent-registry-status", [demo](fw::Request) {
|
||||
google::CloudContext c = demo->context();
|
||||
Json j = Json::object();
|
||||
j.set("configured", Json(demo->configured()));
|
||||
j.set("project_set", Json(c.valid()));
|
||||
j.set("location", Json(c.location));
|
||||
j.set("calls", Json(static_cast<int>(reg::Calls::all().size())));
|
||||
Json caps = Json::object();
|
||||
caps.set("page_size", Json(reg::RegistryCaps::kPageSize));
|
||||
caps.set("max_pages", Json(static_cast<int>(reg::RegistryCaps::kMaxPages)));
|
||||
caps.set("max_ledger_entries", Json(static_cast<int>(reg::RegistryCaps::kMaxLedgerEntries)));
|
||||
caps.set("max_audit_entries", Json(static_cast<int>(reg::RegistryCaps::kMaxAuditEntries)));
|
||||
j.set("caps", caps);
|
||||
j.set("approvals", Json(static_cast<int>(demo->ledger().size())));
|
||||
j.set("audit_dropped", Json(static_cast<int>(demo->ledger().audit().dropped())));
|
||||
return json_reply(j);
|
||||
});
|
||||
|
||||
router.get("/agent/registry/calls", "agent-registry-calls", [](fw::Request) {
|
||||
Json rows = Json::array();
|
||||
for (const reg::CallSpec& c : reg::Calls::all()) {
|
||||
Json r = Json::object();
|
||||
r.set("id", Json(c.id));
|
||||
r.set("method", Json(c.method));
|
||||
r.set("path", Json(std::string(c.version) + c.path));
|
||||
r.set("service", Json(c.service));
|
||||
r.set("rpc", Json(c.rpc));
|
||||
rows.push(r);
|
||||
}
|
||||
Json j = Json::object();
|
||||
j.set("calls", rows);
|
||||
return json_reply(j);
|
||||
});
|
||||
|
||||
router.get("/agent/registry/approvals", "agent-registry-approvals", [demo](fw::Request) {
|
||||
Json arr = Json::array();
|
||||
for (const auto& e : demo->ledger().entries()) {
|
||||
Json o = Json::object();
|
||||
o.set("name", Json(e.name));
|
||||
o.set("state", Json(reg::approval_state_name(e.state)));
|
||||
o.set("actor", Json(e.actor));
|
||||
o.set("note", Json(e.note));
|
||||
o.set("replaced_by", Json(e.replaced_by));
|
||||
arr.push(o);
|
||||
}
|
||||
Json j = Json::object();
|
||||
j.set("scope", Json("local"));
|
||||
j.set("approvals", arr);
|
||||
return json_reply(j);
|
||||
});
|
||||
|
||||
router.get("/agent/registry/audit", "agent-registry-audit", [demo](fw::Request) {
|
||||
Json arr = Json::array();
|
||||
for (const auto& r : demo->ledger().audit().snapshot()) {
|
||||
Json o = Json::object();
|
||||
o.set("seq", Json(static_cast<int>(r.seq)));
|
||||
o.set("actor", Json(r.actor));
|
||||
o.set("action", Json(r.action));
|
||||
o.set("target", Json(r.target));
|
||||
o.set("detail", Json(r.detail));
|
||||
arr.push(o);
|
||||
}
|
||||
Json j = Json::object();
|
||||
j.set("scope", Json("local"));
|
||||
j.set("dropped", Json(static_cast<int>(demo->ledger().audit().dropped())));
|
||||
j.set("entries", arr);
|
||||
return json_reply(j);
|
||||
});
|
||||
|
||||
// ---- Local helper: build identity strings. No credential needed.
|
||||
router.post("/agent/registry/identity/principal", "agent-registry-principal", [](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
return guarded([&] {
|
||||
std::string org = field(in, "org_id");
|
||||
std::string project_number = field(in, "project_number");
|
||||
Json j = Json::object();
|
||||
j.set("org_trust_domain", Json(reg::AgentPrincipal::org_trust_domain(org)));
|
||||
j.set("org_set", Json(reg::AgentPrincipal::org_set(org)));
|
||||
if (!project_number.empty())
|
||||
j.set("project_set", Json(reg::AgentPrincipal::project_set(org, project_number)));
|
||||
std::string agent = field(in, "agent");
|
||||
if (!agent.empty()) {
|
||||
reg::AgentPrincipal p{reg::AgentPrincipal::org_trust_domain(org),
|
||||
field(in, "namespace").empty() ? "aiplatform" : field(in, "namespace"),
|
||||
agent};
|
||||
j.set("principal", Json(p.str()));
|
||||
}
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- Reads from Google (POST: they call out).
|
||||
router.post("/agent/registry/list", "agent-registry-list", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
reg::AssetKind kind = kind_of(field(in, "kind"),
|
||||
{reg::AssetKind::kAgents, reg::AssetKind::kMcpServers,
|
||||
reg::AssetKind::kEndpoints, reg::AssetKind::kServices,
|
||||
reg::AssetKind::kSkills, reg::AssetKind::kBindings,
|
||||
reg::AssetKind::kPublishers});
|
||||
bool approved_only = field(in, "approved_only") == "true";
|
||||
return guarded([&] {
|
||||
reg::AgentRegistry registry = demo->registry();
|
||||
Json items = approved_only ? registry.list_approved(kind, demo->ledger())
|
||||
: registry.list(kind);
|
||||
Json j = Json::object();
|
||||
j.set("kind", Json(reg::asset_collection(kind)));
|
||||
j.set("approved_only", Json(approved_only));
|
||||
j.set("count", Json(static_cast<int>(items.as_array().size())));
|
||||
j.set("items", items);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/registry/get", "agent-registry-get", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
reg::AssetKind kind = kind_of(field(in, "kind"),
|
||||
{reg::AssetKind::kAgents, reg::AssetKind::kMcpServers,
|
||||
reg::AssetKind::kEndpoints, reg::AssetKind::kServices,
|
||||
reg::AssetKind::kSkills, reg::AssetKind::kBindings});
|
||||
std::string id = field(in, "id");
|
||||
if (!google::valid_segment(id)) bad("id is empty or has invalid characters");
|
||||
return guarded([&] {
|
||||
Json item = demo->registry().get(kind, id);
|
||||
Json j = Json::object();
|
||||
j.set("kind", Json(reg::asset_collection(kind)));
|
||||
j.set("item", item);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/registry/search", "agent-registry-search", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
reg::AssetKind kind = kind_of(field(in, "kind"),
|
||||
{reg::AssetKind::kAgents, reg::AssetKind::kMcpServers});
|
||||
std::string q = field(in, "q");
|
||||
if (q.size() > reg::RegistryCaps::kMaxSearchChars) bad("q is too long");
|
||||
return guarded([&] {
|
||||
reg::SearchPage page = demo->registry().search(kind, q);
|
||||
Json j = Json::object();
|
||||
j.set("kind", Json(reg::asset_collection(kind)));
|
||||
j.set("items", page.items);
|
||||
j.set("next_page_token", Json(page.next_page_token));
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- Approval (local ledger). Reads the asset from Google first, so an
|
||||
// unknown asset is never recorded as approved.
|
||||
auto decide = [demo](const char* action) {
|
||||
return [demo, action](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
std::string name = field(in, "name");
|
||||
std::string actor = field(in, "actor");
|
||||
if (!reg::Ids::asset_name(name)) bad("name must be a full registry asset name");
|
||||
if (actor.empty() || actor.size() > reg::RegistryCaps::kMaxActorChars) bad("actor is required (max 128)");
|
||||
if (field(in, "note").size() > reg::RegistryCaps::kMaxNoteChars) bad("note is too long");
|
||||
std::string replaced_by = field(in, "replaced_by");
|
||||
if (!replaced_by.empty() && !reg::Ids::asset_name(replaced_by)) bad("replaced_by must be an asset name");
|
||||
reg::AssetKind kind;
|
||||
if (!reg::asset_kind_from(reg::Ids::collection_of(name), kind)) bad("unknown collection");
|
||||
return guarded([&] {
|
||||
reg::AgentRegistry registry = demo->registry();
|
||||
std::string id = name.substr(name.rfind('/') + 1);
|
||||
if (name.rfind(registry.context().parent() + "/", 0) != 0)
|
||||
bad("asset is not in the configured project and location");
|
||||
registry.get(kind, id); // Read before decide.
|
||||
reg::ApprovalEntry e;
|
||||
std::string a = action;
|
||||
if (a == "approve") e = demo->ledger().approve(name, actor, field(in, "note"));
|
||||
else if (a == "deprecate") e = demo->ledger().deprecate(name, actor, field(in, "note"), replaced_by);
|
||||
else e = demo->ledger().reject(name, actor, field(in, "note"));
|
||||
Json j = Json::object();
|
||||
j.set("scope", Json("local"));
|
||||
j.set("name", Json(e.name));
|
||||
j.set("state", Json(reg::approval_state_name(e.state)));
|
||||
return json_reply(j);
|
||||
});
|
||||
};
|
||||
};
|
||||
router.post("/agent/registry/approve", "agent-registry-approve", decide("approve"));
|
||||
router.post("/agent/registry/deprecate", "agent-registry-deprecate", decide("deprecate"));
|
||||
router.post("/agent/registry/reject", "agent-registry-reject", decide("reject"));
|
||||
|
||||
// ---- Skills have a real lifecycle field: this one is a Google PATCH.
|
||||
router.post("/agent/registry/skills/state", "agent-registry-skill-state", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
std::string id = field(in, "id");
|
||||
std::string state = field(in, "state");
|
||||
if (!google::valid_segment(id)) bad("id is empty or has invalid characters");
|
||||
reg::SkillState s;
|
||||
if (state == "draft") s = reg::SkillState::kDraft;
|
||||
else if (state == "active") s = reg::SkillState::kActive;
|
||||
else if (state == "disabled") s = reg::SkillState::kDisabled;
|
||||
else if (state == "deprecated") s = reg::SkillState::kDeprecated;
|
||||
else if (state == "decommissioned") s = reg::SkillState::kDecommissioned;
|
||||
else bad("state must be draft, active, disabled, deprecated or decommissioned");
|
||||
return guarded([&] {
|
||||
Json skill = demo->registry().set_skill_state(id, s);
|
||||
Json j = Json::object();
|
||||
j.set("scope", Json("google"));
|
||||
j.set("skill", skill);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- Destructive and explicit: DELETE only.
|
||||
router.del("/agent/registry/services/{id}", "agent-registry-service-delete", [demo](fw::Request req) {
|
||||
std::string id = req.param_or("id", "");
|
||||
if (!reg::Ids::service_id(id)) bad("service id must be 4-63 of a-z 0-9 - and start with a letter");
|
||||
return guarded([&] {
|
||||
Json j = demo->registry().remove_service(id);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- Gateways and policies (reads, plus an explicit delete).
|
||||
router.post("/agent/registry/gateways/list", "agent-registry-gateways", [demo](fw::Request) {
|
||||
return guarded([&] {
|
||||
Json items = demo->gateway().list();
|
||||
Json j = Json::object();
|
||||
j.set("count", Json(static_cast<int>(items.as_array().size())));
|
||||
j.set("gateways", items);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/registry/gateways/get", "agent-registry-gateway-get", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
std::string id = field(in, "id");
|
||||
if (!google::valid_segment(id)) bad("id is empty or has invalid characters");
|
||||
return guarded([&] { return json_reply(demo->gateway().get(id)); });
|
||||
});
|
||||
|
||||
router.del("/agent/registry/gateways/{id}", "agent-registry-gateway-delete", [demo](fw::Request req) {
|
||||
std::string id = req.param_or("id", "");
|
||||
if (!google::valid_segment(id)) bad("id has invalid characters");
|
||||
return guarded([&] { return json_reply(demo->gateway().remove_gateway(id)); });
|
||||
});
|
||||
|
||||
router.post("/agent/registry/policies/list", "agent-registry-policies", [demo](fw::Request) {
|
||||
return guarded([&] {
|
||||
Json items = demo->policies().list();
|
||||
Json j = Json::object();
|
||||
j.set("count", Json(static_cast<int>(items.as_array().size())));
|
||||
j.set("policies", items);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- Identity of one deployed agent (engine id).
|
||||
router.post("/agent/registry/identity", "agent-registry-identity", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
std::string engine = field(in, "engine_id");
|
||||
if (!google::valid_segment(engine)) bad("engine_id is empty or has invalid characters");
|
||||
return guarded([&] {
|
||||
reg::IdentityInfo info = demo->identity().identity(engine);
|
||||
Json j = Json::object();
|
||||
j.set("engine", Json(info.engine));
|
||||
j.set("identity_type", Json(info.identity_type));
|
||||
j.set("effective_identity", Json(info.effective));
|
||||
j.set("principal", Json(info.principal));
|
||||
j.set("agent_identity", Json(info.agent_identity));
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/agent/registry/identity/audit", "agent-registry-identity-audit", [demo](fw::Request req) {
|
||||
auto in = fw::parse_json_object_flat(req.body);
|
||||
std::string principal = field(in, "principal");
|
||||
if (!reg::AgentPrincipal::is_agent_principal(principal)) bad("principal must be a principal:// agent identity");
|
||||
return guarded([&] {
|
||||
Json arr = Json::array();
|
||||
for (const auto& e : demo->identity().audit(principal, 50)) arr.push(reg::AuditMapper::to_json(e));
|
||||
Json j = Json::object();
|
||||
j.set("events", arr);
|
||||
return json_reply(j);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace molo::example::app
|
||||
|
||||
Reference in New Issue
Block a user