UNPKG

microsoft-cognitiveservices-speech-sdk

Version:
183 lines (181 loc) 8.22 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.DialogServiceConnector = void 0; const DialogConnectorFactory_js_1 = require("../common.speech/DialogConnectorFactory.js"); const Exports_js_1 = require("../common.speech/Exports.js"); const PhraseDetectionContext_js_1 = require("../common.speech/ServiceMessages/PhraseDetection/PhraseDetectionContext.js"); const Exports_js_2 = require("../common/Exports.js"); const Contracts_js_1 = require("./Contracts.js"); const Exports_js_3 = require("./Exports.js"); const PropertyId_js_1 = require("./PropertyId.js"); /** * Dialog Service Connector * @class DialogServiceConnector */ class DialogServiceConnector extends Exports_js_3.Recognizer { /** * Initializes an instance of the DialogServiceConnector. * @constructor * @param {DialogServiceConfig} dialogConfig - Set of properties to configure this recognizer. * @param {AudioConfig} audioConfig - An optional audio config associated with the recognizer */ constructor(dialogConfig, audioConfig) { const dialogServiceConfigImpl = dialogConfig; Contracts_js_1.Contracts.throwIfNull(dialogConfig, "dialogConfig"); super(audioConfig, dialogServiceConfigImpl.properties, new DialogConnectorFactory_js_1.DialogConnectionFactory()); this.isTurnComplete = true; this.privIsDisposed = false; this.privProperties = dialogServiceConfigImpl.properties.clone(); const agentConfig = this.buildAgentConfig(); this.privReco.agentConfig.set(agentConfig); } /** * Starts a connection to the service. * Users can optionally call connect() to manually set up a connection in advance, before starting interactions. * * Note: On return, the connection might not be ready yet. Please subscribe to the Connected event to * be notified when the connection is established. * @member DialogServiceConnector.prototype.connect * @function * @public */ connect(cb, err) { (0, Exports_js_2.marshalPromiseToCallbacks)(this.privReco.connect(), cb, err); } /** * Closes the connection the service. * Users can optionally call disconnect() to manually shutdown the connection of the associated DialogServiceConnector. * * If disconnect() is called during a recognition, recognition will fail and cancel with an error. */ disconnect(cb, err) { (0, Exports_js_2.marshalPromiseToCallbacks)(this.privReco.disconnect(), cb, err); } /** * Gets the authorization token used to communicate with the service. * @member DialogServiceConnector.prototype.authorizationToken * @function * @public * @returns {string} Authorization token. */ get authorizationToken() { return this.properties.getProperty(PropertyId_js_1.PropertyId.SpeechServiceAuthorization_Token); } /** * Sets the authorization token used to communicate with the service. * @member DialogServiceConnector.prototype.authorizationToken * @function * @public * @param {string} token - Authorization token. */ set authorizationToken(token) { Contracts_js_1.Contracts.throwIfNullOrWhitespace(token, "token"); this.properties.setProperty(PropertyId_js_1.PropertyId.SpeechServiceAuthorization_Token, token); } /** * The collection of properties and their values defined for this DialogServiceConnector. * @member DialogServiceConnector.prototype.properties * @function * @public * @returns {PropertyCollection} The collection of properties and their values defined for this DialogServiceConnector. */ get properties() { return this.privProperties; } /** Gets the template for the activity generated by service from speech. * Properties from the template will be stamped on the generated activity. * It can be empty */ get speechActivityTemplate() { return this.properties.getProperty(PropertyId_js_1.PropertyId.Conversation_Speech_Activity_Template); } /** Sets the template for the activity generated by service from speech. * Properties from the template will be stamped on the generated activity. * It can be null or empty. * Note: it has to be a valid Json object. */ set speechActivityTemplate(speechActivityTemplate) { this.properties.setProperty(PropertyId_js_1.PropertyId.Conversation_Speech_Activity_Template, speechActivityTemplate); } /** * Starts recognition and stops after the first utterance is recognized. * @member DialogServiceConnector.prototype.listenOnceAsync * @function * @public * @param cb - Callback that received the result when the reco has completed. * @param err - Callback invoked in case of an error. */ listenOnceAsync(cb, err) { if (this.isTurnComplete) { Contracts_js_1.Contracts.throwIfDisposed(this.privIsDisposed); const callbackHolder = async () => { await this.privReco.connect(); await this.implRecognizerStop(); this.isTurnComplete = false; const ret = new Exports_js_2.Deferred(); await this.privReco.recognize(PhraseDetectionContext_js_1.RecognitionMode.Conversation, ret.resolve, ret.reject); const e = await ret.promise; await this.implRecognizerStop(); return e; }; const retPromise = callbackHolder(); retPromise.catch(() => { // Destroy the recognizer. // We've done all we can here. // eslint-disable-next-line @typescript-eslint/no-empty-function this.dispose(true).catch(() => { }); }); (0, Exports_js_2.marshalPromiseToCallbacks)(retPromise.finally(() => { this.isTurnComplete = true; }), cb, err); } } sendActivityAsync(activity, cb, errCb) { (0, Exports_js_2.marshalPromiseToCallbacks)(this.privReco.sendMessage(activity), cb, errCb); } /** * closes all external resources held by an instance of this class. * @member DialogServiceConnector.prototype.close * @function * @public */ close(cb, err) { Contracts_js_1.Contracts.throwIfDisposed(this.privIsDisposed); (0, Exports_js_2.marshalPromiseToCallbacks)(this.dispose(true), cb, err); } async dispose(disposing) { if (this.privIsDisposed) { return; } if (disposing) { this.privIsDisposed = true; await this.implRecognizerStop(); await super.dispose(disposing); } } createRecognizerConfig(speechConfig) { return new Exports_js_1.RecognizerConfig(speechConfig, this.privProperties); } createServiceRecognizer(authentication, connectionFactory, audioConfig, recognizerConfig) { const audioSource = audioConfig; return new Exports_js_1.DialogServiceAdapter(authentication, connectionFactory, audioSource, recognizerConfig, this); } buildAgentConfig() { const communicationType = this.properties.getProperty("Conversation_Communication_Type", "Default"); return { botInfo: { commType: communicationType, commandsCulture: undefined, connectionId: this.properties.getProperty(PropertyId_js_1.PropertyId.Conversation_Agent_Connection_Id), conversationId: this.properties.getProperty(PropertyId_js_1.PropertyId.Conversation_Conversation_Id, undefined), fromId: this.properties.getProperty(PropertyId_js_1.PropertyId.Conversation_From_Id, undefined), ttsAudioFormat: this.properties.getProperty(PropertyId_js_1.PropertyId.SpeechServiceConnection_SynthOutputFormat, undefined) }, version: 0.2 }; } } exports.DialogServiceConnector = DialogServiceConnector; //# sourceMappingURL=DialogServiceConnector.js.map