import re with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'r', encoding='utf-8') as f: content = f.read() # 1. Remove dead _rawToFormatted block (lines 1171-1188) # Find and remove the entire block from comment to closing brace pattern = r" // .*?Build format map from chartData.*?\n var _rawToFormatted = \{\};.*?\n \}\n\n // Build series elements" replacement = " // Build series elements" new_content = re.sub(pattern, replacement, content, flags=re.DOTALL) if new_content != content: content = new_content print("CLEANED: Removed dead _rawToFormatted block") else: # Try a simpler pattern idx = content.find("var _rawToFormatted = {};") if idx >= 0: # Go back to find the comment before it comment_start = content.rfind(" // ", 0, idx) # Find the end - next " // Build series elements" end_marker = " // Build series elements" end_idx = content.find(end_marker, idx) if end_idx > 0 and comment_start > 0: content = content[:comment_start] + "\n" + content[end_idx:] print("CLEANED: Removed dead _rawToFormatted block (manual)") else: print(f"WARNING: Could not find boundaries. comment_start={comment_start}, end_idx={end_idx}") else: print("INFO: _rawToFormatted not found") # 2. Remove all [AXIS-DEBUG] console.log from generated HTML # These are in the client-side code embedded in buildChatGPTChartHtml axis_debug_count = len(re.findall(r"console\.log\('\[AXIS-DEBUG\]", content)) if axis_debug_count > 0: # Remove console.log('[AXIS-DEBUG]...'); lines content = re.sub(r" console\.log\('\[AXIS-DEBUG\].*?\);\n", "", content) content = re.sub(r" console\.log\('\[AXIS-DEBUG\].*?\);\n", "", content) print(f"CLEANED: Removed {axis_debug_count} [AXIS-DEBUG] console.log statements") else: print("INFO: No [AXIS-DEBUG] logs found") # 3. Remove [WIDGET] extractChartConfig FAIL logging idx = content.find("console.log(`[WIDGET] extractChartConfig: FAIL") if idx >= 0: line_end = content.find(";", idx) + 1 content = content[:idx] + content[line_end:] print("CLEANED: Removed extractChartConfig FAIL logging") else: print("INFO: extractChartConfig FAIL logging not found") # 4. Remove redundant const fs = require inside renderChatGPTChart # Check for local fs requires (should not exist since fs is at top) fs_local_count = len(re.findall(r"const fs = require\('fs'\)", content)) if fs_local_count > 1: # Keep the first (top-level), remove duplicates first = content.find("const fs = require('fs')") content = content[:first] + "__FS_MARKER__" + content[first+len("const fs = require('fs')"):] content = content.replace("const fs = require('fs')", "") content = content.replace("__FS_MARKER__", "const fs = require('fs')") print(f"CLEANED: Removed {fs_local_count - 1} duplicate fs requires") else: print(f"INFO: fs requires count = {fs_local_count} (OK)") # 5. Clean up the comment about Chart.js detection (remove redundant pattern) # The new\s+C\s*\( pattern double-counts pattern = r"\(widgetHtml\.match\(/new\\s\+C\\s\*\\\(/gi\) \|\| \[\]\)\.length \+\s*\n\s*" if re.search(pattern, content): content = re.sub(pattern, "", content) print("CLEANED: Removed double-count new C( regex") else: print("INFO: Double-count regex not found") # 6. Remove the pieActualDataKey dead variable content = content.replace("var pieActualDataKey = valueKey || chartSeries[0]?.dataKey || 'value';\n", "") print("CLEANED: Removed dead pieActualDataKey (second pass)") with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'w', encoding='utf-8') as f: f.write(content) print("\nDone.")