1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-24 04:31:31 +03:00

fix(bridge/nodejs): improve Memory types with external skill memory

This commit is contained in:
Divlo 2023-05-15 22:58:40 +02:00
parent 623039415a
commit 839c7bfc3c
2 changed files with 15 additions and 11 deletions

View File

@ -8,7 +8,7 @@ interface MemoryOptions<T> {
defaultMemory?: T
}
export class Memory<T> {
export class Memory<T = unknown> {
private readonly memoryPath: string | undefined
private readonly name: string
private readonly defaultMemory: T | undefined
@ -55,12 +55,13 @@ export class Memory<T> {
* Read the memory
* @example read()
*/
public async read(): Promise<T | undefined> {
public async read(): Promise<T> {
if (!this.memoryPath) {
throw new Error(
`You cannot read the memory "${this.name}" as it belongs to another skill which haven't written to this memory yet`
)
}
try {
if (!this.memoryPath) {
return
}
if (!fs.existsSync(this.memoryPath)) {
await this.clear()
}

View File

@ -24,14 +24,17 @@ export const run: ActionFunction = async function () {
///
const otherSkillMemory = new Memory<unknown>({
const otherSkillMemory = new Memory({
name: 'productivity:todo_list:db'
})
try {
const todoLists = await otherSkillMemory.read()
console.log('todoLists', todoLists)
} catch {
console.log('todoLists', [])
}
const postsMemory = new Memory<Post[]>({ name: 'posts', defaultMemory: [] })
const todoLists = await otherSkillMemory.read()
console.log('todoLists', todoLists)
await postsMemory.write([
{
id: 0,