#include "../AgentDemos.hpp"
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "google/adk/ap2.h"
#include "google/adk/caps.h"
#include "google/adk/direct_model.h"
#include "google/adk/error.h"
#include "google/adk/garden.h"
#include "google/adk/runner.h"
#include "google/adk/studio.h"
#include "google/credentials.h"
#include "google/generative.h"
namespace molo::example::app {
// Agent Platform: Agent Development Kit (src/google/adk). Pages and the
// catalog work without a credential; /agent/adk/run is the one route that
// calls Google (503 google_not_configured without a key).
namespace {
using clients::body::Json;
namespace adk = google::adk;
const char kAdkPage[] = R"ADK({% extends "base.html" %}
{% block content %}
Agent Development Kit
Run a Garden template as an agent on Gemini, or export and
import an agent definition. Caps: 12 model calls, 24 tool calls, 60 s per turn.
{% endblock %}
)ADK";
Json parse_body(const fw::Request& req) {
try {
Json doc = Json::parse(req.body);
if (!doc.is_object()) throw std::runtime_error("object expected");
return doc;
} catch (const std::exception&) {
throw fw::HttpError(422, "validation_error", "body must be a JSON object");
}
}
std::string text_of(const Json& doc, const char* key) {
const Json* v = doc.find(key);
return v && v->is_string() ? v->as_string() : "";
}
template
auto guarded(F&& f) -> decltype(f()) {
try {
return f();
} catch (const adk::AdkError& e) {
if (e.code() == "invalid_argument")
throw fw::HttpError(422, "validation_error", e.message());
throw fw::HttpError(502, "adk_error", e.message());
} catch (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());
}
}
fw::Response json_out(const Json& doc, int status = 200) {
fw::Response res = fw::JsonResponse(doc.dump(), status);
res.headers.set("Cache-Control", "no-store");
return res;
}
// The single local tool the demo agents get: deterministic, no network.
std::shared_ptr word_count_tool() {
Json text = Json::object();
text.set("type", Json("string"));
Json props = Json::object();
props.set("text", text);
Json req = Json::array();
req.push(Json("text"));
Json schema = Json::object();
schema.set("type", Json("object"));
schema.set("properties", props);
schema.set("required", req);
return std::make_shared(
"word_count", "Count the words in a text", schema,
[](const Json& args, adk::ToolContext&) {
const std::string& s = args.find("text")->as_string();
long n = 0;
bool in = false;
for (char c : s) {
bool space = c == ' ' || c == '\n' || c == '\t';
if (!space && !in) ++n;
in = !space;
}
Json out = Json::object();
out.set("words", Json(n));
return out;
});
}
} // namespace
void register_agent_adk_demo(fw::Router& router, Services& services) {
if (!services.google) services.google = google::GoogleClient::from_env();
auto gclient = services.google;
fw::Environment* env = services.templates.get();
env->add_template("adk.html", kAdkPage);
services.add_feature("agent-adk", "/agent/adk");
auto registry = std::make_shared();
registry->add(word_count_tool());
auto sessions = std::make_shared();
router.get("/agent/adk", "agent-adk-page", [env](fw::Request) {
fw::TemplateCtx ctx;
ctx["title"] = std::string("Agent Development Kit");
return fw::Response(200, env->render("adk.html", ctx), "text/html; charset=utf-8");
});
router.get("/agent/adk/status", "agent-adk-status", [gclient](fw::Request) {
using C = adk::AdkCaps;
Json caps = Json::object();
caps.set("max_steps", Json(C::kMaxSteps));
caps.set("max_tool_calls", Json(C::kMaxToolCalls));
caps.set("max_wall_clock_ms", Json(C::kMaxWallClockMs));
caps.set("max_sub_agents", Json(static_cast(C::kMaxSubAgents)));
caps.set("max_loop_iterations", Json(C::kMaxLoopIterations));
Json out = Json::object();
out.set("configured", Json(gclient->configured()));
out.set("model", Json(google::Generative::default_model()));
out.set("caps", caps);
out.set("templates", Json(static_cast(adk::AgentGarden::templates().size())));
return json_out(out);
});
router.get("/agent/adk/garden", "agent-adk-garden", [](fw::Request) {
return json_out(adk::AgentGarden::catalog_json());
});
// {"prompt": "...", "template": "invoice_processing", "cid": "optional"}
router.post("/agent/adk/run", "agent-adk-run",
[gclient, registry, sessions](fw::Request req) {
Json in = parse_body(req);
std::string prompt = text_of(in, "prompt");
std::string tmpl = text_of(in, "template");
std::string cid = text_of(in, "cid");
if (tmpl.empty()) tmpl = "financial_analysis";
if (cid.empty()) cid = "demo";
return guarded([&] {
if (prompt.empty() || prompt.size() > 2000)
throw adk::AdkError::invalid("prompt must be 1..2000 characters");
std::shared_ptr agent = adk::AgentGarden::instantiate(tmpl, *registry);
if (!gclient->configured()) throw google::NotConfiguredError("generativelanguage");
bool express = google::Credentials::env().rfind("AQ.", 0) == 0;
auto model = std::make_shared(
gclient, google::Generative::default_model(),
express ? google::endpoints::vertex_generative()
: google::endpoints::generative());
adk::Runner runner(model, sessions);
return json_out(runner.run(agent, cid, prompt).to_json());
});
});
// {"template": "invoice_processing"} -> the Agent Studio definition.
router.post("/agent/adk/export", "agent-adk-export", [registry](fw::Request req) {
Json in = parse_body(req);
return guarded([&] {
auto agent = adk::AgentGarden::instantiate(text_of(in, "template"), *registry);
Json out = Json::object();
out.set("definition", adk::AgentDefinition::to_json(*agent));
out.set("yaml", Json(adk::AgentDefinition::to_yaml(*agent)));
return json_out(out);
});
});
// The body is an agent definition; answers its normalized re-export.
router.post("/agent/adk/import", "agent-adk-import", [registry](fw::Request req) {
return guarded([&] {
auto agent = adk::AgentDefinition::from_text(req.body, *registry);
return json_out(adk::AgentDefinition::to_json(*agent));
});
});
// {"intent": {...}, "cart": {...}, "payment": {...}} (cart and payment optional)
router.post("/agent/adk/ap2/validate", "agent-adk-ap2", [](fw::Request req) {
Json in = parse_body(req);
return guarded([&] {
const Json* i = in.find("intent");
if (!i) throw adk::AdkError::invalid("intent is required");
auto intent = adk::ap2::IntentMandate::from_json(*i);
intent.validate();
if (const Json* c = in.find("cart")) {
auto cart = adk::ap2::CartMandate::from_json(*c);
cart.validate_against(intent);
if (const Json* p = in.find("payment"))
adk::ap2::PaymentMandate::from_json(*p).validate_against(cart);
}
Json out = Json::object();
out.set("valid", Json(true));
return json_out(out);
});
});
}
} // namespace molo::example::app