feat: web frontend with voice selector, transcript, LiveKit client

This commit is contained in:
Shane
2026-08-22 07:43:31 -04:00
parent 97633cc585
commit 456fd4e7e2
3 changed files with 395 additions and 0 deletions
+172
View File
@@ -0,0 +1,172 @@
// Voice Assistant — LiveKit client
const { Room, RoomEvent, RemoteParticipant, AudioPlaybackStats } = LivekitClient;
// ── Config (injected from environment at build time, or hardcoded for LAN) ──
const LIVEKIT_URL = `ws://${window.location.hostname}:7880`;
const API_KEY = "devkey";
const ROOM_NAME = "voice-room";
const AGENT_NAME = "voice-assistant";
// ── State ──
let room = null;
let agentParticipant = null;
// ── DOM ──
const startBtn = document.getElementById("startBtn");
const stopBtn = document.getElementById("stopBtn");
const statusEl = document.getElementById("status");
const messagesEl = document.getElementById("messages");
const voiceSelect = document.getElementById("voiceSelect");
// ── Voice switching ──
voiceSelect.addEventListener("change", () => {
const voice = voiceSelect.value;
if (room && agentParticipant) {
// Send data message to agent to switch voice
room.localParticipant.publishData(
JSON.stringify({ type: "set_voice", voice }).encodeInto(new Uint8Array()),
[agentParticipant]
);
addMessage("system", `Voice changed to ${voice}`);
} else {
// Save preference for next session
localStorage.setItem("preferred_voice", voice);
}
});
// Restore saved voice preference
const savedVoice = localStorage.getItem("preferred_voice");
if (savedVoice) {
voiceSelect.value = savedVoice;
}
// ── Start ──
startBtn.addEventListener("click", async () => {
try {
statusEl.textContent = "Connecting...";
room = new Room({
adaptiveStream: true,
dynacast: true,
});
// Listen for transcripts from the agent
room.on(RoomEvent.ParticipantConnected, (participant) => {
if (participant.identity === AGENT_NAME || participant.kind === "agent") {
agentParticipant = participant;
statusEl.textContent = "Agent connected";
// Send current voice selection
const voice = voiceSelect.value;
room.localParticipant.publishData(
JSON.stringify({ type: "set_voice", voice }).encodeInto(new Uint8Array()),
[participant]
);
}
});
room.on(RoomEvent.DataReceived, (payload, participant) => {
try {
const msg = JSON.parse(payload.decodeToString());
if (msg.type === "transcript") {
addMessage(msg.role || "agent", msg.text);
}
} catch (e) {
// Ignore non-JSON data
}
});
room.on(RoomEvent.ConnectionQualityChanged, (participant, quality) => {
if (participant === room.localParticipant) {
statusEl.textContent = `Connected (${quality} quality)`;
}
});
room.on(RoomEvent.Disconnected, () => {
statusEl.textContent = "Disconnected";
startBtn.disabled = false;
stopBtn.disabled = true;
});
await room.connect(LIVEKIT_URL, {
accessToken: generateToken(),
autoSubscribe: true,
});
// Request agent join
await requestAgentJoin();
statusEl.textContent = "Connected — speak now";
startBtn.disabled = true;
stopBtn.disabled = false;
} catch (err) {
console.error("Connection failed:", err);
statusEl.textContent = `Error: ${err.message}`;
}
});
// ── Stop ──
stopBtn.addEventListener("click", () => {
if (room) {
room.disconnect();
room = null;
agentParticipant = null;
}
statusEl.textContent = "Disconnected";
startBtn.disabled = false;
stopBtn.disabled = true;
});
// ── Helper: generate a LiveKit access token (simplified for LAN dev) ──
// In production, this would be done server-side. For LAN dev, we use a static token.
function generateToken() {
// The LiveKit server with devkey/devsecret will accept any token signed with that key.
// For simplicity in this LAN setup, we'll let the server handle auth.
return "dev-token";
}
// ── Helper: request agent to join the room ──
async function requestAgentJoin() {
// Send a data message to trigger agent dispatch
if (room && room.localParticipant) {
const msg = JSON.stringify({ type: "join_agent", agent: AGENT_NAME });
room.localParticipant.publishData(
msg.encodeInto(new Uint8Array()),
[] // broadcast to all participants
);
}
}
// ── Helper: add a message to the transcript ──
function addMessage(role, text) {
const div = document.createElement("div");
div.className = `message ${role}`;
div.innerHTML = `<span class="role">${role === "user" ? "You" : role === "agent" ? "Assistant" : "System"}</span> ${text}`;
messagesEl.appendChild(div);
messagesEl.scrollTop = messagesEl.scrollHeight;
}
// ── Capture user speech for transcript display ──
// We'll use the Web Speech API as a fallback for showing what the user said.
// The actual STT is handled by the agent's Azure STT pipeline.
let recognition = null;
if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
const SpeechRec = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRec();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = "en-US";
recognition.onresult = (event) => {
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
addMessage("user", event.results[i][0].transcript);
}
}
};
// Start/stop recognition with the room connection
const origConnect = Room.prototype.connect;
// We'll start recognition when room connects
}
+50
View File
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voice Assistant</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<header>
<h1>Voice Assistant</h1>
<p class="subtitle">Azure Speech + Gemma LLM, real-time conversation</p>
</header>
<div class="controls">
<button id="startBtn" class="btn btn-primary">Start Conversation</button>
<button id="stopBtn" class="btn btn-danger" disabled>Stop</button>
</div>
<div class="voice-panel">
<label for="voiceSelect">Voice:</label>
<select id="voiceSelect">
<option value="en-US-AvaNeural" selected>Ava (DragonHD)</option>
<option value="en-US-JennyNeural">Jenny</option>
<option value="en-US-GuyNeural">Guy</option>
<option value="en-US-AndrewNeural">Andrew</option>
<option value="en-US-AriaNeural">Aria</option>
<option value="en-US-EmmaNeural">Emma</option>
<option value="en-US-EricNeural">Eric</option>
<option value="en-US-BrianNeural">Brian</option>
<option value="en-US-AshleyNeural">Ashley</option>
<option value="en-US-RichardNeural">Richard</option>
<option value="en-US-TinaNeural">Tina</option>
<option value="en-US-SteffanNeural">Steffan</option>
</select>
</div>
<div class="status" id="status">Disconnected</div>
<div class="transcript" id="transcript">
<div class="transcript-header">Conversation</div>
<div id="messages"></div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.umd.min.js"></script>
<script src="app.js"></script>
</body>
</html>
+173
View File
@@ -0,0 +1,173 @@
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #0f1117;
color: #e4e4e7;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: flex-start;
padding: 2rem 1rem;
}
.container {
width: 100%;
max-width: 680px;
}
header {
text-align: center;
margin-bottom: 2rem;
}
h1 {
font-size: 2rem;
font-weight: 700;
background: linear-gradient(135deg, #60a5fa, #a78bfa);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.subtitle {
color: #71717a;
margin-top: 0.5rem;
font-size: 0.9rem;
}
.controls {
display: flex;
gap: 1rem;
justify-content: center;
margin-bottom: 1.5rem;
}
.btn {
padding: 0.75rem 2rem;
border: none;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.btn:disabled {
opacity: 0.4;
cursor: not-allowed;
}
.btn-primary {
background: #3b82f6;
color: white;
}
.btn-primary:hover:not(:disabled) {
background: #2563eb;
transform: translateY(-1px);
}
.btn-danger {
background: #ef4444;
color: white;
}
.btn-danger:hover:not(:disabled) {
background: #dc2626;
}
.voice-panel {
display: flex;
align-items: center;
gap: 0.75rem;
justify-content: center;
margin-bottom: 1.5rem;
padding: 1rem;
background: #1c1e26;
border-radius: 8px;
}
.voice-panel label {
font-weight: 600;
color: #a1a1aa;
}
.voice-panel select {
padding: 0.5rem 1rem;
background: #27272a;
color: #e4e4e7;
border: 1px solid #3f3f46;
border-radius: 6px;
font-size: 0.9rem;
cursor: pointer;
}
.voice-panel select:focus {
outline: none;
border-color: #3b82f6;
}
.status {
text-align: center;
padding: 0.75rem;
background: #1c1e26;
border-radius: 8px;
margin-bottom: 1.5rem;
font-size: 0.9rem;
color: #a1a1aa;
}
.transcript {
background: #1c1e26;
border-radius: 8px;
padding: 1rem;
max-height: 50vh;
overflow-y: auto;
}
.transcript-header {
font-weight: 700;
color: #a1a1aa;
margin-bottom: 1rem;
font-size: 0.85rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.message {
padding: 0.6rem 0.8rem;
margin-bottom: 0.5rem;
border-radius: 6px;
font-size: 0.95rem;
line-height: 1.5;
}
.message.user {
background: #1e3a5f;
border-left: 3px solid #3b82f6;
}
.message.agent {
background: #2d1f4e;
border-left: 3px solid #a78bfa;
}
.message.system {
background: #27272a;
color: #71717a;
font-style: italic;
font-size: 0.85rem;
}
.role {
font-weight: 700;
margin-right: 0.5rem;
}
.message.user .role { color: #60a5fa; }
.message.agent .role { color: #a78bfa; }
/* Scrollbar */
.transcript::-webkit-scrollbar { width: 6px; }
.transcript::-webkit-scrollbar-track { background: transparent; }
.transcript::-webkit-scrollbar-thumb { background: #3f3f46; border-radius: 3px; }