344 lines
14 KiB
C++
344 lines
14 KiB
C++
#include "google/runtime/a2a.h"
|
|||
|
|
|
||
|
|
#include "clients/url.h"
|
||
|
|
#include "google/errors.h"
|
||
|
|
#include "google/operation.h"
|
||
|
|
|
||
|
|
namespace molo::google::runtime {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
std::string str(const Json& doc, const char* key) {
|
||
|
|
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
|
||
|
|
return v && v->is_string() ? v->as_string() : std::string();
|
||
|
|
}
|
||
|
|
|
||
|
|
void check_size(const Json& j, const char* what) {
|
||
|
|
if (j.dump().size() > A2aCaps::kMaxBodyBytes)
|
||
|
|
throw GoogleError::invalid(std::string(what) + " over " +
|
||
|
|
std::to_string(A2aCaps::kMaxBodyBytes) + " bytes");
|
||
|
|
}
|
||
|
|
|
||
|
|
void check_filter(const std::string& filter, const std::string& order_by) {
|
||
|
|
if (filter.size() > 512) throw GoogleError::invalid("filter over 512 characters");
|
||
|
|
if (order_by.size() > 128) throw GoogleError::invalid("orderBy over 128 characters");
|
||
|
|
}
|
||
|
|
|
||
|
|
void check_segment(const std::string& id, const char* what) {
|
||
|
|
if (!valid_segment(id)) throw GoogleError::invalid(std::string("bad ") + what);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string enc(const std::string& s) { return clients::Url::encode_component(s); }
|
||
|
|
|
||
|
|
void set_if(Json& o, const char* key, const std::string& v) {
|
||
|
|
if (!v.empty()) o.set(key, Json(v));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
bool A2aRequest::is_get() const {
|
||
|
|
return op != A2aOp::kTaskCancel && op != A2aOp::kMessageSend && op != A2aOp::kMessageStream;
|
||
|
|
}
|
||
|
|
|
||
|
|
void A2aTaskInfo::validate() const {
|
||
|
|
if (context_id.size() > 256 || app_id.size() > 256 || user_id.size() > 256 ||
|
||
|
|
ttl.size() > 32 || expire_time.size() > 64)
|
||
|
|
throw GoogleError::invalid("task field too long");
|
||
|
|
if (!metadata.is_null()) {
|
||
|
|
if (!metadata.is_object()) throw GoogleError::invalid("metadata must be an object");
|
||
|
|
check_size(metadata, "metadata");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Json A2aTaskInfo::to_json() const {
|
||
|
|
validate();
|
||
|
|
Json o = Json::object();
|
||
|
|
set_if(o, "contextId", context_id);
|
||
|
|
set_if(o, "appId", app_id);
|
||
|
|
set_if(o, "userId", user_id);
|
||
|
|
set_if(o, "ttl", ttl);
|
||
|
|
set_if(o, "expireTime", expire_time);
|
||
|
|
if (metadata.is_object()) o.set("metadata", metadata);
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
|
||
|
|
A2aTaskInfo A2aTaskInfo::from_json(const Json& doc) {
|
||
|
|
A2aTaskInfo t;
|
||
|
|
t.raw = doc;
|
||
|
|
t.name = str(doc, "name");
|
||
|
|
t.context_id = str(doc, "contextId");
|
||
|
|
t.app_id = str(doc, "appId");
|
||
|
|
t.user_id = str(doc, "userId");
|
||
|
|
t.ttl = str(doc, "ttl");
|
||
|
|
t.expire_time = str(doc, "expireTime");
|
||
|
|
t.create_time = str(doc, "createTime");
|
||
|
|
t.update_time = str(doc, "updateTime");
|
||
|
|
t.generation = str(doc, "generation");
|
||
|
|
if (const Json* s = doc.is_object() ? doc.find("state") : nullptr; s && s->is_string())
|
||
|
|
t.state = s->as_string();
|
||
|
|
else if (const Json* st = doc.is_object() ? doc.find("status") : nullptr; st && st->is_object())
|
||
|
|
t.state = str(*st, "state");
|
||
|
|
if (const Json* m = doc.is_object() ? doc.find("metadata") : nullptr; m && m->is_object())
|
||
|
|
t.metadata = *m;
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
RuntimeA2a::RuntimeA2a(std::shared_ptr<GoogleClient> client, CloudContext context)
|
||
|
|
: Service(std::move(client), endpoints::aiplatform_beta(context.location)),
|
||
|
|
context_(std::move(context)) {}
|
||
|
|
|
||
|
|
std::string RuntimeA2a::engine_name(const std::string& engine_id) const {
|
||
|
|
return context_.child("reasoningEngines", engine_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string RuntimeA2a::store_name(const std::string& store_id) const {
|
||
|
|
return context_.child("taskStores", store_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
void RuntimeA2a::check_task_id(const std::string& id) {
|
||
|
|
if (id.empty() || id.size() > A2aCaps::kMaxTaskIdChars)
|
||
|
|
throw GoogleError::invalid("task id must be 1.." + std::to_string(A2aCaps::kMaxTaskIdChars) +
|
||
|
|
" characters");
|
||
|
|
for (std::size_t i = 0; i < id.size(); ++i) {
|
||
|
|
char c = id[i];
|
||
|
|
bool alnum = (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
|
||
|
|
if (!(alnum || (i > 0 && c == '-')))
|
||
|
|
throw GoogleError::invalid("task id must be lowercase letters, digits and hyphens, "
|
||
|
|
"starting with a letter or digit");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string RuntimeA2a::task_name(const std::string& engine_id, const std::string& task_id) const {
|
||
|
|
check_segment(task_id, "task id");
|
||
|
|
return engine_name(engine_id) + "/a2aTasks/" + task_id;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string RuntimeA2a::record_name(const std::string& store_id,
|
||
|
|
const std::string& record_id) const {
|
||
|
|
check_segment(record_id, "record id");
|
||
|
|
return store_name(store_id) + "/a2aTaskRecords/" + record_id;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json RuntimeA2a::send(Call call, const std::string& target, const std::string& query,
|
||
|
|
const Json* body) const {
|
||
|
|
return client_->request(method_of(call), endpoint_of(call, context_.location, true),
|
||
|
|
render(call, target) + query, body);
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- gateway -----------------------------------------------------------------
|
||
|
|
|
||
|
|
Call RuntimeA2a::call_of(A2aTarget target, A2aProtocol protocol, A2aOp op) {
|
||
|
|
int family = static_cast<int>(target) * 2 + static_cast<int>(protocol);
|
||
|
|
return static_cast<Call>(static_cast<int>(Call::kA2aEngineCard) + family * 10 +
|
||
|
|
static_cast<int>(op));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string RuntimeA2a::tail_of(const A2aRequest& r) {
|
||
|
|
std::string pre = r.protocol == A2aProtocol::kV1 ? "v1/" : "";
|
||
|
|
switch (r.op) {
|
||
|
|
case A2aOp::kCard: return pre + "card";
|
||
|
|
case A2aOp::kExtendedAgentCard: return pre + "extendedAgentCard";
|
||
|
|
case A2aOp::kTasks: return pre + "tasks";
|
||
|
|
case A2aOp::kTaskGet:
|
||
|
|
case A2aOp::kTaskCancel:
|
||
|
|
case A2aOp::kTaskSubscribe: return pre + "tasks/" + r.task_id;
|
||
|
|
case A2aOp::kPushConfigs: return pre + "tasks/" + r.task_id + "/pushNotificationConfigs";
|
||
|
|
case A2aOp::kPushConfigGet:
|
||
|
|
return pre + "tasks/" + r.task_id + "/pushNotificationConfigs/" + r.config_id;
|
||
|
|
case A2aOp::kMessageSend:
|
||
|
|
case A2aOp::kMessageStream: return pre + "message";
|
||
|
|
}
|
||
|
|
throw GoogleError::invalid("unknown A2A op");
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string RuntimeA2a::path_of(const A2aRequest& r) const {
|
||
|
|
std::string target = engine_name(r.engine_id);
|
||
|
|
if (r.target == A2aTarget::kRevision) {
|
||
|
|
check_segment(r.revision_id, "revision id");
|
||
|
|
target += "/runtimeRevisions/" + r.revision_id;
|
||
|
|
} else if (!r.revision_id.empty()) {
|
||
|
|
throw GoogleError::invalid("revision id given for an engine target");
|
||
|
|
}
|
||
|
|
bool needs_task = r.op == A2aOp::kTaskGet || r.op == A2aOp::kTaskCancel ||
|
||
|
|
r.op == A2aOp::kTaskSubscribe || r.op == A2aOp::kPushConfigs ||
|
||
|
|
r.op == A2aOp::kPushConfigGet;
|
||
|
|
if (needs_task) check_segment(r.task_id, "task id");
|
||
|
|
else if (!r.task_id.empty()) throw GoogleError::invalid("task id given for an op without one");
|
||
|
|
if (r.op == A2aOp::kPushConfigGet) check_segment(r.config_id, "config id");
|
||
|
|
else if (!r.config_id.empty()) throw GoogleError::invalid("config id given for an op without one");
|
||
|
|
if (r.history_length < 0 || r.history_length > A2aCaps::kMaxHistoryLength)
|
||
|
|
throw GoogleError::invalid("historyLength outside 0.." +
|
||
|
|
std::to_string(A2aCaps::kMaxHistoryLength));
|
||
|
|
if (r.history_length > 0 && (!r.is_get() || r.op == A2aOp::kTaskSubscribe))
|
||
|
|
throw GoogleError::invalid("historyLength applies to GET ops without subscribe");
|
||
|
|
std::string path = render(call_of(r.target, r.protocol, r.op), target, tail_of(r));
|
||
|
|
if (r.history_length > 0) path += "?historyLength=" + std::to_string(r.history_length);
|
||
|
|
return path;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json RuntimeA2a::call(const A2aRequest& r) const {
|
||
|
|
if (r.is_stream() || r.op == A2aOp::kTaskSubscribe)
|
||
|
|
throw GoogleError::invalid("use stream() or subscribe() for this op");
|
||
|
|
std::string path = path_of(r);
|
||
|
|
const Json* body = nullptr;
|
||
|
|
if (!r.is_get()) {
|
||
|
|
if (!r.body.is_object()) throw GoogleError::invalid("body must be an object");
|
||
|
|
check_size(r.body, "body");
|
||
|
|
body = &r.body;
|
||
|
|
}
|
||
|
|
Call c = call_of(r.target, r.protocol, r.op);
|
||
|
|
return client_->request(method_of(c), endpoint_of(c, context_.location, true), path, body);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t RuntimeA2a::stream(const A2aRequest& r, const GoogleClient::EventSink& sink) const {
|
||
|
|
if (!r.is_stream()) throw GoogleError::invalid("stream() takes the message stream op");
|
||
|
|
std::string path = path_of(r);
|
||
|
|
if (!r.body.is_object()) throw GoogleError::invalid("body must be an object");
|
||
|
|
check_size(r.body, "body");
|
||
|
|
Call c = call_of(r.target, r.protocol, r.op);
|
||
|
|
std::size_t n = 0;
|
||
|
|
client_->stream_json(endpoint_of(c, context_.location, true), path, r.body,
|
||
|
|
[&](const Json& e) {
|
||
|
|
++n;
|
||
|
|
return sink(e) && n < A2aCaps::kMaxStreamEvents;
|
||
|
|
});
|
||
|
|
return n;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json RuntimeA2a::subscribe(const A2aRequest& r) const {
|
||
|
|
if (r.op != A2aOp::kTaskSubscribe) throw GoogleError::invalid("subscribe() takes the subscribe op");
|
||
|
|
Call c = call_of(r.target, r.protocol, r.op);
|
||
|
|
return client_->request(method_of(c), endpoint_of(c, context_.location, true), path_of(r));
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- tasks and records ---------------------------------------------------------
|
||
|
|
|
||
|
|
std::vector<A2aTaskInfo> RuntimeA2a::list_items(Call call, const std::string& parent,
|
||
|
|
const std::string& key,
|
||
|
|
const std::string& filter,
|
||
|
|
const std::string& order_by) const {
|
||
|
|
check_filter(filter, order_by);
|
||
|
|
std::string path = render(call, parent) + "?pageSize=50";
|
||
|
|
if (!filter.empty()) path += "&filter=" + enc(filter);
|
||
|
|
if (!order_by.empty()) path += "&orderBy=" + enc(order_by);
|
||
|
|
Json items = list_all(*client_, endpoint_of(call, context_.location, true), path, key,
|
||
|
|
A2aCaps::kMaxListPages);
|
||
|
|
std::vector<A2aTaskInfo> out;
|
||
|
|
for (const Json& j : items.as_array()) {
|
||
|
|
if (out.size() >= A2aCaps::kMaxItemsReturned) break;
|
||
|
|
out.push_back(A2aTaskInfo::from_json(j));
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
A2aTaskInfo RuntimeA2a::create_task(const std::string& engine_id, const std::string& task_id,
|
||
|
|
const A2aTaskInfo& task) const {
|
||
|
|
check_task_id(task_id);
|
||
|
|
Json body = task.to_json();
|
||
|
|
return A2aTaskInfo::from_json(send(Call::kA2aTaskCreate, engine_name(engine_id),
|
||
|
|
"?a2aTaskId=" + enc(task_id), &body));
|
||
|
|
}
|
||
|
|
|
||
|
|
A2aTaskInfo RuntimeA2a::get_task(const std::string& engine_id, const std::string& task_id,
|
||
|
|
const std::string& view) const {
|
||
|
|
std::string q;
|
||
|
|
if (!view.empty()) {
|
||
|
|
if (view.size() > 64) throw GoogleError::invalid("bad view");
|
||
|
|
for (char c : view)
|
||
|
|
if (!((c >= 'A' && c <= 'Z') || c == '_')) throw GoogleError::invalid("bad view");
|
||
|
|
q = "?view=" + view;
|
||
|
|
}
|
||
|
|
return A2aTaskInfo::from_json(send(Call::kA2aTaskGet, task_name(engine_id, task_id), q));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<A2aTaskInfo> RuntimeA2a::list_tasks(const std::string& engine_id,
|
||
|
|
const std::string& filter,
|
||
|
|
const std::string& order_by) const {
|
||
|
|
return list_items(Call::kA2aTaskList, engine_name(engine_id), "a2aTasks", filter, order_by);
|
||
|
|
}
|
||
|
|
|
||
|
|
void RuntimeA2a::delete_task(const std::string& engine_id, const std::string& task_id,
|
||
|
|
bool confirm) const {
|
||
|
|
if (!confirm) throw GoogleError::invalid("deleting a task needs confirm=true");
|
||
|
|
send(Call::kA2aTaskDelete, task_name(engine_id, task_id));
|
||
|
|
}
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
Json append_body(const Json& events, const std::string& generation) {
|
||
|
|
if (!events.is_array() || events.as_array().empty() || events.as_array().size() > 100)
|
||
|
|
throw GoogleError::invalid("events must be an array of 1..100 items");
|
||
|
|
check_size(events, "events");
|
||
|
|
if (generation.size() > 32) throw GoogleError::invalid("generation too long");
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("events", events);
|
||
|
|
if (!generation.empty()) body.set("generation", Json(generation));
|
||
|
|
return body;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
Json RuntimeA2a::append_task_events(const std::string& engine_id, const std::string& task_id,
|
||
|
|
const Json& events, const std::string& generation) const {
|
||
|
|
Json body = append_body(events, generation);
|
||
|
|
return send(Call::kA2aTaskAppendEvents, task_name(engine_id, task_id), "", &body);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json RuntimeA2a::list_task_events(const std::string& engine_id, const std::string& task_id,
|
||
|
|
const std::string& filter) const {
|
||
|
|
check_filter(filter, "");
|
||
|
|
return send(Call::kA2aTaskEventsList, task_name(engine_id, task_id),
|
||
|
|
"?pageSize=100" + (filter.empty() ? std::string() : "&filter=" + enc(filter)));
|
||
|
|
}
|
||
|
|
|
||
|
|
A2aTaskInfo RuntimeA2a::create_record(const std::string& store_id, const std::string& record_id,
|
||
|
|
const A2aTaskInfo& record) const {
|
||
|
|
std::string q;
|
||
|
|
if (!record_id.empty()) {
|
||
|
|
check_segment(record_id, "record id");
|
||
|
|
q = "?a2aTaskRecordId=" + enc(record_id);
|
||
|
|
}
|
||
|
|
Json body = record.to_json();
|
||
|
|
return A2aTaskInfo::from_json(send(Call::kTaskRecordCreate, store_name(store_id), q, &body));
|
||
|
|
}
|
||
|
|
|
||
|
|
A2aTaskInfo RuntimeA2a::get_record(const std::string& store_id, const std::string& record_id,
|
||
|
|
const std::string& view) const {
|
||
|
|
std::string q;
|
||
|
|
if (!view.empty()) {
|
||
|
|
if (view.size() > 64) throw GoogleError::invalid("bad view");
|
||
|
|
for (char c : view)
|
||
|
|
if (!((c >= 'A' && c <= 'Z') || c == '_')) throw GoogleError::invalid("bad view");
|
||
|
|
q = "?view=" + view;
|
||
|
|
}
|
||
|
|
return A2aTaskInfo::from_json(send(Call::kTaskRecordGet, record_name(store_id, record_id), q));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<A2aTaskInfo> RuntimeA2a::list_records(const std::string& store_id,
|
||
|
|
const std::string& filter,
|
||
|
|
const std::string& order_by) const {
|
||
|
|
return list_items(Call::kTaskRecordList, store_name(store_id), "a2aTaskRecords", filter,
|
||
|
|
order_by);
|
||
|
|
}
|
||
|
|
|
||
|
|
A2aTaskInfo RuntimeA2a::delete_record(const std::string& store_id, const std::string& record_id,
|
||
|
|
bool confirm) const {
|
||
|
|
if (!confirm) throw GoogleError::invalid("deleting a task record needs confirm=true");
|
||
|
|
return A2aTaskInfo::from_json(send(Call::kTaskRecordDelete, record_name(store_id, record_id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json RuntimeA2a::append_record_events(const std::string& store_id, const std::string& record_id,
|
||
|
|
const Json& events, const std::string& generation) const {
|
||
|
|
Json body = append_body(events, generation);
|
||
|
|
return send(Call::kTaskRecordAppendEvents, record_name(store_id, record_id), "", &body);
|
||
|
|
}
|
||
|
|
|
||
|
|
Json RuntimeA2a::list_record_events(const std::string& store_id, const std::string& record_id,
|
||
|
|
const std::string& filter) const {
|
||
|
|
check_filter(filter, "");
|
||
|
|
return send(Call::kTaskRecordEventsList, record_name(store_id, record_id),
|
||
|
|
"?pageSize=100" + (filter.empty() ? std::string() : "&filter=" + enc(filter)));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::runtime
|