@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
52 lines (51 loc) • 1.71 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PersistentState = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
class PersistentState {
constructor(persistenceDir, logger) {
this.logger = logger;
this.state = {};
this.persistenceDir = persistenceDir;
if (this.persistenceDir) {
this.loadState();
}
}
getState() {
return { ...this.state };
}
setState(newState) {
this.state = { ...this.state, ...newState };
this.saveState();
}
async loadState() {
if (!this.persistenceDir)
return;
const filePath = path_1.default.join(this.persistenceDir, 'assistant_state.json');
try {
const data = await promises_1.default.readFile(filePath, 'utf8');
this.state = JSON.parse(data);
}
catch (error) {
if (error.code !== 'ENOENT') {
this.logger?.error('Error loading persistent state:', error);
}
}
}
async saveState() {
if (!this.persistenceDir)
return;
const filePath = path_1.default.join(this.persistenceDir, 'assistant_state.json');
try {
await promises_1.default.writeFile(filePath, JSON.stringify(this.state), 'utf8');
}
catch (error) {
this.logger?.error('Error saving persistent state:', error);
}
}
}
exports.PersistentState = PersistentState;