Skip to content

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

Python

Summarize prompt history refactor

Abi Raja515b915ff277
diff --git a/backend/prompts/builders.py b/backend/prompts/builders.py
index dcce2553..bf353967 100644
--- a/backend/prompts/builders.py
+++ b/backend/prompts/builders.py
@@ -1,10 +1,10 @@
-from typing import Any, cast
+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, Stack
+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
@@ -23,19 +23,32 @@ async def build_prompt_messages(
input_mode: InputMode,
generation_type: str,
prompt: PromptContent,
- history: list[dict[str, Any]],
+ history: list[PromptHistoryMessage],
is_imported_from_code: bool,
) -> list[ChatCompletionMessageParam]:
prompt_messages: list[ChatCompletionMessageParam] = []
if is_imported_from_code:
- original_imported_code = history[0]["text"] if history else ""
+ 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":
+ prompt_messages = [
+ cast(
+ ChatCompletionMessageParam,
+ {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
+ )
+ ]
+ for item in history:
+ prompt_messages.append(build_history_message(item))
else:
if input_mode == "image":
image_urls = prompt.get("images", [])
@@ -51,23 +64,18 @@ async def build_prompt_messages(
stack=stack,
)
elif input_mode == "video":
- if generation_type == "create":
+ 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:
- prompt_messages = [
- cast(
- ChatCompletionMessageParam,
- {"role": "system", "content": system_prompt.SYSTEM_PROMPT},
- )
- ]
+ 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", "")
@@ -77,33 +85,30 @@ async def build_prompt_messages(
text_prompt=text_prompt,
)
- if generation_type == "update":
- for index, item in enumerate(history):
- role = "assistant" if index % 2 == 0 else "user"
- message = build_history_message(item, role)
- prompt_messages.append(message)
-
return prompt_messages
-def build_history_message(
- item: dict[str, Any], role: str
-) -> ChatCompletionMessageParam:
- if role == "user" and item.get("images") and len(item["images"]) > 0:
+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 image_url in item["images"]:
+ for media_url in media_urls:
user_content.append(
{
"type": "image_url",
- "image_url": {"url": image_url, "detail": "high"},
+ "image_url": {"url": media_url, "detail": "high"},
}
)
user_content.append(
{
"type": "text",
- "text": item["text"],
+ "text": item.get("text", ""),
}
)
@@ -119,7 +124,7 @@ def build_history_message(
ChatCompletionMessageParam,
{
"role": role,
- "content": item["text"],
+ "content": item.get("text", ""),
},
)
@@ -145,56 +150,47 @@ def build_imported_code_prompt_messages(
},
build_history_message(
{
+ "role": "user",
"text": user_prompt.get("text", ""),
"images": user_prompt.get("images", []),
- },
- "user",
+ "videos": user_prompt.get("videos", []),
+ }
),
]
def resolve_imported_code_user_prompt(
prompt: PromptContent,
- history: list[dict[str, Any]],
+ history: list[PromptHistoryMessage],
) -> PromptContent:
prompt_text = prompt.get("text", "")
prompt_images = prompt.get("images", [])
- normalized_prompt_images: list[str] = []
- if isinstance(prompt_images, list):
- raw_prompt_images = cast(list[object], prompt_images)
- for image in raw_prompt_images:
- if isinstance(image, str):
- normalized_prompt_images.append(image)
+ prompt_videos = prompt.get("videos", [])
if prompt_text.strip():
return {
"text": prompt_text,
- "images": normalized_prompt_images,
+ "images": prompt_images,
+ "videos": prompt_videos,
}
- for index in range(len(history) - 1, 0, -1):
- if index % 2 == 1:
- item = history[index]
- text = item.get("text", "")
- images = item.get("images", [])
- normalized_images: list[str] = []
- if isinstance(images, list):
- raw_images = cast(list[object], images)
- for image in raw_images:
- if isinstance(image, str):
- normalized_images.append(image)
+ for item in reversed(history):
+ if item["role"] == "user":
return {
- "text": text if isinstance(text, str) else "",
- "images": normalized_images,
+ "text": item["text"],
+ "images": item.get("images", []),
+ "videos": item.get("videos", []),
}
- if normalized_prompt_images:
+ if prompt_images or prompt_videos:
return {
"text": "",
- "images": normalized_prompt_images,
+ "images": prompt_images,
+ "videos": prompt_videos,
}
return {
"text": "Update the imported code according to the latest request.",
"images": [],
+ "videos": [],
}
diff --git a/backend/prompts/prompt_types.py b/backend/prompts/prompt_types.py
index fad9456a..890dafae 100644
--- a/backend/prompts/prompt_types.py
+++ b/backend/prompts/prompt_types.py
@@ -1,4 +1,4 @@
-from typing import Literal, TypedDict, List
+from typing import List, Literal, TypedDict
class PromptContent(TypedDict):
@@ -6,6 +6,16 @@ class PromptContent(TypedDict):
text: str
images: List[str]
+ videos: List[str]
+
+
+class PromptHistoryMessage(TypedDict):
+ """Explicit role-based message structure for edit history."""
+
+ role: Literal["user", "assistant"]
+ text: str
+ images: List[str]
+ videos: List[str]
Stack = Literal[
diff --git a/backend/prompts/request_parsing.py b/backend/prompts/request_parsing.py
new file mode 100644
index 00000000..4ede6346
--- /dev/null
+++ b/backend/prompts/request_parsing.py
@@ -0,0 +1,51 @@
+from typing import List, cast
+
+from prompts.prompt_types import PromptContent, PromptHistoryMessage
+
+
+def _to_string_list(value: object) -> List[str]:
+ if not isinstance(value, list):
+ return []
+ raw_list = cast(List[object], value)
+ return [item for item in raw_list if isinstance(item, str)]
+
+
+def parse_prompt_content(raw_prompt: object) -> PromptContent:
+ if not isinstance(raw_prompt, dict):
+ return {"text": "", "images": [], "videos": []}
+
+ prompt_dict = cast(dict[str, object], raw_prompt)
+ text = prompt_dict.get("text")
+ return {
+ "text": text if isinstance(text, str) else "",
+ "images": _to_string_list(prompt_dict.get("images")),
+ "videos": _to_string_list(prompt_dict.get("videos")),
+ }
+
+
+def parse_prompt_history(raw_history: object) -> List[PromptHistoryMessage]:
+ if not isinstance(raw_history, list):
+ return []
+
+ history: List[PromptHistoryMessage] = []
+ raw_items = cast(List[object], raw_history)
+ for item in raw_items:
+ if not isinstance(item, dict):
+ continue
+
+ item_dict = cast(dict[str, object], item)
+ role_value = item_dict.get("role")
+ if not isinstance(role_value, str) or role_value not in ("user", "assistant"):
+ continue
+
+ text = item_dict.get("text")
+ history.append(
+ {
+ "role": role_value,
+ "text": text if isinstance(text, str) else "",
+ "images": _to_string_list(item_dict.get("images")),
+ "videos": _to_string_list(item_dict.get("videos")),
+ }
+ )
+
+ return history
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index 98274430..485ea915 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -34,7 +34,7 @@ from typing import (
)
from openai.types.chat import ChatCompletionMessageParam
-from utils import print_prompt_summary
+from utils import print_prompt_preview
# WebSocket message types
MessageType = Literal[
@@ -52,7 +52,8 @@ MessageType = Literal[
"toolResult",
]
from prompts.builders import build_prompt_messages
-from prompts.prompt_types import Stack, PromptContent
+from prompts.request_parsing import parse_prompt_content, parse_prompt_history
+from prompts.prompt_types import PromptContent, PromptHistoryMessage, Stack
from agent.runner import Agent
# from utils import pprint_prompt
@@ -220,7 +221,7 @@ class ExtractedParams:
openai_base_url: str | None
generation_type: Literal["create", "update"]
prompt: PromptContent
- history: List[Dict[str, Any]]
+ history: List[PromptHistoryMessage]
is_imported_from_code: bool
file_state: Dict[str, str] | None
option_codes: List[str]
@@ -280,25 +281,12 @@ class ParameterExtractionStage:
generation_type = cast(Literal["create", "update"], generation_type)
# Extract prompt content
- prompt: PromptContent = {"text": "", "images": []}
- raw_prompt = params.get("prompt")
- if isinstance(raw_prompt, dict):
- text = raw_prompt.get("text")
- images = raw_prompt.get("images")
- prompt = {
- "text": text if isinstance(text, str) else "",
- "images": [img for img in images if isinstance(img, str)]
- if isinstance(images, list)
- else [],
- }
+ prompt: PromptContent = parse_prompt_content(params.get("prompt"))
# Extract history (default to empty list)
- history: List[Dict[str, Any]] = []
- raw_history = params.get("history")
- if isinstance(raw_history, list):
- for item in raw_history:
- if isinstance(item, dict):
- history.append(item)
+ history: List[PromptHistoryMessage] = parse_prompt_history(
+ params.get("history")
+ )
# Extract imported code flag
is_imported_from_code = bool(params.get("isImportedFromCode", False))
@@ -484,8 +472,7 @@ class PromptCreationStage:
history=extracted_params.history,
is_imported_from_code=extracted_params.is_imported_from_code,
)
-
- print_prompt_summary(prompt_messages, truncate=False)
+ print_prompt_preview(prompt_messages)
return prompt_messages
except Exception:
diff --git a/backend/tests/test_prompt_summary.py b/backend/tests/test_prompt_summary.py
index 644540eb..8ba80586 100644
--- a/backend/tests/test_prompt_summary.py
+++ b/backend/tests/test_prompt_summary.py
@@ -4,7 +4,12 @@ from typing import cast
from openai.types.chat import ChatCompletionMessageParam
-from utils import format_prompt_summary, print_prompt_summary
+from utils import (
+ format_prompt_preview,
+ format_prompt_summary,
+ print_prompt_preview,
+ print_prompt_summary,
+)
def test_format_prompt_summary():
@@ -127,3 +132,44 @@ def test_print_prompt_summary_no_truncate():
# Check that full content is shown
assert "shown in full when truncate=False" in output
assert "..." not in output
+
+
+def test_format_prompt_preview_collapses_long_content():
+ long_code = "<html>\n" + ("x" * 800) + "\n</html>"
+ messages = [
+ {"role": "system", "content": "short"},
+ {"role": "assistant", "content": long_code},
+ ]
+
+ preview = format_prompt_preview(
+ cast(list[ChatCompletionMessageParam], messages), max_chars_per_message=120
+ )
+
+ assert "1. SYSTEM" in preview
+ assert "2. ASSISTANT" in preview
+ assert "[collapsed " in preview
+
+
+def test_print_prompt_preview():
+ messages = [
+ {"role": "system", "content": "System message"},
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "User request"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAA"}},
+ ],
+ },
+ ]
+
+ captured_output = io.StringIO()
+ sys.stdout = captured_output
+
+ print_prompt_preview(cast(list[ChatCompletionMessageParam], messages))
+
+ sys.stdout = sys.__stdout__
+
+ output = captured_output.getvalue()
+ assert "PROMPT PREVIEW" in output
+ assert "1. SYSTEM" in output
+ assert "2. USER [1 media]" in output
diff --git a/backend/tests/test_prompts.py b/backend/tests/test_prompts.py
index 0c7f5edc..f7686d4d 100644
--- a/backend/tests/test_prompts.py
+++ b/backend/tests/test_prompts.py
@@ -138,10 +138,10 @@ class TestCreatePrompt:
"prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
"generationType": "update",
"history": [
- {"text": "<html>Initial code</html>"}, # Assistant's initial code
- {"text": "Make the background blue"}, # User's update request
- {"text": "<html>Updated code</html>"}, # Assistant's response
- {"text": "Add a header"}, # User's new request
+ {"role": "assistant", "text": "<html>Initial code</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Make the background blue", "images": [], "videos": []},
+ {"role": "assistant", "text": "<html>Updated code</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Add a header", "images": [], "videos": []},
],
}
@@ -160,22 +160,6 @@ class TestCreatePrompt:
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 the provided screenshot(s).>",
- },
- ],
- },
{"role": "assistant", "content": "<html>Initial code</html>"},
{"role": "user", "content": "Make the background blue"},
{"role": "assistant", "content": "<html>Updated code</html>"},
@@ -240,10 +224,10 @@ class TestCreatePrompt:
},
"generationType": "update",
"history": [
- {"text": "<html>Initial dashboard</html>"}, # Assistant's initial code
- {"text": "Add a sidebar"}, # User's update request
- {"text": "<html>Dashboard with sidebar</html>"}, # Assistant's response
- {"text": "Now add a navigation menu"} # User's new request
+ {"role": "assistant", "text": "<html>Initial dashboard</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Add a sidebar", "images": [], "videos": []},
+ {"role": "assistant", "text": "<html>Dashboard with sidebar</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Now add a navigation menu", "images": [], "videos": []},
]
}
with patch('prompts.system_prompt.SYSTEM_PROMPT', self.MOCK_SYSTEM_PROMPT):
@@ -264,10 +248,6 @@ class TestCreatePrompt:
"role": "system",
"content": self.MOCK_SYSTEM_PROMPT
},
- {
- "role": "user",
- "content": f"<CONTAINS:Generate UI for {text_description}>"
- },
{
"role": "assistant",
"content": "<html>Initial dashboard</html>"
@@ -355,9 +335,9 @@ class TestCreatePrompt:
"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": []}
+ {"role": "assistant", "text": "<html>Initial code</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Add a button", "images": [reference_image_url], "videos": []},
+ {"role": "assistant", "text": "<html>Code with button</html>", "images": [], "videos": []},
]
}
@@ -376,22 +356,6 @@ class TestCreatePrompt:
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 the provided screenshot(s).>",
- },
- ],
- },
{"role": "assistant", "content": "<html>Initial code</html>"},
{
"role": "user",
@@ -427,9 +391,9 @@ class TestCreatePrompt:
"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": []}
+ {"role": "assistant", "text": "<html>Initial code</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Style like these examples", "images": [example1_url, example2_url], "videos": []},
+ {"role": "assistant", "text": "<html>Styled code</html>", "images": [], "videos": []},
]
}
@@ -448,22 +412,6 @@ class TestCreatePrompt:
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 the provided screenshot(s).>",
- },
- ],
- },
{"role": "assistant", "content": "<html>Initial code</html>"},
{
"role": "user",
@@ -504,9 +452,9 @@ class TestCreatePrompt:
"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": []}
+ {"role": "assistant", "text": "<html>Initial code</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Make it blue", "images": [], "videos": []},
+ {"role": "assistant", "text": "<html>Blue code</html>", "images": [], "videos": []},
]
}
@@ -525,22 +473,6 @@ class TestCreatePrompt:
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 the provided screenshot(s).>",
- },
- ],
- },
{"role": "assistant", "content": "<html>Initial code</html>"},
{"role": "user", "content": "Make it blue"}, # Text-only message
{"role": "assistant", "content": "<html>Blue code</html>"},
@@ -560,9 +492,9 @@ class TestCreatePrompt:
"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": []}
+ {"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": []},
]
}
@@ -572,7 +504,7 @@ class TestCreatePrompt:
stack=self.TEST_STACK,
input_mode="image",
generation_type=params["generationType"],
- prompt=params.get("prompt", {"text": "", "images": []}),
+ prompt=params.get("prompt", {"text": "", "images": [], "videos": []}),
history=params.get("history", []),
is_imported_from_code=params.get("isImportedFromCode", False),
)
@@ -615,8 +547,8 @@ class TestCreatePrompt:
"generationType": "update",
"prompt": {"text": "", "images": [self.TEST_IMAGE_URL]},
"history": [
- {"text": "<html>Original imported code</html>", "images": []},
- {"text": "Make the header blue", "images": []},
+ {"role": "assistant", "text": "<html>Original imported code</html>", "images": [], "videos": []},
+ {"role": "user", "text": "Make the header blue", "images": [], "videos": []},
],
}
diff --git a/backend/tests/test_prompts_additional.py b/backend/tests/test_prompts_additional.py
deleted file mode 100644
index 502f6f3c..00000000
--- a/backend/tests/test_prompts_additional.py
+++ /dev/null
@@ -1,272 +0,0 @@
-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.builders import build_prompt_messages
-from prompts.prompt_types import Stack
-
-# Type definitions for test structures
-class ExpectedResult(TypedDict):
- messages: List[ChatCompletionMessageParam]
-
-
-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": []}
- ]
- }
-
- 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["prompt"],
- history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
- )
-
- # 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 the provided screenshot(s).>",
- },
- ],
- },
- {"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>"},
- ],
- }
-
- # Assert the structure matches
- actual: ExpectedResult = {"messages": messages}
- 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": []}
- ]
- }
-
- 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["prompt"],
- history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
- )
-
- # 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 the provided screenshot(s).>",
- },
- ],
- },
- {"role": "assistant", "content": "<html>Initial code</html>"},
- {"role": "user", "content": "Make it blue"}, # Text-only message
- {"role": "assistant", "content": "<html>Blue code</html>"},
- ],
- }
-
- # Assert the structure matches
- actual: ExpectedResult = {"messages": messages}
- 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": []}
- ]
- }
-
- 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": []}),
- history=params.get("history", []),
- is_imported_from_code=params.get("isImportedFromCode", False),
- )
-
- # Define expected structure
- expected: ExpectedResult = {
- "messages": [
- {
- "role": "system",
- "content": "<CONTAINS:continuing from an imported codebase>",
- },
- {
- "role": "user",
- "content": [
- {
- "type": "image_url",
- "image_url": {
- "url": ref_image_url,
- "detail": "high",
- },
- },
- {
- "type": "text",
- "text": "Update with this reference",
- },
- ],
- },
- ],
- }
-
- # Assert the structure matches
- actual: ExpectedResult = {"messages": messages}
- assert_structure_match(actual, expected)
diff --git a/backend/tests/test_request_parsing.py b/backend/tests/test_request_parsing.py
new file mode 100644
index 00000000..08ede3a0
--- /dev/null
+++ b/backend/tests/test_request_parsing.py
@@ -0,0 +1,104 @@
+from prompts.request_parsing import parse_prompt_content, parse_prompt_history
+
+
+def test_parse_prompt_content_with_valid_data() -> None:
+ result = parse_prompt_content(
+ {
+ "text": "Build this page",
+ "images": ["img1", "img2"],
+ "videos": ["vid1"],
+ }
+ )
+
+ assert result == {
+ "text": "Build this page",
+ "images": ["img1", "img2"],
+ "videos": ["vid1"],
+ }
+
+
+def test_parse_prompt_content_filters_invalid_media_types() -> None:
+ result = parse_prompt_content(
+ {
+ "text": "Prompt",
+ "images": ["img1", 123, None, "img2"],
+ "videos": ["vid1", {"x": 1}, "vid2"],
+ }
+ )
+
+ assert result == {
+ "text": "Prompt",
+ "images": ["img1", "img2"],
+ "videos": ["vid1", "vid2"],
+ }
+
+
+def test_parse_prompt_content_defaults_for_invalid_payload() -> None:
+ assert parse_prompt_content(None) == {"text": "", "images": [], "videos": []}
+ assert parse_prompt_content("bad") == {"text": "", "images": [], "videos": []}
+ assert parse_prompt_content({"text": 1}) == {"text": "", "images": [], "videos": []}
+
+
+def test_parse_prompt_history_with_valid_entries() -> None:
+ result = parse_prompt_history(
+ [
+ {
+ "role": "assistant",
+ "text": "<html/>",
+ "images": [],
+ "videos": [],
+ },
+ {
+ "role": "user",
+ "text": "Please update",
+ "images": ["img1"],
+ "videos": ["vid1"],
+ },
+ ]
+ )
+
+ assert result == [
+ {
+ "role": "assistant",
+ "text": "<html/>",
+ "images": [],
+ "videos": [],
+ },
+ {
+ "role": "user",
+ "text": "Please update",
+ "images": ["img1"],
+ "videos": ["vid1"],
+ },
+ ]
+
+
+def test_parse_prompt_history_filters_invalid_items() -> None:
+ result = parse_prompt_history(
+ [
+ "bad",
+ {"role": "tool", "text": "skip me"},
+ {"role": "user", "text": "keep me", "images": ["img1", 3], "videos": [None, "vid1"]},
+ {"role": "assistant", "text": 123, "images": "bad", "videos": "bad"},
+ ]
+ )
+
+ assert result == [
+ {
+ "role": "user",
+ "text": "keep me",
+ "images": ["img1"],
+ "videos": ["vid1"],
+ },
+ {
+ "role": "assistant",
+ "text": "",
+ "images": [],
+ "videos": [],
+ },
+ ]
+
+
+def test_parse_prompt_history_defaults_for_invalid_payload() -> None:
+ assert parse_prompt_history(None) == []
+ assert parse_prompt_history({"role": "user"}) == []
diff --git a/backend/utils.py b/backend/utils.py
index 719c0a1b..c89338f3 100644
--- a/backend/utils.py
+++ b/backend/utils.py
@@ -1,5 +1,6 @@
import copy
import json
+import textwrap
from typing import List
from openai.types.chat import ChatCompletionMessageParam
@@ -75,6 +76,99 @@ def print_prompt_summary(prompt_messages: List[ChatCompletionMessageParam], trun
print()
+def _collapse_preview_text(text: str, max_chars: int = 280) -> str:
+ trimmed = text.strip()
+ if not trimmed:
+ return "(no text)"
+
+ looks_like_code = any(
+ marker in trimmed for marker in ("```", "<html", "<!DOCTYPE", "function ", "class ")
+ )
+ normalized = trimmed if looks_like_code else " ".join(trimmed.split())
+
+ if len(normalized) <= max_chars:
+ return normalized
+
+ head_len = max_chars // 2
+ tail_len = max_chars // 4
+ head = normalized[:head_len].rstrip()
+ tail = normalized[-tail_len:].lstrip()
+ omitted = max(0, len(normalized) - len(head) - len(tail))
+ return f"{head} ... [collapsed {omitted} chars] ... {tail}"
+
+
+def format_prompt_preview(
+ prompt_messages: List[ChatCompletionMessageParam],
+ max_chars_per_message: int = 280,
+) -> str:
+ parts: list[str] = []
+ for idx, message in enumerate(prompt_messages):
+ role = str(message.get("role", "unknown")).upper()
+ content = message.get("content")
+ text_chunks: list[str] = []
+ media_count = 0
+
+ if isinstance(content, list):
+ for item in content:
+ if not isinstance(item, dict):
+ continue
+ item_type = item.get("type")
+ if item_type == "image_url":
+ media_count += 1
+ elif item_type == "text":
+ item_text = item.get("text")
+ if isinstance(item_text, str):
+ text_chunks.append(item_text)
+ else:
+ text_chunks.append("" if content is None else str(content))
+
+ preview_text = _collapse_preview_text(
+ "\n".join(text_chunks), max_chars=max_chars_per_message
+ )
+ media_suffix = f" [{media_count} media]" if media_count else ""
+ parts.append(f"{idx + 1}. {role}{media_suffix}")
+ wrapped_preview = textwrap.wrap(
+ preview_text, width=100, break_long_words=False, break_on_hyphens=False
+ )
+ if wrapped_preview:
+ for line in wrapped_preview:
+ parts.append(f" {line}")
+ else:
+ parts.append(" (no text)")
+
+ return "\n".join(parts)
+
+
+def print_prompt_preview(prompt_messages: List[ChatCompletionMessageParam]) -> None:
+ preview = format_prompt_preview(prompt_messages)
+ lines = preview.split("\n")
+ max_length = max(len(line) for line in lines) if lines else 20
+ max_length = max(20, min(120, max_length))
+
+ title = "PROMPT PREVIEW"
+ max_length = max(max_length, len(title) + 4)
+
+ print("┌─" + "─" * max_length + "─┐")
+ title_padding = (max_length - len(title)) // 2
+ print(
+ f"│ {' ' * title_padding}{title}{' ' * (max_length - len(title) - title_padding)} │"
+ )
+ print("├─" + "─" * max_length + "─┤")
+
+ for line in lines:
+ if len(line) <= max_length:
+ print(f"│ {line:<{max_length}} │")
+ else:
+ wrapped = textwrap.wrap(
+ line, width=max_length, break_long_words=False, break_on_hyphens=False
+ )
+ for wrapped_line in wrapped:
+ print(f"│ {wrapped_line:<{max_length}} │")
+
+ print("└─" + "─" * max_length + "─┘")
+ print()
+
+
def truncate_data_strings(data: List[ChatCompletionMessageParam]): # type: ignore
# Deep clone the data to avoid modifying the original object
cloned_data = copy.deepcopy(data)
diff --git a/design-docs/prompt-history-refactor.md b/design-docs/prompt-history-refactor.md
new file mode 100644
index 00000000..7d236a1b
--- /dev/null
+++ b/design-docs/prompt-history-refactor.md
@@ -0,0 +1,145 @@
+# Prompt History Refactor (Frontend -> Backend)
+
+## Goal
+Simplify edit prompt history so we no longer reconstruct conversation state from commit ancestry and index parity.
+
+The new model is:
+- Frontend stores explicit conversation history per variant.
+- Frontend sends explicit role-based history to backend.
+- Backend trusts structured history and only assembles messages.
+
+## Key decisions (final)
+- No `sourceVersionNumber` field is required.
+ - The selected variant is the source of truth for "what history to extend next".
+- No backend persistence/migration work is needed for this refactor.
+ - Prompt history state is frontend-side session/project state.
+- No legacy reconstruction parity path.
+ - Old reconstruction/index-parity behavior is removed rather than preserved.
+
+## Previous behavior (removed)
+Before this refactor:
+- Frontend rebuilt history via `extractHistory(...)` by walking commit parent links.
+- Backend inferred message role from array index parity (`assistant` for even, `user` for odd).
+- History state was implicit and brittle, especially with branching and variant selection.
+
+## New frontend storage model
+
+### 1) Per-variant explicit history
+Each variant now carries its own history:
+- `Variant.history: VariantHistoryMessage[]`
+- `VariantHistoryMessage`:
+ - `role: "user" | "assistant"`
+ - `text: string`
+ - `imageAssetIds: string[]`
+ - `videoAssetIds: string[]`
+
+This history is authoritative for that variant.
+
+### 2) Shared media asset store
+Frontend keeps media in one shared map:
+- `assetsById: Record<string, PromptAsset>`
+- `PromptAsset`:
+ - `id`
+ - `type: "image" | "video"`
+ - `dataUrl`
+
+Variant history references media by ID, not by embedding large base64 strings directly.
+
+### 3) Utilities extracted from `App.tsx`
+Prompt-history/media helper logic moved to:
+- `frontend/src/lib/prompt-history.ts`
+
+Key helpers:
+- `cloneVariantHistory(...)`
+- `registerAssetIds(...)`
+- `toRequestHistory(...)`
+- `buildUserHistoryMessage(...)`
+- `buildAssistantHistoryMessage(...)`
+
+## How requests are built now
+
+### Create flow
+- Create seeds `variantHistory` with a single `user` message.
+- Images/videos are registered in `assetsById` and referenced by IDs in the variant history.
+- Request payload includes:
+ - `prompt` (create input)
+ - `variantHistory` (for local commit state)
+
+### Update flow
+- Uses the currently selected variant as source of truth.
+- Base history = selected variant history (or fallback assistant snapshot of current code if empty).
+- Appends new `user` update message (+ optional media IDs).
+- Converts variant history into request history (`role`, `text`, `images`, `videos`) via `toRequestHistory(...)`.
+
+### Completion behavior
+On variant completion:
+- Final generated code is appended as an `assistant` history message for that specific variant.
+
+## Branching behavior
+We still keep flat version labels (v1, v2, ...), but edits can branch from any selected version/variant.
+
+Important detail:
+- The active selected variant's explicit history is what gets extended for the next edit.
+- This naturally supports branching without reconstructing from global commit ancestry.
+
+## Request payload shape (simplified)
+When sending an edit, frontend sends explicit role history like:
+- `history[i].role`: `"user"` or `"assistant"`
+- `history[i].text`: textual instruction or generated code
+- `history[i].images`: data URLs for image inputs for that message
+- `history[i].videos`: data URLs for video inputs for that message
+
+Backend does not infer roles by index anymore; it uses the provided role directly.
+
+## Backend parsing and prompt assembly
+
+### Request parsing extracted
+Raw request normalization moved into:
+- `backend/prompts/request_parsing.py`
+
+Functions:
+- `parse_prompt_content(raw_prompt)`
+- `parse_prompt_history(raw_history)`
+
+`generate_code.py` now calls these helpers, so the route file is smaller and parsing is centralized.
+
+### Prompt assembly changes
+`backend/prompts/builders.py` now:
+- Consumes explicit `PromptHistoryMessage` entries.
+- Uses provided `role` directly (no index parity inference).
+- For update generation, builds:
+ - `system` message
+ - followed by all provided explicit history messages
+
+### Imported-code path
+Imported-code update logic now works with explicit role history and chooses the latest relevant `user` instruction cleanly.
+
+## Logging/observability updates
+- Removed runtime `PROMPT SUMMARY` logging from generation path.
+- Added compact `PROMPT PREVIEW` logging before model execution.
+- Large text/code is collapsed for readability.
+
+## Tests updated/added
+
+### Backend
+- Updated prompt assembly expectations to explicit role history:
+ - `backend/tests/test_prompts.py`
+- Merged and removed duplicate file:
+ - deleted `backend/tests/test_prompts_additional.py`
+- Added request parser tests:
+ - `backend/tests/test_request_parsing.py`
+- Added prompt preview tests:
+ - `backend/tests/test_prompt_summary.py`
+
+### Frontend
+- Removed legacy `extractHistory` tests and updated fixtures:
+ - `frontend/src/components/history/utils.test.ts`
+- Added helper tests for extracted prompt-history utilities:
+ - `frontend/src/lib/prompt-history.test.ts`
+
+## Net result
+The prompt-history pipeline is now explicit, variant-local, and much easier to reason about:
+- No implicit role inference.
+- No tree reconstruction for edit prompts.
+- Cleaner branch handling via selected variant history.
+- Smaller route parsing surface via dedicated parser module.
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 022b5800..6e21d7cd 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -1,18 +1,26 @@
import { useEffect, useRef, useState } from "react";
import { generateCode } from "./generateCode";
-import { AppState, CodeGenerationParams, EditorTheme, Settings } from "./types";
+import { AppState, EditorTheme, Settings } from "./types";
import { IS_RUNNING_ON_CLOUD } from "./config";
import { PicoBadge } from "./components/messages/PicoBadge";
import { OnboardingNote } from "./components/messages/OnboardingNote";
import { usePersistedState } from "./hooks/usePersistedState";
import TermsOfServiceDialog from "./components/TermsOfServiceDialog";
import { USER_CLOSE_WEB_SOCKET_CODE } from "./constants";
-import { extractHistory } from "./components/history/utils";
import toast from "react-hot-toast";
+import { nanoid } from "nanoid";
import { Stack } from "./lib/stacks";
import { CodeGenerationModel } from "./lib/models";
import useBrowserTabIndicator from "./hooks/useBrowserTabIndicator";
import { LuChevronLeft } from "react-icons/lu";
+import {
+ buildAssistantHistoryMessage,
+ buildUserHistoryMessage,
+ cloneVariantHistory,
+ GenerationRequest,
+ registerAssetIds,
+ toRequestHistory,
+} from "./lib/prompt-history";
// import TipLink from "./components/messages/TipLink";
import { useAppStore } from "./store/app-store";
import { useProjectStore } from "./store/project-store";
@@ -35,6 +43,8 @@ function App() {
setReferenceImages,
initialPrompt,
setInitialPrompt,
+ upsertPromptAssets,
+ resetPromptAssets,
head,
commits,
@@ -48,6 +58,7 @@ function App() {
updateVariantStatus,
resizeVariants,
setVariantModels,
+ appendVariantHistoryMessage,
startAgentEvent,
appendAgentEventContent,
finishAgentEvent,
@@ -109,6 +120,8 @@ function App() {
}
}, [settings.generatedCodeConfig, setSettings]);
+ const getAssetsById = () => useProjectStore.getState().assetsById;
+
// Functions
const reset = () => {
setAppState(AppState.INITIAL);
@@ -119,6 +132,7 @@ function App() {
resetCommits();
resetHead();
+ resetPromptAssets();
// Inputs
setInputMode("image");
@@ -176,39 +190,51 @@ function App() {
}
};
- function doGenerateCode(params: CodeGenerationParams) {
+ function doGenerateCode(params: GenerationRequest) {
// Reset the execution console
resetExecutionConsoles();
// Set the app state to coding during generation
setAppState(AppState.CODING);
+ const { variantHistory, ...requestParams } = params;
+
// Merge settings with params
- const updatedParams = { ...params, ...settings };
+ const updatedParams = { ...requestParams, ...settings };
// Create variants dynamically - start with 4 to handle most cases
// Backend will use however many it needs (typically 3)
const baseCommitObject = {
variants: Array(4)
.fill(null)
- .map(() => ({ code: "" })),
+ .map(() => ({
+ code: "",
+ history: cloneVariantHistory(variantHistory),
+ })),
};
+ const latestHistoryMessage =
+ requestParams.history && requestParams.history.length > 0
+ ? requestParams.history[requestParams.history.length - 1]
+ : null;
+
const commitInputObject =
- params.generationType === "create"
+ requestParams.generationType === "create"
? {
...baseCommitObject,
type: "ai_create" as const,
parentHash: null,
- inputs: params.prompt,
+ inputs: requestParams.prompt,
}
: {
...baseCommitObject,
type: "ai_edit" as const,
parentHash: head,
- inputs: params.history
- ? params.history[params.history.length - 1]
- : { text: "", images: [] },
+ inputs: {
+ text: latestHistoryMessage?.text || "",
+ images: latestHistoryMessage?.images || [],
+ videos: latestHistoryMessage?.videos || [],
+ },
};
// Create a new commit and set it as the head
@@ -232,6 +258,16 @@ function App() {
onVariantComplete: (variantIndex) => {
console.log(`Variant ${variantIndex} complete event received`);
updateVariantStatus(commit.hash, variantIndex, "complete");
+ const currentCode =
+ useProjectStore.getState().commits[commit.hash]?.variants[variantIndex]
+ ?.code || "";
+ if (currentCode.trim().length > 0) {
+ appendVariantHistoryMessage(
+ commit.hash,
+ variantIndex,
+ buildAssistantHistoryMessage(currentCode)
+ );
+ }
const lastThinking = lastThinkingEventIdRef.current[variantIndex];
if (lastThinking) {
finishAgentEvent(commit.hash, variantIndex, lastThinking, {
@@ -395,12 +431,40 @@ function App() {
// Kick off the code generation
if (referenceImages.length > 0) {
- const images =
+ const media =
inputMode === "video" ? [referenceImages[0]] : referenceImages;
+ const imageAssetIds =
+ inputMode === "image"
+ ? registerAssetIds(
+ "image",
+ media,
+ getAssetsById,
+ upsertPromptAssets,
+ nanoid
+ )
+ : [];
+ const videoAssetIds =
+ inputMode === "video"
+ ? registerAssetIds(
+ "video",
+ media,
+ getAssetsById,
+ upsertPromptAssets,
+ nanoid
+ )
+ : [];
+ const variantHistory = [
+ buildUserHistoryMessage(textPrompt, imageAssetIds, videoAssetIds),
+ ];
doGenerateCode({
generationType: "create",
inputMode,
- prompt: { text: textPrompt, images },
+ prompt: {
+ text: textPrompt,
+ images: inputMode === "image" ? media : [],
+ videos: inputMode === "video" ? media : [],
+ },
+ variantHistory,
});
}
}
@@ -414,7 +478,8 @@ function App() {
doGenerateCode({
generationType: "create",
inputMode: "text",
- prompt: { text, images: [] },
+ prompt: { text, images: [], videos: [] },
+ variantHistory: [buildUserHistoryMessage(text)],
});
}
@@ -442,16 +507,6 @@ function App() {
(variant) => variant.code || ""
);
- let historyTree;
- try {
- historyTree = extractHistory(head, commits);
- } catch {
- toast.error(
- "Version history is invalid. This shouldn't happen. Please contact support or open a Github issue."
- );
- throw new Error("Invalid version history");
- }
-
let modifiedUpdateInstruction = updateInstruction;
// Send in a reference to the selected element if it exists
@@ -462,21 +517,39 @@ function App() {
selectedElement.outerHTML;
}
- const updatedHistory = [
- ...historyTree,
- { text: modifiedUpdateInstruction, images: updateImages },
+ const selectedVariant = currentCommit.variants[currentCommit.selectedVariantIndex];
+ const baseVariantHistory =
+ selectedVariant.history.length > 0
+ ? selectedVariant.history
+ : currentCode.trim().length > 0
+ ? [buildAssistantHistoryMessage(currentCode)]
+ : [];
+ const updateImageAssetIds = registerAssetIds(
+ "image",
+ updateImages,
+ getAssetsById,
+ upsertPromptAssets,
+ nanoid
+ );
+ const updatedVariantHistory = [
+ ...cloneVariantHistory(baseVariantHistory),
+ buildUserHistoryMessage(modifiedUpdateInstruction, updateImageAssetIds),
];
+ const updatedHistory = toRequestHistory(updatedVariantHistory, getAssetsById);
doGenerateCode({
generationType: "update",
inputMode,
prompt:
inputMode === "text"
- ? { text: initialPrompt, images: [] }
- : { text: "", images: [referenceImages[0]] },
+ ? { text: initialPrompt, images: [], videos: [] }
+ : inputMode === "video"
+ ? { text: "", images: [], videos: [referenceImages[0]] }
+ : { text: "", images: [referenceImages[0]], videos: [] },
history: updatedHistory,
isImportedFromCode,
optionCodes,
+ variantHistory: updatedVariantHistory,
fileState: currentCode
? {
path: "index.html",
@@ -517,7 +590,7 @@ function App() {
const commit = createCommit({
type: "code_create",
parentHash: null,
- variants: [{ code }],
+ variants: [{ code, history: [buildAssistantHistoryMessage(code)] }],
inputs: null,
});
addCommit(commit);
diff --git a/frontend/src/components/commits/types.ts b/frontend/src/components/commits/types.ts
index 6b666a1d..353623c2 100644
--- a/frontend/src/components/commits/types.ts
+++ b/frontend/src/components/commits/types.ts
@@ -1,3 +1,5 @@
+import { PromptContent, PromptMessageRole } from "../../types";
+
export type CommitHash = string;
export type VariantStatus = "generating" | "complete" | "cancelled" | "error";
@@ -17,8 +19,16 @@ export type AgentEvent = {
endedAt?: number;
};
+export type VariantHistoryMessage = {
+ role: PromptMessageRole;
+ text: string;
+ imageAssetIds: string[];
+ videoAssetIds: string[];
+};
+
export type Variant = {
code: string;
+ history: VariantHistoryMessage[];
status?: VariantStatus;
errorMessage?: string;
thinking?: string;
@@ -39,8 +49,6 @@ export type BaseCommit = {
export type CommitType = "ai_create" | "ai_edit" | "code_create";
-import { PromptContent } from "../../types";
-
export type AiCreateCommit = BaseCommit & {
type: "ai_create";
inputs: PromptContent;
diff --git a/frontend/src/components/history/utils.test.ts b/frontend/src/components/history/utils.test.ts
index 24063737..33041be0 100644
--- a/frontend/src/components/history/utils.test.ts
+++ b/frontend/src/components/history/utils.test.ts
@@ -1,4 +1,4 @@
-import { extractHistory, renderHistory } from "./utils";
+import { renderHistory } from "./utils";
import { Commit, CommitHash } from "../commits/types";
const basicLinearHistory: Record<CommitHash, Commit> = {
@@ -8,7 +8,7 @@ const basicLinearHistory: Record<CommitHash, Commit> = {
isCommitted: false,
type: "ai_create",
parentHash: null,
- variants: [{ code: "<html>1. create</html>" }],
+ variants: [{ code: "<html>1. create</html>", history: [] }],
selectedVariantIndex: 0,
inputs: { text: "", images: [""] },
},
@@ -18,7 +18,7 @@ const basicLinearHistory: Record<CommitHash, Commit> = {
isCommitted: false,
type: "ai_edit",
parentHash: "0",
- variants: [{ code: "<html>2. edit with better icons</html>" }],
+ variants: [{ code: "<html>2. edit with better icons</html>", history: [] }],
selectedVariantIndex: 0,
inputs: { text: "use better icons", images: [] },
},
@@ -28,7 +28,7 @@ const basicLinearHistory: Record<CommitHash, Commit> = {
isCommitted: false,
type: "ai_edit",
parentHash: "1",
- variants: [{ code: "<html>3. edit with better icons and red text</html>" }],
+ variants: [{ code: "<html>3. edit with better icons and red text</html>", history: [] }],
selectedVariantIndex: 0,
inputs: { text: "make text red", images: [] },
},
@@ -41,7 +41,7 @@ const basicLinearHistoryWithCode: Record<CommitHash, Commit> = {
isCommitted: false,
type: "code_create",
parentHash: null,
- variants: [{ code: "<html>1. create</html>" }],
+ variants: [{ code: "<html>1. create</html>", history: [] }],
selectedVariantIndex: 0,
inputs: null,
},
@@ -57,105 +57,14 @@ const basicBranchingHistory: Record<CommitHash, Commit> = {
type: "ai_edit",
parentHash: "1",
variants: [
- { code: "<html>4. edit with better icons and green text</html>" },
+ { code: "<html>4. edit with better icons and green text</html>", history: [] },
],
selectedVariantIndex: 0,
inputs: { text: "make text green", images: [] },
},
};
-const longerBranchingHistory: Record<CommitHash, Commit> = {
- ...basicBranchingHistory,
- "4": {
- hash: "4",
- dateCreated: new Date(),
- isCommitted: false,
- type: "ai_edit",
- parentHash: "3",
- variants: [
- { code: "<html>5. edit with better icons and green, bold text</html>" },
- ],
- selectedVariantIndex: 0,
- inputs: { text: "make text bold", images: [] },
- },
-};
-
-const basicBadHistory: Record<CommitHash, Commit> = {
- "0": {
- hash: "0",
- dateCreated: new Date(),
- isCommitted: false,
- type: "ai_create",
- parentHash: null,
- variants: [{ code: "<html>1. create</html>" }],
- selectedVariantIndex: 0,
- inputs: { text: "", images: [""] },
- },
- "1": {
- hash: "1",
- dateCreated: new Date(),
- isCommitted: false,
- type: "ai_edit",
- parentHash: "2", // <- Bad parent hash
- variants: [{ code: "<html>2. edit with better icons</html>" }],
- selectedVariantIndex: 0,
- inputs: { text: "use better icons", images: [] },
- },
-};
-
describe("History Utils", () => {
- test("should correctly extract the history tree", () => {
- expect(extractHistory("2", basicLinearHistory)).toEqual([
- { text: "<html>1. create</html>", images: [] },
- { text: "use better icons", images: [] },
- { text: "<html>2. edit with better icons</html>", images: [] },
- { text: "make text red", images: [] },
- { text: "<html>3. edit with better icons and red text</html>", images: [] },
- ]);
-
- expect(extractHistory("0", basicLinearHistory)).toEqual([
- { text: "<html>1. create</html>", images: [] },
- ]);
-
- // Test branching
- expect(extractHistory("3", basicBranchingHistory)).toEqual([
- { text: "<html>1. create</html>", images: [] },
- { text: "use better icons", images: [] },
- { text: "<html>2. edit with better icons</html>", images: [] },
- { text: "make text green", images: [] },
- { text: "<html>4. edit with better icons and green text</html>", images: [] },
- ]);
-
- expect(extractHistory("4", longerBranchingHistory)).toEqual([
- { text: "<html>1. create</html>", images: [] },
- { text: "use better icons", images: [] },
- { text: "<html>2. edit with better icons</html>", images: [] },
- { text: "make text green", images: [] },
- { text: "<html>4. edit with better icons and green text</html>", images: [] },
- { text: "make text bold", images: [] },
- {
- text: "<html>5. edit with better icons and green, bold text</html>",
- images: [],
- },
- ]);
-
- expect(extractHistory("2", longerBranchingHistory)).toEqual([
- { text: "<html>1. create</html>", images: [] },
- { text: "use better icons", images: [] },
- { text: "<html>2. edit with better icons</html>", images: [] },
- { text: "make text red", images: [] },
- { text: "<html>3. edit with better icons and red text</html>", images: [] },
- ]);
-
- // Errors
-
- // Bad hash
- expect(() => extractHistory("100", basicLinearHistory)).toThrow();
-
- // Bad tree
- expect(() => extractHistory("1", basicBadHistory)).toThrow();
- });
-
test("should correctly render the history tree", () => {
expect(renderHistory(Object.values(basicLinearHistory))).toEqual([
{
diff --git a/frontend/src/components/history/utils.ts b/frontend/src/components/history/utils.ts
index ae30c8fd..2c0f7482 100644
--- a/frontend/src/components/history/utils.ts
+++ b/frontend/src/components/history/utils.ts
@@ -1,36 +1,4 @@
-import { Commit, CommitHash, CommitType } from "../commits/types";
-import { PromptContent } from "../../types";
-
-export function extractHistory(
- hash: CommitHash,
- commits: Record<CommitHash, Commit>
-): PromptContent[] {
- const flatHistory: PromptContent[] = [];
-
- let currentCommitHash: CommitHash | null = hash;
- while (currentCommitHash !== null) {
- const commit: Commit | null = commits[currentCommitHash];
-
- if (commit) {
- flatHistory.unshift({
- text: commit.variants[commit.selectedVariantIndex].code,
- images: [],
- });
-
- // For edits, add the prompt to the history
- if (commit.type === "ai_edit") {
- flatHistory.unshift(commit.inputs);
- }
-
- // Move to the parent of the current item
- currentCommitHash = commit.parentHash;
- } else {
- throw new Error("Malformed history: missing parent index");
- }
- }
-
- return flatHistory;
-}
+import { Commit, CommitType } from "../commits/types";
function displayHistoryItemType(itemType: CommitType) {
switch (itemType) {
diff --git a/frontend/src/lib/prompt-history.test.ts b/frontend/src/lib/prompt-history.test.ts
new file mode 100644
index 00000000..e2791915
--- /dev/null
+++ b/frontend/src/lib/prompt-history.test.ts
@@ -0,0 +1,123 @@
+import {
+ buildAssistantHistoryMessage,
+ buildUserHistoryMessage,
+ cloneVariantHistory,
+ registerAssetIds,
+ toRequestHistory,
+} from "./prompt-history";
+import { PromptAsset } from "../types";
+
+describe("prompt-history helpers", () => {
+ test("cloneVariantHistory deep-copies asset id arrays", () => {
+ const source = [
+ {
+ role: "user" as const,
+ text: "Update this",
+ imageAssetIds: ["img-1"],
+ videoAssetIds: ["vid-1"],
+ },
+ ];
+
+ const cloned = cloneVariantHistory(source);
+ cloned[0].imageAssetIds.push("img-2");
+ cloned[0].videoAssetIds.push("vid-2");
+
+ expect(source[0].imageAssetIds).toEqual(["img-1"]);
+ expect(source[0].videoAssetIds).toEqual(["vid-1"]);
+ expect(cloned[0].imageAssetIds).toEqual(["img-1", "img-2"]);
+ expect(cloned[0].videoAssetIds).toEqual(["vid-1", "vid-2"]);
+ });
+
+ test("registerAssetIds reuses existing ids and only upserts new assets", () => {
+ const assetsById: Record<string, PromptAsset> = {
+ existing: { id: "existing", type: "image", dataUrl: "data:image/one" },
+ };
+ const upsertCalls: PromptAsset[][] = [];
+
+ let idCounter = 0;
+ const ids = registerAssetIds(
+ "image",
+ ["data:image/one", "data:image/two", "data:image/two"],
+ () => assetsById,
+ (assets) => {
+ upsertCalls.push(assets);
+ for (const asset of assets) assetsById[asset.id] = asset;
+ },
+ () => `generated-${++idCounter}`
+ );
+
+ expect(ids[0]).toBe("existing");
+ expect(ids[1]).toBe(ids[2]);
+ expect(upsertCalls).toHaveLength(1);
+ expect(upsertCalls[0]).toHaveLength(1);
+ expect(upsertCalls[0][0].type).toBe("image");
+ expect(upsertCalls[0][0].dataUrl).toBe("data:image/two");
+ });
+
+ test("registerAssetIds ignores blank media strings", () => {
+ const upsertCalls: PromptAsset[][] = [];
+ const ids = registerAssetIds(
+ "video",
+ ["", " "],
+ () => ({}),
+ (assets) => upsertCalls.push(assets),
+ () => "unused"
+ );
+
+ expect(ids).toEqual([]);
+ expect(upsertCalls).toEqual([]);
+ });
+
+ test("toRequestHistory resolves ids and drops missing assets", () => {
+ const assetsById: Record<string, PromptAsset> = {
+ img1: { id: "img1", type: "image", dataUrl: "data:image/one" },
+ vid1: { id: "vid1", type: "video", dataUrl: "data:video/one" },
+ };
+
+ const history = [
+ {
+ role: "user" as const,
+ text: "Do this",
+ imageAssetIds: ["img1", "img-missing"],
+ videoAssetIds: ["vid1", "vid-missing"],
+ },
+ {
+ role: "assistant" as const,
+ text: "<html/>",
+ imageAssetIds: [],
+ videoAssetIds: [],
+ },
+ ];
+
+ expect(toRequestHistory(history, () => assetsById)).toEqual([
+ {
+ role: "user",
+ text: "Do this",
+ images: ["data:image/one"],
+ videos: ["data:video/one"],
+ },
+ {
+ role: "assistant",
+ text: "<html/>",
+ images: [],
+ videos: [],
+ },
+ ]);
+ });
+
+ test("message builders produce expected shapes", () => {
+ expect(buildUserHistoryMessage("change", ["img"], ["vid"])).toEqual({
+ role: "user",
+ text: "change",
+ imageAssetIds: ["img"],
+ videoAssetIds: ["vid"],
+ });
+
+ expect(buildAssistantHistoryMessage("<html/>")).toEqual({
+ role: "assistant",
+ text: "<html/>",
+ imageAssetIds: [],
+ videoAssetIds: [],
+ });
+ });
+});
diff --git a/frontend/src/lib/prompt-history.ts b/frontend/src/lib/prompt-history.ts
new file mode 100644
index 00000000..9cf7cdc5
--- /dev/null
+++ b/frontend/src/lib/prompt-history.ts
@@ -0,0 +1,109 @@
+import { VariantHistoryMessage } from "../components/commits/types";
+import {
+ CodeGenerationParams,
+ PromptAsset,
+ PromptAssetType,
+ PromptHistoryMessage,
+} from "../types";
+
+export type GenerationRequest = CodeGenerationParams & {
+ variantHistory: VariantHistoryMessage[];
+};
+
+type AssetsById = Record<string, PromptAsset>;
+type GetAssetsById = () => AssetsById;
+type UpsertPromptAssets = (assets: PromptAsset[]) => void;
+type CreateId = () => string;
+
+export function cloneVariantHistory(
+ history: VariantHistoryMessage[]
+): VariantHistoryMessage[] {
+ return history.map((message) => ({
+ ...message,
+ imageAssetIds: [...message.imageAssetIds],
+ videoAssetIds: [...message.videoAssetIds],
+ }));
+}
+
+export function registerAssetIds(
+ type: PromptAssetType,
+ dataUrls: string[],
+ getAssetsById: GetAssetsById,
+ upsertPromptAssets: UpsertPromptAssets,
+ createId: CreateId
+): string[] {
+ const existingByUrl = new Map<string, string>();
+ Object.values(getAssetsById()).forEach((asset) => {
+ if (asset.type === type) {
+ existingByUrl.set(asset.dataUrl, asset.id);
+ }
+ });
+
+ const newAssets: PromptAsset[] = [];
+ const ids: string[] = [];
+
+ for (const rawDataUrl of dataUrls) {
+ const dataUrl = rawDataUrl.trim();
+ if (!dataUrl) continue;
+
+ let id = existingByUrl.get(dataUrl);
+ if (!id) {
+ id = createId();
+ existingByUrl.set(dataUrl, id);
+ newAssets.push({ id, type, dataUrl });
+ }
+ ids.push(id);
+ }
+
+ if (newAssets.length > 0) {
+ upsertPromptAssets(newAssets);
+ }
+
+ return ids;
+}
+
+export function resolveAssetIdsToDataUrls(
+ assetIds: string[],
+ getAssetsById: GetAssetsById
+): string[] {
+ const assetsById = getAssetsById();
+ return assetIds
+ .map((assetId) => assetsById[assetId]?.dataUrl)
+ .filter((value): value is string => Boolean(value));
+}
+
+export function toRequestHistory(
+ history: VariantHistoryMessage[],
+ getAssetsById: GetAssetsById
+): PromptHistoryMessage[] {
+ return history.map((message) => ({
+ role: message.role,
+ text: message.text,
+ images: resolveAssetIdsToDataUrls(message.imageAssetIds, getAssetsById),
+ videos: resolveAssetIdsToDataUrls(message.videoAssetIds, getAssetsById),
+ }));
+}
+
+export function buildUserHistoryMessage(
+ text: string,
+ imageAssetIds: string[] = [],
+ videoAssetIds: string[] = []
+): VariantHistoryMessage {
+ return {
+ role: "user",
+ text,
+ imageAssetIds,
+ videoAssetIds,
+ };
+}
+
+export function buildAssistantHistoryMessage(
+ code: string
+): VariantHistoryMessage {
+ return {
+ role: "assistant",
+ text: code,
+ imageAssetIds: [],
+ videoAssetIds: [],
+ };
+}
diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts
index 594f47d6..4277426a 100644
--- a/frontend/src/store/project-store.ts
+++ b/frontend/src/store/project-store.ts
@@ -3,8 +3,10 @@ import {
AgentEvent,
Commit,
CommitHash,
+ VariantHistoryMessage,
VariantStatus,
} from "../components/commits/types";
+import { PromptAsset } from "../types";
// Store for app-wide state
interface ProjectStore {
@@ -17,6 +19,9 @@ interface ProjectStore {
setReferenceImages: (images: string[]) => void;
initialPrompt: string;
setInitialPrompt: (prompt: string) => void;
+ assetsById: Record<string, PromptAsset>;
+ upsertPromptAssets: (assets: PromptAsset[]) => void;
+ resetPromptAssets: () => void;
// Outputs
commits: Record<string, Commit>;
@@ -38,6 +43,11 @@ interface ProjectStore {
thinking: string
) => void;
setCommitCode: (hash: CommitHash, numVariant: number, code: string) => void;
+ appendVariantHistoryMessage: (
+ hash: CommitHash,
+ numVariant: number,
+ message: VariantHistoryMessage
+ ) => void;
updateSelectedVariantIndex: (hash: CommitHash, index: number) => void;
updateVariantStatus: (
hash: CommitHash,
@@ -84,6 +94,17 @@ export const useProjectStore = create<ProjectStore>((set) => ({
setReferenceImages: (images) => set({ referenceImages: images }),
initialPrompt: "",
setInitialPrompt: (prompt) => set({ initialPrompt: prompt }),
+ assetsById: {},
+ upsertPromptAssets: (assets) =>
+ set((state) => {
+ if (assets.length === 0) return state;
+ const merged = { ...state.assetsById };
+ for (const asset of assets) {
+ merged[asset.id] = asset;
+ }
+ return { assetsById: merged };
+ }),
+ resetPromptAssets: () => set({ assetsById: {} }),
// Outputs
commits: {},
@@ -96,6 +117,7 @@ export const useProjectStore = create<ProjectStore>((set) => ({
...commit,
variants: commit.variants.map((variant) => ({
...variant,
+ history: variant.history || [],
status: variant.status || ("generating" as VariantStatus),
thinkingStartTime: Date.now(),
agentEvents: [],
@@ -200,6 +222,30 @@ export const useProjectStore = create<ProjectStore>((set) => ({
},
};
}),
+ appendVariantHistoryMessage: (hash, numVariant, message) =>
+ set((state) => {
+ const commit = state.commits[hash];
+ if (!commit || commit.isCommitted) return state;
+ const variants = commit.variants.map((variant, index) => {
+ if (index !== numVariant) return variant;
+ const history = variant.history || [];
+ const last = history[history.length - 1];
+ const isDuplicate =
+ last &&
+ last.role === message.role &&
+ last.text === message.text &&
+ last.imageAssetIds.join("|") === message.imageAssetIds.join("|") &&
+ last.videoAssetIds.join("|") === message.videoAssetIds.join("|");
+ if (isDuplicate) return variant;
+ return { ...variant, history: [...history, message] };
+ });
+ return {
+ commits: {
+ ...state.commits,
+ [hash]: { ...commit, variants },
+ },
+ };
+ }),
updateSelectedVariantIndex: (hash: CommitHash, index: number) =>
set((state) => {
const commit = state.commits[hash];
@@ -253,8 +299,18 @@ export const useProjectStore = create<ProjectStore>((set) => ({
// Resize variants array to match backend count
const currentVariants = commit.variants;
+ const seedHistory = currentVariants[0]?.history || [];
const newVariants = Array(count).fill(null).map((_, index) =>
- currentVariants[index] || { code: "", status: "generating" as VariantStatus, agentEvents: [] }
+ currentVariants[index] || {
+ code: "",
+ history: seedHistory.map((message) => ({
+ ...message,
+ imageAssetIds: [...message.imageAssetIds],
+ videoAssetIds: [...message.videoAssetIds],
+ })),
+ status: "generating" as VariantStatus,
+ agentEvents: [],
+ }
);
return {
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 260e657e..7eb6f659 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -31,16 +31,33 @@ export enum ScreenRecorderState {
FINISHED = "finished",
}
+export type PromptMessageRole = "user" | "assistant";
+export type PromptAssetType = "image" | "video";
+
+export interface PromptAsset {
+ id: string;
+ type: PromptAssetType;
+ dataUrl: string;
+}
+
export interface PromptContent {
text: string;
images: string[]; // Array of data URLs
+ videos?: string[]; // Array of data URLs
+}
+
+export interface PromptHistoryMessage {
+ role: PromptMessageRole;
+ text: string;
+ images: string[];
+ videos: string[];
}
export interface CodeGenerationParams {
generationType: "create" | "update";
inputMode: "image" | "video" | "text";
prompt: PromptContent;
- history?: PromptContent[];
+ history?: PromptHistoryMessage[];
isImportedFromCode?: boolean;
fileState?: {
path: string;