rate limit queue, content

This commit is contained in:
overcuriousity
2025-07-28 22:30:33 +02:00
parent 6cdac6ec7c
commit e90da3b2fb
5 changed files with 765 additions and 296 deletions

View File

@@ -998,10 +998,10 @@ document.addEventListener('DOMContentLoaded', () => {
return;
}
// Reset smart prompting when submitting
resetSmartPrompting();
const taskId = `ai_${Date.now()}_${Math.random().toString(36).substr(2, 6)}`;
console.log(`[FRONTEND] Starting AI request with taskId: ${taskId}`);
aiResults.style.display = 'none';
aiError.style.display = 'none';
@@ -1011,7 +1011,7 @@ document.addEventListener('DOMContentLoaded', () => {
const taskIdDisplay = document.getElementById('current-task-id');
if (queueStatus && taskIdDisplay) {
queueStatus.style.display = 'block';
taskIdDisplay.textContent = taskId;
taskIdDisplay.textContent = taskId.slice(-8);
}
aiSubmitBtn.disabled = true;
@@ -1023,64 +1023,90 @@ document.addEventListener('DOMContentLoaded', () => {
const updateQueueStatus = async () => {
try {
const response = await fetch(`/api/ai/queue-status?taskId=${taskId}`);
if (!response.ok) {
console.error(`[FRONTEND] Queue status HTTP error: ${response.status}`);
return;
}
const data = await response.json();
if (data.success) {
const queueLength = document.getElementById('queue-length');
const estimatedTime = document.getElementById('estimated-time');
const positionBadge = document.getElementById('queue-position-badge');
const progressBar = document.getElementById('queue-progress');
if (queueLength) queueLength.textContent = data.queueLength;
if (estimatedTime) {
if (data.estimatedWaitTime > 0) {
estimatedTime.textContent = formatDuration(data.estimatedWaitTime);
} else {
estimatedTime.textContent = 'Verarbeitung läuft...';
}
const queueLength = document.getElementById('queue-length');
const estimatedTime = document.getElementById('estimated-time');
const positionBadge = document.getElementById('queue-position-badge');
const progressBar = document.getElementById('queue-progress');
if (queueLength) {
queueLength.textContent = data.queueLength || 0;
}
if (estimatedTime) {
if (data.estimatedWaitTime > 0) {
estimatedTime.textContent = formatDuration(data.estimatedWaitTime);
} else {
estimatedTime.textContent = 'Verarbeitung läuft...';
}
if (positionBadge && data.currentPosition) {
}
if (positionBadge) {
if (data.currentPosition) {
positionBadge.textContent = data.currentPosition;
if (progressBar && data.queueLength > 0) {
const progress = Math.max(0, ((data.queueLength - data.currentPosition + 1) / data.queueLength) * 100);
progressBar.style.width = `${progress}%`;
}
}
if (data.isProcessing && !data.currentPosition) {
if (positionBadge) positionBadge.textContent = '⚡';
if (progressBar) progressBar.style.width = '100%';
if (estimatedTime) estimatedTime.textContent = 'Verarbeitung läuft...';
} else {
if (data.taskStatus === 'processing') {
positionBadge.textContent = '⚡';
if (progressBar) progressBar.style.width = '100%';
console.log(`[FRONTEND] Task ${taskId.slice(-6)} is processing but no position returned`);
} else if (data.taskStatus === 'completed') {
positionBadge.textContent = '✅';
if (progressBar) progressBar.style.width = '100%';
console.log(`[FRONTEND] Task ${taskId.slice(-6)} completed`);
} else if (data.taskStatus === 'failed') {
positionBadge.textContent = '❌';
if (progressBar) progressBar.style.width = '100%';
console.log(`[FRONTEND] Task ${taskId.slice(-6)} failed`);
} else {
positionBadge.textContent = '?';
if (progressBar) progressBar.style.width = '0%';
console.log(`[FRONTEND] Task ${taskId.slice(-6)} status unknown:`, data.taskStatus);
}
}
}
} catch (error) {
console.warn('Queue status update failed:', error);
console.error('[FRONTEND] Queue status update failed:', error);
}
};
};
const aiRequestPromise = fetch('/api/ai/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
mode: currentMode,
taskId
})
});
updateQueueStatus();
statusInterval = setInterval(updateQueueStatus, 500);
setTimeout(() => {
updateQueueStatus();
statusInterval = setInterval(updateQueueStatus, 1000); // Poll every 1 second for better responsiveness
}, 500);
try {
const response = await fetch('/api/ai/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
mode: currentMode,
taskId
})
});
const response = await aiRequestPromise;
const data = await response.json();
if (statusInterval) clearInterval(statusInterval);
if (statusInterval) {
clearInterval(statusInterval);
console.log(`[FRONTEND] AI request completed for ${taskId.slice(-6)}, stopping status polling`);
}
if (!response.ok) {
throw new Error(data.error || `HTTP ${response.status}`);
@@ -1102,7 +1128,7 @@ document.addEventListener('DOMContentLoaded', () => {
aiResults.style.display = 'block';
} catch (error) {
console.error('AI query failed:', error);
console.error(`[FRONTEND] AI query failed for ${taskId.slice(-6)}:`, error);
if (statusInterval) clearInterval(statusInterval);