windsurf-memory-optimization
Version:
Memory optimization tools for Windsurf AI
148 lines • 6.31 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SnapshotManager = void 0;
const node_persist_1 = __importDefault(require("node-persist"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class SnapshotManager {
constructor() {
this.initialized = false;
}
async init(storageDir, logging, ttl) {
if (this.initialized)
return;
try {
// Sử dụng một thư mục tạm thời mới để tránh xung đột
const baseDir = storageDir || process.env.WINDSURF_MEMORY_DIR || '.windsurf';
const snapshotStorageDir = path.join(baseDir, 'snapshot-storage');
console.log(`Will use snapshot storage directory: ${snapshotStorageDir}`);
// Xóa và tạo lại thư mục nếu đã tồn tại
if (!fs.existsSync(baseDir)) {
console.log(`Creating base directory: ${baseDir}`);
fs.mkdirSync(baseDir, { recursive: true });
}
if (!fs.existsSync(snapshotStorageDir)) {
console.log(`Creating snapshot storage directory: ${snapshotStorageDir}`);
fs.mkdirSync(snapshotStorageDir, { recursive: true });
}
// Kiểm tra xem snapshotStorageDir có phải là thư mục không
const stats = fs.statSync(snapshotStorageDir);
if (!stats.isDirectory()) {
console.error(`Snapshot storage path exists but is not a directory: ${snapshotStorageDir}`);
// Xóa file và tạo thư mục
fs.unlinkSync(snapshotStorageDir);
fs.mkdirSync(snapshotStorageDir, { recursive: true });
}
// Tạo và khởi tạo storage
console.log(`Initializing snapshot storage in: ${snapshotStorageDir}`);
this.storage = await node_persist_1.default.create({
dir: snapshotStorageDir,
logging: logging || true,
ttl: ttl || true
});
console.log('Initializing snapshot storage...');
await this.storage.init();
console.log('Snapshot storage initialized successfully');
this.initialized = true;
}
catch (error) {
console.error('Error initializing snapshot storage:', error instanceof Error ? error.message : String(error));
throw error;
}
}
async createSnapshot(tag, pins) {
const snapshotDir = process.env.WINDSURF_SNAPSHOTS_DIR || '.windsurf/snapshots';
const timestamp = new Date().toISOString();
const filename = `snapshot-${tag}-${timestamp}.json`;
// Tạo thư mục snapshot nếu chưa tồn tại
try {
await fs.promises.mkdir(snapshotDir, { recursive: true });
}
catch (error) {
console.error('Error creating snapshot directory:', error);
throw error;
}
// Lưu snapshot
await this.storage.setItem(filename, {
tag,
timestamp,
pins
});
return filename;
}
async restoreSnapshot(tag) {
const keys = await this.storage.keys();
for (const key of keys) {
const snapshot = await this.storage.getItem(key);
if (snapshot && snapshot.tag === tag) {
// TODO: Phục hồi snapshot
return;
}
}
throw new Error(`Snapshot with tag ${tag} not found`);
}
/**
* Liệt kê tất cả các snapshots hiện có
* @returns Danh sách các snapshot với thông tin chi tiết
*/
async listSnapshots() {
if (!this.initialized) {
await this.init();
}
try {
// Lấy tất cả các keys từ storage
const keys = await this.storage.keys();
const snapshots = [];
// Lọc các keys bắt đầu bằng 'snapshot-' và lấy thông tin chi tiết
for (const key of keys) {
if (key.startsWith('snapshot-')) {
const snapshot = await this.storage.getItem(key);
if (snapshot) {
snapshots.push({
id: key,
tag: snapshot.tag || 'unknown',
timestamp: snapshot.timestamp || new Date().toISOString(),
pinCount: snapshot.pins ? snapshot.pins.length : 0
});
}
}
}
// Sắp xếp theo thời gian mới nhất trước
return snapshots.sort((a, b) => {
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
});
}
catch (error) {
console.error('Error listing snapshots:', error instanceof Error ? error.message : String(error));
return [];
}
}
}
exports.SnapshotManager = SnapshotManager;
//# sourceMappingURL=snapshot-manager.js.map