Skip to content

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

Python

Rework backend prompt construction

Abi Raja8f54f012d149
diff --git a/backend/prompts/builders.py b/backend/prompts/builders.py
index bf353967..695fb077 100644
--- a/backend/prompts/builders.py
+++ b/backend/prompts/builders.py
@@ -9,46 +9,43 @@ 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
-IMPORTED_CODE_INSTRUCTIONS = """
-You are continuing from an imported codebase. Follow these instructions while updating it:
-
-- Do not add comments in the code such as "<!-- Add other navigation links as needed -->" or "<!-- ... other news items ... -->" in place of writing the full code. WRITE THE FULL CODE.
-- Repeat elements as needed. For example, if there are 15 items, the code should have 15 items. DO NOT LEAVE comments like "<!-- Repeat for each news item -->" or bad things will happen.
-- For images, use placeholder images from https://placehold.co and include a detailed description of the image in the alt text so that an image generation AI can generate the image later.
-"""
-
-
async def build_prompt_messages(
stack: Stack,
input_mode: InputMode,
generation_type: str,
prompt: PromptContent,
history: list[PromptHistoryMessage],
- is_imported_from_code: bool,
+ file_state: dict[str, str] | None = None,
) -> list[ChatCompletionMessageParam]:
prompt_messages: list[ChatCompletionMessageParam] = []
- if is_imported_from_code:
- original_imported_code = ""
- for message in history:
- if message["role"] == "assistant":
- original_imported_code = message["text"]
- break
- imported_user_prompt = resolve_imported_code_user_prompt(prompt, history)
- prompt_messages = build_imported_code_prompt_messages(
- original_imported_code,
- stack,
- imported_user_prompt,
- )
- elif generation_type == "update":
+ if generation_type == "update":
prompt_messages = [
cast(
ChatCompletionMessageParam,
{"role": "system", "content": system_prompt.SYSTEM_PROMPT},
)
]
- for item in history:
- prompt_messages.append(build_history_message(item))
+ 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", [])
@@ -88,6 +85,23 @@ async def build_prompt_messages(
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", [])
@@ -127,70 +141,3 @@ def build_history_message(item: PromptHistoryMessage) -> ChatCompletionMessagePa
"content": item.get("text", ""),
},
)
-
-
-def build_imported_code_prompt_messages(
- code: str,
- stack: Stack,
- user_prompt: PromptContent,
-) -> list[ChatCompletionMessageParam]:
- system_content = (
- system_prompt.SYSTEM_PROMPT.strip()
- + "\n\n"
- + IMPORTED_CODE_INSTRUCTIONS.strip()
- + f"\n\nSelected stack: {stack}."
- + "\n\nThe current app code is provided below. Update it per the request."
- + f"\n\n{code}"
- )
-
- return [
- {
- "role": "system",
- "content": system_content,
- },
- build_history_message(
- {
- "role": "user",
- "text": user_prompt.get("text", ""),
- "images": user_prompt.get("images", []),
- "videos": user_prompt.get("videos", []),
- }
- ),
- ]
-
-
-def resolve_imported_code_user_prompt(
- prompt: PromptContent,
- history: list[PromptHistoryMessage],
-) -> PromptContent:
- prompt_text = prompt.get("text", "")
- prompt_images = prompt.get("images", [])
- prompt_videos = prompt.get("videos", [])
-
- if prompt_text.strip():
- return {
- "text": prompt_text,
- "images": prompt_images,
- "videos": prompt_videos,
- }
-
- for item in reversed(history):
- if item["role"] == "user":
- return {
- "text": item["text"],
- "images": item.get("images", []),
- "videos": item.get("videos", []),
- }
-
- if prompt_images or prompt_videos:
- return {
- "text": "",
- "images": prompt_images,
- "videos": prompt_videos,
- }
-
- return {
- "text": "Update the imported code according to the latest request.",
- "images": [],
- "videos": [],
- }
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index 485ea915..f6c51fc5 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -222,7 +222,6 @@ class ExtractedParams:
generation_type: Literal["create", "update"]
prompt: PromptContent
history: List[PromptHistoryMessage]
- is_imported_from_code: bool
file_state: Dict[str, str] | None
option_codes: List[str]
@@ -288,9 +287,6 @@ class ParameterExtractionStage:
params.get("history")
)
- # Extract imported code flag
- is_imported_from_code = bool(params.get("isImportedFromCode", False))
-
# Extract file state for agent edits
raw_file_state = params.get("fileState")
file_state: Dict[str, str] | None = None
@@ -321,7 +317,6 @@ class ParameterExtractionStage:
generation_type=generation_type,
prompt=prompt,
history=history,
- is_imported_from_code=is_imported_from_code,
file_state=file_state,
option_codes=option_codes,
)
@@ -470,7 +465,7 @@ class PromptCreationStage:
generation_type=extracted_params.generation_type,
prompt=extracted_params.prompt,
history=extracted_params.history,
- is_imported_from_code=extracted_params.is_imported_from_code,
+ file_state=extracted_params.file_state,
)
print_prompt_preview(prompt_messages)
diff --git a/backend/tests/test_prompts.py b/backend/tests/test_prompts.py
index f7686d4d..de00fd91 100644
--- a/backend/tests/test_prompts.py
+++ b/backend/tests/test_prompts.py
@@ -99,7 +99,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure
@@ -153,7 +152,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure
@@ -191,7 +189,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure
@@ -238,7 +235,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure
@@ -296,7 +292,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
expected: ExpectedResult = {
@@ -349,7 +344,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure
@@ -405,7 +399,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure
@@ -466,7 +459,6 @@ class TestCreatePrompt:
generation_type=params["generationType"],
prompt=params["prompt"],
history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
)
# Define expected structure - should be text-only messages
@@ -484,38 +476,32 @@ class TestCreatePrompt:
assert_structure_match(actual, expected)
@pytest.mark.asyncio
- async def test_imported_code_update_with_images_in_history(self) -> None:
- """Test imported code flow with images in update history."""
- # Setup test data
+ async def test_update_bootstraps_from_file_state_when_history_is_empty(self) -> None:
+ """Update should synthesize a user message from fileState + prompt when history is empty."""
ref_image_url: str = "data:image/png;base64,ref_image"
params: Dict[str, Any] = {
- "isImportedFromCode": True,
"generationType": "update",
- "history": [
- {"role": "assistant", "text": "<html>Original imported code</html>", "images": [], "videos": []},
- {"role": "user", "text": "Update with this reference", "images": [ref_image_url], "videos": []},
- {"role": "assistant", "text": "<html>Updated code</html>", "images": [], "videos": []},
- ]
+ "prompt": {"text": "Make the header blue", "images": [ref_image_url], "videos": []},
+ "history": [],
+ "fileState": {
+ "path": "index.html",
+ "content": "<html>Original imported code</html>",
+ },
}
with patch("prompts.system_prompt.SYSTEM_PROMPT", self.MOCK_SYSTEM_PROMPT):
- # Call the function
messages = await build_prompt_messages(
stack=self.TEST_STACK,
input_mode="image",
generation_type=params["generationType"],
- prompt=params.get("prompt", {"text": "", "images": [], "videos": []}),
- history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
+ prompt=params["prompt"],
+ history=params["history"],
+ file_state=params["fileState"],
)
- # Define expected structure
expected: ExpectedResult = {
"messages": [
- {
- "role": "system",
- "content": "<CONTAINS:continuing from an imported codebase>",
- },
+ {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
{
"role": "user",
"content": [
@@ -528,52 +514,35 @@ class TestCreatePrompt:
},
{
"type": "text",
- "text": "Update with this reference",
+ "text": "<CONTAINS:<current_file path=\"index.html\">>",
},
],
},
],
}
- # Assert the structure matches
actual: ExpectedResult = {"messages": messages}
assert_structure_match(actual, expected)
+ user_content = messages[1].get("content")
+ assert isinstance(user_content, list)
+ text_part = next(
+ (part for part in user_content if isinstance(part, dict) and part.get("type") == "text"),
+ None,
+ )
+ assert isinstance(text_part, dict)
+ synthesized_text = text_part.get("text", "")
+ assert isinstance(synthesized_text, str)
+ assert "<html>Original imported code</html>" in synthesized_text
+ assert "<change_request>" in synthesized_text
+ assert "Make the header blue" in synthesized_text
@pytest.mark.asyncio
- async def test_imported_code_update_prefers_history_text_over_prompt_images(self) -> None:
- """Imported-code updates should not lose text instructions when prompt only has images."""
- params: Dict[str, Any] = {
- "isImportedFromCode": True,
- "generationType": "update",
- "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
- "history": [
- {"role": "assistant", "text": "<html>Original imported code</html>", "images": [], "videos": []},
- {"role": "user", "text": "Make the header blue", "images": [], "videos": []},
- ],
- }
-
- with patch("prompts.system_prompt.SYSTEM_PROMPT", self.MOCK_SYSTEM_PROMPT):
- messages = await build_prompt_messages(
+ async def test_update_requires_history_or_file_state(self) -> None:
+ with pytest.raises(ValueError):
+ await build_prompt_messages(
stack=self.TEST_STACK,
input_mode="image",
- generation_type=params["generationType"],
- prompt=params["prompt"],
- history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
+ generation_type="update",
+ prompt={"text": "Change title", "images": [], "videos": []},
+ history=[],
)
-
- expected: ExpectedResult = {
- "messages": [
- {
- "role": "system",
- "content": "<CONTAINS:continuing from an imported codebase>",
- },
- {
- "role": "user",
- "content": "Make the header blue",
- },
- ],
- }
-
- actual: ExpectedResult = {"messages": messages}
- assert_structure_match(actual, expected)
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 6e21d7cd..b6d30829 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -37,8 +37,6 @@ function App() {
// Inputs
inputMode,
setInputMode,
- isImportedFromCode,
- setIsImportedFromCode,
referenceImages,
setReferenceImages,
initialPrompt,
@@ -137,7 +135,6 @@ function App() {
// Inputs
setInputMode("image");
setReferenceImages([]);
- setIsImportedFromCode(false);
};
const regenerate = () => {
@@ -217,6 +214,11 @@ function App() {
requestParams.history && requestParams.history.length > 0
? requestParams.history[requestParams.history.length - 1]
: null;
+ const latestUpdateInput = latestHistoryMessage ?? {
+ text: requestParams.prompt.text,
+ images: requestParams.prompt.images,
+ videos: requestParams.prompt.videos ?? [],
+ };
const commitInputObject =
requestParams.generationType === "create"
@@ -231,9 +233,9 @@ function App() {
type: "ai_edit" as const,
parentHash: head,
inputs: {
- text: latestHistoryMessage?.text || "",
- images: latestHistoryMessage?.images || [],
- videos: latestHistoryMessage?.videos || [],
+ text: latestUpdateInput.text,
+ images: latestUpdateInput.images,
+ videos: latestUpdateInput.videos,
},
};
@@ -518,12 +520,7 @@ function App() {
}
const selectedVariant = currentCommit.variants[currentCommit.selectedVariantIndex];
- const baseVariantHistory =
- selectedVariant.history.length > 0
- ? selectedVariant.history
- : currentCode.trim().length > 0
- ? [buildAssistantHistoryMessage(currentCode)]
- : [];
+ const baseVariantHistory = selectedVariant.history;
const updateImageAssetIds = registerAssetIds(
"image",
updateImages,
@@ -535,19 +532,21 @@ function App() {
...cloneVariantHistory(baseVariantHistory),
buildUserHistoryMessage(modifiedUpdateInstruction, updateImageAssetIds),
];
- const updatedHistory = toRequestHistory(updatedVariantHistory, getAssetsById);
+ const shouldBootstrapFromFileState =
+ baseVariantHistory.length === 0 && currentCode.trim().length > 0;
+ const updatedHistory = shouldBootstrapFromFileState
+ ? []
+ : toRequestHistory(updatedVariantHistory, getAssetsById);
doGenerateCode({
generationType: "update",
inputMode,
- prompt:
- inputMode === "text"
- ? { text: initialPrompt, images: [], videos: [] }
- : inputMode === "video"
- ? { text: "", images: [], videos: [referenceImages[0]] }
- : { text: "", images: [referenceImages[0]], videos: [] },
+ prompt: {
+ text: modifiedUpdateInstruction,
+ images: updateImages,
+ videos: [],
+ },
history: updatedHistory,
- isImportedFromCode,
optionCodes,
variantHistory: updatedVariantHistory,
fileState: currentCode
@@ -580,9 +579,6 @@ function App() {
// Reset any existing state
reset();
- // Set input state
- setIsImportedFromCode(true);
-
// Set up this project
setStack(stack);
@@ -590,7 +586,7 @@ function App() {
const commit = createCommit({
type: "code_create",
parentHash: null,
- variants: [{ code, history: [buildAssistantHistoryMessage(code)] }],
+ variants: [{ code, history: [] }],
inputs: null,
});
addCommit(commit);
diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts
index 4277426a..5054136a 100644
--- a/frontend/src/store/project-store.ts
+++ b/frontend/src/store/project-store.ts
@@ -13,8 +13,6 @@ interface ProjectStore {
// Inputs
inputMode: "image" | "video" | "text";
setInputMode: (mode: "image" | "video" | "text") => void;
- isImportedFromCode: boolean;
- setIsImportedFromCode: (imported: boolean) => void;
referenceImages: string[];
setReferenceImages: (images: string[]) => void;
initialPrompt: string;
@@ -88,8 +86,6 @@ export const useProjectStore = create<ProjectStore>((set) => ({
// Inputs and their setters
inputMode: "image",
setInputMode: (mode) => set({ inputMode: mode }),
- isImportedFromCode: false,
- setIsImportedFromCode: (imported) => set({ isImportedFromCode: imported }),
referenceImages: [],
setReferenceImages: (images) => set({ referenceImages: images }),
initialPrompt: "",
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 7eb6f659..7f961e54 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -58,7 +58,6 @@ export interface CodeGenerationParams {
inputMode: "image" | "video" | "text";
prompt: PromptContent;
history?: PromptHistoryMessage[];
- isImportedFromCode?: boolean;
fileState?: {
path: string;
content: string;