diff --git a/examples/webpage/app.js b/examples/webpage/app.js index 140775e..e18caad 100644 --- a/examples/webpage/app.js +++ b/examples/webpage/app.js @@ -39,6 +39,8 @@ const els = { micLabel: document.querySelector(".mic-btn__label"), micIndicator: document.getElementById("mic-indicator"), botIndicator: document.getElementById("bot-indicator"), + stateIndicator: document.getElementById("state-indicator"), + stateLabel: document.getElementById("state-label"), clearBtn: document.getElementById("clear-btn"), clearWsLogBtn: document.getElementById("clear-ws-log-btn"), wsLog: document.getElementById("ws-log"), @@ -71,6 +73,7 @@ const state = { // Chat state. currentAssistantBubble: null, + assistantState: "", // VU meter smoothing. meterLevel: 0, @@ -123,6 +126,15 @@ function setBotIndicator(active) { els.botIndicator.classList.toggle("is-active", active); } +function setAssistantState(value) { + const text = typeof value === "string" ? value.trim() : ""; + const label = text.length > 32 ? `${text.slice(0, 31)}…` : text; + state.assistantState = text; + els.stateIndicator.classList.toggle("is-active", Boolean(text)); + els.stateLabel.textContent = label ? `State ${label}` : "State -"; + els.stateIndicator.title = label ? `Assistant state: ${text}` : "Assistant state"; +} + function addBubble(role, text) { if (els.chatLog.querySelector(".chat__empty")) { els.chatLog.innerHTML = ""; @@ -157,6 +169,7 @@ function scrollChatToBottom() { function clearChat() { els.chatLog.innerHTML = ""; state.currentAssistantBubble = null; + setAssistantState(""); const empty = document.createElement("div"); empty.className = "chat__empty"; empty.innerHTML = "

Chat cleared.

"; @@ -769,6 +782,9 @@ function handleEvent(event) { case "response.text.final": handleAssistantFinal(event.text, event.interrupted); break; + case "response.state": + setAssistantState(event.state); + break; case "input.transcript.final": handleUserTranscript(event.text); break; @@ -885,6 +901,7 @@ async function connect() { state.ws = null; state.connected = false; state.connecting = false; + setAssistantState(""); if (state.micEnabled) stopMic(); stopPlaybackQueue(); setConnectButton(); diff --git a/examples/webpage/index.html b/examples/webpage/index.html index deef69e..85b1469 100644 --- a/examples/webpage/index.html +++ b/examples/webpage/index.html @@ -118,6 +118,10 @@ Bot + + + State - +