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 lines2526272829303132333429 unmodified lines6465666768691 unmodified line717273747576777824 unmodified linesconst [previousPrompt, setPreviousPrompt] = useState("");const callAi = async () => {if (isAiWorking) return;setisAiWorking(true);let contentResponse = "";try {const request = await fetch("/api/ask-ai", {method: "POST",29 unmodified linessetPreviousPrompt(prompt);setisAiWorking(false);setHasAsked(true);return;}1 unmodified linecontentResponse += chunk;const newHtml = contentResponse.match(/<!DOCTYPE html>[\s\S]*/)?.[0];if (newHtml) {setHtml(newHtml);if (newHtml?.length > 200) {onScrollToBottom();}}24 unmodified lines252627282930313233343529 unmodified lines656667686970717273747576771 unmodified line798081828384858687888990919293949596979824 unmodified linesconst [previousPrompt, setPreviousPrompt] = useState("");const callAi = async () => {if (isAiWorking || !prompt.trim()) return;setisAiWorking(true);let contentResponse = "";let lastRenderTime = 0;try {const request = await fetch("/api/ask-ai", {method: "POST",29 unmodified linessetPreviousPrompt(prompt);setisAiWorking(false);setHasAsked(true);// Now we have the complete HTML including </html>, so set it to be sureconst finalDoc = contentResponse.match(/<!DOCTYPE html>[\s\S]*<\/html>/)?.[0];if (finalDoc) {setHtml(finalDoc);}return;}1 unmodified linecontentResponse += chunk;const newHtml = contentResponse.match(/<!DOCTYPE html>[\s\S]*/)?.[0];if (newHtml) {// Force-close the HTML tag so the iframe doesn't render half-finished markuplet partialDoc = newHtml;if (!partialDoc.includes("</html>")) {partialDoc += "\n</html>";}// Throttle the re-renders to avoid flashing/flickerconst 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