Skip to content

Commit

Permalink
chore(interactions): run eslint --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Nov 22, 2023
1 parent 42d415c commit 48a2595
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 89 deletions.
2 changes: 1 addition & 1 deletion packages/interactions/src/errors/assertValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { InteractionsError } from './InteractionsError';
export function assertValidationError(
assertion: boolean,
name: InteractionsValidationErrorCode,
message?: string
message?: string,
): asserts assertion {
if (!assertion) {
const { message: defaultMessage, recoverySuggestion } =
Expand Down
21 changes: 12 additions & 9 deletions packages/interactions/src/lex-v1/AWSLexProvider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
InteractionsOnCompleteCallback,
InteractionsMessage,
InteractionsOnCompleteCallback,
InteractionsResponse,
} from '../types/Interactions';
} from '~/types/Interactions';
import {
DialogState,
LexRuntimeServiceClient,
Expand All @@ -17,7 +17,8 @@ import {
} from '@aws-sdk/client-lex-runtime-service';
import { getAmplifyUserAgentObject } from '@aws-amplify/core/internals/utils';
import { ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';
import { convert } from '../utils';
import { convert } from '~/utils';

import { AWSLexProviderOption } from './types';

const logger = new ConsoleLogger('AWSLexProvider');
Expand All @@ -44,7 +45,7 @@ class AWSLexProvider {
*/
reportBotStatus(
data: AWSLexProviderSendResponse,
{ name }: AWSLexProviderOption
{ name }: AWSLexProviderOption,
) {
const callback = this._botsCompleteCallback[name];
if (!callback) {
Expand All @@ -68,14 +69,14 @@ class AWSLexProvider {

async sendMessage(
botConfig: AWSLexProviderOption,
message: string | InteractionsMessage
message: string | InteractionsMessage,
): Promise<InteractionsResponse> {
// check if credentials are present
let session;
try {
session = await fetchAuthSession();
} catch (error) {
return Promise.reject('No credentials');
return Promise.reject(new Error('No credentials'));
}

const { name, region, alias } = botConfig;
Expand All @@ -100,6 +101,7 @@ class AWSLexProvider {
const data = await client.send(postTextCommand);

this.reportBotStatus(data, botConfig);

return data;
} catch (err) {
return Promise.reject(err);
Expand All @@ -111,7 +113,7 @@ class AWSLexProvider {
} = message;
if (messageType === 'voice') {
if (typeof content !== 'object') {
return Promise.reject('invalid content type');
return Promise.reject(new Error('invalid content type'));
}
const inputStream =
content instanceof Uint8Array ? content : await convert(content);
Expand All @@ -126,7 +128,7 @@ class AWSLexProvider {
};
} else {
if (typeof content !== 'string')
return Promise.reject('invalid content type');
return Promise.reject(new Error('invalid content type'));

params = {
botAlias: alias,
Expand All @@ -149,6 +151,7 @@ class AWSLexProvider {
const response = { ...data, ...{ audioStream: audioArray } };

this.reportBotStatus(response, botConfig);

return response;
} catch (err) {
return Promise.reject(err);
Expand All @@ -158,7 +161,7 @@ class AWSLexProvider {

onComplete(
{ name }: AWSLexProviderOption,
callback: InteractionsOnCompleteCallback
callback: InteractionsOnCompleteCallback,
) {
this._botsCompleteCallback[name] = callback;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/interactions/src/lex-v1/apis/onComplete.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { OnCompleteInput } from '../types';
import { resolveBotConfig } from '../utils';
import { lexProvider } from '../AWSLexProvider';
import { OnCompleteInput } from '~/lex-v1/types';
import { resolveBotConfig } from '~/lex-v1/utils';
import { lexProvider } from '~/lex-v1/AWSLexProvider';
import {
assertValidationError,
InteractionsValidationErrorCode,
} from '../../errors';
assertValidationError,
} from '~/errors';

export const onComplete = (input: OnCompleteInput): void => {
const { botName, callback } = input;
const botConfig = resolveBotConfig(botName);
assertValidationError(
!!botConfig,
InteractionsValidationErrorCode.NoBotConfig,
`Bot ${botName} does not exist.`
`Bot ${botName} does not exist.`,
);
lexProvider.onComplete(botConfig, callback);
};
13 changes: 7 additions & 6 deletions packages/interactions/src/lex-v1/apis/send.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { SendInput, SendOutput } from '../types';
import { resolveBotConfig } from '../utils';
import { lexProvider } from '../AWSLexProvider';
import { SendInput, SendOutput } from '~/lex-v1/types';
import { resolveBotConfig } from '~/lex-v1/utils';
import { lexProvider } from '~/lex-v1/AWSLexProvider';
import {
assertValidationError,
InteractionsValidationErrorCode,
} from '../../errors';
assertValidationError,
} from '~/errors';

export const send = async (input: SendInput): Promise<SendOutput> => {
const { botName, message } = input;
const botConfig = resolveBotConfig(botName);
assertValidationError(
!!botConfig,
InteractionsValidationErrorCode.NoBotConfig,
`Bot ${botName} does not exist.`
`Bot ${botName} does not exist.`,
);

return lexProvider.sendMessage(botConfig, message);
};
2 changes: 1 addition & 1 deletion packages/interactions/src/lex-v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { send, onComplete } from './apis';
import { onComplete, send } from './apis';
import { IInteractions } from './types/AWSLexProviderOption';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { SendInput, OnCompleteInput } from './inputs';
import { OnCompleteInput, SendInput } from './inputs';
import { SendOutput } from './outputs';

export interface AWSLexProviderOption {
Expand Down
4 changes: 2 additions & 2 deletions packages/interactions/src/lex-v1/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

import {
InteractionsSendInput,
InteractionsOnCompleteInput,
} from '../../types';
InteractionsSendInput,
} from '~/types';

/**
* Input type for LexV1 send API.
Expand Down
2 changes: 1 addition & 1 deletion packages/interactions/src/lex-v1/types/outputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { InteractionsSendOutput } from '../../types';
import { InteractionsSendOutput } from '~/types';

/**
* Output type for LexV1 send API.
Expand Down
4 changes: 2 additions & 2 deletions packages/interactions/src/lex-v1/utils/resolveBotConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { AWSLexProviderOption } from '../types';
import { AWSLexProviderOption } from '~/lex-v1/types';
import { Amplify } from '@aws-amplify/core';

export const resolveBotConfig = (
botName: string
botName: string,
): AWSLexProviderOption | undefined => {
const { [botName]: botConfig = undefined } =
Amplify.getConfig().Interactions?.LexV1 ?? {};
Expand Down
47 changes: 26 additions & 21 deletions packages/interactions/src/lex-v2/AWSLexV2Provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
InteractionsOnCompleteCallback,
InteractionsMessage,
InteractionsOnCompleteCallback,
InteractionsResponse,
} from '../types/Interactions';
} from '~/types/Interactions';
import {
IntentState,
LexRuntimeV2Client,
Expand All @@ -17,8 +17,9 @@ import {
} from '@aws-sdk/client-lex-runtime-v2';
import { getAmplifyUserAgentObject } from '@aws-amplify/core/internals/utils';
import { ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';
import { convert, unGzipBase64AsJson } from '../utils';
import { convert, unGzipBase64AsJson } from '~/utils';
import { v4 as uuid } from 'uuid';

import { AWSLexV2ProviderOption } from './types';

const logger = new ConsoleLogger('AWSLexV2Provider');
Expand All @@ -43,18 +44,19 @@ type AWSLexV2ProviderSendResponse =
| RecognizeTextCommandOutput
| RecognizeUtteranceCommandOutputFormatted;

type lexV2BaseReqParams = {
interface lexV2BaseReqParams {
botId: string;
botAliasId: string;
localeId: string;
sessionId: string;
};
}

class AWSLexV2Provider {
private readonly _botsCompleteCallback: Record<
string,
InteractionsOnCompleteCallback
> = {};

private defaultSessionId: string = uuid();

/**
Expand All @@ -66,14 +68,14 @@ class AWSLexV2Provider {
*/
public async sendMessage(
botConfig: AWSLexV2ProviderOption,
message: string | InteractionsMessage
message: string | InteractionsMessage,
): Promise<InteractionsResponse> {
// check if credentials are present
let session;
try {
session = await fetchAuthSession();
} catch (error) {
return Promise.reject('No credentials');
return Promise.reject(new Error('No credentials'));
}

const { region, aliasId, localeId, botId } = botConfig;
Expand All @@ -98,16 +100,17 @@ class AWSLexV2Provider {
botConfig,
message,
reqBaseParams,
client
client,
);
} else {
response = await this._handleRecognizeUtteranceCommand(
botConfig,
message,
reqBaseParams,
client
client,
);
}

return response;
}

Expand All @@ -119,7 +122,7 @@ class AWSLexV2Provider {
*/
public onComplete(
{ name }: AWSLexV2ProviderOption,
callback: InteractionsOnCompleteCallback
callback: InteractionsOnCompleteCallback,
) {
this._botsCompleteCallback[name] = callback;
}
Expand All @@ -129,7 +132,7 @@ class AWSLexV2Provider {
*/
_reportBotStatus(
data: AWSLexV2ProviderSendResponse,
{ name }: AWSLexV2ProviderOption
{ name }: AWSLexV2ProviderOption,
) {
const sessionState = data?.sessionState;

Expand Down Expand Up @@ -159,7 +162,7 @@ class AWSLexV2Provider {
* update audioStream format
*/
private async _formatUtteranceCommandOutput(
data: RecognizeUtteranceCommandOutput
data: RecognizeUtteranceCommandOutput,
): Promise<RecognizeUtteranceCommandOutputFormatted> {
return {
...data,
Expand All @@ -182,7 +185,7 @@ class AWSLexV2Provider {
botConfig: AWSLexV2ProviderOption,
data: string,
baseParams: lexV2BaseReqParams,
client: LexRuntimeV2Client
client: LexRuntimeV2Client,
) {
logger.debug('postText to lex2', data);

Expand All @@ -193,10 +196,11 @@ class AWSLexV2Provider {

try {
const recognizeTextCommand = new RecognizeTextCommand(params);
const data = await client.send(recognizeTextCommand);
const result = await client.send(recognizeTextCommand);

this._reportBotStatus(data, botConfig);
return data;
this._reportBotStatus(result, botConfig);

return result;
} catch (err) {
return Promise.reject(err);
}
Expand All @@ -210,7 +214,7 @@ class AWSLexV2Provider {
botConfig: AWSLexV2ProviderOption,
data: InteractionsMessage,
baseParams: lexV2BaseReqParams,
client: LexRuntimeV2Client
client: LexRuntimeV2Client,
) {
const {
content,
Expand All @@ -223,7 +227,7 @@ class AWSLexV2Provider {
// prepare params
if (messageType === 'voice') {
if (typeof content !== 'object') {
return Promise.reject('invalid content type');
return Promise.reject(new Error('invalid content type'));
}

const inputStream =
Expand All @@ -237,7 +241,7 @@ class AWSLexV2Provider {
} else {
// text input
if (typeof content !== 'string')
return Promise.reject('invalid content type');
return Promise.reject(new Error('invalid content type'));

params = {
...baseParams,
Expand All @@ -249,10 +253,11 @@ class AWSLexV2Provider {
// make API call to lex
try {
const recognizeUtteranceCommand = new RecognizeUtteranceCommand(params);
const data = await client.send(recognizeUtteranceCommand);
const result = await client.send(recognizeUtteranceCommand);

const response = await this._formatUtteranceCommandOutput(data);
const response = await this._formatUtteranceCommandOutput(result);
this._reportBotStatus(response, botConfig);

return response;
} catch (err) {
return Promise.reject(err);
Expand Down
Loading

0 comments on commit 48a2595

Please sign in to comment.