Skip to content

Commit

Permalink
fix(skills): fixed choice skill repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
slvnperron committed Jul 29, 2019
1 parent 61d906e commit ffc7ccd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
3 changes: 2 additions & 1 deletion modules/basic-skills/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"bluebird-global": "^1.0.1",
"lodash": "^4.17.11",
"react-dnd": "^7.4.5",
"react-tag-input": "6.1.0"
"react-tag-input": "6.1.0",
"yn": "^3.1.0"
}
}
13 changes: 3 additions & 10 deletions modules/basic-skills/src/actions/choice_invalid_answer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict'
const _ = require('lodash')

/**
* @hidden true
*/
const invalidAnswer = async () => {
const key = 'skill-choice-invalid-count'
const value = (temp[key] || 0) + 1

temp[key] = value
}

return invalidAnswer()
const key = 'skill-choice-invalid-count'
const value = (temp[key] || 0) + 1
temp[key] = value
32 changes: 23 additions & 9 deletions modules/basic-skills/src/backend/choice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as sdk from 'botpress/sdk'
import _ from 'lodash'
import yn from 'yn'

const setup = async bp => {
const router = bp.http.createRouterForBot('basic-skills')
Expand Down Expand Up @@ -33,7 +34,25 @@ const setup = async bp => {

const generateFlow = async (data: any, metadata: sdk.FlowGeneratorMetadata): Promise<sdk.FlowGenerationResult> => {
const hardRetryLimit = 10
const nbMaxRetries = Number(Math.min(data.config.nbMaxRetries, hardRetryLimit))
const nbMaxRetries = Math.min(Number(data.config.nbMaxRetries), hardRetryLimit)
const repeatQuestion = yn(data.config.repeatChoicesOnInvalid)

const sorrySteps = []

if (data.invalidContentId && data.invalidContentId.length >= 3) {
sorrySteps.push({
type: sdk.NodeActionType.RenderElement,
name: `#!${data.invalidContentId}`
})
}

if (repeatQuestion) {
sorrySteps.push({
type: sdk.NodeActionType.RenderElement,
name: `#!${data.contentId}`,
args: { skill: 'choice' }
})
}

const nodes: sdk.SkillFlowNode[] = [
{
Expand Down Expand Up @@ -68,21 +87,16 @@ const generateFlow = async (data: any, metadata: sdk.FlowGeneratorMetadata): Pro
],
next: [
{
condition: `temp['skill-choice-invalid-count'] == ${nbMaxRetries}`,
condition: `Number(temp['skill-choice-invalid-count']) >= Number(${nbMaxRetries})`,
node: '#'
},
{ condition: 'true', node: 'sorry' }
]
},
{
name: 'sorry',
onEnter: data.invalidContentId && [
{
type: sdk.NodeActionType.RenderElement,
name: `#!${data.invalidContentId}`
}
],
next: [{ condition: 'true', node: 'entry' }]
onEnter: sorrySteps,
next: [{ condition: 'true', node: 'parse' }]
}
]

Expand Down
23 changes: 17 additions & 6 deletions modules/basic-skills/src/views/full/choice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ export class Choice extends React.Component {
}

onMaxRetriesChanged = event => {
const value = Number(event.target.value)

if (value > MAX_RETRIES) {
this.setState({ config: { ...this.state.config, nbMaxRetries: MAX_RETRIES } })
return
const config = {
...this.state.config,
nbMaxRetries: isNaN(Number(event.target.value)) ? MAX_RETRIES : Number(event.target.value)
}
this.setState({ config })
}

this.setState({ config: { ...this.state.config, nbMaxRetries: value } })
onToggleRepeatChoicesOnInvalid = event => {
this.setState({
config: { ...this.state.config, repeatChoicesOnInvalid: !this.state.config.repeatChoicesOnInvalid }
})
}

onBlocNameChanged = key => event => {
Expand Down Expand Up @@ -228,13 +231,21 @@ export class Choice extends React.Component {
onChange={this.handleInvalidContentChange}
placeholder="Pick a reply"
/>
<Label htmlFor="repeatChoices">Repeat choices on invalid?</Label>
<input
id="repeatChoices"
type="checkbox"
checked={this.state.config.repeatChoicesOnInvalid}
onChange={this.onToggleRepeatChoicesOnInvalid}
/>
</div>

<div>
<Label htmlFor="contentElementType">Default choice content type:</Label>
<Input
id="contentElementType"
type="text"
style={{ marginLeft: '5px' }}
value={this.getContentType()}
onChange={this.handleConfigTextChanged('contentElement')}
/>
Expand Down
5 changes: 5 additions & 0 deletions modules/basic-skills/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5713,3 +5713,8 @@ yargs@^7.0.0:
which-module "^1.0.0"
y18n "^3.2.1"
yargs-parser "^5.0.0"

yn@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.0.tgz#fcbe2db63610361afcc5eb9e0ac91e976d046114"
integrity sha512-kKfnnYkbTfrAdd0xICNFw7Atm8nKpLcLv9AZGEt+kczL/WQVai4e2V6ZN8U/O+iI6WrNuJjNNOyu4zfhl9D3Hg==

0 comments on commit ffc7ccd

Please sign in to comment.