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AAI;IAW3C,OAAO,CAAC,kBAAkB;CAsD7B","file":"IntentRecognizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport {\n    AddedLmIntent,\n    IAuthentication,\n    IConnectionFactory,\n    IntentConnectionFactory,\n    IntentServiceRecognizer,\n    RecognitionMode,\n    RecognizerConfig,\n    ServiceRecognizerBase,\n    SpeechServiceConfig,\n} from \"../common.speech/Exports\";\nimport { AudioConfigImpl } from \"./Audio/AudioConfig\";\nimport { Contracts } from \"./Contracts\";\nimport {\n    AudioConfig,\n    IntentRecognitionCanceledEventArgs,\n    IntentRecognitionEventArgs,\n    IntentRecognitionResult,\n    KeywordRecognitionModel,\n    LanguageUnderstandingModel,\n    PropertyCollection,\n    PropertyId,\n    Recognizer,\n    SpeechConfig,\n} from \"./Exports\";\nimport { LanguageUnderstandingModelImpl } from \"./LanguageUnderstandingModel\";\nimport { SpeechConfigImpl } from \"./SpeechConfig\";\n\n/**\n * Intent recognizer.\n * @class\n */\nexport class IntentRecognizer extends Recognizer {\n    private privDisposedIntentRecognizer: boolean;\n    private privAddedIntents: string[][];\n    private privAddedLmIntents: { [id: string]: AddedLmIntent; };\n    private privUmbrellaIntent: AddedLmIntent;\n\n    /**\n     * Initializes an instance of the IntentRecognizer.\n     * @constructor\n     * @param {SpeechConfig} speechConfig - The set of configuration properties.\n     * @param {AudioConfig} audioConfig - An optional audio input config associated with the recognizer\n     */\n    public constructor(speechConfig: SpeechConfig, audioConfig?: AudioConfig) {\n        Contracts.throwIfNullOrUndefined(speechConfig, \"speechConfig\");\n        const configImpl: SpeechConfigImpl = speechConfig as SpeechConfigImpl;\n        Contracts.throwIfNullOrUndefined(configImpl, \"speechConfig\");\n\n        super(audioConfig, configImpl.properties, new IntentConnectionFactory());\n        this.privAddedIntents = [];\n        this.privAddedLmIntents = {};\n\n        this.privDisposedIntentRecognizer = false;\n        this.privProperties = configImpl.properties;\n\n        Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage), PropertyId[PropertyId.SpeechServiceConnection_RecoLanguage]);\n    }\n\n    /**\n     * The event recognizing signals that an intermediate recognition result is received.\n     * @member IntentRecognizer.prototype.recognizing\n     * @function\n     * @public\n     */\n    public recognizing: (sender: IntentRecognizer, event: IntentRecognitionEventArgs) => void;\n\n    /**\n     * The event recognized signals that a final recognition result is received.\n     * @member IntentRecognizer.prototype.recognized\n     * @function\n     * @public\n     */\n    public recognized: (sender: IntentRecognizer, event: IntentRecognitionEventArgs) => void;\n\n    /**\n     * The event canceled signals that an error occurred during recognition.\n     * @member IntentRecognizer.prototype.canceled\n     * @function\n     * @public\n     */\n    public canceled: (sender: IntentRecognizer, event: IntentRecognitionCanceledEventArgs) => void;\n\n    /**\n     * Gets the spoken language of recognition.\n     * @member IntentRecognizer.prototype.speechRecognitionLanguage\n     * @function\n     * @public\n     * @returns {string} the spoken language of recognition.\n     */\n    public get speechRecognitionLanguage(): string {\n        Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n\n        return this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);\n    }\n\n    /**\n     * Gets the authorization token used to communicate with the service.\n     * @member IntentRecognizer.prototype.authorizationToken\n     * @function\n     * @public\n     * @returns {string} Authorization token.\n     */\n    public get authorizationToken(): string {\n        return this.properties.getProperty(PropertyId.SpeechServiceAuthorization_Token);\n    }\n\n    /**\n     * Gets/Sets the authorization token used to communicate with the service.\n     * Note: Please use a token derived from your LanguageUnderstanding subscription key for the Intent recognizer.\n     * @member IntentRecognizer.prototype.authorizationToken\n     * @function\n     * @public\n     * @param {string} value - Authorization token.\n     */\n    public set authorizationToken(value: string) {\n        this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value);\n    }\n\n    /**\n     * The collection of properties and their values defined for this IntentRecognizer.\n     * @member IntentRecognizer.prototype.properties\n     * @function\n     * @public\n     * @returns {PropertyCollection} The collection of properties and their\n     *          values defined for this IntentRecognizer.\n     */\n    public get properties(): PropertyCollection {\n        return this.privProperties;\n    }\n\n    /**\n     * Starts intent recognition, and stops after the first utterance is recognized.\n     * The task returns the recognition text and intent as result.\n     * Note: RecognizeOnceAsync() returns when the first utterance has been recognized,\n     *       so it is suitable only for single shot recognition like command or query.\n     *       For long-running recognition, use StartContinuousRecognitionAsync() instead.\n     * @member IntentRecognizer.prototype.recognizeOnceAsync\n     * @function\n     * @public\n     * @param cb - Callback that received the recognition has finished with an IntentRecognitionResult.\n     * @param err - Callback invoked in case of an error.\n     */\n    public recognizeOnceAsync(cb?: (e: IntentRecognitionResult) => void, err?: (e: string) => void): void {\n        try {\n            Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n\n            this.implRecognizerStop();\n\n            if (Object.keys(this.privAddedLmIntents).length !== 0 || undefined !== this.privUmbrellaIntent) {\n                const context: IIntentContext = this.buildSpeechContext();\n\n                this.privReco.speechContext.setSection(\"intent\", context.Intent);\n                this.privReco.dynamicGrammar.addReferenceGrammar(context.ReferenceGrammars);\n\n                const intentReco: IntentServiceRecognizer = this.privReco as IntentServiceRecognizer;\n                intentReco.setIntents(this.privAddedLmIntents, this.privUmbrellaIntent);\n            }\n\n            this.implRecognizerStart(RecognitionMode.Interactive, (e: IntentRecognitionResult) => {\n                this.implRecognizerStop();\n                if (!!cb) {\n                    cb(e);\n                }\n            }, (e: string) => {\n                this.implRecognizerStop();\n                if (!!err) {\n                    err(e);\n                }\n            });\n\n        } catch (error) {\n            if (!!err) {\n                if (error instanceof Error) {\n                    const typedError: Error = error as Error;\n                    err(typedError.name + \": \" + typedError.message);\n                } else {\n                    err(error);\n                }\n            }\n\n            // Destroy the recognizer.\n            this.dispose(true);\n        }\n    }\n\n    /**\n     * Starts speech recognition, until stopContinuousRecognitionAsync() is called.\n     * User must subscribe to events to receive recognition results.\n     * @member IntentRecognizer.prototype.startContinuousRecognitionAsync\n     * @function\n     * @public\n     * @param cb - Callback invoked once the recognition has started.\n     * @param err - Callback invoked in case of an error.\n     */\n    public startContinuousRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\n        try {\n            Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n\n            this.implRecognizerStop();\n\n            if (Object.keys(this.privAddedLmIntents).length !== 0) {\n                const context: IIntentContext = this.buildSpeechContext();\n\n                this.privReco.speechContext.setSection(\"intent\", context.Intent);\n                this.privReco.dynamicGrammar.addReferenceGrammar(context.ReferenceGrammars);\n\n                const intentReco: IntentServiceRecognizer = this.privReco as IntentServiceRecognizer;\n                intentReco.setIntents(this.privAddedLmIntents, this.privUmbrellaIntent);\n            }\n\n            this.implRecognizerStart(RecognitionMode.Conversation, undefined, undefined);\n\n            // report result to promise.\n            if (!!cb) {\n                try {\n                    cb();\n                } catch (e) {\n                    if (!!err) {\n                        err(e);\n                    }\n                }\n                cb = undefined;\n            }\n        } catch (error) {\n            if (!!err) {\n                if (error instanceof Error) {\n                    const typedError: Error = error as Error;\n                    err(typedError.name + \": \" + typedError.message);\n                } else {\n                    err(error);\n                }\n            }\n\n            // Destroy the recognizer.\n            this.dispose(true);\n        }\n    }\n\n    /**\n     * Stops continuous intent recognition.\n     * @member IntentRecognizer.prototype.stopContinuousRecognitionAsync\n     * @function\n     * @public\n     * @param cb - Callback invoked once the recognition has stopped.\n     * @param err - Callback invoked in case of an error.\n     */\n    public stopContinuousRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\n        try {\n            Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n\n            this.implRecognizerStop();\n\n            if (!!cb) {\n                try {\n                    cb();\n                } catch (e) {\n                    if (!!err) {\n                        err(e);\n                    }\n                }\n            }\n        } catch (error) {\n            if (!!err) {\n                if (error instanceof Error) {\n                    const typedError: Error = error as Error;\n                    err(typedError.name + \": \" + typedError.message);\n                } else {\n                    err(error);\n                }\n            }\n\n            // Destroy the recognizer.\n            this.dispose(true);\n        }\n    }\n\n    /**\n     * Starts speech recognition with keyword spotting, until stopKeywordRecognitionAsync() is called.\n     * User must subscribe to events to receive recognition results.\n     * Note: Key word spotting functionality is only available on the Speech Devices SDK.\n     *       This functionality is currently not included in the SDK itself.\n     * @member IntentRecognizer.prototype.startKeywordRecognitionAsync\n     * @function\n     * @public\n     * @param {KeywordRecognitionModel} model - The keyword recognition model that specifies the keyword to be recognized.\n     * @param cb - Callback invoked once the recognition has started.\n     * @param err - Callback invoked in case of an error.\n     */\n    public startKeywordRecognitionAsync(model: KeywordRecognitionModel, cb?: () => void, err?: (e: string) => void): void {\n        Contracts.throwIfNull(model, \"model\");\n\n        if (!!err) {\n            err(\"Not yet implemented.\");\n        }\n    }\n\n    /**\n     * Stops continuous speech recognition.\n     * Note: Key word spotting functionality is only available on the Speech Devices SDK.\n     *       This functionality is currently not included in the SDK itself.\n     * @member IntentRecognizer.prototype.stopKeywordRecognitionAsync\n     * @function\n     * @public\n     * @param cb - Callback invoked once the recognition has stopped.\n     * @param err - Callback invoked in case of an error.\n     */\n    public stopKeywordRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\n        if (!!cb) {\n            cb();\n        }\n    }\n\n    /**\n     * Adds a phrase that should be recognized as intent.\n     * @member IntentRecognizer.prototype.addIntent\n     * @function\n     * @public\n     * @param {string} intentId - A String that represents the identifier of the intent to be recognized.\n     * @param {string} phrase - A String that specifies the phrase representing the intent.\n     */\n    public addIntent(simplePhrase: string, intentId?: string): void {\n        Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n        Contracts.throwIfNullOrWhitespace(intentId, \"intentId\");\n        Contracts.throwIfNullOrWhitespace(simplePhrase, \"simplePhrase\");\n\n        this.privAddedIntents.push([intentId, simplePhrase]);\n    }\n\n    /**\n     * Adds an intent from Language Understanding service for recognition.\n     * @member IntentRecognizer.prototype.addIntentWithLanguageModel\n     * @function\n     * @public\n     * @param {string} intentId - A String that represents the identifier of the intent\n     *        to be recognized. Ignored if intentName is empty.\n     * @param {string} model - The intent model from Language Understanding service.\n     * @param {string} intentName - The intent name defined in the intent model. If it\n     *        is empty, all intent names defined in the model will be added.\n     */\n    public addIntentWithLanguageModel(intentId: string, model: LanguageUnderstandingModel, intentName?: string): void {\n        Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n        Contracts.throwIfNullOrWhitespace(intentId, \"intentId\");\n        Contracts.throwIfNull(model, \"model\");\n\n        const modelImpl: LanguageUnderstandingModelImpl = model as LanguageUnderstandingModelImpl;\n        Contracts.throwIfNullOrWhitespace(modelImpl.appId, \"model.appId\");\n\n        this.privAddedLmIntents[intentId] = new AddedLmIntent(modelImpl, intentName);\n    }\n\n    /**\n     * @summary Adds all intents from the specified Language Understanding Model.\n     * @member IntentRecognizer.prototype.addAllIntents\n     * @function\n     * @public\n     * @function\n     * @public\n     * @param {LanguageUnderstandingModel} model - The language understanding model containing the intents.\n     * @param {string} intentId - A custom id String to be returned in the IntentRecognitionResult's getIntentId() method.\n     */\n    public addAllIntents(model: LanguageUnderstandingModel, intentId?: string): void {\n        Contracts.throwIfNull(model, \"model\");\n\n        const modelImpl: LanguageUnderstandingModelImpl = model as LanguageUnderstandingModelImpl;\n        Contracts.throwIfNullOrWhitespace(modelImpl.appId, \"model.appId\");\n\n        this.privUmbrellaIntent = new AddedLmIntent(modelImpl, intentId);\n    }\n\n    /**\n     * closes all external resources held by an instance of this class.\n     * @member IntentRecognizer.prototype.close\n     * @function\n     * @public\n     */\n    public close(): void {\n        Contracts.throwIfDisposed(this.privDisposedIntentRecognizer);\n\n        this.dispose(true);\n    }\n\n    protected createRecognizerConfig(speechConfig: SpeechServiceConfig): RecognizerConfig {\n        return new RecognizerConfig(speechConfig, this.properties);\n    }\n\n    protected createServiceRecognizer(authentication: IAuthentication, connectionFactory: IConnectionFactory, audioConfig: AudioConfig, recognizerConfig: RecognizerConfig): ServiceRecognizerBase {\n        const audioImpl: AudioConfigImpl = audioConfig as AudioConfigImpl;\n        return new IntentServiceRecognizer(authentication, connectionFactory, audioImpl, recognizerConfig, this);\n    }\n\n    protected dispose(disposing: boolean): void {\n        if (this.privDisposedIntentRecognizer) {\n            return;\n        }\n\n        if (disposing) {\n            this.privDisposedIntentRecognizer = true;\n            super.dispose(disposing);\n        }\n    }\n\n    private buildSpeechContext(): IIntentContext {\n        let appId: string;\n        let region: string;\n        let subscriptionKey: string;\n        const refGrammers: string[] = [];\n\n        if (undefined !== this.privUmbrellaIntent) {\n            appId = this.privUmbrellaIntent.modelImpl.appId;\n            region = this.privUmbrellaIntent.modelImpl.region;\n            subscriptionKey = this.privUmbrellaIntent.modelImpl.subscriptionKey;\n        }\n\n        // Build the reference grammer array.\n        for (const intentId of Object.keys(this.privAddedLmIntents)) {\n            const addedLmIntent: AddedLmIntent = this.privAddedLmIntents[intentId];\n\n            // validate all the same model, region, and key...\n            if (appId === undefined) {\n                appId = addedLmIntent.modelImpl.appId;\n            } else {\n                if (appId !== addedLmIntent.modelImpl.appId) {\n                    throw new Error(\"Intents must all be from the same LUIS model\");\n                }\n            }\n\n            if (region === undefined) {\n                region = addedLmIntent.modelImpl.region;\n            } else {\n                if (region !== addedLmIntent.modelImpl.region) {\n                    throw new Error(\"Intents must all be from the same LUIS model in a single region\");\n                }\n            }\n\n            if (subscriptionKey === undefined) {\n                subscriptionKey = addedLmIntent.modelImpl.subscriptionKey;\n            } else {\n                if (subscriptionKey !== addedLmIntent.modelImpl.subscriptionKey) {\n                    throw new Error(\"Intents must all use the same subscription key\");\n                }\n            }\n\n            const grammer: string = \"luis/\" + appId + \"-PRODUCTION#\" + intentId;\n            refGrammers.push(grammer);\n        }\n\n        return {\n            Intent: {\n                id: appId,\n                key: (subscriptionKey === undefined) ? this.privProperties.getProperty(PropertyId[PropertyId.SpeechServiceConnection_Key]) : subscriptionKey,\n                provider: \"LUIS\",\n            },\n            ReferenceGrammars: (undefined === this.privUmbrellaIntent) ? refGrammers : [\"luis/\" + appId + \"-PRODUCTION\"],\n        };\n    }\n}\n\ninterface IIntentContext {\n    Intent: {\n        id: string,\n        key: string,\n        provider: string,\n    };\n    ReferenceGrammars: string[];\n}\n"]}