@voice-ping/cognitive-services-speech
Version:
VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft
194 lines (192 loc) • 7.75 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";
import { SpeechSynthesisOutputFormat } from "./SpeechSynthesisOutputFormat";
/**
* 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.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() {
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;
}
/** 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.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.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) {
try {
Contracts.throwIfDisposed(this.privIsDisposed);
this.connect();
this.implRecognizerStop();
this.isTurnComplete = false;
this.privReco.recognize(RecognitionMode.Conversation, (e) => {
this.implRecognizerStop();
this.isTurnComplete = true;
if (!!cb) {
cb(e);
}
}, (e) => {
this.implRecognizerStop();
this.isTurnComplete = true;
if (!!err) {
err(e);
}
/* tslint:disable:no-empty */
}).on((_) => { }, (error) => {
if (!!err) {
err(error);
}
});
}
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,
commandsCulture: undefined,
connectionId: this.properties.getProperty(PropertyId.Conversation_ApplicationId),
conversationId: this.properties.getProperty(PropertyId.Conversation_Conversation_Id, undefined),
fromId: this.properties.getProperty(PropertyId.Conversation_From_Id, undefined),
ttsAudioFormat: SpeechSynthesisOutputFormat[this.properties.getProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, undefined)]
},
version: 0.2
};
}
}
//# sourceMappingURL=DialogServiceConnector.js.map