packfs-core
Version:
Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.
134 lines • 5.19 kB
JavaScript
;
/**
* Simple Enhanced PackFS - A wrapper that adds logging and enhanced features
* without the complexity of extending FakeFS
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleEnhancedPackFS = void 0;
exports.createSimpleEnhancedFileSystem = createSimpleEnhancedFileSystem;
const logger_js_1 = require("../core/logger.js");
const filesystem_js_1 = require("../core/filesystem.js");
const disk_js_1 = require("../backends/disk.js");
class SimpleEnhancedPackFS extends filesystem_js_1.FileSystemInterface {
constructor(backend, config = {}) {
super();
this.backend = backend;
this.config = {
enableLogging: true,
logLevel: 'info',
...config
};
this.logger = logger_js_1.Logger.getInstance().createChildLogger('SimpleEnhancedPackFS');
if (this.config.enableLogging) {
this.logger.info('Initialized enhanced filesystem', { config: this.config });
}
}
async readFile(path, options) {
this.logger.debug(`Reading file: ${path}`, { options });
try {
const data = await this.backend.read(path);
this.logger.info(`Successfully read file: ${path}`, { size: data.length });
return options?.encoding ? data.toString(options.encoding) : data;
}
catch (error) {
this.logger.error(`Failed to read file: ${path}`, { error });
throw error;
}
}
async writeFile(path, data, options) {
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, options?.encoding || 'utf8');
this.logger.debug(`Writing file: ${path}`, { size: buffer.length, options });
try {
await this.backend.write(path, buffer);
this.logger.info(`Successfully wrote file: ${path}`, { size: buffer.length });
}
catch (error) {
this.logger.error(`Failed to write file: ${path}`, { error });
throw error;
}
}
async exists(path) {
this.logger.debug(`Checking existence: ${path}`);
const exists = await this.backend.exists(path);
this.logger.debug(`File ${exists ? 'exists' : 'does not exist'}: ${path}`);
return exists;
}
async stat(path) {
this.logger.debug(`Getting file stats: ${path}`);
try {
const stats = await this.backend.stat(path);
this.logger.debug(`Got file stats: ${path}`, stats);
return stats;
}
catch (error) {
this.logger.error(`Failed to stat file: ${path}`, { error });
throw error;
}
}
async readdir(path) {
this.logger.debug(`Listing directory: ${path}`);
try {
const items = await this.backend.list(path);
this.logger.info(`Listed directory: ${path}`, { count: items.length });
return items;
}
catch (error) {
this.logger.error(`Failed to list directory: ${path}`, { error });
throw error;
}
}
async mkdir(path, recursive) {
this.logger.debug(`Creating directory: ${path}`, { recursive });
try {
// Backend doesn't have mkdir, so we simulate it
await this.backend.write(`${path}/.gitkeep`, Buffer.from(''));
this.logger.info(`Created directory: ${path}`);
}
catch (error) {
this.logger.error(`Failed to create directory: ${path}`, { error });
throw error;
}
}
async remove(path, recursive) {
this.logger.debug(`Removing: ${path}`, { recursive });
try {
await this.backend.delete(path);
this.logger.info(`Removed: ${path}`);
}
catch (error) {
this.logger.error(`Failed to remove: ${path}`, { error });
throw error;
}
}
async copy(source, destination) {
this.logger.debug(`Copying from ${source} to ${destination}`);
try {
const data = await this.backend.read(source);
await this.backend.write(destination, data);
this.logger.info(`Copied ${source} to ${destination}`);
}
catch (error) {
this.logger.error(`Failed to copy from ${source} to ${destination}`, { error });
throw error;
}
}
async move(source, destination) {
this.logger.debug(`Moving from ${source} to ${destination}`);
try {
const data = await this.backend.read(source);
await this.backend.write(destination, data);
await this.backend.delete(source);
this.logger.info(`Moved ${source} to ${destination}`);
}
catch (error) {
this.logger.error(`Failed to move from ${source} to ${destination}`, { error });
throw error;
}
}
}
exports.SimpleEnhancedPackFS = SimpleEnhancedPackFS;
function createSimpleEnhancedFileSystem(basePath, config) {
const backend = new disk_js_1.DiskBackend(basePath);
return new SimpleEnhancedPackFS(backend, config);
}
//# sourceMappingURL=SimpleEnhancedPackFS.js.map