{"version":3,"sources":["src/common.speech/ConversationTranscriptionServiceRecognizer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EACH,qBAAqB,EACrB,kBAAkB,EAQlB,uBAAuB,EAC1B,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;AAKhF,qBAAa,0CAA2C,SAAQ,qBAAqB;IAEjF,OAAO,CAAC,2BAA2B,CAA0B;gBAGzD,cAAc,EAAE,eAAe,EAC/B,iBAAiB,EAAE,kBAAkB,EACrC,WAAW,EAAE,YAAY,EACzB,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,uBAAuB;IAMpD,SAAS,CAAC,yBAAyB,IAAI,IAAI;cAc3B,2BAA2B,CAAC,iBAAiB,EAAE,uBAAuB,GAAG,OAAO,CAAC,OAAO,CAAC;IAmHzG,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;CAkB3B","file":"ConversationTranscriptionServiceRecognizer.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    ConversationTranscriptionCanceledEventArgs,\r\n    ConversationTranscriptionEventArgs,\r\n    ConversationTranscriptionResult,\r\n    ConversationTranscriber,\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\nimport { SpeakerDiarization, SpeakerDiarizationMode } from \"./ServiceMessages/PhraseDetection/SpeakerDiarization.js\";\r\nimport { RecognitionMode } from \"./ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\r\n\r\n// eslint-disable-next-line max-classes-per-file\r\nexport class ConversationTranscriptionServiceRecognizer extends ServiceRecognizerBase {\r\n\r\n    private privConversationTranscriber: ConversationTranscriber;\r\n\r\n    public constructor(\r\n        authentication: IAuthentication,\r\n        connectionFactory: IConnectionFactory,\r\n        audioSource: IAudioSource,\r\n        recognizerConfig: RecognizerConfig,\r\n        conversationTranscriber: ConversationTranscriber) {\r\n        super(authentication, connectionFactory, audioSource, recognizerConfig, conversationTranscriber);\r\n        this.privConversationTranscriber = conversationTranscriber;\r\n        this.setSpeakerDiarizationJson();\r\n    }\r\n\r\n    protected setSpeakerDiarizationJson(): void {\r\n        if (this.privEnableSpeakerId) {\r\n            const phraseDetection = this.privSpeechContext.getContext().phraseDetection || {};\r\n            phraseDetection.mode = RecognitionMode.Conversation;\r\n            const speakerDiarization: SpeakerDiarization = {};\r\n            speakerDiarization.mode = SpeakerDiarizationMode.Anonymous;\r\n            speakerDiarization.audioSessionId = this.privDiarizationSessionId;\r\n            speakerDiarization.audioOffsetMs = 0;\r\n            speakerDiarization.diarizeIntermediates = this.privRecognizerConfig.parameters.getProperty(PropertyId.SpeechServiceResponse_DiarizeIntermediateResults, \"false\") === \"true\";\r\n            phraseDetection.speakerDiarization = speakerDiarization;\r\n            this.privSpeechContext.getContext().phraseDetection = phraseDetection;\r\n        }\r\n    }\r\n\r\n    protected async processTypeSpecificMessages(connectionMessage: SpeechConnectionMessage): Promise<boolean> {\r\n\r\n        let result: ConversationTranscriptionResult;\r\n        const resultProps: PropertyCollection = new PropertyCollection();\r\n        resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, connectionMessage.textBody);\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\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 ConversationTranscriptionResult(\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                    hypothesis.SpeakerId,\r\n                    undefined,\r\n                    hypothesis.asJson(),\r\n                    resultProps);\r\n\r\n                const ev = new ConversationTranscriptionEventArgs(result, hypothesis.Duration, this.privRequestSession.sessionId);\r\n\r\n                if (!!this.privConversationTranscriber.transcribing) {\r\n                    try {\r\n                        this.privConversationTranscriber.transcribing(this.privConversationTranscriber, 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                const resultReason: ResultReason = EnumTranslation.implTranslateRecognitionResult(simple.RecognitionStatus);\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                    if (!(this.privRequestSession.isSpeechEnded && resultReason === ResultReason.NoMatch && simple.RecognitionStatus !== RecognitionStatus.InitialSilenceTimeout)) {\r\n                        if (this.privRecognizerConfig.parameters.getProperty(OutputFormatPropertyName) === OutputFormat[OutputFormat.Simple]) {\r\n                            result = new ConversationTranscriptionResult(\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                                simple.SpeakerId,\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\r\n                            result = new ConversationTranscriptionResult(\r\n                                this.privRequestSession.requestId,\r\n                                resultReason,\r\n                                detailed.RecognitionStatus === RecognitionStatus.Success ? detailed.NBest[0].Display : undefined,\r\n                                detailed.Duration,\r\n                                detailed.Offset,\r\n                                detailed.Language,\r\n                                detailed.LanguageDetectionConfidence,\r\n                                simple.SpeakerId,\r\n                                undefined,\r\n                                detailed.asJson(),\r\n                                resultProps);\r\n                        }\r\n\r\n                        const event: ConversationTranscriptionEventArgs = new ConversationTranscriptionEventArgs(result, result.offset, this.privRequestSession.sessionId);\r\n\r\n                        if (!!this.privConversationTranscriber.transcribed) {\r\n                            try {\r\n                                this.privConversationTranscriber.transcribed(this.privConversationTranscriber, 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                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.privConversationTranscriber.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.privConversationTranscriber.canceled(this.privConversationTranscriber, cancelEvent);\r\n                /* eslint-disable no-empty */\r\n            } catch { }\r\n        }\r\n    }\r\n}\r\n"]}