fix(audit): corrigindo lógica da barra de progresso e botão de cancelar

This commit is contained in:
2026-07-11 21:29:05 +00:00
parent 513f9f86d4
commit 77b70db65a
+37 -16
View File
@@ -212,29 +212,50 @@ export async function runAudit(
onProgress?: (progress: number) => void, onProgress?: (progress: number) => void,
): Promise<AuditReport> { ): Promise<AuditReport> {
onProgress?.(0); onProgress?.(0);
const maxRetries = 2; const maxRetriesPerChunk = 2;
const chunkSize = Math.ceil(scenarios.length / 1); const chunkSize = 10; // Enviar 10 cenários por vez é muito mais seguro
let lastError: Error | null = null; const allLlmResults = [];
for (let retry = 0; retry <= maxRetries; retry++) { let completed = 0;
let rawResponseFull = '';
for (let i = 0; i < scenarios.length; i += chunkSize) {
const chunk = scenarios.slice(i, i + chunkSize);
let chunkSuccess = false;
let chunkError: Error | null = null;
for (let retry = 0; retry <= maxRetriesPerChunk; retry++) {
try { try {
const prompt = buildPrompt(scenarios); const prompt = buildPrompt(chunk);
const rawResponse = await runLLMAudit(config, prompt.system, prompt.user, () => { // Avisar a UI do progresso absoluto
onProgress?.(Math.round((retry * chunkSize) / maxRetries)); onProgress?.(completed);
const rawResponse = await runLLMAudit(config, prompt.system, prompt.user, (msg) => {
// Chamamos o onProgress para disparar a verificação de abortar do AuditPanel
onProgress?.(completed);
}); });
const llmResults = parseLLMResponse(rawResponse, scenarios.map(s => s.id)); rawResponseFull += rawResponse + '\n\n';
const scenarioMap = new Map(scenarios.map(s => [s.id, { module: s.module, moduleLabel: s.moduleLabel }]));
const report = buildAuditReport(llmResults, scenarioMap, config.provider, config.model, rawResponse); const llmResults = parseLLMResponse(rawResponse, chunk.map(s => s.id));
onProgress?.(scenarios.length); allLlmResults.push(...llmResults);
return report; chunkSuccess = true;
break; // Sucesso, sai do retry loop
} catch (e) { } catch (e) {
lastError = e instanceof Error ? e : new Error(String(e)); chunkError = e instanceof Error ? e : new Error(String(e));
if (retry < maxRetries) { console.warn(`Erro no chunk ${i} (tentativa ${retry}):`, chunkError);
onProgress?.(0);
} }
} }
if (!chunkSuccess) {
throw chunkError ?? new Error(`Falha ao auditar cenários no índice ${i} após retentativas.`);
} }
throw lastError ?? new Error('Audit failed after retries'); completed += chunk.length;
onProgress?.(completed);
}
const scenarioMap = new Map(scenarios.map(s => [s.id, { module: s.module, moduleLabel: s.moduleLabel }]));
const report = buildAuditReport(allLlmResults, scenarioMap, config.provider, config.model, rawResponseFull);
return report;
} }
async function fetchWithTimeout( async function fetchWithTimeout(