@convo-lang/convo-lang
Version:
The language of AI
47 lines • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConvoUserVfsCache = void 0;
const common_1 = require("@iyio/common");
const ConvoHashCacheBase_1 = require("./ConvoHashCacheBase");
const ConvoLocalStorageCache_1 = require("./ConvoLocalStorageCache");
const ConvoVfsCache_1 = require("./ConvoVfsCache");
const convo_lib_1 = require("./convo-lib");
/**
* Uses a combination of local storage and the virtual file system to cache. Read attempts will
* first try the vfs then fallback to local storage. Writing to the vfs will only occur if a user
* is signed in. This cache type is good for applications that want user to be able to always read
* from the vfs cache but don't want unauthorized user to write to the cache.
*/
class ConvoUserVfsCache extends ConvoHashCacheBase_1.ConvoHashCacheBase {
localStorageCache;
vfsCache;
constructor(options = {}) {
super(convo_lib_1.commonConvoCacheTypes.userVfs);
this.localStorageCache = new ConvoLocalStorageCache_1.ConvoLocalStorageCache(options.localStorageOptions);
this.vfsCache = new ConvoVfsCache_1.ConvoVfsCache(options.vfsOptions);
}
async getMessagesByKey(key) {
const user = common_1.currentBaseUser.get();
if (user) {
return await this.vfsCache.getMessagesByKey(key);
}
else {
const [fs, ls] = await Promise.all([
this.vfsCache.getMessagesByKey(key),
this.localStorageCache.getMessagesByKey(key)
]);
return fs ?? ls;
}
}
async cacheMessagesByKey(key, messages) {
const user = common_1.currentBaseUser.get();
if (user) {
await this.vfsCache.cacheMessagesByKey(key, messages);
}
else {
this.localStorageCache.cacheMessagesByKey(key, messages);
}
}
}
exports.ConvoUserVfsCache = ConvoUserVfsCache;
//# sourceMappingURL=ConvoUserVfsCache.js.map