Skip to content

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

Python

Update prompt imports to builder

Abi Rajae0618f93434e
diff --git a/backend/prompts/create/__init__.py b/backend/prompts/create/__init__.py
index 5918c48a..a3520be8 100644
--- a/backend/prompts/create/__init__.py
+++ b/backend/prompts/create/__init__.py
@@ -3,7 +3,7 @@ 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
+from prompts.message_builder import Prompt
def build_create_prompt_from_input(
diff --git a/backend/prompts/render.py b/backend/prompts/message_builder.py
similarity index 100%
rename from backend/prompts/render.py
rename to backend/prompts/message_builder.py
diff --git a/backend/prompts/pipeline.py b/backend/prompts/pipeline.py
index 3062086a..3035e406 100644
--- a/backend/prompts/pipeline.py
+++ b/backend/prompts/pipeline.py
@@ -2,7 +2,7 @@ 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.message_builder import Prompt
from prompts.update import (
build_update_prompt_from_file_snapshot,
build_update_prompt_from_history,
@@ -27,8 +27,12 @@ async def build_prompt_messages(
strategy = plan["construction_strategy"]
if strategy == "update_from_history":
- return build_update_prompt_from_history(history)
+ return build_update_prompt_from_history(stack=stack, 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_update_prompt_from_file_snapshot(
+ stack=stack,
+ prompt=prompt,
+ file_state=file_state,
+ )
return build_create_prompt_from_input(input_mode, stack, prompt)
diff --git a/backend/prompts/update/__init__.py b/backend/prompts/update/__init__.py
index 0f592c12..f897bebe 100644
--- a/backend/prompts/update/__init__.py
+++ b/backend/prompts/update/__init__.py
@@ -1,11 +1,7 @@
-from prompts.update.from_file_snapshot import (
- build_update_bootstrap_prompt_text,
- build_update_prompt_from_file_snapshot,
-)
+from prompts.update.from_file_snapshot import 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
index 5d010608..744a202f 100644
--- a/backend/prompts/update/from_file_snapshot.py
+++ b/backend/prompts/update/from_file_snapshot.py
@@ -3,46 +3,41 @@ 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
+from prompts.prompt_types import Stack, UserTurnInput
+from prompts.message_builder import Prompt, build_history_message
def build_update_prompt_from_file_snapshot(
+ stack: Stack,
prompt: UserTurnInput,
file_state: dict[str, str],
) -> Prompt:
+ path = file_state.get("path", "index.html")
+ request_text = prompt.get("text", "").strip() or "Apply the requested update."
+ bootstrap_text = (
+ f"Selected stack: {stack}.\n\n"
+ "You are editing an existing file.\n\n"
+ f'<current_file path="{path}">\n'
+ f'{file_state["content"]}\n'
+ "</current_file>\n\n"
+ "<change_request>\n"
+ f"{request_text}\n"
+ "</change_request>"
+ )
return [
cast(
ChatCompletionMessageParam,
- {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
+ {
+ "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", ""),
- ),
+ "text": bootstrap_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
index 6c39374e..157ee18f 100644
--- a/backend/prompts/update/from_history.py
+++ b/backend/prompts/update/from_history.py
@@ -3,19 +3,49 @@ 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
+from prompts.prompt_types import PromptHistoryMessage, Stack
+from prompts.message_builder import Prompt, build_history_message
def build_update_prompt_from_history(
+ stack: Stack,
history: list[PromptHistoryMessage],
) -> Prompt:
+ first_user_index = next(
+ (index for index, item in enumerate(history) if item["role"] == "user"),
+ -1,
+ )
+ if first_user_index == -1:
+ raise ValueError("Update history must include at least one user message")
+
prompt_messages: Prompt = [
cast(
ChatCompletionMessageParam,
- {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
+ {
+ "role": "system",
+ "content": system_prompt.SYSTEM_PROMPT,
+ },
)
]
- for item in history:
+ for index, item in enumerate(history):
+ if index == first_user_index:
+ stack_prefix = f"Selected stack: {stack}."
+ user_text = item.get("text", "")
+ prefixed_text = (
+ f"{stack_prefix}\n\n{user_text}" if user_text.strip() else stack_prefix
+ )
+ prompt_messages.append(
+ build_history_message(
+ {
+ "role": "user",
+ "text": prefixed_text,
+ "images": item.get("images", []),
+ "videos": item.get("videos", []),
+ }
+ )
+ )
+ continue
+
prompt_messages.append(build_history_message(item))
+
return prompt_messages
diff --git a/backend/tests/test_prompts.py b/backend/tests/test_prompts.py
index 8c1e1562..50f1d2fa 100644
--- a/backend/tests/test_prompts.py
+++ b/backend/tests/test_prompts.py
@@ -188,9 +188,15 @@ class TestCreatePrompt:
# Define expected structure
expected: ExpectedResult = {
"messages": [
- {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "system",
+ "content": self.MOCK_SYSTEM_PROMPT,
+ },
{"role": "assistant", "content": "<html>Initial code</html>"},
- {"role": "user", "content": "Make the background blue"},
+ {
+ "role": "user",
+ "content": f"Selected stack: {self.TEST_STACK}.\n\nMake the background blue",
+ },
{"role": "assistant", "content": "<html>Updated code</html>"},
{"role": "user", "content": "Add a header"},
],
@@ -273,7 +279,7 @@ class TestCreatePrompt:
"messages": [
{
"role": "system",
- "content": self.MOCK_SYSTEM_PROMPT
+ "content": self.MOCK_SYSTEM_PROMPT,
},
{
"role": "assistant",
@@ -281,7 +287,7 @@ class TestCreatePrompt:
},
{
"role": "user",
- "content": "Add a sidebar"
+ "content": f"Selected stack: {self.TEST_STACK}.\n\nAdd a sidebar",
},
{
"role": "assistant",
@@ -397,7 +403,10 @@ class TestCreatePrompt:
# Define expected structure
expected: ExpectedResult = {
"messages": [
- {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "system",
+ "content": self.MOCK_SYSTEM_PROMPT,
+ },
{"role": "assistant", "content": "<html>Initial code</html>"},
{
"role": "user",
@@ -411,7 +420,7 @@ class TestCreatePrompt:
},
{
"type": "text",
- "text": "Add a button",
+ "text": f"Selected stack: {self.TEST_STACK}.\n\nAdd a button",
},
],
},
@@ -452,7 +461,10 @@ class TestCreatePrompt:
# Define expected structure
expected: ExpectedResult = {
"messages": [
- {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "system",
+ "content": self.MOCK_SYSTEM_PROMPT,
+ },
{"role": "assistant", "content": "<html>Initial code</html>"},
{
"role": "user",
@@ -473,7 +485,7 @@ class TestCreatePrompt:
},
{
"type": "text",
- "text": "Style like these examples",
+ "text": f"Selected stack: {self.TEST_STACK}.\n\nStyle like these examples",
},
],
},
@@ -512,9 +524,15 @@ class TestCreatePrompt:
# Define expected structure - should be text-only messages
expected: ExpectedResult = {
"messages": [
- {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "system",
+ "content": self.MOCK_SYSTEM_PROMPT,
+ },
{"role": "assistant", "content": "<html>Initial code</html>"},
- {"role": "user", "content": "Make it blue"}, # Text-only message
+ {
+ "role": "user",
+ "content": f"Selected stack: {self.TEST_STACK}.\n\nMake it blue",
+ }, # Text-only message
{"role": "assistant", "content": "<html>Blue code</html>"},
],
}
@@ -549,7 +567,10 @@ class TestCreatePrompt:
expected: ExpectedResult = {
"messages": [
- {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "system",
+ "content": self.MOCK_SYSTEM_PROMPT,
+ },
{
"role": "user",
"content": [
@@ -580,6 +601,7 @@ class TestCreatePrompt:
assert isinstance(text_part, dict)
synthesized_text = text_part.get("text", "")
assert isinstance(synthesized_text, str)
+ assert f"Selected stack: {self.TEST_STACK}." in synthesized_text
assert "<html>Original imported code</html>" in synthesized_text
assert "<change_request>" in synthesized_text
assert "Make the header blue" in synthesized_text
@@ -594,3 +616,23 @@ class TestCreatePrompt:
prompt={"text": "Change title", "images": [], "videos": []},
history=[],
)
+
+ @pytest.mark.asyncio
+ async def test_update_history_requires_user_message(self) -> None:
+ with pytest.raises(
+ ValueError, match="Update history must include at least one user message"
+ ):
+ await build_prompt_messages(
+ stack=self.TEST_STACK,
+ input_mode="image",
+ generation_type="update",
+ prompt={"text": "Change title", "images": [], "videos": []},
+ history=[
+ {
+ "role": "assistant",
+ "text": "<html>Code only</html>",
+ "images": [],
+ "videos": [],
+ }
+ ],
+ )