mirror of
https://github.com/leon-ai/leon.git
synced 2024-09-11 10:25:40 +03:00
refactor(server): skill domain helper to static class
This commit is contained in:
parent
46c0b579aa
commit
7544534ea4
@ -2,7 +2,7 @@ import fs from 'fs'
|
||||
import { join } from 'path'
|
||||
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
/**
|
||||
* This script delete test DB files if they exist
|
||||
@ -11,7 +11,7 @@ export default () =>
|
||||
new Promise(async (resolve, reject) => {
|
||||
LogHelper.info('Cleaning test DB files...')
|
||||
|
||||
const skillDomains = await SKILL_DOMAIN.getSkillDomains()
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
for (const currentDomain of skillDomains.values()) {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
|
@ -5,7 +5,7 @@ import path from 'path'
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
|
||||
import { langs } from '@@/core/langs.json'
|
||||
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
@ -30,7 +30,7 @@ export default () =>
|
||||
const lang = langs[process.env.LEON_HTTP_API_LANG].short
|
||||
|
||||
try {
|
||||
const skillDomains = await SKILL_DOMAIN.getSkillDomains()
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
const finalObj = {
|
||||
endpoints: []
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
/**
|
||||
* Setup skills configuration
|
||||
@ -12,7 +12,7 @@ export default () =>
|
||||
new Promise(async (resolve, reject) => {
|
||||
LogHelper.info('Setting up skills configuration...')
|
||||
|
||||
const skillDomains = await SKILL_DOMAIN.getSkillDomains()
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
for (const currentDomain of skillDomains.values()) {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
|
@ -4,7 +4,7 @@ import { composeFromPattern } from '@nlpjs/utils'
|
||||
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { StringHelper } from '@/helpers/string-helper'
|
||||
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
/**
|
||||
* Train skills actions
|
||||
@ -14,7 +14,7 @@ export default (lang, nlp) =>
|
||||
LogHelper.title('Skills actions training')
|
||||
|
||||
const supportedActionTypes = ['dialog', 'logic']
|
||||
const skillDomains = await SKILL_DOMAIN.getSkillDomains()
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
for (const [domainName, currentDomain] of skillDomains) {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
@ -34,7 +34,7 @@ export default (lang, nlp) =>
|
||||
)
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
const { actions, variables } = await SKILL_DOMAIN.getSkillConfig(
|
||||
const { actions, variables } = await SkillDomainHelper.getSkillConfig(
|
||||
configFilePath,
|
||||
lang
|
||||
)
|
||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import { composeFromPattern } from '@nlpjs/utils'
|
||||
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
|
||||
/**
|
||||
* Train skills resolvers
|
||||
@ -12,7 +12,7 @@ export default (lang, nlp) =>
|
||||
new Promise(async (resolve) => {
|
||||
LogHelper.title('Skills resolvers training')
|
||||
|
||||
const skillDomains = await SKILL_DOMAIN.getSkillDomains()
|
||||
const skillDomains = await SkillDomainHelper.getSkillDomains()
|
||||
|
||||
skillDomains.forEach((currentDomain) => {
|
||||
const skillKeys = Object.keys(currentDomain.skills)
|
||||
@ -26,7 +26,7 @@ export default (lang, nlp) =>
|
||||
)
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
const { resolvers } = await SKILL_DOMAIN.getSkillConfig(
|
||||
const { resolvers } = await SkillDomainHelper.getSkillConfig(
|
||||
configFilePath,
|
||||
lang
|
||||
)
|
||||
|
@ -7,7 +7,7 @@ import { HAS_TTS } from '@/constants'
|
||||
import { LogHelper } from '@/helpers/log-helper'
|
||||
import { StringHelper } from '@/helpers/string-helper'
|
||||
import { LangHelper } from '@/helpers/lang-helper'
|
||||
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
||||
import { SkillDomainHelper } from '@/helpers/skill-domain-helper'
|
||||
import Synchronizer from '@/core/synchronizer'
|
||||
|
||||
class Brain {
|
||||
@ -236,8 +236,8 @@ class Brain {
|
||||
const domainName = obj.classification.domain
|
||||
const skillName = obj.classification.skill
|
||||
const { name: domainFriendlyName } =
|
||||
SKILL_DOMAIN.getSkillDomainInfo(domainName)
|
||||
const { name: skillFriendlyName } = SKILL_DOMAIN.getSkillInfo(
|
||||
SkillDomainHelper.getSkillDomainInfo(domainName)
|
||||
const { name: skillFriendlyName } = SkillDomainHelper.getSkillInfo(
|
||||
domainName,
|
||||
skillName
|
||||
)
|
||||
@ -411,7 +411,7 @@ class Brain {
|
||||
'config',
|
||||
`${this._lang}.json`
|
||||
)
|
||||
const { actions, entities } = await SKILL_DOMAIN.getSkillConfig(
|
||||
const { actions, entities } = await SkillDomainHelper.getSkillConfig(
|
||||
configFilePath,
|
||||
this._lang
|
||||
)
|
||||
|
@ -23,19 +23,11 @@ interface SkillDomain {
|
||||
|
||||
const DOMAINS_DIR = path.join(process.cwd(), 'skills')
|
||||
|
||||
class SkillDomainHelper {
|
||||
private static instance: SkillDomainHelper
|
||||
|
||||
constructor() {
|
||||
if (SkillDomainHelper.instance == null) {
|
||||
SkillDomainHelper.instance = this
|
||||
}
|
||||
}
|
||||
|
||||
export class SkillDomainHelper {
|
||||
/**
|
||||
* List all skills domains with skills data inside
|
||||
*/
|
||||
public async getSkillDomains() {
|
||||
public static async getSkillDomains() {
|
||||
const skillDomains = new Map<string, SkillDomain>()
|
||||
|
||||
await Promise.all(
|
||||
@ -85,7 +77,7 @@ class SkillDomainHelper {
|
||||
* Get information of a specific domain
|
||||
* @param domain Domain to get info from
|
||||
*/
|
||||
public getSkillDomainInfo(domain: SkillDomain['name']) {
|
||||
public static getSkillDomainInfo(domain: SkillDomain['name']) {
|
||||
return JSON.parse(
|
||||
fs.readFileSync(path.join(DOMAINS_DIR, domain, 'domain.json'), 'utf8')
|
||||
)
|
||||
@ -96,7 +88,10 @@ class SkillDomainHelper {
|
||||
* @param domain Domain where the skill belongs
|
||||
* @param skill Skill to get info from
|
||||
*/
|
||||
public getSkillInfo(domain: SkillDomain['name'], skill: Skill['name']) {
|
||||
public static getSkillInfo(
|
||||
domain: SkillDomain['name'],
|
||||
skill: Skill['name']
|
||||
) {
|
||||
return JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.join(DOMAINS_DIR, domain, skill, 'skill.json'),
|
||||
@ -110,7 +105,10 @@ class SkillDomainHelper {
|
||||
* @param configFilePath Path of the skill config file
|
||||
* @param lang Language short code
|
||||
*/
|
||||
public getSkillConfig(configFilePath: string, lang: ShortLanguageCode) {
|
||||
public static getSkillConfig(
|
||||
configFilePath: string,
|
||||
lang: ShortLanguageCode
|
||||
) {
|
||||
const sharedDataPath = path.join(process.cwd(), 'core/data', lang)
|
||||
const configData = JSON.parse(fs.readFileSync(configFilePath, 'utf8'))
|
||||
const { entities } = configData
|
||||
@ -133,5 +131,3 @@ class SkillDomainHelper {
|
||||
return configData
|
||||
}
|
||||
}
|
||||
|
||||
export const SKILL_DOMAIN = new SkillDomainHelper()
|
Loading…
Reference in New Issue
Block a user