{"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;IA6GzG,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.\n// Licensed under the MIT license.\n\nimport { IAudioSource } from \"../common/Exports.js\";\nimport {\n    CancellationErrorCode,\n    CancellationReason,\n    OutputFormat,\n    PropertyCollection,\n    PropertyId,\n    ResultReason,\n    ConversationTranscriptionCanceledEventArgs,\n    ConversationTranscriptionEventArgs,\n    ConversationTranscriptionResult,\n    ConversationTranscriber,\n} from \"../sdk/Exports.js\";\nimport {\n    CancellationErrorCodePropertyName,\n    DetailedSpeechPhrase,\n    EnumTranslation,\n    OutputFormatPropertyName,\n    RecognitionStatus,\n    ServiceRecognizerBase,\n    SimpleSpeechPhrase,\n    SpeechHypothesis,\n} from \"./Exports.js\";\nimport { IAuthentication } from \"./IAuthentication.js\";\nimport { IConnectionFactory } from \"./IConnectionFactory.js\";\nimport { RecognizerConfig } from \"./RecognizerConfig.js\";\nimport { SpeechConnectionMessage } from \"./SpeechConnectionMessage.Internal.js\";\nimport { SpeakerDiarization, SpeakerDiarizationMode } from \"./ServiceMessages/PhraseDetection/SpeakerDiarization.js\";\nimport { RecognitionMode } from \"./ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\n\n// eslint-disable-next-line max-classes-per-file\nexport class ConversationTranscriptionServiceRecognizer extends ServiceRecognizerBase {\n\n    private privConversationTranscriber: ConversationTranscriber;\n\n    public constructor(\n        authentication: IAuthentication,\n        connectionFactory: IConnectionFactory,\n        audioSource: IAudioSource,\n        recognizerConfig: RecognizerConfig,\n        conversationTranscriber: ConversationTranscriber) {\n        super(authentication, connectionFactory, audioSource, recognizerConfig, conversationTranscriber);\n        this.privConversationTranscriber = conversationTranscriber;\n        this.setSpeakerDiarizationJson();\n    }\n\n    protected setSpeakerDiarizationJson(): void {\n        if (this.privEnableSpeakerId) {\n            const phraseDetection = this.privSpeechContext.getContext().phraseDetection || {};\n            phraseDetection.mode = RecognitionMode.Conversation;\n            const speakerDiarization: SpeakerDiarization = {};\n            speakerDiarization.mode = SpeakerDiarizationMode.Anonymous;\n            speakerDiarization.audioSessionId = this.privDiarizationSessionId;\n            speakerDiarization.audioOffsetMs = 0;\n            speakerDiarization.diarizeIntermediates = this.privRecognizerConfig.parameters.getProperty(PropertyId.SpeechServiceResponse_DiarizeIntermediateResults, \"false\") === \"true\";\n            phraseDetection.speakerDiarization = speakerDiarization;\n            this.privSpeechContext.getContext().phraseDetection = phraseDetection;\n        }\n    }\n\n    protected async processTypeSpecificMessages(connectionMessage: SpeechConnectionMessage): Promise<boolean> {\n\n        let result: ConversationTranscriptionResult;\n        const resultProps: PropertyCollection = new PropertyCollection();\n        resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, connectionMessage.textBody);\n        let processed: boolean = false;\n\n        switch (connectionMessage.path.toLowerCase()) {\n            case \"speech.hypothesis\":\n            case \"speech.fragment\":\n                const hypothesis: SpeechHypothesis = SpeechHypothesis.fromJSON(connectionMessage.textBody, this.privRequestSession.currentTurnAudioOffset);\n\n                result = new ConversationTranscriptionResult(\n                    this.privRequestSession.requestId,\n                    ResultReason.RecognizingSpeech,\n                    hypothesis.Text,\n                    hypothesis.Duration,\n                    hypothesis.Offset,\n                    hypothesis.Language,\n                    hypothesis.LanguageDetectionConfidence,\n                    hypothesis.SpeakerId,\n                    undefined,\n                    hypothesis.asJson(),\n                    resultProps);\n\n                this.privRequestSession.onHypothesis(hypothesis.Offset);\n\n                const ev = new ConversationTranscriptionEventArgs(result, hypothesis.Duration, this.privRequestSession.sessionId);\n\n                if (!!this.privConversationTranscriber.transcribing) {\n                    try {\n                        this.privConversationTranscriber.transcribing(this.privConversationTranscriber, ev);\n                        /* eslint-disable no-empty */\n                    } catch (error) {\n                        // Not going to let errors in the event handler\n                        // trip things up.\n                    }\n                }\n                processed = true;\n                break;\n            case \"speech.phrase\":\n                const simple: SimpleSpeechPhrase = SimpleSpeechPhrase.fromJSON(connectionMessage.textBody, this.privRequestSession.currentTurnAudioOffset);\n                const resultReason: ResultReason = EnumTranslation.implTranslateRecognitionResult(simple.RecognitionStatus);\n\n                this.privRequestSession.onPhraseRecognized(simple.Offset + simple.Duration);\n\n                if (ResultReason.Canceled === resultReason) {\n                    const cancelReason: CancellationReason = EnumTranslation.implTranslateCancelResult(simple.RecognitionStatus);\n                    const cancellationErrorCode: CancellationErrorCode = EnumTranslation.implTranslateCancelErrorCode(simple.RecognitionStatus);\n\n                    await this.cancelRecognitionLocal(\n                        cancelReason,\n                        cancellationErrorCode,\n                        EnumTranslation.implTranslateErrorDetails(cancellationErrorCode));\n\n                } else {\n                    if (!(this.privRequestSession.isSpeechEnded && resultReason === ResultReason.NoMatch && simple.RecognitionStatus !== RecognitionStatus.InitialSilenceTimeout)) {\n                        if (this.privRecognizerConfig.parameters.getProperty(OutputFormatPropertyName) === OutputFormat[OutputFormat.Simple]) {\n                            result = new ConversationTranscriptionResult(\n                                this.privRequestSession.requestId,\n                                resultReason,\n                                simple.DisplayText,\n                                simple.Duration,\n                                simple.Offset,\n                                simple.Language,\n                                simple.LanguageDetectionConfidence,\n                                simple.SpeakerId,\n                                undefined,\n                                simple.asJson(),\n                                resultProps);\n                        } else {\n                            const detailed: DetailedSpeechPhrase = DetailedSpeechPhrase.fromJSON(connectionMessage.textBody, this.privRequestSession.currentTurnAudioOffset);\n\n                            result = new ConversationTranscriptionResult(\n                                this.privRequestSession.requestId,\n                                resultReason,\n                                detailed.RecognitionStatus === RecognitionStatus.Success ? detailed.NBest[0].Display : undefined,\n                                detailed.Duration,\n                                detailed.Offset,\n                                detailed.Language,\n                                detailed.LanguageDetectionConfidence,\n                                simple.SpeakerId,\n                                undefined,\n                                detailed.asJson(),\n                                resultProps);\n                        }\n\n                        const event: ConversationTranscriptionEventArgs = new ConversationTranscriptionEventArgs(result, result.offset, this.privRequestSession.sessionId);\n\n                        if (!!this.privConversationTranscriber.transcribed) {\n                            try {\n                                this.privConversationTranscriber.transcribed(this.privConversationTranscriber, event);\n                                /* eslint-disable no-empty */\n                            } catch (error) {\n                                // Not going to let errors in the event handler\n                                // trip things up.\n                            }\n                        }\n                    }\n                }\n                processed = true;\n                break;\n            default:\n                break;\n        }\n        return processed;\n    }\n\n    // Cancels recognition.\n    protected cancelRecognition(\n        sessionId: string,\n        requestId: string,\n        cancellationReason: CancellationReason,\n        errorCode: CancellationErrorCode,\n        error: string): void {\n\n        const properties: PropertyCollection = new PropertyCollection();\n        properties.setProperty(CancellationErrorCodePropertyName, CancellationErrorCode[errorCode]);\n\n        if (!!this.privConversationTranscriber.canceled) {\n            const cancelEvent: ConversationTranscriptionCanceledEventArgs = new ConversationTranscriptionCanceledEventArgs(\n                cancellationReason,\n                error,\n                errorCode,\n                undefined,\n                sessionId);\n            try {\n                this.privConversationTranscriber.canceled(this.privConversationTranscriber, cancelEvent);\n                /* eslint-disable no-empty */\n            } catch { }\n        }\n    }\n}\n"]}