Skip to content

Commit

Permalink
feat(@aws-amplify/interactions): call onComplete callback with entire…
Browse files Browse the repository at this point in the history
… response (#10248)

* feat(interactions): call onComplete callback with entire response
  • Loading branch information
ashwinkumar6 authored Aug 26, 2022
1 parent 511751a commit 7f2534d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
36 changes: 21 additions & 15 deletions packages/interactions/src/Providers/AWSLexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ import { convert } from './AWSLexProviderHelper/convert';

const logger = new Logger('AWSLexProvider');

interface PostContentCommandOutputFormatted
extends Omit<PostContentCommandOutput, 'audioStream'> {
audioStream?: Uint8Array;
}

type AWSLexProviderSendResponse =
| PostTextCommandOutput
| PostContentCommandOutputFormatted;

export class AWSLexProvider extends AbstractInteractionsProvider {
private lexRuntimeServiceClient: LexRuntimeServiceClient;
private _botsCompleteCallback: object;
Expand Down Expand Up @@ -62,32 +71,27 @@ 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 (
data.dialogState === 'ReadyForFulfillment' ||
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);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/interactions/src/types/Providers/AWSLexProvider.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 7f2534d

Please sign in to comment.