Files
moloapp/example/assets/ai_audio.js
T

83 lines
3.5 KiB
JavaScript
Raw Normal View History

// Audio page: thin client over /ai/audio/* (all limits are enforced server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("au-out"), err = $("au-error"), player = $("au-audio");
var templates = {
"tts": { text: "Hello from the audio demo.", voice: "", format: "mp3", confirm: false },
"stt": { url: "https://example.com/audio.mp3", language: "en", diarize: false, confirm: false },
"voices": {},
"call": { id: "get_models", segments: [], query: {}, body: {}, confirm: false },
"session": { text: "Streaming hello.", voice: "", confirm: false },
"ws/preview": { channel: "tts-stream-input", params: { voice_id: "VOICE_ID" } },
"split": { text: "One sentence. Another sentence. A third one.", max_chars: 20 }
};
var providerDefaults = {
"elevenlabs": { call: { id: "get_models" }, ws: "tts-stream-input" },
"deepgram": { call: { id: "models_list_3" }, ws: "listen-v1" },
"cartesia": { call: { id: "get_status" }, ws: "tts-websocket" },
"speechmatics": { call: { id: "jobs_list", query: { limit: 5 } }, ws: "realtime" },
"hume": { call: { id: "voices_list", query: { provider: "HUME_AI", page_size: 5 } }, ws: "tts-stream-input" },
"assemblyai": { call: { id: "listTranscripts", query: { limit: 5 } }, ws: "streaming" }
};
function show(j, ok) {
if (ok) {
err.textContent = "";
if (j && j.audio_base64) {
player.hidden = false;
player.src = "data:" + (j.content_type || "audio/mpeg") + ";base64," + j.audio_base64;
var c = {};
for (var k in j) { if (k !== "audio_base64") { c[k] = j[k]; } }
j = c;
} else { player.hidden = true; }
out.textContent = JSON.stringify(j, null, 2);
} else {
err.textContent = j.message || "Request failed";
out.textContent = "";
player.hidden = true;
}
}
function fill() {
var route = $("au-route").value, prov = $("au-provider").value;
var t = JSON.parse(JSON.stringify(templates[route]));
var d = providerDefaults[prov] || {};
if (route === "call" && d.call) { for (var k in d.call) { t[k] = d.call[k]; } }
if (route === "ws/preview" && d.ws) { t.channel = d.ws; }
if (route !== "split") { t.provider = prov; }
$("au-body").value = JSON.stringify(t, null, 1);
}
function send() {
var body;
try { body = JSON.parse($("au-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
err.textContent = "";
out.textContent = "...";
fetch("/ai/audio/" + $("au-route").value, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (res) {
return res.text().then(function (t) {
var j;
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
show(j, res.ok);
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
fetch("/ai/audio/status").then(function (r) { return r.json(); }).then(function (s) {
var missing = [], found = [];
s.providers.forEach(function (p) { (p.key_configured ? found : missing).push(p.key_env); });
$("au-config").textContent = (found.length ? "Keys found: " + found.join(", ") + ". " : "") +
(missing.length ? "No key for: " + missing.join(", ") + " (those routes answer 503)." : "");
});
$("au-route").onchange = fill;
$("au-provider").onchange = fill;
$("au-send").onclick = send;
fill();
})();