{"version":3,"sources":["src/common.speech/SpeechServiceRecognizer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EACH,qBAAqB,EACrB,kBAAkB,EAQlB,gBAAgB,EACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAMH,qBAAqB,EAGxB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAGhF,qBAAa,uBAAwB,SAAQ,qBAAqB;IAE9D,OAAO,CAAC,oBAAoB,CAAmB;gBAG3C,cAAc,EAAE,eAAe,EAC/B,iBAAiB,EAAE,kBAAkB,EACrC,WAAW,EAAE,YAAY,EACzB,gBAAgB,EAAE,gBAAgB,EAClC,gBAAgB,EAAE,gBAAgB;cAMtB,2BAA2B,CAAC,iBAAiB,EAAE,uBAAuB,GAAG,OAAO,CAAC,OAAO,CAAC;IA2IzG,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;CAsC3B","file":"SpeechServiceRecognizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport { IAudioSource } from \"../common/Exports.js\";\r\nimport {\r\n    CancellationErrorCode,\r\n    CancellationReason,\r\n    OutputFormat,\r\n    PropertyCollection,\r\n    PropertyId,\r\n    ResultReason,\r\n    SpeechRecognitionCanceledEventArgs,\r\n    SpeechRecognitionEventArgs,\r\n    SpeechRecognitionResult,\r\n    SpeechRecognizer,\r\n} from \"../sdk/Exports.js\";\r\nimport {\r\n    CancellationErrorCodePropertyName,\r\n    DetailedSpeechPhrase,\r\n    EnumTranslation,\r\n    OutputFormatPropertyName,\r\n    RecognitionStatus,\r\n    ServiceRecognizerBase,\r\n    SimpleSpeechPhrase,\r\n    SpeechHypothesis,\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 SpeechServiceRecognizer extends ServiceRecognizerBase {\r\n\r\n    private privSpeechRecognizer: SpeechRecognizer;\r\n\r\n    public constructor(\r\n        authentication: IAuthentication,\r\n        connectionFactory: IConnectionFactory,\r\n        audioSource: IAudioSource,\r\n        recognizerConfig: RecognizerConfig,\r\n        speechRecognizer: SpeechRecognizer) {\r\n        super(authentication, connectionFactory, audioSource, recognizerConfig, speechRecognizer);\r\n        this.privSpeechRecognizer = speechRecognizer;\r\n\r\n    }\r\n\r\n    protected async processTypeSpecificMessages(connectionMessage: SpeechConnectionMessage): Promise<boolean> {\r\n\r\n        let result: SpeechRecognitionResult;\r\n\r\n        const resultProps: PropertyCollection = new PropertyCollection();\r\n\r\n        let processed: boolean = false;\r\n\r\n        switch (connectionMessage.path.toLowerCase()) {\r\n            case \"speech.hypothesis\":\r\n            case \"speech.fragment\":\r\n                const hypothesis: SpeechHypothesis = SpeechHypothesis.fromJSON(connectionMessage.textBody, this.privRequestSession.currentTurnAudioOffset);\r\n                resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, hypothesis.asJson());\r\n\r\n                const hypothesisLatencyMs = this.privRequestSession.onHypothesis(hypothesis.Offset);\r\n                if (hypothesisLatencyMs > 0) {\r\n                    resultProps.setProperty(PropertyId.SpeechServiceResponse_RecognitionLatencyMs, hypothesisLatencyMs.toString());\r\n                }\r\n\r\n                result = new SpeechRecognitionResult(\r\n                    this.privRequestSession.requestId,\r\n                    ResultReason.RecognizingSpeech,\r\n                    hypothesis.Text,\r\n                    hypothesis.Duration,\r\n                    hypothesis.Offset,\r\n                    hypothesis.Language,\r\n                    hypothesis.LanguageDetectionConfidence,\r\n                    undefined, // Speaker Id\r\n                    undefined,\r\n                    hypothesis.asJson(),\r\n                    resultProps);\r\n\r\n                const ev = new SpeechRecognitionEventArgs(result, hypothesis.Offset, this.privRequestSession.sessionId);\r\n\r\n                if (!!this.privSpeechRecognizer.recognizing) {\r\n                    try {\r\n                        this.privSpeechRecognizer.recognizing(this.privSpeechRecognizer, 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                processed = true;\r\n                break;\r\n            case \"speech.phrase\":\r\n                const simple: SimpleSpeechPhrase = SimpleSpeechPhrase.fromJSON(connectionMessage.textBody, this.privRequestSession.currentTurnAudioOffset);\r\n                resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, simple.asJson());\r\n\r\n                const resultReason: ResultReason = EnumTranslation.implTranslateRecognitionResult(simple.RecognitionStatus, this.privExpectContentAssessmentResponse);\r\n\r\n                const phraseLatencyMs = this.privRequestSession.onPhraseRecognized(simple.Offset + simple.Duration);\r\n                if (phraseLatencyMs > 0) {\r\n                    resultProps.setProperty(PropertyId.SpeechServiceResponse_RecognitionLatencyMs, phraseLatencyMs.toString());\r\n                }\r\n\r\n                if (ResultReason.Canceled === resultReason) {\r\n                    const cancelReason: CancellationReason = EnumTranslation.implTranslateCancelResult(simple.RecognitionStatus);\r\n                    const cancellationErrorCode: CancellationErrorCode = EnumTranslation.implTranslateCancelErrorCode(simple.RecognitionStatus);\r\n\r\n                    await this.cancelRecognitionLocal(\r\n                        cancelReason,\r\n                        cancellationErrorCode,\r\n                        EnumTranslation.implTranslateErrorDetails(cancellationErrorCode));\r\n\r\n                } else {\r\n                    // Like the native SDK's, don't event / return an EndOfDictation message.\r\n                    if (simple.RecognitionStatus === RecognitionStatus.EndOfDictation) {\r\n                        break;\r\n                    }\r\n\r\n                    if (this.privRecognizerConfig.parameters.getProperty(OutputFormatPropertyName) === OutputFormat[OutputFormat.Simple]) {\r\n                        result = new SpeechRecognitionResult(\r\n                            this.privRequestSession.requestId,\r\n                            resultReason,\r\n                            simple.DisplayText,\r\n                            simple.Duration,\r\n                            simple.Offset,\r\n                            simple.Language,\r\n                            simple.LanguageDetectionConfidence,\r\n                            undefined, // Speaker Id\r\n                            undefined,\r\n                            simple.asJson(),\r\n                            resultProps);\r\n                    } else {\r\n                        const detailed: DetailedSpeechPhrase = DetailedSpeechPhrase.fromJSON(connectionMessage.textBody, this.privRequestSession.currentTurnAudioOffset);\r\n                        resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, detailed.asJson());\r\n\r\n                        result = new SpeechRecognitionResult(\r\n                            this.privRequestSession.requestId,\r\n                            resultReason,\r\n                            detailed.RecognitionStatus === RecognitionStatus.Success ? detailed.NBest[0].Display : \"\",\r\n                            detailed.Duration,\r\n                            detailed.Offset,\r\n                            detailed.Language,\r\n                            detailed.LanguageDetectionConfidence,\r\n                            undefined, // Speaker Id\r\n                            undefined,\r\n                            detailed.asJson(),\r\n                            resultProps);\r\n                    }\r\n\r\n                    const event: SpeechRecognitionEventArgs = new SpeechRecognitionEventArgs(result, result.offset, this.privRequestSession.sessionId);\r\n\r\n                    if (!!this.privSpeechRecognizer.recognized) {\r\n                        try {\r\n                            this.privSpeechRecognizer.recognized(this.privSpeechRecognizer, event);\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\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                }\r\n                processed = true;\r\n                break;\r\n            default:\r\n                break;\r\n        }\r\n        return processed;\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.privSpeechRecognizer.canceled) {\r\n            const cancelEvent: SpeechRecognitionCanceledEventArgs = new SpeechRecognitionCanceledEventArgs(\r\n                cancellationReason,\r\n                error,\r\n                errorCode,\r\n                undefined,\r\n                sessionId);\r\n            try {\r\n                this.privSpeechRecognizer.canceled(this.privSpeechRecognizer, cancelEvent);\r\n                /* eslint-disable no-empty */\r\n            } catch { }\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"]}