@hahnpro/ms-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 13.5 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/DialogServiceConnector.ts"],"names":[],"mappings":"AAIA,OAAO,EAGH,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACtB,MAAM,6BAA6B,CAAC;AAMrC,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAG3E,OAAO,EAAE,mBAAmB,EAA2B,MAAM,0BAA0B,CAAC;AACxF,OAAO,EACH,WAAW,EACX,kBAAkB,EAClB,UAAU,EACV,kCAAkC,EAClC,0BAA0B,EAC1B,uBAAuB,EAC1B,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAE/E;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,UAAU;IAClD,OAAO,CAAC,cAAc,CAAU;IAChC,OAAO,CAAC,cAAc,CAAU;IAEhC;;;;;OAKG;gBACgB,YAAY,EAAE,mBAAmB,EAAE,WAAW,CAAC,EAAE,WAAW;IAc/E;;;;;OAKG;IACI,WAAW,EAAE,CAAC,MAAM,EAAE,sBAAsB,EAAE,KAAK,EAAE,0BAA0B,KAAK,IAAI,CAAC;IAEhG;;;;;OAKG;IACI,UAAU,EAAE,CAAC,MAAM,EAAE,sBAAsB,EAAE,KAAK,EAAE,0BAA0B,KAAK,IAAI,CAAC;IAE/F;;;;;OAKG;IACI,QAAQ,EAAE,CAAC,MAAM,EAAE,sBAAsB,EAAE,KAAK,EAAE,kCAAkC,KAAK,IAAI,CAAC;IAErG;;;;;OAKG;IACI,gBAAgB,EAAE,CAAC,MAAM,EAAE,sBAAsB,EAAE,KAAK,EAAE,yBAAyB,KAAK,IAAI,CAAC;IAEpG;;;;;;;;OAQG;IACI,kBAAkB,EAAE,CAAC,MAAM,EAAE,sBAAsB,EAAE,KAAK,EAAE,2BAA2B,KAAK,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/B,iBAAiB,EAAE,kBAAkB,EACrC,WAAW,EAAE,WAAW,EACxB,gBAAgB,EAAE,gBAAgB,GAAG,qBAAqB;IAO9D,OAAO,CAAC,gBAAgB;CAe3B","file":"DialogServiceConnector.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport { DialogConnectionFactory } from \"../common.speech/DialogConnectorFactory.js\";\nimport {\n DialogServiceAdapter,\n IAgentConfig,\n IAuthentication,\n IConnectionFactory,\n RecognizerConfig,\n ServiceRecognizerBase,\n SpeechServiceConfig\n} from \"../common.speech/Exports.js\";\nimport { RecognitionMode } from \"../common.speech/ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\nimport {\n Deferred,\n marshalPromiseToCallbacks\n} from \"../common/Exports.js\";\nimport { ActivityReceivedEventArgs } from \"./ActivityReceivedEventArgs.js\";\nimport { AudioConfigImpl } from \"./Audio/AudioConfig.js\";\nimport { Contracts } from \"./Contracts.js\";\nimport { DialogServiceConfig, DialogServiceConfigImpl } from \"./DialogServiceConfig.js\";\nimport {\n AudioConfig,\n PropertyCollection,\n Recognizer,\n SpeechRecognitionCanceledEventArgs,\n SpeechRecognitionEventArgs,\n SpeechRecognitionResult\n} from \"./Exports.js\";\nimport { PropertyId } from \"./PropertyId.js\";\nimport { TurnStatusReceivedEventArgs } from \"./TurnStatusReceivedEventArgs.js\";\n\n/**\n * Dialog Service Connector\n * @class DialogServiceConnector\n */\nexport class DialogServiceConnector extends Recognizer {\n private privIsDisposed: boolean;\n private isTurnComplete: boolean;\n\n /**\n * Initializes an instance of the DialogServiceConnector.\n * @constructor\n * @param {DialogServiceConfig} dialogConfig - Set of properties to configure this recognizer.\n * @param {AudioConfig} audioConfig - An optional audio config associated with the recognizer\n */\n public constructor(dialogConfig: DialogServiceConfig, audioConfig?: AudioConfig) {\n const dialogServiceConfigImpl = dialogConfig as DialogServiceConfigImpl;\n Contracts.throwIfNull(dialogConfig, \"dialogConfig\");\n\n super(audioConfig, dialogServiceConfigImpl.properties, new DialogConnectionFactory());\n\n this.isTurnComplete = true;\n this.privIsDisposed = false;\n this.privProperties = dialogServiceConfigImpl.properties.clone();\n\n const agentConfig = this.buildAgentConfig();\n this.privReco.agentConfig.set(agentConfig);\n }\n\n /**\n * The event recognizing signals that an intermediate recognition result is received.\n * @member DialogServiceConnector.prototype.recognizing\n * @function\n * @public\n */\n public recognizing: (sender: DialogServiceConnector, event: SpeechRecognitionEventArgs) => void;\n\n /**\n * The event recognized signals that a final recognition result is received.\n * @member DialogServiceConfig.prototype.recognized\n * @function\n * @public\n */\n public recognized: (sender: DialogServiceConnector, event: SpeechRecognitionEventArgs) => void;\n\n /**\n * The event canceled signals that an error occurred during recognition.\n * @member DialogServiceConnector.prototype.canceled\n * @function\n * @public\n */\n public canceled: (sender: DialogServiceConnector, event: SpeechRecognitionCanceledEventArgs) => void;\n\n /**\n * The event activityReceived signals that an activity has been received.\n * @member DialogServiceConnector.prototype.activityReceived\n * @function\n * @public\n */\n public activityReceived: (sender: DialogServiceConnector, event: ActivityReceivedEventArgs) => void;\n\n /**\n * The event turnStatusReceived signals that a turn status message has been received. These messages are\n * associated with both an interaction and a conversation. They are used to notify the client in the event\n * of an interaction failure with the dialog backend, e.g. in the event of a network issue, timeout, crash,\n * or other problem.\n * @member DialogServiceConnector.prototype.turnStatusReceived\n * @function\n * @public\n */\n public turnStatusReceived: (sender: DialogServiceConnector, event: TurnStatusReceivedEventArgs) => void;\n\n /**\n * Starts a connection to the service.\n * Users can optionally call connect() to manually set up a connection in advance, before starting interactions.\n *\n * Note: On return, the connection might not be ready yet. Please subscribe to the Connected event to\n * be notified when the connection is established.\n * @member DialogServiceConnector.prototype.connect\n * @function\n * @public\n */\n public connect(cb?: () => void, err?: (error: string) => void): void {\n marshalPromiseToCallbacks(this.privReco.connect(), cb, err);\n }\n\n /**\n * Closes the connection the service.\n * Users can optionally call disconnect() to manually shutdown the connection of the associated DialogServiceConnector.\n *\n * If disconnect() is called during a recognition, recognition will fail and cancel with an error.\n */\n public disconnect(cb?: () => void, err?: (error: string) => void): void {\n marshalPromiseToCallbacks(this.privReco.disconnect(), cb, err);\n }\n\n /**\n * Gets the authorization token used to communicate with the service.\n * @member DialogServiceConnector.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 * Sets the authorization token used to communicate with the service.\n * @member DialogServiceConnector.prototype.authorizationToken\n * @function\n * @public\n * @param {string} token - Authorization token.\n */\n public set authorizationToken(token: string) {\n Contracts.throwIfNullOrWhitespace(token, \"token\");\n this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, token);\n }\n\n /**\n * The collection of properties and their values defined for this DialogServiceConnector.\n * @member DialogServiceConnector.prototype.properties\n * @function\n * @public\n * @returns {PropertyCollection} The collection of properties and their values defined for this DialogServiceConnector.\n */\n public get properties(): PropertyCollection {\n return this.privProperties;\n }\n\n /** Gets the template for the activity generated by service from speech.\n * Properties from the template will be stamped on the generated activity.\n * It can be empty\n */\n public get speechActivityTemplate(): string {\n return this.properties.getProperty(PropertyId.Conversation_Speech_Activity_Template);\n }\n\n /** Sets the template for the activity generated by service from speech.\n * Properties from the template will be stamped on the generated activity.\n * It can be null or empty.\n * Note: it has to be a valid Json object.\n */\n public set speechActivityTemplate(speechActivityTemplate: string) {\n this.properties.setProperty(PropertyId.Conversation_Speech_Activity_Template, speechActivityTemplate);\n }\n\n /**\n * Starts recognition and stops after the first utterance is recognized.\n * @member DialogServiceConnector.prototype.listenOnceAsync\n * @function\n * @public\n * @param cb - Callback that received the result when the reco has completed.\n * @param err - Callback invoked in case of an error.\n */\n public listenOnceAsync(cb?: (e: SpeechRecognitionResult) => void, err?: (e: string) => void): void {\n if (this.isTurnComplete) {\n Contracts.throwIfDisposed(this.privIsDisposed);\n const callbackHolder = async (): Promise<SpeechRecognitionResult> => {\n await this.privReco.connect();\n await this.implRecognizerStop();\n this.isTurnComplete = false;\n\n const ret: Deferred<SpeechRecognitionResult> = new Deferred<SpeechRecognitionResult>();\n await this.privReco.recognize(RecognitionMode.Conversation, ret.resolve, ret.reject);\n\n const e: SpeechRecognitionResult = await ret.promise;\n await this.implRecognizerStop();\n\n return e;\n };\n\n const retPromise: Promise<SpeechRecognitionResult> = callbackHolder();\n\n retPromise.catch((): void => {\n // Destroy the recognizer.\n // We've done all we can here.\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n this.dispose(true).catch((): void => { });\n });\n\n marshalPromiseToCallbacks(retPromise.finally((): void => {\n this.isTurnComplete = true;\n }), cb, err);\n }\n }\n\n public sendActivityAsync(activity: string, cb?: () => void, errCb?: (error: string) => void): void {\n marshalPromiseToCallbacks((this.privReco as DialogServiceAdapter).sendMessage(activity), cb, errCb);\n }\n\n /**\n * closes all external resources held by an instance of this class.\n * @member DialogServiceConnector.prototype.close\n * @function\n * @public\n */\n public close(cb?: () => void, err?: (error: string) => void): void {\n Contracts.throwIfDisposed(this.privIsDisposed);\n\n marshalPromiseToCallbacks(this.dispose(true), cb, err);\n }\n\n protected async dispose(disposing: boolean): Promise<void> {\n if (this.privIsDisposed) {\n return;\n }\n\n if (disposing) {\n this.privIsDisposed = true;\n await this.implRecognizerStop();\n await super.dispose(disposing);\n }\n }\n\n protected createRecognizerConfig(speechConfig: SpeechServiceConfig): RecognizerConfig {\n return new RecognizerConfig(speechConfig, this.privProperties);\n }\n\n protected createServiceRecognizer(\n authentication: IAuthentication,\n connectionFactory: IConnectionFactory,\n audioConfig: AudioConfig,\n recognizerConfig: RecognizerConfig): ServiceRecognizerBase {\n\n const audioSource: AudioConfigImpl = audioConfig as AudioConfigImpl;\n\n return new DialogServiceAdapter(authentication, connectionFactory, audioSource, recognizerConfig, this);\n }\n\n private buildAgentConfig(): IAgentConfig {\n const communicationType = this.properties.getProperty(\"Conversation_Communication_Type\", \"Default\");\n\n return {\n botInfo: {\n commType: communicationType,\n commandsCulture: undefined,\n connectionId: this.properties.getProperty(PropertyId.Conversation_Agent_Connection_Id),\n conversationId: this.properties.getProperty(PropertyId.Conversation_Conversation_Id, undefined),\n fromId: this.properties.getProperty(PropertyId.Conversation_From_Id, undefined),\n ttsAudioFormat: this.properties.getProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, undefined)\n },\n version: 0.2\n };\n }\n}\n"]}