mirror of
https://github.com/leon-ai/leon.git
synced 2024-12-20 07:11:40 +03:00
Merge branch 'nodejs-bridge-memory' into develop
This commit is contained in:
commit
3ed19bd6d0
@ -3,70 +3,37 @@ import fs from 'node:fs'
|
|||||||
|
|
||||||
import { SKILL_PATH } from '@bridge/constants'
|
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 memoryPath: string
|
||||||
private readonly name: 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
|
this.name = name
|
||||||
|
this.defaultMemory = defaultMemory
|
||||||
|
this.memoryPath = path.join(SKILL_PATH, 'memory', `${options.name}.json`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the memory
|
* Clear the memory and set it to the default memory value
|
||||||
*/
|
|
||||||
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()
|
* @example clear()
|
||||||
*/
|
*/
|
||||||
public async clear(): Promise<void> {
|
public async clear(): Promise<void> {
|
||||||
await this.write({})
|
await this.write(this.defaultMemory)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the memory
|
* Read the memory
|
||||||
|
* @example read()
|
||||||
*/
|
*/
|
||||||
private async read(): Promise<Record<string, unknown>> {
|
public async read(): Promise<T> {
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(this.memoryPath)) {
|
if (!fs.existsSync(this.memoryPath)) {
|
||||||
await this.clear()
|
await this.clear()
|
||||||
@ -75,22 +42,26 @@ export class Memory {
|
|||||||
return JSON.parse(await fs.promises.readFile(this.memoryPath, 'utf-8'))
|
return JSON.parse(await fs.promises.readFile(this.memoryPath, 'utf-8'))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`Error while reading memory for ${this.name}:`, e)
|
console.error(`Error while reading memory for ${this.name}:`, e)
|
||||||
return {}
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the memory
|
* Write the memory
|
||||||
* @param memory The memory to write
|
* @param memory The memory to write
|
||||||
|
* @example write({ foo: 'bar' }) // { foo: 'bar' }
|
||||||
*/
|
*/
|
||||||
private async write(memory: Record<string, unknown>): Promise<void> {
|
public async write(memory: T): Promise<T> {
|
||||||
try {
|
try {
|
||||||
await fs.promises.writeFile(
|
await fs.promises.writeFile(
|
||||||
this.memoryPath,
|
this.memoryPath,
|
||||||
JSON.stringify(memory, null, 2)
|
JSON.stringify(memory, null, 2)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return memory
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`Error while writing memory for ${this.name}:`, e)
|
console.error(`Error while writing memory for ${this.name}:`, e)
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,10 @@ export class Network {
|
|||||||
*/
|
*/
|
||||||
public async isNetworkAvailable(): Promise<boolean> {
|
public async isNetworkAvailable(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
await dns.promises.resolve('apple.com')
|
await dns.promises.resolve('getleon.ai')
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (e) {
|
} catch {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,23 +17,8 @@ interface Post {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const run: ActionFunction = async function () {
|
export const run: ActionFunction = async function () {
|
||||||
const postsMemory = new Memory('posts')
|
const postsMemory = new Memory<Post[]>({ name: 'posts', defaultMemory: [] })
|
||||||
const websiteLayoutMemory = new Memory('websiteLayout')
|
await postsMemory.write([
|
||||||
|
|
||||||
await postsMemory.delete('1')
|
|
||||||
|
|
||||||
await websiteLayoutMemory.set('webSiteLayout', [
|
|
||||||
{
|
|
||||||
name: 'header',
|
|
||||||
component: '<Header>'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'footer',
|
|
||||||
component: '<Footer>'
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
await postsMemory.set('posts', [
|
|
||||||
{
|
{
|
||||||
id: 0,
|
id: 0,
|
||||||
title: 'Hello world',
|
title: 'Hello world',
|
||||||
@ -51,40 +36,26 @@ export const run: ActionFunction = async function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
let posts = await postsMemory.read()
|
||||||
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('posts', posts)
|
||||||
console.log('metaData', metaData)
|
|
||||||
console.log('websiteLayout', websiteLayout)
|
|
||||||
|
|
||||||
await postsMemory.set('posts', [
|
posts = await postsMemory.write([
|
||||||
...posts,
|
...posts,
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
title: 'Hello world 3',
|
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 foundPost = posts.find((post) => post.id === 2)
|
||||||
|
|
||||||
const foundPost = posts2.find((post) => post.id === 2)
|
|
||||||
|
|
||||||
console.log('foundPost', foundPost)
|
console.log('foundPost', foundPost)
|
||||||
|
|
||||||
console.log('keyBy', _.keyBy(posts2, 'id'))
|
console.log('keyBy', _.keyBy(posts, 'id'))
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user