Fix: Resolved E2BIG error by switching to file-based payload passing for large requests (images)

This commit is contained in:
2026-01-03 13:44:18 +00:00
parent 6bde387760
commit 0761c05b62
2 changed files with 38 additions and 10 deletions

View File

@@ -48,15 +48,25 @@ app.post('/api/run', (req, res) => {
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');
// E2BIG FIX: Write payload to a temporary file instead of passing via command line args
const tmpDir = path.join(__dirname, 'tmp');
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}
const payloadFilePath = path.join(tmpDir, `payload_${Date.now()}_${Math.random().toString(36).substring(7)}.json`);
try {
fs.writeFileSync(payloadFilePath, JSON.stringify(payload));
} catch (err) {
console.error("Failed to write payload file:", err);
return res.status(500).json({ error: "Failed to process request payload." });
}
const pythonScriptPath = path.join(__dirname, 'gtm_architect_orchestrator.py');
const pythonProcess = spawn('python3', [
pythonScriptPath,
'--mode', mode,
'--payload_base64', payloadBase64
'--payload_file', payloadFilePath // Use file path instead of base64 string
]);
let stdoutData = '';
@@ -76,6 +86,13 @@ app.post('/api/run', (req, res) => {
});
pythonProcess.on('close', (code) => {
// Cleanup temp file
try {
if (fs.existsSync(payloadFilePath)) fs.unlinkSync(payloadFilePath);
} catch (e) {
console.error("Failed to delete temp payload file:", e);
}
if (code !== 0) {
console.error(`Python script exited with code ${code}`);
console.error('Stderr:', stderrData);

View File

@@ -379,17 +379,28 @@ def main():
"""
parser = argparse.ArgumentParser(description="GTM Architect Orchestrator")
parser.add_argument("--mode", required=True, help="The execution mode (e.g., phase1, phase2).")
parser.add_argument("--payload_base64", required=True, help="The Base64 encoded JSON payload.")
parser.add_argument("--payload_base64", help="The Base64 encoded JSON payload (deprecated, use payload_file).")
parser.add_argument("--payload_file", help="Path to a JSON file containing the payload (preferred).")
args = parser.parse_args()
payload = {}
try:
if args.payload_file:
if not os.path.exists(args.payload_file):
raise FileNotFoundError(f"Payload file not found: {args.payload_file}")
with open(args.payload_file, 'r', encoding='utf-8') as f:
payload = json.load(f)
elif args.payload_base64:
payload_str = base64.b64decode(args.payload_base64).decode('utf-8')
payload = json.loads(payload_str)
except (json.JSONDecodeError, base64.binascii.Error) as e:
logging.error(f"Failed to decode payload: {e}")
else:
raise ValueError("No payload provided (neither --payload_file nor --payload_base64).")
except (json.JSONDecodeError, base64.binascii.Error, ValueError, FileNotFoundError) as e:
logging.error(f"Failed to load payload: {e}")
# Print error as JSON to stdout for the server to catch
print(json.dumps({"error": "Invalid payload format.", "details": str(e)}))
print(json.dumps({"error": "Invalid payload.", "details": str(e)}))
sys.exit(1)
# Function mapping to dynamically call the correct phase