@aws-amplify/interactions 5.2.12-api-v6-models.b3abc9b.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
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { AbstractInteractionsProvider } from './InteractionsProvider';
|
|
4
3
|
import {
|
|
5
|
-
|
|
6
|
-
AWSLexV2ProviderOptions,
|
|
7
|
-
InteractionsResponse,
|
|
4
|
+
InteractionsOnCompleteCallback,
|
|
8
5
|
InteractionsMessage,
|
|
9
|
-
|
|
6
|
+
InteractionsResponse,
|
|
7
|
+
} from '../types/Interactions';
|
|
10
8
|
import {
|
|
9
|
+
IntentState,
|
|
11
10
|
LexRuntimeV2Client,
|
|
12
11
|
RecognizeTextCommand,
|
|
13
12
|
RecognizeTextCommandInput,
|
|
@@ -16,15 +15,13 @@ import {
|
|
|
16
15
|
RecognizeUtteranceCommandInput,
|
|
17
16
|
RecognizeUtteranceCommandOutput,
|
|
18
17
|
} from '@aws-sdk/client-lex-runtime-v2';
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
} from '
|
|
24
|
-
import { convert } from './AWSLexProviderHelper/utils';
|
|
25
|
-
import { unGzipBase64AsJson } from './AWSLexProviderHelper/commonUtils';
|
|
18
|
+
import { getAmplifyUserAgentObject } from '@aws-amplify/core/internals/utils';
|
|
19
|
+
import { ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';
|
|
20
|
+
import { convert, unGzipBase64AsJson } from '../utils';
|
|
21
|
+
import { v4 as uuid } from 'uuid';
|
|
22
|
+
import { AWSLexV2ProviderOption } from './types';
|
|
26
23
|
|
|
27
|
-
const logger = new
|
|
24
|
+
const logger = new ConsoleLogger('AWSLexV2Provider');
|
|
28
25
|
|
|
29
26
|
interface RecognizeUtteranceCommandOutputFormatted
|
|
30
27
|
extends Omit<
|
|
@@ -53,82 +50,36 @@ type lexV2BaseReqParams = {
|
|
|
53
50
|
sessionId: string;
|
|
54
51
|
};
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* @param {InteractionsOptions} options - Configuration object for Interactions
|
|
63
|
-
*/
|
|
64
|
-
constructor(options: InteractionsOptions = {}) {
|
|
65
|
-
super(options);
|
|
66
|
-
this._botsCompleteCallback = {};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* get provider name of the plugin
|
|
71
|
-
* @returns {string} name of the provider
|
|
72
|
-
*/
|
|
73
|
-
public getProviderName() {
|
|
74
|
-
return 'AWSLexV2Provider';
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Configure Interactions part with aws configuration
|
|
79
|
-
* @param {AWSLexV2ProviderOptions} config - Configuration of the Interactions
|
|
80
|
-
* @return {AWSLexV2ProviderOptions} - Current configuration
|
|
81
|
-
*/
|
|
82
|
-
public configure(
|
|
83
|
-
config: AWSLexV2ProviderOptions = {}
|
|
84
|
-
): AWSLexV2ProviderOptions {
|
|
85
|
-
const propertiesToTest = [
|
|
86
|
-
'name',
|
|
87
|
-
'botId',
|
|
88
|
-
'aliasId',
|
|
89
|
-
'localeId',
|
|
90
|
-
'providerName',
|
|
91
|
-
'region',
|
|
92
|
-
];
|
|
93
|
-
|
|
94
|
-
Object.keys(config).forEach(botKey => {
|
|
95
|
-
const botConfig = config[botKey];
|
|
96
|
-
|
|
97
|
-
// is bot config correct
|
|
98
|
-
if (!propertiesToTest.every(x => x in botConfig)) {
|
|
99
|
-
throw new Error('invalid bot configuration');
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
return super.configure(config);
|
|
103
|
-
}
|
|
53
|
+
class AWSLexV2Provider {
|
|
54
|
+
private readonly _botsCompleteCallback: Record<
|
|
55
|
+
string,
|
|
56
|
+
InteractionsOnCompleteCallback
|
|
57
|
+
> = {};
|
|
58
|
+
private defaultSessionId: string = uuid();
|
|
104
59
|
|
|
105
60
|
/**
|
|
106
61
|
* Send a message to a bot
|
|
107
62
|
* @async
|
|
108
|
-
* @param {
|
|
63
|
+
* @param {AWSLexV2ProviderOption} botConfig - Bot configuration for sending the message
|
|
109
64
|
* @param {string | InteractionsMessage} message - message to send to the bot
|
|
110
65
|
* @return {Promise<InteractionsResponse>} A promise resolves to the response from the bot
|
|
111
66
|
*/
|
|
112
67
|
public async sendMessage(
|
|
113
|
-
|
|
68
|
+
botConfig: AWSLexV2ProviderOption,
|
|
114
69
|
message: string | InteractionsMessage
|
|
115
70
|
): Promise<InteractionsResponse> {
|
|
116
|
-
// check if bot exists
|
|
117
|
-
if (!this._config[botname]) {
|
|
118
|
-
return Promise.reject('Bot ' + botname + ' does not exist');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
71
|
// check if credentials are present
|
|
122
|
-
let
|
|
72
|
+
let session;
|
|
123
73
|
try {
|
|
124
|
-
|
|
74
|
+
session = await fetchAuthSession();
|
|
125
75
|
} catch (error) {
|
|
126
76
|
return Promise.reject('No credentials');
|
|
127
77
|
}
|
|
128
78
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
79
|
+
const { region, aliasId, localeId, botId } = botConfig;
|
|
80
|
+
const client = new LexRuntimeV2Client({
|
|
81
|
+
region,
|
|
82
|
+
credentials: session.credentials,
|
|
132
83
|
customUserAgent: getAmplifyUserAgentObject(),
|
|
133
84
|
});
|
|
134
85
|
|
|
@@ -136,23 +87,25 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
136
87
|
|
|
137
88
|
// common base params for all requests
|
|
138
89
|
const reqBaseParams: lexV2BaseReqParams = {
|
|
139
|
-
botAliasId:
|
|
140
|
-
botId
|
|
141
|
-
localeId
|
|
142
|
-
sessionId:
|
|
90
|
+
botAliasId: aliasId,
|
|
91
|
+
botId,
|
|
92
|
+
localeId,
|
|
93
|
+
sessionId: session.identityId ?? this.defaultSessionId,
|
|
143
94
|
};
|
|
144
95
|
|
|
145
96
|
if (typeof message === 'string') {
|
|
146
97
|
response = await this._handleRecognizeTextCommand(
|
|
147
|
-
|
|
98
|
+
botConfig,
|
|
148
99
|
message,
|
|
149
|
-
reqBaseParams
|
|
100
|
+
reqBaseParams,
|
|
101
|
+
client
|
|
150
102
|
);
|
|
151
103
|
} else {
|
|
152
104
|
response = await this._handleRecognizeUtteranceCommand(
|
|
153
|
-
|
|
105
|
+
botConfig,
|
|
154
106
|
message,
|
|
155
|
-
reqBaseParams
|
|
107
|
+
reqBaseParams,
|
|
108
|
+
client
|
|
156
109
|
);
|
|
157
110
|
}
|
|
158
111
|
return response;
|
|
@@ -161,64 +114,42 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
161
114
|
/**
|
|
162
115
|
* Attach a onComplete callback function to a bot.
|
|
163
116
|
* The callback is called once the bot's intent is fulfilled
|
|
164
|
-
* @param {
|
|
165
|
-
* @param {
|
|
117
|
+
* @param {AWSLexV2ProviderOption} botConfig - Bot configuration to attach the onComplete callback
|
|
118
|
+
* @param {InteractionsOnCompleteCallback} callback - called when Intent Fulfilled
|
|
166
119
|
*/
|
|
167
120
|
public onComplete(
|
|
168
|
-
|
|
169
|
-
callback:
|
|
121
|
+
{ name }: AWSLexV2ProviderOption,
|
|
122
|
+
callback: InteractionsOnCompleteCallback
|
|
170
123
|
) {
|
|
171
|
-
|
|
172
|
-
if (!this._config[botname]) {
|
|
173
|
-
throw new Error('Bot ' + botname + ' does not exist');
|
|
174
|
-
}
|
|
175
|
-
this._botsCompleteCallback[botname] = callback;
|
|
124
|
+
this._botsCompleteCallback[name] = callback;
|
|
176
125
|
}
|
|
177
126
|
|
|
178
127
|
/**
|
|
179
|
-
* @private
|
|
180
128
|
* call onComplete callback for a bot if configured
|
|
181
129
|
*/
|
|
182
|
-
|
|
130
|
+
_reportBotStatus(
|
|
183
131
|
data: AWSLexV2ProviderSendResponse,
|
|
184
|
-
|
|
132
|
+
{ name }: AWSLexV2ProviderOption
|
|
185
133
|
) {
|
|
186
134
|
const sessionState = data?.sessionState;
|
|
187
135
|
|
|
188
136
|
// Check if state is fulfilled to resolve onFullfilment promise
|
|
189
137
|
logger.debug('postContent state', sessionState?.intent?.state);
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const isApiOnCompleteAttached =
|
|
195
|
-
typeof this._botsCompleteCallback?.[botname] === 'function';
|
|
196
|
-
|
|
197
|
-
// no onComplete callbacks added
|
|
198
|
-
if (!isConfigOnCompleteAttached && !isApiOnCompleteAttached) return;
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
sessionState?.intent?.state === 'ReadyForFulfillment' ||
|
|
202
|
-
sessionState?.intent?.state === 'Fulfilled'
|
|
203
|
-
) {
|
|
204
|
-
if (isApiOnCompleteAttached) {
|
|
205
|
-
setTimeout(() => this._botsCompleteCallback?.[botname](null, data), 0);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (isConfigOnCompleteAttached) {
|
|
209
|
-
setTimeout(() => this._config[botname].onComplete(null, data), 0);
|
|
210
|
-
}
|
|
138
|
+
const callback = this._botsCompleteCallback[name];
|
|
139
|
+
if (!callback) {
|
|
140
|
+
return;
|
|
211
141
|
}
|
|
212
142
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
143
|
+
switch (sessionState?.intent?.state) {
|
|
144
|
+
case IntentState.READY_FOR_FULFILLMENT:
|
|
145
|
+
case IntentState.FULFILLED:
|
|
146
|
+
callback(undefined, data);
|
|
147
|
+
break;
|
|
148
|
+
case IntentState.FAILED:
|
|
149
|
+
callback(new Error('Bot conversation failed'));
|
|
150
|
+
break;
|
|
151
|
+
default:
|
|
152
|
+
break;
|
|
222
153
|
}
|
|
223
154
|
}
|
|
224
155
|
|
|
@@ -230,7 +161,7 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
230
161
|
private async _formatUtteranceCommandOutput(
|
|
231
162
|
data: RecognizeUtteranceCommandOutput
|
|
232
163
|
): Promise<RecognizeUtteranceCommandOutputFormatted> {
|
|
233
|
-
|
|
164
|
+
return {
|
|
234
165
|
...data,
|
|
235
166
|
messages: await unGzipBase64AsJson(data.messages),
|
|
236
167
|
sessionState: await unGzipBase64AsJson(data.sessionState),
|
|
@@ -241,7 +172,6 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
241
172
|
? await convert(data.audioStream)
|
|
242
173
|
: undefined,
|
|
243
174
|
};
|
|
244
|
-
return response;
|
|
245
175
|
}
|
|
246
176
|
|
|
247
177
|
/**
|
|
@@ -249,9 +179,10 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
249
179
|
* used for sending simple text message
|
|
250
180
|
*/
|
|
251
181
|
private async _handleRecognizeTextCommand(
|
|
252
|
-
|
|
182
|
+
botConfig: AWSLexV2ProviderOption,
|
|
253
183
|
data: string,
|
|
254
|
-
baseParams: lexV2BaseReqParams
|
|
184
|
+
baseParams: lexV2BaseReqParams,
|
|
185
|
+
client: LexRuntimeV2Client
|
|
255
186
|
) {
|
|
256
187
|
logger.debug('postText to lex2', data);
|
|
257
188
|
|
|
@@ -262,11 +193,9 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
262
193
|
|
|
263
194
|
try {
|
|
264
195
|
const recognizeTextCommand = new RecognizeTextCommand(params);
|
|
265
|
-
const data = await
|
|
266
|
-
recognizeTextCommand
|
|
267
|
-
);
|
|
196
|
+
const data = await client.send(recognizeTextCommand);
|
|
268
197
|
|
|
269
|
-
this._reportBotStatus(data,
|
|
198
|
+
this._reportBotStatus(data, botConfig);
|
|
270
199
|
return data;
|
|
271
200
|
} catch (err) {
|
|
272
201
|
return Promise.reject(err);
|
|
@@ -278,9 +207,10 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
278
207
|
* used for obj text or obj voice message
|
|
279
208
|
*/
|
|
280
209
|
private async _handleRecognizeUtteranceCommand(
|
|
281
|
-
|
|
210
|
+
botConfig: AWSLexV2ProviderOption,
|
|
282
211
|
data: InteractionsMessage,
|
|
283
|
-
baseParams: lexV2BaseReqParams
|
|
212
|
+
baseParams: lexV2BaseReqParams,
|
|
213
|
+
client: LexRuntimeV2Client
|
|
284
214
|
) {
|
|
285
215
|
const {
|
|
286
216
|
content,
|
|
@@ -319,15 +249,15 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
|
|
|
319
249
|
// make API call to lex
|
|
320
250
|
try {
|
|
321
251
|
const recognizeUtteranceCommand = new RecognizeUtteranceCommand(params);
|
|
322
|
-
const data = await
|
|
323
|
-
recognizeUtteranceCommand
|
|
324
|
-
);
|
|
252
|
+
const data = await client.send(recognizeUtteranceCommand);
|
|
325
253
|
|
|
326
254
|
const response = await this._formatUtteranceCommandOutput(data);
|
|
327
|
-
this._reportBotStatus(response,
|
|
255
|
+
this._reportBotStatus(response, botConfig);
|
|
328
256
|
return response;
|
|
329
257
|
} catch (err) {
|
|
330
258
|
return Promise.reject(err);
|
|
331
259
|
}
|
|
332
260
|
}
|
|
333
261
|
}
|
|
262
|
+
|
|
263
|
+
export const lexProvider = new AWSLexV2Provider();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { OnCompleteInput } from '../types';
|
|
5
|
+
import { resolveBotConfig } from '../utils';
|
|
6
|
+
import { lexProvider } from '../AWSLexV2Provider';
|
|
7
|
+
import {
|
|
8
|
+
assertValidationError,
|
|
9
|
+
InteractionsValidationErrorCode,
|
|
10
|
+
} from '../../errors';
|
|
11
|
+
|
|
12
|
+
export const onComplete = (input: OnCompleteInput): void => {
|
|
13
|
+
const { botName, callback } = input;
|
|
14
|
+
const botConfig = resolveBotConfig(botName);
|
|
15
|
+
assertValidationError(
|
|
16
|
+
!!botConfig,
|
|
17
|
+
InteractionsValidationErrorCode.NoBotConfig,
|
|
18
|
+
`Bot ${botName} does not exist.`
|
|
19
|
+
);
|
|
20
|
+
lexProvider.onComplete(botConfig, callback);
|
|
21
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { SendInput, SendOutput } from '../types';
|
|
5
|
+
import { lexProvider } from '../AWSLexV2Provider';
|
|
6
|
+
import { resolveBotConfig } from '../utils';
|
|
7
|
+
import {
|
|
8
|
+
assertValidationError,
|
|
9
|
+
InteractionsValidationErrorCode,
|
|
10
|
+
} from '../../errors';
|
|
11
|
+
|
|
12
|
+
export const send = async (input: SendInput): Promise<SendOutput> => {
|
|
13
|
+
const { botName, message } = input;
|
|
14
|
+
const botConfig = resolveBotConfig(botName);
|
|
15
|
+
assertValidationError(
|
|
16
|
+
!!botConfig,
|
|
17
|
+
InteractionsValidationErrorCode.NoBotConfig,
|
|
18
|
+
`Bot ${botName} does not exist.`
|
|
19
|
+
);
|
|
20
|
+
return lexProvider.sendMessage(botConfig, message);
|
|
21
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { send, onComplete } from './apis';
|
|
5
|
+
import { IInteractions } from './types/AWSLexV2ProviderOption';
|
|
6
|
+
|
|
7
|
+
export const Interactions: IInteractions = {
|
|
8
|
+
send,
|
|
9
|
+
onComplete,
|
|
10
|
+
};
|
|
11
|
+
export { SendInput, OnCompleteInput, SendOutput } from './types';
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { SendInput, OnCompleteInput } from './inputs';
|
|
5
|
+
import { SendOutput } from './outputs';
|
|
6
|
+
|
|
3
7
|
export interface AWSLexV2ProviderOption {
|
|
4
8
|
name: string;
|
|
5
9
|
botId: string;
|
|
6
10
|
aliasId: string;
|
|
7
11
|
localeId: string;
|
|
8
12
|
region: string;
|
|
9
|
-
providerName: string;
|
|
10
|
-
onComplete?(botname: string, callback: (err, confirmation) => void): void;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
export interface
|
|
14
|
-
|
|
15
|
+
export interface IInteractions {
|
|
16
|
+
send(input: SendInput): Promise<SendOutput>;
|
|
17
|
+
onComplete(input: OnCompleteInput): void;
|
|
15
18
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export { AWSLexV2ProviderOption } from './AWSLexV2ProviderOption';
|
|
5
|
+
export { SendInput, OnCompleteInput } from './inputs';
|
|
6
|
+
export { SendOutput } from './outputs';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
InteractionsSendInput,
|
|
6
|
+
InteractionsOnCompleteInput,
|
|
7
|
+
} from '../../types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Input type for LexV2 send API.
|
|
11
|
+
*/
|
|
12
|
+
export type SendInput = InteractionsSendInput;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Input type for LexV2 onComplete API.
|
|
16
|
+
*/
|
|
17
|
+
export type OnCompleteInput = InteractionsOnCompleteInput;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { InteractionsSendOutput } from '../../types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Output type for LexV2 send API.
|
|
8
|
+
*/
|
|
9
|
+
export type SendOutput = InteractionsSendOutput;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { AWSLexV2ProviderOption } from '../types';
|
|
5
|
+
import { Amplify } from '@aws-amplify/core';
|
|
6
|
+
|
|
7
|
+
export const resolveBotConfig = (
|
|
8
|
+
botName: string
|
|
9
|
+
): AWSLexV2ProviderOption | undefined => {
|
|
10
|
+
const { [botName]: botConfig = undefined } =
|
|
11
|
+
Amplify.getConfig().Interactions?.LexV2 ?? {};
|
|
12
|
+
if (botConfig !== undefined) {
|
|
13
|
+
return { ...botConfig, name: botName };
|
|
14
|
+
}
|
|
15
|
+
};
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
export interface InteractionsOptions {
|
|
4
|
-
[key: string]: any;
|
|
5
|
-
}
|
|
6
3
|
|
|
7
4
|
export type InteractionsTextMessage = {
|
|
8
5
|
content: string;
|
|
@@ -21,3 +18,12 @@ export type InteractionsVoiceMessage = {
|
|
|
21
18
|
export type InteractionsMessage =
|
|
22
19
|
| InteractionsTextMessage
|
|
23
20
|
| InteractionsVoiceMessage;
|
|
21
|
+
|
|
22
|
+
export type InteractionsOnCompleteCallback = (
|
|
23
|
+
error?: Error,
|
|
24
|
+
completion?: InteractionsResponse
|
|
25
|
+
) => void;
|
|
26
|
+
|
|
27
|
+
export type InteractionsResponse = {
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
};
|
package/src/types/index.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export * from './Providers/AWSLexV2Provider';
|
|
7
|
-
export * from './Response';
|
|
3
|
+
|
|
4
|
+
export { InteractionsSendInput, InteractionsOnCompleteInput } from './inputs';
|
|
5
|
+
export { InteractionsSendOutput } from './outputs';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
InteractionsMessage,
|
|
6
|
+
InteractionsOnCompleteCallback,
|
|
7
|
+
} from './Interactions';
|
|
8
|
+
|
|
9
|
+
export type InteractionsSendInput = {
|
|
10
|
+
botName: string;
|
|
11
|
+
message: string | InteractionsMessage;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type InteractionsOnCompleteInput = {
|
|
15
|
+
botName: string;
|
|
16
|
+
callback: InteractionsOnCompleteCallback;
|
|
17
|
+
};
|
package/lib/.tsbuildinfo
DELETED
package/lib/Interactions.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { InteractionsOptions, InteractionsProvider, InteractionsMessage, InteractionsResponse } from './types';
|
|
2
|
-
export declare class InteractionsClass {
|
|
3
|
-
private _options;
|
|
4
|
-
private _pluggables;
|
|
5
|
-
/**
|
|
6
|
-
* Initialize PubSub with AWS configurations
|
|
7
|
-
*
|
|
8
|
-
* @param {InteractionsOptions} options - Configuration object for Interactions
|
|
9
|
-
*/
|
|
10
|
-
constructor(options?: InteractionsOptions);
|
|
11
|
-
getModuleName(): string;
|
|
12
|
-
/**
|
|
13
|
-
*
|
|
14
|
-
* @param {InteractionsOptions} options - Configuration object for Interactions
|
|
15
|
-
* @return {InteractionsOptions} - The current configuration
|
|
16
|
-
*/
|
|
17
|
-
configure(options: InteractionsOptions): InteractionsOptions;
|
|
18
|
-
addPluggable(pluggable: InteractionsProvider): void;
|
|
19
|
-
send(botname: string, message: string): Promise<InteractionsResponse>;
|
|
20
|
-
send(botname: string, message: InteractionsMessage): Promise<InteractionsResponse>;
|
|
21
|
-
send(botname: string, message: object): Promise<InteractionsResponse>;
|
|
22
|
-
onComplete(botname: string, callback: (err: any, confirmation: any) => void): void;
|
|
23
|
-
}
|
|
24
|
-
export declare const Interactions: InteractionsClass;
|