dist / tools / systemTools.js
dist / tools / systemTools.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeSystemTool = executeSystemTool;
const child_process_1 = require("child_process");
const util_1 = require("util");
const constants_1 = require("./constants");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const fs_1 = require("fs");
const constants_2 = require("./constants");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
async function executeSystemTool(toolName, args) {
switch (toolName) {
case "getNow":
case "getCurrentTime": {
const now = new Date();
return {
success: true,
iso: now.toISOString(),
locale: now.toLocaleString(),
time: now.toLocaleTimeString(),
timestamp: Date.now(),
};
}
case "getCurrentDate": {
const today = new Date();
return { success: true, date: today.toLocaleDateString(), isoDate: today.toISOString().slice(0, 10) };
}
case "terminal":
case "wolfTerminal": {
const cmd = args?.cmd || args?.action || "";
const cwd = args?.cwd || constants_1.targetBase;
try {
const response = await fetch("http://localhost:9090/api/v1", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "exec", data: { cmd, cwd } }),
});
if (response.ok) {
const res = (await response.json());
return {
success: true,
output: res.output || res.stdout || "",
error: res.stderr || res.error || "",
exitCode: res.exitCode ?? 0,
system_note: `Executed via terminal server: ${cmd}`,
};
}
}
catch { }
// Local fallback
return await new Promise((resolve) => {
(0, child_process_1.exec)(cmd, { cwd }, (error, stdout, stderr) => {
resolve({
success: !error,
output: stdout,
error: stderr || error?.message,
exitCode: error ? 1 : 0,
system_note: `Executed locally: ${cmd}`,
});
});
});
}
case "runCode": {
const { language = "javascript", code = "" } = args;
const tempDir = (0, path_1.join)(constants_1.targetBase, "temp_scripts");
await (0, constants_2.ensureDirectory)(tempDir);
const ext = language === "python" ? ".py" : ".js";
const tempFile = (0, path_1.join)(tempDir, `exec_${Date.now()}${ext}`);
await (0, promises_1.writeFile)(tempFile, code, "utf-8");
try {
const cmd = language === "python" ? `python "${tempFile}"` : `node "${tempFile}"`;
const { stdout, stderr } = await execAsync(cmd, { timeout: 30000 });
await (0, promises_1.rm)(tempFile, { force: true }).catch(() => { });
return { success: true, stdout: stdout.slice(0, 5000), stderr: stderr.slice(0, 1000) };
}
catch (error) {
await (0, promises_1.rm)(tempFile, { force: true }).catch(() => { });
return { success: false, error: error?.message };
}
}
case "eagleEye": {
const filePath = args?.filePath || args?.file || "";
try {
const fullPath = (0, constants_1.resolveInsideBase)(filePath);
const fileStat = await (0, promises_1.stat)(fullPath);
const content = await (0, promises_1.readFile)(fullPath, "utf-8");
const lines = content.split("\n");
const suspicious = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g;
const hasSuspicious = suspicious.test(content);
const ext = (0, path_1.extname)(filePath).toLowerCase();
let preview = lines.slice(0, 30).join("\n");
if (preview.length > 2000)
preview = preview.slice(0, 2000) + "\n... (truncated)";
return {
success: true,
file: filePath,
size: fileStat.size,
lines: lines.length,
chars: content.length,
hasSuspicious,
preview,
recommendation: hasSuspicious ? "Warning: potentially malicious non-printable characters found." : "Safe to read.",
};
}
catch (e) {
return { success: false, error: e.message };
}
}
case "ocrImage": {
const imagePath = args?.imagePath || "";
const language = args?.language || "ara+eng";
try {
const fullPath = (0, constants_1.resolveInsideBase)(imagePath);
if (!(0, fs_1.existsSync)(fullPath))
return { success: false, error: `File not found: ${imagePath}` };
const tesseractPaths = [
"D:\\ocr\\Tesseract-OCR\\tesseract.exe",
"C:\\Program Files\\Tesseract-OCR\\tesseract.exe",
"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe",
];
let tesseractPath = null;
for (const p of tesseractPaths) {
if ((0, fs_1.existsSync)(p)) {
tesseractPath = p;
break;
}
}
if (!tesseractPath) {
const { stdout } = await execAsync(`where tesseract 2>nul`).catch(() => ({ stdout: "" }));
if (stdout.trim())
tesseractPath = stdout.trim().split("\n")[0];
}
if (!tesseractPath)
return { success: false, error: "Tesseract OCR not installed or found in PATH." };
const { stdout, stderr } = await execAsync(`"${tesseractPath}" "${fullPath}" stdout -l ${language} 2>nul`);
if (stderr && !stderr.includes("Tesseract Open Source"))
return { success: false, error: stderr };
const text = stdout.trim();
if (!text)
return { success: false, error: "No text extracted from image." };
return { success: true, text, language };
}
catch (e) {
return { success: false, error: e.message };
}
}
default:
return null;
}
}
//# sourceMappingURL=systemTools.js.map"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeSystemTool = executeSystemTool;
const child_process_1 = require("child_process");
const util_1 = require("util");
const constants_1 = require("./constants");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const fs_1 = require("fs");
const constants_2 = require("./constants");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
async function executeSystemTool(toolName, args) {
switch (toolName) {
case "getNow":
case "getCurrentTime": {
const now = new Date();
return {
success: true,
iso: now.toISOString(),
locale: now.toLocaleString(),
time: now.toLocaleTimeString(),
timestamp: Date.now(),
};
}
case "getCurrentDate": {
const today = new Date();
return { success: true, date: today.toLocaleDateString(), isoDate: today.toISOString().slice(0, 10) };
}
case "terminal":
case "wolfTerminal": {
const cmd = args?.cmd || args?.action || "";
const cwd = args?.cwd || constants_1.targetBase;
try {
const response = await fetch("http://localhost:9090/api/v1", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "exec", data: { cmd, cwd } }),
});
if (response.ok) {
const res = (await response.json());
return {
success: true,
output: res.output || res.stdout || "",
error: res.stderr || res.error || "",
exitCode: res.exitCode ?? 0,
system_note: `Executed via terminal server: ${cmd}`,
};
}
}
catch { }
// Local fallback
return await new Promise((resolve) => {
(0, child_process_1.exec)(cmd, { cwd }, (error, stdout, stderr) => {
resolve({
success: !error,
output: stdout,
error: stderr || error?.message,
exitCode: error ? 1 : 0,
system_note: `Executed locally: ${cmd}`,
});
});
});
}
case "runCode": {
const { language = "javascript", code = "" } = args;
const tempDir = (0, path_1.join)(constants_1.targetBase, "temp_scripts");
await (0, constants_2.ensureDirectory)(tempDir);
const ext = language === "python" ? ".py" : ".js";
const tempFile = (0, path_1.join)(tempDir, `exec_${Date.now()}${ext}`);
await (0, promises_1.writeFile)(tempFile, code, "utf-8");
try {
const cmd = language === "python" ? `python "${tempFile}"` : `node "${tempFile}"`;
const { stdout, stderr } = await execAsync(cmd, { timeout: 30000 });
await (0, promises_1.rm)(tempFile, { force: true }).catch(() => { });
return { success: true, stdout: stdout.slice(0, 5000), stderr: stderr.slice(0, 1000) };
}
catch (error) {
await (0, promises_1.rm)(tempFile, { force: true }).catch(() => { });
return { success: false, error: error?.message };
}
}
case "eagleEye": {
const filePath = args?.filePath || args?.file || "";
try {
const fullPath = (0, constants_1.resolveInsideBase)(filePath);
const fileStat = await (0, promises_1.stat)(fullPath);
const content = await (0, promises_1.readFile)(fullPath, "utf-8");
const lines = content.split("\n");
const suspicious = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g;
const hasSuspicious = suspicious.test(content);
const ext = (0, path_1.extname)(filePath).toLowerCase();
let preview = lines.slice(0, 30).join("\n");
if (preview.length > 2000)
preview = preview.slice(0, 2000) + "\n... (truncated)";
return {
success: true,
file: filePath,
size: fileStat.size,
lines: lines.length,
chars: content.length,
hasSuspicious,
preview,
recommendation: hasSuspicious ? "Warning: potentially malicious non-printable characters found." : "Safe to read.",
};
}
catch (e) {
return { success: false, error: e.message };
}
}
case "ocrImage": {
const imagePath = args?.imagePath || "";
const language = args?.language || "ara+eng";
try {
const fullPath = (0, constants_1.resolveInsideBase)(imagePath);
if (!(0, fs_1.existsSync)(fullPath))
return { success: false, error: `File not found: ${imagePath}` };
const tesseractPaths = [
"D:\\ocr\\Tesseract-OCR\\tesseract.exe",
"C:\\Program Files\\Tesseract-OCR\\tesseract.exe",
"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe",
];
let tesseractPath = null;
for (const p of tesseractPaths) {
if ((0, fs_1.existsSync)(p)) {
tesseractPath = p;
break;
}
}
if (!tesseractPath) {
const { stdout } = await execAsync(`where tesseract 2>nul`).catch(() => ({ stdout: "" }));
if (stdout.trim())
tesseractPath = stdout.trim().split("\n")[0];
}
if (!tesseractPath)
return { success: false, error: "Tesseract OCR not installed or found in PATH." };
const { stdout, stderr } = await execAsync(`"${tesseractPath}" "${fullPath}" stdout -l ${language} 2>nul`);
if (stderr && !stderr.includes("Tesseract Open Source"))
return { success: false, error: stderr };
const text = stdout.trim();
if (!text)
return { success: false, error: "No text extracted from image." };
return { success: true, text, language };
}
catch (e) {
return { success: false, error: e.message };
}
}
default:
return null;
}
}
//# sourceMappingURL=systemTools.js.map