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AW,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.\r\n// Licensed under the MIT license.\r\n\r\nimport { DialogConnectionFactory } from \"../common.speech/DialogConnectorFactory.js\";\r\nimport {\r\n DialogServiceAdapter,\r\n IAgentConfig,\r\n IAuthentication,\r\n IConnectionFactory,\r\n RecognitionMode,\r\n RecognizerConfig,\r\n ServiceRecognizerBase,\r\n SpeechServiceConfig\r\n} from \"../common.speech/Exports.js\";\r\nimport {\r\n Deferred,\r\n marshalPromiseToCallbacks\r\n} from \"../common/Exports.js\";\r\nimport { ActivityReceivedEventArgs } from \"./ActivityReceivedEventArgs.js\";\r\nimport { AudioConfigImpl } from \"./Audio/AudioConfig.js\";\r\nimport { Contracts } from \"./Contracts.js\";\r\nimport { DialogServiceConfig, DialogServiceConfigImpl } from \"./DialogServiceConfig.js\";\r\nimport {\r\n AudioConfig,\r\n PropertyCollection,\r\n Recognizer,\r\n SpeechRecognitionCanceledEventArgs,\r\n SpeechRecognitionEventArgs,\r\n SpeechRecognitionResult\r\n} from \"./Exports.js\";\r\nimport { PropertyId } from \"./PropertyId.js\";\r\nimport { TurnStatusReceivedEventArgs } from \"./TurnStatusReceivedEventArgs.js\";\r\n\r\n/**\r\n * Dialog Service Connector\r\n * @class DialogServiceConnector\r\n */\r\nexport class DialogServiceConnector extends Recognizer {\r\n private privIsDisposed: boolean;\r\n private isTurnComplete: boolean;\r\n\r\n /**\r\n * Initializes an instance of the DialogServiceConnector.\r\n * @constructor\r\n * @param {DialogServiceConfig} dialogConfig - Set of properties to configure this recognizer.\r\n * @param {AudioConfig} audioConfig - An optional audio config associated with the recognizer\r\n */\r\n public constructor(dialogConfig: DialogServiceConfig, audioConfig?: AudioConfig) {\r\n const dialogServiceConfigImpl = dialogConfig as DialogServiceConfigImpl;\r\n Contracts.throwIfNull(dialogConfig, \"dialogConfig\");\r\n\r\n super(audioConfig, dialogServiceConfigImpl.properties, new DialogConnectionFactory());\r\n\r\n this.isTurnComplete = true;\r\n this.privIsDisposed = false;\r\n this.privProperties = dialogServiceConfigImpl.properties.clone();\r\n\r\n const agentConfig = this.buildAgentConfig();\r\n this.privReco.agentConfig.set(agentConfig);\r\n }\r\n\r\n /**\r\n * The event recognizing signals that an intermediate recognition result is received.\r\n * @member DialogServiceConnector.prototype.recognizing\r\n * @function\r\n * @public\r\n */\r\n public recognizing: (sender: DialogServiceConnector, event: SpeechRecognitionEventArgs) => void;\r\n\r\n /**\r\n * The event recognized signals that a final recognition result is received.\r\n * @member DialogServiceConfig.prototype.recognized\r\n * @function\r\n * @public\r\n */\r\n public recognized: (sender: DialogServiceConnector, event: SpeechRecognitionEventArgs) => void;\r\n\r\n /**\r\n * The event canceled signals that an error occurred during recognition.\r\n * @member DialogServiceConnector.prototype.canceled\r\n * @function\r\n * @public\r\n */\r\n public canceled: (sender: DialogServiceConnector, event: SpeechRecognitionCanceledEventArgs) => void;\r\n\r\n /**\r\n * The event activityReceived signals that an activity has been received.\r\n * @member DialogServiceConnector.prototype.activityReceived\r\n * @function\r\n * @public\r\n */\r\n public activityReceived: (sender: DialogServiceConnector, event: ActivityReceivedEventArgs) => void;\r\n\r\n /**\r\n * The event turnStatusReceived signals that a turn status message has been received. These messages are\r\n * associated with both an interaction and a conversation. They are used to notify the client in the event\r\n * of an interaction failure with the dialog backend, e.g. in the event of a network issue, timeout, crash,\r\n * or other problem.\r\n * @member DialogServiceConnector.prototype.turnStatusReceived\r\n * @function\r\n * @public\r\n */\r\n public turnStatusReceived: (sender: DialogServiceConnector, event: TurnStatusReceivedEventArgs) => void;\r\n\r\n /**\r\n * Starts a connection to the service.\r\n * Users can optionally call connect() to manually set up a connection in advance, before starting interactions.\r\n *\r\n * Note: On return, the connection might not be ready yet. Please subscribe to the Connected event to\r\n * be notified when the connection is established.\r\n * @member DialogServiceConnector.prototype.connect\r\n * @function\r\n * @public\r\n */\r\n public connect(cb?: () => void, err?: (error: string) => void): void {\r\n marshalPromiseToCallbacks(this.privReco.connect(), cb, err);\r\n }\r\n\r\n /**\r\n * Closes the connection the service.\r\n * Users can optionally call disconnect() to manually shutdown the connection of the associated DialogServiceConnector.\r\n *\r\n * If disconnect() is called during a recognition, recognition will fail and cancel with an error.\r\n */\r\n public disconnect(cb?: () => void, err?: (error: string) => void): void {\r\n marshalPromiseToCallbacks(this.privReco.disconnect(), cb, err);\r\n }\r\n\r\n /**\r\n * Gets the authorization token used to communicate with the service.\r\n * @member DialogServiceConnector.prototype.authorizationToken\r\n * @function\r\n * @public\r\n * @returns {string} Authorization token.\r\n */\r\n public get authorizationToken(): string {\r\n return this.properties.getProperty(PropertyId.SpeechServiceAuthorization_Token);\r\n }\r\n\r\n /**\r\n * Sets the authorization token used to communicate with the service.\r\n * @member DialogServiceConnector.prototype.authorizationToken\r\n * @function\r\n * @public\r\n * @param {string} token - Authorization token.\r\n */\r\n public set authorizationToken(token: string) {\r\n Contracts.throwIfNullOrWhitespace(token, \"token\");\r\n this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, token);\r\n }\r\n\r\n /**\r\n * The collection of properties and their values defined for this DialogServiceConnector.\r\n * @member DialogServiceConnector.prototype.properties\r\n * @function\r\n * @public\r\n * @returns {PropertyCollection} The collection of properties and their values defined for this DialogServiceConnector.\r\n */\r\n public get properties(): PropertyCollection {\r\n return this.privProperties;\r\n }\r\n\r\n /** Gets the template for the activity generated by service from speech.\r\n * Properties from the template will be stamped on the generated activity.\r\n * It can be empty\r\n */\r\n public get speechActivityTemplate(): string {\r\n return this.properties.getProperty(PropertyId.Conversation_Speech_Activity_Template);\r\n }\r\n\r\n /** Sets the template for the activity generated by service from speech.\r\n * Properties from the template will be stamped on the generated activity.\r\n * It can be null or empty.\r\n * Note: it has to be a valid Json object.\r\n */\r\n public set speechActivityTemplate(speechActivityTemplate: string) {\r\n this.properties.setProperty(PropertyId.Conversation_Speech_Activity_Template, speechActivityTemplate);\r\n }\r\n\r\n /**\r\n * Starts recognition and stops after the first utterance is recognized.\r\n * @member DialogServiceConnector.prototype.listenOnceAsync\r\n * @function\r\n * @public\r\n * @param cb - Callback that received the result when the reco has completed.\r\n * @param err - Callback invoked in case of an error.\r\n */\r\n public listenOnceAsync(cb?: (e: SpeechRecognitionResult) => void, err?: (e: string) => void): void {\r\n if (this.isTurnComplete) {\r\n Contracts.throwIfDisposed(this.privIsDisposed);\r\n const callbackHolder = async (): Promise<SpeechRecognitionResult> => {\r\n await this.privReco.connect();\r\n await this.implRecognizerStop();\r\n this.isTurnComplete = false;\r\n\r\n const ret: Deferred<SpeechRecognitionResult> = new Deferred<SpeechRecognitionResult>();\r\n await this.privReco.recognize(RecognitionMode.Conversation, ret.resolve, ret.reject);\r\n\r\n const e: SpeechRecognitionResult = await ret.promise;\r\n await this.implRecognizerStop();\r\n\r\n return e;\r\n };\r\n\r\n const retPromise: Promise<SpeechRecognitionResult> = callbackHolder();\r\n\r\n retPromise.catch((): void => {\r\n // Destroy the recognizer.\r\n // We've done all we can here.\r\n // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n this.dispose(true).catch((): void => { });\r\n });\r\n\r\n marshalPromiseToCallbacks(retPromise.finally((): void => {\r\n this.isTurnComplete = true;\r\n }), cb, err);\r\n }\r\n }\r\n\r\n public sendActivityAsync(activity: string, cb?: () => void, errCb?: (error: string) => void): void {\r\n marshalPromiseToCallbacks((this.privReco as DialogServiceAdapter).sendMessage(activity), cb, errCb);\r\n }\r\n\r\n /**\r\n * closes all external resources held by an instance of this class.\r\n * @member DialogServiceConnector.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public close(cb?: () => void, err?: (error: string) => void): void {\r\n Contracts.throwIfDisposed(this.privIsDisposed);\r\n\r\n marshalPromiseToCallbacks(this.dispose(true), cb, err);\r\n }\r\n\r\n protected async dispose(disposing: boolean): Promise<void> {\r\n if (this.privIsDisposed) {\r\n return;\r\n }\r\n\r\n if (disposing) {\r\n this.privIsDisposed = true;\r\n await this.implRecognizerStop();\r\n await super.dispose(disposing);\r\n }\r\n }\r\n\r\n protected createRecognizerConfig(speechConfig: SpeechServiceConfig): RecognizerConfig {\r\n return new RecognizerConfig(speechConfig, this.privProperties);\r\n }\r\n\r\n protected createServiceRecognizer(\r\n authentication: IAuthentication,\r\n connectionFactory: IConnectionFactory,\r\n audioConfig: AudioConfig,\r\n recognizerConfig: RecognizerConfig): ServiceRecognizerBase {\r\n\r\n const audioSource: AudioConfigImpl = audioConfig as AudioConfigImpl;\r\n\r\n return new DialogServiceAdapter(authentication, connectionFactory, audioSource, recognizerConfig, this);\r\n }\r\n\r\n private buildAgentConfig(): IAgentConfig {\r\n const communicationType = this.properties.getProperty(\"Conversation_Communication_Type\", \"Default\");\r\n\r\n return {\r\n botInfo: {\r\n commType: communicationType,\r\n commandsCulture: undefined,\r\n connectionId: this.properties.getProperty(PropertyId.Conversation_Agent_Connection_Id),\r\n conversationId: this.properties.getProperty(PropertyId.Conversation_Conversation_Id, undefined),\r\n fromId: this.properties.getProperty(PropertyId.Conversation_From_Id, undefined),\r\n ttsAudioFormat: this.properties.getProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, undefined)\r\n },\r\n version: 0.2\r\n };\r\n }\r\n}\r\n"]}