File size: 5,400 Bytes
24a2ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 <div> which breaks':
        continue
    if 'the complete HTML document (script tags in <head> 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")