95 lines
4.0 KiB
C++
95 lines
4.0 KiB
C++
// Route: POST /voice/turn (one live-conversation turn: hear, think, speak)
|
|||
|
|
#include "support/GoogleRig.hpp"
|
||
|
|
|
||
|
|
#include "security/crypto/Base64.h"
|
||
|
|
|
||
|
|
using namespace molo::example::test;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
setenv("GOOGLE_GENAI_MODEL", "rig-model", 1);
|
||
|
|
GoogleRig rig;
|
||
|
|
rig.stt("wat is de hoofdstad van Nederland");
|
||
|
|
rig.gemini("Amsterdam.");
|
||
|
|
rig.tts("SPOKEN-ANSWER");
|
||
|
|
Fixture f(rig.client);
|
||
|
|
|
||
|
|
auto res = f.client->post("/voice/turn?cid=abc123&lang=nl-NL", "WEBM-BYTES",
|
||
|
|
"audio/webm;codecs=opus");
|
||
|
|
check(res.status == 200, "status 200");
|
||
|
|
check(contains(res.headers.get("Content-Type"), "application/json"), "json");
|
||
|
|
check(contains(res.body, "\"transcript\":\"wat is de hoofdstad van Nederland\""),
|
||
|
|
"transcript");
|
||
|
|
check(contains(res.body, "\"reply\":\"Amsterdam.\""), "reply text");
|
||
|
|
check(contains(res.body, "\"mime\":\"audio/mpeg\""), "mime");
|
||
|
|
check(contains(res.body, "\"audio_b64\":\"" +
|
||
|
|
molo::security::crypto::Base64::encode("SPOKEN-ANSWER") + "\""),
|
||
|
|
"spoken answer as base64");
|
||
|
|
check(rig.mock->send_count() == 3, "stt, gemini, tts called once each");
|
||
|
|
check(f.services.voice->sessions().history_size("abc123") == 2,
|
||
|
|
"side effect: user and model turn remembered");
|
||
|
|
check(contains(rig.mock->seen()[1].body, "hoofdstad"),
|
||
|
|
"transcript fed to the model");
|
||
|
|
|
||
|
|
// Second turn carries the first as history.
|
||
|
|
rig.stt("en van Duitsland");
|
||
|
|
rig.gemini("Berlijn.");
|
||
|
|
rig.tts("SPOKEN-2");
|
||
|
|
f.client->post("/voice/turn?cid=abc123", "WEBM", "audio/webm");
|
||
|
|
auto gem2 = rig.mock->seen()[4].body;
|
||
|
|
check(contains(gem2, "Amsterdam.") && contains(gem2, "Duitsland"),
|
||
|
|
"history sent with the next turn");
|
||
|
|
|
||
|
|
// Typed turn.
|
||
|
|
rig.gemini("Hallo!");
|
||
|
|
rig.tts("SPOKEN-3");
|
||
|
|
auto typed = f.client->post("/voice/turn?cid=abc123", "{\"text\":\"hoi\"}",
|
||
|
|
"application/json");
|
||
|
|
check(typed.status == 200 && contains(typed.body, "\"reply\":\"Hallo!\""),
|
||
|
|
"typed turn answered");
|
||
|
|
|
||
|
|
// Silence: one STT call, nothing else.
|
||
|
|
std::size_t before = rig.mock->send_count();
|
||
|
|
rig.stt("");
|
||
|
|
auto quiet = f.client->post("/voice/turn?cid=quiet1", "WEBM", "audio/webm");
|
||
|
|
check(quiet.status == 200 && contains(quiet.body, "\"transcript\":\"\"") &&
|
||
|
|
contains(quiet.body, "\"audio_b64\":\"\""),
|
||
|
|
"silence answers an empty turn");
|
||
|
|
check(rig.mock->send_count() == before + 1, "silence costs one STT call");
|
||
|
|
|
||
|
|
// Validation and limits.
|
||
|
|
check(f.client->post("/voice/turn", "x", "audio/webm").status == 422,
|
||
|
|
"missing cid is 422");
|
||
|
|
check(f.client->post("/voice/turn?cid=bad%20id", "x", "audio/webm").status == 422,
|
||
|
|
"bad cid is 422");
|
||
|
|
check(f.client->post("/voice/turn?cid=a1", "x", "video/mp4").status == 415,
|
||
|
|
"unknown media type is 415");
|
||
|
|
check(f.client->post("/voice/turn?cid=a1", "", "audio/webm").status == 422,
|
||
|
|
"empty audio is 422");
|
||
|
|
check(f.client->post("/voice/turn?cid=a1", "{\"text\":\"\"}",
|
||
|
|
"application/json").status == 422,
|
||
|
|
"empty typed text is 422");
|
||
|
|
check(f.client->post("/voice/turn?cid=a1", std::string(1024 * 1024 + 8192, 'a'),
|
||
|
|
"audio/webm").status == 413,
|
||
|
|
"body over the cap is 413");
|
||
|
|
check(f.client->get("/voice/turn").status == 405, "get is 405");
|
||
|
|
|
||
|
|
// A failed model call leaves no half conversation.
|
||
|
|
GoogleRig failing;
|
||
|
|
failing.stt("hallo");
|
||
|
|
failing.upstream_error("/v1beta/models/rig-model:generateContent", 503,
|
||
|
|
"UNAVAILABLE", "overloaded");
|
||
|
|
Fixture g(failing.client);
|
||
|
|
auto bad = g.client->post("/voice/turn?cid=fail1", "x", "audio/webm");
|
||
|
|
check(bad.status == 502 && contains(bad.body, "UNAVAILABLE"), "502 on model failure");
|
||
|
|
check(g.services.voice->sessions().history_size("fail1") == 0,
|
||
|
|
"failed turn rolled back");
|
||
|
|
|
||
|
|
GoogleRig none(false);
|
||
|
|
Fixture h(none.client);
|
||
|
|
auto nc = h.client->post("/voice/turn?cid=n1", "x", "audio/webm");
|
||
|
|
check(nc.status == 503 && contains(nc.body, "google_not_configured"),
|
||
|
|
"no key is a loud 503");
|
||
|
|
unsetenv("GOOGLE_GENAI_MODEL");
|
||
|
|
return finish("voice/turn");
|
||
|
|
}
|