feat(clients): google foundation for the agent platform: all-verb client, streaming, typed Gemini content, operations
GoogleClient gets every verb and stream_json (SSE and plain JSON), typed Content/Part/Tool/GenerateRequest with the LanguageModel seam, CloudContext resource names, long-running operation polling and pagination, OAuth bearer tokens via GOOGLE_ACCESS_TOKEN. Example scaffold: one stub per agent module under example/app/agent, wildcard builds, locust excludes /voice /google /agent. plans/16-agent-platform.md splits the work into nine modules. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#include "google/operation.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "google/errors.h"
|
||||
|
||||
namespace molo::google {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
Operation Operation::from_json(const Json& doc) {
|
||||
Operation op;
|
||||
if (const Json* v = doc.find("name"); v && v->is_string()) op.name = v->as_string();
|
||||
if (const Json* v = doc.find("done"); v && v->is_bool()) op.done = v->as_bool();
|
||||
if (const Json* v = doc.find("response")) op.response = *v;
|
||||
if (const Json* v = doc.find("metadata")) op.metadata = *v;
|
||||
if (const Json* e = doc.find("error"); e && e->is_object()) {
|
||||
const Json* c = e->find("code");
|
||||
op.error_code = c && c->is_number() ? static_cast<int>(c->as_int()) : 2;
|
||||
if (op.error_code == 0) op.error_code = 2;
|
||||
const Json* m = e->find("message");
|
||||
op.error_message = m && m->is_string() ? m->as_string() : "operation failed";
|
||||
}
|
||||
return op;
|
||||
}
|
||||
|
||||
Operation wait_operation(GoogleClient& client, const ServiceEndpoint& endpoint,
|
||||
const std::string& operation_name, const Sleeper& sleeper) {
|
||||
if (operation_name.empty() || operation_name.find("..") != std::string::npos)
|
||||
throw GoogleError::invalid("bad operation name");
|
||||
Sleeper sleep = sleeper ? sleeper : [](std::chrono::milliseconds d) {
|
||||
std::this_thread::sleep_for(d);
|
||||
};
|
||||
std::chrono::milliseconds waited{0};
|
||||
for (std::size_t i = 0; i < OperationCaps::kMaxPolls; ++i) {
|
||||
Operation op = Operation::from_json(client.get_json(endpoint, "/" + operation_name));
|
||||
if (op.done) {
|
||||
if (op.failed())
|
||||
throw GoogleError(502, "OPERATION_FAILED", op.error_message, endpoint.name);
|
||||
return op;
|
||||
}
|
||||
if (waited >= OperationCaps::kMaxWait) break;
|
||||
sleep(OperationCaps::kInterval);
|
||||
waited += OperationCaps::kInterval;
|
||||
}
|
||||
throw GoogleError(504, "DEADLINE_EXCEEDED",
|
||||
"operation still running at the wait cap: " + operation_name,
|
||||
endpoint.name);
|
||||
}
|
||||
|
||||
Json list_all(GoogleClient& client, const ServiceEndpoint& endpoint,
|
||||
const std::string& path, const std::string& items_key,
|
||||
std::size_t max_pages) {
|
||||
Json items = Json::array();
|
||||
std::string token;
|
||||
for (std::size_t page = 0; page < max_pages; ++page) {
|
||||
std::string url = path;
|
||||
if (!token.empty())
|
||||
url += (path.find('?') == std::string::npos ? "?" : "&") +
|
||||
std::string("pageToken=") + clients::Url::encode_component(token);
|
||||
Json reply = client.get_json(endpoint, url);
|
||||
if (const Json* arr = reply.find(items_key); arr && arr->is_array())
|
||||
for (const Json& item : arr->as_array()) items.push(item);
|
||||
const Json* next = reply.find("nextPageToken");
|
||||
if (!next || !next->is_string() || next->as_string().empty()) break;
|
||||
token = next->as_string();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
} // namespace molo::google
|
||||
Reference in New Issue
Block a user