Skip to content

Commit

Permalink
Merge branch 'nodejs-bridge-memory' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed May 14, 2023
2 parents e32bf37 + c163f59 commit 3ed19bd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 92 deletions.
73 changes: 22 additions & 51 deletions bridges/nodejs/src/sdk/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,37 @@ 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(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()
constructor(options: MemoryOptions<T>) {
const { name, defaultMemory } = options

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.name = name
this.defaultMemory = defaultMemory
this.memoryPath = path.join(SKILL_PATH, 'memory', `${options.name}.json`)
}

/**
* Clear the memory
* Clear the memory and set it to the default memory value
* @example clear()
*/
public async clear(): Promise<void> {
await this.write({})
await this.write(this.defaultMemory)
}

/**
* Read the memory
* @example read()
*/
private async read(): Promise<Record<string, unknown>> {
public async read(): Promise<T> {
try {
if (!fs.existsSync(this.memoryPath)) {
await this.clear()
Expand All @@ -75,22 +42,26 @@ 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
}
}

/**
* Write the memory
* @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 {
await fs.promises.writeFile(
this.memoryPath,
JSON.stringify(memory, null, 2)
)

return memory
} catch (e) {
console.error(`Error while writing memory for ${this.name}:`, e)
throw e
}
}
}
4 changes: 2 additions & 2 deletions bridges/nodejs/src/sdk/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
49 changes: 10 additions & 39 deletions skills/leon/age/src/actions/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -51,40 +36,26 @@ 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')

let posts = await postsMemory.read()
console.log('posts', posts)
console.log('metaData', metaData)
console.log('websiteLayout', websiteLayout)

await postsMemory.set('posts', [
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 foundPost = posts2.find((post) => post.id === 2)
const foundPost = posts.find((post) => post.id === 2)

console.log('foundPost', foundPost)

console.log('keyBy', _.keyBy(posts2, 'id'))
console.log('keyBy', _.keyBy(posts, 'id'))

///

Expand Down

0 comments on commit 3ed19bd

Please sign in to comment.