@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.
Files changed (261) hide show
  1. package/README.md +3 -0
  2. package/lex-v1/package.json +7 -0
  3. package/lex-v2/package.json +7 -0
  4. package/lib/errors/InteractionsError.d.ts +4 -0
  5. package/lib/errors/InteractionsError.js +16 -0
  6. package/lib/errors/assertValidationError.d.ts +2 -0
  7. package/lib/errors/assertValidationError.js +18 -0
  8. package/lib/errors/index.d.ts +2 -0
  9. package/lib/errors/index.js +9 -0
  10. package/lib/errors/validation.d.ts +5 -0
  11. package/lib/errors/validation.js +14 -0
  12. package/lib/index.d.ts +2 -5
  13. package/lib/index.js +3 -9
  14. package/lib/lex-v1/AWSLexProvider.d.ts +20 -0
  15. package/lib/lex-v1/AWSLexProvider.js +119 -0
  16. package/lib/lex-v1/apis/index.d.ts +2 -0
  17. package/lib/lex-v1/apis/index.js +9 -0
  18. package/lib/lex-v1/apis/onComplete.d.ts +2 -0
  19. package/lib/lex-v1/apis/onComplete.js +15 -0
  20. package/lib/lex-v1/apis/send.d.ts +2 -0
  21. package/lib/lex-v1/apis/send.js +15 -0
  22. package/lib/lex-v1/index.d.ts +6 -0
  23. package/lib/lex-v1/index.js +13 -0
  24. package/lib/lex-v1/types/AWSLexProviderOption.d.ts +11 -0
  25. package/lib/{types/Response.js → lex-v1/types/AWSLexProviderOption.js} +0 -1
  26. package/lib/lex-v1/types/index.d.ts +3 -0
  27. package/lib/lex-v1/types/index.js +4 -0
  28. package/lib/lex-v1/types/inputs.d.ts +9 -0
  29. package/lib/lex-v1/types/inputs.js +4 -0
  30. package/lib/lex-v1/types/outputs.d.ts +5 -0
  31. package/lib/lex-v1/types/outputs.js +4 -0
  32. package/lib/lex-v1/utils/index.d.ts +1 -0
  33. package/lib/lex-v1/utils/index.js +7 -0
  34. package/lib/lex-v1/utils/resolveBotConfig.d.ts +2 -0
  35. package/lib/lex-v1/utils/resolveBotConfig.js +13 -0
  36. package/lib/lex-v2/AWSLexV2Provider.d.ts +52 -0
  37. package/lib/lex-v2/AWSLexV2Provider.js +166 -0
  38. package/lib/lex-v2/apis/index.d.ts +2 -0
  39. package/lib/lex-v2/apis/index.js +9 -0
  40. package/lib/lex-v2/apis/onComplete.d.ts +2 -0
  41. package/lib/lex-v2/apis/onComplete.js +15 -0
  42. package/lib/lex-v2/apis/send.d.ts +2 -0
  43. package/lib/lex-v2/apis/send.js +15 -0
  44. package/lib/lex-v2/index.d.ts +3 -0
  45. package/lib/lex-v2/index.js +10 -0
  46. package/lib/lex-v2/types/AWSLexV2ProviderOption.d.ts +13 -0
  47. package/lib/lex-v2/types/AWSLexV2ProviderOption.js +4 -0
  48. package/lib/lex-v2/types/index.d.ts +3 -0
  49. package/lib/lex-v2/types/index.js +4 -0
  50. package/lib/lex-v2/types/inputs.d.ts +9 -0
  51. package/lib/lex-v2/types/inputs.js +4 -0
  52. package/lib/lex-v2/types/outputs.d.ts +5 -0
  53. package/lib/lex-v2/types/outputs.js +4 -0
  54. package/lib/lex-v2/utils/index.d.ts +1 -0
  55. package/lib/lex-v2/utils/index.js +7 -0
  56. package/lib/lex-v2/utils/resolveBotConfig.d.ts +2 -0
  57. package/lib/lex-v2/utils/resolveBotConfig.js +13 -0
  58. package/lib/tsconfig.tsbuildinfo +1 -0
  59. package/lib/types/Interactions.d.ts +7 -6
  60. package/lib/types/Interactions.js +2 -1
  61. package/lib/types/index.d.ts +2 -5
  62. package/lib/types/index.js +2 -1
  63. package/lib/types/inputs.d.ts +9 -0
  64. package/lib/types/inputs.js +4 -0
  65. package/lib/types/outputs.d.ts +2 -0
  66. package/lib/types/outputs.js +4 -0
  67. package/lib/utils/commonUtils.d.ts +1 -0
  68. package/lib/utils/commonUtils.js +19 -0
  69. package/lib/utils/index.d.ts +2 -0
  70. package/lib/utils/index.js +11 -0
  71. package/lib/utils/utils.js +32 -0
  72. package/lib/utils/utils.native.js +45 -0
  73. package/lib-esm/errors/InteractionsError.d.ts +4 -0
  74. package/lib-esm/errors/InteractionsError.js +12 -0
  75. package/lib-esm/errors/assertValidationError.d.ts +2 -0
  76. package/lib-esm/errors/assertValidationError.js +14 -0
  77. package/lib-esm/errors/index.d.ts +2 -0
  78. package/lib-esm/errors/index.js +4 -0
  79. package/lib-esm/errors/validation.d.ts +5 -0
  80. package/lib-esm/errors/validation.js +11 -0
  81. package/lib-esm/index.d.ts +2 -5
  82. package/lib-esm/index.js +1 -5
  83. package/lib-esm/lex-v1/AWSLexProvider.d.ts +20 -0
  84. package/lib-esm/lex-v1/AWSLexProvider.js +116 -0
  85. package/lib-esm/lex-v1/apis/index.d.ts +2 -0
  86. package/lib-esm/lex-v1/apis/index.js +4 -0
  87. package/lib-esm/lex-v1/apis/onComplete.d.ts +2 -0
  88. package/lib-esm/lex-v1/apis/onComplete.js +11 -0
  89. package/lib-esm/lex-v1/apis/send.d.ts +2 -0
  90. package/lib-esm/lex-v1/apis/send.js +11 -0
  91. package/lib-esm/lex-v1/index.d.ts +6 -0
  92. package/lib-esm/lex-v1/index.js +10 -0
  93. package/lib-esm/lex-v1/types/AWSLexProviderOption.d.ts +11 -0
  94. package/lib-esm/{types/Response.js → lex-v1/types/AWSLexProviderOption.js} +1 -1
  95. package/lib-esm/lex-v1/types/index.d.ts +3 -0
  96. package/lib-esm/lex-v1/types/index.js +3 -0
  97. package/lib-esm/lex-v1/types/inputs.d.ts +9 -0
  98. package/lib-esm/lex-v1/types/inputs.js +3 -0
  99. package/lib-esm/lex-v1/types/outputs.d.ts +5 -0
  100. package/lib-esm/lex-v1/types/outputs.js +3 -0
  101. package/lib-esm/lex-v1/utils/index.d.ts +1 -0
  102. package/lib-esm/lex-v1/utils/index.js +3 -0
  103. package/lib-esm/lex-v1/utils/resolveBotConfig.d.ts +2 -0
  104. package/lib-esm/lex-v1/utils/resolveBotConfig.js +9 -0
  105. package/lib-esm/lex-v2/AWSLexV2Provider.d.ts +52 -0
  106. package/lib-esm/lex-v2/AWSLexV2Provider.js +163 -0
  107. package/lib-esm/lex-v2/apis/index.d.ts +2 -0
  108. package/lib-esm/lex-v2/apis/index.js +4 -0
  109. package/lib-esm/lex-v2/apis/onComplete.d.ts +2 -0
  110. package/lib-esm/lex-v2/apis/onComplete.js +11 -0
  111. package/lib-esm/lex-v2/apis/send.d.ts +2 -0
  112. package/lib-esm/lex-v2/apis/send.js +11 -0
  113. package/lib-esm/lex-v2/index.d.ts +3 -0
  114. package/lib-esm/lex-v2/index.js +7 -0
  115. package/lib-esm/lex-v2/types/AWSLexV2ProviderOption.d.ts +13 -0
  116. package/lib-esm/lex-v2/types/AWSLexV2ProviderOption.js +3 -0
  117. package/lib-esm/lex-v2/types/index.d.ts +3 -0
  118. package/lib-esm/lex-v2/types/index.js +3 -0
  119. package/lib-esm/lex-v2/types/inputs.d.ts +9 -0
  120. package/lib-esm/lex-v2/types/inputs.js +3 -0
  121. package/lib-esm/lex-v2/types/outputs.d.ts +5 -0
  122. package/lib-esm/lex-v2/types/outputs.js +3 -0
  123. package/lib-esm/lex-v2/utils/index.d.ts +1 -0
  124. package/lib-esm/lex-v2/utils/index.js +3 -0
  125. package/lib-esm/lex-v2/utils/resolveBotConfig.d.ts +2 -0
  126. package/lib-esm/lex-v2/utils/resolveBotConfig.js +9 -0
  127. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  128. package/lib-esm/types/Interactions.d.ts +7 -6
  129. package/lib-esm/types/Interactions.js +3 -1
  130. package/lib-esm/types/index.d.ts +2 -5
  131. package/lib-esm/types/index.js +3 -1
  132. package/lib-esm/types/inputs.d.ts +9 -0
  133. package/lib-esm/types/inputs.js +3 -0
  134. package/lib-esm/types/outputs.d.ts +2 -0
  135. package/lib-esm/types/outputs.js +3 -0
  136. package/lib-esm/utils/commonUtils.d.ts +1 -0
  137. package/lib-esm/utils/commonUtils.js +15 -0
  138. package/lib-esm/utils/index.d.ts +2 -0
  139. package/lib-esm/utils/index.js +4 -0
  140. package/lib-esm/utils/utils.js +26 -0
  141. package/lib-esm/utils/utils.native.js +39 -0
  142. package/package.json +67 -28
  143. package/src/errors/InteractionsError.ts +18 -0
  144. package/src/errors/assertValidationError.ts +24 -0
  145. package/src/errors/index.ts +5 -0
  146. package/src/errors/validation.ts +15 -0
  147. package/src/index.ts +2 -5
  148. package/src/lex-v1/AWSLexProvider.ts +167 -0
  149. package/src/lex-v1/apis/index.ts +5 -0
  150. package/src/lex-v1/apis/onComplete.ts +21 -0
  151. package/src/lex-v1/apis/send.ts +21 -0
  152. package/src/lex-v1/index.ts +14 -0
  153. package/src/lex-v1/types/AWSLexProviderOption.ts +16 -0
  154. package/src/lex-v1/types/index.ts +6 -0
  155. package/src/lex-v1/types/inputs.ts +17 -0
  156. package/src/lex-v1/types/outputs.ts +9 -0
  157. package/src/{types/Response.ts → lex-v1/utils/index.ts} +1 -3
  158. package/src/lex-v1/utils/resolveBotConfig.ts +15 -0
  159. package/src/{Providers → lex-v2}/AWSLexV2Provider.ts +67 -137
  160. package/src/lex-v2/apis/index.ts +5 -0
  161. package/src/lex-v2/apis/onComplete.ts +21 -0
  162. package/src/lex-v2/apis/send.ts +21 -0
  163. package/src/lex-v2/index.ts +11 -0
  164. package/src/{types/Providers/AWSLexV2Provider.ts → lex-v2/types/AWSLexV2ProviderOption.ts} +7 -4
  165. package/src/lex-v2/types/index.ts +6 -0
  166. package/src/lex-v2/types/inputs.ts +17 -0
  167. package/src/lex-v2/types/outputs.ts +9 -0
  168. package/src/lex-v2/utils/index.ts +4 -0
  169. package/src/lex-v2/utils/resolveBotConfig.ts +15 -0
  170. package/src/types/Interactions.ts +9 -3
  171. package/src/types/index.ts +3 -5
  172. package/src/types/inputs.ts +17 -0
  173. package/src/types/outputs.ts +6 -0
  174. package/src/utils/index.ts +5 -0
  175. package/lib/.tsbuildinfo +0 -3
  176. package/lib/Interactions.d.ts +0 -24
  177. package/lib/Interactions.js +0 -123
  178. package/lib/Interactions.js.map +0 -1
  179. package/lib/Providers/AWSLexProvider.d.ts +0 -24
  180. package/lib/Providers/AWSLexProvider.js +0 -187
  181. package/lib/Providers/AWSLexProvider.js.map +0 -1
  182. package/lib/Providers/AWSLexProviderHelper/commonUtils.d.ts +0 -1
  183. package/lib/Providers/AWSLexProviderHelper/commonUtils.js +0 -29
  184. package/lib/Providers/AWSLexProviderHelper/commonUtils.js.map +0 -1
  185. package/lib/Providers/AWSLexProviderHelper/utils.js +0 -38
  186. package/lib/Providers/AWSLexProviderHelper/utils.js.map +0 -1
  187. package/lib/Providers/AWSLexProviderHelper/utils.native.js +0 -55
  188. package/lib/Providers/AWSLexProviderHelper/utils.native.js.map +0 -1
  189. package/lib/Providers/AWSLexV2Provider.d.ts +0 -58
  190. package/lib/Providers/AWSLexV2Provider.js +0 -282
  191. package/lib/Providers/AWSLexV2Provider.js.map +0 -1
  192. package/lib/Providers/InteractionsProvider.d.ts +0 -11
  193. package/lib/Providers/InteractionsProvider.js +0 -32
  194. package/lib/Providers/InteractionsProvider.js.map +0 -1
  195. package/lib/Providers/index.d.ts +0 -3
  196. package/lib/Providers/index.js +0 -11
  197. package/lib/Providers/index.js.map +0 -1
  198. package/lib/index.js.map +0 -1
  199. package/lib/types/Interactions.js.map +0 -1
  200. package/lib/types/Provider.d.ts +0 -12
  201. package/lib/types/Provider.js +0 -3
  202. package/lib/types/Provider.js.map +0 -1
  203. package/lib/types/Providers/AWSLexProvider.d.ts +0 -10
  204. package/lib/types/Providers/AWSLexProvider.js +0 -3
  205. package/lib/types/Providers/AWSLexProvider.js.map +0 -1
  206. package/lib/types/Providers/AWSLexV2Provider.d.ts +0 -12
  207. package/lib/types/Providers/AWSLexV2Provider.js +0 -3
  208. package/lib/types/Providers/AWSLexV2Provider.js.map +0 -1
  209. package/lib/types/Response.d.ts +0 -3
  210. package/lib/types/Response.js.map +0 -1
  211. package/lib/types/index.js.map +0 -1
  212. package/lib-esm/.tsbuildinfo +0 -3
  213. package/lib-esm/Interactions.d.ts +0 -24
  214. package/lib-esm/Interactions.js +0 -121
  215. package/lib-esm/Interactions.js.map +0 -1
  216. package/lib-esm/Providers/AWSLexProvider.d.ts +0 -24
  217. package/lib-esm/Providers/AWSLexProvider.js +0 -185
  218. package/lib-esm/Providers/AWSLexProvider.js.map +0 -1
  219. package/lib-esm/Providers/AWSLexProviderHelper/commonUtils.d.ts +0 -1
  220. package/lib-esm/Providers/AWSLexProviderHelper/commonUtils.js +0 -27
  221. package/lib-esm/Providers/AWSLexProviderHelper/commonUtils.js.map +0 -1
  222. package/lib-esm/Providers/AWSLexProviderHelper/utils.js +0 -36
  223. package/lib-esm/Providers/AWSLexProviderHelper/utils.js.map +0 -1
  224. package/lib-esm/Providers/AWSLexProviderHelper/utils.native.js +0 -53
  225. package/lib-esm/Providers/AWSLexProviderHelper/utils.native.js.map +0 -1
  226. package/lib-esm/Providers/AWSLexV2Provider.d.ts +0 -58
  227. package/lib-esm/Providers/AWSLexV2Provider.js +0 -280
  228. package/lib-esm/Providers/AWSLexV2Provider.js.map +0 -1
  229. package/lib-esm/Providers/InteractionsProvider.d.ts +0 -11
  230. package/lib-esm/Providers/InteractionsProvider.js +0 -30
  231. package/lib-esm/Providers/InteractionsProvider.js.map +0 -1
  232. package/lib-esm/Providers/index.d.ts +0 -3
  233. package/lib-esm/Providers/index.js +0 -6
  234. package/lib-esm/Providers/index.js.map +0 -1
  235. package/lib-esm/index.js.map +0 -1
  236. package/lib-esm/types/Interactions.js.map +0 -1
  237. package/lib-esm/types/Provider.d.ts +0 -12
  238. package/lib-esm/types/Provider.js +0 -1
  239. package/lib-esm/types/Provider.js.map +0 -1
  240. package/lib-esm/types/Providers/AWSLexProvider.d.ts +0 -10
  241. package/lib-esm/types/Providers/AWSLexProvider.js +0 -1
  242. package/lib-esm/types/Providers/AWSLexProvider.js.map +0 -1
  243. package/lib-esm/types/Providers/AWSLexV2Provider.d.ts +0 -12
  244. package/lib-esm/types/Providers/AWSLexV2Provider.js +0 -1
  245. package/lib-esm/types/Providers/AWSLexV2Provider.js.map +0 -1
  246. package/lib-esm/types/Response.d.ts +0 -3
  247. package/lib-esm/types/Response.js.map +0 -1
  248. package/lib-esm/types/index.js.map +0 -1
  249. package/src/Interactions.ts +0 -162
  250. package/src/Providers/AWSLexProvider.ts +0 -212
  251. package/src/Providers/InteractionsProvider.ts +0 -49
  252. package/src/Providers/index.ts +0 -5
  253. package/src/types/Provider.ts +0 -26
  254. package/src/types/Providers/AWSLexProvider.ts +0 -13
  255. /package/lib/{Providers/AWSLexProviderHelper → utils}/utils.d.ts +0 -0
  256. /package/lib/{Providers/AWSLexProviderHelper → utils}/utils.native.d.ts +0 -0
  257. /package/lib-esm/{Providers/AWSLexProviderHelper → utils}/utils.d.ts +0 -0
  258. /package/lib-esm/{Providers/AWSLexProviderHelper → utils}/utils.native.d.ts +0 -0
  259. /package/src/{Providers/AWSLexProviderHelper → utils}/commonUtils.ts +0 -0
  260. /package/src/{Providers/AWSLexProviderHelper → utils}/utils.native.ts +0 -0
  261. /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
- InteractionsOptions,
6
- AWSLexV2ProviderOptions,
7
- InteractionsResponse,
4
+ InteractionsOnCompleteCallback,
8
5
  InteractionsMessage,
9
- } from '../types';
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
- ConsoleLogger as Logger,
21
- Credentials,
22
- getAmplifyUserAgentObject,
23
- } from '@aws-amplify/core';
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 Logger('AWSLexV2Provider');
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
- export class AWSLexV2Provider extends AbstractInteractionsProvider {
57
- private _lexRuntimeServiceV2Client: LexRuntimeV2Client;
58
- private _botsCompleteCallback: object;
59
-
60
- /**
61
- * Initialize Interactions with AWS configurations
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 {string} botname - Bot name to send the message
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
- botname: string,
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 credentials;
72
+ let session;
123
73
  try {
124
- credentials = await Credentials.get();
74
+ session = await fetchAuthSession();
125
75
  } catch (error) {
126
76
  return Promise.reject('No credentials');
127
77
  }
128
78
 
129
- this._lexRuntimeServiceV2Client = new LexRuntimeV2Client({
130
- region: this._config[botname].region,
131
- credentials,
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: this._config[botname].aliasId,
140
- botId: this._config[botname].botId,
141
- localeId: this._config[botname].localeId,
142
- sessionId: credentials.identityId,
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
- botname,
98
+ botConfig,
148
99
  message,
149
- reqBaseParams
100
+ reqBaseParams,
101
+ client
150
102
  );
151
103
  } else {
152
104
  response = await this._handleRecognizeUtteranceCommand(
153
- botname,
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 {string} botname - Bot name to attach the onComplete callback
165
- * @param {(err: Error | null, confirmation: InteractionsResponse) => void} callback - called when Intent Fulfilled
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
- botname: string,
169
- callback: (err: Error | null, confirmation: InteractionsResponse) => void
121
+ { name }: AWSLexV2ProviderOption,
122
+ callback: InteractionsOnCompleteCallback
170
123
  ) {
171
- // does bot exist
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
- private _reportBotStatus(
130
+ _reportBotStatus(
183
131
  data: AWSLexV2ProviderSendResponse,
184
- botname: string
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
- const isConfigOnCompleteAttached =
192
- typeof this._config?.[botname].onComplete === 'function';
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
- if (sessionState?.intent?.state === 'Failed') {
214
- const error = new Error('Bot conversation failed');
215
- if (isApiOnCompleteAttached) {
216
- setTimeout(() => this._botsCompleteCallback[botname](error), 0);
217
- }
218
-
219
- if (isConfigOnCompleteAttached) {
220
- setTimeout(() => this._config[botname].onComplete(error), 0);
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
- const response: RecognizeUtteranceCommandOutputFormatted = {
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
- botname: string,
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 this._lexRuntimeServiceV2Client.send(
266
- recognizeTextCommand
267
- );
196
+ const data = await client.send(recognizeTextCommand);
268
197
 
269
- this._reportBotStatus(data, botname);
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
- botname: string,
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 this._lexRuntimeServiceV2Client.send(
323
- recognizeUtteranceCommand
324
- );
252
+ const data = await client.send(recognizeUtteranceCommand);
325
253
 
326
254
  const response = await this._formatUtteranceCommandOutput(data);
327
- this._reportBotStatus(response, botname);
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,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { send } from './send';
5
+ export { onComplete } from './onComplete';
@@ -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 AWSLexV2ProviderOptions {
14
- [key: string]: AWSLexV2ProviderOption;
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,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { resolveBotConfig } from './resolveBotConfig';
@@ -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
+ };
@@ -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
- export * from './Interactions';
4
- export * from './Provider';
5
- export * from './Providers/AWSLexProvider';
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
+ };
@@ -0,0 +1,6 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { InteractionsResponse } from './Interactions';
5
+
6
+ export type InteractionsSendOutput = InteractionsResponse;
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { convert, base64ToArrayBuffer, gzipDecompressToString } from './utils';
5
+ export { unGzipBase64AsJson } from './commonUtils';
package/lib/.tsbuildinfo DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "version": "3.8.3"
3
- }
@@ -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;