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

refactor(server): finish NLU typing

This commit is contained in:
louistiti 2023-04-02 18:42:00 +08:00
parent 37e5ca0e87
commit bf4806c987
No known key found for this signature in database
GPG Key ID: 92CD6A2E497E1669

View File

@ -5,6 +5,7 @@ import kill from 'tree-kill'
import type { Language, ShortLanguageCode } from '@/types' import type { Language, ShortLanguageCode } from '@/types'
import type { NLPAction, NLPDomain, NLPJSProcessResult, NLPSkill, NLPUtterance, NLUResult } from '@/core/nlp/types' import type { NLPAction, NLPDomain, NLPJSProcessResult, NLPSkill, NLPUtterance, NLUResult } from '@/core/nlp/types'
import type { BrainProcessResult } from '@/core/brain/types'
import { langs } from '@@/core/langs.json' import { langs } from '@@/core/langs.json'
import { TCP_SERVER_BIN_PATH } from '@/constants' import { TCP_SERVER_BIN_PATH } from '@/constants'
import { TCP_CLIENT, BRAIN, SOCKET_SERVER, MODEL_LOADER, NER } from '@/core' import { TCP_CLIENT, BRAIN, SOCKET_SERVER, MODEL_LOADER, NER } from '@/core'
@ -14,6 +15,15 @@ import { ActionLoop } from '@/core/nlp/nlu/action-loop'
import { SlotFilling } from '@/core/nlp/nlu/slot-filling' import { SlotFilling } from '@/core/nlp/nlu/slot-filling'
import Conversation, { DEFAULT_ACTIVE_CONTEXT } from '@/core/nlp/conversation' import Conversation, { DEFAULT_ACTIVE_CONTEXT } from '@/core/nlp/conversation'
type NLUProcessResult = Promise<
Partial<
BrainProcessResult & {
processingTime: number
nluProcessingTime: number
}
> | null
>
export const DEFAULT_NLU_RESULT = { export const DEFAULT_NLU_RESULT = {
utterance: '', utterance: '',
currentEntities: [], currentEntities: [],
@ -51,7 +61,7 @@ export default class NLU {
private switchLanguage( private switchLanguage(
utterance: NLPUtterance, utterance: NLPUtterance,
locale: ShortLanguageCode locale: ShortLanguageCode
): unknown { ): void {
const connectedHandler = async (): Promise<void> => { const connectedHandler = async (): Promise<void> => {
await this.process(utterance) await this.process(utterance)
} }
@ -69,8 +79,6 @@ export default class NLU {
TCP_CLIENT.ee.removeListener('connected', connectedHandler) TCP_CLIENT.ee.removeListener('connected', connectedHandler)
TCP_CLIENT.ee.on('connected', connectedHandler) TCP_CLIENT.ee.on('connected', connectedHandler)
}) })
return {}
} }
/** /**
@ -78,7 +86,9 @@ export default class NLU {
* pick-up the right classification * pick-up the right classification
* and extract entities * and extract entities
*/ */
public process(utterance: NLPUtterance) { public process(
utterance: NLPUtterance
): NLUProcessResult {
const processingTimeStart = Date.now() const processingTimeStart = Date.now()
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
@ -167,7 +177,8 @@ export default class NLU {
// Trigger language switching // Trigger language switching
if (BRAIN.lang !== locale) { if (BRAIN.lang !== locale) {
return resolve(this.switchLanguage(utterance, locale)) this.switchLanguage(utterance, locale)
return resolve(null)
} }
// this.sendLog() // this.sendLog()
@ -190,13 +201,7 @@ export default class NLU {
const msg = 'Intent not found' const msg = 'Intent not found'
LogHelper.warning(msg) LogHelper.warning(msg)
const processingTimeEnd = Date.now() return resolve(null)
const processingTime = processingTimeEnd - processingTimeStart
return resolve({
processingTime,
message: msg
})
} }
this.nluResult = fallback this.nluResult = fallback
@ -249,6 +254,7 @@ export default class NLU {
this.conversation.cleanActiveContext() this.conversation.cleanActiveContext()
} }
this.conversation.activeContext = { this.conversation.activeContext = {
...DEFAULT_ACTIVE_CONTEXT,
lang: BRAIN.lang, lang: BRAIN.lang,
slots: {}, slots: {},
isInActionLoop: false, isInActionLoop: false,