UNPKG

@convo-lang/convo-lang

Version:
44 lines 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConvoVfsCache = exports.defaultVfsConvoCacheDir = void 0; const common_1 = require("@iyio/common"); const vfs_1 = require("@iyio/vfs"); const ConvoHashCacheBase_1 = require("./ConvoHashCacheBase"); const convo_lib_1 = require("./convo-lib"); exports.defaultVfsConvoCacheDir = '/cache/conversations'; /** * Caches conversation using the virtual file system */ class ConvoVfsCache extends ConvoHashCacheBase_1.ConvoHashCacheBase { cacheDir; logErrors; constructor({ cacheDir = exports.defaultVfsConvoCacheDir, logErrors = true, } = {}) { super(convo_lib_1.commonConvoCacheTypes.vfs); this.cacheDir = cacheDir; this.logErrors = logErrors; } getHashPath(hash) { if (hash.length >= 3) { return (0, common_1.joinPaths)(this.cacheDir, hash.substring(0, 3), hash + '.json'); } else { return (0, common_1.joinPaths)(this.cacheDir, hash + '.json'); } } async getMessagesByKey(key) { try { return await (0, vfs_1.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 (0, vfs_1.vfs)().writeObjectAsync(this.getHashPath(key), messages); } } exports.ConvoVfsCache = ConvoVfsCache; //# sourceMappingURL=ConvoVfsCache.js.map