async function refreshSpeechToken()

in static/js/chat.js [363:387]


async function refreshSpeechToken() {
    try {
        const response = await fetch("/get-speech-token");
        if (!response.ok) {
            throw new Error("Failed to refresh token, status " + response.status);
        }
        const tokenData = await response.json();
        token = tokenData.token;
        speechTokenTimestamp = new Date();
        console.log("Speech token refreshed.");

        // default to eastus2
        const currentRegion = speechRegion || "eastus2";

        // If the avatarSynthesizer is already initialized, update its SpeechConfig.
        if (avatarSynthesizer) {
            let newSpeechSynthesisConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(token, speechRegion);
            newSpeechSynthesisConfig.speechSynthesisVoiceName = avatarSynthesizer.speechSynthesisConfig.speechSynthesisVoiceName;
            // Update the existing synthesizer's configuration.
            avatarSynthesizer.speechSynthesisConfig = newSpeechSynthesisConfig;
        }
    } catch (error) {
        console.error("Error refreshing speech token:", error);
    }
}