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CAAoC,GAAG,IAAI;IAI5F,SAAS,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,8BAA8B,GAAG,IAAI;IAIlF,SAAS,CAAC,iBAAiB,CAAC,kBAAkB,EAAE,gCAAgC,GAAG,IAAI;CAI1F","file":"SynthesisAdapterBase.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n    ArgumentNullError,\r\n    ConnectionClosedEvent,\r\n    ConnectionEvent,\r\n    ConnectionMessage,\r\n    ConnectionState,\r\n    createNoDashGuid,\r\n    EventSource,\r\n    IAudioDestination,\r\n    IConnection,\r\n    IDisposable,\r\n    MessageType,\r\n    ServiceEvent,\r\n} from \"../common/Exports.js\";\r\nimport { AudioOutputFormatImpl } from \"../sdk/Audio/AudioOutputFormat.js\";\r\nimport {\r\n    CancellationErrorCode,\r\n    CancellationReason,\r\n    PropertyCollection,\r\n    PropertyId,\r\n    ResultReason,\r\n    SpeechSynthesisBookmarkEventArgs,\r\n    SpeechSynthesisResult,\r\n    SpeechSynthesisVisemeEventArgs,\r\n    SpeechSynthesisWordBoundaryEventArgs,\r\n    Synthesizer,\r\n} from \"../sdk/Exports.js\";\r\nimport { SpeechSynthesisRequest } from \"../sdk/SpeechSynthesisRequest.js\";\r\nimport {\r\n    AgentConfig,\r\n    CancellationErrorCodePropertyName,\r\n    ISynthesisConnectionFactory,\r\n    ISynthesisMetadata,\r\n    MetadataType,\r\n    SynthesisAudioMetadata,\r\n    SynthesisContext,\r\n    SynthesisTurn,\r\n    SynthesizerConfig\r\n} from \"./Exports.js\";\r\nimport { AuthInfo, IAuthentication } from \"./IAuthentication.js\";\r\nimport { SpeechConnectionMessage } from \"./SpeechConnectionMessage.Internal.js\";\r\n\r\nexport abstract class SynthesisAdapterBase implements IDisposable {\r\n    protected privSynthesisTurn: SynthesisTurn;\r\n    protected privConnectionId: string;\r\n    protected privSynthesizerConfig: SynthesizerConfig;\r\n    protected privSynthesizer: Synthesizer;\r\n    protected privSuccessCallback: (e: SpeechSynthesisResult) => void;\r\n    protected privErrorCallback: (e: string) => void;\r\n\r\n    public get synthesizerConfig(): SynthesizerConfig {\r\n        return this.privSynthesizerConfig;\r\n    }\r\n\r\n    public get synthesisContext(): SynthesisContext {\r\n        return this.privSynthesisContext;\r\n    }\r\n\r\n    public get agentConfig(): AgentConfig {\r\n        return this.privAgentConfig;\r\n    }\r\n\r\n    public get connectionEvents(): EventSource<ConnectionEvent> {\r\n        return this.privConnectionEvents;\r\n    }\r\n\r\n    public get serviceEvents(): EventSource<ServiceEvent> {\r\n        return this.privServiceEvents;\r\n    }\r\n\r\n    protected speakOverride: (ssml: string, requestId: string, sc: (e: SpeechSynthesisResult) => void, ec: (e: string) => void) => void = undefined;\r\n\r\n    // Called when telemetry data is sent to the service.\r\n    // Used for testing Telemetry capture.\r\n    public static telemetryData: (json: string) => void;\r\n    public static telemetryDataEnabled: boolean = true;\r\n\r\n    public set activityTemplate(messagePayload: string) {\r\n        this.privActivityTemplate = messagePayload;\r\n    }\r\n    public get activityTemplate(): string {\r\n        return this.privActivityTemplate;\r\n    }\r\n\r\n    protected receiveMessageOverride: () => void = undefined;\r\n\r\n    protected connectImplOverride: (isUnAuthorized: boolean) => void = undefined;\r\n\r\n    protected configConnectionOverride: (connection: IConnection) => Promise<IConnection> = undefined;\r\n\r\n    public set audioOutputFormat(format: AudioOutputFormatImpl) {\r\n        this.privAudioOutputFormat = format;\r\n        this.privSynthesisTurn.audioOutputFormat = format;\r\n        if (this.privSessionAudioDestination !== undefined) {\r\n            this.privSessionAudioDestination.format = format;\r\n        }\r\n        if (this.synthesisContext !== undefined) {\r\n            this.synthesisContext.audioOutputFormat = format;\r\n        }\r\n    }\r\n    private privAuthentication: IAuthentication;\r\n    private privConnectionFactory: ISynthesisConnectionFactory;\r\n\r\n    // A promise for a configured connection.\r\n    // Do not consume directly, call fetchConnection instead.\r\n    private privConnectionConfigurationPromise: Promise<IConnection> = undefined;\r\n\r\n    // A promise for a connection, but one that has not had the speech context sent yet.\r\n    // Do not consume directly, call fetchConnection instead.\r\n    private privConnectionPromise: Promise<IConnection>;\r\n    private privAuthFetchEventId: string;\r\n    private privIsDisposed: boolean;\r\n    private privConnectionEvents: EventSource<ConnectionEvent>;\r\n    private privServiceEvents: EventSource<ServiceEvent>;\r\n    protected privSynthesisContext: SynthesisContext;\r\n    private privAgentConfig: AgentConfig;\r\n    private privActivityTemplate: string;\r\n    protected privAudioOutputFormat: AudioOutputFormatImpl;\r\n    private privSessionAudioDestination: IAudioDestination;\r\n\r\n    public constructor(\r\n        authentication: IAuthentication,\r\n        connectionFactory: ISynthesisConnectionFactory,\r\n        synthesizerConfig: SynthesizerConfig,\r\n        audioDestination: IAudioDestination) {\r\n\r\n        if (!authentication) {\r\n            throw new ArgumentNullError(\"authentication\");\r\n        }\r\n\r\n        if (!connectionFactory) {\r\n            throw new ArgumentNullError(\"connectionFactory\");\r\n        }\r\n\r\n        if (!synthesizerConfig) {\r\n            throw new ArgumentNullError(\"synthesizerConfig\");\r\n        }\r\n\r\n        this.privAuthentication = authentication;\r\n        this.privConnectionFactory = connectionFactory;\r\n        this.privSynthesizerConfig = synthesizerConfig;\r\n        this.privIsDisposed = false;\r\n        this.privSessionAudioDestination = audioDestination;\r\n        this.privSynthesisTurn = new SynthesisTurn();\r\n        this.privConnectionEvents = new EventSource<ConnectionEvent>();\r\n        this.privServiceEvents = new EventSource<ServiceEvent>();\r\n        this.privSynthesisContext = new SynthesisContext();\r\n        this.privAgentConfig = new AgentConfig();\r\n\r\n        this.connectionEvents.attach((connectionEvent: ConnectionEvent): void => {\r\n            if (connectionEvent.name === \"ConnectionClosedEvent\") {\r\n                const connectionClosedEvent = connectionEvent as ConnectionClosedEvent;\r\n                if (connectionClosedEvent.statusCode !== 1000) {\r\n                    this.cancelSynthesisLocal(CancellationReason.Error,\r\n                        connectionClosedEvent.statusCode === 1007 ? CancellationErrorCode.BadRequestParameters : CancellationErrorCode.ConnectionFailure,\r\n                        `${connectionClosedEvent.reason} websocket error code: ${connectionClosedEvent.statusCode}`);\r\n                }\r\n            }\r\n        });\r\n    }\r\n\r\n    public isDisposed(): boolean {\r\n        return this.privIsDisposed;\r\n    }\r\n\r\n    public async dispose(reason?: string): Promise<void> {\r\n        this.privIsDisposed = true;\r\n        if (this.privSessionAudioDestination !== undefined) {\r\n            this.privSessionAudioDestination.close();\r\n        }\r\n        if (this.privConnectionConfigurationPromise !== undefined) {\r\n            try {\r\n                const connection: IConnection = await this.privConnectionConfigurationPromise;\r\n                await connection.dispose(reason);\r\n            } catch {\r\n                // Connection was never successfully established, nothing to dispose\r\n            }\r\n        }\r\n    }\r\n\r\n    public async connect(): Promise<void> {\r\n        await this.connectImpl();\r\n    }\r\n\r\n    public async sendNetworkMessage(path: string, payload: string | ArrayBuffer): Promise<void> {\r\n        const type: MessageType = typeof payload === \"string\" ? MessageType.Text : MessageType.Binary;\r\n        const contentType: string = typeof payload === \"string\" ? \"application/json\" : \"\";\r\n\r\n        const connection: IConnection = await this.fetchConnection();\r\n        return connection.send(new SpeechConnectionMessage(type, path, this.privSynthesisTurn.requestId, contentType, payload));\r\n    }\r\n\r\n    public async Speak(\r\n        text: string,\r\n        isSSML: boolean,\r\n        requestId: string,\r\n        successCallback: (e: SpeechSynthesisResult) => void,\r\n        errorCallBack: (e: string) => void,\r\n        audioDestination: IAudioDestination | undefined,\r\n    ): Promise<void> {\r\n\r\n        let ssml: string;\r\n\r\n        if (isSSML) {\r\n            ssml = text;\r\n        } else {\r\n            ssml = this.privSynthesizer.buildSsml(text);\r\n        }\r\n\r\n        if (this.speakOverride !== undefined) {\r\n            return this.speakOverride(ssml, requestId, successCallback, errorCallBack);\r\n        }\r\n\r\n        this.privSuccessCallback = successCallback;\r\n        this.privErrorCallback = errorCallBack;\r\n\r\n        this.privSynthesisTurn.startNewSynthesis(requestId, text, isSSML, audioDestination);\r\n\r\n        try {\r\n            const connection: IConnection = await this.fetchConnection();\r\n            await this.sendSynthesisContext(connection);\r\n            await this.sendSsmlMessage(connection, ssml, requestId);\r\n            this.onSynthesisStarted(requestId);\r\n\r\n            void this.receiveMessage();\r\n        } catch (e) {\r\n            this.cancelSynthesisLocal(CancellationReason.Error, CancellationErrorCode.ConnectionFailure, e as string);\r\n            return Promise.reject(e);\r\n        }\r\n    }\r\n\r\n    public async stopSpeaking(): Promise<void> {\r\n        const connection: IConnection = await this.fetchConnection();\r\n\r\n        return connection.send(new SpeechConnectionMessage(\r\n            MessageType.Text,\r\n            \"synthesis.control\",\r\n            this.privSynthesisTurn.requestId,\r\n            \"application/json\",\r\n            JSON.stringify({\r\n                action: \"stop\"\r\n            })\r\n        ));\r\n    }\r\n\r\n    /**\r\n     * Performs text streaming synthesis using a SpeechSynthesisRequest.\r\n     * Text pieces are sent over WebSocket as they arrive via the request's input stream.\r\n     */\r\n    public async SpeakStream(\r\n        request: SpeechSynthesisRequest,\r\n        requestId: string,\r\n        successCallback: (e: SpeechSynthesisResult) => void,\r\n        errorCallBack: (e: string) => void,\r\n        audioDestination: IAudioDestination | undefined,\r\n    ): Promise<void> {\r\n\r\n        this.privSuccessCallback = successCallback;\r\n        this.privErrorCallback = errorCallBack;\r\n\r\n        this.privSynthesisTurn.startNewSynthesis(requestId, \"\", false, audioDestination);\r\n\r\n        try {\r\n            const connection: IConnection = await this.fetchConnection();\r\n\r\n            // Send synthesis.context with streaming input section embedded\r\n            await this.sendStreamingSynthesisContext(connection, request, requestId);\r\n\r\n            this.onSynthesisStarted(requestId);\r\n\r\n            // Set up text piece callback - each piece is sent as a text.piece message\r\n            request.onTextPiece = (text: string): void => {\r\n                this.sendTextPiece(connection, text, requestId).catch((e: unknown): void => {\r\n                    this.cancelSynthesisLocal(CancellationReason.Error, CancellationErrorCode.ConnectionFailure, e as string);\r\n                });\r\n            };\r\n\r\n            // Set up close callback - sends the finish signal\r\n            request.onClose = (): void => {\r\n                this.sendTextStreamEnd(connection, requestId).catch((e: unknown): void => {\r\n                    this.cancelSynthesisLocal(CancellationReason.Error, CancellationErrorCode.ConnectionFailure, e as string);\r\n                });\r\n            };\r\n\r\n            void this.receiveMessage();\r\n        } catch (e) {\r\n            this.cancelSynthesisLocal(CancellationReason.Error, CancellationErrorCode.ConnectionFailure, e as string);\r\n            return Promise.reject(e);\r\n        }\r\n    }\r\n\r\n    // Cancels synthesis.\r\n    protected cancelSynthesis(\r\n        requestId: string,\r\n        _cancellationReason: CancellationReason,\r\n        errorCode: CancellationErrorCode,\r\n        error: string): void {\r\n        const properties: PropertyCollection = new PropertyCollection();\r\n        properties.setProperty(CancellationErrorCodePropertyName, CancellationErrorCode[errorCode]);\r\n        const result: SpeechSynthesisResult = new SpeechSynthesisResult(\r\n            requestId,\r\n            ResultReason.Canceled,\r\n            undefined,\r\n            error,\r\n            properties\r\n        );\r\n\r\n        this.onSynthesisCancelled(result);\r\n\r\n        if (!!this.privSuccessCallback) {\r\n            try {\r\n                this.privSuccessCallback(result);\r\n                /* eslint-disable no-empty */\r\n            } catch { }\r\n        }\r\n    }\r\n\r\n    // Cancels synthesis.\r\n    protected cancelSynthesisLocal(\r\n        cancellationReason: CancellationReason,\r\n        errorCode: CancellationErrorCode,\r\n        error: string): void {\r\n\r\n        if (!!this.privSynthesisTurn.isSynthesizing) {\r\n            this.privSynthesisTurn.onStopSynthesizing();\r\n\r\n            this.cancelSynthesis(\r\n                this.privSynthesisTurn.requestId,\r\n                cancellationReason,\r\n                errorCode,\r\n                error);\r\n        }\r\n    }\r\n\r\n    // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n    protected processTypeSpecificMessages(_connectionMessage: SpeechConnectionMessage): boolean {\r\n        return true;\r\n    }\r\n\r\n    protected async receiveMessage(): Promise<void> {\r\n        try {\r\n            const connection: IConnection = await this.fetchConnection();\r\n            const message: ConnectionMessage = await connection.read();\r\n\r\n            if (this.receiveMessageOverride !== undefined) {\r\n                return this.receiveMessageOverride();\r\n            }\r\n            if (this.privIsDisposed) {\r\n                // We're done.\r\n                return;\r\n            }\r\n\r\n            // indicates we are draining the queue and it came with no message;\r\n            if (!message) {\r\n                if (!this.privSynthesisTurn.isSynthesizing) {\r\n                    return;\r\n                } else {\r\n                    return this.receiveMessage();\r\n                }\r\n            }\r\n\r\n\r\n            const connectionMessage = SpeechConnectionMessage.fromConnectionMessage(message);\r\n\r\n            if (connectionMessage.requestId.toLowerCase() === this.privSynthesisTurn.requestId.toLowerCase()) {\r\n                switch (connectionMessage.path.toLowerCase()) {\r\n                    case \"turn.start\":\r\n                        this.privSynthesisTurn.onServiceTurnStartResponse(connectionMessage.textBody);\r\n                        break;\r\n                    case \"response\":\r\n                        this.privSynthesisTurn.onServiceResponseMessage(connectionMessage.textBody);\r\n                        break;\r\n                    case \"audio\":\r\n                        if (this.privSynthesisTurn.streamId.toLowerCase() === connectionMessage.streamId.toLowerCase()\r\n                            && !!connectionMessage.binaryBody) {\r\n                            this.privSynthesisTurn.onAudioChunkReceived(connectionMessage.binaryBody);\r\n                            this.onSynthesizing(connectionMessage.binaryBody);\r\n                            if (this.privSessionAudioDestination !== undefined) {\r\n                                this.privSessionAudioDestination.write(connectionMessage.binaryBody);\r\n                            }\r\n                        }\r\n                        break;\r\n                    case \"audio.metadata\":\r\n                        const metadataList = SynthesisAudioMetadata.fromJSON(connectionMessage.textBody).Metadata;\r\n                        for (const metadata of metadataList) {\r\n                            switch (metadata.Type) {\r\n                                case MetadataType.WordBoundary:\r\n                                case MetadataType.SentenceBoundary:\r\n                                    this.privSynthesisTurn.onTextBoundaryEvent(metadata);\r\n                                    const wordBoundaryEventArgs: SpeechSynthesisWordBoundaryEventArgs = new SpeechSynthesisWordBoundaryEventArgs(\r\n                                        metadata.Data.Offset,\r\n                                        metadata.Data.Duration,\r\n                                        metadata.Data.text.Text,\r\n                                        metadata.Data.text.Length,\r\n                                        metadata.Type === MetadataType.WordBoundary\r\n                                            ? this.privSynthesisTurn.currentTextOffset : this.privSynthesisTurn.currentSentenceOffset,\r\n                                        metadata.Data.text.BoundaryType);\r\n                                    this.onWordBoundary(wordBoundaryEventArgs);\r\n                                    break;\r\n                                case MetadataType.Bookmark:\r\n                                    const bookmarkEventArgs: SpeechSynthesisBookmarkEventArgs = new SpeechSynthesisBookmarkEventArgs(\r\n                                        metadata.Data.Offset,\r\n                                        metadata.Data.Bookmark);\r\n                                    this.onBookmarkReached(bookmarkEventArgs);\r\n                                    break;\r\n                                case MetadataType.Viseme:\r\n                                    this.privSynthesisTurn.onVisemeMetadataReceived(metadata);\r\n                                    if (metadata.Data.IsLastAnimation) {\r\n                                        const visemeEventArgs: SpeechSynthesisVisemeEventArgs = new SpeechSynthesisVisemeEventArgs(\r\n                                            metadata.Data.Offset,\r\n                                            metadata.Data.VisemeId,\r\n                                            this.privSynthesisTurn.getAndClearVisemeAnimation());\r\n                                        this.onVisemeReceived(visemeEventArgs);\r\n                                    }\r\n                                    break;\r\n                                case MetadataType.AvatarSignal:\r\n                                    this.onAvatarEvent(metadata);\r\n                                    break;\r\n                                case MetadataType.SessionEnd:\r\n                                    this.privSynthesisTurn.onSessionEnd(metadata);\r\n                                    break;\r\n                            }\r\n                        }\r\n                        break;\r\n                    case \"turn.end\":\r\n                        this.privSynthesisTurn.onServiceTurnEndResponse();\r\n                        let result: SpeechSynthesisResult;\r\n                        try {\r\n                            result = await this.privSynthesisTurn.constructSynthesisResult();\r\n                            this.onSynthesisCompleted(result);\r\n                            if (!!this.privSuccessCallback) {\r\n                                this.privSuccessCallback(result);\r\n                            }\r\n                        } catch (error) {\r\n                            if (!!this.privErrorCallback) {\r\n                                this.privErrorCallback(error as string);\r\n                            }\r\n                        }\r\n                        break;\r\n\r\n                    default:\r\n                        if (!this.processTypeSpecificMessages(connectionMessage)) {\r\n                            // here are some messages that the derived class has not processed, dispatch them to connect class\r\n                            if (!!this.privServiceEvents) {\r\n                                this.serviceEvents.onEvent(new ServiceEvent(connectionMessage.path.toLowerCase(), connectionMessage.textBody));\r\n                            }\r\n                        }\r\n\r\n                }\r\n            }\r\n\r\n            return this.receiveMessage();\r\n\r\n        } catch (e) {\r\n            // TODO: What goes here?\r\n        }\r\n    }\r\n\r\n    protected sendSynthesisContext(connection: IConnection): Promise<void> {\r\n        this.setSynthesisContextSynthesisSection();\r\n        const synthesisContextJson = this.synthesisContext.toJSON();\r\n\r\n        if (synthesisContextJson) {\r\n            return connection.send(new SpeechConnectionMessage(\r\n                MessageType.Text,\r\n                \"synthesis.context\",\r\n                this.privSynthesisTurn.requestId,\r\n                \"application/json\",\r\n                synthesisContextJson));\r\n        }\r\n        return;\r\n    }\r\n\r\n    protected abstract setSynthesisContextSynthesisSection(): void;\r\n\r\n    protected setSpeechConfigSynthesisSection(): void {\r\n        return;\r\n    }\r\n\r\n    protected connectImpl(isUnAuthorized: boolean = false): Promise<IConnection> {\r\n        if (this.privConnectionPromise != null) {\r\n            return this.privConnectionPromise.then((connection: IConnection): Promise<IConnection> => {\r\n                if (connection.state() === ConnectionState.Disconnected) {\r\n                    this.privConnectionId = null;\r\n                    this.privConnectionPromise = null;\r\n                    return this.connectImpl();\r\n                }\r\n                return this.privConnectionPromise;\r\n            }, (): Promise<IConnection> => {\r\n                this.privConnectionId = null;\r\n                this.privConnectionPromise = null;\r\n                return this.connectImpl();\r\n            });\r\n        }\r\n        this.privAuthFetchEventId = createNoDashGuid();\r\n        this.privConnectionId = createNoDashGuid();\r\n\r\n        this.privSynthesisTurn.onPreConnectionStart(this.privAuthFetchEventId);\r\n\r\n        const authPromise = isUnAuthorized ? this.privAuthentication.fetchOnExpiry(this.privAuthFetchEventId) : this.privAuthentication.fetch(this.privAuthFetchEventId);\r\n\r\n        this.privConnectionPromise = authPromise.then(async (result: AuthInfo): Promise<IConnection> => {\r\n            this.privSynthesisTurn.onAuthCompleted(false);\r\n\r\n            const connection: IConnection = await this.privConnectionFactory.create(this.privSynthesizerConfig, result, this.privConnectionId);\r\n\r\n            // Attach to the underlying event. No need to hold onto the detach pointers as in the event the connection goes away,\r\n            // it'll stop sending events.\r\n            connection.events.attach((event: ConnectionEvent): void => {\r\n                this.connectionEvents.onEvent(event);\r\n            });\r\n            const response = await connection.open();\r\n            if (response.statusCode === 200) {\r\n                this.privSynthesisTurn.onConnectionEstablishCompleted(response.statusCode);\r\n                return Promise.resolve(connection);\r\n            } else if (response.statusCode === 403 && !isUnAuthorized) {\r\n                return this.connectImpl(true);\r\n            } else {\r\n                this.privSynthesisTurn.onConnectionEstablishCompleted(response.statusCode);\r\n                return Promise.reject(\r\n                    `Unable to contact server. StatusCode: ${response.statusCode},\r\n                    ${this.privSynthesizerConfig.parameters.getProperty(PropertyId.SpeechServiceConnection_Url)} Reason: ${response.reason}`);\r\n            }\r\n        }, (error: string): Promise<IConnection> => {\r\n            this.privSynthesisTurn.onAuthCompleted(true);\r\n            throw new Error(error);\r\n        });\r\n\r\n        // Attach an empty handler to allow the promise to run in the background while\r\n        // other startup events happen. It'll eventually be awaited on.\r\n        // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n        this.privConnectionPromise.catch((): void => { });\r\n\r\n        return this.privConnectionPromise;\r\n    }\r\n    protected sendSpeechServiceConfig(connection: IConnection, SpeechServiceConfigJson: string): Promise<void> {\r\n        if (SpeechServiceConfigJson) {\r\n            return connection.send(new SpeechConnectionMessage(\r\n                MessageType.Text,\r\n                \"speech.config\",\r\n                this.privSynthesisTurn.requestId,\r\n                \"application/json\",\r\n                SpeechServiceConfigJson));\r\n        }\r\n    }\r\n\r\n    protected sendSsmlMessage(connection: IConnection, ssml: string, requestId: string): Promise<void> {\r\n        return connection.send(new SpeechConnectionMessage(\r\n            MessageType.Text,\r\n            \"ssml\",\r\n            requestId,\r\n            \"application/ssml+xml\",\r\n            ssml));\r\n    }\r\n\r\n    /**\r\n     * Sends the synthesis.context message for text streaming mode.\r\n     * Includes the standard audio/language sections plus an input section\r\n     * with bidirectionalStreamingMode, voice name, and per-request properties.\r\n     */\r\n    protected async sendStreamingSynthesisContext(\r\n        connection: IConnection,\r\n        request: SpeechSynthesisRequest,\r\n        requestId: string\r\n    ): Promise<void> {\r\n        // Build the standard synthesis context first\r\n        this.setSynthesisContextSynthesisSection();\r\n\r\n        // Build the streaming input section\r\n        const inputSection: { [key: string]: any } = {\r\n            bidirectionalStreamingMode: true,\r\n        };\r\n\r\n        // Add voice name and language from synthesizer properties\r\n        const voiceName = this.privSynthesizerConfig.parameters.getProperty(\r\n            PropertyId.SpeechServiceConnection_SynthVoice, \"\");\r\n        const language = this.privSynthesizerConfig.parameters.getProperty(\r\n            PropertyId.SpeechServiceConnection_SynthLanguage, \"en-US\");\r\n\r\n        if (voiceName) {\r\n            inputSection.voiceName = voiceName;\r\n        }\r\n        inputSection.language = language;\r\n\r\n        // Add per-request voice properties\r\n        const pitch = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_Pitch, undefined);\r\n        const rate = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_Rate, undefined);\r\n        const volume = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_Volume, undefined);\r\n        const style = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_Style, undefined);\r\n        const temperature = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_Temperature, undefined);\r\n        const customLexiconUrl = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_CustomLexiconUrl, undefined);\r\n        const preferLocales = request.properties.getProperty(PropertyId.SpeechSynthesisRequest_PreferLocales, undefined);\r\n\r\n        if (pitch !== undefined) {\r\n            inputSection.pitch = pitch;\r\n        }\r\n        if (rate !== undefined) {\r\n            inputSection.rate = rate;\r\n        }\r\n        if (volume !== undefined) {\r\n            inputSection.volume = volume;\r\n        }\r\n        if (style !== undefined) {\r\n            inputSection.style = style;\r\n        }\r\n        if (temperature !== undefined) {\r\n            inputSection.temperature = temperature;\r\n        }\r\n        if (customLexiconUrl !== undefined) {\r\n            inputSection.customLexiconUrl = customLexiconUrl;\r\n        }\r\n        if (preferLocales !== undefined) {\r\n            inputSection.preferLocales = preferLocales;\r\n        }\r\n\r\n        // Add the input section to the synthesis context\r\n        const existingSynthesis = this.privSynthesisContext.getSection<Record<string, unknown>>(\"synthesis\") ?? {};\r\n        this.privSynthesisContext.setSection(\"synthesis\", {\r\n            ...existingSynthesis,\r\n            input: inputSection,\r\n        } as Record<string, unknown>);\r\n\r\n        const synthesisContextJson = this.privSynthesisContext.toJSON();\r\n        if (synthesisContextJson) {\r\n            return connection.send(new SpeechConnectionMessage(\r\n                MessageType.Text,\r\n                \"synthesis.context\",\r\n                requestId,\r\n                \"application/json\",\r\n                synthesisContextJson));\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Sends a text piece for streaming synthesis.\r\n     * Path: text.piece, Content-Type: text/plain, Body: raw text\r\n     */\r\n    protected async sendTextPiece(connection: IConnection, text: string, requestId: string): Promise<void> {\r\n        return connection.send(new SpeechConnectionMessage(\r\n            MessageType.Text,\r\n            \"text.piece\",\r\n            requestId,\r\n            \"text/plain\",\r\n            text));\r\n    }\r\n\r\n    /**\r\n     * Sends the end-of-stream signal for text streaming synthesis.\r\n     * Path: text.end, Content-Type: text/plain, Body: empty\r\n     */\r\n    protected async sendTextStreamEnd(connection: IConnection, requestId: string): Promise<void> {\r\n        return connection.send(new SpeechConnectionMessage(\r\n            MessageType.Text,\r\n            \"text.end\",\r\n            requestId,\r\n            \"text/plain\",\r\n            \"\"));\r\n    }\r\n\r\n    private async fetchConnection(): Promise<IConnection> {\r\n        if (this.privConnectionConfigurationPromise !== undefined) {\r\n            return this.privConnectionConfigurationPromise.then((connection: IConnection): Promise<IConnection> => {\r\n                if (connection.state() === ConnectionState.Disconnected) {\r\n                    this.privConnectionId = null;\r\n                    this.privConnectionConfigurationPromise = undefined;\r\n                    return this.fetchConnection();\r\n                }\r\n                return this.privConnectionConfigurationPromise;\r\n            }, (): Promise<IConnection> => {\r\n                this.privConnectionId = null;\r\n                this.privConnectionConfigurationPromise = undefined;\r\n                return this.fetchConnection();\r\n            });\r\n        }\r\n        this.privConnectionConfigurationPromise = this.configureConnection();\r\n        return await this.privConnectionConfigurationPromise;\r\n    }\r\n\r\n    // Takes an established websocket connection to the endpoint and sends speech configuration information.\r\n    private async configureConnection(): Promise<IConnection> {\r\n        const connection: IConnection = await this.connectImpl();\r\n        if (this.configConnectionOverride !== undefined) {\r\n            return this.configConnectionOverride(connection);\r\n        }\r\n        this.setSpeechConfigSynthesisSection();\r\n        await this.sendSpeechServiceConfig(connection, this.privSynthesizerConfig.SpeechServiceConfig.serialize());\r\n        return connection;\r\n    }\r\n\r\n    protected onAvatarEvent(_metadata: ISynthesisMetadata): void {\r\n        return;\r\n    }\r\n\r\n    protected onSynthesisStarted(_requestId: string): void {\r\n        return;\r\n    }\r\n\r\n    protected onSynthesizing(_audio: ArrayBuffer): void {\r\n        return;\r\n    }\r\n\r\n    protected onSynthesisCancelled(_result: SpeechSynthesisResult): void {\r\n        return;\r\n    }\r\n\r\n    protected onSynthesisCompleted(_result: SpeechSynthesisResult): void {\r\n        return;\r\n    }\r\n\r\n    protected onWordBoundary(_wordBoundaryEventArgs: SpeechSynthesisWordBoundaryEventArgs): void {\r\n        return;\r\n    }\r\n\r\n    protected onVisemeReceived(_visemeEventArgs: SpeechSynthesisVisemeEventArgs): void {\r\n        return;\r\n    }\r\n\r\n    protected onBookmarkReached(_bookmarkEventArgs: SpeechSynthesisBookmarkEventArgs): void {\r\n        return;\r\n    }\r\n\r\n}\r\n"]}