Skip to content

Commit

Permalink
Merge pull request #1131 from botpress/ya-fix-skill
Browse files Browse the repository at this point in the history
fix(skill-choice): fixed issue with skill choice
  • Loading branch information
slvnperron committed Nov 18, 2018
2 parents c82a347 + 4d55c6c commit 6a01aff
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 32 deletions.
7 changes: 4 additions & 3 deletions modules/basic-skills/package.json
Expand Up @@ -26,11 +26,12 @@
"devDependencies": {
"@types/node": "^10.11.0",
"classnames": "^2.2.5",
"module-builder": "../../build/module-builder",
"react-sortable-hoc": "^0.6.8",
"react-tag-input": "^5.0.2",
"module-builder": "../../build/module-builder"
"react-tag-input": "^5.0.2"
},
"dependencies": {
"bluebird": "^3.4.6"
"bluebird": "^3.4.6",
"lodash": "^4.17.11"
}
}
Expand Up @@ -11,7 +11,7 @@ const INTENT_PREFIX = 'intent:'
*/
const validateChoice = async data => {
let choice = undefined
const config = await bp.config.getModuleConfigForBot('skill-choice', event.botId)
const config = await bp.config.getModuleConfigForBot('basic-skills', event.botId)
const nb = _.get(event.preview.match(/^[#).!]?([\d]{1,2})[#).!]?$/), '[1]')

if (config.matchNumbers && nb) {
Expand Down
15 changes: 9 additions & 6 deletions modules/basic-skills/src/backend/choice.ts
Expand Up @@ -3,9 +3,9 @@ import * as sdk from 'botpress/sdk'
import _ from 'lodash'

const generateFlow = (data): sdk.FlowGenerationResult => {
const invalidTextData: any = {}
let onInvalidText = undefined
if (data.config.invalidText && data.config.invalidText.length) {
invalidTextData.text = data.config.invalidText
onInvalidText = data.config.invalidText
}

const maxAttempts = data.config.nbMaxRetries
Expand All @@ -27,7 +27,7 @@ const generateFlow = (data): sdk.FlowGenerationResult => {
onReceive: [
{
type: sdk.NodeActionType.RunAction,
name: 'skill-choice/parse_answer',
name: 'basic-skills/choice_parse_answer',
args: data
}
],
Expand All @@ -38,11 +38,14 @@ const generateFlow = (data): sdk.FlowGenerationResult => {
onEnter: [
{
type: sdk.NodeActionType.RunAction,
name: 'skill-choice/invalid_answer'
name: 'basic-skills/choice_invalid_answer'
}
],
next: [
{ condition: `state['skill-choice-invalid-count'] <= "3"`, node: 'sorry' },
{
condition: `state['skill-choice-invalid-count'] <= ${maxAttempts}`,
node: 'sorry'
},
{ condition: 'true', node: '#' }
]
},
Expand All @@ -52,7 +55,7 @@ const generateFlow = (data): sdk.FlowGenerationResult => {
{
type: sdk.NodeActionType.RenderElement,
name: `#!${data.contentId}`,
args: { ...invalidTextData, skill: 'choice' }
args: { ...{ skill: 'choice' }, ...(onInvalidText ? { text: onInvalidText } : {}) }
}
],
next: [{ condition: 'true', node: 'parse' }]
Expand Down
2 changes: 1 addition & 1 deletion modules/basic-skills/src/views/choice.jsx
Expand Up @@ -199,7 +199,7 @@ export default class TemplateModule extends React.Component {
/>
</div>
<div>
<label htmlFor="invalidText">On invalid choice, say this instead of repeating question:</label>
<label htmlFor="invalidText">On invalid choice, say this before repeating question:</label>
<div>
<textarea
id="invalidText"
Expand Down
5 changes: 2 additions & 3 deletions src/bp/core/services/cms/content-sender.ts
Expand Up @@ -17,7 +17,7 @@ export class ContentElementSender {
) {}

// TODO: Test if the payload is parsing its template properly
async sendContent(contentId: string, args: string, state, event) {
async sendContent(contentId: string, args: object = {}, state, event) {
process.ASSERT_LICENSED()
contentId = contentId.replace(/^#?/i, '')

Expand All @@ -43,8 +43,7 @@ export class ContentElementSender {
if (message) {
_.set(content, 'formData.text', Mustache.render(message, view))
}

renderedElements = await this.cms.renderElement(content.contentType, content.formData, channel)
renderedElements = await this.cms.renderElement(content.contentType, { ...content.formData, ...args }, channel)
} else {
renderedElements = await this.cms.renderElement(contentId, args, channel)
}
Expand Down
4 changes: 2 additions & 2 deletions src/bp/core/services/dialog/engine.ts
Expand Up @@ -242,7 +242,7 @@ export class DialogEngine {
currentFlowName: parentFlow.name,
queue: queue.toString()
}
this._logExitFlow(event.botId, context.currentFlowName, context.currentFlowName, parentFlow, parentNode)
this._logExitFlow(event.botId, context.currentFlowName, context.currentFlowName, parentFlow.name, parentNode.name)
} else if (transitionTo === 'END') {
// END means the node has a transition of "end flow" in the flow editor
await this.sessionService.deleteSession(sessionId)
Expand Down Expand Up @@ -343,7 +343,7 @@ export class DialogEngine {
}

private _logExitFlow(botId, currentFlow, currentNode, previousFlow, previousNode) {
this.logger.forBot(botId).debug(`(${currentFlow})[${currentNode}] << (${previousFlow})[${previousNode}]`)
this.logger.forBot(botId).debug(`(${previousFlow})[${previousNode}] >> (${currentFlow})[${currentNode}]`)
}

private _logEnterFlow(botId, currentFlow, currentNode, previousFlow, previousNode) {
Expand Down
4 changes: 2 additions & 2 deletions src/bp/core/services/dialog/instruction/strategy.ts
Expand Up @@ -57,7 +57,7 @@ export class ActionStrategy implements InstructionStrategy {
}

const outputType = chunks[1]
let args
let args: object = {}

if (params.length > 0) {
try {
Expand Down Expand Up @@ -145,7 +145,7 @@ export class TransitionStrategy implements InstructionStrategy {
const runner = new VmRunner()
const code = `
try {
return ${instruction.fn}
return ${instruction.fn};
} catch (err) {
if (err instanceof TypeError) {
console.log(err)
Expand Down
22 changes: 8 additions & 14 deletions src/bp/core/services/dialog/skill/service.ts
Expand Up @@ -25,28 +25,22 @@ export class SkillService {
}

private parseActionQuery(nodes) {
const strNodes: string[] = []

if (nodes && nodes.length > 0) {
_.forEach(nodes, node => {
strNodes.push(this.actionToString(node))
})
if (typeof nodes === 'undefined') {
return undefined
}

return strNodes
return (nodes && nodes.length && nodes.map(this.actionToString)) || []
}

private actionToString(action): string {
let finalNode: string = ''

if (action.type === NodeActionType.RunAction) {
finalNode = action.args ? `${action.name} ${JSON.stringify(action.args)}` : action.name
} else if (action.type == NodeActionType.RenderText) {
} else if (action.type === NodeActionType.RenderText) {
finalNode = `say #builtin_text ${action.name}`
} else if (action.type == NodeActionType.RenderElement) {
finalNode = _.isString(action.args)
? `say ${action.name} ${action.args}`
: `say ${action.name} ${JSON.stringify(action.args)}`
} else if (action.type === NodeActionType.RenderElement) {
const args = action.args || {}
finalNode = _.isString(args) ? `say ${action.name} ${args}` : `say ${action.name} ${JSON.stringify(args)}`
}

return finalNode
Expand All @@ -56,7 +50,7 @@ export class SkillService {
const defaultNode: FlowNode = {
name: '',
onEnter: [],
onReceive: [],
onReceive: undefined,
next: []
}

Expand Down

0 comments on commit 6a01aff

Please sign in to comment.