andy-stack_vaultkeeper-ai/Services/MemoriesService.ts
Andrew Beal f1e6619923 feat: add memory system for cross-session context retention
Add memories feature allowing AI to retain vault conventions, user preferences, and established workflows across conversation sessions. Include read-only mode option, validation requiring read-before-write, and settings UI with toggle controls.
Adjusted default search and snippet size values in plugin settings.
2026-04-04 20:04:41 +01:00

53 lines
No EOL
1.9 KiB
TypeScript

import { Copy } from "Enums/Copy";
import { Path } from "Enums/Path";
import { Resolve } from "./DependencyService";
import type { FileSystemService } from "./FileSystemService";
import { Services } from "./Services";
import type { WorkSpaceService } from "./WorkSpaceService";
export class MemoriesService {
private readonly maxMemoriesLength: number = 10;
private readonly maxMemoriesLineLength: number = 200;
private readonly fileSystemService: FileSystemService;
private readonly workSpaceService: WorkSpaceService;
constructor() {
this.fileSystemService = Resolve<FileSystemService>(Services.FileSystemService);
this.workSpaceService = Resolve<WorkSpaceService>(Services.WorkSpaceService);
}
public async openMemories() {
if (!await this.fileSystemService.exists(Path.Memories, true)) {
await this.updateMemories(""); // Create memories file if one doesn't exist
}
await this.workSpaceService.openNoteByPath(Path.Memories);
}
public async readMemories(): Promise<string> {
const result = await this.fileSystemService.readFile(Path.Memories, true);
if (result instanceof Error) {
return Copy.MemoriesEmpty;
}
return result;
}
public async updateMemories(newMemories: string): Promise<string|Error> {
if (!this.isValidMemoryLength(newMemories)) {
return Copy.MemoriesMaxLengthError;
}
const result = await this.fileSystemService.writeFile(Path.Memories, newMemories, true, false);
return result instanceof Error ? result : Copy.MemoriesUpdatedSuccess;
}
private isValidMemoryLength(memories: string): boolean {
const lines = memories.split(/\r?\n/);
return lines.length <= this.maxMemoriesLength &&
lines.every(line => line.length <= this.maxMemoriesLineLength);
}
}