dist / tools / fileTools.js
dist / tools / fileTools.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeFileTool = executeFileTool;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const fs_1 = require("fs");
const constants_1 = require("./constants");
async function executeFileTool(toolName, args) {
const targetPath = args?.file || args?.path || args?.folder || args?.source || args?.oldName || "";
const p = (0, constants_1.resolveInsideBase)(targetPath);
switch (toolName) {
case "readFile":
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `File ${targetPath} not found.` };
const content = await (0, promises_1.readFile)(p, "utf-8");
return { success: true, content, size: content.length };
case "writeFile":
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(p));
await (0, promises_1.writeFile)(p, args?.content || "", "utf-8");
return { success: true, message: `File ${targetPath} written successfully.` };
case "appendFile":
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(p));
const current = (0, fs_1.existsSync)(p) ? await (0, promises_1.readFile)(p, "utf-8") : "";
await (0, promises_1.writeFile)(p, current + (args?.content || ""), "utf-8");
return { success: true, message: `Content appended to ${targetPath}.` };
case "renameFile":
case "moveFolder":
const newP = (0, constants_1.resolveInsideBase)(args?.newName || args?.dest || "");
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `Path ${targetPath} not found.` };
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(newP));
await (0, promises_1.rename)(p, newP);
return { success: true, message: `Successfully moved/renamed to ${args?.newName || args?.dest}.` };
case "deleteFile":
case "deleteFolder":
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `Path ${targetPath} not found.` };
await (0, promises_1.rm)(p, { recursive: true, force: true });
return { success: true, message: `Successfully deleted ${targetPath}.` };
case "copyFile":
const toP = (0, constants_1.resolveInsideBase)(args?.to || "");
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `File ${targetPath} not found.` };
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(toP));
await (0, promises_1.cp)(p, toP, { force: !!args?.overwrite, errorOnExist: !args?.overwrite });
return { success: true, message: `Successfully copied to ${args?.to}.` };
case "listFolder":
case "listDirectoryStructure":
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `Directory not found.` };
const entries = await (0, promises_1.readdir)(p, { withFileTypes: true });
const items = await Promise.all(entries.map(async (e) => {
const itemPath = (0, path_1.join)(p, e.name);
const s = await (0, promises_1.stat)(itemPath);
return {
name: e.name,
type: e.isDirectory() ? "folder" : "file",
size: e.isDirectory() ? "-" : `${(s.size / 1024).toFixed(2)} KB`,
modified: s.mtime.toISOString(),
};
}));
return { success: true, folder: targetPath || "[Root]", items };
case "createFolder":
await (0, constants_1.ensureDirectory)(p);
return { success: true, message: `Folder ${targetPath} created successfully.` };
case "searchFiles":
return await searchFiles(args?.searchPath || args?.path || constants_1.targetBase, args?.query || args?.pattern || "", args?.searchType || args?.type || "name");
default:
return null;
}
}
async function searchFiles(searchPath, query, searchType = "name") {
const results = [];
let scanned = 0;
const maxDepth = 5;
const excludeDirs = [
'$RECYCLE.BIN', 'System Volume Information', 'Windows',
'Program Files', 'Program Files (x86)', 'node_modules',
'.git', 'temp', 'tmp', 'old_data', 'dist', 'build',
'__pycache__', '.venv', 'venv', 'env', 'Library',
'System32', 'AppData', 'Local Settings'
];
const excludeExtensions = ['.exe', '.dll', '.bin', '.dat', '.log', '.tmp', '.cache'];
const textExtensions = ['.txt', '.md', '.js', '.jsx', '.ts', '.tsx', '.py', '.json', '.html', '.css', '.csv', '.xml', '.yml', '.yaml', '.sh', '.bash', '.go', '.rs', '.rb', '.php'];
async function scan(dir, depth = 0) {
if (depth > maxDepth)
return;
try {
const entries = await (0, promises_1.readdir)(dir, { withFileTypes: true });
for (const entry of entries) {
if (excludeDirs.includes(entry.name))
continue;
const fullPath = (0, path_1.join)(dir, entry.name);
try {
if (entry.isDirectory()) {
await scan(fullPath, depth + 1);
}
else {
const stats = await (0, promises_1.stat)(fullPath);
if (stats.size > 10 * 1024 * 1024)
continue;
const ext = (0, path_1.extname)(entry.name).toLowerCase();
if (excludeExtensions.includes(ext))
continue;
scanned++;
let match = false;
if (searchType === "name") {
match = entry.name.toLowerCase().includes(query.toLowerCase());
}
else {
if (!textExtensions.includes(ext))
continue;
try {
const content = await (0, promises_1.readFile)(fullPath, "utf-8");
match = content.toLowerCase().includes(query.toLowerCase());
}
catch { /* skip */ }
}
if (match) {
results.push({ path: fullPath, name: entry.name, size: stats.size, modified: stats.mtime, extension: ext || 'none' });
}
if (results.length >= 50)
return;
}
}
catch { /* skip */ }
}
}
catch { /* skip */ }
}
try {
const p = (0, constants_1.resolveInsideBase)(searchPath);
await (0, promises_1.access)(p);
await scan(p, 0);
return {
success: true,
results: results.slice(0, 50),
total: results.length,
scanned,
searchType,
query,
path: searchPath,
system_note: `Search completed: scanned ${scanned} files, found ${results.length} results. Use relevant info.`
};
}
catch (error) {
return { success: false, error: `Path not accessible: ${searchPath}`, details: error.message };
}
}
//# sourceMappingURL=fileTools.js.map"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeFileTool = executeFileTool;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const fs_1 = require("fs");
const constants_1 = require("./constants");
async function executeFileTool(toolName, args) {
const targetPath = args?.file || args?.path || args?.folder || args?.source || args?.oldName || "";
const p = (0, constants_1.resolveInsideBase)(targetPath);
switch (toolName) {
case "readFile":
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `File ${targetPath} not found.` };
const content = await (0, promises_1.readFile)(p, "utf-8");
return { success: true, content, size: content.length };
case "writeFile":
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(p));
await (0, promises_1.writeFile)(p, args?.content || "", "utf-8");
return { success: true, message: `File ${targetPath} written successfully.` };
case "appendFile":
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(p));
const current = (0, fs_1.existsSync)(p) ? await (0, promises_1.readFile)(p, "utf-8") : "";
await (0, promises_1.writeFile)(p, current + (args?.content || ""), "utf-8");
return { success: true, message: `Content appended to ${targetPath}.` };
case "renameFile":
case "moveFolder":
const newP = (0, constants_1.resolveInsideBase)(args?.newName || args?.dest || "");
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `Path ${targetPath} not found.` };
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(newP));
await (0, promises_1.rename)(p, newP);
return { success: true, message: `Successfully moved/renamed to ${args?.newName || args?.dest}.` };
case "deleteFile":
case "deleteFolder":
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `Path ${targetPath} not found.` };
await (0, promises_1.rm)(p, { recursive: true, force: true });
return { success: true, message: `Successfully deleted ${targetPath}.` };
case "copyFile":
const toP = (0, constants_1.resolveInsideBase)(args?.to || "");
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `File ${targetPath} not found.` };
await (0, constants_1.ensureDirectory)((0, path_1.dirname)(toP));
await (0, promises_1.cp)(p, toP, { force: !!args?.overwrite, errorOnExist: !args?.overwrite });
return { success: true, message: `Successfully copied to ${args?.to}.` };
case "listFolder":
case "listDirectoryStructure":
if (!(0, fs_1.existsSync)(p))
return { success: false, error: `Directory not found.` };
const entries = await (0, promises_1.readdir)(p, { withFileTypes: true });
const items = await Promise.all(entries.map(async (e) => {
const itemPath = (0, path_1.join)(p, e.name);
const s = await (0, promises_1.stat)(itemPath);
return {
name: e.name,
type: e.isDirectory() ? "folder" : "file",
size: e.isDirectory() ? "-" : `${(s.size / 1024).toFixed(2)} KB`,
modified: s.mtime.toISOString(),
};
}));
return { success: true, folder: targetPath || "[Root]", items };
case "createFolder":
await (0, constants_1.ensureDirectory)(p);
return { success: true, message: `Folder ${targetPath} created successfully.` };
case "searchFiles":
return await searchFiles(args?.searchPath || args?.path || constants_1.targetBase, args?.query || args?.pattern || "", args?.searchType || args?.type || "name");
default:
return null;
}
}
async function searchFiles(searchPath, query, searchType = "name") {
const results = [];
let scanned = 0;
const maxDepth = 5;
const excludeDirs = [
'$RECYCLE.BIN', 'System Volume Information', 'Windows',
'Program Files', 'Program Files (x86)', 'node_modules',
'.git', 'temp', 'tmp', 'old_data', 'dist', 'build',
'__pycache__', '.venv', 'venv', 'env', 'Library',
'System32', 'AppData', 'Local Settings'
];
const excludeExtensions = ['.exe', '.dll', '.bin', '.dat', '.log', '.tmp', '.cache'];
const textExtensions = ['.txt', '.md', '.js', '.jsx', '.ts', '.tsx', '.py', '.json', '.html', '.css', '.csv', '.xml', '.yml', '.yaml', '.sh', '.bash', '.go', '.rs', '.rb', '.php'];
async function scan(dir, depth = 0) {
if (depth > maxDepth)
return;
try {
const entries = await (0, promises_1.readdir)(dir, { withFileTypes: true });
for (const entry of entries) {
if (excludeDirs.includes(entry.name))
continue;
const fullPath = (0, path_1.join)(dir, entry.name);
try {
if (entry.isDirectory()) {
await scan(fullPath, depth + 1);
}
else {
const stats = await (0, promises_1.stat)(fullPath);
if (stats.size > 10 * 1024 * 1024)
continue;
const ext = (0, path_1.extname)(entry.name).toLowerCase();
if (excludeExtensions.includes(ext))
continue;
scanned++;
let match = false;
if (searchType === "name") {
match = entry.name.toLowerCase().includes(query.toLowerCase());
}
else {
if (!textExtensions.includes(ext))
continue;
try {
const content = await (0, promises_1.readFile)(fullPath, "utf-8");
match = content.toLowerCase().includes(query.toLowerCase());
}
catch { /* skip */ }
}
if (match) {
results.push({ path: fullPath, name: entry.name, size: stats.size, modified: stats.mtime, extension: ext || 'none' });
}
if (results.length >= 50)
return;
}
}
catch { /* skip */ }
}
}
catch { /* skip */ }
}
try {
const p = (0, constants_1.resolveInsideBase)(searchPath);
await (0, promises_1.access)(p);
await scan(p, 0);
return {
success: true,
results: results.slice(0, 50),
total: results.length,
scanned,
searchType,
query,
path: searchPath,
system_note: `Search completed: scanned ${scanned} files, found ${results.length} results. Use relevant info.`
};
}
catch (error) {
return { success: false, error: `Path not accessible: ${searchPath}`, details: error.message };
}
}
//# sourceMappingURL=fileTools.js.map