diff --git a/bridges/nodejs/src/sdk/memory.ts b/bridges/nodejs/src/sdk/memory.ts index c707d9fe..86f026a3 100644 --- a/bridges/nodejs/src/sdk/memory.ts +++ b/bridges/nodejs/src/sdk/memory.ts @@ -3,70 +3,37 @@ import fs from 'node:fs' import { SKILL_PATH } from '@bridge/constants' -export class Memory { +interface MemoryOptions { + name: string + defaultMemory: T +} + +export class Memory { private readonly memoryPath: string private readonly name: string + private readonly defaultMemory: T + + constructor(options: MemoryOptions) { + const { name, defaultMemory } = options - constructor(name: string) { - this.memoryPath = path.join(SKILL_PATH, 'memory', `${name}.json`) this.name = name + this.defaultMemory = defaultMemory + this.memoryPath = path.join(SKILL_PATH, 'memory', `${options.name}.json`) } /** - * Get the memory - */ - public async getMemory(): Promise> { - return this.read() - } - - /** - * Get a value from the memory - * @param key The key to get - * @example get('key') // { name: 'Leon' } - */ - public async get(key: string): Promise { - 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(key: string, value: T): Promise { - 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 { - const memory = await this.read() - delete memory[key] - - await this.write(memory) - } - - /** - * Clear the memory + * Clear the memory and set it to the default memory value * @example clear() */ public async clear(): Promise { - await this.write({}) + await this.write(this.defaultMemory) } /** * Read the memory + * @example read() */ - private async read(): Promise> { + public async read(): Promise { try { if (!fs.existsSync(this.memoryPath)) { await this.clear() @@ -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): Promise { + public async write(memory: T): Promise { 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 } } } diff --git a/bridges/nodejs/src/sdk/network.ts b/bridges/nodejs/src/sdk/network.ts index 6f0a789b..855ddc83 100644 --- a/bridges/nodejs/src/sdk/network.ts +++ b/bridges/nodejs/src/sdk/network.ts @@ -112,10 +112,10 @@ export class Network { */ public async isNetworkAvailable(): Promise { try { - await dns.promises.resolve('apple.com') + await dns.promises.resolve('getleon.ai') return true - } catch (e) { + } catch { return false } } diff --git a/skills/leon/age/src/actions/run.ts b/skills/leon/age/src/actions/run.ts index 1980efc6..360327e1 100644 --- a/skills/leon/age/src/actions/run.ts +++ b/skills/leon/age/src/actions/run.ts @@ -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: '
' - }, - { - name: 'footer', - component: '