UNPKG

@convo-lang/convo-lang

Version:
40 lines 1.32 kB
import { joinPaths } from "@iyio/common"; import { vfs } from "@iyio/vfs"; import { ConvoHashCacheBase } from "./ConvoHashCacheBase"; import { commonConvoCacheTypes } from "./convo-lib"; export const defaultVfsConvoCacheDir = '/cache/conversations'; /** * Caches conversation using the virtual file system */ export class ConvoVfsCache extends ConvoHashCacheBase { cacheDir; logErrors; constructor({ cacheDir = defaultVfsConvoCacheDir, logErrors = true, } = {}) { super(commonConvoCacheTypes.vfs); 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