fix(audit): corrigindo lógica da barra de progresso e botão de cancelar
This commit is contained in:
+41
-20
@@ -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;
|
||||||
try {
|
let rawResponseFull = '';
|
||||||
const prompt = buildPrompt(scenarios);
|
|
||||||
const rawResponse = await runLLMAudit(config, prompt.system, prompt.user, () => {
|
for (let i = 0; i < scenarios.length; i += chunkSize) {
|
||||||
onProgress?.(Math.round((retry * chunkSize) / maxRetries));
|
const chunk = scenarios.slice(i, i + chunkSize);
|
||||||
});
|
let chunkSuccess = false;
|
||||||
const llmResults = parseLLMResponse(rawResponse, scenarios.map(s => s.id));
|
let chunkError: Error | null = null;
|
||||||
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);
|
for (let retry = 0; retry <= maxRetriesPerChunk; retry++) {
|
||||||
onProgress?.(scenarios.length);
|
try {
|
||||||
return report;
|
const prompt = buildPrompt(chunk);
|
||||||
} catch (e) {
|
// Avisar a UI do progresso absoluto
|
||||||
lastError = e instanceof Error ? e : new Error(String(e));
|
onProgress?.(completed);
|
||||||
if (retry < maxRetries) {
|
|
||||||
onProgress?.(0);
|
const rawResponse = await runLLMAudit(config, prompt.system, prompt.user, (msg) => {
|
||||||
|
// Chamamos o onProgress para disparar a verificação de abortar do AuditPanel
|
||||||
|
onProgress?.(completed);
|
||||||
|
});
|
||||||
|
rawResponseFull += rawResponse + '\n\n';
|
||||||
|
|
||||||
|
const llmResults = parseLLMResponse(rawResponse, chunk.map(s => s.id));
|
||||||
|
allLlmResults.push(...llmResults);
|
||||||
|
chunkSuccess = true;
|
||||||
|
break; // Sucesso, sai do retry loop
|
||||||
|
} catch (e) {
|
||||||
|
chunkError = e instanceof Error ? e : new Error(String(e));
|
||||||
|
console.warn(`Erro no chunk ${i} (tentativa ${retry}):`, chunkError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!chunkSuccess) {
|
||||||
|
throw chunkError ?? new Error(`Falha ao auditar cenários no índice ${i} após retentativas.`);
|
||||||
|
}
|
||||||
|
completed += chunk.length;
|
||||||
|
onProgress?.(completed);
|
||||||
}
|
}
|
||||||
throw lastError ?? new Error('Audit failed after retries');
|
|
||||||
|
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user