357 lines
15 KiB
C++
357 lines
15 KiB
C++
#include "google/registry/agent_registry.h"
|
|||
|
|
|
||
|
|
#include "google/errors.h"
|
||
|
|
#include "google/registry/caps.h"
|
||
|
|
#include "google/registry/ids.h"
|
||
|
|
|
||
|
|
namespace molo::google::registry {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
const char* asset_collection(AssetKind kind) {
|
||
|
|
switch (kind) {
|
||
|
|
case AssetKind::kAgents: return "agents";
|
||
|
|
case AssetKind::kMcpServers: return "mcpServers";
|
||
|
|
case AssetKind::kEndpoints: return "endpoints";
|
||
|
|
case AssetKind::kServices: return "services";
|
||
|
|
case AssetKind::kSkills: return "skills";
|
||
|
|
case AssetKind::kBindings: return "bindings";
|
||
|
|
case AssetKind::kPublishers: return "publishers";
|
||
|
|
case AssetKind::kAiApplications: return "aiApplications";
|
||
|
|
}
|
||
|
|
return "agents";
|
||
|
|
}
|
||
|
|
|
||
|
|
bool asset_kind_from(const std::string& c, AssetKind& out) {
|
||
|
|
for (AssetKind k : {AssetKind::kAgents, AssetKind::kMcpServers, AssetKind::kEndpoints,
|
||
|
|
AssetKind::kServices, AssetKind::kSkills, AssetKind::kBindings,
|
||
|
|
AssetKind::kPublishers, AssetKind::kAiApplications})
|
||
|
|
if (c == asset_collection(k)) {
|
||
|
|
out = k;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
ServiceEndpoint AgentRegistry::default_endpoint() {
|
||
|
|
return endpoints::custom("agentregistry", "https://agentregistry.googleapis.com/v1");
|
||
|
|
}
|
||
|
|
|
||
|
|
AgentRegistry::AgentRegistry(std::shared_ptr<GoogleClient> client, CloudContext context)
|
||
|
|
: RegistryService(std::move(client), default_endpoint(), std::move(context)) {}
|
||
|
|
|
||
|
|
std::string AgentRegistry::call_id(AssetKind kind, const char* verb) const {
|
||
|
|
// Skills (CRUD, search, revisions) and publishers exist in v1alpha only.
|
||
|
|
bool alpha = kind == AssetKind::kSkills || kind == AssetKind::kPublishers;
|
||
|
|
return std::string("agentregistry.") + (alpha ? "v1alpha." : "v1.") + asset_collection(kind) +
|
||
|
|
"." + verb;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string AgentRegistry::name_of(AssetKind kind, const std::string& id) const {
|
||
|
|
return context_.child(asset_collection(kind), id);
|
||
|
|
}
|
||
|
|
|
||
|
|
CallArgs AgentRegistry::parent_args() const { return with("parent", context_.parent()); }
|
||
|
|
|
||
|
|
// ---- Reads
|
||
|
|
|
||
|
|
Json AgentRegistry::locations() const {
|
||
|
|
return runner_.list("agentregistry.v1.locations.list", "locations",
|
||
|
|
with("name", "projects/" + context_.project));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::location(const std::string& id) const {
|
||
|
|
Ids::require_segment(id, "location id");
|
||
|
|
return runner_.invoke("agentregistry.v1.locations.get",
|
||
|
|
with("name", "projects/" + context_.project + "/locations/" + id));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::list(AssetKind kind, const std::map<std::string, std::string>& query) const {
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
for (const auto& kv : query) {
|
||
|
|
if (kv.first == "filter") Ids::require_text(kv.second, RegistryCaps::kMaxFilterChars, "filter");
|
||
|
|
a.query[kv.first] = kv.second;
|
||
|
|
}
|
||
|
|
return runner_.list(call_id(kind, "list"), asset_collection(kind), a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::get(AssetKind kind, const std::string& id) const {
|
||
|
|
return runner_.invoke(call_id(kind, "get"), with("name", name_of(kind, id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
SearchPage AgentRegistry::search(AssetKind kind, const std::string& query, int page_size,
|
||
|
|
const std::string& page_token) const {
|
||
|
|
if (kind != AssetKind::kAgents && kind != AssetKind::kMcpServers)
|
||
|
|
throw GoogleError::invalid("POST search exists for agents and mcpServers only");
|
||
|
|
Ids::require_text(query, RegistryCaps::kMaxSearchChars, "query");
|
||
|
|
Ids::require_text(page_token, 2048, "page token");
|
||
|
|
if (page_size < 1 || page_size > RegistryCaps::kPageSize)
|
||
|
|
throw GoogleError::invalid("page size must be 1..100");
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
a.body = Json::object();
|
||
|
|
if (!query.empty()) a.body.set("searchString", Json(query));
|
||
|
|
a.body.set("pageSize", Json(page_size));
|
||
|
|
if (!page_token.empty()) a.body.set("pageToken", Json(page_token));
|
||
|
|
Json reply = runner_.invoke(call_id(kind, "search"), a);
|
||
|
|
SearchPage page;
|
||
|
|
if (const Json* items = reply.find(asset_collection(kind)); items && items->is_array())
|
||
|
|
for (const Json& i : items->as_array()) page.items.push(i);
|
||
|
|
page.next_page_token = string_at(reply, "nextPageToken");
|
||
|
|
return page;
|
||
|
|
}
|
||
|
|
|
||
|
|
SearchPage AgentRegistry::search_skills(const std::string& query, const std::string& search_type,
|
||
|
|
int page_size, const std::string& page_token) const {
|
||
|
|
Ids::require_text(query, RegistryCaps::kMaxSearchChars, "query");
|
||
|
|
if (page_size < 1 || page_size > RegistryCaps::kPageSize)
|
||
|
|
throw GoogleError::invalid("page size must be 1..100");
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
put_query(a, "searchString", query);
|
||
|
|
put_query(a, "searchType", search_type);
|
||
|
|
put_query(a, "pageToken", page_token);
|
||
|
|
a.query["pageSize"] = std::to_string(page_size);
|
||
|
|
Json reply = runner_.invoke("agentregistry.v1alpha.skills.search", a);
|
||
|
|
SearchPage page;
|
||
|
|
if (const Json* items = reply.find("skills"); items && items->is_array())
|
||
|
|
for (const Json& i : items->as_array()) page.items.push(i);
|
||
|
|
page.next_page_token = string_at(reply, "nextPageToken");
|
||
|
|
return page;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::tools(const std::string& mcp_server_id) const {
|
||
|
|
Json server = get(AssetKind::kMcpServers, mcp_server_id);
|
||
|
|
const Json* t = server.find("tools");
|
||
|
|
return t && t->is_array() ? *t : Json::array();
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::list_approved(AssetKind kind, const ApprovalLedger& ledger) const {
|
||
|
|
return ledger.restrict_to_approved(list(kind));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- Services
|
||
|
|
|
||
|
|
Json AgentRegistry::register_service(const std::string& id, const ServiceRecord& record,
|
||
|
|
const std::string& request_id) const {
|
||
|
|
Ids::require_service_id(id);
|
||
|
|
Ids::require_request_id(request_id);
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
a.body = record.to_json(); // Validate before any traffic.
|
||
|
|
a.query["serviceId"] = id;
|
||
|
|
put_query(a, "requestId", request_id);
|
||
|
|
if (exists([&] { return get(AssetKind::kServices, id); }))
|
||
|
|
throw GoogleError(409, "ALREADY_EXISTS", "service " + id + " is already registered");
|
||
|
|
runner_.mutate("agentregistry.v1.services.create", a);
|
||
|
|
return get(AssetKind::kServices, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::update_service(const std::string& id, const ServiceRecord& record,
|
||
|
|
const std::vector<std::string>& mask,
|
||
|
|
const std::string& request_id) const {
|
||
|
|
Ids::require_service_id(id);
|
||
|
|
CallArgs a = with("name", name_of(AssetKind::kServices, id));
|
||
|
|
a.body = record.to_json();
|
||
|
|
a.query["updateMask"] = CallRunner::update_mask("agentregistry.v1.services.patch", mask);
|
||
|
|
put_query(a, "requestId", request_id);
|
||
|
|
get(AssetKind::kServices, id); // Must exist.
|
||
|
|
runner_.mutate("agentregistry.v1.services.patch", a);
|
||
|
|
return get(AssetKind::kServices, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::remove_service(const std::string& id) const {
|
||
|
|
Ids::require_service_id(id);
|
||
|
|
get(AssetKind::kServices, id);
|
||
|
|
return runner_.mutate("agentregistry.v1.services.delete",
|
||
|
|
with("name", name_of(AssetKind::kServices, id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- Skills
|
||
|
|
|
||
|
|
Json AgentRegistry::create_skill(const std::string& id, const SkillRecord& skill,
|
||
|
|
const SkillRevisionRecord* initial) const {
|
||
|
|
Ids::require_segment(id, "skill id");
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
a.body = skill.to_create_json(initial);
|
||
|
|
a.query["skillId"] = id;
|
||
|
|
if (exists([&] { return get(AssetKind::kSkills, id); }))
|
||
|
|
throw GoogleError(409, "ALREADY_EXISTS", "skill " + id + " already exists");
|
||
|
|
runner_.mutate("agentregistry.v1alpha.skills.create", a);
|
||
|
|
return get(AssetKind::kSkills, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::set_skill_state(const std::string& id, SkillState state) const {
|
||
|
|
Ids::require_segment(id, "skill id");
|
||
|
|
get(AssetKind::kSkills, id); // Read first.
|
||
|
|
CallArgs a = with("name", name_of(AssetKind::kSkills, id));
|
||
|
|
a.body = Json::object();
|
||
|
|
a.body.set("targetState", Json(skill_target_state_name(state)));
|
||
|
|
a.query["updateMask"] = CallRunner::update_mask("agentregistry.v1alpha.skills.patch", {"targetState"});
|
||
|
|
runner_.mutate("agentregistry.v1alpha.skills.patch", a);
|
||
|
|
return get(AssetKind::kSkills, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::delete_skill(const std::string& id, bool force) const {
|
||
|
|
Ids::require_segment(id, "skill id");
|
||
|
|
get(AssetKind::kSkills, id);
|
||
|
|
CallArgs a = with("name", name_of(AssetKind::kSkills, id));
|
||
|
|
if (force) a.query["force"] = "true";
|
||
|
|
return runner_.mutate("agentregistry.v1alpha.skills.delete", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::list_revisions(const std::string& skill_id) const {
|
||
|
|
Ids::require_segment(skill_id, "skill id");
|
||
|
|
return runner_.list("agentregistry.v1alpha.skills.revisions.list", "skillRevisions",
|
||
|
|
with("parent", name_of(AssetKind::kSkills, skill_id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::get_revision(const std::string& skill_id, const std::string& revision) const {
|
||
|
|
Ids::require_segment(skill_id, "skill id");
|
||
|
|
Ids::require_segment(revision, "revision id");
|
||
|
|
return runner_.invoke("agentregistry.v1alpha.skills.revisions.get",
|
||
|
|
with("name", name_of(AssetKind::kSkills, skill_id) + "/revisions/" + revision));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::create_revision(const std::string& skill_id, const std::string& revision_id,
|
||
|
|
const SkillRevisionRecord& revision) const {
|
||
|
|
Ids::require_segment(skill_id, "skill id");
|
||
|
|
Ids::require_segment(revision_id, "revision id");
|
||
|
|
get(AssetKind::kSkills, skill_id);
|
||
|
|
CallArgs a = with("parent", name_of(AssetKind::kSkills, skill_id));
|
||
|
|
a.body = revision.to_json();
|
||
|
|
a.query["skillRevisionId"] = revision_id;
|
||
|
|
runner_.mutate("agentregistry.v1alpha.skills.revisions.create", a);
|
||
|
|
return get_revision(skill_id, revision_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::delete_revision(const std::string& skill_id, const std::string& revision) const {
|
||
|
|
get_revision(skill_id, revision);
|
||
|
|
return runner_.mutate("agentregistry.v1alpha.skills.revisions.delete",
|
||
|
|
with("name", name_of(AssetKind::kSkills, skill_id) + "/revisions/" + revision));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- Bindings
|
||
|
|
|
||
|
|
Json AgentRegistry::create_binding(const std::string& id, const BindingRecord& binding) const {
|
||
|
|
Ids::require_segment(id, "binding id");
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
a.body = binding.to_json();
|
||
|
|
a.query["bindingId"] = id;
|
||
|
|
if (exists([&] { return get(AssetKind::kBindings, id); }))
|
||
|
|
throw GoogleError(409, "ALREADY_EXISTS", "binding " + id + " already exists");
|
||
|
|
runner_.mutate("agentregistry.v1.bindings.create", a);
|
||
|
|
return get(AssetKind::kBindings, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::update_binding(const std::string& id, const BindingRecord& binding,
|
||
|
|
const std::vector<std::string>& mask) const {
|
||
|
|
Ids::require_segment(id, "binding id");
|
||
|
|
CallArgs a = with("name", name_of(AssetKind::kBindings, id));
|
||
|
|
a.body = binding.to_json();
|
||
|
|
a.query["updateMask"] = CallRunner::update_mask("agentregistry.v1.bindings.patch", mask);
|
||
|
|
get(AssetKind::kBindings, id);
|
||
|
|
runner_.mutate("agentregistry.v1.bindings.patch", a);
|
||
|
|
return get(AssetKind::kBindings, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::delete_binding(const std::string& id) const {
|
||
|
|
Ids::require_segment(id, "binding id");
|
||
|
|
get(AssetKind::kBindings, id);
|
||
|
|
return runner_.mutate("agentregistry.v1.bindings.delete",
|
||
|
|
with("name", name_of(AssetKind::kBindings, id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::fetch_available_bindings(const std::string& source_id,
|
||
|
|
const std::string& target_id) const {
|
||
|
|
if (!source_id.empty() && !BindingRecord::valid_identifier(source_id, true))
|
||
|
|
throw GoogleError::invalid("source id must be urn:agent:{publisher}:{namespace}:{name}");
|
||
|
|
if (!target_id.empty() && !BindingRecord::valid_identifier(target_id, false))
|
||
|
|
throw GoogleError::invalid("target id must be a urn:agent|mcp|endpoint identifier");
|
||
|
|
CallArgs a = parent_args();
|
||
|
|
put_query(a, "sourceIdentifier", source_id);
|
||
|
|
put_query(a, "targetIdentifier", target_id);
|
||
|
|
return runner_.list("agentregistry.v1.bindings.fetchAvailable", "bindings", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- AI applications
|
||
|
|
|
||
|
|
Json AgentRegistry::application_assets(const std::string& application_id, AssetKind kind) const {
|
||
|
|
if (kind != AssetKind::kAgents && kind != AssetKind::kMcpServers && kind != AssetKind::kEndpoints)
|
||
|
|
throw GoogleError::invalid("applications hold agents, mcpServers and endpoints");
|
||
|
|
return runner_.list(std::string("agentregistry.v1.aiApplications.") + asset_collection(kind) + ".list",
|
||
|
|
asset_collection(kind),
|
||
|
|
with("parent", name_of(AssetKind::kAiApplications, application_id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- Operations
|
||
|
|
|
||
|
|
Json AgentRegistry::operation(const std::string& id) const {
|
||
|
|
Ids::require_segment(id, "operation id");
|
||
|
|
return runner_.invoke("agentregistry.v1.operations.get",
|
||
|
|
with("name", context_.parent() + "/operations/" + id));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::operations() const {
|
||
|
|
return runner_.list("agentregistry.v1.operations.list", "operations",
|
||
|
|
with("name", context_.parent()));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::cancel_operation(const std::string& id) const {
|
||
|
|
operation(id);
|
||
|
|
CallArgs a = with("name", context_.parent() + "/operations/" + id);
|
||
|
|
a.body = Json::object();
|
||
|
|
return runner_.invoke("agentregistry.v1.operations.cancel", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::delete_operation(const std::string& id) const {
|
||
|
|
operation(id);
|
||
|
|
return runner_.invoke("agentregistry.v1.operations.delete",
|
||
|
|
with("name", context_.parent() + "/operations/" + id));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- IAM
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
void require_iam_kind(AssetKind k) {
|
||
|
|
if (k != AssetKind::kSkills && k != AssetKind::kAiApplications)
|
||
|
|
throw GoogleError::invalid("IAM exists for skills and aiApplications only");
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
Json AgentRegistry::iam_policy(AssetKind kind, const std::string& id) const {
|
||
|
|
require_iam_kind(kind);
|
||
|
|
std::string cid = kind == AssetKind::kSkills ? "agentregistry.v1.skills.getIamPolicy"
|
||
|
|
: "agentregistry.v1.aiApplications.getIamPolicy";
|
||
|
|
return runner_.invoke(cid, with("resource", name_of(kind, id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::set_iam_policy(AssetKind kind, const std::string& id, const Json& policy) const {
|
||
|
|
require_iam_kind(kind);
|
||
|
|
if (!policy.is_object()) throw GoogleError::invalid("policy must be a JSON object");
|
||
|
|
iam_policy(kind, id); // Read first.
|
||
|
|
std::string cid = kind == AssetKind::kSkills ? "agentregistry.v1.skills.setIamPolicy"
|
||
|
|
: "agentregistry.v1.aiApplications.setIamPolicy";
|
||
|
|
CallArgs a = with("resource", name_of(kind, id));
|
||
|
|
a.body = Json::object();
|
||
|
|
a.body.set("policy", policy);
|
||
|
|
return runner_.invoke(cid, a);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json AgentRegistry::test_iam_permissions(AssetKind kind, const std::string& id,
|
||
|
|
const std::vector<std::string>& permissions) const {
|
||
|
|
require_iam_kind(kind);
|
||
|
|
if (permissions.empty() || permissions.size() > RegistryCaps::kMaxPermissions)
|
||
|
|
throw GoogleError::invalid("1..64 permissions");
|
||
|
|
std::string cid = kind == AssetKind::kSkills ? "agentregistry.v1.skills.testIamPermissions"
|
||
|
|
: "agentregistry.v1.aiApplications.testIamPermissions";
|
||
|
|
CallArgs a = with("resource", name_of(kind, id));
|
||
|
|
a.body = Json::object();
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const auto& p : permissions) {
|
||
|
|
Ids::require_text(p, 256, "permission");
|
||
|
|
arr.push(Json(p));
|
||
|
|
}
|
||
|
|
a.body.set("permissions", arr);
|
||
|
|
return runner_.invoke(cid, a);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::registry
|