Spaces:
Sleeping
Sleeping
toto (#2)
Browse files- refactor: Improve AI prompt for better HTML generation and code structure (891e1dcde5faaac21c71dfb80023877f738e1127)
Co-authored-by: Victor Mustar <victor@users.noreply.huggingface.co>
24 unmodified lines25262728282930313233343529 unmodified lines656667686970717273747576771 unmodified line7980817475828384858687888990919293949596979824 unmodified lines const [previousPrompt, setPreviousPrompt] = useState("");
const callAi = async () => { if (isAiWorking) return; if (isAiWorking || !prompt.trim()) return; setisAiWorking(true);
let contentResponse = ""; let lastRenderTime = 0; try { const request = await fetch("/api/ask-ai", { method: "POST",29 unmodified lines setPreviousPrompt(prompt); setisAiWorking(false); setHasAsked(true); // Now we have the complete HTML including </html>, so set it to be sure const finalDoc = contentResponse.match(/<!DOCTYPE html>[\s\S]*<\/html>/)?.[0]; if (finalDoc) { setHtml(finalDoc); } return; }
1 unmodified line contentResponse += chunk; const newHtml = contentResponse.match(/<!DOCTYPE html>[\s\S]*/)?.[0]; if (newHtml) { setHtml(newHtml); if (newHtml?.length > 200) { // Force-close the HTML tag so the iframe doesn't render half-finished markup let partialDoc = newHtml; if (!partialDoc.includes("</html>")) { partialDoc += "\n</html>"; }
// Throttle the re-renders to avoid flashing/flicker const now = Date.now(); if (now - lastRenderTime > 300) { setHtml(partialDoc); lastRenderTime = now; }
if (partialDoc.length > 200) { onScrollToBottom(); } }src/components/ask-ai/ask-ai.tsx+23-3