Skip to content

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

Python

feat: add support for images in update history

Abi Raja4f1b5f1fe0e3
diff --git a/backend/prompts/__init__.py b/backend/prompts/__init__.py
index d8e06c6e..ae832027 100644
--- a/backend/prompts/__init__.py
+++ b/backend/prompts/__init__.py
@@ -1,4 +1,4 @@
-from typing import Union, Any
+from typing import Union, Any, cast
from openai.types.chat import ChatCompletionMessageParam, ChatCompletionContentPartParam
from custom_types import InputMode
@@ -31,10 +31,7 @@ async def create_prompt(
prompt_messages = assemble_imported_code_prompt(original_imported_code, stack)
for index, item in enumerate(params["history"][1:]):
role = "user" if index % 2 == 0 else "assistant"
- message: ChatCompletionMessageParam = {
- "role": role,
- "content": item["text"],
- }
+ message = create_message_from_history_item(item, role)
prompt_messages.append(message)
else:
# Assemble the prompt for non-imported code
@@ -63,10 +60,7 @@ async def create_prompt(
# Transform the history tree into message format
for index, item in enumerate(params["history"]):
role = "assistant" if index % 2 == 0 else "user"
- message: ChatCompletionMessageParam = {
- "role": role,
- "content": item["text"],
- }
+ message = create_message_from_history_item(item, role)
prompt_messages.append(message)
image_cache = create_alt_url_mapping(params["history"][-2]["text"])
@@ -78,6 +72,43 @@ async def create_prompt(
return prompt_messages, image_cache
+def create_message_from_history_item(
+ item: dict[str, Any], role: str
+) -> ChatCompletionMessageParam:
+ """
+ Create a ChatCompletionMessageParam from a history item.
+ Handles both text-only and text+images content.
+ """
+ # Check if this is a user message with images
+ if role == "user" and item.get("images") and len(item["images"]) > 0:
+ # Create multipart content for user messages with images
+ user_content: list[ChatCompletionContentPartParam] = []
+
+ # Add all images first
+ for image_url in item["images"]:
+ user_content.append({
+ "type": "image_url",
+ "image_url": {"url": image_url, "detail": "high"},
+ })
+
+ # Add text content
+ user_content.append({
+ "type": "text",
+ "text": item["text"],
+ })
+
+ return cast(ChatCompletionMessageParam, {
+ "role": role,
+ "content": user_content,
+ })
+ else:
+ # Regular text-only message
+ return cast(ChatCompletionMessageParam, {
+ "role": role,
+ "content": item["text"],
+ })
+
+
def assemble_imported_code_prompt(
code: str, stack: Stack
) -> list[ChatCompletionMessageParam]:
diff --git a/backend/tests/test_prompts.py b/backend/tests/test_prompts.py
index f2210701..2dc2115a 100644
--- a/backend/tests/test_prompts.py
+++ b/backend/tests/test_prompts.py
@@ -431,3 +431,259 @@ class TestCreatePrompt:
# Assert the structure matches
actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
assert_structure_match(actual, expected)
+
+
+ @pytest.mark.asyncio
+ async def test_image_mode_update_with_single_image_in_history(self) -> None:
+ """Test update with user message containing a single image."""
+ # Setup test data
+ reference_image_url: str = "data:image/png;base64,reference_image"
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Add a button", "images": [reference_image_url]},
+ {"text": "<html>Code with button</html>", "images": []}
+ ]
+ }
+
+ # Mock the system prompts and image cache function
+ mock_system_prompts: Dict[str, str] = {self.TEST_STACK: self.MOCK_SYSTEM_PROMPT}
+
+ with patch("prompts.SYSTEM_PROMPTS", mock_system_prompts), \
+ patch("prompts.create_alt_url_mapping", return_value={"mock": "cache"}):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure
+ expected: ExpectedResult = {
+ "messages": [
+ {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": self.TEST_IMAGE_URL,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "<CONTAINS:Generate code for a web page that looks exactly like this.>",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Initial code</html>"},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": reference_image_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "Add a button",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Code with button</html>"},
+ ],
+ "image_cache": {"mock": "cache"},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ assert_structure_match(actual, expected)
+
+ @pytest.mark.asyncio
+ async def test_image_mode_update_with_multiple_images_in_history(self) -> None:
+ """Test update with user message containing multiple images."""
+ # Setup test data
+ example1_url: str = "data:image/png;base64,example1"
+ example2_url: str = "data:image/png;base64,example2"
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Style like these examples", "images": [example1_url, example2_url]},
+ {"text": "<html>Styled code</html>", "images": []}
+ ]
+ }
+
+ # Mock the system prompts and image cache function
+ mock_system_prompts: Dict[str, str] = {self.TEST_STACK: self.MOCK_SYSTEM_PROMPT}
+
+ with patch("prompts.SYSTEM_PROMPTS", mock_system_prompts), \
+ patch("prompts.create_alt_url_mapping", return_value={"mock": "cache"}):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure
+ expected: ExpectedResult = {
+ "messages": [
+ {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": self.TEST_IMAGE_URL,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "<CONTAINS:Generate code for a web page that looks exactly like this.>",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Initial code</html>"},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": example1_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": example2_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "Style like these examples",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Styled code</html>"},
+ ],
+ "image_cache": {"mock": "cache"},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ assert_structure_match(actual, expected)
+
+ @pytest.mark.asyncio
+ async def test_update_with_empty_images_arrays(self) -> None:
+ """Test that empty images arrays don't break existing functionality."""
+ # Setup test data with explicit empty images arrays
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Make it blue", "images": []}, # Explicit empty array
+ {"text": "<html>Blue code</html>", "images": []}
+ ]
+ }
+
+ # Mock the system prompts and image cache function
+ mock_system_prompts: Dict[str, str] = {self.TEST_STACK: self.MOCK_SYSTEM_PROMPT}
+
+ with patch("prompts.SYSTEM_PROMPTS", mock_system_prompts), \
+ patch("prompts.create_alt_url_mapping", return_value={}):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure - should be text-only messages
+ expected: ExpectedResult = {
+ "messages": [
+ {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": self.TEST_IMAGE_URL,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "<CONTAINS:Generate code for a web page that looks exactly like this.>",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Initial code</html>"},
+ {"role": "user", "content": "Make it blue"}, # Text-only message
+ {"role": "assistant", "content": "<html>Blue code</html>"},
+ ],
+ "image_cache": {},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ 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
+ ref_image_url: str = "data:image/png;base64,ref_image"
+ params: Dict[str, Any] = {
+ "isImportedFromCode": True,
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Original imported code</html>", "images": []},
+ {"text": "Update with this reference", "images": [ref_image_url]},
+ {"text": "<html>Updated code</html>", "images": []}
+ ]
+ }
+
+ # Mock the imported code system prompts
+ mock_imported_prompts: Dict[str, str] = {
+ self.TEST_STACK: "Mock Imported Code System Prompt"
+ }
+
+ with patch("prompts.IMPORTED_CODE_SYSTEM_PROMPTS", mock_imported_prompts):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure
+ expected: ExpectedResult = {
+ "messages": [
+ {
+ "role": "system",
+ "content": "Mock Imported Code System Prompt\n Here is the code of the app: <html>Original imported code</html>",
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": ref_image_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "Update with this reference",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Updated code</html>"},
+ ],
+ "image_cache": {},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ assert_structure_match(actual, expected)
diff --git a/backend/tests/test_prompts_additional.py b/backend/tests/test_prompts_additional.py
new file mode 100644
index 00000000..8d60ecbe
--- /dev/null
+++ b/backend/tests/test_prompts_additional.py
@@ -0,0 +1,269 @@
+import pytest
+from unittest.mock import patch, MagicMock
+import sys
+from typing import Any, Dict, List, TypedDict
+from openai.types.chat import ChatCompletionMessageParam
+
+# Mock moviepy before importing prompts
+sys.modules["moviepy"] = MagicMock()
+sys.modules["moviepy.editor"] = MagicMock()
+
+from prompts import create_prompt
+from prompts.types import Stack
+
+# Type definitions for test structures
+class ExpectedResult(TypedDict):
+ messages: List[ChatCompletionMessageParam]
+ image_cache: Dict[str, str]
+
+
+def assert_structure_match(actual: object, expected: object, path: str = "") -> None:
+ """
+ Compare actual and expected structures with special markers:
+ - <ANY>: Matches any value
+ - <CONTAINS:text>: Checks if the actual value contains 'text'
+
+ Args:
+ actual: The actual value to check
+ expected: The expected value or pattern
+ path: Current path in the structure (for error messages)
+ """
+ if (
+ isinstance(expected, str)
+ and expected.startswith("<")
+ and expected.endswith(">")
+ ):
+ # Handle special markers
+ if expected == "<ANY>":
+ # Match any value
+ return
+ elif expected.startswith("<CONTAINS:") and expected.endswith(">"):
+ # Extract the text to search for
+ search_text = expected[10:-1] # Remove "<CONTAINS:" and ">"
+ assert isinstance(
+ actual, str
+ ), f"At {path}: expected string, got {type(actual).__name__}"
+ assert (
+ search_text in actual
+ ), f"At {path}: '{search_text}' not found in '{actual}'"
+ return
+
+ # Handle different types
+ if isinstance(expected, dict):
+ assert isinstance(
+ actual, dict
+ ), f"At {path}: expected dict, got {type(actual).__name__}"
+ expected_dict: Dict[str, object] = expected
+ actual_dict: Dict[str, object] = actual
+ for key, value in expected_dict.items():
+ assert key in actual_dict, f"At {path}: key '{key}' not found in actual"
+ assert_structure_match(actual_dict[key], value, f"{path}.{key}" if path else key)
+ elif isinstance(expected, list):
+ assert isinstance(
+ actual, list
+ ), f"At {path}: expected list, got {type(actual).__name__}"
+ expected_list: List[object] = expected
+ actual_list: List[object] = actual
+ assert len(actual_list) == len(
+ expected_list
+ ), f"At {path}: list length mismatch (expected {len(expected_list)}, got {len(actual_list)})"
+ for i, (a, e) in enumerate(zip(actual_list, expected_list)):
+ assert_structure_match(a, e, f"{path}[{i}]")
+ else:
+ # Direct comparison for other types
+ assert actual == expected, f"At {path}: expected {expected}, got {actual}"
+
+
+class TestCreatePromptImageSupport:
+ """Test cases for create_prompt function with image support in updates."""
+
+ # Test data constants
+ TEST_IMAGE_URL: str = "data:image/png;base64,test_image_data"
+ MOCK_SYSTEM_PROMPT: str = "Mock HTML Tailwind system prompt"
+ TEST_STACK: Stack = "html_tailwind"
+
+ @pytest.mark.asyncio
+ async def test_image_mode_update_with_multiple_images_in_history(self) -> None:
+ """Test update with user message containing multiple images."""
+ # Setup test data
+ example1_url: str = "data:image/png;base64,example1"
+ example2_url: str = "data:image/png;base64,example2"
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Style like these examples", "images": [example1_url, example2_url]},
+ {"text": "<html>Styled code</html>", "images": []}
+ ]
+ }
+
+ # Mock the system prompts and image cache function
+ mock_system_prompts: Dict[str, str] = {self.TEST_STACK: self.MOCK_SYSTEM_PROMPT}
+
+ with patch("prompts.SYSTEM_PROMPTS", mock_system_prompts), \
+ patch("prompts.create_alt_url_mapping", return_value={"mock": "cache"}):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure
+ expected: ExpectedResult = {
+ "messages": [
+ {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": self.TEST_IMAGE_URL,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "<CONTAINS:Generate code for a web page that looks exactly like this.>",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Initial code</html>"},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": example1_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": example2_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "Style like these examples",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Styled code</html>"},
+ ],
+ "image_cache": {"mock": "cache"},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ assert_structure_match(actual, expected)
+
+ @pytest.mark.asyncio
+ async def test_update_with_empty_images_arrays(self) -> None:
+ """Test that empty images arrays don't break existing functionality."""
+ # Setup test data with explicit empty images arrays
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Make it blue", "images": []}, # Explicit empty array
+ {"text": "<html>Blue code</html>", "images": []}
+ ]
+ }
+
+ # Mock the system prompts and image cache function
+ mock_system_prompts: Dict[str, str] = {self.TEST_STACK: self.MOCK_SYSTEM_PROMPT}
+
+ with patch("prompts.SYSTEM_PROMPTS", mock_system_prompts), \
+ patch("prompts.create_alt_url_mapping", return_value={}):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure - should be text-only messages
+ expected: ExpectedResult = {
+ "messages": [
+ {"role": "system", "content": self.MOCK_SYSTEM_PROMPT},
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": self.TEST_IMAGE_URL,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "<CONTAINS:Generate code for a web page that looks exactly like this.>",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Initial code</html>"},
+ {"role": "user", "content": "Make it blue"}, # Text-only message
+ {"role": "assistant", "content": "<html>Blue code</html>"},
+ ],
+ "image_cache": {},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ 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
+ ref_image_url: str = "data:image/png;base64,ref_image"
+ params: Dict[str, Any] = {
+ "isImportedFromCode": True,
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Original imported code</html>", "images": []},
+ {"text": "Update with this reference", "images": [ref_image_url]},
+ {"text": "<html>Updated code</html>", "images": []}
+ ]
+ }
+
+ # Mock the imported code system prompts
+ mock_imported_prompts: Dict[str, str] = {
+ self.TEST_STACK: "Mock Imported Code System Prompt"
+ }
+
+ with patch("prompts.IMPORTED_CODE_SYSTEM_PROMPTS", mock_imported_prompts):
+ # Call the function
+ messages, image_cache = await create_prompt(params, self.TEST_STACK, "image")
+
+ # Define expected structure
+ expected: ExpectedResult = {
+ "messages": [
+ {
+ "role": "system",
+ "content": "Mock Imported Code System Prompt\n Here is the code of the app: <html>Original imported code</html>",
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": ref_image_url,
+ "detail": "high",
+ },
+ },
+ {
+ "type": "text",
+ "text": "Update with this reference",
+ },
+ ],
+ },
+ {"role": "assistant", "content": "<html>Updated code</html>"},
+ ],
+ "image_cache": {},
+ }
+
+ # Assert the structure matches
+ actual: ExpectedResult = {"messages": messages, "image_cache": image_cache}
+ assert_structure_match(actual, expected)
diff --git a/design-docs/images-in-update-history.md b/design-docs/images-in-update-history.md
new file mode 100644
index 00000000..c7aee5c0
--- /dev/null
+++ b/design-docs/images-in-update-history.md
@@ -0,0 +1,276 @@
+# Images in Update History - Backend Implementation Plan
+
+## Overview
+
+This document outlines the backend changes needed to support multiple images in update history items. Currently, the backend only processes the `text` field from history items and ignores the `images` field that already exists in the `PromptContent` structure.
+
+## Current State Analysis
+
+### ✅ What Already Works
+- History items have `PromptContent` structure with both `text` and `images` fields
+- Backend receives full history items with images field (but ignores it)
+- Image handling infrastructure exists for initial prompts
+
+### ❌ What's Missing
+- Backend only uses `item["text"]` and ignores `item["images"]` in history processing
+- No logic to create multipart content for user messages with images
+
+## Required Changes
+
+### 1. Core Function Update: `create_prompt()`
+
+**File**: `backend/prompts/__init__.py`
+
+#### 1.1 Update History Processing for Regular Updates (Lines 64-70)
+
+**Current Code:**
+```python
+if params["generationType"] == "update":
+ # Transform the history tree into message format
+ for index, item in enumerate(params["history"]):
+ role = "assistant" if index % 2 == 0 else "user"
+ message: ChatCompletionMessageParam = {
+ "role": role,
+ "content": item["text"],
+ }
+ prompt_messages.append(message)
+```
+
+**New Code:**
+```python
+if params["generationType"] == "update":
+ # Transform the history tree into message format
+ for index, item in enumerate(params["history"]):
+ role = "assistant" if index % 2 == 0 else "user"
+
+ # Check if this is a user message with images
+ if role == "user" and item.get("images") and len(item["images"]) > 0:
+ # Create multipart content for user messages with images
+ user_content: list[ChatCompletionContentPartParam] = []
+
+ # Add all images first
+ for image_url in item["images"]:
+ user_content.append({
+ "type": "image_url",
+ "image_url": {"url": image_url, "detail": "high"},
+ })
+
+ # Add text content
+ user_content.append({
+ "type": "text",
+ "text": item["text"],
+ })
+
+ message: ChatCompletionMessageParam = {
+ "role": role,
+ "content": user_content,
+ }
+ else:
+ # Regular text-only message (assistant messages or user messages without images)
+ message: ChatCompletionMessageParam = {
+ "role": role,
+ "content": item["text"],
+ }
+
+ prompt_messages.append(message)
+```
+
+#### 1.2 Update Imported Code History Processing (Lines 32-38)
+
+**Current Code:**
+```python
+for index, item in enumerate(params["history"][1:]):
+ role = "user" if index % 2 == 0 else "assistant"
+ message: ChatCompletionMessageParam = {
+ "role": role,
+ "content": item["text"],
+ }
+ prompt_messages.append(message)
+```
+
+**New Code:**
+```python
+for index, item in enumerate(params["history"][1:]):
+ role = "user" if index % 2 == 0 else "assistant"
+
+ # Apply same image handling logic for imported code history
+ if role == "user" and item.get("images") and len(item["images"]) > 0:
+ user_content: list[ChatCompletionContentPartParam] = []
+
+ for image_url in item["images"]:
+ user_content.append({
+ "type": "image_url",
+ "image_url": {"url": image_url, "detail": "high"},
+ })
+
+ user_content.append({
+ "type": "text",
+ "text": item["text"],
+ })
+
+ message: ChatCompletionMessageParam = {
+ "role": role,
+ "content": user_content,
+ }
+ else:
+ message: ChatCompletionMessageParam = {
+ "role": role,
+ "content": item["text"],
+ }
+
+ prompt_messages.append(message)
+```
+
+### 2. Extract Common Logic into Helper Function
+
+**Add New Function** (around line 80):
+
+```python
+def create_message_from_history_item(
+ item: dict[str, Any], role: str
+) -> ChatCompletionMessageParam:
+ """
+ Create a ChatCompletionMessageParam from a history item.
+ Handles both text-only and text+images content.
+ """
+ # Check if this is a user message with images
+ if role == "user" and item.get("images") and len(item["images"]) > 0:
+ # Create multipart content for user messages with images
+ user_content: list[ChatCompletionContentPartParam] = []
+
+ # Add all images first
+ for image_url in item["images"]:
+ user_content.append({
+ "type": "image_url",
+ "image_url": {"url": image_url, "detail": "high"},
+ })
+
+ # Add text content
+ user_content.append({
+ "type": "text",
+ "text": item["text"],
+ })
+
+ return {
+ "role": role,
+ "content": user_content,
+ }
+ else:
+ # Regular text-only message
+ return {
+ "role": role,
+ "content": item["text"],
+ }
+```
+
+**Then Update Both History Processing Sections:**
+
+```python
+# For regular updates:
+message = create_message_from_history_item(item, role)
+prompt_messages.append(message)
+
+# For imported code:
+message = create_message_from_history_item(item, role)
+prompt_messages.append(message)
+```
+
+### 3. Testing Requirements
+
+**Add to `tests/test_prompts.py`:**
+
+#### 3.1 Test Update with Single Image
+```python
+async def test_image_mode_update_with_single_image_in_history(self) -> None:
+ """Test update with user message containing a single image."""
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Add a button", "images": ["data:image/png;base64,reference_image"]},
+ {"text": "<html>Code with button</html>", "images": []}
+ ]
+ }
+ # Test that user message has multipart content with image + text
+```
+
+#### 3.2 Test Update with Multiple Images
+```python
+async def test_image_mode_update_with_multiple_images_in_history(self) -> None:
+ """Test update with user message containing multiple images."""
+ params: Dict[str, Any] = {
+ "prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Initial code</html>", "images": []},
+ {"text": "Style like these examples", "images": [
+ "data:image/png;base64,example1",
+ "data:image/png;base64,example2"
+ ]},
+ {"text": "<html>Styled code</html>", "images": []}
+ ]
+ }
+ # Test that user message has multipart content with 2 images + text
+```
+
+#### 3.3 Test Backward Compatibility
+```python
+async def test_update_with_empty_images_arrays(self) -> None:
+ """Test that empty images arrays don't break existing functionality."""
+ # Test with explicit empty arrays and missing images fields
+```
+
+#### 3.4 Test Imported Code with Images
+```python
+async def test_imported_code_update_with_images_in_history(self) -> None:
+ """Test imported code flow with images in update history."""
+ params: Dict[str, Any] = {
+ "isImportedFromCode": True,
+ "generationType": "update",
+ "history": [
+ {"text": "<html>Original imported code</html>", "images": []},
+ {"text": "Update with this reference", "images": ["data:image/png;base64,ref_image"]},
+ {"text": "<html>Updated code</html>", "images": []}
+ ]
+ }
+ # Test that imported code flow also handles images correctly
+```
+
+## Implementation Summary
+
+### Files to Modify
+1. `backend/prompts/__init__.py` - Main logic changes
+2. `backend/tests/test_prompts.py` - New test cases
+
+### Key Functions Updated
+1. `create_prompt()` - History processing logic for both regular and imported code updates
+2. **New**: `create_message_from_history_item()` - Helper function for DRY code
+3. **Tests**: 4 new test cases for comprehensive image support
+
+### Backward Compatibility
+- ✅ Existing history items without images continue to work
+- ✅ Empty images arrays are handled gracefully
+- ✅ Assistant messages (code) remain text-only
+- ✅ No breaking changes to existing API
+
+### Key Design Decisions
+
+1. **Only User Messages Support Images**: Assistant messages (generated code) remain text-only, which makes sense since they contain code, not user instructions.
+
+2. **Images First, Then Text**: In multipart content, images are added before text to maintain consistency with the initial prompt structure.
+
+3. **DRY Principle**: Common logic is extracted into a helper function to avoid code duplication between regular updates and imported code updates.
+
+4. **Graceful Degradation**: The system handles missing `images` fields and empty arrays without breaking.
+
+### Estimated Implementation Time
+- **Core changes**: 2-3 hours
+- **Tests**: 1-2 hours
+- **Total**: 3-5 hours
+
+This implementation will enable the frontend to send images with update instructions, allowing for more precise and visually-guided code modifications.