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

Update Emulator to Use New Cognitive Service API #1878

Merged
merged 1 commit into from
Oct 8, 2019

Conversation

bwamie
Copy link

@bwamie bwamie commented Sep 22, 2019

Update Emulator to Use New Cognitive Service API
Add token refresh 5 minutes before it expires.

@coveralls
Copy link

coveralls commented Sep 22, 2019

Coverage Status

Coverage increased (+0.02%) to 66.959% when pulling e2ccb9a on edwinb/speech-for-paid-api-changes into 98e7b21 on master.

Copy link
Contributor

@compulim compulim left a comment

Choose a reason for hiding this comment

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

Add tests.

packages/app/client/src/state/sagas/chatSagas.ts Outdated Show resolved Hide resolved
packages/emulator/core/src/facility/botEndpoint.ts Outdated Show resolved Hide resolved
packages/emulator/core/src/facility/botEndpoint.ts Outdated Show resolved Hide resolved
packages/sdk/shared/src/types/index.ts Outdated Show resolved Hide resolved
packages/sdk/shared/src/types/speechRegionToken.ts Outdated Show resolved Hide resolved
if (!this.msaAppId || !this.msaPassword) {
throw new Error('bot must have Microsoft App ID and password');
}
private isTokenExpired(): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't duplicate code.

Copy link
Author

Choose a reason for hiding this comment

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

With which one? Its not duplacted. If you mean with isTokenExpiringSoon then it is not. They are different.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the isTokenExpired and isTokenExpiringSoon is too similar.

It may make sense to return the expiry, or building a function named willTokenExpireWithin(durationInMs: number = 0).


const query = new URLSearchParams({ goodForInMinutes: duration } as any);
const res = await this.fetchWithAuth(new URL(`?${query.toString()}`, speechEndpoint.tokenEndpoint).toString());
private renewTokenBeforeExpiry() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use then, use async/await.

packages/app/shared/src/types/speechTypes.ts Show resolved Hide resolved
@@ -0,0 +1,38 @@
//
Copy link
Contributor

Choose a reason for hiding this comment

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

Why there is a dupe file in /sdk/?

Copy link
Author

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor

Choose a reason for hiding this comment

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

We should just choose one -- I vote for sdk -- and delete the other one. There should be minimal code churn because the type SpeechTokenInfo is only used in one place

packages/app/shared/src/types/index.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@tonyanziano tonyanziano left a comment

Choose a reason for hiding this comment

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

After you finish resolving PR feedback, please run the command npm run lint:fix from the root of the project. This should fix issues like import sorting, however it might not alphabetize them.

@@ -0,0 +1,38 @@
//
Copy link
Contributor

Choose a reason for hiding this comment

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

We should just choose one -- I vote for sdk -- and delete the other one. There should be minimal code churn because the type SpeechTokenInfo is only used in one place

}

if (!this.msaAppId || !this.msaPassword) {
throw new Error('bot must have Microsoft App ID and password');
Copy link
Contributor

Choose a reason for hiding this comment

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

I mean it looks a bit late to throw here.

Copy link
Contributor

@tonyanziano tonyanziano left a comment

Choose a reason for hiding this comment

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

Pulled it down and tested it. Looks good to me, but you should address the comments William left. 👍

packages/app/client/src/state/sagas/chatSagas.ts Outdated Show resolved Hide resolved
packages/emulator/core/src/facility/botEndpoint.ts Outdated Show resolved Hide resolved
}

if (this.speechRegionToken && !refresh && !this.willTokenExpireWithin(0)) {
if (this.willTokenExpireWithin(this.speechRegionToken.tokenLife * 0.5)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (this.willTokenExpireWithin(this.speechRegionToken.tokenLife * 0.5)) {
if (this.willTokenExpireWithin(this.speechRegionToken.tokenLife / 2)) {

}
private willTokenExpireWithin(millisecondsToExpire: number): boolean {
const currentDateTime = new Date();
const timeFromNowMilliseconds = currentDateTime.getTime() + millisecondsToExpire;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const timeFromNowMilliseconds = currentDateTime.getTime() + millisecondsToExpire;
const timeFromNowMilliseconds = Date.now() + millisecondsToExpire;

packages/emulator/core/src/authEndpoints.ts Outdated Show resolved Hide resolved
packages/app/client/src/state/sagas/chatSagas.ts Outdated Show resolved Hide resolved
packages/emulator/core/src/facility/botEndpoint.ts Outdated Show resolved Hide resolved
packages/emulator/core/src/facility/botEndpoint.ts Outdated Show resolved Hide resolved
packages/emulator/core/src/facility/botEndpoint.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@compulim compulim left a comment

Choose a reason for hiding this comment

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

In addition for comments below, there are couple of things that are still unanswered in previous reviews.


if (speechAuthenticationToken && speechAuthenticationToken.access_Token && speechAuthenticationToken.region) {
const factory = yield call(createCognitiveServicesSpeechServicesPonyfillFactory, {
authorizationToken: () => Promise.resolve(speechAuthenticationToken.access_Token),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should a Promise function. When called, will fetch the token from the server. And the token need to be cached for its half-life.

@tonyanziano tonyanziano force-pushed the edwinb/speech-for-paid-api-changes branch from 505fd53 to da0ef01 Compare October 7, 2019 23:20
Copy link
Contributor

@compulim compulim left a comment

Choose a reason for hiding this comment

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

Looks good to me.

If the speech token call is taking too much time, and the user keep clicking on the microphone button. It may end up having multiple ongoing "get token call". I am okay with this for now.

Love your tests. AFAIR, people said test code is statistically 3 times the size of production code.

Let's wait for Chris to test it out.

packages/emulator/core/src/authEndpoints.ts Outdated Show resolved Hide resolved
@cwhitten cwhitten dismissed stale reviews from compulim and tonyanziano October 8, 2019 17:02

stale

cwhitten
cwhitten previously approved these changes Oct 8, 2019
@cwhitten
Copy link
Member

cwhitten commented Oct 8, 2019

Nice work @edwin4microsoft & @tonyanziano. Let's resolve conflicts and get it in.

@tonyanziano tonyanziano force-pushed the edwinb/speech-for-paid-api-changes branch from 06b0443 to e2ccb9a Compare October 8, 2019 17:36
@tonyanziano tonyanziano merged commit 90384b8 into master Oct 8, 2019
@tonyanziano tonyanziano deleted the edwinb/speech-for-paid-api-changes branch October 8, 2019 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants