mirror of
https://github.com/leon-ai/leon.git
synced 2024-11-24 12:45:58 +03:00
Merge branch 'nodejs-bridge-memory' into develop
This commit is contained in:
commit
e32bf370db
1
.gitignore
vendored
1
.gitignore
vendored
@ -33,6 +33,7 @@ packages/**/config/config.json
|
||||
skills/**/src/config.json
|
||||
packages/**/data/db/*.json
|
||||
skills/**/memory/*.json
|
||||
skills/**/memory/*.db*
|
||||
core/data/models/*.nlp
|
||||
package.json.backup
|
||||
.python-version
|
||||
|
@ -13,6 +13,10 @@
|
||||
"url": "https://github.com/leon-ai/leon/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "1.4.0"
|
||||
"axios": "1.4.0",
|
||||
"lodash": "4.17.21"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash": "4.14.194"
|
||||
}
|
||||
}
|
||||
|
@ -12,29 +12,18 @@ const {
|
||||
export const INTENT_OBJECT: IntentObject = JSON.parse(
|
||||
fs.readFileSync(INTENT_OBJ_FILE_PATH as string, 'utf8')
|
||||
)
|
||||
export const SKILL_PATH = path.join(
|
||||
process.cwd(),
|
||||
'skills',
|
||||
INTENT_OBJECT.domain,
|
||||
INTENT_OBJECT.skill
|
||||
)
|
||||
export const SKILL_CONFIG: SkillConfigSchema = JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.join(
|
||||
process.cwd(),
|
||||
'skills',
|
||||
INTENT_OBJECT.domain,
|
||||
INTENT_OBJECT.skill,
|
||||
'config',
|
||||
INTENT_OBJECT.lang + '.json'
|
||||
),
|
||||
path.join(SKILL_PATH, 'config', INTENT_OBJECT.lang + '.json'),
|
||||
'utf8'
|
||||
)
|
||||
)
|
||||
export const SKILL_SRC_CONFIG: Record<string, unknown> = JSON.parse(
|
||||
fs.readFileSync(
|
||||
path.join(
|
||||
process.cwd(),
|
||||
'skills',
|
||||
INTENT_OBJECT.domain,
|
||||
INTENT_OBJECT.skill,
|
||||
'src',
|
||||
'config.json'
|
||||
),
|
||||
'utf8'
|
||||
)
|
||||
fs.readFileSync(path.join(SKILL_PATH, 'src', 'config.json'), 'utf8')
|
||||
).configurations
|
||||
|
96
bridges/nodejs/src/sdk/memory.ts
Normal file
96
bridges/nodejs/src/sdk/memory.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
import { SKILL_PATH } from '@bridge/constants'
|
||||
|
||||
export class Memory {
|
||||
private readonly memoryPath: string
|
||||
private readonly name: string
|
||||
|
||||
constructor(name: string) {
|
||||
this.memoryPath = path.join(SKILL_PATH, 'memory', `${name}.json`)
|
||||
this.name = name
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the memory
|
||||
*/
|
||||
public async getMemory(): Promise<Record<string, unknown>> {
|
||||
return this.read()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value from the memory
|
||||
* @param key The key to get
|
||||
* @example get('key') // { name: 'Leon' }
|
||||
*/
|
||||
public async get<T>(key: string): Promise<T> {
|
||||
const memory = await this.read()
|
||||
|
||||
return memory[key] as T
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value in the memory
|
||||
* @param key The key to set
|
||||
* @param value The value to set
|
||||
* @example set('key', { name: 'Leon' })
|
||||
*/
|
||||
public async set<T>(key: string, value: T): Promise<void> {
|
||||
const memory = await this.read()
|
||||
memory[key] = value
|
||||
|
||||
await this.write(memory)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a value from the memory
|
||||
* @param key The key to delete
|
||||
* @example delete('key')
|
||||
*/
|
||||
public async delete(key: string): Promise<void> {
|
||||
const memory = await this.read()
|
||||
delete memory[key]
|
||||
|
||||
await this.write(memory)
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the memory
|
||||
* @example clear()
|
||||
*/
|
||||
public async clear(): Promise<void> {
|
||||
await this.write({})
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the memory
|
||||
*/
|
||||
private async read(): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
if (!fs.existsSync(this.memoryPath)) {
|
||||
await this.clear()
|
||||
}
|
||||
|
||||
return JSON.parse(await fs.promises.readFile(this.memoryPath, 'utf-8'))
|
||||
} catch (e) {
|
||||
console.error(`Error while reading memory for ${this.name}:`, e)
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the memory
|
||||
* @param memory The memory to write
|
||||
*/
|
||||
private async write(memory: Record<string, unknown>): Promise<void> {
|
||||
try {
|
||||
await fs.promises.writeFile(
|
||||
this.memoryPath,
|
||||
JSON.stringify(memory, null, 2)
|
||||
)
|
||||
} catch (e) {
|
||||
console.error(`Error while writing memory for ${this.name}:`, e)
|
||||
}
|
||||
}
|
||||
}
|
1
bridges/nodejs/src/sdk/packages/lodash.ts
Normal file
1
bridges/nodejs/src/sdk/packages/lodash.ts
Normal file
@ -0,0 +1 @@
|
||||
export { default } from 'lodash'
|
@ -70,7 +70,6 @@
|
||||
"docker:check": "docker run --rm --interactive leon-ai/leon npm run check"
|
||||
},
|
||||
"dependencies": {
|
||||
"leon-nodejs-bridge": "file:./bridges/nodejs",
|
||||
"@aws-sdk/client-polly": "3.18.0",
|
||||
"@fastify/static": "6.9.0",
|
||||
"@ffmpeg-installer/ffmpeg": "1.1.0",
|
||||
|
@ -4,6 +4,7 @@ import stream from 'node:stream'
|
||||
import readline from 'node:readline'
|
||||
|
||||
import axios from 'axios'
|
||||
import { command } from 'execa'
|
||||
import prettyBytes from 'pretty-bytes'
|
||||
import prettyMilliseconds from 'pretty-ms'
|
||||
import extractZip from 'extract-zip'
|
||||
@ -11,6 +12,7 @@ import extractZip from 'extract-zip'
|
||||
import {
|
||||
BINARIES_FOLDER_NAME,
|
||||
GITHUB_URL,
|
||||
NODEJS_BRIDGE_ROOT_PATH,
|
||||
NODEJS_BRIDGE_DIST_PATH,
|
||||
PYTHON_BRIDGE_DIST_PATH,
|
||||
TCP_SERVER_DIST_PATH,
|
||||
@ -96,6 +98,23 @@ const setupBinaries = async (key) => {
|
||||
fs.promises.rm(archivePath, { recursive: true, force: true })
|
||||
])
|
||||
|
||||
if (key === 'nodejs-bridge') {
|
||||
try {
|
||||
LogHelper.info('Installing Node.js bridge npm packages...')
|
||||
|
||||
await command(
|
||||
`npm install --package-lock=false --prefix ${NODEJS_BRIDGE_ROOT_PATH}`,
|
||||
{
|
||||
shell: true
|
||||
}
|
||||
)
|
||||
|
||||
LogHelper.success('Node.js bridge npm packages installed')
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to install Node.js bridge npm packages: ${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
LogHelper.info(`Downloading ${name}...`)
|
||||
|
||||
@ -142,8 +161,8 @@ const setupBinaries = async (key) => {
|
||||
|
||||
LogHelper.success(`${name} manifest file created`)
|
||||
LogHelper.success(`${name} ${version} ready`)
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to set up ${name}: ${error}`)
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to set up ${name}: ${e}`)
|
||||
}
|
||||
} else {
|
||||
LogHelper.success(`${name} is already at the latest version (${version})`)
|
||||
|
@ -1,11 +1,93 @@
|
||||
import * as utility from 'utility'
|
||||
import utility from 'utility'
|
||||
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
import { Network } from '@sdk/network'
|
||||
import { Button } from '@sdk/aurora/button'
|
||||
import { Memory } from '@sdk/memory'
|
||||
import _ from '@sdk/packages/lodash'
|
||||
|
||||
interface Post {
|
||||
id: number
|
||||
title: string
|
||||
content: string
|
||||
author: {
|
||||
name: string
|
||||
}
|
||||
}
|
||||
|
||||
export const run: ActionFunction = async function () {
|
||||
const postsMemory = new Memory('posts')
|
||||
const websiteLayoutMemory = new Memory('websiteLayout')
|
||||
|
||||
await postsMemory.delete('1')
|
||||
|
||||
await websiteLayoutMemory.set('webSiteLayout', [
|
||||
{
|
||||
name: 'header',
|
||||
component: '<Header>'
|
||||
},
|
||||
{
|
||||
name: 'footer',
|
||||
component: '<Footer>'
|
||||
}
|
||||
])
|
||||
|
||||
await postsMemory.set('posts', [
|
||||
{
|
||||
id: 0,
|
||||
title: 'Hello world',
|
||||
content: 'This is a test post',
|
||||
author: {
|
||||
name: 'Louis'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
title: 'Hello world 2',
|
||||
content: 'This is a test post 2',
|
||||
author: {
|
||||
name: 'Louis'
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
await postsMemory.set('metaData', {
|
||||
size: 50484,
|
||||
type: 'image/png'
|
||||
})
|
||||
|
||||
const metaData = await postsMemory.get<{ size: number; type: string }>(
|
||||
'metaData'
|
||||
)
|
||||
const posts = await postsMemory.get<Post[]>('posts')
|
||||
const websiteLayout = await websiteLayoutMemory.get<{
|
||||
webSiteLayout: { name: string; component: string }[]
|
||||
}>('webSiteLayout')
|
||||
|
||||
console.log('posts', posts)
|
||||
console.log('metaData', metaData)
|
||||
console.log('websiteLayout', websiteLayout)
|
||||
|
||||
await postsMemory.set('posts', [
|
||||
...posts,
|
||||
{
|
||||
id: 2,
|
||||
title: 'Hello world 3',
|
||||
content: 'This is a test post 3'
|
||||
}
|
||||
])
|
||||
|
||||
const posts2 = await postsMemory.get<Post[]>('posts')
|
||||
|
||||
const foundPost = posts2.find((post) => post.id === 2)
|
||||
|
||||
console.log('foundPost', foundPost)
|
||||
|
||||
console.log('keyBy', _.keyBy(posts2, 'id'))
|
||||
|
||||
///
|
||||
|
||||
await leon.answer({ key: 'default' })
|
||||
|
||||
await leon.answer({ key: utility.md5('test') })
|
||||
|
Loading…
Reference in New Issue
Block a user