Skip to content

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

Python

Refactor prompt builders layout

Abi Rajaac86c58b0ae4
diff --git a/backend/evals/core.py b/backend/evals/core.py
index f19240ab..00822490 100644
--- a/backend/evals/core.py
+++ b/backend/evals/core.py
@@ -6,7 +6,7 @@ from config import (
)
from llm import Llm, OPENAI_MODELS, ANTHROPIC_MODELS, GEMINI_MODELS
from agent.runner import Agent
-from prompts.image_prompt_builder import build_image_prompt_messages
+from prompts.create.image import build_image_prompt_messages
from prompts.prompt_types import Stack
from openai.types.chat import ChatCompletionMessageParam
from typing import Any
diff --git a/backend/prompts/builders.py b/backend/prompts/builders.py
deleted file mode 100644
index 695fb077..00000000
--- a/backend/prompts/builders.py
+++ /dev/null
@@ -1,143 +0,0 @@
-from typing import cast
-
-from openai.types.chat import ChatCompletionContentPartParam, ChatCompletionMessageParam
-
-from custom_types import InputMode
-from prompts.image_prompt_builder import build_image_prompt_messages
-from prompts.prompt_types import PromptContent, PromptHistoryMessage, Stack
-from prompts import system_prompt
-from prompts.text_prompt_builder import build_text_prompt_messages
-from prompts.video_prompt_builder import build_video_prompt_messages
-
-async def build_prompt_messages(
- stack: Stack,
- input_mode: InputMode,
- generation_type: str,
- prompt: PromptContent,
- history: list[PromptHistoryMessage],
- file_state: dict[str, str] | None = None,
-) -> list[ChatCompletionMessageParam]:
- prompt_messages: list[ChatCompletionMessageParam] = []
-
- if generation_type == "update":
- prompt_messages = [
- cast(
- ChatCompletionMessageParam,
- {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
- )
- ]
- if len(history) > 0:
- for item in history:
- prompt_messages.append(build_history_message(item))
- elif file_state and file_state.get("content", "").strip():
- prompt_messages.append(
- build_history_message(
- {
- "role": "user",
- "text": build_update_bootstrap_prompt_text(
- path=file_state.get("path", "index.html"),
- current_code=file_state["content"],
- instruction=prompt.get("text", ""),
- ),
- "images": prompt.get("images", []),
- "videos": prompt.get("videos", []),
- }
- )
- )
- else:
- raise ValueError("Update requests require history or fileState.content")
- else:
- if input_mode == "image":
- image_urls = prompt.get("images", [])
- text_prompt = prompt.get("text", "")
- prompt_messages = build_image_prompt_messages(
- image_data_urls=image_urls,
- stack=stack,
- text_prompt=text_prompt,
- )
- elif input_mode == "text":
- prompt_messages = build_text_prompt_messages(
- text_prompt=prompt["text"],
- stack=stack,
- )
- elif input_mode == "video":
- video_urls = prompt.get("videos", [])
- if not video_urls:
- # Backward compatibility for older frontend payloads.
- video_urls = prompt.get("images", [])
- if not video_urls:
- raise ValueError("Video mode requires a video to be provided")
- video_url = video_urls[0]
- prompt_messages = build_video_prompt_messages(
- video_data_url=video_url,
- stack=stack,
- text_prompt=prompt.get("text", ""),
- )
- else:
- image_urls = prompt.get("images", [])
- text_prompt = prompt.get("text", "")
- prompt_messages = build_image_prompt_messages(
- image_data_urls=image_urls,
- stack=stack,
- text_prompt=text_prompt,
- )
-
- return prompt_messages
-
-
-def build_update_bootstrap_prompt_text(
- path: str,
- current_code: str,
- instruction: str,
-) -> str:
- request_text = instruction.strip() or "Apply the requested update."
- return (
- "You are editing an existing file.\n\n"
- f'<current_file path="{path}">\n'
- f"{current_code}\n"
- "</current_file>\n\n"
- "<change_request>\n"
- f"{request_text}\n"
- "</change_request>"
- )
-
-
-def build_history_message(item: PromptHistoryMessage) -> ChatCompletionMessageParam:
- role = item["role"]
- image_urls = item.get("images", [])
- video_urls = item.get("videos", [])
- media_urls = [*image_urls, *video_urls]
-
- if role == "user" and len(media_urls) > 0:
- user_content: list[ChatCompletionContentPartParam] = []
-
- for media_url in media_urls:
- user_content.append(
- {
- "type": "image_url",
- "image_url": {"url": media_url, "detail": "high"},
- }
- )
-
- user_content.append(
- {
- "type": "text",
- "text": item.get("text", ""),
- }
- )
-
- return cast(
- ChatCompletionMessageParam,
- {
- "role": role,
- "content": user_content,
- },
- )
-
- return cast(
- ChatCompletionMessageParam,
- {
- "role": role,
- "content": item.get("text", ""),
- },
- )
diff --git a/backend/prompts/create/__init__.py b/backend/prompts/create/__init__.py
new file mode 100644
index 00000000..5918c48a
--- /dev/null
+++ b/backend/prompts/create/__init__.py
@@ -0,0 +1,40 @@
+from custom_types import InputMode
+from prompts.create.image import build_image_prompt_messages
+from prompts.create.text import build_text_prompt_messages
+from prompts.create.video import build_video_prompt_messages
+from prompts.prompt_types import Stack, UserTurnInput
+from prompts.render import Prompt
+
+
+def build_create_prompt_from_input(
+ input_mode: InputMode,
+ stack: Stack,
+ prompt: UserTurnInput,
+) -> Prompt:
+ if input_mode == "image":
+ image_urls = prompt.get("images", [])
+ text_prompt = prompt.get("text", "")
+ return build_image_prompt_messages(
+ image_data_urls=image_urls,
+ stack=stack,
+ text_prompt=text_prompt,
+ )
+ if input_mode == "text":
+ return build_text_prompt_messages(
+ text_prompt=prompt["text"],
+ stack=stack,
+ )
+ if input_mode == "video":
+ video_urls = prompt.get("videos", [])
+ if not video_urls:
+ raise ValueError("Video mode requires a video to be provided")
+ video_url = video_urls[0]
+ return build_video_prompt_messages(
+ video_data_url=video_url,
+ stack=stack,
+ text_prompt=prompt.get("text", ""),
+ )
+ raise ValueError(f"Unsupported input mode: {input_mode}")
+
+
+__all__ = ["build_create_prompt_from_input"]
diff --git a/backend/prompts/image_prompt_builder.py b/backend/prompts/create/image.py
similarity index 100%
rename from backend/prompts/image_prompt_builder.py
rename to backend/prompts/create/image.py
diff --git a/backend/prompts/text_prompt_builder.py b/backend/prompts/create/text.py
similarity index 100%
rename from backend/prompts/text_prompt_builder.py
rename to backend/prompts/create/text.py
diff --git a/backend/prompts/video_prompt_builder.py b/backend/prompts/create/video.py
similarity index 100%
rename from backend/prompts/video_prompt_builder.py
rename to backend/prompts/create/video.py
diff --git a/backend/prompts/pipeline.py b/backend/prompts/pipeline.py
new file mode 100644
index 00000000..3062086a
--- /dev/null
+++ b/backend/prompts/pipeline.py
@@ -0,0 +1,34 @@
+from custom_types import InputMode
+from prompts.create import build_create_prompt_from_input
+from prompts.plan import derive_prompt_construction_plan
+from prompts.prompt_types import PromptHistoryMessage, Stack, UserTurnInput
+from prompts.render import Prompt
+from prompts.update import (
+ build_update_prompt_from_file_snapshot,
+ build_update_prompt_from_history,
+)
+
+
+async def build_prompt_messages(
+ stack: Stack,
+ input_mode: InputMode,
+ generation_type: str,
+ prompt: UserTurnInput,
+ history: list[PromptHistoryMessage],
+ file_state: dict[str, str] | None = None,
+) -> Prompt:
+ plan = derive_prompt_construction_plan(
+ stack=stack,
+ input_mode=input_mode,
+ generation_type=generation_type,
+ history=history,
+ file_state=file_state,
+ )
+
+ strategy = plan["construction_strategy"]
+ if strategy == "update_from_history":
+ return build_update_prompt_from_history(history)
+ if strategy == "update_from_file_snapshot":
+ assert file_state is not None
+ return build_update_prompt_from_file_snapshot(prompt, file_state)
+ return build_create_prompt_from_input(input_mode, stack, prompt)
diff --git a/backend/prompts/plan.py b/backend/prompts/plan.py
new file mode 100644
index 00000000..4f83cb7e
--- /dev/null
+++ b/backend/prompts/plan.py
@@ -0,0 +1,35 @@
+from custom_types import InputMode
+from prompts.prompt_types import (
+ PromptConstructionPlan,
+ PromptHistoryMessage,
+ Stack,
+)
+
+
+def derive_prompt_construction_plan(
+ stack: Stack,
+ input_mode: InputMode,
+ generation_type: str,
+ history: list[PromptHistoryMessage],
+ file_state: dict[str, str] | None,
+) -> PromptConstructionPlan:
+ if generation_type == "update":
+ if len(history) > 0:
+ strategy = "update_from_history"
+ elif file_state and file_state.get("content", "").strip():
+ strategy = "update_from_file_snapshot"
+ else:
+ raise ValueError("Update requests require history or fileState.content")
+ return {
+ "generation_type": "update",
+ "input_mode": input_mode,
+ "stack": stack,
+ "construction_strategy": strategy,
+ }
+
+ return {
+ "generation_type": "create",
+ "input_mode": input_mode,
+ "stack": stack,
+ "construction_strategy": "create_from_input",
+ }
diff --git a/backend/prompts/prompt_types.py b/backend/prompts/prompt_types.py
index 890dafae..08020652 100644
--- a/backend/prompts/prompt_types.py
+++ b/backend/prompts/prompt_types.py
@@ -1,8 +1,8 @@
from typing import List, Literal, TypedDict
-class PromptContent(TypedDict):
- """Unified structure for prompt text and images."""
+class UserTurnInput(TypedDict):
+ """Normalized current user turn payload from the request."""
text: str
images: List[str]
@@ -18,6 +18,13 @@ class PromptHistoryMessage(TypedDict):
videos: List[str]
+PromptConstructionStrategy = Literal[
+ "create_from_input",
+ "update_from_history",
+ "update_from_file_snapshot",
+]
+
+
Stack = Literal[
"html_css",
"html_tailwind",
@@ -26,3 +33,12 @@ Stack = Literal[
"ionic_tailwind",
"vue_tailwind",
]
+
+
+class PromptConstructionPlan(TypedDict):
+ """Derived plan used by prompt builders to choose a single construction path."""
+
+ generation_type: Literal["create", "update"]
+ input_mode: Literal["image", "video", "text"]
+ stack: Stack
+ construction_strategy: PromptConstructionStrategy
diff --git a/backend/prompts/render.py b/backend/prompts/render.py
new file mode 100644
index 00000000..986cb844
--- /dev/null
+++ b/backend/prompts/render.py
@@ -0,0 +1,48 @@
+from typing import cast
+
+from openai.types.chat import ChatCompletionContentPartParam, ChatCompletionMessageParam
+
+from prompts.prompt_types import PromptHistoryMessage
+
+Prompt = list[ChatCompletionMessageParam]
+
+
+def build_history_message(item: PromptHistoryMessage) -> ChatCompletionMessageParam:
+ role = item["role"]
+ image_urls = item.get("images", [])
+ video_urls = item.get("videos", [])
+ media_urls = [*image_urls, *video_urls]
+
+ if role == "user" and len(media_urls) > 0:
+ user_content: list[ChatCompletionContentPartParam] = []
+
+ for media_url in media_urls:
+ user_content.append(
+ {
+ "type": "image_url",
+ "image_url": {"url": media_url, "detail": "high"},
+ }
+ )
+
+ user_content.append(
+ {
+ "type": "text",
+ "text": item.get("text", ""),
+ }
+ )
+
+ return cast(
+ ChatCompletionMessageParam,
+ {
+ "role": role,
+ "content": user_content,
+ },
+ )
+
+ return cast(
+ ChatCompletionMessageParam,
+ {
+ "role": role,
+ "content": item.get("text", ""),
+ },
+ )
diff --git a/backend/prompts/request_parsing.py b/backend/prompts/request_parsing.py
index 4ede6346..6ac7f81a 100644
--- a/backend/prompts/request_parsing.py
+++ b/backend/prompts/request_parsing.py
@@ -1,6 +1,6 @@
from typing import List, cast
-from prompts.prompt_types import PromptContent, PromptHistoryMessage
+from prompts.prompt_types import PromptHistoryMessage, UserTurnInput
def _to_string_list(value: object) -> List[str]:
@@ -10,7 +10,7 @@ def _to_string_list(value: object) -> List[str]:
return [item for item in raw_list if isinstance(item, str)]
-def parse_prompt_content(raw_prompt: object) -> PromptContent:
+def parse_prompt_content(raw_prompt: object) -> UserTurnInput:
if not isinstance(raw_prompt, dict):
return {"text": "", "images": [], "videos": []}
diff --git a/backend/prompts/update/__init__.py b/backend/prompts/update/__init__.py
new file mode 100644
index 00000000..0f592c12
--- /dev/null
+++ b/backend/prompts/update/__init__.py
@@ -0,0 +1,11 @@
+from prompts.update.from_file_snapshot import (
+ build_update_bootstrap_prompt_text,
+ build_update_prompt_from_file_snapshot,
+)
+from prompts.update.from_history import build_update_prompt_from_history
+
+__all__ = [
+ "build_update_bootstrap_prompt_text",
+ "build_update_prompt_from_file_snapshot",
+ "build_update_prompt_from_history",
+]
diff --git a/backend/prompts/update/from_file_snapshot.py b/backend/prompts/update/from_file_snapshot.py
new file mode 100644
index 00000000..5d010608
--- /dev/null
+++ b/backend/prompts/update/from_file_snapshot.py
@@ -0,0 +1,48 @@
+from typing import cast
+
+from openai.types.chat import ChatCompletionMessageParam
+
+from prompts import system_prompt
+from prompts.prompt_types import UserTurnInput
+from prompts.render import Prompt, build_history_message
+
+
+def build_update_prompt_from_file_snapshot(
+ prompt: UserTurnInput,
+ file_state: dict[str, str],
+) -> Prompt:
+ return [
+ cast(
+ ChatCompletionMessageParam,
+ {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
+ ),
+ build_history_message(
+ {
+ "role": "user",
+ "text": build_update_bootstrap_prompt_text(
+ path=file_state.get("path", "index.html"),
+ current_code=file_state["content"],
+ instruction=prompt.get("text", ""),
+ ),
+ "images": prompt.get("images", []),
+ "videos": prompt.get("videos", []),
+ }
+ ),
+ ]
+
+
+def build_update_bootstrap_prompt_text(
+ path: str,
+ current_code: str,
+ instruction: str,
+) -> str:
+ request_text = instruction.strip() or "Apply the requested update."
+ return (
+ "You are editing an existing file.\n\n"
+ f'<current_file path="{path}">\n'
+ f"{current_code}\n"
+ "</current_file>\n\n"
+ "<change_request>\n"
+ f"{request_text}\n"
+ "</change_request>"
+ )
diff --git a/backend/prompts/update/from_history.py b/backend/prompts/update/from_history.py
new file mode 100644
index 00000000..6c39374e
--- /dev/null
+++ b/backend/prompts/update/from_history.py
@@ -0,0 +1,21 @@
+from typing import cast
+
+from openai.types.chat import ChatCompletionMessageParam
+
+from prompts import system_prompt
+from prompts.prompt_types import PromptHistoryMessage
+from prompts.render import Prompt, build_history_message
+
+
+def build_update_prompt_from_history(
+ history: list[PromptHistoryMessage],
+) -> Prompt:
+ prompt_messages: Prompt = [
+ cast(
+ ChatCompletionMessageParam,
+ {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
+ )
+ ]
+ for item in history:
+ prompt_messages.append(build_history_message(item))
+ return prompt_messages
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index f6c51fc5..3675f8e6 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -51,9 +51,9 @@ MessageType = Literal[
"toolStart",
"toolResult",
]
-from prompts.builders import build_prompt_messages
+from prompts.pipeline import build_prompt_messages
from prompts.request_parsing import parse_prompt_content, parse_prompt_history
-from prompts.prompt_types import PromptContent, PromptHistoryMessage, Stack
+from prompts.prompt_types import PromptHistoryMessage, Stack, UserTurnInput
from agent.runner import Agent
# from utils import pprint_prompt
@@ -220,7 +220,7 @@ class ExtractedParams:
anthropic_api_key: str | None
openai_base_url: str | None
generation_type: Literal["create", "update"]
- prompt: PromptContent
+ prompt: UserTurnInput
history: List[PromptHistoryMessage]
file_state: Dict[str, str] | None
option_codes: List[str]
@@ -280,7 +280,7 @@ class ParameterExtractionStage:
generation_type = cast(Literal["create", "update"], generation_type)
# Extract prompt content
- prompt: PromptContent = parse_prompt_content(params.get("prompt"))
+ prompt: UserTurnInput = parse_prompt_content(params.get("prompt"))
# Extract history (default to empty list)
history: List[PromptHistoryMessage] = parse_prompt_history(
diff --git a/backend/tests/test_prompts.py b/backend/tests/test_prompts.py
index de00fd91..8c1e1562 100644
--- a/backend/tests/test_prompts.py
+++ b/backend/tests/test_prompts.py
@@ -1,14 +1,15 @@
import pytest
from unittest.mock import patch, MagicMock
import sys
-from typing import Any, Dict, List, TypedDict
+from typing import Any, Dict, List, TypedDict, cast
from openai.types.chat import ChatCompletionMessageParam
# Mock moviepy before importing prompts
sys.modules["moviepy"] = MagicMock()
sys.modules["moviepy.editor"] = MagicMock()
-from prompts.builders import build_prompt_messages
+from prompts.pipeline import build_prompt_messages
+from prompts.plan import derive_prompt_construction_plan
from prompts.prompt_types import Stack
# Type definitions for test structures
@@ -82,6 +83,36 @@ class TestCreatePrompt:
MOCK_SYSTEM_PROMPT: str = "Mock HTML Tailwind system prompt"
TEST_STACK: Stack = "html_tailwind"
+ def test_plan_create_uses_create_from_input(self) -> None:
+ plan = derive_prompt_construction_plan(
+ stack=self.TEST_STACK,
+ input_mode="image",
+ generation_type="create",
+ history=[],
+ file_state=None,
+ )
+ assert plan["construction_strategy"] == "create_from_input"
+
+ def test_plan_update_with_history_uses_history_strategy(self) -> None:
+ plan = derive_prompt_construction_plan(
+ stack=self.TEST_STACK,
+ input_mode="image",
+ generation_type="update",
+ history=[{"role": "user", "text": "change", "images": [], "videos": []}],
+ file_state=None,
+ )
+ assert plan["construction_strategy"] == "update_from_history"
+
+ def test_plan_update_without_history_uses_file_snapshot_strategy(self) -> None:
+ plan = derive_prompt_construction_plan(
+ stack=self.TEST_STACK,
+ input_mode="image",
+ generation_type="update",
+ history=[],
+ file_state={"path": "index.html", "content": "<html></html>"},
+ )
+ assert plan["construction_strategy"] == "update_from_file_snapshot"
+
@pytest.mark.asyncio
async def test_image_mode_create_single_image(self) -> None:
"""Test create generation with single image in image mode."""
@@ -280,7 +311,8 @@ class TestCreatePrompt:
params: Dict[str, Any] = {
"prompt": {
"text": "",
- "images": [video_data_url]
+ "images": [],
+ "videos": [video_data_url],
},
"generationType": "create"
}
@@ -320,6 +352,22 @@ class TestCreatePrompt:
actual: ExpectedResult = {"messages": messages}
assert_structure_match(actual, expected)
+ @pytest.mark.asyncio
+ async def test_create_raises_on_unsupported_input_mode(self) -> None:
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL], "videos": []},
+ "generationType": "create",
+ }
+
+ with pytest.raises(ValueError, match="Unsupported input mode: audio"):
+ await build_prompt_messages(
+ stack=self.TEST_STACK,
+ input_mode=cast(Any, "audio"),
+ generation_type=params["generationType"],
+ prompt=params["prompt"],
+ history=[],
+ )
+
@pytest.mark.asyncio
async def test_image_mode_update_with_single_image_in_history(self) -> None: