import re with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'r', encoding='utf-8') as f: lines = f.readlines() new_lines = [] skip_until = -1 # Line index to skip until for i, line in enumerate(lines): # Skip lines until skip_until if i < skip_until: continue # 1. Remove dead baseWaitTime/sizeWaitTime/totalWaitTime (lines ~240-242) if 'const baseWaitTime = imgCount > 0' in line: continue if 'const sizeWaitTime = imgSizeMB > 0' in line: continue if 'const totalWaitTime = Math.min(baseWaitTime' in line: continue # 2. Remove /tmp debug HTML write block (lines ~943-946) if line.strip() == '// DEBUG: Write HTML to disk for inspection': continue if line.strip() == "const fs = require('fs');" and i > 900 and i < 1000: # Only remove the one inside renderChatGPTChart, not the top-level import # Check context: previous line should mention DEBUG if i > 0 and 'DEBUG' in lines[i-1]: continue else: new_lines.append(line) continue if 'fs.writeFileSync(`/tmp/chart-debug-' in line: continue if line.strip() == '// DO NOT use renderWidgetPuppeteer — it wraps content in a
which breaks': continue if 'the complete HTML document (script tags in won' in line: continue if '// Instead, use page.setContent directly with the full HTML.' in line: continue # 3. Remove debugId from _renderFullHtml call and signature if 'return this._renderFullHtml(html, chartTitle, 8000, chartType, debugId)' in line: new_lines.append(line.replace(', debugId', '')) continue if 'async _renderFullHtml(htmlContent, title, renderTimeout, chartType, debugId)' in line: new_lines.append(line.replace(', debugId', '')) continue # 4. Remove viewport comment + CDN + console forwarding if '// CRITICAL: Use a compact viewport height to avoid excessive whitespace' in line: continue if '// in fullPage screenshots. Width 800 gives enough room for 650px chart.' in line: continue if '// CDN failure listener for debugging' in line: # Skip this line and the next 3 lines skip_until = i + 4 continue if '// Forward page console logs to Node.js console for debug' in line: # Skip this line and the next 6 lines skip_until = i + 7 continue if '// Set complete HTML document directly' in line: continue # 5. Remove containerInfo debug block if '// ── DEBUG: Log what\'s actually rendered in the container' in line: # Skip until the closing console.log of this block skip_until = i + 1 # Find the end - console.log line with containerInfo while skip_until < len(lines) and 'container info:' not in lines[skip_until]: skip_until += 1 skip_until += 1 # Skip the console.log line too continue # 6. Remove line chart debug block if '// ── DEBUG: For line charts, verify SVG path contains all data points' in line: skip_until = i + 1 while skip_until < len(lines) and 'LINE DEBUG:' not in lines[skip_until]: skip_until += 1 skip_until += 1 continue # 7. Remove pie chart debug block if '// ── DEBUG: For pie charts, verify the actual SVG output' in line: skip_until = i + 1 while skip_until < len(lines) and 'PIE DEBUG:' not in lines[skip_until]: skip_until += 1 skip_until += 1 continue # 8. Remove debug screenshot block if '// ── DEBUG: Save screenshot to disk for visual verification' in line: skip_until = i + 1 while skip_until < len(lines) and 'Use element screenshot' not in lines[skip_until]: skip_until += 1 # Now skip until the dataUrl line while skip_until < len(lines) and 'const dataUrl' not in lines[skip_until]: skip_until += 1 continue # 9. Remove PNG dimension parsing if '// Parse PNG dimensions for verification' in line: skip_until = i + 1 while skip_until < len(lines) and 'PNG dimensions:' not in lines[skip_until]: skip_until += 1 skip_until += 1 continue # 10. Remove debugId screenshot write (independent block) if 'if (debugId) {' in line and i > 1050: # Skip this if block and its contents skip_until = i + 1 brace_count = 1 while skip_until < len(lines) and brace_count > 0: if '{' in lines[skip_until]: brace_count += lines[skip_until].count('{') if '}' in lines[skip_until]: brace_count -= lines[skip_until].count('}') skip_until += 1 continue new_lines.append(line) # Verify we didn't destroy the file result = ''.join(new_lines) if 'buildChatGPTChartHtml' in result and 'render_charts' in result: with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'w', encoding='utf-8') as f: f.write(result) print(f"SUCCESS: Cleaned file from {len(lines)} to {len(new_lines)} lines") print(f"Verified: buildChatGPTChartHtml and render_charts present") else: print("ERROR: Critical functions missing! NOT saving file.") print(f"Result length: {len(result)} chars")