feat(clients): adk+live: close gaps (YAML config import, VertexModel only, MCP tools/call and A2A card live, AP2 v0.2, live features verified, wolfSSL timeout/close status and MSG_NOSIGNAL)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,6 +28,7 @@ struct ServerConn {
|
||||
int fd = -1;
|
||||
FrameDecoder decoder{8u * 1024u * 1024u, true};
|
||||
std::string request; // handshake request text
|
||||
int index = 0; // 0-based connection number
|
||||
|
||||
void send_raw(const std::string& bytes) const {
|
||||
std::size_t sent = 0;
|
||||
@@ -78,8 +79,10 @@ class LoopbackServer {
|
||||
|
||||
enum class Handshake { Ok, BadAccept, Status403, CloseImmediately };
|
||||
|
||||
explicit LoopbackServer(Script script, Handshake mode = Handshake::Ok)
|
||||
: script_(std::move(script)), mode_(mode) {
|
||||
// connections > 1 serves that many sequential clients (mode Ok only); the
|
||||
// script sees the 0-based connection number in conn.index.
|
||||
explicit LoopbackServer(Script script, Handshake mode = Handshake::Ok, int connections = 1)
|
||||
: script_(std::move(script)), mode_(mode), connections_(connections) {
|
||||
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
int one = 1;
|
||||
::setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
|
||||
@@ -107,19 +110,25 @@ class LoopbackServer {
|
||||
|
||||
private:
|
||||
void run() {
|
||||
for (int i = 0; i < connections_; ++i)
|
||||
if (!serve_one(i)) return;
|
||||
}
|
||||
|
||||
bool serve_one(int index) {
|
||||
timeval tv{2, 0};
|
||||
int fd = ::accept(listen_fd_, nullptr, nullptr);
|
||||
if (fd < 0) return;
|
||||
if (fd < 0) return false;
|
||||
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
|
||||
ServerConn conn;
|
||||
conn.fd = fd;
|
||||
conn.index = index;
|
||||
std::string head;
|
||||
char buf[2048];
|
||||
while (head.find("\r\n\r\n") == std::string::npos) {
|
||||
ssize_t n = ::recv(fd, buf, sizeof buf, 0);
|
||||
if (n <= 0) {
|
||||
::close(fd);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
head.append(buf, (std::size_t)n);
|
||||
}
|
||||
@@ -132,12 +141,12 @@ class LoopbackServer {
|
||||
std::string key = head.substr(k + 19, head.find("\r\n", k) - k - 19);
|
||||
if (mode_ == Handshake::CloseImmediately) {
|
||||
::close(fd);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (mode_ == Handshake::Status403) {
|
||||
conn.send_raw("HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n");
|
||||
::close(fd);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
std::string accept = WsHandshake::accept_for(key);
|
||||
if (mode_ == Handshake::BadAccept) accept = "AAAAAAAAAAAAAAAAAAAAAAAAAAA=";
|
||||
@@ -150,10 +159,12 @@ class LoopbackServer {
|
||||
}
|
||||
}
|
||||
::close(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
Script script_;
|
||||
Handshake mode_;
|
||||
int connections_ = 1;
|
||||
int listen_fd_ = -1;
|
||||
int port_ = 0;
|
||||
std::thread thread_;
|
||||
|
||||
@@ -157,6 +157,96 @@ TEST(live_config, setup_matches_spec_names) {
|
||||
ASSERT_EQ(copy.find("setup")->find("model")->as_string(), "models/" + c.model);
|
||||
}
|
||||
|
||||
TEST(live_config, vad_compression_affective_and_media_options) {
|
||||
LiveConfig c;
|
||||
LiveActivityDetection v;
|
||||
v.start_sensitivity = "START_SENSITIVITY_LOW";
|
||||
v.end_sensitivity = "END_SENSITIVITY_HIGH";
|
||||
v.prefix_padding_ms = 100;
|
||||
v.silence_duration_ms = 800;
|
||||
c.activity_detection = v;
|
||||
c.activity_handling = "NO_INTERRUPTION";
|
||||
c.turn_coverage = "TURN_INCLUDES_ONLY_ACTIVITY";
|
||||
c.affective_dialog = true;
|
||||
c.media_resolution = "MEDIA_RESOLUTION_LOW";
|
||||
c.context_compression = true;
|
||||
c.compression_trigger_tokens = 20000;
|
||||
c.compression_target_tokens = 10000;
|
||||
Json j = c.to_setup(LiveEndpoint::api_key("k"));
|
||||
const Json& s = *j.find("setup");
|
||||
const Json& rt = *s.find("realtimeInputConfig");
|
||||
ASSERT_EQ(rt.find("automaticActivityDetection")->find("silenceDurationMs")->as_int(), 800);
|
||||
ASSERT_EQ(rt.find("activityHandling")->as_string(), "NO_INTERRUPTION");
|
||||
ASSERT_TRUE(s.find("generationConfig")->find("enableAffectiveDialog")->as_bool());
|
||||
ASSERT_EQ(s.find("generationConfig")->find("mediaResolution")->as_string(), "MEDIA_RESOLUTION_LOW");
|
||||
// int64 fields travel as strings in the proto3 JSON mapping.
|
||||
ASSERT_EQ(s.find("contextWindowCompression")->find("triggerTokens")->as_string(), "20000");
|
||||
ASSERT_EQ(s.find("contextWindowCompression")->find("slidingWindow")->find("targetTokens")->as_string(), "10000");
|
||||
auto fields = spec_fields();
|
||||
std::set<std::string> keys;
|
||||
collect_keys(j, keys);
|
||||
for (const auto& k : keys) ASSERT_TRUE(fields.count(k) == 1 || k == "setup");
|
||||
|
||||
auto ep = LiveEndpoint::api_key("k");
|
||||
auto bad = [&](auto mutate) {
|
||||
LiveConfig b;
|
||||
mutate(b);
|
||||
ASSERT_THROW(b.to_setup(ep), GoogleError);
|
||||
};
|
||||
bad([](LiveConfig& b) { LiveActivityDetection d; d.start_sensitivity = "HIGH"; b.activity_detection = d; });
|
||||
bad([](LiveConfig& b) { LiveActivityDetection d; d.end_sensitivity = "x"; b.activity_detection = d; });
|
||||
bad([](LiveConfig& b) { LiveActivityDetection d; d.silence_duration_ms = 10001; b.activity_detection = d; });
|
||||
bad([](LiveConfig& b) { LiveActivityDetection d; d.prefix_padding_ms = -5; b.activity_detection = d; });
|
||||
bad([](LiveConfig& b) { b.activity_handling = "ALWAYS"; });
|
||||
bad([](LiveConfig& b) { b.turn_coverage = "ALL"; });
|
||||
bad([](LiveConfig& b) { b.media_resolution = "HUGE"; });
|
||||
bad([](LiveConfig& b) { b.context_compression = true; b.compression_trigger_tokens = -1; });
|
||||
}
|
||||
|
||||
TEST(live_session, resume_needs_a_handle_and_resumption_enabled) {
|
||||
// Failures before any socket: not connected, resumption off.
|
||||
LiveSession s(LiveEndpoint::api_key("k"), LiveConfig{});
|
||||
ASSERT_THROW(s.resume(), GoogleError);
|
||||
}
|
||||
|
||||
TEST(live_session, resume_reconnects_with_the_latest_handle) {
|
||||
std::vector<std::string> setups(2);
|
||||
LoopbackServer server([&](ServerConn& c) {
|
||||
setups[(std::size_t)c.index] = c.read_message().value_or("");
|
||||
c.send_text("{\"setupComplete\":{}}");
|
||||
if (c.index == 0) {
|
||||
c.send_text("{\"sessionResumptionUpdate\":{\"newHandle\":\"h-1\",\"resumable\":true}}");
|
||||
c.send_text("{\"goAway\":{\"timeLeft\":\"57s\"}}");
|
||||
c.read_frame(); // the client's close during resume()
|
||||
c.send_close(1000);
|
||||
} else {
|
||||
c.send_text("{\"serverContent\":{\"modelTurn\":{\"parts\":[{\"text\":\"resumed\"}]}}}");
|
||||
c.send_text("{\"serverContent\":{\"turnComplete\":true}}");
|
||||
c.read_frame();
|
||||
c.send_close(1000);
|
||||
}
|
||||
}, LoopbackServer::Handshake::Ok, 2);
|
||||
LiveConfig cfg;
|
||||
cfg.session_resumption = true;
|
||||
LiveSession s(local_endpoint(server), cfg);
|
||||
s.connect();
|
||||
auto first = until(s, LiveEventKind::GoAway);
|
||||
ASSERT_TRUE(first.back().kind == LiveEventKind::GoAway);
|
||||
ASSERT_TRUE(first.back().seconds == 57.0);
|
||||
for (int i = 0; i < 50 && s.resumption_handle().empty(); ++i) s.next_event(50);
|
||||
ASSERT_EQ(s.resumption_handle(), "h-1");
|
||||
s.resume();
|
||||
ASSERT_TRUE(s.open());
|
||||
auto second = until(s, LiveEventKind::TurnComplete);
|
||||
ASSERT_TRUE(second.back().kind == LiveEventKind::TurnComplete);
|
||||
// Sequence numbers carry on across the reconnect.
|
||||
for (std::size_t i = 1; i < second.size(); ++i) ASSERT_TRUE(second[i].seq > second[i - 1].seq);
|
||||
s.close();
|
||||
Json setup1 = Json::parse(setups[1]);
|
||||
ASSERT_EQ(setup1.find("setup")->find("sessionResumption")->find("handle")->as_string(), "h-1");
|
||||
ASSERT_TRUE(Json::parse(setups[0]).find("setup")->find("sessionResumption")->find("handle") == nullptr);
|
||||
}
|
||||
|
||||
TEST(live_config, validation) {
|
||||
auto ep = LiveEndpoint::api_key("k");
|
||||
auto bad = [&](auto mutate) {
|
||||
|
||||
@@ -0,0 +1,375 @@
|
||||
// Opt-in real network checks of the Live API features beyond a plain text
|
||||
// turn: tool calls (toolCall / toolResponse round trip), clientContent turns,
|
||||
// input and output transcription, session resumption (handle + reconnect),
|
||||
// video input frames, goAway. MOLO_LIVE_NET=1 plus GOOGLE_CLOUD_PROJECT and
|
||||
// GOOGLE_API_KEY (express key, header) and/or GOOGLE_ACCESS_TOKEN (OAuth
|
||||
// bearer); each test runs once per credential that is set. Messages are tiny.
|
||||
// Credentials are never printed. Loud SKIP otherwise. The goAway check waits
|
||||
// for the server's connection lifetime and needs MOLO_LIVE_SLOW=1 as well.
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "framework.h"
|
||||
#include "google/errors.h"
|
||||
#include "google/live/live_session.h"
|
||||
#include "security/crypto/Base64.h"
|
||||
|
||||
using namespace molo::google;
|
||||
using namespace molo::google::live;
|
||||
using molo::clients::body::Json;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Auth {
|
||||
std::string label;
|
||||
LiveEndpoint endpoint;
|
||||
};
|
||||
|
||||
// Empty (with a loud SKIP) unless the opt-in env is complete.
|
||||
std::vector<Auth> auths(const char* test) {
|
||||
std::vector<Auth> out;
|
||||
const char* on = std::getenv("MOLO_LIVE_NET");
|
||||
const char* project = std::getenv("GOOGLE_CLOUD_PROJECT");
|
||||
if (!on || std::string(on) != "1" || !project) {
|
||||
std::printf(" SKIP %s: set MOLO_LIVE_NET=1, GOOGLE_CLOUD_PROJECT and GOOGLE_API_KEY / "
|
||||
"GOOGLE_ACCESS_TOKEN\n", test);
|
||||
return out;
|
||||
}
|
||||
CloudContext ctx = CloudContext::from_env();
|
||||
if (const char* k = std::getenv("GOOGLE_API_KEY"))
|
||||
out.push_back({"express", LiveEndpoint::vertex_express(ctx, k)});
|
||||
if (const char* t = std::getenv("GOOGLE_ACCESS_TOKEN"))
|
||||
out.push_back({"oauth", LiveEndpoint::vertex(ctx, t)});
|
||||
if (out.empty()) std::printf(" SKIP %s: no credential in the environment\n", test);
|
||||
return out;
|
||||
}
|
||||
|
||||
using Pred = std::function<bool(const LiveEvent&)>;
|
||||
|
||||
// Reads events until `stop` accepts one (inclusive), the session ends or the
|
||||
// timeout passes. Returns everything seen.
|
||||
std::vector<LiveEvent> pump(LiveSession& s, const Pred& stop, int ms = 30000) {
|
||||
std::vector<LiveEvent> seen;
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
auto e = s.next_event(200);
|
||||
if (!e) {
|
||||
if (!s.open() && s.pending() == 0) break;
|
||||
continue;
|
||||
}
|
||||
seen.push_back(*e);
|
||||
if (stop(*e)) break;
|
||||
}
|
||||
return seen;
|
||||
}
|
||||
|
||||
bool is(const LiveEvent& e, LiveEventKind k) { return e.kind == k; }
|
||||
bool turn_done(const LiveEvent& e) { return is(e, LiveEventKind::TurnComplete); }
|
||||
|
||||
std::string transcript(const std::vector<LiveEvent>& evs, LiveEventKind kind) {
|
||||
std::string out;
|
||||
for (const auto& e : evs)
|
||||
if (e.kind == kind) out += e.text;
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string audio_of(const std::vector<LiveEvent>& evs) {
|
||||
std::string out;
|
||||
for (const auto& e : evs)
|
||||
if (e.kind == LiveEventKind::Audio) out += e.data;
|
||||
return out;
|
||||
}
|
||||
|
||||
// 24 kHz 16-bit mono PCM (model output) to 16 kHz (model input), linear.
|
||||
std::string resample_24k_to_16k(const std::string& pcm) {
|
||||
std::size_t n = pcm.size() / 2;
|
||||
auto at = [&](std::size_t i) {
|
||||
return static_cast<int16_t>(static_cast<uint8_t>(pcm[2 * i]) |
|
||||
(static_cast<uint8_t>(pcm[2 * i + 1]) << 8));
|
||||
};
|
||||
std::string out;
|
||||
for (std::size_t j = 0; j * 3 / 2 + 1 < n; ++j) {
|
||||
double pos = j * 1.5;
|
||||
std::size_t i = static_cast<std::size_t>(pos);
|
||||
double f = pos - static_cast<double>(i);
|
||||
int16_t v = static_cast<int16_t>(at(i) * (1.0 - f) + at(i + 1) * f);
|
||||
out.push_back(static_cast<char>(v & 0xFF));
|
||||
out.push_back(static_cast<char>((v >> 8) & 0xFF));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string kinds(const std::vector<LiveEvent>& evs) {
|
||||
std::string out;
|
||||
for (const auto& e : evs) {
|
||||
if (!out.empty()) out += ",";
|
||||
out += live_event_name(e.kind);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void show(const std::string& label, const char* what, const std::string& text) {
|
||||
std::printf(" [%s] %s: %s\n", label.c_str(), what, text.substr(0, 120).c_str());
|
||||
}
|
||||
|
||||
LiveConfig base_config() {
|
||||
LiveConfig cfg;
|
||||
cfg.output_transcription = true;
|
||||
return cfg;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(live_net, tool_call_round_trip_and_client_content_turn) {
|
||||
for (auto& a : auths("live_net.tool_call_round_trip_and_client_content_turn")) {
|
||||
LiveConfig cfg = base_config();
|
||||
cfg.system_instruction =
|
||||
"For any temperature question call get_temperature. After the tool result answer in one short sentence.";
|
||||
LiveFunctionDeclaration fn;
|
||||
fn.name = "get_temperature";
|
||||
fn.description = "Current temperature in Celsius for a city";
|
||||
fn.parameters = Json::parse(
|
||||
"{\"type\":\"OBJECT\",\"properties\":{\"city\":{\"type\":\"STRING\"}},\"required\":[\"city\"]}");
|
||||
cfg.functions.push_back(fn);
|
||||
LiveSession s(a.endpoint, cfg);
|
||||
s.connect();
|
||||
s.send_turn("What is the temperature in Utrecht right now?", true); // clientContent
|
||||
auto evs = pump(s, [](const LiveEvent& e) { return is(e, LiveEventKind::ToolCall) || turn_done(e); });
|
||||
ASSERT_TRUE(!evs.empty() && evs.back().kind == LiveEventKind::ToolCall);
|
||||
const LiveEvent& call = evs.back();
|
||||
ASSERT_EQ(call.calls.size(), 1u);
|
||||
ASSERT_EQ(call.calls[0].name, "get_temperature");
|
||||
ASSERT_TRUE(call.calls[0].args.is_object() && call.calls[0].args.has("city"));
|
||||
show(a.label, "toolCall", call.calls[0].name + " id=" + (call.calls[0].id.empty() ? "(none)" : "set") +
|
||||
" args=" + call.calls[0].args.dump());
|
||||
Json resp = Json::object();
|
||||
resp.set("temp_c", Json(17));
|
||||
s.send_tool_responses({{call.calls[0].id, call.calls[0].name, resp}});
|
||||
// The turn that carried the toolCall ends first; the answer follows it.
|
||||
bool answered = false;
|
||||
auto after = pump(s, [&](const LiveEvent& e) {
|
||||
answered = answered || is(e, LiveEventKind::Audio) ||
|
||||
(is(e, LiveEventKind::OutputTranscription) && !e.text.empty());
|
||||
return answered && turn_done(e);
|
||||
});
|
||||
show(a.label, "kinds after toolResponse", kinds(after).substr(0, 60));
|
||||
ASSERT_TRUE(!after.empty() && after.back().kind == LiveEventKind::TurnComplete);
|
||||
std::string said = transcript(after, LiveEventKind::OutputTranscription);
|
||||
show(a.label, "answer after toolResponse", said);
|
||||
ASSERT_TRUE(!said.empty() || !audio_of(after).empty());
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, input_transcription_from_audio_and_audio_stream_end) {
|
||||
for (auto& a : auths("live_net.input_transcription_from_audio_and_audio_stream_end")) {
|
||||
// Speech to feed: the model's own audio for a fixed sentence.
|
||||
std::string speech;
|
||||
{
|
||||
LiveSession tts(a.endpoint, base_config());
|
||||
tts.connect();
|
||||
tts.send_text("Say exactly these words and nothing else: the blue moon rises.");
|
||||
auto evs = pump(tts, turn_done);
|
||||
speech = resample_24k_to_16k(audio_of(evs));
|
||||
tts.close();
|
||||
}
|
||||
ASSERT_TRUE(speech.size() > 8000);
|
||||
LiveConfig cfg = base_config();
|
||||
cfg.input_transcription = true;
|
||||
LiveSession s(a.endpoint, cfg);
|
||||
s.connect();
|
||||
const std::size_t chunk = 8000; // 250 ms
|
||||
for (std::size_t off = 0; off < speech.size(); off += chunk)
|
||||
s.send_audio(speech.substr(off, chunk));
|
||||
s.send_audio(std::string(32000, '\0')); // 1 s of silence so the server's VAD ends the utterance
|
||||
s.end_audio(); // audioStreamEnd
|
||||
auto evs = pump(s, turn_done);
|
||||
std::printf(" speech bytes=%zu received=%llu\n", speech.size(), (unsigned long long)s.stats().received);
|
||||
show(a.label, "kinds", kinds(evs));
|
||||
std::string heard = transcript(evs, LiveEventKind::InputTranscription);
|
||||
show(a.label, "inputTranscription", heard);
|
||||
ASSERT_TRUE(!heard.empty());
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, session_resumption_handle_and_reconnect) {
|
||||
for (auto& a : auths("live_net.session_resumption_handle_and_reconnect")) {
|
||||
std::string handle;
|
||||
{
|
||||
LiveConfig cfg = base_config();
|
||||
cfg.session_resumption = true;
|
||||
LiveSession s(a.endpoint, cfg);
|
||||
s.connect();
|
||||
s.send_turn("Remember this secret word: pineapple. Reply with just OK.", true);
|
||||
auto evs = pump(s, turn_done);
|
||||
ASSERT_TRUE(!evs.empty() && evs.back().kind == LiveEventKind::TurnComplete);
|
||||
// The handle arrives in sessionResumptionUpdate, around the turn end.
|
||||
for (int i = 0; i < 40 && s.resumption_handle().empty(); ++i) {
|
||||
auto e = s.next_event(250);
|
||||
(void)e;
|
||||
}
|
||||
handle = s.resumption_handle();
|
||||
std::printf(" [%s] resumption handle %s (%zu chars)\n", a.label.c_str(),
|
||||
handle.empty() ? "NOT received" : "received", handle.size());
|
||||
s.close();
|
||||
}
|
||||
ASSERT_TRUE(!handle.empty());
|
||||
LiveConfig cfg = base_config();
|
||||
cfg.session_resumption = true;
|
||||
cfg.resumption_handle = handle;
|
||||
LiveSession r(a.endpoint, cfg);
|
||||
r.connect(); // setupComplete on the resumed session
|
||||
r.send_turn("What was the secret word? Answer with the word only.", true);
|
||||
auto evs = pump(r, turn_done);
|
||||
std::string said = transcript(evs, LiveEventKind::OutputTranscription);
|
||||
show(a.label, "resumed answer", said);
|
||||
ASSERT_TRUE(!evs.empty() && evs.back().kind == LiveEventKind::TurnComplete);
|
||||
r.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, new_input_while_the_model_speaks_interrupts) {
|
||||
for (auto& a : auths("live_net.new_input_while_the_model_speaks_interrupts")) {
|
||||
LiveSession s(a.endpoint, base_config());
|
||||
s.connect();
|
||||
s.send_text("Count slowly from one to thirty, saying every number.");
|
||||
auto first = pump(s, [](const LiveEvent& e) { return is(e, LiveEventKind::Audio); });
|
||||
ASSERT_TRUE(!first.empty() && first.back().kind == LiveEventKind::Audio);
|
||||
s.send_text("Stop. Say only: stopped.");
|
||||
bool interrupted = false;
|
||||
auto evs = pump(s, [&](const LiveEvent& e) {
|
||||
interrupted = interrupted || is(e, LiveEventKind::Interrupted);
|
||||
return interrupted && turn_done(e);
|
||||
}, 40000);
|
||||
show(a.label, "interrupted", interrupted ? "yes" : "no");
|
||||
ASSERT_TRUE(interrupted);
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, resume_on_the_same_session_object) {
|
||||
for (auto& a : auths("live_net.resume_on_the_same_session_object")) {
|
||||
LiveConfig cfg = base_config();
|
||||
cfg.session_resumption = true;
|
||||
LiveSession s(a.endpoint, cfg);
|
||||
s.connect();
|
||||
s.send_turn("Remember this secret word: mango. Reply with just OK.", true);
|
||||
pump(s, turn_done);
|
||||
for (int i = 0; i < 40 && s.resumption_handle().empty(); ++i) s.next_event(250);
|
||||
ASSERT_TRUE(!s.resumption_handle().empty());
|
||||
s.resume(); // new connection, same object, handle in setup
|
||||
ASSERT_TRUE(s.open());
|
||||
s.send_turn("What was the secret word? Answer with the word only.", true);
|
||||
bool answered = false;
|
||||
auto evs = pump(s, [&](const LiveEvent& e) {
|
||||
answered = answered || is(e, LiveEventKind::Audio) ||
|
||||
(is(e, LiveEventKind::OutputTranscription) && !e.text.empty());
|
||||
return answered && turn_done(e);
|
||||
});
|
||||
std::string said = transcript(evs, LiveEventKind::OutputTranscription);
|
||||
show(a.label, "answer after resume()", said);
|
||||
ASSERT_TRUE(answered);
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, setup_options_vad_compression_media_resolution_are_accepted) {
|
||||
for (auto& a : auths("live_net.setup_options_vad_compression_media_resolution_are_accepted")) {
|
||||
LiveConfig cfg = base_config();
|
||||
LiveActivityDetection vad;
|
||||
vad.start_sensitivity = "START_SENSITIVITY_LOW";
|
||||
vad.end_sensitivity = "END_SENSITIVITY_LOW";
|
||||
vad.prefix_padding_ms = 100;
|
||||
vad.silence_duration_ms = 700;
|
||||
cfg.activity_detection = vad;
|
||||
cfg.activity_handling = "START_OF_ACTIVITY_INTERRUPTS";
|
||||
cfg.media_resolution = "MEDIA_RESOLUTION_LOW";
|
||||
cfg.context_compression = true;
|
||||
cfg.compression_trigger_tokens = 20000;
|
||||
cfg.compression_target_tokens = 10000;
|
||||
LiveSession s(a.endpoint, cfg);
|
||||
s.connect(); // a rejected option closes the socket before setupComplete
|
||||
s.send_turn("Say OK.", true);
|
||||
auto evs = pump(s, turn_done);
|
||||
show(a.label, "kinds", kinds(evs).substr(0, 60));
|
||||
ASSERT_TRUE(!evs.empty() && evs.back().kind == LiveEventKind::TurnComplete);
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, video_input_frame) {
|
||||
static const char* kJpegB64 =
|
||||
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAEAAQADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDz+iiivmD9yCiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/Z";
|
||||
std::string jpeg;
|
||||
ASSERT_TRUE(molo::security::crypto::Base64::decode(kJpegB64, jpeg));
|
||||
for (auto& a : auths("live_net.video_input_frame")) {
|
||||
LiveSession s(a.endpoint, base_config());
|
||||
s.connect();
|
||||
for (int i = 0; i < 4; ++i) { // a short stream of 256x256 frames (a 64x64 frame was not seen by the model)
|
||||
s.send_video(jpeg, "image/jpeg");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(700));
|
||||
}
|
||||
s.send_text("What color is the video frame I am streaming? Answer with one word.");
|
||||
// The model sometimes closes the first turn empty when frames and the
|
||||
// question race; ask again once. Two attempts, then report.
|
||||
bool answered = false;
|
||||
std::vector<LiveEvent> evs;
|
||||
for (int attempt = 0; attempt < 2 && !answered; ++attempt) {
|
||||
if (attempt) s.send_text("Again: what color is the video frame? One word.");
|
||||
evs = pump(s, [&](const LiveEvent& e) {
|
||||
answered = answered || is(e, LiveEventKind::Audio) ||
|
||||
(is(e, LiveEventKind::OutputTranscription) && !e.text.empty());
|
||||
return answered && turn_done(e);
|
||||
}, 20000);
|
||||
}
|
||||
show(a.label, "answer about the frame", transcript(evs, LiveEventKind::OutputTranscription));
|
||||
for (const auto& e : evs) {
|
||||
ASSERT_TRUE(e.kind != LiveEventKind::Error);
|
||||
ASSERT_TRUE(e.kind != LiveEventKind::Closed); // frames were accepted, no server close
|
||||
}
|
||||
ASSERT_TRUE(answered);
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(live_net, go_away_before_the_connection_lifetime_ends) {
|
||||
const char* slow = std::getenv("MOLO_LIVE_SLOW");
|
||||
if (!slow || std::string(slow) != "1") {
|
||||
std::printf(" SKIP live_net.go_away_before_the_connection_lifetime_ends: set MOLO_LIVE_SLOW=1 "
|
||||
"(waits up to 14 minutes idle for the server's goAway)\n");
|
||||
return;
|
||||
}
|
||||
auto list = auths("live_net.go_away_before_the_connection_lifetime_ends");
|
||||
if (list.empty()) return;
|
||||
Auth& a = list.front();
|
||||
LiveConfig cfg = base_config();
|
||||
cfg.session_resumption = true;
|
||||
LiveOptions opt;
|
||||
opt.max_session_seconds = 15 * 60; // lift our own 30 min cap below the server's
|
||||
LiveSession s(a.endpoint, cfg, opt);
|
||||
auto t0 = std::chrono::steady_clock::now();
|
||||
s.connect();
|
||||
auto secs = [&] {
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - t0).count();
|
||||
};
|
||||
bool go_away = false;
|
||||
long go_away_at = -1;
|
||||
double left = -1;
|
||||
long closed_at = -1;
|
||||
int close_code = 0;
|
||||
auto evs = pump(s, [&](const LiveEvent& e) {
|
||||
if (e.kind == LiveEventKind::GoAway) { go_away = true; go_away_at = secs(); left = e.seconds; }
|
||||
if (e.kind == LiveEventKind::Closed) { closed_at = secs(); close_code = e.code; return true; }
|
||||
return false;
|
||||
}, 14 * 60 * 1000);
|
||||
std::printf(" [%s] goAway=%d at %lds timeLeft=%.1fs; closed at %lds code=%d; events=%zu\n",
|
||||
a.label.c_str(), go_away, go_away_at, left, closed_at, close_code, evs.size());
|
||||
s.close();
|
||||
ASSERT_TRUE(go_away || closed_at >= 0);
|
||||
}
|
||||
@@ -392,3 +392,43 @@ TEST(live_ws, tls_read_distinguishes_timeout_from_eof) {
|
||||
ASSERT_TRUE(m.has_value());
|
||||
ASSERT_EQ(m->code, (std::uint16_t)1006);
|
||||
}
|
||||
|
||||
namespace {
|
||||
// The TlsReadStatus contract of the wolfSSL session: a socket timeout throws
|
||||
// TlsTimeoutError, a closed peer returns 0 with last_read_status() kClosed.
|
||||
class ReportingTls : public PassthroughTls {
|
||||
public:
|
||||
std::size_t recv_some(char* out, std::size_t max) override {
|
||||
ssize_t r = ::recv(fd_, out, max, 0);
|
||||
if (r > 0) {
|
||||
status_ = molo::clients::transports::TlsReadStatus::kData;
|
||||
return (std::size_t)r;
|
||||
}
|
||||
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
||||
throw molo::clients::transports::TlsTimeoutError("read timed out");
|
||||
status_ = molo::clients::transports::TlsReadStatus::kClosed;
|
||||
return 0;
|
||||
}
|
||||
molo::clients::transports::TlsReadStatus last_read_status() const override { return status_; }
|
||||
molo::clients::transports::TlsReadStatus status_ = molo::clients::transports::TlsReadStatus::kUnknown;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST(live_ws, tls_reported_status_beats_the_elapsed_time_guess) {
|
||||
// The peer drops the connection 600 ms in, with a 1000 ms read timeout. The
|
||||
// old elapsed-time rule (at least half the timeout means timeout) called
|
||||
// that a timeout and kept waiting; the reported status says closed.
|
||||
LoopbackServer server([](ServerConn& c) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||
(void)c;
|
||||
});
|
||||
WsUrl u = WsUrl::parse("wss://127.0.0.1:" + std::to_string(server.port()) + "/ws");
|
||||
WsClient c(open_stream(u, 2000, 2000, [] { return std::make_shared<ReportingTls>(); }));
|
||||
c.handshake(u, {}, 2000);
|
||||
c.set_read_timeout_ms(1000);
|
||||
auto t0 = std::chrono::steady_clock::now();
|
||||
auto m = c.receive();
|
||||
ASSERT_TRUE(m.has_value());
|
||||
ASSERT_EQ(m->code, (std::uint16_t)1006);
|
||||
ASSERT_TRUE(std::chrono::steady_clock::now() - t0 < std::chrono::milliseconds(950));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user