UNPKG

@convo-lang/convo-lang

Version:
48 lines 1.61 kB
import { joinPaths } from "@iyio/common"; import { vfs } from "@iyio/vfs"; import { ConvoHashCacheBase } from "./ConvoHashCacheBase.js"; import { commonConvoCacheTypes } from "./convo-lib.js"; import { convoProjectConfig } from "./convo.deps.js"; export const defaultVfsConvoCacheDir = '/cache/conversations'; /** * Caches conversation using the virtual file system */ export class ConvoVfsCache extends ConvoHashCacheBase { constructor({ cacheDir, logErrors = true, } = {}) { super(commonConvoCacheTypes.vfs); if (!cacheDir) { const projectPath = convoProjectConfig().path; if (projectPath) { cacheDir = joinPaths(projectPath, '.convo-cache'); } else { cacheDir = defaultVfsConvoCacheDir; } } this.cacheDir = cacheDir; this.logErrors = logErrors; } getHashPath(hash) { if (hash.length >= 3) { return joinPaths(this.cacheDir, hash.substring(0, 3), hash + '.json'); } else { return joinPaths(this.cacheDir, hash + '.json'); } } async getMessagesByKey(key) { try { return await vfs().readObjectAsync(this.getHashPath(key)); } catch (ex) { if (this.logErrors) { console.error('Read cached messages attempt failed', ex); } return null; } } async cacheMessagesByKey(key, messages) { await vfs().writeObjectAsync(this.getHashPath(key), messages); } } //# sourceMappingURL=ConvoVfsCache.js.map