Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(interactions): add audio type Uint8Array to lexV1 #10273

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure I'm just not thinking of it but what is the case in which an InteractionsMessage might be of type voice but not have an object type content?

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 ||
Copy link
Member Author

@ashwinkumar6 ashwinkumar6 Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RN does not support ReadableStream. So initially thought of moving this guard clause into separate a function, and RN's logic for the same will lie in .native file

But if the guard clause is moved externally to a function Ts isn't able to detect it and cribs.
Finally moved the guard clause directly inside the convert function and made it argument generic.

content instanceof Uint8Array
)
) {
return Promise.reject('invalid content type');
}

if (messageType === 'voice' && typeof content === 'object') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here

const inputStream =
content instanceof Uint8Array ? content : await convert(content);

Expand Down