129 lines
4.2 KiB
C++
129 lines
4.2 KiB
C++
#include "google/live/live_requests.h"
|
|
|
|
#include <cctype>
|
|
|
|
#include "google/errors.h"
|
|
#include "google/live/caps.h"
|
|
#include "security/crypto/Base64.h"
|
|
|
|
namespace molo::google::live {
|
|
|
|
using clients::body::Json;
|
|
using molo::security::crypto::Base64;
|
|
|
|
namespace {
|
|
|
|
void check_text(const std::string& t) {
|
|
if (t.empty()) throw GoogleError::invalid("text is empty");
|
|
if (t.size() > LiveCaps::kMaxTextChars)
|
|
throw GoogleError::invalid("text over " + std::to_string(LiveCaps::kMaxTextChars) + " bytes");
|
|
}
|
|
|
|
std::string blob_message(const char* field, const std::string& bytes,
|
|
const std::string& mime, std::size_t cap) {
|
|
if (bytes.empty()) throw GoogleError::invalid(std::string(field) + " chunk is empty");
|
|
if (bytes.size() > cap)
|
|
throw GoogleError::invalid(std::string(field) + " chunk over " + std::to_string(cap) + " bytes");
|
|
Json blob = Json::object();
|
|
blob.set("data", Base64::encode(bytes));
|
|
blob.set("mimeType", mime);
|
|
Json input = Json::object();
|
|
input.set(field, blob);
|
|
Json root = Json::object();
|
|
root.set("realtimeInput", input);
|
|
return root.dump();
|
|
}
|
|
|
|
bool valid_id(const std::string& s) {
|
|
if (s.empty() || s.size() > LiveCaps::kMaxIdChars) return false;
|
|
for (unsigned char c : s)
|
|
if (c <= 0x20 || c == 0x7f) return false;
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void LiveRequests::check_mime(const std::string& mime, const char* a, const char* b) {
|
|
if (mime.empty() || mime.size() > LiveCaps::kMaxMimeChars)
|
|
throw GoogleError::invalid("mime type empty or too long");
|
|
for (unsigned char c : mime)
|
|
if (!(std::isalnum(c) || c == '/' || c == ';' || c == '=' || c == '-' ||
|
|
c == '+' || c == '.' || c == '_'))
|
|
throw GoogleError::invalid("mime type has invalid characters");
|
|
bool ok = mime.rfind(a, 0) == 0 || (b && mime.rfind(b, 0) == 0);
|
|
if (!ok) throw GoogleError::invalid("mime type must start with " + std::string(a) +
|
|
(b ? std::string(" or ") + b : ""));
|
|
}
|
|
|
|
std::string LiveRequests::text(const std::string& t) {
|
|
check_text(t);
|
|
Json input = Json::object();
|
|
input.set("text", t);
|
|
Json root = Json::object();
|
|
root.set("realtimeInput", input);
|
|
return root.dump();
|
|
}
|
|
|
|
std::string LiveRequests::turn(const std::string& t, bool turn_complete) {
|
|
check_text(t);
|
|
Json part = Json::object();
|
|
part.set("text", t);
|
|
Json parts = Json::array();
|
|
parts.push(part);
|
|
Json turn = Json::object();
|
|
turn.set("role", "user");
|
|
turn.set("parts", parts);
|
|
Json turns = Json::array();
|
|
turns.push(turn);
|
|
Json cc = Json::object();
|
|
cc.set("turns", turns);
|
|
cc.set("turnComplete", turn_complete);
|
|
Json root = Json::object();
|
|
root.set("clientContent", cc);
|
|
return root.dump();
|
|
}
|
|
|
|
std::string LiveRequests::audio(const std::string& bytes, const std::string& mime) {
|
|
check_mime(mime, "audio/");
|
|
return blob_message("audio", bytes, mime, LiveCaps::kMaxAudioChunkBytes);
|
|
}
|
|
|
|
std::string LiveRequests::video(const std::string& bytes, const std::string& mime) {
|
|
check_mime(mime, "image/", "video/");
|
|
return blob_message("video", bytes, mime, LiveCaps::kMaxVideoChunkBytes);
|
|
}
|
|
|
|
std::string LiveRequests::audio_stream_end() {
|
|
Json input = Json::object();
|
|
input.set("audioStreamEnd", true);
|
|
Json root = Json::object();
|
|
root.set("realtimeInput", input);
|
|
return root.dump();
|
|
}
|
|
|
|
std::string LiveRequests::tool_responses(const std::vector<LiveToolResponse>& rs) {
|
|
if (rs.empty() || rs.size() > LiveCaps::kMaxFunctionResponses)
|
|
throw GoogleError::invalid("tool responses need 1.." +
|
|
std::to_string(LiveCaps::kMaxFunctionResponses) + " entries");
|
|
Json arr = Json::array();
|
|
for (const auto& r : rs) {
|
|
if (!valid_id(r.id)) throw GoogleError::invalid("tool response id is malformed");
|
|
if (r.name.empty() || r.name.size() > 64)
|
|
throw GoogleError::invalid("tool response name is malformed");
|
|
if (!r.response.is_object())
|
|
throw GoogleError::invalid("tool response must be a JSON object");
|
|
Json o = Json::object();
|
|
o.set("id", r.id);
|
|
o.set("name", r.name);
|
|
o.set("response", r.response);
|
|
arr.push(o);
|
|
}
|
|
Json tr = Json::object();
|
|
tr.set("functionResponses", arr);
|
|
Json root = Json::object();
|
|
root.set("toolResponse", tr);
|
|
return root.dump();
|
|
}
|
|
|
|
} // namespace molo::google::live
|