Refactor GTM Architect to v2: Python-driven architecture, 9-phase process, new DB and Docker setup

This commit is contained in:
2026-01-02 19:00:05 +00:00
parent a3dc012da8
commit b47a65eb83
300 changed files with 68128 additions and 4782 deletions

View File

@@ -1,93 +1,89 @@
console.log("--- GTM Architect Server starting ---");
const express = require('express');
const { spawn } = require('child_process');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
try {
const express = require('express');
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3005; // Port for the GTM Architect service
const app = express();
const port = 3005;
app.use(express.json({ limit: '50mb' }));
// Enable CORS for all routes
app.use(cors());
// Determine Environment and Paths
const distPath = path.join(__dirname, 'dist');
const isProduction = fs.existsSync(distPath);
const staticDir = isProduction ? distPath : __dirname;
console.log(`[Init] Serving static files from: ${staticDir}`);
app.use(express.static(staticDir));
// Determine Python Script Path
// In Docker (optimized), script is in same dir. Locally, it might be one level up.
let pythonScriptPath = path.join(__dirname, 'gtm_architect_orchestrator.py');
if (!fs.existsSync(pythonScriptPath)) {
pythonScriptPath = path.join(__dirname, '../gtm_architect_orchestrator.py');
}
console.log(`[Init] Using Python script at: ${pythonScriptPath}`);
function callPythonScript(mode, data, res) {
const dataString = JSON.stringify(data);
const pythonProcess = spawn('python3', [pythonScriptPath, '--mode', mode, '--data', dataString]);
let pythonData = '';
let errorData = '';
pythonProcess.stdout.on('data', (data) => {
pythonData += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
errorData += data.toString();
});
pythonProcess.on('close', (code) => {
if (code !== 0) {
console.error(`Python script exited with code ${code}`);
console.error('Stderr:', errorData);
return res.status(500).json({ error: 'Python script execution failed.', details: errorData });
}
try {
const result = JSON.parse(pythonData);
res.json(result);
} catch (e) {
console.error('Failed to parse Python script output:', e);
console.error('Raw output:', pythonData);
res.status(500).json({ error: 'Failed to parse Python script output.', details: pythonData });
}
});
}
// API endpoint to handle requests from the frontend
app.post('/api/gtm', (req, res) => {
const { mode, data } = req.body;
if (!mode || !data) {
return res.status(400).json({ error: 'Missing mode or data in request body' });
}
callPythonScript(mode, data, res);
});
// Serve the main index.html for any other requests to support client-side routing
app.get('*', (req, res) => {
res.sendFile(path.join(staticDir, 'index.html'));
});
const VERSION = "1.1.1_Fix_20260101_1200_FINAL"; // Add a version for debugging
const server = app.listen(port, () => {
console.log(`GTM Architect server listening at http://localhost:${port} (Version: ${VERSION})`);
});
// Prevent 502 Bad Gateway by increasing Node.js server timeouts to match Nginx
server.setTimeout(600000);
server.keepAliveTimeout = 610000;
server.headersTimeout = 620000;
} catch (e) {
console.error("!!! A CRITICAL ERROR OCCURRED ON STARTUP !!!");
console.error(e);
process.exit(1);
// Serve static files from the 'dist' directory
const staticPath = path.join(__dirname, 'dist');
if (fs.existsSync(staticPath)) {
app.use(express.static(staticPath));
} else {
console.warn(`Static path not found: ${staticPath}. The frontend might not be served correctly. Make sure to build the frontend first.`);
}
app.use(express.json({ limit: '50mb' }));
// API endpoint to run the Python script
app.post('/api/run', (req, res) => {
const { mode, ...payload } = req.body;
if (!mode) {
return res.status(400).json({ error: 'Mode is required' });
}
// Convert payload to a JSON string and then to a Base64 string to ensure safe command line passing
const payloadString = JSON.stringify(payload);
const payloadBase64 = Buffer.from(payloadString).toString('base64');
const pythonScriptPath = path.join(__dirname, '../gtm_architect_orchestrator.py');
const pythonProcess = spawn('python3', [
pythonScriptPath,
'--mode', mode,
'--payload_base64', payloadBase64
]);
let stdoutData = '';
let stderrData = '';
pythonProcess.stdout.on('data', (data) => {
stdoutData += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
stderrData += data.toString();
});
pythonProcess.on('close', (code) => {
if (code !== 0) {
console.error(`Python script exited with code ${code}`);
console.error('Stderr:', stderrData);
return res.status(500).json({
error: 'Python script execution failed.',
stderr: stderrData,
});
}
try {
// The Python script is expected to print JSON to stdout
const result = JSON.parse(stdoutData);
res.json(result);
} catch (e) {
console.error('Failed to parse JSON from Python script stdout:', e);
console.error('Stdout:', stdoutData);
res.status(500).json({
error: 'Failed to parse response from Python script.',
stdout: stdoutData,
});
}
});
});
// Serve the main index.html for any other GET request to support client-side routing
if (fs.existsSync(staticPath)) {
app.get('*', (req, res) => {
res.sendFile(path.join(staticPath, 'index.html'));
});
}
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});