@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
164 lines (162 loc) • 6.05 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { DialogConnectionFactory } from "../common.speech/DialogConnectorFactory";
import { DialogServiceAdapter, RecognitionMode, RecognizerConfig, } from "../common.speech/Exports";
import { Contracts } from "./Contracts";
import { Recognizer } from "./Exports";
import { PropertyId } from "./PropertyId";
/**
* Dialog Service Connector
* @class DialogServiceConnector
*/
export class DialogServiceConnector extends 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.throwIfNull(dialogConfig, "dialogConfig");
super(audioConfig, dialogServiceConfigImpl.properties, new DialogConnectionFactory());
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() {
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.
*/
disconnect() {
this.privReco.disconnect();
}
/**
* 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.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.throwIfNullOrWhitespace(token, "token");
this.properties.setProperty(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;
}
/**
* 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) {
try {
Contracts.throwIfDisposed(this.privIsDisposed);
this.connect();
this.implRecognizerStop();
this.implRecognizerStart(RecognitionMode.Conversation, (e) => {
this.implRecognizerStop();
if (!!cb) {
cb(e);
}
}, (e) => {
this.implRecognizerStop();
if (!!err) {
err(e);
}
});
}
catch (error) {
if (!!err) {
if (error instanceof Error) {
const typedError = error;
err(typedError.name + ": " + typedError.message);
}
else {
err(error);
}
}
// Destroy the recognizer.
this.dispose(true);
}
}
sendActivityAsync(activity) {
this.privReco.sendMessage(activity);
}
/**
* closes all external resources held by an instance of this class.
* @member DialogServiceConnector.prototype.close
* @function
* @public
*/
close() {
Contracts.throwIfDisposed(this.privIsDisposed);
this.dispose(true);
}
dispose(disposing) {
if (this.privIsDisposed) {
return;
}
if (disposing) {
this.implRecognizerStop();
this.privIsDisposed = true;
super.dispose(disposing);
}
}
createRecognizerConfig(speechConfig) {
return new RecognizerConfig(speechConfig, this.privProperties);
}
createServiceRecognizer(authentication, connectionFactory, audioConfig, recognizerConfig) {
const audioSource = audioConfig;
return new DialogServiceAdapter(authentication, connectionFactory, audioSource, recognizerConfig, this);
}
buildAgentConfig() {
const communicationType = this.properties.getProperty("Conversation_Communication_Type", "Default");
return {
botInfo: {
commType: communicationType,
connectionId: this.properties.getProperty(PropertyId.Conversation_ApplicationId),
conversationId: undefined
},
version: 0.2
};
}
}
//# sourceMappingURL=DialogServiceConnector.js.map