From 9b839d3e6616ecc2563bf4b747fc7e3d60162b2b Mon Sep 17 00:00:00 2001 From: louistiti Date: Tue, 2 May 2023 23:53:41 +0800 Subject: [PATCH] feat(bridge/nodejs): first draft --- bridges/nodejs/src/main.ts | 72 ++++++++++++++++++++++++-- bridges/nodejs/tsconfig.json | 19 ++----- core/skills-endpoints.json | 5 ++ server/src/constants.ts | 12 +++-- skills/leon/age/README.md | 0 skills/leon/age/config/en.json | 12 +++++ skills/leon/age/memory/.gitkeep | 0 skills/leon/age/skill.json | 12 +++++ skills/leon/age/src/actions/run.ts | 3 ++ skills/leon/age/src/config.sample.json | 6 +++ skills/leon/age/src/lib/.gitkeep | 0 skills/leon/age/test/.gitkeep | 0 12 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 skills/leon/age/README.md create mode 100644 skills/leon/age/config/en.json create mode 100644 skills/leon/age/memory/.gitkeep create mode 100644 skills/leon/age/skill.json create mode 100644 skills/leon/age/src/actions/run.ts create mode 100644 skills/leon/age/src/config.sample.json create mode 100644 skills/leon/age/src/lib/.gitkeep create mode 100644 skills/leon/age/test/.gitkeep diff --git a/bridges/nodejs/src/main.ts b/bridges/nodejs/src/main.ts index c2b7f186..4a3747aa 100644 --- a/bridges/nodejs/src/main.ts +++ b/bridges/nodejs/src/main.ts @@ -1,5 +1,71 @@ -import { VERSION } from './version' +import fs from 'node:fs' +import path from 'node:path' -console.log('[WIP] Node.js bridge', VERSION) +const { + argv: [, , INTENT_OBJ_FILE_PATH] +} = process -// TODO +;(async (): Promise => { + if (INTENT_OBJ_FILE_PATH) { + const { + domain, + skill, + action, + lang, + utterance, + current_entities: currentEntities, + entities, + current_resolvers: currentResolvers, + resolvers, + slots + } = JSON.parse(await fs.promises.readFile(INTENT_OBJ_FILE_PATH, 'utf8')) + + const params = { + lang, + utterance, + currentEntities, + entities, + currentResolvers, + resolvers, + slots + } + + try { + const { [action]: actionFunction } = await import( + path.join( + process.cwd(), + 'skills', + domain, + skill, + 'src', + 'actions', + `${action}.ts` + ) + ) + + const speech = actionFunction(params) + + console.log( + JSON.stringify({ + domain, + skill, + action, + lang, + utterance, + entities, + slots, + // TODO + output: { + type: 'end', + codes: '', + speech, + core: {}, + options: {} + } + }) + ) + } catch (e) { + console.error('Error while running action:', e) + } + } +})() diff --git a/bridges/nodejs/tsconfig.json b/bridges/nodejs/tsconfig.json index 27f9d35a..61c5397a 100644 --- a/bridges/nodejs/tsconfig.json +++ b/bridges/nodejs/tsconfig.json @@ -1,23 +1,10 @@ { - "extends": "@tsconfig/node16-strictest/tsconfig.json", + "extends": "../../tsconfig.json", "compilerOptions": { "rootDir": "./src", "outDir": "./dist/bin", - "baseUrl": ".", - "paths": { - "@/*": ["./src/*"] - }, - "ignoreDeprecations": "5.0", - "allowJs": true, - "checkJs": false, - "resolveJsonModule": true + "baseUrl": "." }, - "ts-node": { - "swc": true, - "require": ["tsconfig-paths/register"], - "files": true - }, - "files": [], "include": ["src/**/*"], - "exclude": ["dist"] + "exclude": ["node_modules", "dist"] } diff --git a/core/skills-endpoints.json b/core/skills-endpoints.json index 125d2169..459c3b34 100644 --- a/core/skills-endpoints.json +++ b/core/skills-endpoints.json @@ -141,6 +141,11 @@ "route": "/api/action/games/rochambeau/rematch", "params": [] }, + { + "method": "GET", + "route": "/api/action/leon/age/run", + "params": [] + }, { "method": "GET", "route": "/api/action/leon/color/favorite_color", diff --git a/server/src/constants.ts b/server/src/constants.ts index 44c8dcd3..71bd8d9c 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -72,11 +72,13 @@ export const PYTHON_BRIDGE_BIN_PATH = path.join( BINARIES_FOLDER_NAME, PYTHON_BRIDGE_BIN_NAME ) -export const NODEJS_BRIDGE_BIN_PATH = `${process.execPath} ${path.join( - NODEJS_BRIDGE_DIST_PATH, - 'bin', - NODEJS_BRIDGE_BIN_NAME -)}` +export const NODEJS_BRIDGE_BIN_PATH = `${path.join( + process.cwd(), + 'node_modules', + 'ts-node', + 'dist', + 'bin.js' +)} --swc ${path.join(NODEJS_BRIDGE_DIST_PATH, 'bin', NODEJS_BRIDGE_BIN_NAME)}` export const LEON_VERSION = process.env['npm_package_version'] diff --git a/skills/leon/age/README.md b/skills/leon/age/README.md new file mode 100644 index 00000000..e69de29b diff --git a/skills/leon/age/config/en.json b/skills/leon/age/config/en.json new file mode 100644 index 00000000..f57882bb --- /dev/null +++ b/skills/leon/age/config/en.json @@ -0,0 +1,12 @@ +{ + "$schema": "../../../../schemas/skill-schemas/skill-config.json", + "actions": { + "run": { + "type": "logic", + "utterance_samples": ["How old are you?"] + } + }, + "answers": { + "default": ["I'm..."] + } +} diff --git a/skills/leon/age/memory/.gitkeep b/skills/leon/age/memory/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/skills/leon/age/skill.json b/skills/leon/age/skill.json new file mode 100644 index 00000000..72322cb6 --- /dev/null +++ b/skills/leon/age/skill.json @@ -0,0 +1,12 @@ +{ + "$schema": "../../../schemas/skill-schemas/skill.json", + "name": "Age", + "bridge": "nodejs", + "version": "1.0.0", + "description": "Leon tells his age.", + "author": { + "name": "Louis Grenard", + "email": "louis@getleon.ai", + "url": "https://github.com/louistiti" + } +} diff --git a/skills/leon/age/src/actions/run.ts b/skills/leon/age/src/actions/run.ts new file mode 100644 index 00000000..a5b27972 --- /dev/null +++ b/skills/leon/age/src/actions/run.ts @@ -0,0 +1,3 @@ +export function run(): string { + return 'hello' +} diff --git a/skills/leon/age/src/config.sample.json b/skills/leon/age/src/config.sample.json new file mode 100644 index 00000000..9e43d47e --- /dev/null +++ b/skills/leon/age/src/config.sample.json @@ -0,0 +1,6 @@ +{ + "configurations": { + "options": {}, + "credentials": {} + } +} diff --git a/skills/leon/age/src/lib/.gitkeep b/skills/leon/age/src/lib/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/skills/leon/age/test/.gitkeep b/skills/leon/age/test/.gitkeep new file mode 100644 index 00000000..e69de29b