UNPKG

@personnn/personnnkit

Version:

🇵 PersonnnKit - El Agente Kit Universal. Framework revolucionario para crear agentes de IA con HTML + Python. Simplicidad radical vs frameworks gigantes.

132 lines 5.05 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startDevServer = startDevServer; const express_1 = __importDefault(require("express")); const cors_1 = __importDefault(require("cors")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const child_process_1 = require("child_process"); const chokidar_1 = __importDefault(require("chokidar")); function loadConfig() { const configPath = path_1.default.join(process.cwd(), "personnn.config.ts"); const defaultConfig = { port: 3333, pagesDir: "pages", staticDir: "public", apiDir: "runtime/api.js", cronFile: "runtime/cron.js", scriptsDir: "scripts" }; if (fs_1.default.existsSync(configPath)) { try { // In development, use dynamic require delete require.cache[require.resolve(configPath)]; const config = require(configPath).default || require(configPath); return { ...defaultConfig, ...config }; } catch (e) { console.warn("⚠️ Error loading config, using defaults"); return defaultConfig; } } return defaultConfig; } function startDevServer() { const config = loadConfig(); const app = (0, express_1.default)(); // Basic middleware app.use((0, cors_1.default)()); app.use(express_1.default.json()); app.use(express_1.default.urlencoded({ extended: true })); // Serve static files const staticPath = path_1.default.join(process.cwd(), config.staticDir); if (fs_1.default.existsSync(staticPath)) { app.use(express_1.default.static(staticPath)); console.log(`📁 Serving static files from /${config.staticDir}`); } // Load API if exists const apiPath = path_1.default.join(process.cwd(), config.apiDir); if (fs_1.default.existsSync(apiPath)) { try { delete require.cache[require.resolve(apiPath)]; const api = require(apiPath); if (api && typeof api.setup === "function") { api.setup(app); console.log(`🔌 API loaded from ${config.apiDir}`); } } catch (e) { console.warn(`⚠️ Error loading API: ${e}`); } } // Endpoint to run Python scripts app.post("/api/run-script", (req, res) => { const { script } = req.body; if (!script) { return res.status(400).json({ error: "Script required" }); } const scriptPath = path_1.default.join(process.cwd(), config.scriptsDir, script); if (!fs_1.default.existsSync(scriptPath)) { return res.status(404).json({ error: "Script not found" }); } (0, child_process_1.exec)(`python3 ${scriptPath}`, (err, stdout, stderr) => { if (err) { return res.status(500).json({ error: stderr || err.message }); } res.json({ output: stdout, error: stderr }); }); }); // HTML routing app.get("*", (req, res) => { let filePath; if (req.path === "/") { filePath = path_1.default.join(process.cwd(), config.pagesDir, "index.html"); } else { // Remove leading slash and add .html if not present let requestedFile = req.path.slice(1); if (!requestedFile.endsWith(".html")) { requestedFile += ".html"; } filePath = path_1.default.join(process.cwd(), config.pagesDir, requestedFile); } if (fs_1.default.existsSync(filePath)) { res.sendFile(filePath); } else { res.status(404).send(` <h1>404 - Page not found</h1> <p>Could not find: ${req.path}</p> <p>Looking for: ${filePath}</p> <a href="/">← Back to home</a> `); } }); // Start server app.listen(config.port, () => { console.log(`🚀 PersonnnKit running at http://localhost:${config.port}`); console.log(`📄 Pages from: /${config.pagesDir}`); console.log(`🐍 Scripts from: /${config.scriptsDir}`); }); // Run cron if exists const cronPath = path_1.default.join(process.cwd(), config.cronFile); if (fs_1.default.existsSync(cronPath)) { try { require(cronPath); console.log(`⏰ Cron started from ${config.cronFile}`); } catch (e) { console.warn(`⚠️ Error starting cron: ${e}`); } } // Watch for hot reload (optional) if (fs_1.default.existsSync(path_1.default.join(process.cwd(), config.pagesDir))) { chokidar_1.default.watch(path_1.default.join(process.cwd(), config.pagesDir)).on('change', () => { console.log("🔄 HTML files modified"); }); } } //# sourceMappingURL=dev-server.js.map