@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.
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,5 +1,2 @@
1
- export { Interactions } from './Interactions';
2
- export * from './types';
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 './Interactions';
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,2 @@
1
+ export { send } from './send';
2
+ export { onComplete } from './onComplete';
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { send } from './send';
4
+ export { onComplete } from './onComplete';
@@ -0,0 +1,2 @@
1
+ import { OnCompleteInput } from '../types';
2
+ export declare const onComplete: (input: OnCompleteInput) => void;
@@ -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,2 @@
1
+ import { SendInput, SendOutput } from '../types';
2
+ export declare const send: (input: SendInput) => Promise<SendOutput>;
@@ -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,6 @@
1
+ import { IInteractions } from './types/AWSLexProviderOption';
2
+ /**
3
+ * @deprecated recommend to migrate to AWS Lex V2 instead
4
+ * */
5
+ export declare const Interactions: IInteractions;
6
+ export { SendInput, OnCompleteInput, SendOutput } from './types';
@@ -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
+ }
@@ -1,3 +1,3 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- //# sourceMappingURL=Response.js.map
3
+ export {};
@@ -0,0 +1,3 @@
1
+ export { AWSLexProviderOption } from './AWSLexProviderOption';
2
+ export { SendInput, OnCompleteInput } from './inputs';
3
+ export { SendOutput } from './outputs';
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -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,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -0,0 +1,5 @@
1
+ import { InteractionsSendOutput } from '../../types';
2
+ /**
3
+ * Output type for LexV1 send API.
4
+ */
5
+ export type SendOutput = InteractionsSendOutput;
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -0,0 +1 @@
1
+ export { resolveBotConfig } from './resolveBotConfig';
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { resolveBotConfig } from './resolveBotConfig';
@@ -0,0 +1,2 @@
1
+ import { AWSLexProviderOption } from '../types';
2
+ export declare const resolveBotConfig: (botName: string) => AWSLexProviderOption | undefined;
@@ -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,2 @@
1
+ export { send } from './send';
2
+ export { onComplete } from './onComplete';
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { send } from './send';
4
+ export { onComplete } from './onComplete';
@@ -0,0 +1,2 @@
1
+ import { OnCompleteInput } from '../types';
2
+ export declare const onComplete: (input: OnCompleteInput) => void;
@@ -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,2 @@
1
+ import { SendInput, SendOutput } from '../types';
2
+ export declare const send: (input: SendInput) => Promise<SendOutput>;
@@ -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,3 @@
1
+ import { IInteractions } from './types/AWSLexV2ProviderOption';
2
+ export declare const Interactions: IInteractions;
3
+ export { SendInput, OnCompleteInput, SendOutput } from './types';
@@ -0,0 +1,7 @@
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
+ export const Interactions = {
5
+ send,
6
+ onComplete,
7
+ };
@@ -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,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -0,0 +1,3 @@
1
+ export { AWSLexV2ProviderOption } from './AWSLexV2ProviderOption';
2
+ export { SendInput, OnCompleteInput } from './inputs';
3
+ export { SendOutput } from './outputs';
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -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,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -0,0 +1,5 @@
1
+ import { InteractionsSendOutput } from '../../types';
2
+ /**
3
+ * Output type for LexV2 send API.
4
+ */
5
+ export type SendOutput = InteractionsSendOutput;
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -0,0 +1 @@
1
+ export { resolveBotConfig } from './resolveBotConfig';
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { resolveBotConfig } from './resolveBotConfig';
@@ -0,0 +1,2 @@
1
+ import { AWSLexV2ProviderOption } from '../types';
2
+ export declare const resolveBotConfig: (botName: string) => AWSLexV2ProviderOption | undefined;