@aws-amplify/ui-react 1.2.26 → 1.2.54-legacy-ui.10

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 (77) hide show
  1. package/CHANGELOG.md +224 -0
  2. package/dist/0.js.map +1 -1
  3. package/dist/1.js +656 -2000
  4. package/dist/1.js.map +1 -1
  5. package/dist/10.js +23 -681
  6. package/dist/10.js.map +1 -1
  7. package/dist/11.js +1157 -20
  8. package/dist/11.js.map +1 -1
  9. package/dist/12.js +61 -1087
  10. package/dist/12.js.map +1 -1
  11. package/dist/13.js +1146 -64
  12. package/dist/13.js.map +1 -1
  13. package/dist/14.js +34 -1163
  14. package/dist/14.js.map +1 -1
  15. package/dist/15.js +17 -37
  16. package/dist/15.js.map +1 -1
  17. package/dist/16.js +109 -13
  18. package/dist/16.js.map +1 -1
  19. package/dist/17.js +32 -67
  20. package/dist/17.js.map +1 -1
  21. package/dist/18.js +236 -46
  22. package/dist/18.js.map +1 -1
  23. package/dist/19.js +35 -224
  24. package/dist/19.js.map +1 -1
  25. package/dist/2.js +2190 -159
  26. package/dist/2.js.map +1 -1
  27. package/dist/20.js +28 -48
  28. package/dist/20.js.map +1 -1
  29. package/dist/21.js +46 -25
  30. package/dist/21.js.map +1 -1
  31. package/dist/22.js +22 -25
  32. package/dist/22.js.map +1 -1
  33. package/dist/23.js +254 -25
  34. package/dist/23.js.map +1 -1
  35. package/dist/24.js +62 -230
  36. package/dist/24.js.map +1 -1
  37. package/dist/25.js +28 -62
  38. package/dist/25.js.map +1 -1
  39. package/dist/26.js +349 -30
  40. package/dist/26.js.map +1 -1
  41. package/dist/27.js +24 -352
  42. package/dist/27.js.map +1 -1
  43. package/dist/28.js +25 -22
  44. package/dist/28.js.map +1 -1
  45. package/dist/29.js +483 -26
  46. package/dist/29.js.map +1 -1
  47. package/dist/3.js +2710 -1818
  48. package/dist/3.js.map +1 -1
  49. package/dist/30.js +1 -502
  50. package/dist/30.js.map +1 -1
  51. package/dist/31.js +1 -1
  52. package/dist/31.js.map +1 -1
  53. package/dist/32.js +12 -1
  54. package/dist/32.js.map +1 -1
  55. package/dist/4.js +361 -3669
  56. package/dist/4.js.map +1 -1
  57. package/dist/5.js +48 -242
  58. package/dist/5.js.map +1 -1
  59. package/dist/6.js +118 -74
  60. package/dist/6.js.map +1 -1
  61. package/dist/7.js +55 -99
  62. package/dist/7.js.map +1 -1
  63. package/dist/8.js +98 -46
  64. package/dist/8.js.map +1 -1
  65. package/dist/9.js +474 -412
  66. package/dist/9.js.map +1 -1
  67. package/dist/@aws-amplify/ui-react.js +50 -44
  68. package/dist/@aws-amplify/ui-react.js.map +1 -1
  69. package/dist/aws-amplify-react.min.js +2 -2
  70. package/dist/aws-amplify-react.min.js.map +1 -1
  71. package/dist/polyfills-core-js.js +5 -11
  72. package/dist/polyfills-core-js.js.map +1 -1
  73. package/dist/polyfills-css-shim.js.map +1 -1
  74. package/dist/polyfills-dom.js.map +1 -1
  75. package/package.json +3 -3
  76. package/dist/33.js +0 -13
  77. package/dist/33.js.map +0 -1
package/dist/13.js CHANGED
@@ -1,111 +1,1193 @@
1
1
  (this["webpackJsonpaws_amplify_react"] = this["webpackJsonpaws_amplify_react"] || []).push([[13],{
2
2
 
3
- /***/ "../amplify-ui-components/dist/esm-es5/amplify-button_3.entry.js":
4
- /*!***********************************************************************!*\
5
- !*** ../amplify-ui-components/dist/esm-es5/amplify-button_3.entry.js ***!
6
- \***********************************************************************/
7
- /*! exports provided: amplify_button, amplify_loading_spinner, amplify_section */
3
+ /***/ "../amplify-ui-components/dist/esm-es5/amplify-chatbot.entry.js":
4
+ /*!**********************************************************************!*\
5
+ !*** ../amplify-ui-components/dist/esm-es5/amplify-chatbot.entry.js ***!
6
+ \**********************************************************************/
7
+ /*! exports provided: amplify_chatbot */
8
8
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
9
9
 
10
10
  "use strict";
11
11
  __webpack_require__.r(__webpack_exports__);
12
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "amplify_button", function() { return AmplifyButton; });
13
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "amplify_loading_spinner", function() { return AmplifyLoadingSpinner; });
14
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "amplify_section", function() { return AmplifySection; });
12
+ /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "amplify_chatbot", function() { return AmplifyChatbot; });
15
13
  /* harmony import */ var _index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./index-83f2275b.js */ "../amplify-ui-components/dist/esm-es5/index-83f2275b.js");
14
+ /* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @aws-amplify/core */ "@aws-amplify/core");
15
+ /* harmony import */ var _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__);
16
+ /* harmony import */ var _aws_amplify_auth__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @aws-amplify/auth */ "@aws-amplify/auth");
17
+ /* harmony import */ var _aws_amplify_auth__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_auth__WEBPACK_IMPORTED_MODULE_2__);
18
+ /* harmony import */ var _Translations_108d469f_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./Translations-108d469f.js */ "../amplify-ui-components/dist/esm-es5/Translations-108d469f.js");
19
+ /* harmony import */ var _constants_c8ecaa24_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./constants-c8ecaa24.js */ "../amplify-ui-components/dist/esm-es5/constants-c8ecaa24.js");
20
+ /* harmony import */ var _aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! @aws-amplify/interactions */ "@aws-amplify/interactions");
21
+ /* harmony import */ var _aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__);
22
+ var __awaiter = undefined && undefined.__awaiter || function (thisArg, _arguments, P, generator) {
23
+ function adopt(value) {
24
+ return value instanceof P ? value : new P(function (resolve) {
25
+ resolve(value);
26
+ });
27
+ }
16
28
 
17
- var amplifyButtonCss = "amplify-button{--background-color:var(--amplify-primary-color);--background-color-active:var(--amplify-primary-shade);--background-color-disable:var(--amplify-primary-tint);--color:var(--amplify-primary-contrast);--border-width:0;--border-color:initial;--border-style:initial;--border-radius:initial;--link-color:var(--amplify-primary-color);--link-hover:var(--amplify-primary-tint);--link-active:var(--amplify-primary-shade);--text-transform:uppercase;--icon-fill:var(--amplify-white);--icon-height:1.25rem;--padding:1rem;--width:100%;width:var(--width);text-align:center}@media (min-width: 672px){amplify-button{width:inherit}}.button{width:100%;min-width:153px;display:inline-block;margin-bottom:0;font-size:var(--amplify-text-sm);font-family:var(--amplify-font-family);font-weight:600;text-align:center;white-space:nowrap;vertical-align:middle;-ms-touch-action:manipulation;touch-action:manipulation;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-image:none;color:var(--color);padding:var(--padding);letter-spacing:0.75px;text-transform:var(--text-transform);background-color:var(--background-color);border-width:var(--border-width);border-color:var(--border-color);border-style:var(--border-style);border-radius:var(--border-radius)}.button:active{opacity:1;background-color:var(--background-color-active)}.button:hover{opacity:0.8}.button:disabled{opacity:0.65;cursor:auto;background-color:var(--background-color-disable)}.icon{background-color:inherit;border:none;font:inherit;cursor:pointer;padding:var(--padding)}.icon amplify-icon{--icon-fill-color:var(--icon-fill);--height:var(--icon-height)}.anchor{color:var(--link-color);background-color:inherit;padding:0;border:none;font:inherit;cursor:pointer}.anchor:link{color:var(--link-color);text-decoration:none}.anchor:hover{color:var(--link-hover);text-decoration:underline}.anchor:active{color:var(--link-active);text-decoration:underline}";
29
+ return new (P || (P = Promise))(function (resolve, reject) {
30
+ function fulfilled(value) {
31
+ try {
32
+ step(generator.next(value));
33
+ } catch (e) {
34
+ reject(e);
35
+ }
36
+ }
18
37
 
19
- var AmplifyButton =
20
- /** @class */
21
- function () {
22
- function AmplifyButton(hostRef) {
23
- Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["r"])(this, hostRef);
24
- /** Type of the button: 'button', 'submit' or 'reset' */
38
+ function rejected(value) {
39
+ try {
40
+ step(generator["throw"](value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ }
25
45
 
26
- this.type = 'button';
27
- /** Variant of a button: 'button' | 'anchor | 'icon' */
46
+ function step(result) {
47
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
48
+ }
28
49
 
29
- this.variant = 'button';
30
- /** Disabled state of the button */
50
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
51
+ });
52
+ };
53
+
54
+ var __generator = undefined && undefined.__generator || function (thisArg, body) {
55
+ var _ = {
56
+ label: 0,
57
+ sent: function sent() {
58
+ if (t[0] & 1) throw t[1];
59
+ return t[1];
60
+ },
61
+ trys: [],
62
+ ops: []
63
+ },
64
+ f,
65
+ y,
66
+ t,
67
+ g;
68
+ return g = {
69
+ next: verb(0),
70
+ "throw": verb(1),
71
+ "return": verb(2)
72
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
73
+ return this;
74
+ }), g;
31
75
 
32
- this.disabled = false;
76
+ function verb(n) {
77
+ return function (v) {
78
+ return step([n, v]);
79
+ };
33
80
  }
34
81
 
35
- AmplifyButton.prototype.render = function () {
36
- var _a;
82
+ function step(op) {
83
+ if (f) throw new TypeError("Generator is already executing.");
37
84
 
38
- return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["H"], null, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("button", {
39
- "class": (_a = {}, _a[this.variant] = true, _a),
40
- type: this.type,
41
- disabled: this.disabled,
42
- onClick: this.handleButtonClick
43
- }, this.variant === 'icon' && Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-icon", {
44
- name: this.icon
45
- }), Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("slot", null)));
46
- };
85
+ while (_) {
86
+ try {
87
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
88
+ if (y = 0, t) op = [op[0] & 2, t.value];
47
89
 
48
- Object.defineProperty(AmplifyButton.prototype, "el", {
49
- get: function get() {
50
- return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["g"])(this);
51
- },
52
- enumerable: false,
53
- configurable: true
90
+ switch (op[0]) {
91
+ case 0:
92
+ case 1:
93
+ t = op;
94
+ break;
95
+
96
+ case 4:
97
+ _.label++;
98
+ return {
99
+ value: op[1],
100
+ done: false
101
+ };
102
+
103
+ case 5:
104
+ _.label++;
105
+ y = op[1];
106
+ op = [0];
107
+ continue;
108
+
109
+ case 7:
110
+ op = _.ops.pop();
111
+
112
+ _.trys.pop();
113
+
114
+ continue;
115
+
116
+ default:
117
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
118
+ _ = 0;
119
+ continue;
120
+ }
121
+
122
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
123
+ _.label = op[1];
124
+ break;
125
+ }
126
+
127
+ if (op[0] === 6 && _.label < t[1]) {
128
+ _.label = t[1];
129
+ t = op;
130
+ break;
131
+ }
132
+
133
+ if (t && _.label < t[2]) {
134
+ _.label = t[2];
135
+
136
+ _.ops.push(op);
137
+
138
+ break;
139
+ }
140
+
141
+ if (t[2]) _.ops.pop();
142
+
143
+ _.trys.pop();
144
+
145
+ continue;
146
+ }
147
+
148
+ op = body.call(thisArg, _);
149
+ } catch (e) {
150
+ op = [6, e];
151
+ y = 0;
152
+ } finally {
153
+ f = t = 0;
154
+ }
155
+ }
156
+
157
+ if (op[0] & 5) throw op[1];
158
+ return {
159
+ value: op[0] ? op[1] : void 0,
160
+ done: true
161
+ };
162
+ }
163
+ };
164
+
165
+ var __spreadArrays = undefined && undefined.__spreadArrays || function () {
166
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) {
167
+ s += arguments[i].length;
168
+ }
169
+
170
+ for (var r = Array(s), k = 0, i = 0; i < il; i++) {
171
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) {
172
+ r[k] = a[j];
173
+ }
174
+ }
175
+
176
+ return r;
177
+ };
178
+
179
+
180
+
181
+
182
+
183
+
184
+ // AudioRecorder settings
185
+
186
+ var RECORDER_EXPORT_MIME_TYPE = 'application/octet-stream';
187
+ var DEFAULT_EXPORT_SAMPLE_RATE = 16000;
188
+ var FFT_SIZE = 2048; // window size in samples for Fast Fourier Transform (FFT)
189
+
190
+ var FFT_MAX_DECIBELS = -10; // maximum power value in the scaling range for the FFT analysis data
191
+
192
+ var FFT_MIN_DECIBELS = -90; // minimum power value in the scaling range for the FFT analysis data
193
+
194
+ var FFT_SMOOTHING_TIME_CONSTANT = 0.85; // averaging constant with the last analysis frame
195
+
196
+ /**
197
+ * Merges multiple buffers into one.
198
+ */
199
+
200
+ var mergeBuffers = function mergeBuffers(bufferArray, recLength) {
201
+ var result = new Float32Array(recLength);
202
+ var offset = 0;
203
+
204
+ for (var i = 0; i < bufferArray.length; i++) {
205
+ result.set(bufferArray[i], offset);
206
+ offset += bufferArray[i].length;
207
+ }
208
+
209
+ return result;
210
+ };
211
+ /**
212
+ * Downsamples audio to desired export sample rate.
213
+ */
214
+
215
+
216
+ var downsampleBuffer = function downsampleBuffer(buffer, recordSampleRate, exportSampleRate) {
217
+ if (exportSampleRate === recordSampleRate) {
218
+ return buffer;
219
+ }
220
+
221
+ var sampleRateRatio = recordSampleRate / exportSampleRate;
222
+ var newLength = Math.round(buffer.length / sampleRateRatio);
223
+ var result = new Float32Array(newLength);
224
+ var offsetResult = 0;
225
+ var offsetBuffer = 0;
226
+
227
+ while (offsetResult < result.length) {
228
+ var nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);
229
+ var accum = 0,
230
+ count = 0;
231
+
232
+ for (var i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {
233
+ accum += buffer[i];
234
+ count++;
235
+ }
236
+
237
+ result[offsetResult] = accum / count;
238
+ offsetResult++;
239
+ offsetBuffer = nextOffsetBuffer;
240
+ }
241
+
242
+ return result;
243
+ };
244
+ /**
245
+ * converts raw audio values to 16 bit pcm.
246
+ */
247
+
248
+
249
+ var floatTo16BitPCM = function floatTo16BitPCM(output, offset, input) {
250
+ var byteOffset = offset;
251
+
252
+ for (var i = 0; i < input.length; i++, byteOffset += 2) {
253
+ var s = Math.max(-1, Math.min(1, input[i]));
254
+ output.setInt16(byteOffset, s < 0 ? s * 0x8000 : s * 0x7fff, true);
255
+ }
256
+ };
257
+ /**
258
+ * Write given strings in big-endian order.
259
+ */
260
+
261
+
262
+ var writeString = function writeString(view, offset, string) {
263
+ for (var i = 0; i < string.length; i++) {
264
+ view.setUint8(offset + i, string.charCodeAt(i));
265
+ }
266
+ };
267
+ /**
268
+ * Encodes raw pcm audio into a wav file.
269
+ */
270
+
271
+
272
+ var encodeWAV = function encodeWAV(samples, exportSampleRate) {
273
+ /**
274
+ * WAV file consists of three parts: RIFF header, WAVE subchunk, and data subchunk. We precompute the size of them.
275
+ */
276
+ var audioSize = samples.length * 2; // We use 16-bit samples, so we have (2 * sampleLength) bytes.
277
+
278
+ var fmtSize = 24; // Byte size of the fmt subchunk: 24 bytes that the audio information that we'll set below.
279
+
280
+ var dataSize = 8 + audioSize; // Byte size of the data subchunk: raw sound data plus 8 bytes for the subchunk descriptions.
281
+
282
+ var totalByteSize = 12 + fmtSize + dataSize; // Byte size of the whole file, including the chunk header / descriptor.
283
+ // create DataView object to write byte values into
284
+
285
+ var buffer = new ArrayBuffer(totalByteSize); // buffer to write the chunk values in.
286
+
287
+ var view = new DataView(buffer);
288
+ /**
289
+ * Start writing the .wav file. We write top to bottom, so byte offset (first numeric argument) increases strictly.
290
+ */
291
+ // RIFF header
292
+
293
+ writeString(view, 0, 'RIFF'); // At offset 0, write the letters "RIFF"
294
+
295
+ view.setUint32(4, fmtSize + dataSize, true); // At offset 4, write the size of fmt and data chunk size combined.
296
+
297
+ writeString(view, 8, 'WAVE'); // At offset 8, write the format type "WAVE"
298
+ // fmt subchunk
299
+
300
+ writeString(view, 12, 'fmt '); //chunkdId 'fmt '
301
+
302
+ view.setUint32(16, fmtSize - 8, true); // fmt subchunk size below this value. We set 8 bytes already, so subtract 8 bytes from fmtSize.
303
+
304
+ view.setUint16(20, 1, true); // Audio format code, which is 1 for PCM.
305
+
306
+ view.setUint16(22, 1, true); // Number of audio channels. We use mono, ie 1.
307
+
308
+ view.setUint32(24, exportSampleRate, true); // Sample rate of the audio file.
309
+
310
+ view.setUint32(28, exportSampleRate * 2, true); // Data rate, or # of data bytes per second. Since each sample is 2 bytes, this is 2 * sampleRate.
311
+
312
+ view.setUint16(32, 2, true); // block align, # of bytes per sample including all channels, ie. 2 bytes.
313
+
314
+ view.setUint16(34, 16, true); // bits per sample, ie. 16 bits
315
+ // data subchunk
316
+
317
+ writeString(view, 36, 'data'); // write the chunkId 'data'
318
+
319
+ view.setUint32(40, audioSize, true); // Audio byte size
320
+
321
+ floatTo16BitPCM(view, 44, samples); // raw pcm values then go here.
322
+
323
+ return view;
324
+ };
325
+ /**
326
+ * Given arrays of raw pcm audio, downsamples the audio to desired sample rate and encodes it to a wav audio file.
327
+ *
328
+ * @param recBuffer {Float32Array[]} - 2d float array containing the recorded raw audio
329
+ * @param recLength {number} - total length of recorded audio
330
+ * @param recordSampleRate {number} - sample rate of the recorded audio
331
+ * @param exportSampleRate {number} - desired sample rate of the exported file
332
+ */
333
+
334
+
335
+ var exportBuffer = function exportBuffer(recBuffer, recLength, recordSampleRate, exportSampleRate) {
336
+ var mergedBuffers = mergeBuffers(recBuffer, recLength);
337
+ var downsampledBuffer = downsampleBuffer(mergedBuffers, recordSampleRate, exportSampleRate);
338
+ var encodedWav = encodeWAV(downsampledBuffer, exportSampleRate);
339
+ var audioBlob = new Blob([encodedWav], {
340
+ type: RECORDER_EXPORT_MIME_TYPE
54
341
  });
55
- return AmplifyButton;
56
- }();
342
+ return audioBlob;
343
+ };
57
344
 
58
- AmplifyButton.style = amplifyButtonCss;
59
- var amplifyLoadingSpinnerCss = ":host{--spinner-circle-fill:var(--amplify-smoke-white);--spinner-bar-fill:var(--amplify-primary-color);--width:0.875rem;--height:0.875rem}.loading-spinner svg{-webkit-animation:loading-spinner 1s linear infinite;animation:loading-spinner 1s linear infinite;width:var(--width);height:var(--height)}.loading-spinner svg #spinner-circle{fill:var(--spinner-circle-fill)}.loading-spinner svg #spinner-bar{fill:var(--spinner-bar-fill)}@-webkit-keyframes loading-spinner{from{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes loading-spinner{from{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}";
345
+ var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["Logger"]('AudioRecorder');
60
346
 
61
- var AmplifyLoadingSpinner =
347
+ var AudioRecorder =
62
348
  /** @class */
63
349
  function () {
64
- function AmplifyLoadingSpinner(hostRef) {
65
- Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["r"])(this, hostRef);
350
+ function AudioRecorder(options) {
351
+ // input mic stream is stored in a buffer
352
+ this.streamBuffer = [];
353
+ this.streamBufferLength = 0;
354
+ this.recording = false;
355
+ this.options = options;
66
356
  }
357
+ /**
358
+ * This must be called first to enable audio context and request microphone access.
359
+ * Once access granted, it connects all the necessary audio nodes to the context so that it can begin recording or playing.
360
+ */
361
+
362
+
363
+ AudioRecorder.prototype.init = function () {
364
+ return __awaiter(this, void 0, void 0, function () {
365
+ var _this = this;
366
+
367
+ return __generator(this, function (_a) {
368
+ switch (_a.label) {
369
+ case 0:
370
+ if (!Object(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["browserOrNode"])().isBrowser) return [3
371
+ /*break*/
372
+ , 2];
373
+ window.AudioContext = window.AudioContext || window.webkitAudioContext;
374
+ this.audioContext = new AudioContext();
375
+ return [4
376
+ /*yield*/
377
+ , navigator.mediaDevices.getUserMedia({
378
+ audio: true
379
+ }).then(function (stream) {
380
+ _this.audioSupported = true;
381
+
382
+ _this.setupAudioNodes(stream);
383
+ })["catch"](function () {
384
+ _this.audioSupported = false;
385
+ return Promise.reject('Audio is not supported');
386
+ })];
387
+
388
+ case 1:
389
+ _a.sent();
390
+
391
+ return [3
392
+ /*break*/
393
+ , 3];
394
+
395
+ case 2:
396
+ this.audioSupported = false;
397
+ return [2
398
+ /*return*/
399
+ , Promise.reject('Audio is not supported')];
400
+
401
+ case 3:
402
+ return [2
403
+ /*return*/
404
+ ];
405
+ }
406
+ });
407
+ });
408
+ };
409
+ /**
410
+ * Setup audio nodes after successful `init`.
411
+ */
412
+
413
+
414
+ AudioRecorder.prototype.setupAudioNodes = function (stream) {
415
+ return __awaiter(this, void 0, void 0, function () {
416
+ var err_1, sourceNode, processorNode, analyserNode;
417
+
418
+ var _this = this;
419
+
420
+ return __generator(this, function (_a) {
421
+ switch (_a.label) {
422
+ case 0:
423
+ _a.trys.push([0, 2,, 3]);
424
+
425
+ return [4
426
+ /*yield*/
427
+ , this.audioContext.resume()];
428
+
429
+ case 1:
430
+ _a.sent();
431
+
432
+ return [3
433
+ /*break*/
434
+ , 3];
435
+
436
+ case 2:
437
+ err_1 = _a.sent();
438
+ logger.error(err_1);
439
+ return [3
440
+ /*break*/
441
+ , 3];
442
+
443
+ case 3:
444
+ sourceNode = this.audioContext.createMediaStreamSource(stream);
445
+ processorNode = this.audioContext.createScriptProcessor(4096, 1, 1);
446
+
447
+ processorNode.onaudioprocess = function (audioProcessingEvent) {
448
+ if (!_this.recording) return;
449
+ var stream = audioProcessingEvent.inputBuffer.getChannelData(0);
450
+
451
+ _this.streamBuffer.push(new Float32Array(stream)); // set to a copy of the stream
452
+
453
+
454
+ _this.streamBufferLength += stream.length;
455
+
456
+ _this.analyse();
457
+ };
458
+
459
+ analyserNode = this.audioContext.createAnalyser();
460
+ analyserNode.minDecibels = FFT_MIN_DECIBELS;
461
+ analyserNode.maxDecibels = FFT_MAX_DECIBELS;
462
+ analyserNode.smoothingTimeConstant = FFT_SMOOTHING_TIME_CONSTANT;
463
+ sourceNode.connect(analyserNode);
464
+ analyserNode.connect(processorNode);
465
+ processorNode.connect(sourceNode.context.destination);
466
+ this.analyserNode = analyserNode;
467
+ return [2
468
+ /*return*/
469
+ ];
470
+ }
471
+ });
472
+ });
473
+ };
474
+ /**
475
+ * Start recording audio and listen for silence.
476
+ *
477
+ * @param onSilence {SilenceHandler} - called whenever silence is detected
478
+ * @param visualizer {Visualizer} - called with audio data on each audio process to be used for visualization.
479
+ */
480
+
481
+
482
+ AudioRecorder.prototype.startRecording = function (onSilence, visualizer) {
483
+ return __awaiter(this, void 0, void 0, function () {
484
+ var context, err_2;
485
+ return __generator(this, function (_a) {
486
+ switch (_a.label) {
487
+ case 0:
488
+ if (this.recording || !this.audioSupported) return [2
489
+ /*return*/
490
+ ];
491
+
492
+ this.onSilence = onSilence || function () {};
493
+
494
+ this.visualizer = visualizer || function () {};
495
+
496
+ context = this.audioContext;
497
+ _a.label = 1;
498
+
499
+ case 1:
500
+ _a.trys.push([1, 3,, 4]);
501
+
502
+ return [4
503
+ /*yield*/
504
+ , context.resume()];
505
+
506
+ case 2:
507
+ _a.sent();
508
+
509
+ return [3
510
+ /*break*/
511
+ , 4];
512
+
513
+ case 3:
514
+ err_2 = _a.sent();
515
+ logger.error(err_2);
516
+ return [3
517
+ /*break*/
518
+ , 4];
519
+
520
+ case 4:
521
+ this.start = Date.now();
522
+ this.recording = true;
523
+ return [2
524
+ /*return*/
525
+ ];
526
+ }
527
+ });
528
+ });
529
+ };
530
+ /**
531
+ * Pause recording
532
+ */
533
+
534
+
535
+ AudioRecorder.prototype.stopRecording = function () {
536
+ if (!this.audioSupported) return;
537
+ this.recording = false;
538
+ };
539
+ /**
540
+ * Pause recording and clear audio buffer
541
+ */
542
+
543
+
544
+ AudioRecorder.prototype.clear = function () {
545
+ this.stopRecording();
546
+ this.streamBufferLength = 0;
547
+ this.streamBuffer = [];
548
+ };
549
+ /**
550
+ * Plays given audioStream with audioContext
551
+ *
552
+ * @param buffer {Uint8Array} - audioStream to be played
553
+ */
554
+
555
+
556
+ AudioRecorder.prototype.play = function (buffer) {
557
+ var _this = this;
558
+
559
+ if (!buffer || !this.audioSupported) return;
560
+ var myBlob = new Blob([buffer]);
561
+ return new Promise(function (res, rej) {
562
+ var fileReader = new FileReader();
563
+
564
+ fileReader.onload = function () {
565
+ if (_this.playbackSource) _this.playbackSource.disconnect(); // disconnect previous playback source
67
566
 
68
- AmplifyLoadingSpinner.prototype.render = function () {
69
- return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-icon", {
70
- "class": "loading-spinner",
71
- name: "loading"
567
+ _this.playbackSource = _this.audioContext.createBufferSource();
568
+
569
+ var successCallback = function successCallback(buf) {
570
+ _this.playbackSource.buffer = buf;
571
+
572
+ _this.playbackSource.connect(_this.audioContext.destination);
573
+
574
+ _this.playbackSource.onended = function () {
575
+ return res();
576
+ };
577
+
578
+ _this.playbackSource.start(0);
579
+ };
580
+
581
+ var errorCallback = function errorCallback(err) {
582
+ return rej(err);
583
+ };
584
+
585
+ _this.audioContext.decodeAudioData(fileReader.result, successCallback, errorCallback);
586
+ };
587
+
588
+ fileReader.onerror = function () {
589
+ return rej();
590
+ };
591
+
592
+ fileReader.readAsArrayBuffer(myBlob);
593
+ });
594
+ };
595
+ /**
596
+ * Stops playing audio if there's a playback source connected.
597
+ */
598
+
599
+
600
+ AudioRecorder.prototype.stop = function () {
601
+ if (this.playbackSource) {
602
+ this.playbackSource.stop();
603
+ }
604
+ };
605
+ /**
606
+ * Called after each audioProcess. Check for silence and give fft time domain data to visualizer.
607
+ */
608
+
609
+
610
+ AudioRecorder.prototype.analyse = function () {
611
+ if (!this.audioSupported) return;
612
+ var analyser = this.analyserNode;
613
+ analyser.fftSize = FFT_SIZE;
614
+ var bufferLength = analyser.fftSize;
615
+ var dataArray = new Uint8Array(bufferLength);
616
+ var amplitude = this.options.amplitude;
617
+ var time = this.options.time;
618
+ analyser.getByteTimeDomainData(dataArray);
619
+ this.visualizer(dataArray, bufferLength);
620
+
621
+ for (var i = 0; i < bufferLength; i++) {
622
+ // Normalize between -1 and 1.
623
+ var curr_value_time = dataArray[i] / 128 - 1.0;
624
+
625
+ if (curr_value_time > amplitude || curr_value_time < -1 * amplitude) {
626
+ this.start = Date.now();
627
+ }
628
+ }
629
+
630
+ var newtime = Date.now();
631
+ var elapsedTime = newtime - this.start;
632
+
633
+ if (elapsedTime > time) {
634
+ this.onSilence();
635
+ }
636
+ };
637
+ /**
638
+ * Encodes recorded buffer to a wav file and exports it to a blob.
639
+ *
640
+ * @param exportSampleRate {number} - desired sample rate of the exported buffer
641
+ */
642
+
643
+
644
+ AudioRecorder.prototype.exportWAV = function (exportSampleRate) {
645
+ if (exportSampleRate === void 0) {
646
+ exportSampleRate = DEFAULT_EXPORT_SAMPLE_RATE;
647
+ }
648
+
649
+ return __awaiter(this, void 0, void 0, function () {
650
+ var recordSampleRate, blob;
651
+ return __generator(this, function (_a) {
652
+ if (!this.audioSupported) return [2
653
+ /*return*/
654
+ ];
655
+ recordSampleRate = this.audioContext.sampleRate;
656
+ blob = exportBuffer(this.streamBuffer, this.streamBufferLength, recordSampleRate, exportSampleRate);
657
+ this.clear();
658
+ return [2
659
+ /*return*/
660
+ , blob];
661
+ });
72
662
  });
73
663
  };
74
664
 
75
- return AmplifyLoadingSpinner;
665
+ return AudioRecorder;
76
666
  }();
77
667
 
78
- AmplifyLoadingSpinner.style = amplifyLoadingSpinnerCss;
79
- var amplifySectionCss = "amplify-section{--font-family:var(--amplify-font-family);--background-color:var(--amplify-background-color)}.section{position:relative;margin-bottom:var(--margin-bottom, 20px);background-color:var(--background-color);padding:var(--padding, 35px 40px);text-align:left;display:inline-block;border-radius:var(--border-radius, 6px);-webkit-box-shadow:var(--box-shadow, 1px 1px 4px 0 rgba(0, 0, 0, 0.15));box-shadow:var(--box-shadow, 1px 1px 4px 0 rgba(0, 0, 0, 0.15));-webkit-box-sizing:border-box;box-sizing:border-box;font-family:var(--font-family);width:100%;min-width:var(--min-width, 20rem)}@media (min-width: 672px){.section{width:var(--width, 28.75rem)}}";
668
+ var visualize = function visualize(dataArray, bufferLength, canvas) {
669
+ if (!canvas) return;
670
+ if (!Object(_aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["browserOrNode"])().isBrowser) throw new Error('Visualization is not supported on non-browsers.');
671
+
672
+ var _a = canvas.getBoundingClientRect(),
673
+ width = _a.width,
674
+ height = _a.height; // need to update the default canvas width and height
675
+
676
+
677
+ canvas.width = width;
678
+ canvas.height = height;
679
+ var canvasCtx = canvas.getContext('2d');
680
+ canvasCtx.fillStyle = 'white';
681
+ canvasCtx.clearRect(0, 0, width, height);
682
+
683
+ var draw = function draw() {
684
+ canvasCtx.fillRect(0, 0, width, height);
685
+ canvasCtx.lineWidth = 1;
686
+ var color = getComputedStyle(document.documentElement).getPropertyValue('--amplify-primary-color');
687
+ canvasCtx.strokeStyle = !color || color === '' ? '#ff9900' : color; // TODO: try separate css variable
688
+
689
+ canvasCtx.beginPath();
690
+ var sliceWidth = width * 1.0 / bufferLength;
691
+ var x = 0;
692
+
693
+ for (var i = 0; i < bufferLength || i % 3 === 0; i++) {
694
+ var value = dataArray[i] / 128.0;
695
+ var y = value * height / 2;
696
+
697
+ if (i === 0) {
698
+ canvasCtx.moveTo(x, y);
699
+ } else {
700
+ canvasCtx.lineTo(x, y);
701
+ }
702
+
703
+ x += sliceWidth;
704
+ }
705
+
706
+ canvasCtx.lineTo(canvas.width, canvas.height / 2);
707
+ canvasCtx.stroke();
708
+ }; // Register our draw function with requestAnimationFrame.
709
+
710
+
711
+ requestAnimationFrame(draw);
712
+ };
713
+
714
+ var amplifyChatbotCss = ".bot .dot{background-color:var(--bot-dot-color)}.user .dot{background-color:var(--user-dot-color)}.dot-flashing{width:2.625rem}.dot-flashing .dot{display:inline-block;width:0.625rem;height:0.625rem;border-radius:10rem;opacity:0.65}.dot-flashing .left{-webkit-animation:dot-flashing 1s infinite alternate;animation:dot-flashing 1s infinite alternate;-webkit-animation-delay:0s;animation-delay:0s}.dot-flashing .middle{margin-left:0.375rem;margin-right:0.375rem;-webkit-animation:dot-flashing 1s infinite linear alternate;animation:dot-flashing 1s infinite linear alternate;-webkit-animation-delay:0.5s;animation-delay:0.5s}.dot-flashing .right{-webkit-animation:dot-flashing 1s infinite alternate;animation:dot-flashing 1s infinite alternate;-webkit-animation-delay:1s;animation-delay:1s}@-webkit-keyframes dot-flashing{0%{opacity:0.65}50%,100%{opacity:0.1}}@keyframes dot-flashing{0%{opacity:0.65}50%,100%{opacity:0.1}}:host{--width:28.75rem;--height:37.5rem;--header-color:var(--amplify-secondary-color);--header-size:var(--amplify-text-lg);--bot-background-color:rgb(230, 230, 230);--bot-text-color:black;--bot-dot-color:var(--bot-text-color);--user-background-color:var(--amplify-blue);--user-text-color:var(--amplify-white);--user-dot-color:var(--user-text-color)}.amplify-chatbot{display:-ms-inline-flexbox;display:inline-flex;-ms-flex-direction:column;flex-direction:column;background-color:var(--background-color);border-radius:0.375rem;-webkit-box-shadow:0.0625rem 0rem 0.25rem 0 rgba(0, 0, 0, 0.15);box-shadow:0.0625rem 0rem 0.25rem 0 rgba(0, 0, 0, 0.15);-webkit-box-sizing:border-box;box-sizing:border-box;font-family:var(--amplify-font-family);margin-bottom:1rem;width:100%;height:var(--height);max-width:var(--width)}@media (min-width: 672px){.amplify-chatbot{width:var(--width)}}.header{padding:1.25rem 0.375rem 1.25rem 0.375rem;color:var(--header-color);font-size:var(--header-size);font-weight:bold;text-align:center;word-wrap:break-word}.body{border-top:0.0625rem solid rgba(0, 0, 0, 0.05);padding:1.5rem 1rem 0 1rem;display:-ms-flexbox;display:flex;-ms-flex-positive:1;flex-grow:1;-ms-flex-direction:column;flex-direction:column;overflow:auto}.bubble{max-width:100%;padding:0.8em 1.4em;text-align:left;word-wrap:break-word;margin-bottom:0.625rem}.bot{margin-right:auto;background-color:var(--bot-background-color);color:var(--bot-text-color);border-radius:1.5rem 1.5rem 1.5rem 0}.user{margin-left:auto;background-color:var(--user-background-color);color:var(--user-text-color);border-radius:1.5rem 1.5rem 0 1.5rem}.footer{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;border-top:0.062rem solid rgba(0, 0, 0, 0.05);padding-right:0.625rem;min-height:3.125rem}.footer amplify-input{--border:none;--margin:0;-ms-flex-positive:1;flex-grow:1}canvas{margin-left:0.625rem;margin-right:0.625rem;-ms-flex-positive:1;flex-grow:1;height:3.125rem}.icon-button{--icon-height:1.25rem;--icon-fill:var(--amplify-primary-color);--padding:0.625rem;--width:auto}"; // enum for possible bot states
715
+
716
+ var ChatState;
80
717
 
81
- var AmplifySection =
718
+ (function (ChatState) {
719
+ ChatState[ChatState["Initial"] = 0] = "Initial";
720
+ ChatState[ChatState["Listening"] = 1] = "Listening";
721
+ ChatState[ChatState["SendingText"] = 2] = "SendingText";
722
+ ChatState[ChatState["SendingVoice"] = 3] = "SendingVoice";
723
+ ChatState[ChatState["Error"] = 4] = "Error";
724
+ })(ChatState || (ChatState = {})); // Message types
725
+
726
+
727
+ var MessageFrom;
728
+
729
+ (function (MessageFrom) {
730
+ MessageFrom["Bot"] = "bot";
731
+ MessageFrom["User"] = "user";
732
+ })(MessageFrom || (MessageFrom = {})); // Error types
733
+
734
+
735
+ var ChatErrorType;
736
+
737
+ (function (ChatErrorType) {
738
+ ChatErrorType[ChatErrorType["Recoverable"] = 0] = "Recoverable";
739
+ ChatErrorType[ChatErrorType["Unrecoverable"] = 1] = "Unrecoverable";
740
+ })(ChatErrorType || (ChatErrorType = {}));
741
+
742
+ var AmplifyChatbot =
82
743
  /** @class */
83
744
  function () {
84
- function AmplifySection(hostRef) {
745
+ function class_1(hostRef) {
746
+ var _this = this;
747
+
85
748
  Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["r"])(this, hostRef);
86
- /** Equivalent to html section role */
749
+ /** Clear messages when conversation finishes */
87
750
 
88
- this.role = 'application';
89
- }
751
+ this.clearOnComplete = false;
752
+ /** Continue listening to users after they send the message */
753
+
754
+ this.conversationModeOn = false;
755
+ /** Text placed in the top header */
756
+
757
+ this.botTitle = _Translations_108d469f_js__WEBPACK_IMPORTED_MODULE_3__["T"].CHATBOT_TITLE;
758
+ /** Whether voice chat is enabled */
759
+
760
+ this.voiceEnabled = false;
761
+ /** Whether text chat is enabled */
762
+
763
+ this.textEnabled = true;
764
+ /** Amount of silence (in ms) to wait for */
765
+
766
+ this.silenceTime = 1500;
767
+ /** Noise threshold between -1 and 1. Anything below is considered a silence. */
768
+
769
+ this.silenceThreshold = 0.2;
770
+ /** Messages in current session */
771
+
772
+ this.messages = [];
773
+ /** Text input box value */
774
+
775
+ this.text = '';
776
+ /** Current app state */
777
+
778
+ this.chatState = ChatState.Initial;
779
+ /**
780
+ * Rendering methods
781
+ */
782
+
783
+ this.messageJSX = function (messages) {
784
+ var messageList = messages.map(function (message) {
785
+ return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
786
+ "class": "bubble " + message.from
787
+ }, message.content);
788
+ });
789
+
790
+ if (_this.chatState === ChatState.SendingText || _this.chatState === ChatState.SendingVoice) {
791
+ // if waiting for voice message, show animation on user side because app is waiting for transcript. Else put it on bot side.
792
+ var client = _this.chatState === ChatState.SendingText ? MessageFrom.Bot : MessageFrom.User;
793
+ messageList.push(Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
794
+ "class": "bubble " + client
795
+ }, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
796
+ "class": "dot-flashing " + client
797
+ }, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("span", {
798
+ "class": "dot left"
799
+ }), Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("span", {
800
+ "class": "dot middle"
801
+ }), Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("span", {
802
+ "class": "dot right"
803
+ }))));
804
+ }
805
+
806
+ return messageList;
807
+ };
808
+
809
+ this.chatCompleted = Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["c"])(this, "chatCompleted", 7);
810
+ } // Occurs when user presses enter in input box
811
+
812
+
813
+ class_1.prototype.submitHandler = function (_event) {
814
+ this.sendTextMessage();
815
+ };
816
+ /**
817
+ * Lifecycle functions
818
+ */
819
+
820
+
821
+ class_1.prototype.componentWillLoad = function () {
822
+ console.warn('Version `1.x` of Amplify UI has been deprecated and will be removed in a future major version of `aws-amplify`. Please visit https://ui.docs.amplify.aws/ for the current version of Amplify UI.');
823
+
824
+ if (!_aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__["Interactions"] || typeof _aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__["Interactions"].onComplete !== 'function') {
825
+ throw new Error(_constants_c8ecaa24_js__WEBPACK_IMPORTED_MODULE_4__["c"]);
826
+ }
827
+
828
+ this.validateProps();
829
+ };
830
+
831
+ class_1.prototype.componentDidRender = function () {
832
+ // scroll to the bottom if necessary
833
+ var body = this.element.shadowRoot.querySelector('.body');
834
+ body.scrollTop = body.scrollHeight;
835
+ };
836
+
837
+ class_1.prototype.validateProps = function () {
838
+ var _this = this;
839
+
840
+ if (!this.voiceEnabled && !this.textEnabled) {
841
+ this.setError(_Translations_108d469f_js__WEBPACK_IMPORTED_MODULE_3__["T"].CHAT_DISABLED_ERROR, ChatErrorType.Unrecoverable);
842
+ return;
843
+ } else if (!this.botName) {
844
+ this.setError(_Translations_108d469f_js__WEBPACK_IMPORTED_MODULE_3__["T"].NO_BOT_NAME_ERROR, ChatErrorType.Unrecoverable);
845
+ return;
846
+ }
847
+
848
+ if (this.welcomeMessage) this.appendToChat(this.welcomeMessage, MessageFrom.Bot); // Initialize AudioRecorder if voice is enabled
849
+
850
+ if (this.voiceEnabled) {
851
+ this.audioRecorder = new AudioRecorder({
852
+ time: this.silenceTime,
853
+ amplitude: this.silenceThreshold
854
+ });
855
+ this.audioRecorder.init()["catch"](function (err) {
856
+ _this.setError(err, ChatErrorType.Recoverable);
857
+ });
858
+ } // Callback function to be called after chat is completed
859
+
860
+
861
+ var onComplete = function onComplete(err, data) {
862
+ _this.chatCompleted.emit({
863
+ data: data,
864
+ err: err
865
+ });
866
+
867
+ if (_this.clearOnComplete) {
868
+ _this.reset();
869
+ } else {
870
+ _this.chatState = ChatState.Initial;
871
+ }
872
+ };
873
+
874
+ try {
875
+ _aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__["Interactions"].onComplete(this.botName, onComplete);
876
+ } catch (err) {
877
+ this.setError(err, ChatErrorType.Unrecoverable);
878
+ }
879
+ };
880
+ /**
881
+ * Handlers
882
+ */
883
+
884
+
885
+ class_1.prototype.handleSubmit = function (event) {
886
+ event.preventDefault();
887
+ this.sendTextMessage();
888
+ };
889
+
890
+ class_1.prototype.handleMicButton = function () {
891
+ var _this = this;
892
+
893
+ if (this.chatState !== ChatState.Initial) return;
894
+ this.audioRecorder.stop();
895
+ this.chatState = ChatState.Listening;
896
+ this.audioRecorder.startRecording(function () {
897
+ return _this.handleSilence();
898
+ }, function (data, length) {
899
+ return _this.visualizer(data, length);
900
+ });
901
+ };
902
+
903
+ class_1.prototype.handleSilence = function () {
904
+ var _this = this;
905
+
906
+ this.chatState = ChatState.SendingVoice;
907
+ this.audioRecorder.stopRecording();
908
+ this.audioRecorder.exportWAV().then(function (blob) {
909
+ _this.sendVoiceMessage(blob);
910
+ });
911
+ };
912
+
913
+ class_1.prototype.handleTextChange = function (event) {
914
+ var target = event.target;
915
+ this.text = target.value;
916
+ };
917
+
918
+ class_1.prototype.handleCancelButton = function () {
919
+ this.audioRecorder.clear();
920
+ this.chatState = ChatState.Initial;
921
+ };
922
+
923
+ class_1.prototype.handleToastClose = function (errorType) {
924
+ this.error = undefined; // clear error
925
+ // if error is recoverable, reset the app state to initial
926
+
927
+ if (errorType === ChatErrorType.Recoverable) {
928
+ this.chatState = ChatState.Initial;
929
+ }
930
+ };
931
+ /**
932
+ * Visualization
933
+ */
934
+
935
+
936
+ class_1.prototype.visualizer = function (dataArray, bufferLength) {
937
+ var canvas = this.element.shadowRoot.querySelector('canvas');
938
+ visualize(dataArray, bufferLength, canvas);
939
+ };
940
+ /**
941
+ * Interactions helpers
942
+ */
943
+
944
+
945
+ class_1.prototype.sendTextMessage = function () {
946
+ return __awaiter(this, void 0, void 0, function () {
947
+ var text, response, err_3;
948
+ return __generator(this, function (_a) {
949
+ switch (_a.label) {
950
+ case 0:
951
+ if (this.text.length === 0 || this.chatState !== ChatState.Initial) return [2
952
+ /*return*/
953
+ ];
954
+ text = this.text;
955
+ this.text = '';
956
+ this.appendToChat(text, MessageFrom.User);
957
+ this.chatState = ChatState.SendingText;
958
+ _a.label = 1;
959
+
960
+ case 1:
961
+ _a.trys.push([1, 3,, 4]);
962
+
963
+ return [4
964
+ /*yield*/
965
+ , _aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__["Interactions"].send(this.botName, text)];
966
+
967
+ case 2:
968
+ response = _a.sent();
969
+ return [3
970
+ /*break*/
971
+ , 4];
972
+
973
+ case 3:
974
+ err_3 = _a.sent();
975
+ this.setError(err_3, ChatErrorType.Recoverable);
976
+ return [2
977
+ /*return*/
978
+ ];
979
+
980
+ case 4:
981
+ if (response.message) {
982
+ this.appendToChat(response.message, MessageFrom.Bot);
983
+ }
984
+
985
+ this.chatState = ChatState.Initial;
986
+ return [2
987
+ /*return*/
988
+ ];
989
+ }
990
+ });
991
+ });
992
+ };
993
+
994
+ class_1.prototype.sendVoiceMessage = function (audioInput) {
995
+ return __awaiter(this, void 0, void 0, function () {
996
+ var interactionsMessage, response, err_4, dialogState;
997
+
998
+ var _this = this;
999
+
1000
+ return __generator(this, function (_a) {
1001
+ switch (_a.label) {
1002
+ case 0:
1003
+ interactionsMessage = {
1004
+ content: audioInput,
1005
+ options: {
1006
+ messageType: 'voice'
1007
+ }
1008
+ };
1009
+ _a.label = 1;
1010
+
1011
+ case 1:
1012
+ _a.trys.push([1, 3,, 4]);
1013
+
1014
+ return [4
1015
+ /*yield*/
1016
+ , _aws_amplify_interactions__WEBPACK_IMPORTED_MODULE_5__["Interactions"].send(this.botName, interactionsMessage)];
1017
+
1018
+ case 2:
1019
+ response = _a.sent();
1020
+ return [3
1021
+ /*break*/
1022
+ , 4];
1023
+
1024
+ case 3:
1025
+ err_4 = _a.sent();
1026
+ this.setError(err_4, ChatErrorType.Recoverable);
1027
+ return [2
1028
+ /*return*/
1029
+ ];
1030
+
1031
+ case 4:
1032
+ this.chatState = ChatState.Initial;
1033
+ dialogState = response.dialogState;
1034
+ if (response.inputTranscript) this.appendToChat(response.inputTranscript, MessageFrom.User);
1035
+ this.appendToChat(response.message, MessageFrom.Bot);
1036
+ return [4
1037
+ /*yield*/
1038
+ , this.audioRecorder.play(response.audioStream).then(function () {
1039
+ // if conversationMode is on, chat is incomplete, and mic button isn't pressed yet, resume listening.
1040
+ if (_this.conversationModeOn && dialogState !== 'Fulfilled' && dialogState !== 'Failed' && _this.chatState === ChatState.Initial) {
1041
+ _this.handleMicButton();
1042
+ }
1043
+ })["catch"](function (err) {
1044
+ return _this.setError(err, ChatErrorType.Recoverable);
1045
+ })];
1046
+
1047
+ case 5:
1048
+ _a.sent();
1049
+
1050
+ return [2
1051
+ /*return*/
1052
+ ];
1053
+ }
1054
+ });
1055
+ });
1056
+ };
1057
+
1058
+ class_1.prototype.appendToChat = function (content, from) {
1059
+ this.messages = __spreadArrays(this.messages, [{
1060
+ content: content,
1061
+ from: from
1062
+ }]);
1063
+ };
1064
+ /**
1065
+ * State control methods
1066
+ */
1067
+
1068
+
1069
+ class_1.prototype.setError = function (error, errorType) {
1070
+ var message = typeof error === 'string' ? error : error.message;
1071
+ this.chatState = ChatState.Error;
1072
+ this.error = {
1073
+ message: message,
1074
+ errorType: errorType
1075
+ };
1076
+ };
1077
+
1078
+ class_1.prototype.reset = function () {
1079
+ this.chatState = ChatState.Initial;
1080
+ this.text = '';
1081
+ this.error = undefined;
1082
+ this.messages = [];
1083
+ if (this.welcomeMessage) this.appendToChat(this.welcomeMessage, MessageFrom.Bot);
1084
+ this.audioRecorder && this.audioRecorder.clear();
1085
+ };
1086
+
1087
+ class_1.prototype.listeningFooterJSX = function () {
1088
+ var _this = this;
1089
+
1090
+ var visualization = Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("canvas", {
1091
+ height: "50"
1092
+ });
1093
+ var cancelButton = Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-button", {
1094
+ "data-test": "chatbot-cancel-button",
1095
+ handleButtonClick: function handleButtonClick() {
1096
+ return _this.handleCancelButton();
1097
+ },
1098
+ "class": "icon-button",
1099
+ variant: "icon",
1100
+ icon: "ban"
1101
+ });
1102
+ return [visualization, cancelButton];
1103
+ };
1104
+
1105
+ class_1.prototype.footerJSX = function () {
1106
+ var _this = this;
1107
+
1108
+ if (this.chatState === ChatState.Listening) return this.listeningFooterJSX();
1109
+ var inputPlaceholder = this.textEnabled ? _Translations_108d469f_js__WEBPACK_IMPORTED_MODULE_3__["T"].TEXT_INPUT_PLACEHOLDER : _Translations_108d469f_js__WEBPACK_IMPORTED_MODULE_3__["T"].VOICE_INPUT_PLACEHOLDER;
1110
+ var textInput = Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-input", {
1111
+ placeholder: _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["I18n"].get(inputPlaceholder),
1112
+ description: "text",
1113
+ handleInputChange: function handleInputChange(evt) {
1114
+ return _this.handleTextChange(evt);
1115
+ },
1116
+ value: this.text,
1117
+ disabled: this.chatState === ChatState.Error || !this.textEnabled
1118
+ });
1119
+ var micButton = this.voiceEnabled && Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-button", {
1120
+ "data-test": "chatbot-mic-button",
1121
+ handleButtonClick: function handleButtonClick() {
1122
+ return _this.handleMicButton();
1123
+ },
1124
+ "class": "icon-button",
1125
+ variant: "icon",
1126
+ icon: "microphone",
1127
+ disabled: this.chatState === ChatState.Error || this.chatState !== ChatState.Initial
1128
+ });
1129
+ var sendButton = this.textEnabled && Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-button", {
1130
+ "data-test": "chatbot-send-button",
1131
+ "class": "icon-button",
1132
+ variant: "icon",
1133
+ icon: "send",
1134
+ handleButtonClick: function handleButtonClick() {
1135
+ return _this.sendTextMessage();
1136
+ },
1137
+ disabled: this.chatState === ChatState.Error || this.chatState !== ChatState.Initial
1138
+ });
1139
+ return [textInput, micButton, sendButton];
1140
+ };
1141
+
1142
+ class_1.prototype.errorToast = function () {
1143
+ var _this = this;
1144
+
1145
+ if (!this.error) return;
1146
+ var _a = this.error,
1147
+ message = _a.message,
1148
+ errorType = _a.errorType;
1149
+ return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("amplify-toast", {
1150
+ message: _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["I18n"].get(message),
1151
+ handleClose: function handleClose() {
1152
+ return _this.handleToastClose(errorType);
1153
+ }
1154
+ });
1155
+ };
1156
+
1157
+ class_1.prototype.render = function () {
1158
+ var _this = this;
90
1159
 
91
- AmplifySection.prototype.render = function () {
92
- return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("section", {
93
- "class": "section",
94
- role: this.role
95
- }, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("slot", null));
1160
+ return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["H"], null, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
1161
+ "class": "amplify-chatbot"
1162
+ }, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("slot", {
1163
+ name: "header"
1164
+ }, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
1165
+ "class": "header",
1166
+ "data-test": "chatbot-header"
1167
+ }, _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["I18n"].get(this.botTitle))), Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
1168
+ "class": "body",
1169
+ "data-test": "chatbot-body"
1170
+ }, this.messageJSX(this.messages)), Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("form", {
1171
+ onSubmit: function onSubmit(e) {
1172
+ return _this.handleSubmit(e);
1173
+ }
1174
+ }, Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["h"])("div", {
1175
+ "class": "footer",
1176
+ "data-test": "chatbot-footer"
1177
+ }, this.footerJSX())), this.errorToast()));
96
1178
  };
97
1179
 
98
- Object.defineProperty(AmplifySection.prototype, "el", {
1180
+ Object.defineProperty(class_1.prototype, "element", {
99
1181
  get: function get() {
100
1182
  return Object(_index_83f2275b_js__WEBPACK_IMPORTED_MODULE_0__["g"])(this);
101
1183
  },
102
1184
  enumerable: false,
103
1185
  configurable: true
104
1186
  });
105
- return AmplifySection;
1187
+ return class_1;
106
1188
  }();
107
1189
 
108
- AmplifySection.style = amplifySectionCss;
1190
+ AmplifyChatbot.style = amplifyChatbotCss;
109
1191
 
110
1192
 
111
1193
  /***/ })