1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-18 14:21:32 +03:00

Merge branch 'nodejs-bridge-improvements' into nodejs-bridge-memory

This commit is contained in:
louistiti 2023-05-14 08:52:03 +08:00
commit 6a01e1e743
No known key found for this signature in database
GPG Key ID: 92CD6A2E497E1669
3 changed files with 28 additions and 88 deletions

View File

@ -3,56 +3,22 @@ import fs from 'node:fs'
import { SKILL_PATH } from '@bridge/constants'
export class Memory {
interface MemoryOptions<T> {
name: string
defaultMemory: T
}
export class Memory<T> {
private readonly memoryPath: string
private readonly name: string
private readonly defaultMemory: T
constructor(options: MemoryOptions<T>) {
const { name, defaultMemory } = options
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)
this.defaultMemory = defaultMemory
this.memoryPath = path.join(SKILL_PATH, 'memory', `${options.name}.json`)
}
/**
@ -60,13 +26,13 @@ export class Memory {
* @example clear()
*/
public async clear(): Promise<void> {
await this.write({})
await this.write(this.defaultMemory)
}
/**
* Read the memory
*/
private async read(): Promise<Record<string, unknown>> {
public async read(): Promise<T> {
try {
if (!fs.existsSync(this.memoryPath)) {
await this.clear()
@ -75,7 +41,7 @@ export class Memory {
return JSON.parse(await fs.promises.readFile(this.memoryPath, 'utf-8'))
} catch (e) {
console.error(`Error while reading memory for ${this.name}:`, e)
return {}
throw e
}
}
@ -83,7 +49,7 @@ export class Memory {
* Write the memory
* @param memory The memory to write
*/
private async write(memory: Record<string, unknown>): Promise<void> {
public async write(memory: T): Promise<void> {
try {
await fs.promises.writeFile(
this.memoryPath,
@ -91,6 +57,7 @@ export class Memory {
)
} catch (e) {
console.error(`Error while writing memory for ${this.name}:`, e)
throw e
}
}
}

View File

@ -112,10 +112,10 @@ export class Network {
*/
public async isNetworkAvailable(): Promise<boolean> {
try {
await dns.promises.resolve('apple.com')
await dns.promises.resolve('getleon.ai')
return true
} catch (e) {
} catch {
return false
}
}

View File

@ -17,23 +17,8 @@ interface Post {
}
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', [
const postsMemory = new Memory<Post[]>({ name: 'posts', defaultMemory: [] })
await postsMemory.write([
{
id: 0,
title: 'Hello world',
@ -51,34 +36,22 @@ export const run: ActionFunction = async function () {
}
}
])
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')
const posts = await postsMemory.read()
console.log('posts', posts)
console.log('metaData', metaData)
console.log('websiteLayout', websiteLayout)
await postsMemory.set('posts', [
await postsMemory.write([
...posts,
{
id: 2,
title: 'Hello world 3',
content: 'This is a test post 3'
content: 'This is a test post 3',
author: {
name: 'Louis'
}
}
])
const posts2 = await postsMemory.get<Post[]>('posts')
const posts2 = await postsMemory.read()
const foundPost = posts2.find((post) => post.id === 2)