1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-23 20:12:08 +03:00

refactor(server): use full query object path as child process arg

This commit is contained in:
Louistiti 2019-03-02 16:41:35 +08:00
parent fb987b5e30
commit 7d5a35c9fc
7 changed files with 98 additions and 98 deletions

View File

@ -15,10 +15,10 @@ import requests
dirname = path.dirname(path.realpath(__file__))
queryid = argv[1]
queryobjectpath = argv[1]
serversrc = 'dist' if environ.get('LEON_NODE_ENV') == 'production' else 'src'
queryobjfile = open(dirname + '/../../server/' + serversrc + '/tmp/' + queryid + '.json', 'r', encoding = 'utf8')
queryobjfile = open(queryobjectpath, 'r', encoding = 'utf8')
queryobj = loads(queryobjfile.read())
queryobjfile.close()

View File

@ -2,7 +2,7 @@
"langs": {
"en-US": {
"short": "en",
"min_confidence": 0.8,
"min_confidence": 0.6,
"fallbacks": [
]
},

View File

@ -0,0 +1 @@
{"lang":"en","package":"leon","module":"randomnumber","query":"Give me a random number","entities":[]}

View File

@ -78,7 +78,7 @@ export default () => new Promise(async (resolve, reject) => {
// Module execution checking
try {
const p = await shell('pipenv run python bridges/python/main.py en leon randomnumber "Give me a random number"')
const p = await shell('pipenv run python bridges/python/main.py scripts/assets/query-object.json')
log.info(p.cmd)
log.success(`${p.stdout}\n`)
} catch (e) {

View File

@ -37,9 +37,9 @@ class Brain {
/**
* Delete query object file
*/
static deleteQueryObjFile (queryId) {
static deleteQueryObjFile (queryObjectPath) {
try {
fs.unlinkSync(`${__dirname}/../tmp/${queryId}.json`)
fs.unlinkSync(queryObjectPath)
} catch (e) {
log.error(`Failed to delete query object file: ${e}`)
}
@ -93,115 +93,114 @@ class Brain {
*/
execute (obj) {
return new Promise((resolve, reject) => {
let queryId = ''
const queryId = `${Date.now()}-${string.random(4)}`
const queryObjectPath = `${__dirname}/../tmp/${queryId}.json`
// Ask to repeat if Leon is not sure about the request
if (obj.classification.confidence < langs[process.env.LEON_LANG].min_confidence) {
this.talk(`${this.wernicke('random_not_sure')}.`)
Brain.deleteQueryObjFile(queryId)
this.socket.emit('is-typing', false)
resolve()
}
} else {
// Ensure the process is empty (to be able to execute other processes outside of Brain)
if (Object.keys(this.process).length === 0) {
/**
* Execute a module in a standalone way (CLI):
*
* 1. Need to be at the root of the project
* 2. PIPENV_PIPFILE=bridges/python/Pipfile pipenv run
* python bridges/python/main.py en leon whoami "Who are you?" "[]"
*/
const queryObj = {
id: queryId,
lang: langs[process.env.LEON_LANG].short,
package: obj.classification.package,
module: obj.classification.module,
query: obj.query,
entities: obj.entities
}
// Ensure the process is empty (to be able to execute other processes outside of Brain)
if (Object.keys(this.process).length === 0) {
/**
* Execute a module in a standalone way (CLI):
*
* 1. Need to be at the root of the project
* 2. PIPENV_PIPFILE=bridges/python/Pipfile pipenv run
* python bridges/python/main.py en leon whoami "Who are you?" "[]"
*/
queryId = `${Date.now()}-${string.random(4)}`
const queryObj = {
id: queryId,
lang: langs[process.env.LEON_LANG].short,
package: obj.classification.package,
module: obj.classification.module,
query: obj.query,
entities: obj.entities
try {
fs.writeFileSync(queryObjectPath, JSON.stringify(queryObj))
this.process = spawn(`pipenv run python bridges/python/main.py ${queryObjectPath}`, { shell: true })
} catch (e) {
log.error(`Failed to save query object: ${e}`)
}
}
try {
fs.writeFileSync(`${__dirname}/../tmp/${queryId}.json`, JSON.stringify(queryObj))
this.process = spawn(`pipenv run python bridges/python/main.py ${queryId}`, { shell: true })
} catch (e) {
log.error(`Failed to save query object: ${e}`)
}
}
const packageName = string.ucfirst(obj.classification.package)
const moduleName = string.ucfirst(obj.classification.module)
let output = ''
const packageName = string.ucfirst(obj.classification.package)
const moduleName = string.ucfirst(obj.classification.module)
let output = ''
// Read output
this.process.stdout.on('data', (data) => {
const obj = JSON.parse(data.toString())
// Read output
this.process.stdout.on('data', (data) => {
const obj = JSON.parse(data.toString())
if (typeof obj === 'object') {
if (obj.output.type === 'inter') {
log.title(`${packageName} package`)
log.info(data.toString())
if (typeof obj === 'object') {
if (obj.output.type === 'inter') {
log.title(`${packageName} package`)
log.info(data.toString())
this.interOutput = obj.output
this.talk(obj.output.speech.toString())
this.interOutput = obj.output
this.talk(obj.output.speech.toString())
} else {
output += data
}
} else {
output += data
/* istanbul ignore next */
reject({ type: 'warning', obj: new Error(`The ${moduleName} module of the ${packageName} package is not well configured. Check the configuration file.`) })
}
} else {
/* istanbul ignore next */
reject({ type: 'warning', obj: new Error(`The ${moduleName} module of the ${packageName} package is not well configured. Check the configuration file.`) })
}
})
})
// Handle error
this.process.stderr.on('data', (data) => {
this.talk(`${this.wernicke('random_package_module_errors', '',
{ '%module_name%': moduleName, '%package_name%': packageName })}!`)
Brain.deleteQueryObjFile(queryId)
this.socket.emit('is-typing', false)
// Handle error
this.process.stderr.on('data', (data) => {
this.talk(`${this.wernicke('random_package_module_errors', '',
{ '%module_name%': moduleName, '%package_name%': packageName })}!`)
Brain.deleteQueryObjFile(queryObjectPath)
this.socket.emit('is-typing', false)
log.title(packageName)
reject({ type: 'error', obj: data })
})
log.title(packageName)
reject({ type: 'error', obj: data })
})
// Catch the end of the module execution
this.process.stdout.on('end', () => {
log.title(`${packageName} package`)
log.info(output)
// Catch the end of the module execution
this.process.stdout.on('end', () => {
log.title(`${packageName} package`)
log.info(output)
this.finalOutput = output
this.finalOutput = output
// Check if there is an output (no module error)
if (this.finalOutput !== '') {
this.finalOutput = JSON.parse(this.finalOutput).output
this.talk(this.finalOutput.speech.toString())
// Check if there is an output (no module error)
if (this.finalOutput !== '') {
this.finalOutput = JSON.parse(this.finalOutput).output
this.talk(this.finalOutput.speech.toString())
/* istanbul ignore next */
// Synchronize the downloaded content if enabled
if (this.finalOutput.type === 'end' && this.finalOutput.options.synchronization && this.finalOutput.options.synchronization.enabled &&
this.finalOutput.options.synchronization.enabled === true) {
const sync = new Synchronizer(
this,
obj.classification,
this.finalOutput.options.synchronization
)
/* istanbul ignore next */
// Synchronize the downloaded content if enabled
if (this.finalOutput.type === 'end' && this.finalOutput.options.synchronization && this.finalOutput.options.synchronization.enabled &&
this.finalOutput.options.synchronization.enabled === true) {
const sync = new Synchronizer(
this,
obj.classification,
this.finalOutput.options.synchronization
)
// When the synchronization is finished
sync.synchronize((speech) => {
this.talk(speech)
})
// When the synchronization is finished
sync.synchronize((speech) => {
this.talk(speech)
})
}
}
}
Brain.deleteQueryObjFile(queryId)
this.socket.emit('is-typing', false)
resolve()
})
Brain.deleteQueryObjFile(queryObjectPath)
this.socket.emit('is-typing', false)
resolve()
})
// Reset the child process
this.process = { }
// Reset the child process
this.process = { }
}
})
}
}

View File

@ -34,11 +34,11 @@
"Sorry, I'm still very young, I didn't got your point"
],
"random_not_sure": [
"Sorry, you may repeat",
"Sorry, you may repeat in an another way",
"Sorry, I'm not sure to understand",
"Sorry, I'm not sure for what you asked, please repeat",
"Sorry, please repeat again",
"Sorry, I didn't correctly clean my ears today! Oh wait, I'm your personal assistant then repeat please"
"Sorry, I'm not sure for what you asked, please repeat with a different way",
"Sorry, please repeat again by formulating differently",
"Sorry, I didn't correctly clean my ears today! Oh wait, I'm your personal assistant then please try again with a new way"
],
"random_not_able": [
"Sorry, I'm not able to answer. I understand what you said, but please repeat in another way",

View File

@ -34,11 +34,11 @@
"Désolé, je suis encore très jeune, je n'ai pas compris votre demande"
],
"random_not_sure": [
"Désolé, vous pouvez répéter",
"Désolé, vous pouvez répéter d'une autre façon",
"Désolé, je ne suis pas sûr de comprendre",
"Désolé, je ne suis pas certain de votre demande, merci de répéter",
"Désolé, merci de répéter à nouveau",
"Désolé, je n'ai pas nettoyé mes oreilles correctement ! Attendez-voir, je suis votre assistant personnel, je vous prie donc de répéter"
"Désolé, je ne suis pas certain de votre demande, merci de répéter d'une manière différente",
"Désolé, merci de répéter à nouveau en formulant différemment",
"Désolé, je n'ai pas nettoyé mes oreilles correctement ! Attendez-voir, je suis votre assistant personnel, je vous prie donc de répéter d'une nouvelle façon"
],
"random_not_able": [
"Désolé, je ne suis pas capable de répondre. J'ai compris ce que vous avez dit, mais je vous prie de répéter d'une autre façon",