async function go(prompt) { const cont = document.getElementById('content'), choi = document.getElementById('choices'), imgBox = document.getElementById('image-container'), imgStatus = document.getElementById('img-status'); choi.innerHTML = '
Kirjoitetaan...
'; imgBox.style.display = "flex"; imgStatus.innerText = "Kuva latautuu..."; const oldImg = imgBox.querySelector('img'); if(oldImg) oldImg.remove(); history.push({role: 'user', content: prompt}); try { // 1. Haetaan tarina const textData = await call(history); const storyText = textData.text; history.push({role: 'assistant', content: storyText}); // 2. Haetaan kuva HETI perään (ei odoteta tekstin kirjoitusta) fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ type: 'image', prompt: storyText.substring(0, 300) }) }).then(r => r.json()).then(imgData => { if(imgData.imageUrl) { const img = document.createElement('img'); img.src = imgData.imageUrl; img.onload = () => { imgStatus.style.display = "none"; }; imgBox.appendChild(img); } }); lueAaneen(storyText); typeOut(cont, storyText, async () => { // ... (jatkovalinnat kuten aiemmin) }); } catch(e) { choi.innerHTML = `
Virhe: ${e.message}
`; } }