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

move numberOfAttempts to state from options #889

Merged
merged 7 commits into from
Apr 25, 2019
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
26 changes: 13 additions & 13 deletions libraries/botbuilder-ai/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libraries/botbuilder-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"botbuilder-core": "~4.1.6",
"html-entities": "^1.2.1",
"moment": "^2.20.1",
"@azure/ms-rest-js": "1.8.1",
"@azure/ms-rest-js": "1.8.2",
"request": "^2.87.0",
"request-promise-native": "1.0.5",
"url-parse": "^1.4.4"
Expand Down
7 changes: 6 additions & 1 deletion libraries/botbuilder-dialogs/src/prompts/activityPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ export class ActivityPrompt extends Dialog {
const state: any = dc.activeDialog.state as ActivityPromptState;
const recognized: PromptRecognizerResult<Activity> = await this.onRecognize(dc.context, state.state, state.options);

if (state.state['attemptCount'] === undefined) {
state.state['attemptCount'] = 1;
}

// Validate the return value
// - Unlike the other prompts a validator is required for an ActivityPrompt so we don't
// need to check for its existence before calling it.
const isValid: boolean = await this.validator({
context: dc.context,
recognized: recognized,
state: state.state,
options: state.options
options: state.options,
attemptCount: state.state['attemptCount']
});

// Return recognized value or re-prompt
Expand Down
8 changes: 7 additions & 1 deletion libraries/botbuilder-dialogs/src/prompts/oauthPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,20 @@ export class OAuthPrompt extends Dialog {
if (hasTimedOut) {
return await dc.endDialog(undefined);
} else {

if (state.state['attemptCount'] === undefined) {
state.state['attemptCount'] = 1;
}

// Validate the return value
let isValid = false;
if (this.validator) {
isValid = await this.validator({
context: dc.context,
recognized: recognized,
state: state.state,
options: state.options
options: state.options,
attemptCount: state.state['attemptCount']
});
} else if (recognized.succeeded) {
isValid = true;
Expand Down
23 changes: 13 additions & 10 deletions libraries/botbuilder-dialogs/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ export interface PromptOptions {
* (Optional) Additional validation rules to pass the prompts validator routine.
*/
validations?: object;

/**
* (Optional) Count of the number of times the prompt has retried.
*/
numberOfAttempts?: number;
}

/**
Expand Down Expand Up @@ -156,6 +151,13 @@ export interface PromptValidatorContext<T> {
* The validator can extend this interface to support additional prompt options.
*/
readonly options: PromptOptions;

/**
* A count of the number of times the prompt has been executed.
*
* A number indicating how many times the prompt was invoked (starting at 1 for the first time it was invoked).
*/
readonly attemptCount: number;
}

/**
Expand Down Expand Up @@ -206,17 +208,18 @@ export abstract class Prompt<T> extends Dialog {
// Validate the return value
let isValid = false;
if (this.validator) {
if (state.options.numberOfAttempts === undefined) {
state.options.numberOfAttempts = 0;
if (state.state['attemptCount'] === undefined) {
state.state['attemptCount'] = 1;
}
isValid = await this.validator({
context: dc.context,
recognized: recognized,
state: state.state,
options: state.options
options: state.options,
attemptCount: state.state['attemptCount']
});
if (state.options.numberOfAttempts !== undefined) {
state.options.numberOfAttempts++;
if (state.state['attemptCount'] !== undefined) {
state.state['attemptCount']++;
}
} else if (recognized.succeeded) {
isValid = true;
Expand Down
16 changes: 8 additions & 8 deletions libraries/botbuilder-dialogs/tests/numberPrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('NumberPrompt', function () {
.assertReply('0')
});

it ('should see numberOfAttempts counter increment', async function() {
it ('should see attemptCount increment', async function() {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand All @@ -222,7 +222,7 @@ describe('NumberPrompt', function () {

dialogs.add(new NumberPrompt('prompt', async (prompt) => {
if (prompt.recognized.value !== 0) {
prompt.context.sendActivity(`numberOfAttempts ${prompt.options.numberOfAttempts}`);
prompt.context.sendActivity(`attemptCount ${prompt.attemptCount}`);
return false;
}
return true;
Expand All @@ -231,21 +231,21 @@ describe('NumberPrompt', function () {
await adapter.send('Hello')
.assertReply('Send me a zero')
gabog marked this conversation as resolved.
Show resolved Hide resolved
.send('100')
.assertReply('numberOfAttempts 0')
.assertReply('attemptCount 1')
.send('200')
.assertReply('numberOfAttempts 1')
.assertReply('attemptCount 2')
.send('300')
.assertReply('numberOfAttempts 2')
.assertReply('attemptCount 3')
.send('0')
.assertReply('ok')
.send('Another!')
.assertReply('Send me a zero')
.send('100')
.assertReply('numberOfAttempts 0')
.assertReply('attemptCount 1')
.send('200')
.assertReply('numberOfAttempts 1')
.assertReply('attemptCount 2')
.send('300')
.assertReply('numberOfAttempts 2')
.assertReply('attemptCount 3')
.send('0')
.assertReply('ok')
});
Expand Down
Loading