Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Fix video mode preview not showing generated code

Abi Rajafb0d2dc1a6c7
diff --git a/backend/codegen/utils.py b/backend/codegen/utils.py
index 107579b8..4b983973 100644
--- a/backend/codegen/utils.py
+++ b/backend/codegen/utils.py
@@ -2,13 +2,29 @@ import re
def extract_html_content(text: str):
- # Use regex to find content within <html> tags and include the tags themselves
+ # First, strip markdown code fences if present
+ text = re.sub(r'^```html?\s*\n?', '', text, flags=re.MULTILINE)
+ text = re.sub(r'\n?```\s*$', '', text, flags=re.MULTILINE)
+
+ # Try to find DOCTYPE + html tags together
+ match_with_doctype = re.search(
+ r"(<!DOCTYPE\s+html[^>]*>.*?<html.*?>.*?</html>)", text, re.DOTALL | re.IGNORECASE
+ )
+ if match_with_doctype:
+ extracted = match_with_doctype.group(1)
+ print(f"[HTML Extraction] Successfully extracted {len(extracted)} chars of HTML (with DOCTYPE)")
+ return extracted
+
+ # Fall back to just <html> tags
match = re.search(r"(<html.*?>.*?</html>)", text, re.DOTALL)
if match:
- return match.group(1)
+ extracted = match.group(1)
+ print(f"[HTML Extraction] Successfully extracted {len(extracted)} chars of HTML")
+ return extracted
else:
# Otherwise, we just send the previous HTML over
print(
- "[HTML Extraction] No <html> tags found in the generated content: " + text
+ "[HTML Extraction] No <html> tags found in the generated content (length: "
+ + str(len(text)) + " chars). First 500 chars: " + text[:500]
)
return text
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index 757d4239..9a723c63 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -797,6 +797,11 @@ class ParallelGenerationStage:
completion = await task
print(f"{model.value} completion took {completion['duration']:.2f} seconds")
+ print(f"[DEBUG] Completion code length: {len(completion['code'])} chars")
+ if len(completion['code']) < 200:
+ print(f"[DEBUG] Full completion: {completion['code']}")
+ else:
+ print(f"[DEBUG] First 200 chars: {completion['code'][:200]}")
variant_completions[index] = completion["code"]
try:
diff --git a/frontend/src/components/preview/extractHtml.ts b/frontend/src/components/preview/extractHtml.ts
index 6e9c9ffa..b2090caf 100644
--- a/frontend/src/components/preview/extractHtml.ts
+++ b/frontend/src/components/preview/extractHtml.ts
@@ -1,6 +1,12 @@
-// Not robust enough to support <html lang='en'> for instance
+// Extract HTML content, supporting <html> tags with attributes like <html lang="en">
export function extractHtml(code: string): string {
- const lastHtmlStartIndex = code.lastIndexOf("<html>");
+ // Use regex to find <html> tag with optional attributes
+ const htmlStartMatch = code.match(/<html[^>]*>/i);
+ if (!htmlStartMatch) {
+ return "";
+ }
+
+ const lastHtmlStartIndex = code.lastIndexOf(htmlStartMatch[0]);
let htmlEndIndex = code.indexOf("</html>", lastHtmlStartIndex);
if (lastHtmlStartIndex !== -1) {
diff --git a/frontend/src/generateCode.ts b/frontend/src/generateCode.ts
index 410b0216..d8decc74 100644
--- a/frontend/src/generateCode.ts
+++ b/frontend/src/generateCode.ts
@@ -54,23 +54,28 @@ export function generateCode(
ws.addEventListener("message", async (event: MessageEvent) => {
const response = JSON.parse(event.data) as WebSocketResponse;
- if (response.type === "chunk") {
- callbacks.onChange(response.value, response.variantIndex);
- } else if (response.type === "status") {
- callbacks.onStatusUpdate(response.value, response.variantIndex);
- } else if (response.type === "setCode") {
- callbacks.onSetCode(response.value, response.variantIndex);
- } else if (response.type === "variantComplete") {
- callbacks.onVariantComplete(response.variantIndex);
- } else if (response.type === "variantError") {
- callbacks.onVariantError(response.variantIndex, response.value);
- } else if (response.type === "variantCount") {
- callbacks.onVariantCount(parseInt(response.value));
- } else if (response.type === "thinking") {
- callbacks.onThinking(response.value, response.variantIndex);
- } else if (response.type === "error") {
- console.error("Error generating code", response.value);
- toast.error(response.value);
+ try {
+ if (response.type === "chunk") {
+ callbacks.onChange(response.value, response.variantIndex);
+ } else if (response.type === "status") {
+ callbacks.onStatusUpdate(response.value, response.variantIndex);
+ } else if (response.type === "setCode") {
+ console.log(`[WS] Received setCode for variant ${response.variantIndex}, code length: ${response.value.length}`);
+ callbacks.onSetCode(response.value, response.variantIndex);
+ } else if (response.type === "variantComplete") {
+ callbacks.onVariantComplete(response.variantIndex);
+ } else if (response.type === "variantError") {
+ callbacks.onVariantError(response.variantIndex, response.value);
+ } else if (response.type === "variantCount") {
+ callbacks.onVariantCount(parseInt(response.value));
+ } else if (response.type === "thinking") {
+ callbacks.onThinking(response.value, response.variantIndex);
+ } else if (response.type === "error") {
+ console.error("Error generating code", response.value);
+ toast.error(response.value);
+ }
+ } catch (error) {
+ console.error(`[WS] Error processing ${response.type} message:`, error);
}
});
diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts
index 779b9799..ed4ce5da 100644
--- a/frontend/src/store/project-store.ts
+++ b/frontend/src/store/project-store.ts
@@ -103,9 +103,14 @@ export const useProjectStore = create<ProjectStore>((set) => ({
appendCommitCode: (hash: CommitHash, numVariant: number, code: string) =>
set((state) => {
const commit = state.commits[hash];
+ if (!commit) {
+ console.error(`[Store] appendCommitCode: Commit not found for hash: ${hash}`);
+ return state;
+ }
// Don't update if the commit is already committed
if (commit.isCommitted) {
- throw new Error("Attempted to append code to a committed commit");
+ console.error(`[Store] appendCommitCode: Attempted to append code to a committed commit: ${hash}`);
+ return state; // Return unchanged state instead of throwing
}
const variant = commit.variants[numVariant];
const isFirstCode = !variant.code && variant.thinkingStartTime;
@@ -153,10 +158,17 @@ export const useProjectStore = create<ProjectStore>((set) => ({
setCommitCode: (hash: CommitHash, numVariant: number, code: string) =>
set((state) => {
const commit = state.commits[hash];
+ console.log(`[Store] setCommitCode called for hash=${hash}, variant=${numVariant}, code length=${code.length}`);
+ if (!commit) {
+ console.error(`[Store] Commit not found for hash: ${hash}`);
+ return state;
+ }
// Don't update if the commit is already committed
if (commit.isCommitted) {
- throw new Error("Attempted to set code of a committed commit");
+ console.error(`[Store] Attempted to set code of a committed commit: ${hash}`);
+ return state; // Return unchanged state instead of throwing
}
+ console.log(`[Store] Setting code for variant ${numVariant}`);
return {
commits: {
...state.commits,