From e42655a5ffd2796d6f394d2c9a738fddc9721241 Mon Sep 17 00:00:00 2001 From: louistiti Date: Thu, 4 May 2023 22:34:29 +0800 Subject: [PATCH] refactor(server): remove skill answer type complexity --- server/src/core/brain/brain.ts | 73 +++++++++++++--------------------- server/src/core/brain/types.ts | 5 --- 2 files changed, 28 insertions(+), 50 deletions(-) diff --git a/server/src/core/brain/brain.ts b/server/src/core/brain/brain.ts index 44a11a1f..4cdee1b0 100644 --- a/server/src/core/brain/brain.ts +++ b/server/src/core/brain/brain.ts @@ -15,11 +15,7 @@ import type { IntentObject, SkillResult } from '@/core/brain/types' -import { - SkillActionTypes, - SkillBridges, - SkillOutputTypes -} from '@/core/brain/types' +import { SkillActionTypes, SkillBridges } from '@/core/brain/types' import { langs } from '@@/core/langs.json' import { HAS_TTS, @@ -199,18 +195,15 @@ export default class Brain { const obj = JSON.parse(data.toString()) if (typeof obj === 'object') { - if (obj.output.type === SkillOutputTypes.Intermediate) { - LogHelper.title(`${this.skillFriendlyName} skill`) - LogHelper.info(data.toString()) + LogHelper.title(`${this.skillFriendlyName} skill (on data)`) + LogHelper.info(data.toString()) - const speech = obj.output.speech.toString() - if (!this.isMuted) { - this.talk(speech) - } - this.speeches.push(speech) - } else { - this.skillOutput = data.toString() + const speech = obj.output.speech.toString() + if (!this.isMuted) { + this.talk(speech) } + this.speeches.push(speech) + this.skillOutput = data.toString() return Promise.resolve(null) } else { @@ -388,7 +381,7 @@ export default class Brain { // Catch the end of the skill execution this.skillProcess?.stdout.on('end', () => { - LogHelper.title(`${this.skillFriendlyName} skill`) + LogHelper.title(`${this.skillFriendlyName} skill (on end)`) LogHelper.info(this.skillOutput) let skillResult: SkillResult | undefined = undefined @@ -398,36 +391,26 @@ export default class Brain { try { skillResult = JSON.parse(this.skillOutput) - if (skillResult?.output.speech) { - skillResult.output.speech = - skillResult.output.speech.toString() - if (!this.isMuted) { - this.talk(skillResult.output.speech, true) - } - speeches.push(skillResult.output.speech) + // Synchronize the downloaded content if enabled + if ( + skillResult && + skillResult.output.options['synchronization'] && + skillResult.output.options['synchronization'].enabled && + skillResult.output.options['synchronization'].enabled === true + ) { + const sync = new Synchronizer( + this, + nluResult.classification, + skillResult.output.options['synchronization'] + ) - // Synchronize the downloaded content if enabled - if ( - skillResult.output.type === SkillOutputTypes.End && - skillResult.output.options['synchronization'] && - skillResult.output.options['synchronization'].enabled && - skillResult.output.options['synchronization'].enabled === - true - ) { - const sync = new Synchronizer( - this, - nluResult.classification, - skillResult.output.options['synchronization'] - ) - - // When the synchronization is finished - sync.synchronize((speech: string) => { - if (!this.isMuted) { - this.talk(speech) - } - speeches.push(speech) - }) - } + // When the synchronization is finished + sync.synchronize((speech: string) => { + if (!this.isMuted) { + this.talk(speech) + } + speeches.push(speech) + }) } } catch (e) { LogHelper.title(`${this.skillFriendlyName} skill`) diff --git a/server/src/core/brain/types.ts b/server/src/core/brain/types.ts index 4bf0ced1..9aa96382 100644 --- a/server/src/core/brain/types.ts +++ b/server/src/core/brain/types.ts @@ -28,7 +28,6 @@ export interface SkillResult { entities: NEREntity[] slots: NLUSlots output: { - type: SkillOutputTypes codes: string[] speech: string core: SkillCoreData | undefined @@ -41,10 +40,6 @@ export enum SkillBridges { Python = 'python', NodeJS = 'nodejs' } -export enum SkillOutputTypes { - Intermediate = 'inter', - End = 'end' -} export enum SkillActionTypes { Logic = 'logic', Dialog = 'dialog'