This commit is contained in:
root
2026-05-28 15:40:02 +00:00
parent 81913f3d8d
commit cc1ea166c8
5 changed files with 75 additions and 40 deletions

View File

@@ -1,6 +1,7 @@
import asyncio
import json
import re
import shutil
import subprocess
import tempfile
import uuid
@@ -22,6 +23,7 @@ from database import (
list_bausteine,
update_baustein,
)
from paths import final_paths, temp_paths
_semaphore = asyncio.Semaphore(MAX_CONCURRENT_GENERATIONS)
_active_processes: dict[str, asyncio.subprocess.Process] = {}
@@ -174,8 +176,7 @@ async def generate_guide(guide_id: str, topic: str, format_name: str, instructio
now = datetime.now(timezone.utc).isoformat()
await update_guide(guide_id, status="generating", progress="Recherche…", updated_at=now)
html_path = STORAGE_DIR / "html" / f"{guide_id}.html"
pdf_path = STORAGE_DIR / "pdf" / f"{guide_id}.pdf"
html_path, pdf_path = final_paths(topic, format_name)
try:
if guide_id in _cancelled:
@@ -241,12 +242,7 @@ async def generate_guide(guide_id: str, topic: str, format_name: str, instructio
now = datetime.now(timezone.utc).isoformat()
await update_guide(
guide_id,
status="done",
progress=None,
html_path=str(html_path),
pdf_path=str(pdf_path),
updated_at=now,
guide_id, status="done", progress=None, updated_at=now,
)
except asyncio.TimeoutError:
@@ -263,16 +259,23 @@ async def rework_guide(guide_id: str, topic: str, format_name: str, instructions
now = datetime.now(timezone.utc).isoformat()
await update_guide(guide_id, status="generating", progress="Überarbeite…", updated_at=now)
html_path = STORAGE_DIR / "html" / f"{guide_id}.html"
pdf_path = STORAGE_DIR / "pdf" / f"{guide_id}.pdf"
final_html, final_pdf = final_paths(topic, format_name)
tmp_html, tmp_pdf = temp_paths(guide_id)
try:
if guide_id in _cancelled:
return
if not final_html.exists():
await _fail(guide_id, "Original-HTML nicht gefunden")
return
shutil.copy2(final_html, tmp_html)
current_step = "Überarbeitung"
current_timeout = AGENT_TIMEOUT
rework_prompt = _build_rework_prompt(topic, format_name, html_path, instructions)
rework_prompt = _build_rework_prompt(topic, format_name, tmp_html, instructions)
returncode, stdout, stderr = await _run_claude(guide_id, rework_prompt, AGENT_TIMEOUT)
if guide_id in _cancelled:
@@ -281,20 +284,23 @@ async def rework_guide(guide_id: str, topic: str, format_name: str, instructions
await _fail(guide_id, f"Rework-Fehler: {stderr[:1000]}")
return
if not html_path.exists():
if not tmp_html.exists():
await _fail(guide_id, "HTML-Datei wurde nicht erstellt")
return
await _set_progress(guide_id, "Rendere PDF…")
ok, err = await _render_pdf(html_path, pdf_path)
ok, err = await _render_pdf(tmp_html, tmp_pdf)
if not ok:
await _fail(guide_id, f"WeasyPrint-Fehler: {err}")
return
# Atomar: Temp → Final umbenennen
tmp_html.replace(final_html)
tmp_pdf.replace(final_pdf)
now = datetime.now(timezone.utc).isoformat()
await update_guide(
guide_id, status="done", progress=None,
html_path=str(html_path), pdf_path=str(pdf_path), updated_at=now,
guide_id, status="done", progress=None, updated_at=now,
)
except asyncio.TimeoutError:
@@ -304,6 +310,8 @@ async def rework_guide(guide_id: str, topic: str, format_name: str, instructions
finally:
_active_processes.pop(guide_id, None)
_cancelled.discard(guide_id)
tmp_html.unlink(missing_ok=True)
tmp_pdf.unlink(missing_ok=True)
async def _fail(guide_id: str, msg: str) -> None: