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SAAS,EAAE,MAAM,GAAG,IAAI;IAY/G,SAAS,CAAC,iBAAiB,CACvB,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,kBAAkB,EAAE,kBAAkB,EACtC,SAAS,EAAE,qBAAqB,EAChC,KAAK,EAAE,MAAM,GAAG,IAAI;cAuDR,0BAA0B,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAelF,SAAS,CAAC,eAAe,CAAC,UAAU,EAAE,WAAW,EAAE,OAAO,EAAE;QAAE,CAAC,EAAE,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAcjG,OAAO,CAAC,wBAAwB;IAOhC,OAAO,CAAC,+BAA+B;CAO1C","file":"TranscriptionServiceRecognizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n    IAudioSource,\r\n    IConnection,\r\n    MessageType\r\n} from \"../common/Exports.js\";\r\nimport {\r\n    CancellationErrorCode,\r\n    CancellationReason,\r\n    ConversationTranscriptionCanceledEventArgs,\r\n    MeetingTranscriptionCanceledEventArgs,\r\n    PropertyCollection,\r\n    PropertyId,\r\n    ResultReason,\r\n    SpeechRecognitionEventArgs,\r\n    SpeechRecognitionResult,\r\n} from \"../sdk/Exports.js\";\r\nimport { ConversationInfo, MeetingInfo } from \"../sdk/Transcription/Exports.js\";\r\nimport { ConversationProperties } from \"../sdk/Transcription/IConversation.js\";\r\nimport { MeetingProperties } from \"../sdk/Transcription/IMeeting.js\";\r\nimport {\r\n    CancellationErrorCodePropertyName,\r\n    ConversationServiceRecognizer,\r\n    TranscriberRecognizer\r\n} from \"./Exports.js\";\r\nimport { IAuthentication } from \"./IAuthentication.js\";\r\nimport { IConnectionFactory } from \"./IConnectionFactory.js\";\r\nimport { RecognizerConfig } from \"./RecognizerConfig.js\";\r\nimport { SpeechConnectionMessage } from \"./SpeechConnectionMessage.Internal.js\";\r\n\r\n// eslint-disable-next-line max-classes-per-file\r\nexport class TranscriptionServiceRecognizer extends ConversationServiceRecognizer {\r\n\r\n    private privTranscriberRecognizer: TranscriberRecognizer;\r\n\r\n    public constructor(\r\n        authentication: IAuthentication,\r\n        connectionFactory: IConnectionFactory,\r\n        audioSource: IAudioSource,\r\n        recognizerConfig: RecognizerConfig,\r\n        transcriber: TranscriberRecognizer) {\r\n        super(authentication, connectionFactory, audioSource, recognizerConfig, transcriber);\r\n        this.privTranscriberRecognizer = transcriber;\r\n        this.sendPrePayloadJSONOverride = (connection: IConnection): Promise<void> => this.sendTranscriptionStartJSON(connection);\r\n        if (this.privRecognizerConfig.parameters.getProperty(PropertyId.SpeechServiceResponse_RequestWordLevelTimestamps) === \"true\") {\r\n            this.privSpeechContext.setWordLevelTimings();\r\n        }\r\n    }\r\n\r\n    public async sendSpeechEventAsync(info: ConversationInfo, command: string): Promise<void> {\r\n        if (!!this.privRequestSession.isRecognizing) {\r\n            const connection: IConnection = await this.fetchConnection();\r\n            await this.sendSpeechEvent(connection, this.createSpeechEventPayload(info, command));\r\n        }\r\n    }\r\n\r\n    public async sendMeetingSpeechEventAsync(info: MeetingInfo, command: string): Promise<void> {\r\n        if (!!this.privRequestSession.isRecognizing) {\r\n            const connection: IConnection = await this.fetchConnection();\r\n            await this.sendSpeechEvent(connection, this.createMeetingSpeechEventPayload(info, command));\r\n        }\r\n    }\r\n\r\n    protected processTypeSpecificMessages(connectionMessage: SpeechConnectionMessage): Promise<boolean> {\r\n        return this.processSpeechMessages(connectionMessage);\r\n    }\r\n\r\n    protected handleRecognizedCallback(result: SpeechRecognitionResult, offset: number, sessionId: string): void {\r\n        try {\r\n            const event: SpeechRecognitionEventArgs = new SpeechRecognitionEventArgs(result, offset, sessionId);\r\n            this.privTranscriberRecognizer.recognized(this.privTranscriberRecognizer, event);\r\n            if (!!this.privSuccessCallback) {\r\n                try {\r\n                    this.privSuccessCallback(result);\r\n                } catch (e) {\r\n                    if (!!this.privErrorCallback) {\r\n                        this.privErrorCallback(e as string);\r\n                    }\r\n                }\r\n                // Only invoke the call back once.\r\n                // and if it's successful don't invoke the\r\n                // error after that.\r\n                this.privSuccessCallback = undefined;\r\n                this.privErrorCallback = undefined;\r\n            }\r\n        /* eslint-disable no-empty */\r\n        } catch (error) {\r\n            // Not going to let errors in the event handler\r\n            // trip things up.\r\n        }\r\n    }\r\n\r\n    protected handleRecognizingCallback(result: SpeechRecognitionResult, duration: number, sessionId: string): void {\r\n        try {\r\n            const ev = new SpeechRecognitionEventArgs(result, duration, sessionId);\r\n            this.privTranscriberRecognizer.recognizing(this.privTranscriberRecognizer, ev);\r\n            /* eslint-disable no-empty */\r\n        } catch (error) {\r\n            // Not going to let errors in the event handler\r\n            // trip things up.\r\n        }\r\n    }\r\n\r\n    // Cancels recognition.\r\n    protected cancelRecognition(\r\n        sessionId: string,\r\n        requestId: string,\r\n        cancellationReason: CancellationReason,\r\n        errorCode: CancellationErrorCode,\r\n        error: string): void {\r\n\r\n        const properties: PropertyCollection = new PropertyCollection();\r\n        properties.setProperty(CancellationErrorCodePropertyName, CancellationErrorCode[errorCode]);\r\n\r\n        if (this.privTranscriberRecognizer.IsMeetingRecognizer()) {\r\n            if (!!this.privTranscriberRecognizer.canceled) {\r\n                const cancelEvent: MeetingTranscriptionCanceledEventArgs = new MeetingTranscriptionCanceledEventArgs(\r\n                    cancellationReason,\r\n                    error,\r\n                    errorCode,\r\n                    undefined,\r\n                    sessionId);\r\n                try {\r\n                    this.privTranscriberRecognizer.canceled(this.privTranscriberRecognizer, cancelEvent);\r\n                    /* eslint-disable no-empty */\r\n                } catch { }\r\n            }\r\n        } else {\r\n            if (!!this.privTranscriberRecognizer.canceled) {\r\n                const cancelEvent: ConversationTranscriptionCanceledEventArgs = new ConversationTranscriptionCanceledEventArgs(\r\n                    cancellationReason,\r\n                    error,\r\n                    errorCode,\r\n                    undefined,\r\n                    sessionId);\r\n                try {\r\n                    this.privTranscriberRecognizer.canceled(this.privTranscriberRecognizer, cancelEvent);\r\n                    /* eslint-disable no-empty */\r\n                } catch { }\r\n            }\r\n        }\r\n\r\n        if (!!this.privSuccessCallback) {\r\n            const result: SpeechRecognitionResult = new SpeechRecognitionResult(\r\n                requestId,\r\n                ResultReason.Canceled,\r\n                undefined, // Text\r\n                undefined, // Duration\r\n                undefined, // Offset\r\n                undefined, // Language\r\n                undefined, // Language Detection Confidence\r\n                undefined, // Speaker Id\r\n                error,\r\n                undefined, // Json\r\n                properties);\r\n            try {\r\n                this.privSuccessCallback(result);\r\n                this.privSuccessCallback = undefined;\r\n                /* eslint-disable no-empty */\r\n            } catch { }\r\n        }\r\n    }\r\n\r\n    // Encapsulated for derived service recognizers that need to send additional JSON\r\n    protected async sendTranscriptionStartJSON(connection: IConnection): Promise<void> {\r\n        await this.sendSpeechContext(connection, true);\r\n        if (this.privTranscriberRecognizer.IsMeetingRecognizer()) {\r\n            const info: MeetingInfo = this.privTranscriberRecognizer.getMeetingInfo();\r\n            const payload: { [id: string]: any } = this.createMeetingSpeechEventPayload(info, \"start\");\r\n            await this.sendSpeechEvent(connection, payload);\r\n        } else {\r\n            const info: ConversationInfo = this.privTranscriberRecognizer.getConversationInfo();\r\n            const payload: { [id: string]: any } = this.createSpeechEventPayload(info, \"start\");\r\n            await this.sendSpeechEvent(connection, payload);\r\n        }\r\n        await this.sendWaveHeader(connection);\r\n        return;\r\n    }\r\n\r\n    protected sendSpeechEvent(connection: IConnection, payload: { [id: string]: any }): Promise<void> {\r\n        const speechEventJson = JSON.stringify(payload);\r\n\r\n        if (speechEventJson) {\r\n            return connection.send(new SpeechConnectionMessage(\r\n                MessageType.Text,\r\n                \"speech.event\",\r\n                this.privRequestSession.requestId,\r\n                \"application/json\",\r\n                speechEventJson));\r\n        }\r\n        return;\r\n    }\r\n\r\n    private createSpeechEventPayload(info: ConversationInfo, command: string): { [id: string]: any } {\r\n        const eventDict: { id: string; name: string; meeting: ConversationProperties } = { id: \"meeting\", name: command, meeting: info.conversationProperties };\r\n        eventDict.meeting.id = info.id;\r\n        eventDict.meeting.attendees = info.participants;\r\n        return eventDict;\r\n    }\r\n\r\n    private createMeetingSpeechEventPayload(info: MeetingInfo, command: string): { [id: string]: any } {\r\n        const eventDict: { id: string; name: string; meeting: MeetingProperties } = { id: \"meeting\", name: command, meeting: info.meetingProperties };\r\n        eventDict.meeting.id = info.id;\r\n        eventDict.meeting.attendees = info.participants;\r\n        return eventDict;\r\n    }\r\n\r\n}\r\n"]}