diff --git a/main.py b/main.py index 4688576..84ecbad 100644 --- a/main.py +++ b/main.py @@ -508,12 +508,27 @@ async def batch_v2_status(job_id: str, request: Request): job = batch_v2.get_job(job_id) if not job: raise HTTPException(404, "Job not found") + + # If status is still "uploaded" but files exist on disk — Claude finished + # but SSE didn't deliver the done event + if job["status"] == "uploaded" and job.get("job_dir"): + job_dir = Path(job["job_dir"]) + result_data = batch_v2._read_result_files(job_dir) + if result_data.get("script"): + job["analysis"] = { + "python_script": result_data["script"], + "summary": result_data.get("summary", {}), + } + job["status"] = "analyzed" + + analysis = job.get("analysis", {}) return { "status": job["status"], "total": job.get("total_files", 0), "processed": job.get("processed", 0), "errors": job.get("errors", [])[:20], - "script": job.get("analysis", {}).get("python_script", ""), + "script": analysis.get("python_script", ""), + "summary": analysis.get("summary", {}), } diff --git a/static/batch2.html b/static/batch2.html index 44d6a6d..a6c50dc 100644 --- a/static/batch2.html +++ b/static/batch2.html @@ -192,11 +192,37 @@ async function startAnalysis() { } } } catch(e) { - document.getElementById('analyze-spinner').style.display = 'none'; - stopTimer(); - document.getElementById('analyze-status').textContent = 'Ошибка: ' + e.message; - document.getElementById('analyze-status').style.color = 'var(--accent)'; + // Network error or stream closed — ignore, fall through to polling } + + // Fallback: if SSE ended without 'done' event, poll status + if (!document.getElementById('step-confirm').classList.contains('hidden')) return; // already shown + stopTimer(); + document.getElementById('analyze-status').textContent = 'Проверяю результат...'; + for (let i = 0; i < 10; i++) { + await new Promise(r => setTimeout(r, 2000)); + try { + const sr = await fetch('/api/batch-v2/status/' + jobId, {headers: H}); + const sd = await sr.json(); + if (sd.status === 'analyzed' && sd.script) { + document.getElementById('analyze-spinner').style.display = 'none'; + document.getElementById('analyze-status').textContent = 'Анализ завершён'; + document.getElementById('script-code').textContent = sd.script; + // Fetch summary from status if available + renderSummary(sd.summary || {}); + show('step-confirm'); + return; + } else if (sd.status === 'analysis_failed') { + document.getElementById('analyze-spinner').style.display = 'none'; + document.getElementById('analyze-status').textContent = 'Ошибка анализа'; + document.getElementById('analyze-status').style.color = 'var(--accent)'; + return; + } + } catch(e2) {} + } + document.getElementById('analyze-spinner').style.display = 'none'; + document.getElementById('analyze-status').textContent = 'Таймаут — попробуйте ещё раз'; + document.getElementById('analyze-status').style.color = 'var(--accent)'; } async function reanalyze() {