feat(clients): live: Gemini Live API over WebSocket, RFC 6455 client, session, relay, discovery check

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 04:40:00 +02:00
co-authored by Claude Sonnet 5.5
parent 6d744a433f
commit fe2afe0b97
35 changed files with 4441 additions and 3 deletions
+127
View File
@@ -0,0 +1,127 @@
// Live API page: HTTP relay to a server-held WebSocket session.
// POST /agent/live/sessions -> id; POST .../send; GET .../events?cursor=N;
// POST .../close. Model audio (PCM16 mono, rate from the mime) is played as it arrives.
(function () {
"use strict";
var POLL_MS = 400;
var MAX_LOG = 200;
var $ = function (id) { return document.getElementById(id); };
var createBtn = $("l-create"), closeBtn = $("l-close"), stateEl = $("l-state");
var errorEl = $("l-error"), logEl = $("l-log"), form = $("l-form"), textEl = $("l-text");
var sendBtn = form.querySelector("button");
var sid = null, cursor = 0, timer = null, polling = false;
var audioCtx = null, playAt = 0;
function showError(msg) { errorEl.textContent = msg || ""; }
function setState(s) { stateEl.textContent = s; stateEl.className = "pill " + (sid ? "listening" : ""); }
function setActive(on) {
createBtn.disabled = on; closeBtn.disabled = !on;
textEl.disabled = !on; sendBtn.disabled = !on;
}
function addLog(who, text) {
var li = document.createElement("li");
li.className = who;
var b = document.createElement("b");
b.textContent = who + ": ";
li.appendChild(b);
li.appendChild(document.createTextNode(text));
logEl.appendChild(li);
while (logEl.children.length > MAX_LOG) logEl.removeChild(logEl.firstChild);
li.scrollIntoView({ block: "nearest" });
}
function api(method, url, body) {
return fetch(url, {
method: method,
headers: body ? { "Content-Type": "application/json" } : {},
body: body ? JSON.stringify(body) : undefined
}).then(function (res) {
return res.text().then(function (t) {
var j = {};
try { j = JSON.parse(t); } catch (e) { /* keep empty */ }
if (!res.ok) throw new Error(j.message || j.error || ("HTTP " + res.status));
return j;
});
});
}
function play(b64, mime) {
var m = /rate=(\d+)/.exec(mime || "");
var rate = m ? parseInt(m[1], 10) : 24000;
var raw = atob(b64), n = raw.length >> 1;
if (!n) return;
audioCtx = audioCtx || new (window.AudioContext || window.webkitAudioContext)();
var buf = audioCtx.createBuffer(1, n, rate), ch = buf.getChannelData(0);
for (var i = 0; i < n; i++) {
var v = raw.charCodeAt(2 * i) | (raw.charCodeAt(2 * i + 1) << 8);
ch[i] = (v > 32767 ? v - 65536 : v) / 32768;
}
var src = audioCtx.createBufferSource();
src.buffer = buf; src.connect(audioCtx.destination);
playAt = Math.max(playAt, audioCtx.currentTime);
src.start(playAt); playAt += buf.duration;
}
function onEvent(e) {
switch (e.type) {
case "text": addLog("model", e.text); break;
case "output_transcription": if (e.text) addLog("model", e.text); break;
case "input_transcription": if (e.text) addLog("you", e.text); break;
case "audio": try { play(e.data_b64, e.mime); } catch (x) { showError("audio: " + x.message); } break;
case "interrupted": playAt = 0; addLog("event", "interrupted"); break;
case "turn_complete": setState("ready"); break;
case "go_away": addLog("event", "server will disconnect in " + e.seconds + "s"); break;
case "error": showError(e.text); break;
case "closed": addLog("event", "closed (" + e.code + ") " + (e.text || "")); break;
}
}
function poll() {
if (!sid || polling) return;
polling = true;
api("GET", "/agent/live/sessions/" + sid + "/events?cursor=" + cursor + "&max=100").then(function (j) {
j.events.forEach(onEvent);
cursor = j.next_cursor;
if (!j.open && j.events.length === 0) endSession("session ended");
}).catch(function (e) { showError(e.message); endSession("lost"); })
.then(function () { polling = false; });
}
function endSession(label) {
if (timer) clearInterval(timer);
timer = null; sid = null; setActive(false); setState(label || "no session");
}
createBtn.addEventListener("click", function () {
showError(""); createBtn.disabled = true; setState("connecting");
api("POST", "/agent/live/sessions", { modality: "AUDIO", transcribe: "1" }).then(function (j) {
sid = j.id; cursor = 0; setActive(true); setState("ready");
timer = setInterval(poll, POLL_MS);
}).catch(function (e) { showError(e.message); setActive(false); setState("no session"); });
});
closeBtn.addEventListener("click", function () {
var id = sid;
endSession("closing");
if (id) api("POST", "/agent/live/sessions/" + id + "/close", {}).catch(function (e) { showError(e.message); })
.then(function () { setState("no session"); });
});
form.addEventListener("submit", function (ev) {
ev.preventDefault();
var t = textEl.value.trim();
if (!t || !sid) return;
textEl.value = "";
addLog("you", t);
setState("thinking");
api("POST", "/agent/live/sessions/" + sid + "/send", { kind: "text", text: t })
.catch(function (e) { showError(e.message); });
});
api("GET", "/agent/live/status").then(function (s) {
$("l-config").textContent = s.configured
? "Credentials found (" + s.auth_mode + "). Default model: " + s.default_model + "."
: "No Google credential is configured: starting a session answers 503 google_not_configured.";
createBtn.disabled = false;
}).catch(function (e) { showError(e.message); });
})();