From 7f2534d5dc2c936eadc12b5d49c86a3704051f4a Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Date: Fri, 26 Aug 2022 14:45:22 -0700 Subject: [PATCH] feat(@aws-amplify/interactions): call onComplete callback with entire response (#10248) * feat(interactions): call onComplete callback with entire response --- .../providers/AWSLexProvider-unit-test.ts | 9 ++++- .../src/Providers/AWSLexProvider.ts | 36 +++++++++++-------- .../src/types/Providers/AWSLexProvider.ts | 12 +++++++ 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/interactions/__tests__/providers/AWSLexProvider-unit-test.ts b/packages/interactions/__tests__/providers/AWSLexProvider-unit-test.ts index 3df97d856be..00b4fb8d710 100644 --- a/packages/interactions/__tests__/providers/AWSLexProvider-unit-test.ts +++ b/packages/interactions/__tests__/providers/AWSLexProvider-unit-test.ts @@ -395,7 +395,14 @@ describe('Interactions', () => { completeSuccessCallback = jest.fn((err, confirmation) => { expect(err).toEqual(null); - expect(confirmation).toEqual({ slots: { m1: 'hi', m2: 'done' } }); + expect(confirmation).toEqual({ + message: 'echo:done', + dialogState: 'ReadyForFulfillment', + slots: { + m1: 'hi', + m2: 'done', + }, + }); }); completeFailCallback = jest.fn((err, confirmation) => diff --git a/packages/interactions/src/Providers/AWSLexProvider.ts b/packages/interactions/src/Providers/AWSLexProvider.ts index e7621e7268e..38b2416b71a 100644 --- a/packages/interactions/src/Providers/AWSLexProvider.ts +++ b/packages/interactions/src/Providers/AWSLexProvider.ts @@ -35,6 +35,15 @@ import { convert } from './AWSLexProviderHelper/convert'; const logger = new Logger('AWSLexProvider'); +interface PostContentCommandOutputFormatted + extends Omit { + audioStream?: Uint8Array; +} + +type AWSLexProviderSendResponse = + | PostTextCommandOutput + | PostContentCommandOutputFormatted; + export class AWSLexProvider extends AbstractInteractionsProvider { private lexRuntimeServiceClient: LexRuntimeServiceClient; private _botsCompleteCallback: object; @@ -62,10 +71,12 @@ export class AWSLexProvider extends AbstractInteractionsProvider { return super.configure(config); } - reportBotStatus( - data: PostTextCommandOutput | PostContentCommandOutput, - botname: string - ) { + /** + * This is used internally by 'sendMessage' to call onComplete callback + * for a bot if configured + * @deprecated + */ + reportBotStatus(data: AWSLexProviderSendResponse, botname: string) { // Check if state is fulfilled to resolve onFullfilment promise logger.debug('postContent state', data.dialogState); if ( @@ -73,21 +84,14 @@ export class AWSLexProvider extends AbstractInteractionsProvider { data.dialogState === 'Fulfilled' ) { if (typeof this._botsCompleteCallback[botname] === 'function') { - setTimeout( - () => - this._botsCompleteCallback[botname](null, { slots: data.slots }), - 0 - ); + setTimeout(() => this._botsCompleteCallback[botname](null, data), 0); } if ( this._config && typeof this._config[botname].onComplete === 'function' ) { - setTimeout( - () => this._config[botname].onComplete(null, { slots: data.slots }), - 0 - ); + setTimeout(() => this._config[botname].onComplete(null, data), 0); } } @@ -194,8 +198,10 @@ export class AWSLexProvider extends AbstractInteractionsProvider { ? await convert(data.audioStream) : undefined; - this.reportBotStatus(data, botname); - return { ...data, ...{ audioStream: audioArray } }; + const response = { ...data, ...{ audioStream: audioArray } }; + + this.reportBotStatus(response, botname); + return response; } catch (err) { return Promise.reject(err); } diff --git a/packages/interactions/src/types/Providers/AWSLexProvider.ts b/packages/interactions/src/types/Providers/AWSLexProvider.ts index dc18eb66de6..e41efaba0e0 100644 --- a/packages/interactions/src/types/Providers/AWSLexProvider.ts +++ b/packages/interactions/src/types/Providers/AWSLexProvider.ts @@ -1,3 +1,15 @@ +/* + * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with + * the License. A copy of the License is located at + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + */ export interface AWSLexProviderOption { name: string; alias: string;