Skip to content

Commit

Permalink
fix(@aws-amplify/interactions): add audio type Uint8Array to lexV1 (#…
Browse files Browse the repository at this point in the history
…10273)

* fix(interactions): add audio type Uint8Array to lexV1
  • Loading branch information
ashwinkumar6 committed Aug 31, 2022
1 parent 91fdcd9 commit 840086d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 25 deletions.
10 changes: 5 additions & 5 deletions packages/interactions/src/Providers/AWSLexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
Credentials,
getAmplifyUserAgent,
} from '@aws-amplify/core';
import { convert } from './AWSLexProviderHelper/convert';
import { convert } from './AWSLexProviderHelper/utils';

const logger = new Logger('AWSLexProvider');

Expand Down Expand Up @@ -163,17 +163,17 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
content,
options: { messageType },
} = message;
if (messageType === 'voice') {
if (!(content instanceof Blob || content instanceof ReadableStream))
return Promise.reject('invalid content type');
if (messageType === 'voice' && typeof content === 'object') {
const inputStream =
content instanceof Uint8Array ? content : await convert(content);

params = {
botAlias: this._config[botname].alias,
botName: botname,
contentType: 'audio/x-l16; sample-rate=16000; channel-count=1',
inputStream: await convert(content),
userId: credentials.identityId,
accept: 'audio/mpeg',
inputStream,
};
} else {
if (typeof content !== 'string')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* and limitations under the License.
*/
import { strFromU8 } from 'fflate';
import { base64ToArrayBuffer, gzipDecompress } from './convert';
import { base64ToArrayBuffer, gzipDecompress } from './utils';

export const unGzipBase64AsJson = async (gzipBase64: string | undefined) => {
if (typeof gzipBase64 === 'undefined') return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import { decode } from 'base-64';
import { gunzipSync } from 'fflate';

export const convert = async (stream: Blob): Promise<Uint8Array> => {
export const convert = async (stream: object): Promise<Uint8Array> => {
if (!(stream instanceof Blob)) {
return Promise.reject('Invalid content type');
}

return new Promise(async (resolve, reject) => {
try {
const fileReaderInstance = new FileReader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
* and limitations under the License.
*/

import { Readable } from 'stream';
import { gunzip } from 'fflate';

export const convert = async (
stream: Blob | Readable | ReadableStream
): Promise<Uint8Array> => {
export const convert = async (stream: object): Promise<Uint8Array> => {
if (stream instanceof Blob || stream instanceof ReadableStream) {
return new Response(stream)
.arrayBuffer()
.then(buffer => new Uint8Array(buffer));
} else {
return Promise.reject('Readable is not supported.');
return Promise.reject('Invalid content type');
}
};

Expand Down
15 changes: 2 additions & 13 deletions packages/interactions/src/Providers/AWSLexV2Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
Credentials,
getAmplifyUserAgent,
} from '@aws-amplify/core';
import { convert } from './AWSLexProviderHelper/convert';
import { convert } from './AWSLexProviderHelper/utils';
import { unGzipBase64AsJson } from './AWSLexProviderHelper/commonUtils';

const logger = new Logger('AWSLexV2Provider');
Expand Down Expand Up @@ -301,18 +301,7 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
let params: RecognizeUtteranceCommandInput;

// prepare params
if (messageType === 'voice') {
// voice input
if (
!(
content instanceof Blob ||
content instanceof ReadableStream ||
content instanceof Uint8Array
)
) {
return Promise.reject('invalid content type');
}

if (messageType === 'voice' && typeof content === 'object') {
const inputStream =
content instanceof Uint8Array ? content : await convert(content);

Expand Down

0 comments on commit 840086d

Please sign in to comment.