diff --git a/core/skills-endpoints.json b/core/skills-endpoints.json index 957e099c..8ffb0794 100644 --- a/core/skills-endpoints.json +++ b/core/skills-endpoints.json @@ -7,7 +7,12 @@ }, { "method": "GET", - "route": "/api/action/games/guess_the_number/pick_up", + "route": "/api/action/games/guess_the_number/setup", + "params": [] + }, + { + "method": "GET", + "route": "/api/action/games/guess_the_number/guess", "params": [] }, { diff --git a/server/src/core/brain.js b/server/src/core/brain.js index 2ea24910..a4184c86 100644 --- a/server/src/core/brain.js +++ b/server/src/core/brain.js @@ -158,6 +158,7 @@ class Brain { executionTime }) } else { + console.log('brain obj', obj) const { nluDataFilePath, classification: { action: actionName } } = obj const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8')) const { type: actionType } = actions[actionName] diff --git a/server/src/core/conversation.js b/server/src/core/conversation.js index 6f12d8cf..695fc52c 100644 --- a/server/src/core/conversation.js +++ b/server/src/core/conversation.js @@ -58,25 +58,24 @@ class Conversation { entities } = contextObj const slotKeys = Object.keys(slots) + const [skillName] = intent.split('.') + const newContextName = `${domain}.${skillName}` // If slots are required to trigger next actions, then go through the context activation if (slotKeys.length > 0) { const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8')) - const actionsKeys = Object.keys(actions) - // Grab output context from the NLU data file - const { output_context: outputContext } = actions[actionName] - // Define next action - const [nextAction] = actionsKeys.filter((key) => actions[key].input_context === outputContext) + // Grab next action from the NLU data file + const { next_action: nextAction } = actions[actionName] /** * If a new context is triggered * then save the current active context to the contexts history */ - if (this._activeContext.name !== outputContext) { + if (this._activeContext.name !== newContextName) { this.pushToPreviousContextsStack() // Activate new context this._activeContext = { - name: outputContext, + name: newContextName, domain, intent, currentEntities: [], @@ -88,7 +87,7 @@ class Conversation { } log.title('Conversation') - log.info(`New active context: ${outputContext}`) + log.info(`New active context: ${newContextName}`) } this.setSlots(lang, entities, slots) diff --git a/server/src/core/nlu.js b/server/src/core/nlu.js index ce15624f..ca7ac032 100644 --- a/server/src/core/nlu.js +++ b/server/src/core/nlu.js @@ -92,6 +92,7 @@ class Nlu { * TODO: split this method into several methods */ process (utterance, opts) { + console.log('this.conv.activeContext', this.conv.activeContext) const processingTimeStart = Date.now() return new Promise(async (resolve, reject) => { @@ -310,10 +311,20 @@ class Nlu { // Pass context entities to the NLU result object this.nluResultObj.entities = this.conv.activeContext.entities - console.log('this.conv.activeContext', this.conv.activeContext) - try { + // TODO: next action based on next_action const data = await this.brain.execute(this.nluResultObj, { mute: opts.mute }) + + if (this.conv.activeContext.name === 'setup_game') { + // If it is a loop action + // TODO: remove this + data.loop = true + + if (data.loop) { + this.conv.activeContext.nextAction = 'guess' + } + } + const processingTimeEnd = Date.now() const processingTime = processingTimeEnd - processingTimeStart @@ -411,10 +422,6 @@ class Nlu { classification: { domain, skill: skillName, - /** - * Use the next action if it is defined via the skill NLU config - * or take the classified action - */ action: this.conv.activeContext.nextAction, confidence: 1 } @@ -422,12 +429,7 @@ class Nlu { this.conv.cleanActiveContext() - const data = await this.brain.execute(this.nluResultObj, { mute: opts.mute }) - // If it is a loop action - - // TODO: remove this - data.loop = true - return data + return this.brain.execute(this.nluResultObj, { mute: opts.mute }) } this.conv.cleanActiveContext() diff --git a/skills/games/guess_the_number/nlu/en.json b/skills/games/guess_the_number/nlu/en.json index d7b26804..96ad04d5 100644 --- a/skills/games/guess_the_number/nlu/en.json +++ b/skills/games/guess_the_number/nlu/en.json @@ -31,29 +31,17 @@ ] } ], - "output_context": "setup_game" + "next_action": "setup" }, - "pick_up": { + "setup": { "type": "logic", - "input_context": "setup_game", - "answers": { - "bigger": [ - "The number is bigger.", - "Try with a bigger number." - ], - "smaller": [ - "It is smaller.", - "Try a smaller number." - ], - "guessed": [ - "Congrats! The number was %nb% and you guessed in %attempts_nb% attempts. Ready for another round?" - ] - }, - "output_context": "set_restart" + "next_action": "guess" + }, + "guess": { + "type": "logic" }, "decide": { "type": "dialog", - "input_context": "set_restart", "slots": [ { "name": "decision", @@ -71,6 +59,17 @@ "answers": { "ready": [ "Alright, I have set %players_nb% players with the %email_test% email. Go ahead and guess the number between 0 and 100!" + ], + "bigger": [ + "The number is bigger.", + "Try with a bigger number." + ], + "smaller": [ + "It is smaller.", + "Try a smaller number." + ], + "guessed": [ + "Congrats! The number was %nb% and you guessed in %attempts_nb% attempts. Ready for another round?" ] } } diff --git a/skills/games/guess_the_number/src/actions/guess.py b/skills/games/guess_the_number/src/actions/guess.py new file mode 100644 index 00000000..fd94829a --- /dev/null +++ b/skills/games/guess_the_number/src/actions/guess.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- + +import utils + +def guess(params): + """This is a test""" + + entities, slots = params['entities'], params['slots'] + given_nb = -1 + nb_to_guess = 42 # TODO: pick up from DB + + # Find entities + for item in params['entities']: + if item['entity'] == 'number': + given_nb = item['resolution']['value'] + + if given_nb == nb_to_guess: + return utils.output('end', 'guessed', '....CONGRATS....') + if nb_to_guess < given_nb: + # TODO: enable loop + return utils.output('end', 'smaller', utils.translate('smaller') + if nb_to_guess > given_nb: + # TODO: enable loop + return utils.output('end', 'bigger', utils.translate('bigger') diff --git a/skills/games/guess_the_number/src/actions/pick_up.py b/skills/games/guess_the_number/src/actions/setup.py similarity index 90% rename from skills/games/guess_the_number/src/actions/pick_up.py rename to skills/games/guess_the_number/src/actions/setup.py index 527ffc07..35b33693 100644 --- a/skills/games/guess_the_number/src/actions/pick_up.py +++ b/skills/games/guess_the_number/src/actions/setup.py @@ -3,7 +3,7 @@ import utils -def pick_up(params): +def setup(params): """This is a test""" entities, slots = params['entities'], params['slots'] @@ -11,6 +11,8 @@ def pick_up(params): counter = 0 nb = 42 + # TODO: save these values in DB + # if "not init" phase: check nb + increment counter # TODO: "loop" option to return to the core