feat(clients): enterprise: Discovery Engine, Dialogflow, Retail, Insights, CES, Recommendations
1080-row Discovery-derived call table, validating Caller, typed facades, demo under /google/enterprise, Discovery checker, live read checks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,409 @@
|
||||
#include "../GoogleDemos.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../../fw/Router.hpp"
|
||||
#include "../../fw/Templates.hpp"
|
||||
#include "../../fw/Validate.hpp"
|
||||
#include "clients/body/json.h"
|
||||
#include "google/enterprise/apis.h"
|
||||
#include "google/enterprise/caller.h"
|
||||
#include "google/enterprise/calls.h"
|
||||
#include "google/enterprise/caps.h"
|
||||
#include "google/enterprise/contact_center.h"
|
||||
#include "google/enterprise/dialogflow.h"
|
||||
#include "google/enterprise/discovery_engine.h"
|
||||
#include "google/enterprise/retail.h"
|
||||
#include "google/resource.h"
|
||||
|
||||
namespace molo::example::app {
|
||||
|
||||
// Discovery Engine (Gemini Enterprise search and agents), Dialogflow, Retail, Contact Center Insights. Stub: the package agent fills this in.
|
||||
// Google enterprise package (src/google/enterprise): Discovery Engine (Gemini
|
||||
// Enterprise), Dialogflow ES and CX, Retail, Recommendations AI, Contact
|
||||
// Center Insights, CES. GET routes never touch Google. Every route that would
|
||||
// call Google is a POST: input is validated first (422), then a credential is
|
||||
// required (503 google_not_configured). Reads (HTTP GET rows) run at once;
|
||||
// every other call is a dry run unless the body has "confirm":true.
|
||||
|
||||
namespace {
|
||||
|
||||
using Json = clients::body::Json;
|
||||
namespace ent = google::enterprise;
|
||||
|
||||
constexpr std::size_t kMaxBody = 256 * 1024;
|
||||
constexpr std::size_t kMaxStreamEvents = 100;
|
||||
constexpr std::size_t kMaxCallRows = 200;
|
||||
|
||||
[[noreturn]] void bad(const std::string& message) {
|
||||
throw fw::HttpError(422, "validation_error", message);
|
||||
}
|
||||
|
||||
[[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>
|
||||
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;
|
||||
}
|
||||
|
||||
Json parse_object(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 str(const Json& o, const char* key, bool required = false) {
|
||||
const Json* v = o.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();
|
||||
}
|
||||
|
||||
int integer(const Json& o, const char* key, int fallback) {
|
||||
const Json* v = o.find(key);
|
||||
if (!v || v->is_null()) return fallback;
|
||||
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
||||
return static_cast<int>(v->as_int());
|
||||
}
|
||||
|
||||
bool flag(const Json& o, const char* key) {
|
||||
const Json* v = o.find(key);
|
||||
return v && v->is_bool() && v->as_bool();
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> string_map(const Json& o, const char* key) {
|
||||
std::map<std::string, std::string> out;
|
||||
const Json* v = o.find(key);
|
||||
if (!v || v->is_null()) return out;
|
||||
if (!v->is_object()) bad(std::string(key) + " must be an object of strings");
|
||||
for (const auto& kv : v->as_object()) {
|
||||
if (!kv.second.is_string()) bad(std::string(key) + "." + kv.first + " must be a string");
|
||||
out[kv.first] = kv.second.as_string();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Json array_of(const Json& o, const char* key) {
|
||||
const Json* v = o.find(key);
|
||||
if (!v || !v->is_array()) bad(std::string(key) + " must be an array");
|
||||
return *v;
|
||||
}
|
||||
|
||||
ent::Api api_of(const std::string& slug) {
|
||||
try {
|
||||
return ent::api_from_slug(slug);
|
||||
} catch (const google::GoogleError&) {
|
||||
bad("api must be one of discoveryengine, dialogflow-es, dialogflow-cx, retail, insights, ces, recommendations");
|
||||
}
|
||||
}
|
||||
|
||||
class EnterpriseDemo {
|
||||
public:
|
||||
explicit EnterpriseDemo(Services& services) {
|
||||
if (!services.google) services.google = google::GoogleClient::from_env();
|
||||
client_ = services.google;
|
||||
}
|
||||
|
||||
bool configured() const { return client_->configured(); }
|
||||
google::CloudContext context() const { return google::CloudContext::from_env(); }
|
||||
|
||||
ent::Caller caller(ent::Api api, const std::string& location) const {
|
||||
return ent::Caller(client_, api, location.empty() ? "global" : location);
|
||||
}
|
||||
|
||||
// Validate (422), require a credential (503), then run: reads at once, any
|
||||
// other call as a dry run unless confirm.
|
||||
Json run(const ent::Caller& c, const ent::Invocation& inv, bool confirm) const {
|
||||
ent::Prepared p = c.prepare(inv);
|
||||
if (!configured()) throw google::NotConfiguredError("enterprise");
|
||||
const ent::CallSpec& spec = *p.spec;
|
||||
Json out = Json::object();
|
||||
out.set("rpc", Json(std::string(spec.rpc_id)));
|
||||
out.set("method", Json(std::string(spec.http_method)));
|
||||
out.set("url", Json(p.url()));
|
||||
if (spec.read_only()) {
|
||||
if (spec.paginated()) {
|
||||
ent::ListResult r = c.list(inv);
|
||||
out.set("count", Json(static_cast<int>(r.size())));
|
||||
out.set("pages", Json(r.pages));
|
||||
out.set("truncated", Json(r.truncated));
|
||||
out.set("cap_items", Json(static_cast<int>(ent::EnterpriseCaps::kMaxListItems)));
|
||||
out.set("items", r.items);
|
||||
} else {
|
||||
out.set("response", c.invoke(inv));
|
||||
}
|
||||
out.set("dry_run", Json(false));
|
||||
return out;
|
||||
}
|
||||
if (!confirm) {
|
||||
out.set("dry_run", Json(true));
|
||||
out.set("body", p.has_body ? p.body : Json());
|
||||
out.set("hint", Json("not sent: this call is not a read; send \"confirm\":true to run it"));
|
||||
return out;
|
||||
}
|
||||
out.set("dry_run", Json(false));
|
||||
if (spec.streaming()) {
|
||||
Json events = Json::array();
|
||||
std::size_t n = c.invoke_stream(inv, [&](const Json& e) {
|
||||
if (events.as_array().size() < kMaxStreamEvents) events.push(e);
|
||||
return true;
|
||||
});
|
||||
out.set("events", events);
|
||||
out.set("event_count", Json(static_cast<int>(n)));
|
||||
out.set("events_cap", Json(static_cast<int>(kMaxStreamEvents)));
|
||||
} else if (spec.destructive()) {
|
||||
out.set("response", c.invoke_destructive(inv, true));
|
||||
} else {
|
||||
out.set("response", c.invoke(inv));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<google::GoogleClient> client_;
|
||||
};
|
||||
|
||||
const char kPage[] = R"ENT({% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>Enterprise APIs</h1>
|
||||
<p class="hint">Gemini Enterprise (Discovery Engine), Dialogflow ES and CX, Retail,
|
||||
Recommendations AI, Contact Center Insights and CES. Every call comes from
|
||||
Google's Discovery documents. Reads run at once; any other call is a dry run
|
||||
until you tick Confirm. Needs a Google credential and GOOGLE_CLOUD_PROJECT.</p>
|
||||
<p id="e-config" class="hint">Checking configuration...</p>
|
||||
<section class="enterprise">
|
||||
<label class="inline">API
|
||||
<select id="e-api">
|
||||
<option value="discoveryengine">Discovery Engine (Gemini Enterprise)</option>
|
||||
<option value="dialogflow-es">Dialogflow ES</option>
|
||||
<option value="dialogflow-cx">Dialogflow CX</option>
|
||||
<option value="retail">Retail</option>
|
||||
<option value="recommendations">Recommendations AI</option>
|
||||
<option value="insights">Contact Center Insights</option>
|
||||
<option value="ces">CES</option>
|
||||
</select></label>
|
||||
<label class="inline">Find a call
|
||||
<input id="e-q" type="text" maxlength="100" placeholder="search, answer, detectIntent" autocomplete="off"></label>
|
||||
<button id="e-find" type="button">Find calls</button>
|
||||
<label class="inline">Call (rpc id)
|
||||
<input id="e-rpc" type="text" maxlength="200" autocomplete="off"></label>
|
||||
<label class="inline">Path parameters (JSON)
|
||||
<textarea id="e-path" rows="2" maxlength="2000" placeholder='{"parent":"projects/p/locations/global"}'></textarea></label>
|
||||
<label class="inline">Query (JSON)
|
||||
<textarea id="e-query" rows="1" maxlength="1000" placeholder='{"pageSize":"10"}'></textarea></label>
|
||||
<label class="inline">Body (JSON)
|
||||
<textarea id="e-body" rows="3" maxlength="20000"></textarea></label>
|
||||
<label class="inline"><input id="e-confirm" type="checkbox"> Confirm (really send non-read calls)</label>
|
||||
<button id="e-run" type="button" class="primary">Run call</button>
|
||||
<p id="e-error" class="error" role="alert"></p>
|
||||
<pre id="e-out" class="scroll"></pre>
|
||||
</section>
|
||||
<script src="/embedded/google_enterprise.js"></script>
|
||||
{% endblock %}
|
||||
)ENT";
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_google_enterprise_demo(fw::Router& router, Services& services) {
|
||||
(void)router;
|
||||
(void)services;
|
||||
auto demo = std::make_shared<EnterpriseDemo>(services);
|
||||
fw::Environment* env = services.templates.get();
|
||||
env->add_template("google_enterprise.html", kPage);
|
||||
services.add_feature("google-enterprise", "/google/enterprise");
|
||||
|
||||
// ---- Pages and status (never call Google).
|
||||
router.get("/google/enterprise", "google-enterprise-page", [env](fw::Request) {
|
||||
fw::TemplateCtx ctx;
|
||||
ctx["title"] = std::string("Enterprise APIs");
|
||||
return fw::Response(200, env->render("google_enterprise.html", ctx), "text/html; charset=utf-8");
|
||||
});
|
||||
|
||||
router.get("/google/enterprise/status", "google-enterprise-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("calls", Json(static_cast<int>(ent::call_count())));
|
||||
Json apis = Json::array();
|
||||
for (ent::Api a : ent::all_apis()) {
|
||||
const ent::ApiInfo& info = ent::api_info(a);
|
||||
Json o = Json::object();
|
||||
o.set("slug", Json(std::string(info.slug)));
|
||||
o.set("title", Json(std::string(info.title)));
|
||||
o.set("service", Json(std::string(info.service)));
|
||||
o.set("calls", Json(static_cast<int>(ent::calls_of(a).size())));
|
||||
o.set("activation_url", Json(ent::activation_url(a, c.project.empty() ? "example-214512124" : c.project)));
|
||||
apis.push(o);
|
||||
}
|
||||
j.set("apis", apis);
|
||||
Json caps = Json::object();
|
||||
caps.set("max_body_bytes", Json(static_cast<int>(ent::EnterpriseCaps::kMaxBodyBytes)));
|
||||
caps.set("page_size", Json(ent::EnterpriseCaps::kPageSize));
|
||||
caps.set("max_list_pages", Json(ent::EnterpriseCaps::kMaxListPages));
|
||||
caps.set("max_list_items", Json(static_cast<int>(ent::EnterpriseCaps::kMaxListItems)));
|
||||
caps.set("max_stream_events", Json(static_cast<int>(kMaxStreamEvents)));
|
||||
j.set("caps", caps);
|
||||
return json_reply(j);
|
||||
});
|
||||
|
||||
router.get("/google/enterprise/calls", "google-enterprise-calls", [](fw::Request req) {
|
||||
ent::Api api = api_of(req.query_or("api", "discoveryengine"));
|
||||
std::string q = req.query_or("q", "");
|
||||
if (q.size() > 100) bad("q is too long");
|
||||
auto rows = ent::search_calls(api, q, kMaxCallRows + 1);
|
||||
Json arr = Json::array();
|
||||
for (std::size_t i = 0; i < rows.size() && i < kMaxCallRows; ++i) {
|
||||
const ent::CallSpec& c = *rows[i];
|
||||
Json o = Json::object();
|
||||
o.set("rpc", Json(std::string(c.rpc_id)));
|
||||
o.set("method", Json(std::string(c.http_method)));
|
||||
o.set("path", Json(std::string(c.path)));
|
||||
o.set("version", Json(std::string(c.version)));
|
||||
o.set("flags", Json(std::string(c.flags)));
|
||||
arr.push(o);
|
||||
}
|
||||
Json j = Json::object();
|
||||
j.set("api", Json(std::string(ent::api_info(api).slug)));
|
||||
j.set("count", Json(static_cast<int>(arr.as_array().size())));
|
||||
j.set("truncated", Json(rows.size() > kMaxCallRows));
|
||||
j.set("cap_items", Json(static_cast<int>(kMaxCallRows)));
|
||||
j.set("calls", arr);
|
||||
return json_reply(j);
|
||||
});
|
||||
|
||||
// ---- Generic call: any row of the table.
|
||||
router.post("/google/enterprise/call", "google-enterprise-call", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
ent::Api api = api_of(str(in, "api", true));
|
||||
ent::Invocation inv;
|
||||
inv.rpc = str(in, "rpc", true);
|
||||
inv.path = string_map(in, "path");
|
||||
inv.query = string_map(in, "query");
|
||||
if (const Json* b = in.find("body"); b && !b->is_null()) {
|
||||
if (!b->is_object()) bad("body must be an object");
|
||||
inv.body = *b;
|
||||
}
|
||||
bool confirm = flag(in, "confirm");
|
||||
return guarded([&] {
|
||||
return json_reply(demo->run(demo->caller(api, str(in, "location")), inv, confirm));
|
||||
});
|
||||
});
|
||||
|
||||
// ---- Typed routes.
|
||||
router.post("/google/enterprise/search", "google-enterprise-search", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
return guarded([&] {
|
||||
ent::SearchQuery q;
|
||||
q.serving_config = str(in, "serving_config", true);
|
||||
q.query = str(in, "query", true);
|
||||
q.page_size = integer(in, "page_size", 10);
|
||||
q.filter = str(in, "filter");
|
||||
q.user_pseudo_id = str(in, "user_pseudo_id");
|
||||
return json_reply(demo->run(demo->caller(ent::Api::kDiscoveryEngine, ""),
|
||||
ent::DiscoveryEngine::search_invocation(q), flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/enterprise/answer", "google-enterprise-answer", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
return guarded([&] {
|
||||
ent::AnswerQuery q;
|
||||
q.serving_config = str(in, "serving_config", true);
|
||||
q.query_text = str(in, "query", true);
|
||||
q.session = str(in, "session");
|
||||
q.user_pseudo_id = str(in, "user_pseudo_id");
|
||||
return json_reply(demo->run(demo->caller(ent::Api::kDiscoveryEngine, ""),
|
||||
ent::DiscoveryEngine::answer_invocation(q, false), flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/enterprise/rank", "google-enterprise-rank", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
return guarded([&] {
|
||||
return json_reply(demo->run(demo->caller(ent::Api::kDiscoveryEngine, ""),
|
||||
ent::DiscoveryEngine::rank_invocation(str(in, "ranking_config", true),
|
||||
str(in, "query", true),
|
||||
array_of(in, "records"),
|
||||
integer(in, "top_n", 0)),
|
||||
flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/enterprise/ground", "google-enterprise-ground", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
return guarded([&] {
|
||||
return json_reply(demo->run(demo->caller(ent::Api::kDiscoveryEngine, ""),
|
||||
ent::DiscoveryEngine::grounding_invocation(
|
||||
str(in, "grounding_config", true), str(in, "answer", true),
|
||||
array_of(in, "facts")),
|
||||
flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/enterprise/detect", "google-enterprise-detect", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
std::string kind = str(in, "kind", true);
|
||||
if (kind != "es" && kind != "cx") bad("kind must be es or cx");
|
||||
return guarded([&] {
|
||||
std::string session = str(in, "session", true);
|
||||
std::string text = str(in, "text", true);
|
||||
std::string language = str(in, "language");
|
||||
if (language.empty()) language = "en";
|
||||
ent::Invocation inv = kind == "es" ? ent::DialogflowEs::detect_intent_invocation(session, text, language)
|
||||
: ent::DialogflowCx::detect_intent_invocation(session, text, language);
|
||||
return json_reply(demo->run(demo->caller(kind == "es" ? ent::Api::kDialogflowEs : ent::Api::kDialogflowCx, ""),
|
||||
inv, flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/enterprise/predict", "google-enterprise-predict", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
std::string kind = str(in, "kind", true);
|
||||
if (kind != "retail" && kind != "recommendations") bad("kind must be retail or recommendations");
|
||||
return guarded([&] {
|
||||
const Json* ev = in.find("user_event");
|
||||
if (!ev || !ev->is_object()) bad("user_event must be an object");
|
||||
std::string placement = str(in, "placement", true);
|
||||
int page_size = integer(in, "page_size", 10);
|
||||
bool dry = flag(in, "validate_only");
|
||||
ent::Invocation inv = kind == "retail"
|
||||
? ent::Retail::predict_invocation(placement, *ev, page_size, dry)
|
||||
: ent::Recommendations::predict_invocation(placement, *ev, page_size, dry);
|
||||
return json_reply(demo->run(demo->caller(kind == "retail" ? ent::Api::kRetail : ent::Api::kRecommendations, ""),
|
||||
inv, flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/enterprise/ces/run", "google-enterprise-ces-run", [demo](fw::Request req) {
|
||||
Json in = parse_object(req);
|
||||
return guarded([&] {
|
||||
ent::Invocation inv = ent::Ces::run_session_invocation(str(in, "session", true), str(in, "text", true));
|
||||
return json_reply(demo->run(demo->caller(ent::Api::kCes, ""), inv, flag(in, "confirm")));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace molo::example::app
|
||||
|
||||
Reference in New Issue
Block a user