File size: 8,999 Bytes
d6b3b55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Initialize Socket.IO connection
const socket = io();

// DOM Elements
const problemInput = document.getElementById('problemInput');
const providerSelect = document.getElementById('providerSelect');
const maxRunsInput = document.getElementById('maxRunsInput');
const numAgentsInput = document.getElementById('numAgentsInput');
const otherPromptsInput = document.getElementById('otherPromptsInput');
const solveButton = document.getElementById('solveButton');

const statusSection = document.getElementById('statusSection');
const statusText = document.getElementById('statusText');
const sessionId = document.getElementById('sessionId');
const providerInfo = document.getElementById('providerInfo');
const agentsInfo = document.getElementById('agentsInfo');

const logSection = document.getElementById('logSection');
const logOutput = document.getElementById('logOutput');

const solutionSection = document.getElementById('solutionSection');
const solutionOutput = document.getElementById('solutionOutput');
const solutionLogPath = document.getElementById('solutionLogPath');

const errorSection = document.getElementById('errorSection');
const errorOutput = document.getElementById('errorOutput');

let currentSessionId = null;

// Event Listeners
solveButton.addEventListener('click', solveProblem);

// Socket event handlers
socket.on('connect', () => {
    console.log('Connected to server');
});

socket.on('status', (data) => {
    if (data.session_id === currentSessionId) {
        updateStatus(data.message);
    }
});

socket.on('log', (data) => {
    if (data.session_id === currentSessionId) {
        appendLog(data.line);
    }
});

socket.on('complete', (data) => {
    if (data.session_id === currentSessionId) {
        handleCompletion(data);
    }
});

socket.on('error', (data) => {
    if (data.session_id === currentSessionId) {
        handleError(data.error);
    }
});

// Functions
async function solveProblem() {
    const problem = problemInput.value.trim();

    if (!problem) {
        alert('Please enter a problem statement');
        return;
    }

    // Disable button and show status
    solveButton.disabled = true;
    solveButton.innerHTML = '<div class="spinner"></div> Solving...';

    // Hide previous results
    solutionSection.style.display = 'none';
    errorSection.style.display = 'none';

    // Show status and log sections
    statusSection.style.display = 'block';
    logSection.style.display = 'block';

    // Clear log
    logOutput.innerHTML = '';

    // Generate session ID
    currentSessionId = Date.now().toString();

    // Update info
    sessionId.textContent = currentSessionId;
    providerInfo.textContent = providerSelect.options[providerSelect.selectedIndex].text;
    agentsInfo.textContent = numAgentsInput.value;

    try {
        const response = await fetch('/api/solve', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                problem: problem,
                provider: providerSelect.value,
                max_runs: parseInt(maxRunsInput.value),
                num_agents: parseInt(numAgentsInput.value),
                other_prompts: otherPromptsInput.value,
                session_id: currentSessionId
            })
        });

        const data = await response.json();

        if (!response.ok) {
            throw new Error(data.error || 'Failed to start solving');
        }

        updateStatus('Problem submitted successfully');
        appendLog(`Session ${data.session_id} started`);

    } catch (error) {
        handleError(error.message);
        solveButton.disabled = false;
        solveButton.innerHTML = `
            <svg class="button-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polygon points="5 3 19 12 5 21 5 3"/>
            </svg>
            Solve Problem
        `;
    }
}

function updateStatus(message) {
    statusText.textContent = message;
}

function appendLog(line) {
    if (!line.trim()) return;

    const logLine = document.createElement('div');
    logLine.className = 'log-line';

    // Add special styling for important lines
    if (line.includes('SOLUTION FOUND') || line.includes('Found a correct solution')) {
        logLine.classList.add('success');
    } else if (line.includes('ERROR') || line.includes('FAILED')) {
        logLine.classList.add('error');
    } else if (line.includes('WARNING')) {
        logLine.classList.add('warning');
    }

    logLine.textContent = line;
    logOutput.appendChild(logLine);

    // Auto-scroll to bottom
    logOutput.scrollTop = logOutput.scrollHeight;
}

function handleCompletion(data) {
    // Re-enable button
    solveButton.disabled = false;
    solveButton.innerHTML = `
        <svg class="button-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polygon points="5 3 19 12 5 21 5 3"/>
        </svg>
        Solve Problem
    `;

    // Update status
    if (data.solution_found) {
        updateStatus('Solution found!');

        // Show solution section
        solutionSection.style.display = 'block';
        solutionOutput.textContent = data.solution;
        solutionLogPath.textContent = `Log file: ${data.log_file}`;

        // Add success message to log
        appendLog('='.repeat(60));
        appendLog('SOLUTION FOUND!');
        appendLog('='.repeat(60));
    } else {
        updateStatus('Completed - No solution found');

        // Add completion message to log
        appendLog('='.repeat(60));
        appendLog('Process completed without finding a solution');
        appendLog('Check the log above for partial results or insights');
        appendLog('='.repeat(60));
    }
}

function handleError(errorMessage) {
    // Re-enable button
    solveButton.disabled = false;
    solveButton.innerHTML = `
        <svg class="button-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polygon points="5 3 19 12 5 21 5 3"/>
        </svg>
        Solve Problem
    `;

    // Update status
    updateStatus('Error occurred');

    // Show error section
    errorSection.style.display = 'block';
    errorOutput.textContent = errorMessage;

    // Add to log
    appendLog('ERROR: ' + errorMessage);
}

// Add keyboard shortcut (Ctrl+Enter or Cmd+Enter to solve)
problemInput.addEventListener('keydown', (e) => {
    if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
        e.preventDefault();
        solveProblem();
    }
});

// Add sample problems
const sampleProblems = [
    `Let n be a positive integer. There are n red cards and n blue cards. Initially, every red card has the real number 0 written on it, and every blue card has the real number 1 written on it.
An operation consists of selecting one red card and one blue card such that the number x on the red card is strictly less than the number y on the blue card, erasing both numbers, and replacing them both with the average (x+y)/2.
Find the minimum positive integer n such that it is possible, using a finite sequence of these operations, to make the sum of the numbers on the n red cards strictly greater than 100.`,

    `Find all prime numbers p for which there exist non-negative integers x, y, and z such that p = x^3 + y^3 + z^3.`,

    `Determine all positive integers n for which there exists a permutation (a₁, a₂, ..., aₙ) of (1, 2, ..., n) such that for each i ∈ {1, 2, ..., n}, the sum a₁ + a₂ + ... + aᵢ is divisible by i.`
];

// Add sample problem selector (optional feature)
const createSampleSelector = () => {
    const sampleSelector = document.createElement('div');
    sampleSelector.className = 'control-group full-width';
    sampleSelector.innerHTML = `
        <label class="control-label">
            <svg class="label-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"/>
            </svg>
            Or try a sample problem:
        </label>
        <select id="sampleSelect" class="control-select">
            <option value="">-- Select a sample problem --</option>
            <option value="0">IMO-style: Red and Blue Cards</option>
            <option value="1">Number Theory: Prime Sums</option>
            <option value="2">Combinatorics: Permutation Divisibility</option>
        </select>
    `;

    const controls = document.querySelector('.controls');
    controls.insertBefore(sampleSelector, controls.firstChild);

    document.getElementById('sampleSelect').addEventListener('change', (e) => {
        const index = parseInt(e.target.value);
        if (!isNaN(index) && index >= 0 && index < sampleProblems.length) {
            problemInput.value = sampleProblems[index];
        }
    });
};

// Initialize sample selector when page loads
window.addEventListener('load', createSampleSelector);