1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-24 04:31:31 +03:00

fix(bridge/nodejs): typings improvements

This commit is contained in:
Divlo 2023-05-05 20:00:51 +02:00
parent b65cfb70d6
commit 1625aaf3cd
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
6 changed files with 52 additions and 30 deletions

View File

@ -1,14 +1,18 @@
import fs from 'node:fs'
import path from 'node:path'
import type { SkillConfigSchema } from '@/schemas/skill-schemas'
import type { IntentObject } from '@sdk/leon'
const {
argv: [, , INTENT_OBJ_FILE_PATH]
} = process
export const INTENT_OBJECT = JSON.parse(
export const INTENT_OBJECT: IntentObject = JSON.parse(
fs.readFileSync(INTENT_OBJ_FILE_PATH as string, 'utf8')
)
export const SKILL_CONFIG = JSON.parse(
export const SKILL_CONFIG: SkillConfigSchema = JSON.parse(
fs.readFileSync(
path.join(
process.cwd(),

View File

@ -1,6 +1,7 @@
import path from 'node:path'
import { INTENT_OBJECT } from '@bridge/constants'
import type { ActionFunction, ActionParams } from '@sdk/leon'
;(async (): Promise<void> => {
const {
domain,
@ -8,25 +9,25 @@ import { INTENT_OBJECT } from '@bridge/constants'
action,
lang,
utterance,
current_entities: currentEntities,
current_entities,
entities,
current_resolvers: currentResolvers,
current_resolvers,
resolvers,
slots
} = INTENT_OBJECT
const params = {
const params: ActionParams = {
lang,
utterance,
currentEntities,
current_entities,
entities,
currentResolvers,
current_resolvers,
resolvers,
slots
}
try {
const { [action]: actionFunction } = await import(
const actionModule = await import(
path.join(
process.cwd(),
'skills',
@ -37,6 +38,7 @@ import { INTENT_OBJECT } from '@bridge/constants'
`${action}.ts`
)
)
const actionFunction: ActionFunction = actionModule[action]
await actionFunction(params)
} catch (e) {

View File

@ -4,11 +4,7 @@ import {
SKILL_SRC_CONFIG
} from '@bridge/constants'
interface IntentObject {
id: string
domain: string
skill: string
action: string
export interface ActionParams {
lang: string
utterance: string
current_entities: unknown[] // TODO
@ -17,32 +13,44 @@ interface IntentObject {
resolvers: unknown[] // TODO
slots: Record<string, unknown>[] // TODO
}
export type ActionFunction = (params: ActionParams) => Promise<void>
export interface IntentObject extends ActionParams {
id: string
domain: string
skill: string
action: string
}
interface AnswerOutput extends IntentObject {
output: {
code: string
codes: string
speech: string
core: AnswerCoreData
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: Record<string, any>
core?: AnswerCoreData
options: Record<string, string>
}
}
interface AnswerCoreData {
restart?: boolean
isInActionLoop?: boolean
showNextActionSuggestions?: boolean
showSuggestions?: boolean
}
interface TextAnswer {
key: string
data?: AnswerData
core?: AnswerCoreData
}
interface WidgetAnswer {
// TODO
key: 'widget'
data?: AnswerData
core?: AnswerCoreData
}
type AnswerData = Record<string, string | number> | null
type AnswerInput = TextAnswer | WidgetAnswer
@ -59,8 +67,7 @@ class Leon {
* Get source configuration
* @example getSRCConfig() // { credentials: { apiKey: 'abc' } }
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public getSRCConfig(key?: string): Record<string, any> {
public getSRCConfig<T extends Record<string, string>>(key?: string): T {
try {
if (key) {
return SKILL_SRC_CONFIG[key]
@ -70,7 +77,7 @@ class Leon {
} catch (e) {
console.error('Error while getting source configuration:', e)
return {}
return {} as T
}
}
@ -86,15 +93,15 @@ class Leon {
): string | null {
try {
// In case the answer key is a raw answer
if (!SKILL_CONFIG.answers[answerKey]) {
if (SKILL_CONFIG.answers == null || !SKILL_CONFIG.answers[answerKey]) {
return answerKey
}
const answers = SKILL_CONFIG.answers[answerKey]
let answer
const answers = SKILL_CONFIG.answers[answerKey] ?? ''
let answer: string
if (Array.isArray(answers)) {
answer = answers[Math.floor(Math.random() * answers.length)]
answer = answers[Math.floor(Math.random() * answers.length)] ?? ''
} else {
answer = answers
}
@ -134,7 +141,7 @@ class Leon {
...INTENT_OBJECT,
output: {
codes: answerInput.key,
speech: this.setAnswerData(answerInput.key, answerInput.data),
speech: this.setAnswerData(answerInput.key, answerInput.data) ?? '',
core: answerInput.core,
options: this.getSRCConfig('options')
}

View File

@ -1,18 +1,23 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist/bin",
"baseUrl": ".",
"paths": {
"@bridge/*": ["./src/*"]
}
"@@/*": ["../../*"],
"@/*": ["../../server/src/*"],
"@server/schemas/*": ["../../server/src/schemas/*"],
"@bridge/*": ["./src/*"],
"@sdk/*": ["./src/sdk/*"]
},
"exactOptionalPropertyTypes": false,
"declaration": true
},
"ts-node": {
"swc": true,
"require": ["tsconfig-paths/register"],
"files": true
},
"include": ["src/**/*"],
"include": ["src/**/*", "../../server/src/schemas/**/*"],
"exclude": ["node_modules", "dist"]
}

View File

@ -141,6 +141,9 @@ BUILD_TARGETS.set('tcp-server', {
const distMainFilePath = path.join(
NODEJS_BRIDGE_DIST_PATH,
'bin',
'bridges',
'nodejs',
'src',
'main.js'
)
const distRenamedMainFilePath = path.join(

View File

@ -1,7 +1,8 @@
import { leon } from '@sdk/leon'
import type { ActionFunction } from '@sdk/leon'
import { Button } from '@sdk/aurora/button'
export async function run(): Promise<void> {
export const run: ActionFunction = async () => {
await leon.answer({ key: 'default' })
await leon.answer({