Now fix the AI API response handling to catch non-JSON responses (Fix 4). Let me find that section:


1890:                const response = await fetch('../aiapi/api_opus/api/query.php?action=providers');
2318:                var response = await fetch('../aiapi/api_opus/api/query.php', {


  2302→                console.log('======================');
  2303→
  2304→                document.getElementById('loading-message').textContent = 'Analyzing with ' + selectedProvidersList.length + ' AI provider(s)...';
  2305→
  2306→                // Build request body with all selected providers and their models
  2307→                var requestBody = {
  2308→                    user_content: fullContent,
  2309→                    providers: selectedProvidersList
  2310→                };
  2311→
  2312→                // Add model for each provider
  2313→                for (var provider in modelsMap) {
  2314→                    requestBody['model_' + provider] = modelsMap[provider];
  2315→                }
  2316→

                console.log('======================');

                document.getElementById('loading-message').textContent = 'Analyzing with ' + selectedProvidersList.length + ' AI provider(s)...';

                // Build request body with all selected providers and their models
                var requestBody = {
                    user_content: fullContent,
                    providers: selectedProvidersList
                };

                // Add model for each provider
                for (var provider in modelsMap) {
                    requestBody['model_' + provider] = modelsMap[provider];
                }


  2316→
  2317→                // Send to AI API
  2318→                var response = await fetch('../aiapi/api_opus/api/query.php', {
  2319→                    method: 'POST',
  2320→                    headers: { 'Content-Type': 'application/json' },
  2321→                    body: JSON.stringify(requestBody)
  2322→                });
  2323→
  2324→                var data = await response.json();
  2325→
  2326→                // Log response to debug file
  2327→                var responsePreview = '';
  2328→                if (data.responses) {
  2329→                    for (var rk in data.responses) {
  2330→                        if (data.responses[rk] && data.responses[rk].response) {
  2331→                            responsePreview = data.responses[rk].response.substring(0, 500);
  2332→                            break;
  2333→                        }
  2334→                    }
  2335→                }
  2336→                logAiDebug({
  2337→                    type: 'response',
  2338→                    thread_id: currentThreadId,
  2339→                    agent: selectedAgent,
  2340→                    providers: selectedProvidersList,
  2341→                    models: modelsMap,
  2342→                    estimated_tokens: finalTokens,
  2343→                    question: actualQuestion,
  2344→                    response: responsePreview
  2345→                });

                // Send to AI API
                var response = await fetch('../aiapi/api_opus/api/query.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(requestBody)
                });

                var data = await response.json();

                // Log response to debug file
                var responsePreview = '';
                if (data.responses) {
                    for (var rk in data.responses) {
                        if (data.responses[rk] && data.responses[rk].response) {
                            responsePreview = data.responses[rk].response.substring(0, 500);
                            break;
                        }
                    }
                }
                logAiDebug({
                    type: 'response',
                    thread_id: currentThreadId,
                    agent: selectedAgent,
                    providers: selectedProvidersList,
                    models: modelsMap,
                    estimated_tokens: finalTokens,
                    question: actualQuestion,
                    response: responsePreview
                });


Edit

                // Send to AI API
                var response = await fetch('../aiapi/api_opus/api/query.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(requestBody)
                });

                var data = await response.json();

                // Show truncation notice if applicable
                if (wasTruncated) {
                    document.getElementById('loading-message').textContent = 'Content truncated to ' + finalTokens.toLocaleString() + ' tokens. Sending to ' + selectedProvidersList.length + ' AI(s)...';
                } else {
                    document.getElementById('loading-message').textContent = 'Sending ~' + finalTokens.toLocaleString() + ' tokens to ' + selectedProvidersList.length + ' AI provider(s)...';
                }

                // Send to AI API
                var response = await fetch('../aiapi/api_opus/api/query.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(requestBody)
                });

                if (!response.ok) {
                    var errText = '';
                    try { errText = await response.text(); } catch(e) {}
                    throw new Error('AI API returned HTTP ' + response.status + (errText ? ': ' + errText.substring(0, 200) : ''));
                }

                var responseText = await response.text();
                var data;
                try {
                    data = JSON.parse(responseText);
                } catch (parseErr) {
                    throw new Error('AI API returned invalid JSON: ' + responseText.substring(0, 200));
                }