packfs-core
Version:
Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.
123 lines • 5.3 kB
JavaScript
;
/**
* Disk-based storage backend
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiskBackend = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const logger_js_1 = require("../core/logger.js");
class DiskBackend {
constructor(basePath) {
this.basePath = basePath;
this.logger = logger_js_1.Logger.getInstance().createChildLogger('DiskBackend');
}
async initialize() {
this.logger.info(`Initializing disk backend at ${this.basePath}`);
try {
await fs_1.promises.mkdir(this.basePath, { recursive: true });
this.logger.debug(`Successfully created base directory: ${this.basePath}`);
}
catch (error) {
this.logger.error(`Failed to initialize disk backend`, { error: error instanceof Error ? error.message : error });
throw new Error(`Failed to initialize disk backend: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async read(path) {
const fullPath = (0, path_1.join)(this.basePath, path);
this.logger.debug(`Reading file: ${path}`, { fullPath });
try {
const data = await fs_1.promises.readFile(fullPath);
this.logger.info(`Successfully read file: ${path}`, { size: data.length });
return data;
}
catch (error) {
this.logger.error(`Failed to read file: ${path}`, { error: error instanceof Error ? error.message : error });
throw new Error(`Failed to read file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async write(path, data) {
const fullPath = (0, path_1.join)(this.basePath, path);
const dir = (0, path_1.dirname)(fullPath);
this.logger.debug(`Writing file: ${path}`, { fullPath, size: data.length });
try {
await fs_1.promises.mkdir(dir, { recursive: true });
await fs_1.promises.writeFile(fullPath, data);
this.logger.info(`Successfully wrote file: ${path}`, { size: data.length });
}
catch (error) {
this.logger.error(`Failed to write file: ${path}`, { error: error instanceof Error ? error.message : error });
throw new Error(`Failed to write file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async exists(path) {
const fullPath = (0, path_1.join)(this.basePath, path);
this.logger.debug(`Checking existence: ${path}`, { fullPath });
try {
await fs_1.promises.access(fullPath);
this.logger.debug(`File exists: ${path}`);
return true;
}
catch {
this.logger.debug(`File does not exist: ${path}`);
return false;
}
}
async stat(path) {
const fullPath = (0, path_1.join)(this.basePath, path);
this.logger.debug(`Getting file stats: ${path}`, { fullPath });
try {
const stats = await fs_1.promises.stat(fullPath);
const metadata = {
path,
size: stats.size,
mtime: stats.mtime,
isDirectory: stats.isDirectory(),
permissions: stats.mode
};
this.logger.debug(`Got file stats: ${path}`, metadata);
return metadata;
}
catch (error) {
this.logger.error(`Failed to stat file: ${path}`, { error: error instanceof Error ? error.message : error });
throw new Error(`Failed to stat file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async list(path) {
const fullPath = (0, path_1.join)(this.basePath, path);
this.logger.debug(`Listing directory: ${path}`, { fullPath });
try {
const items = await fs_1.promises.readdir(fullPath);
this.logger.info(`Listed directory: ${path}`, { count: items.length });
return items;
}
catch (error) {
this.logger.error(`Failed to list directory: ${path}`, { error: error instanceof Error ? error.message : error });
throw new Error(`Failed to list directory: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async delete(path) {
const fullPath = (0, path_1.join)(this.basePath, path);
this.logger.debug(`Deleting: ${path}`, { fullPath });
try {
const stats = await fs_1.promises.stat(fullPath);
if (stats.isDirectory()) {
await fs_1.promises.rmdir(fullPath, { recursive: true });
this.logger.info(`Deleted directory: ${path}`);
}
else {
await fs_1.promises.unlink(fullPath);
this.logger.info(`Deleted file: ${path}`);
}
}
catch (error) {
this.logger.error(`Failed to delete: ${path}`, { error: error instanceof Error ? error.message : error });
throw new Error(`Failed to delete: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async cleanup() {
// No cleanup needed for disk backend
}
}
exports.DiskBackend = DiskBackend;
//# sourceMappingURL=disk.js.map