diff --git a/script.html b/script.html index c066ee0..6f90f17 100644 --- a/script.html +++ b/script.html @@ -366,6 +366,21 @@ function bindPics() { if (!PICS.length) { showMsg("msg", "Add a picture first."); return; } sg.disabled = true; sg.textContent = "Reading the script…"; var r = await api("suggest_image_placements", { email: email, project_id: pid }); + if (!r.ok) { sg.disabled = false; sg.textContent = "Suggest where they fit"; showMsg("msg", r.error || "Could not suggest."); return; } + // Reading the script is a model call, far too slow to answer inside one + // request, so it runs in the background and we wait for it here. + if (r.pending) { + document.getElementById("sugnote").textContent = r.status_text || "Reading the script…"; + var waited = 0; + r = await new Promise(function (done) { + var t = setInterval(async function () { + waited += 4; + var g = await api("suggest_image_placements", { email: email, project_id: pid }); + if (g.ok && !g.pending) { clearInterval(t); done(g); return; } + if (waited > 150) { clearInterval(t); done({ ok: false, error: "That is taking unusually long." }); } + }, 4000); + }); + } sg.disabled = false; sg.textContent = "Suggest where they fit"; document.getElementById("sugnote").textContent = r.status_text || ""; if (!r.ok) { showMsg("msg", r.error || "Could not suggest."); return; }