@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
193 lines (191 loc) • 7.93 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var DialogConnectorFactory_1 = require("../common.speech/DialogConnectorFactory");
var Exports_1 = require("../common.speech/Exports");
var Contracts_1 = require("./Contracts");
var Exports_2 = require("./Exports");
var PropertyId_1 = require("./PropertyId");
/**
* Dialog Service Connector
* @class DialogServiceConnector
*/
var DialogServiceConnector = /** @class */ (function (_super) {
__extends(DialogServiceConnector, _super);
/**
* 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
*/
function DialogServiceConnector(dialogConfig, audioConfig) {
var _this = this;
var dialogServiceConfigImpl = dialogConfig;
Contracts_1.Contracts.throwIfNull(dialogConfig, "dialogConfig");
_this = _super.call(this, audioConfig, dialogServiceConfigImpl.properties, new DialogConnectorFactory_1.DialogConnectionFactory()) || this;
_this.privIsDisposed = false;
_this.privProperties = dialogServiceConfigImpl.properties.clone();
var agentConfig = _this.buildAgentConfig();
_this.privReco.agentConfig.set(agentConfig);
return _this;
}
/**
* 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
*/
DialogServiceConnector.prototype.connect = function () {
this.privReco.connect();
};
/**
* 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.
*/
DialogServiceConnector.prototype.disconnect = function () {
this.privReco.disconnect();
};
Object.defineProperty(DialogServiceConnector.prototype, "authorizationToken", {
/**
* Gets the authorization token used to communicate with the service.
* @member DialogServiceConnector.prototype.authorizationToken
* @function
* @public
* @returns {string} Authorization token.
*/
get: function () {
return this.properties.getProperty(PropertyId_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: function (token) {
Contracts_1.Contracts.throwIfNullOrWhitespace(token, "token");
this.properties.setProperty(PropertyId_1.PropertyId.SpeechServiceAuthorization_Token, token);
},
enumerable: true,
configurable: true
});
Object.defineProperty(DialogServiceConnector.prototype, "properties", {
/**
* 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: function () {
return this.privProperties;
},
enumerable: true,
configurable: true
});
/**
* 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.
*/
DialogServiceConnector.prototype.listenOnceAsync = function (cb, err) {
var _this = this;
try {
Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed);
this.connect();
this.implRecognizerStop();
this.implRecognizerStart(Exports_1.RecognitionMode.Conversation, function (e) {
_this.implRecognizerStop();
if (!!cb) {
cb(e);
}
}, function (e) {
_this.implRecognizerStop();
if (!!err) {
err(e);
}
});
}
catch (error) {
if (!!err) {
if (error instanceof Error) {
var typedError = error;
err(typedError.name + ": " + typedError.message);
}
else {
err(error);
}
}
// Destroy the recognizer.
this.dispose(true);
}
};
DialogServiceConnector.prototype.sendActivityAsync = function (activity) {
this.privReco.sendMessage(activity);
};
/**
* closes all external resources held by an instance of this class.
* @member DialogServiceConnector.prototype.close
* @function
* @public
*/
DialogServiceConnector.prototype.close = function () {
Contracts_1.Contracts.throwIfDisposed(this.privIsDisposed);
this.dispose(true);
};
DialogServiceConnector.prototype.dispose = function (disposing) {
if (this.privIsDisposed) {
return;
}
if (disposing) {
this.implRecognizerStop();
this.privIsDisposed = true;
_super.prototype.dispose.call(this, disposing);
}
};
DialogServiceConnector.prototype.createRecognizerConfig = function (speechConfig) {
return new Exports_1.RecognizerConfig(speechConfig, this.privProperties);
};
DialogServiceConnector.prototype.createServiceRecognizer = function (authentication, connectionFactory, audioConfig, recognizerConfig) {
var audioSource = audioConfig;
return new Exports_1.DialogServiceAdapter(authentication, connectionFactory, audioSource, recognizerConfig, this);
};
DialogServiceConnector.prototype.buildAgentConfig = function () {
var communicationType = this.properties.getProperty("Conversation_Communication_Type", "Default");
return {
botInfo: {
commType: communicationType,
connectionId: this.properties.getProperty(PropertyId_1.PropertyId.Conversation_ApplicationId),
conversationId: undefined
},
version: 0.2
};
};
return DialogServiceConnector;
}(Exports_2.Recognizer));
exports.DialogServiceConnector = DialogServiceConnector;
//# sourceMappingURL=DialogServiceConnector.js.map