@aws-amplify/interactions 5.2.12-unstable.7762f1a.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/lex-v1/package.json +7 -0
- package/lex-v2/package.json +7 -0
- package/lib/errors/InteractionsError.d.ts +4 -0
- package/lib/errors/InteractionsError.js +16 -0
- package/lib/errors/assertValidationError.d.ts +2 -0
- package/lib/errors/assertValidationError.js +18 -0
- package/lib/errors/index.d.ts +2 -0
- package/lib/errors/index.js +9 -0
- package/lib/errors/validation.d.ts +5 -0
- package/lib/errors/validation.js +14 -0
- package/lib/index.d.ts +2 -5
- package/lib/index.js +3 -9
- package/lib/lex-v1/AWSLexProvider.d.ts +20 -0
- package/lib/lex-v1/AWSLexProvider.js +119 -0
- package/lib/lex-v1/apis/index.d.ts +2 -0
- package/lib/lex-v1/apis/index.js +9 -0
- package/lib/lex-v1/apis/onComplete.d.ts +2 -0
- package/lib/lex-v1/apis/onComplete.js +15 -0
- package/lib/lex-v1/apis/send.d.ts +2 -0
- package/lib/lex-v1/apis/send.js +15 -0
- package/lib/lex-v1/index.d.ts +6 -0
- package/lib/lex-v1/index.js +13 -0
- package/lib/lex-v1/types/AWSLexProviderOption.d.ts +11 -0
- package/lib/{types/Response.js → lex-v1/types/AWSLexProviderOption.js} +0 -1
- package/lib/lex-v1/types/index.d.ts +3 -0
- package/lib/lex-v1/types/index.js +4 -0
- package/lib/lex-v1/types/inputs.d.ts +9 -0
- package/lib/lex-v1/types/inputs.js +4 -0
- package/lib/lex-v1/types/outputs.d.ts +5 -0
- package/lib/lex-v1/types/outputs.js +4 -0
- package/lib/lex-v1/utils/index.d.ts +1 -0
- package/lib/lex-v1/utils/index.js +7 -0
- package/lib/lex-v1/utils/resolveBotConfig.d.ts +2 -0
- package/lib/lex-v1/utils/resolveBotConfig.js +13 -0
- package/lib/lex-v2/AWSLexV2Provider.d.ts +52 -0
- package/lib/lex-v2/AWSLexV2Provider.js +166 -0
- package/lib/lex-v2/apis/index.d.ts +2 -0
- package/lib/lex-v2/apis/index.js +9 -0
- package/lib/lex-v2/apis/onComplete.d.ts +2 -0
- package/lib/lex-v2/apis/onComplete.js +15 -0
- package/lib/lex-v2/apis/send.d.ts +2 -0
- package/lib/lex-v2/apis/send.js +15 -0
- package/lib/lex-v2/index.d.ts +3 -0
- package/lib/lex-v2/index.js +10 -0
- package/lib/lex-v2/types/AWSLexV2ProviderOption.d.ts +13 -0
- package/lib/lex-v2/types/AWSLexV2ProviderOption.js +4 -0
- package/lib/lex-v2/types/index.d.ts +3 -0
- package/lib/lex-v2/types/index.js +4 -0
- package/lib/lex-v2/types/inputs.d.ts +9 -0
- package/lib/lex-v2/types/inputs.js +4 -0
- package/lib/lex-v2/types/outputs.d.ts +5 -0
- package/lib/lex-v2/types/outputs.js +4 -0
- package/lib/lex-v2/utils/index.d.ts +1 -0
- package/lib/lex-v2/utils/index.js +7 -0
- package/lib/lex-v2/utils/resolveBotConfig.d.ts +2 -0
- package/lib/lex-v2/utils/resolveBotConfig.js +13 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/Interactions.d.ts +7 -6
- package/lib/types/Interactions.js +2 -1
- package/lib/types/index.d.ts +2 -5
- package/lib/types/index.js +2 -1
- package/lib/types/inputs.d.ts +9 -0
- package/lib/types/inputs.js +4 -0
- package/lib/types/outputs.d.ts +2 -0
- package/lib/types/outputs.js +4 -0
- package/lib/utils/commonUtils.d.ts +1 -0
- package/lib/utils/commonUtils.js +19 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +11 -0
- package/lib/utils/utils.js +32 -0
- package/lib/utils/utils.native.js +45 -0
- package/lib-esm/errors/InteractionsError.d.ts +4 -0
- package/lib-esm/errors/InteractionsError.js +12 -0
- package/lib-esm/errors/assertValidationError.d.ts +2 -0
- package/lib-esm/errors/assertValidationError.js +14 -0
- package/lib-esm/errors/index.d.ts +2 -0
- package/lib-esm/errors/index.js +4 -0
- package/lib-esm/errors/validation.d.ts +5 -0
- package/lib-esm/errors/validation.js +11 -0
- package/lib-esm/index.d.ts +2 -5
- package/lib-esm/index.js +1 -5
- package/lib-esm/lex-v1/AWSLexProvider.d.ts +20 -0
- package/lib-esm/lex-v1/AWSLexProvider.js +116 -0
- package/lib-esm/lex-v1/apis/index.d.ts +2 -0
- package/lib-esm/lex-v1/apis/index.js +4 -0
- package/lib-esm/lex-v1/apis/onComplete.d.ts +2 -0
- package/lib-esm/lex-v1/apis/onComplete.js +11 -0
- package/lib-esm/lex-v1/apis/send.d.ts +2 -0
- package/lib-esm/lex-v1/apis/send.js +11 -0
- package/lib-esm/lex-v1/index.d.ts +6 -0
- package/lib-esm/lex-v1/index.js +10 -0
- package/lib-esm/lex-v1/types/AWSLexProviderOption.d.ts +11 -0
- package/lib-esm/{types/Response.js → lex-v1/types/AWSLexProviderOption.js} +1 -1
- package/lib-esm/lex-v1/types/index.d.ts +3 -0
- package/lib-esm/lex-v1/types/index.js +3 -0
- package/lib-esm/lex-v1/types/inputs.d.ts +9 -0
- package/lib-esm/lex-v1/types/inputs.js +3 -0
- package/lib-esm/lex-v1/types/outputs.d.ts +5 -0
- package/lib-esm/lex-v1/types/outputs.js +3 -0
- package/lib-esm/lex-v1/utils/index.d.ts +1 -0
- package/lib-esm/lex-v1/utils/index.js +3 -0
- package/lib-esm/lex-v1/utils/resolveBotConfig.d.ts +2 -0
- package/lib-esm/lex-v1/utils/resolveBotConfig.js +9 -0
- package/lib-esm/lex-v2/AWSLexV2Provider.d.ts +52 -0
- package/lib-esm/lex-v2/AWSLexV2Provider.js +163 -0
- package/lib-esm/lex-v2/apis/index.d.ts +2 -0
- package/lib-esm/lex-v2/apis/index.js +4 -0
- package/lib-esm/lex-v2/apis/onComplete.d.ts +2 -0
- package/lib-esm/lex-v2/apis/onComplete.js +11 -0
- package/lib-esm/lex-v2/apis/send.d.ts +2 -0
- package/lib-esm/lex-v2/apis/send.js +11 -0
- package/lib-esm/lex-v2/index.d.ts +3 -0
- package/lib-esm/lex-v2/index.js +7 -0
- package/lib-esm/lex-v2/types/AWSLexV2ProviderOption.d.ts +13 -0
- package/lib-esm/lex-v2/types/AWSLexV2ProviderOption.js +3 -0
- package/lib-esm/lex-v2/types/index.d.ts +3 -0
- package/lib-esm/lex-v2/types/index.js +3 -0
- package/lib-esm/lex-v2/types/inputs.d.ts +9 -0
- package/lib-esm/lex-v2/types/inputs.js +3 -0
- package/lib-esm/lex-v2/types/outputs.d.ts +5 -0
- package/lib-esm/lex-v2/types/outputs.js +3 -0
- package/lib-esm/lex-v2/utils/index.d.ts +1 -0
- package/lib-esm/lex-v2/utils/index.js +3 -0
- package/lib-esm/lex-v2/utils/resolveBotConfig.d.ts +2 -0
- package/lib-esm/lex-v2/utils/resolveBotConfig.js +9 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types/Interactions.d.ts +7 -6
- package/lib-esm/types/Interactions.js +3 -1
- package/lib-esm/types/index.d.ts +2 -5
- package/lib-esm/types/index.js +3 -1
- package/lib-esm/types/inputs.d.ts +9 -0
- package/lib-esm/types/inputs.js +3 -0
- package/lib-esm/types/outputs.d.ts +2 -0
- package/lib-esm/types/outputs.js +3 -0
- package/lib-esm/utils/commonUtils.d.ts +1 -0
- package/lib-esm/utils/commonUtils.js +15 -0
- package/lib-esm/utils/index.d.ts +2 -0
- package/lib-esm/utils/index.js +4 -0
- package/lib-esm/utils/utils.js +26 -0
- package/lib-esm/utils/utils.native.js +39 -0
- package/package.json +67 -28
- package/src/errors/InteractionsError.ts +18 -0
- package/src/errors/assertValidationError.ts +24 -0
- package/src/errors/index.ts +5 -0
- package/src/errors/validation.ts +15 -0
- package/src/index.ts +2 -5
- package/src/lex-v1/AWSLexProvider.ts +167 -0
- package/src/lex-v1/apis/index.ts +5 -0
- package/src/lex-v1/apis/onComplete.ts +21 -0
- package/src/lex-v1/apis/send.ts +21 -0
- package/src/lex-v1/index.ts +14 -0
- package/src/lex-v1/types/AWSLexProviderOption.ts +16 -0
- package/src/lex-v1/types/index.ts +6 -0
- package/src/lex-v1/types/inputs.ts +17 -0
- package/src/lex-v1/types/outputs.ts +9 -0
- package/src/{types/Response.ts → lex-v1/utils/index.ts} +1 -3
- package/src/lex-v1/utils/resolveBotConfig.ts +15 -0
- package/src/{Providers → lex-v2}/AWSLexV2Provider.ts +67 -137
- package/src/lex-v2/apis/index.ts +5 -0
- package/src/lex-v2/apis/onComplete.ts +21 -0
- package/src/lex-v2/apis/send.ts +21 -0
- package/src/lex-v2/index.ts +11 -0
- package/src/{types/Providers/AWSLexV2Provider.ts → lex-v2/types/AWSLexV2ProviderOption.ts} +7 -4
- package/src/lex-v2/types/index.ts +6 -0
- package/src/lex-v2/types/inputs.ts +17 -0
- package/src/lex-v2/types/outputs.ts +9 -0
- package/src/lex-v2/utils/index.ts +4 -0
- package/src/lex-v2/utils/resolveBotConfig.ts +15 -0
- package/src/types/Interactions.ts +9 -3
- package/src/types/index.ts +3 -5
- package/src/types/inputs.ts +17 -0
- package/src/types/outputs.ts +6 -0
- package/src/utils/index.ts +5 -0
- package/lib/.tsbuildinfo +0 -3
- package/lib/Interactions.d.ts +0 -24
- package/lib/Interactions.js +0 -123
- package/lib/Interactions.js.map +0 -1
- package/lib/Providers/AWSLexProvider.d.ts +0 -24
- package/lib/Providers/AWSLexProvider.js +0 -187
- package/lib/Providers/AWSLexProvider.js.map +0 -1
- package/lib/Providers/AWSLexProviderHelper/commonUtils.d.ts +0 -1
- package/lib/Providers/AWSLexProviderHelper/commonUtils.js +0 -29
- package/lib/Providers/AWSLexProviderHelper/commonUtils.js.map +0 -1
- package/lib/Providers/AWSLexProviderHelper/utils.js +0 -38
- package/lib/Providers/AWSLexProviderHelper/utils.js.map +0 -1
- package/lib/Providers/AWSLexProviderHelper/utils.native.js +0 -55
- package/lib/Providers/AWSLexProviderHelper/utils.native.js.map +0 -1
- package/lib/Providers/AWSLexV2Provider.d.ts +0 -58
- package/lib/Providers/AWSLexV2Provider.js +0 -282
- package/lib/Providers/AWSLexV2Provider.js.map +0 -1
- package/lib/Providers/InteractionsProvider.d.ts +0 -11
- package/lib/Providers/InteractionsProvider.js +0 -32
- package/lib/Providers/InteractionsProvider.js.map +0 -1
- package/lib/Providers/index.d.ts +0 -3
- package/lib/Providers/index.js +0 -11
- package/lib/Providers/index.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types/Interactions.js.map +0 -1
- package/lib/types/Provider.d.ts +0 -12
- package/lib/types/Provider.js +0 -3
- package/lib/types/Provider.js.map +0 -1
- package/lib/types/Providers/AWSLexProvider.d.ts +0 -10
- package/lib/types/Providers/AWSLexProvider.js +0 -3
- package/lib/types/Providers/AWSLexProvider.js.map +0 -1
- package/lib/types/Providers/AWSLexV2Provider.d.ts +0 -12
- package/lib/types/Providers/AWSLexV2Provider.js +0 -3
- package/lib/types/Providers/AWSLexV2Provider.js.map +0 -1
- package/lib/types/Response.d.ts +0 -3
- package/lib/types/Response.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/Interactions.d.ts +0 -24
- package/lib-esm/Interactions.js +0 -121
- package/lib-esm/Interactions.js.map +0 -1
- package/lib-esm/Providers/AWSLexProvider.d.ts +0 -24
- package/lib-esm/Providers/AWSLexProvider.js +0 -185
- package/lib-esm/Providers/AWSLexProvider.js.map +0 -1
- package/lib-esm/Providers/AWSLexProviderHelper/commonUtils.d.ts +0 -1
- package/lib-esm/Providers/AWSLexProviderHelper/commonUtils.js +0 -27
- package/lib-esm/Providers/AWSLexProviderHelper/commonUtils.js.map +0 -1
- package/lib-esm/Providers/AWSLexProviderHelper/utils.js +0 -36
- package/lib-esm/Providers/AWSLexProviderHelper/utils.js.map +0 -1
- package/lib-esm/Providers/AWSLexProviderHelper/utils.native.js +0 -53
- package/lib-esm/Providers/AWSLexProviderHelper/utils.native.js.map +0 -1
- package/lib-esm/Providers/AWSLexV2Provider.d.ts +0 -58
- package/lib-esm/Providers/AWSLexV2Provider.js +0 -280
- package/lib-esm/Providers/AWSLexV2Provider.js.map +0 -1
- package/lib-esm/Providers/InteractionsProvider.d.ts +0 -11
- package/lib-esm/Providers/InteractionsProvider.js +0 -30
- package/lib-esm/Providers/InteractionsProvider.js.map +0 -1
- package/lib-esm/Providers/index.d.ts +0 -3
- package/lib-esm/Providers/index.js +0 -6
- package/lib-esm/Providers/index.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/types/Interactions.js.map +0 -1
- package/lib-esm/types/Provider.d.ts +0 -12
- package/lib-esm/types/Provider.js +0 -1
- package/lib-esm/types/Provider.js.map +0 -1
- package/lib-esm/types/Providers/AWSLexProvider.d.ts +0 -10
- package/lib-esm/types/Providers/AWSLexProvider.js +0 -1
- package/lib-esm/types/Providers/AWSLexProvider.js.map +0 -1
- package/lib-esm/types/Providers/AWSLexV2Provider.d.ts +0 -12
- package/lib-esm/types/Providers/AWSLexV2Provider.js +0 -1
- package/lib-esm/types/Providers/AWSLexV2Provider.js.map +0 -1
- package/lib-esm/types/Response.d.ts +0 -3
- package/lib-esm/types/Response.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
- package/src/Interactions.ts +0 -162
- package/src/Providers/AWSLexProvider.ts +0 -212
- package/src/Providers/InteractionsProvider.ts +0 -49
- package/src/Providers/index.ts +0 -5
- package/src/types/Provider.ts +0 -26
- package/src/types/Providers/AWSLexProvider.ts +0 -13
- /package/lib/{Providers/AWSLexProviderHelper → utils}/utils.d.ts +0 -0
- /package/lib/{Providers/AWSLexProviderHelper → utils}/utils.native.d.ts +0 -0
- /package/lib-esm/{Providers/AWSLexProviderHelper → utils}/utils.d.ts +0 -0
- /package/lib-esm/{Providers/AWSLexProviderHelper → utils}/utils.native.d.ts +0 -0
- /package/src/{Providers/AWSLexProviderHelper → utils}/commonUtils.ts +0 -0
- /package/src/{Providers/AWSLexProviderHelper → utils}/utils.native.ts +0 -0
- /package/src/{Providers/AWSLexProviderHelper → utils}/utils.ts +0 -0
package/lib-esm/index.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
export { Interactions } from './
|
|
2
|
-
export
|
|
3
|
-
export { AbstractInteractionsProvider } from './Providers/InteractionsProvider';
|
|
4
|
-
export { AWSLexProvider } from './Providers/AWSLexProvider';
|
|
5
|
-
export { AWSLexV2Provider } from './Providers/AWSLexV2Provider';
|
|
1
|
+
export { Interactions } from './lex-v2';
|
|
2
|
+
export { SendInput, OnCompleteInput, SendOutput } from './lex-v2/types';
|
package/lib-esm/index.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
export { Interactions } from './
|
|
4
|
-
export { AbstractInteractionsProvider } from './Providers/InteractionsProvider';
|
|
5
|
-
export { AWSLexProvider } from './Providers/AWSLexProvider';
|
|
6
|
-
export { AWSLexV2Provider } from './Providers/AWSLexV2Provider';
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
|
3
|
+
export { Interactions } from './lex-v2';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { InteractionsOnCompleteCallback, InteractionsMessage, InteractionsResponse } from '../types/Interactions';
|
|
2
|
+
import { PostContentCommandOutput, PostTextCommandOutput } from '@aws-sdk/client-lex-runtime-service';
|
|
3
|
+
import { AWSLexProviderOption } from './types';
|
|
4
|
+
interface PostContentCommandOutputFormatted extends Omit<PostContentCommandOutput, 'audioStream'> {
|
|
5
|
+
audioStream?: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
type AWSLexProviderSendResponse = PostTextCommandOutput | PostContentCommandOutputFormatted;
|
|
8
|
+
declare class AWSLexProvider {
|
|
9
|
+
private readonly _botsCompleteCallback;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated
|
|
12
|
+
* This is used internally by 'sendMessage' to call onComplete callback
|
|
13
|
+
* for a bot if configured
|
|
14
|
+
*/
|
|
15
|
+
reportBotStatus(data: AWSLexProviderSendResponse, { name }: AWSLexProviderOption): void;
|
|
16
|
+
sendMessage(botConfig: AWSLexProviderOption, message: string | InteractionsMessage): Promise<InteractionsResponse>;
|
|
17
|
+
onComplete({ name }: AWSLexProviderOption, callback: InteractionsOnCompleteCallback): void;
|
|
18
|
+
}
|
|
19
|
+
export declare const lexProvider: AWSLexProvider;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { DialogState, LexRuntimeServiceClient, PostContentCommand, PostTextCommand, } from '@aws-sdk/client-lex-runtime-service';
|
|
2
|
+
import { getAmplifyUserAgentObject } from '@aws-amplify/core/internals/utils';
|
|
3
|
+
import { ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';
|
|
4
|
+
import { convert } from '../utils';
|
|
5
|
+
const logger = new ConsoleLogger('AWSLexProvider');
|
|
6
|
+
class AWSLexProvider {
|
|
7
|
+
constructor() {
|
|
8
|
+
this._botsCompleteCallback = {};
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated
|
|
12
|
+
* This is used internally by 'sendMessage' to call onComplete callback
|
|
13
|
+
* for a bot if configured
|
|
14
|
+
*/
|
|
15
|
+
reportBotStatus(data, { name }) {
|
|
16
|
+
const callback = this._botsCompleteCallback[name];
|
|
17
|
+
if (!callback) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
// Check if state is fulfilled to resolve onFullfilment promise
|
|
21
|
+
logger.debug('postContent state', data.dialogState);
|
|
22
|
+
switch (data.dialogState) {
|
|
23
|
+
case DialogState.READY_FOR_FULFILLMENT:
|
|
24
|
+
case DialogState.FULFILLED:
|
|
25
|
+
callback(undefined, data);
|
|
26
|
+
break;
|
|
27
|
+
case DialogState.FAILED:
|
|
28
|
+
callback(new Error('Bot conversation failed'));
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async sendMessage(botConfig, message) {
|
|
35
|
+
// check if credentials are present
|
|
36
|
+
let session;
|
|
37
|
+
try {
|
|
38
|
+
session = await fetchAuthSession();
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
return Promise.reject('No credentials');
|
|
42
|
+
}
|
|
43
|
+
const { name, region, alias } = botConfig;
|
|
44
|
+
const client = new LexRuntimeServiceClient({
|
|
45
|
+
region,
|
|
46
|
+
credentials: session.credentials,
|
|
47
|
+
customUserAgent: getAmplifyUserAgentObject(),
|
|
48
|
+
});
|
|
49
|
+
let params;
|
|
50
|
+
if (typeof message === 'string') {
|
|
51
|
+
params = {
|
|
52
|
+
botAlias: alias,
|
|
53
|
+
botName: name,
|
|
54
|
+
inputText: message,
|
|
55
|
+
userId: session.identityId,
|
|
56
|
+
};
|
|
57
|
+
logger.debug('postText to lex', message);
|
|
58
|
+
try {
|
|
59
|
+
const postTextCommand = new PostTextCommand(params);
|
|
60
|
+
const data = await client.send(postTextCommand);
|
|
61
|
+
this.reportBotStatus(data, botConfig);
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
return Promise.reject(err);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const { content, options: { messageType }, } = message;
|
|
70
|
+
if (messageType === 'voice') {
|
|
71
|
+
if (typeof content !== 'object') {
|
|
72
|
+
return Promise.reject('invalid content type');
|
|
73
|
+
}
|
|
74
|
+
const inputStream = content instanceof Uint8Array ? content : await convert(content);
|
|
75
|
+
params = {
|
|
76
|
+
botAlias: alias,
|
|
77
|
+
botName: name,
|
|
78
|
+
contentType: 'audio/x-l16; sample-rate=16000; channel-count=1',
|
|
79
|
+
userId: session.identityId,
|
|
80
|
+
accept: 'audio/mpeg',
|
|
81
|
+
inputStream,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
if (typeof content !== 'string')
|
|
86
|
+
return Promise.reject('invalid content type');
|
|
87
|
+
params = {
|
|
88
|
+
botAlias: alias,
|
|
89
|
+
botName: name,
|
|
90
|
+
contentType: 'text/plain; charset=utf-8',
|
|
91
|
+
inputStream: content,
|
|
92
|
+
userId: session.identityId,
|
|
93
|
+
accept: 'audio/mpeg',
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
logger.debug('postContent to lex', message);
|
|
97
|
+
try {
|
|
98
|
+
const postContentCommand = new PostContentCommand(params);
|
|
99
|
+
const data = await client.send(postContentCommand);
|
|
100
|
+
const audioArray = data.audioStream
|
|
101
|
+
? await convert(data.audioStream)
|
|
102
|
+
: undefined;
|
|
103
|
+
const response = { ...data, ...{ audioStream: audioArray } };
|
|
104
|
+
this.reportBotStatus(response, botConfig);
|
|
105
|
+
return response;
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
return Promise.reject(err);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
onComplete({ name }, callback) {
|
|
113
|
+
this._botsCompleteCallback[name] = callback;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export const lexProvider = new AWSLexProvider();
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { resolveBotConfig } from '../utils';
|
|
4
|
+
import { lexProvider } from '../AWSLexProvider';
|
|
5
|
+
import { assertValidationError, InteractionsValidationErrorCode, } from '../../errors';
|
|
6
|
+
export const onComplete = (input) => {
|
|
7
|
+
const { botName, callback } = input;
|
|
8
|
+
const botConfig = resolveBotConfig(botName);
|
|
9
|
+
assertValidationError(!!botConfig, InteractionsValidationErrorCode.NoBotConfig, `Bot ${botName} does not exist.`);
|
|
10
|
+
lexProvider.onComplete(botConfig, callback);
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { resolveBotConfig } from '../utils';
|
|
4
|
+
import { lexProvider } from '../AWSLexProvider';
|
|
5
|
+
import { assertValidationError, InteractionsValidationErrorCode, } from '../../errors';
|
|
6
|
+
export const send = async (input) => {
|
|
7
|
+
const { botName, message } = input;
|
|
8
|
+
const botConfig = resolveBotConfig(botName);
|
|
9
|
+
assertValidationError(!!botConfig, InteractionsValidationErrorCode.NoBotConfig, `Bot ${botName} does not exist.`);
|
|
10
|
+
return lexProvider.sendMessage(botConfig, message);
|
|
11
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { send, onComplete } from './apis';
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated recommend to migrate to AWS Lex V2 instead
|
|
6
|
+
* */
|
|
7
|
+
export const Interactions = {
|
|
8
|
+
send,
|
|
9
|
+
onComplete,
|
|
10
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SendInput, OnCompleteInput } from './inputs';
|
|
2
|
+
import { SendOutput } from './outputs';
|
|
3
|
+
export interface AWSLexProviderOption {
|
|
4
|
+
name: string;
|
|
5
|
+
alias: string;
|
|
6
|
+
region: string;
|
|
7
|
+
}
|
|
8
|
+
export interface IInteractions {
|
|
9
|
+
send(input: SendInput): Promise<SendOutput>;
|
|
10
|
+
onComplete(input: OnCompleteInput): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InteractionsSendInput, InteractionsOnCompleteInput } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Input type for LexV1 send API.
|
|
4
|
+
*/
|
|
5
|
+
export type SendInput = InteractionsSendInput;
|
|
6
|
+
/**
|
|
7
|
+
* Input type for LexV1 onComplete API.
|
|
8
|
+
*/
|
|
9
|
+
export type OnCompleteInput = InteractionsOnCompleteInput;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { resolveBotConfig } from './resolveBotConfig';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { Amplify } from '@aws-amplify/core';
|
|
4
|
+
export const resolveBotConfig = (botName) => {
|
|
5
|
+
const { [botName]: botConfig = undefined } = Amplify.getConfig().Interactions?.LexV1 ?? {};
|
|
6
|
+
if (botConfig !== undefined) {
|
|
7
|
+
return { ...botConfig, name: botName };
|
|
8
|
+
}
|
|
9
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { InteractionsOnCompleteCallback, InteractionsMessage, InteractionsResponse } from '../types/Interactions';
|
|
2
|
+
import { RecognizeTextCommandOutput, RecognizeUtteranceCommandOutput } from '@aws-sdk/client-lex-runtime-v2';
|
|
3
|
+
import { AWSLexV2ProviderOption } from './types';
|
|
4
|
+
interface RecognizeUtteranceCommandOutputFormatted extends Omit<RecognizeUtteranceCommandOutput, 'messages' | 'interpretations' | 'sessionState' | 'requestAttributes' | 'audioStream'> {
|
|
5
|
+
messages?: RecognizeTextCommandOutput['messages'];
|
|
6
|
+
sessionState?: RecognizeTextCommandOutput['sessionState'];
|
|
7
|
+
interpretations?: RecognizeTextCommandOutput['interpretations'];
|
|
8
|
+
requestAttributes?: RecognizeTextCommandOutput['requestAttributes'];
|
|
9
|
+
audioStream?: Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
type AWSLexV2ProviderSendResponse = RecognizeTextCommandOutput | RecognizeUtteranceCommandOutputFormatted;
|
|
12
|
+
declare class AWSLexV2Provider {
|
|
13
|
+
private readonly _botsCompleteCallback;
|
|
14
|
+
private defaultSessionId;
|
|
15
|
+
/**
|
|
16
|
+
* Send a message to a bot
|
|
17
|
+
* @async
|
|
18
|
+
* @param {AWSLexV2ProviderOption} botConfig - Bot configuration for sending the message
|
|
19
|
+
* @param {string | InteractionsMessage} message - message to send to the bot
|
|
20
|
+
* @return {Promise<InteractionsResponse>} A promise resolves to the response from the bot
|
|
21
|
+
*/
|
|
22
|
+
sendMessage(botConfig: AWSLexV2ProviderOption, message: string | InteractionsMessage): Promise<InteractionsResponse>;
|
|
23
|
+
/**
|
|
24
|
+
* Attach a onComplete callback function to a bot.
|
|
25
|
+
* The callback is called once the bot's intent is fulfilled
|
|
26
|
+
* @param {AWSLexV2ProviderOption} botConfig - Bot configuration to attach the onComplete callback
|
|
27
|
+
* @param {InteractionsOnCompleteCallback} callback - called when Intent Fulfilled
|
|
28
|
+
*/
|
|
29
|
+
onComplete({ name }: AWSLexV2ProviderOption, callback: InteractionsOnCompleteCallback): void;
|
|
30
|
+
/**
|
|
31
|
+
* call onComplete callback for a bot if configured
|
|
32
|
+
*/
|
|
33
|
+
_reportBotStatus(data: AWSLexV2ProviderSendResponse, { name }: AWSLexV2ProviderOption): void;
|
|
34
|
+
/**
|
|
35
|
+
* Format UtteranceCommandOutput's response
|
|
36
|
+
* decompress attributes
|
|
37
|
+
* update audioStream format
|
|
38
|
+
*/
|
|
39
|
+
private _formatUtteranceCommandOutput;
|
|
40
|
+
/**
|
|
41
|
+
* handle client's `RecognizeTextCommand`
|
|
42
|
+
* used for sending simple text message
|
|
43
|
+
*/
|
|
44
|
+
private _handleRecognizeTextCommand;
|
|
45
|
+
/**
|
|
46
|
+
* handle client's `RecognizeUtteranceCommand`
|
|
47
|
+
* used for obj text or obj voice message
|
|
48
|
+
*/
|
|
49
|
+
private _handleRecognizeUtteranceCommand;
|
|
50
|
+
}
|
|
51
|
+
export declare const lexProvider: AWSLexV2Provider;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { IntentState, LexRuntimeV2Client, RecognizeTextCommand, RecognizeUtteranceCommand, } from '@aws-sdk/client-lex-runtime-v2';
|
|
2
|
+
import { getAmplifyUserAgentObject } from '@aws-amplify/core/internals/utils';
|
|
3
|
+
import { ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';
|
|
4
|
+
import { convert, unGzipBase64AsJson } from '../utils';
|
|
5
|
+
import { v4 as uuid } from 'uuid';
|
|
6
|
+
const logger = new ConsoleLogger('AWSLexV2Provider');
|
|
7
|
+
class AWSLexV2Provider {
|
|
8
|
+
constructor() {
|
|
9
|
+
this._botsCompleteCallback = {};
|
|
10
|
+
this.defaultSessionId = uuid();
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Send a message to a bot
|
|
14
|
+
* @async
|
|
15
|
+
* @param {AWSLexV2ProviderOption} botConfig - Bot configuration for sending the message
|
|
16
|
+
* @param {string | InteractionsMessage} message - message to send to the bot
|
|
17
|
+
* @return {Promise<InteractionsResponse>} A promise resolves to the response from the bot
|
|
18
|
+
*/
|
|
19
|
+
async sendMessage(botConfig, message) {
|
|
20
|
+
// check if credentials are present
|
|
21
|
+
let session;
|
|
22
|
+
try {
|
|
23
|
+
session = await fetchAuthSession();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
return Promise.reject('No credentials');
|
|
27
|
+
}
|
|
28
|
+
const { region, aliasId, localeId, botId } = botConfig;
|
|
29
|
+
const client = new LexRuntimeV2Client({
|
|
30
|
+
region,
|
|
31
|
+
credentials: session.credentials,
|
|
32
|
+
customUserAgent: getAmplifyUserAgentObject(),
|
|
33
|
+
});
|
|
34
|
+
let response;
|
|
35
|
+
// common base params for all requests
|
|
36
|
+
const reqBaseParams = {
|
|
37
|
+
botAliasId: aliasId,
|
|
38
|
+
botId,
|
|
39
|
+
localeId,
|
|
40
|
+
sessionId: session.identityId ?? this.defaultSessionId,
|
|
41
|
+
};
|
|
42
|
+
if (typeof message === 'string') {
|
|
43
|
+
response = await this._handleRecognizeTextCommand(botConfig, message, reqBaseParams, client);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
response = await this._handleRecognizeUtteranceCommand(botConfig, message, reqBaseParams, client);
|
|
47
|
+
}
|
|
48
|
+
return response;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Attach a onComplete callback function to a bot.
|
|
52
|
+
* The callback is called once the bot's intent is fulfilled
|
|
53
|
+
* @param {AWSLexV2ProviderOption} botConfig - Bot configuration to attach the onComplete callback
|
|
54
|
+
* @param {InteractionsOnCompleteCallback} callback - called when Intent Fulfilled
|
|
55
|
+
*/
|
|
56
|
+
onComplete({ name }, callback) {
|
|
57
|
+
this._botsCompleteCallback[name] = callback;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* call onComplete callback for a bot if configured
|
|
61
|
+
*/
|
|
62
|
+
_reportBotStatus(data, { name }) {
|
|
63
|
+
const sessionState = data?.sessionState;
|
|
64
|
+
// Check if state is fulfilled to resolve onFullfilment promise
|
|
65
|
+
logger.debug('postContent state', sessionState?.intent?.state);
|
|
66
|
+
const callback = this._botsCompleteCallback[name];
|
|
67
|
+
if (!callback) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
switch (sessionState?.intent?.state) {
|
|
71
|
+
case IntentState.READY_FOR_FULFILLMENT:
|
|
72
|
+
case IntentState.FULFILLED:
|
|
73
|
+
callback(undefined, data);
|
|
74
|
+
break;
|
|
75
|
+
case IntentState.FAILED:
|
|
76
|
+
callback(new Error('Bot conversation failed'));
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Format UtteranceCommandOutput's response
|
|
84
|
+
* decompress attributes
|
|
85
|
+
* update audioStream format
|
|
86
|
+
*/
|
|
87
|
+
async _formatUtteranceCommandOutput(data) {
|
|
88
|
+
return {
|
|
89
|
+
...data,
|
|
90
|
+
messages: await unGzipBase64AsJson(data.messages),
|
|
91
|
+
sessionState: await unGzipBase64AsJson(data.sessionState),
|
|
92
|
+
interpretations: await unGzipBase64AsJson(data.interpretations),
|
|
93
|
+
requestAttributes: await unGzipBase64AsJson(data.requestAttributes),
|
|
94
|
+
inputTranscript: await unGzipBase64AsJson(data.inputTranscript),
|
|
95
|
+
audioStream: data.audioStream
|
|
96
|
+
? await convert(data.audioStream)
|
|
97
|
+
: undefined,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* handle client's `RecognizeTextCommand`
|
|
102
|
+
* used for sending simple text message
|
|
103
|
+
*/
|
|
104
|
+
async _handleRecognizeTextCommand(botConfig, data, baseParams, client) {
|
|
105
|
+
logger.debug('postText to lex2', data);
|
|
106
|
+
const params = {
|
|
107
|
+
...baseParams,
|
|
108
|
+
text: data,
|
|
109
|
+
};
|
|
110
|
+
try {
|
|
111
|
+
const recognizeTextCommand = new RecognizeTextCommand(params);
|
|
112
|
+
const data = await client.send(recognizeTextCommand);
|
|
113
|
+
this._reportBotStatus(data, botConfig);
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
return Promise.reject(err);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* handle client's `RecognizeUtteranceCommand`
|
|
122
|
+
* used for obj text or obj voice message
|
|
123
|
+
*/
|
|
124
|
+
async _handleRecognizeUtteranceCommand(botConfig, data, baseParams, client) {
|
|
125
|
+
const { content, options: { messageType }, } = data;
|
|
126
|
+
logger.debug('postContent to lex2', data);
|
|
127
|
+
let params;
|
|
128
|
+
// prepare params
|
|
129
|
+
if (messageType === 'voice') {
|
|
130
|
+
if (typeof content !== 'object') {
|
|
131
|
+
return Promise.reject('invalid content type');
|
|
132
|
+
}
|
|
133
|
+
const inputStream = content instanceof Uint8Array ? content : await convert(content);
|
|
134
|
+
params = {
|
|
135
|
+
...baseParams,
|
|
136
|
+
requestContentType: 'audio/x-l16; sample-rate=16000; channel-count=1',
|
|
137
|
+
inputStream,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
// text input
|
|
142
|
+
if (typeof content !== 'string')
|
|
143
|
+
return Promise.reject('invalid content type');
|
|
144
|
+
params = {
|
|
145
|
+
...baseParams,
|
|
146
|
+
requestContentType: 'text/plain; charset=utf-8',
|
|
147
|
+
inputStream: content,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
// make API call to lex
|
|
151
|
+
try {
|
|
152
|
+
const recognizeUtteranceCommand = new RecognizeUtteranceCommand(params);
|
|
153
|
+
const data = await client.send(recognizeUtteranceCommand);
|
|
154
|
+
const response = await this._formatUtteranceCommandOutput(data);
|
|
155
|
+
this._reportBotStatus(response, botConfig);
|
|
156
|
+
return response;
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
return Promise.reject(err);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export const lexProvider = new AWSLexV2Provider();
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { resolveBotConfig } from '../utils';
|
|
4
|
+
import { lexProvider } from '../AWSLexV2Provider';
|
|
5
|
+
import { assertValidationError, InteractionsValidationErrorCode, } from '../../errors';
|
|
6
|
+
export const onComplete = (input) => {
|
|
7
|
+
const { botName, callback } = input;
|
|
8
|
+
const botConfig = resolveBotConfig(botName);
|
|
9
|
+
assertValidationError(!!botConfig, InteractionsValidationErrorCode.NoBotConfig, `Bot ${botName} does not exist.`);
|
|
10
|
+
lexProvider.onComplete(botConfig, callback);
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { lexProvider } from '../AWSLexV2Provider';
|
|
4
|
+
import { resolveBotConfig } from '../utils';
|
|
5
|
+
import { assertValidationError, InteractionsValidationErrorCode, } from '../../errors';
|
|
6
|
+
export const send = async (input) => {
|
|
7
|
+
const { botName, message } = input;
|
|
8
|
+
const botConfig = resolveBotConfig(botName);
|
|
9
|
+
assertValidationError(!!botConfig, InteractionsValidationErrorCode.NoBotConfig, `Bot ${botName} does not exist.`);
|
|
10
|
+
return lexProvider.sendMessage(botConfig, message);
|
|
11
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SendInput, OnCompleteInput } from './inputs';
|
|
2
|
+
import { SendOutput } from './outputs';
|
|
3
|
+
export interface AWSLexV2ProviderOption {
|
|
4
|
+
name: string;
|
|
5
|
+
botId: string;
|
|
6
|
+
aliasId: string;
|
|
7
|
+
localeId: string;
|
|
8
|
+
region: string;
|
|
9
|
+
}
|
|
10
|
+
export interface IInteractions {
|
|
11
|
+
send(input: SendInput): Promise<SendOutput>;
|
|
12
|
+
onComplete(input: OnCompleteInput): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InteractionsSendInput, InteractionsOnCompleteInput } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Input type for LexV2 send API.
|
|
4
|
+
*/
|
|
5
|
+
export type SendInput = InteractionsSendInput;
|
|
6
|
+
/**
|
|
7
|
+
* Input type for LexV2 onComplete API.
|
|
8
|
+
*/
|
|
9
|
+
export type OnCompleteInput = InteractionsOnCompleteInput;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { resolveBotConfig } from './resolveBotConfig';
|