Skip to content

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

Python

initial version

Abi Rajaae92e9484498
diff --git a/backend/agentic/runner.py b/backend/agentic/runner.py
new file mode 100644
index 00000000..279b0361
--- /dev/null
+++ b/backend/agentic/runner.py
@@ -0,0 +1,902 @@
+import json
+import uuid
+from dataclasses import dataclass
+from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple
+
+from openai import AsyncOpenAI
+from openai.types.chat import ChatCompletionChunk, ChatCompletionMessageParam
+from anthropic import AsyncAnthropic
+from google import genai
+from google.genai import types
+
+from codegen.utils import extract_html_content
+from config import REPLICATE_API_KEY
+from image_generation.core import process_tasks
+from llm import Llm, OPENAI_MODELS, ANTHROPIC_MODELS, GEMINI_MODELS
+
+
+@dataclass
+class ToolCall:
+ id: str
+ name: str
+ arguments: Dict[str, Any]
+
+
+@dataclass
+class ToolExecutionResult:
+ ok: bool
+ result: Dict[str, Any]
+ summary: Dict[str, Any]
+ updated_content: Optional[str] = None
+
+
+@dataclass
+class AgentFileState:
+ path: str = "index.html"
+ content: str = ""
+
+
+def _summarize_text(value: str, limit: int = 240) -> str:
+ if len(value) <= limit:
+ return value
+ return value[:limit] + "..."
+
+
+def _ensure_str(value: Any) -> str:
+ if value is None:
+ return ""
+ return str(value)
+
+
+def _parse_json_arguments(raw_args: Any) -> Tuple[Dict[str, Any], Optional[str]]:
+ if isinstance(raw_args, dict):
+ return raw_args, None
+ if raw_args is None:
+ return {}, None
+ raw_text = _ensure_str(raw_args).strip()
+ if not raw_text:
+ return {}, None
+ try:
+ return json.loads(raw_text), None
+ except json.JSONDecodeError as exc:
+ return {}, f"Invalid JSON arguments: {exc}"
+
+
+class AgentToolbox:
+ def __init__(
+ self,
+ file_state: AgentFileState,
+ image_cache: Dict[str, str],
+ should_generate_images: bool,
+ openai_api_key: Optional[str],
+ openai_base_url: Optional[str],
+ ):
+ self.file_state = file_state
+ self.image_cache = image_cache
+ self.should_generate_images = should_generate_images
+ self.openai_api_key = openai_api_key
+ self.openai_base_url = openai_base_url
+
+ async def execute(self, tool_call: ToolCall) -> ToolExecutionResult:
+ if tool_call.name == "create_file":
+ return self._create_file(tool_call.arguments)
+ if tool_call.name == "edit_file":
+ return self._edit_file(tool_call.arguments)
+ if tool_call.name == "generate_images":
+ return await self._generate_images(tool_call.arguments)
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": f"Unknown tool: {tool_call.name}"},
+ summary={"error": f"Unknown tool: {tool_call.name}"},
+ )
+
+ def _create_file(self, args: Dict[str, Any]) -> ToolExecutionResult:
+ path = _ensure_str(args.get("path") or self.file_state.path or "index.html")
+ content = _ensure_str(args.get("content"))
+ if not content:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "create_file requires non-empty content"},
+ summary={"error": "Missing content"},
+ )
+
+ extracted = extract_html_content(content)
+ self.file_state.path = path
+ self.file_state.content = extracted or content
+
+ summary = {
+ "path": self.file_state.path,
+ "contentLength": len(self.file_state.content),
+ "preview": _summarize_text(self.file_state.content, 320),
+ }
+ result = {
+ "path": self.file_state.path,
+ "content": self.file_state.content,
+ }
+ return ToolExecutionResult(
+ ok=True,
+ result=result,
+ summary=summary,
+ updated_content=self.file_state.content,
+ )
+
+ def _apply_single_edit(
+ self, content: str, old_text: str, new_text: str, count: Optional[int]
+ ) -> Tuple[str, int]:
+ if old_text not in content:
+ return content, 0
+
+ if count is None:
+ replace_count = 1
+ elif count < 0:
+ replace_count = content.count(old_text)
+ else:
+ replace_count = count
+
+ updated = content.replace(old_text, new_text, replace_count)
+ return updated, min(replace_count, content.count(old_text))
+
+ def _edit_file(self, args: Dict[str, Any]) -> ToolExecutionResult:
+ if not self.file_state.content:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "No file exists yet. Call create_file first."},
+ summary={"error": "No file to edit"},
+ )
+
+ edits = args.get("edits")
+ if not edits:
+ old_text = _ensure_str(args.get("old_text"))
+ new_text = _ensure_str(args.get("new_text"))
+ count = args.get("count")
+ edits = [
+ {"old_text": old_text, "new_text": new_text, "count": count}
+ ]
+
+ if not isinstance(edits, list):
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "edits must be a list"},
+ summary={"error": "Invalid edits payload"},
+ )
+
+ content = self.file_state.content
+ summary_edits: List[Dict[str, Any]] = []
+ for edit in edits:
+ old_text = _ensure_str(edit.get("old_text"))
+ new_text = _ensure_str(edit.get("new_text"))
+ count = edit.get("count")
+ if not old_text:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "edit_file requires old_text"},
+ summary={"error": "Missing old_text"},
+ )
+
+ content, replaced = self._apply_single_edit(
+ content, old_text, new_text, count
+ )
+ if replaced == 0:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "old_text not found", "old_text": old_text},
+ summary={
+ "error": "old_text not found",
+ "old_text": _summarize_text(old_text, 160),
+ },
+ )
+
+ summary_edits.append(
+ {
+ "old_text": _summarize_text(old_text, 140),
+ "new_text": _summarize_text(new_text, 140),
+ "replaced": replaced,
+ }
+ )
+
+ self.file_state.content = content
+ summary = {
+ "path": self.file_state.path,
+ "edits": summary_edits,
+ "contentLength": len(self.file_state.content),
+ }
+ result = {
+ "path": self.file_state.path,
+ "content": self.file_state.content,
+ }
+ return ToolExecutionResult(
+ ok=True,
+ result=result,
+ summary=summary,
+ updated_content=self.file_state.content,
+ )
+
+ async def _generate_images(self, args: Dict[str, Any]) -> ToolExecutionResult:
+ if not self.should_generate_images:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "Image generation is disabled."},
+ summary={"error": "Image generation disabled"},
+ )
+
+ prompts = args.get("prompts") or []
+ if not isinstance(prompts, list) or not prompts:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "generate_images requires a non-empty prompts list"},
+ summary={"error": "Missing prompts"},
+ )
+
+ cleaned = [prompt.strip() for prompt in prompts if isinstance(prompt, str)]
+ unique_prompts = list(dict.fromkeys([p for p in cleaned if p]))
+ if not unique_prompts:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "No valid prompts provided"},
+ summary={"error": "No valid prompts"},
+ )
+
+ cached_results: Dict[str, Optional[str]] = {}
+ to_generate: List[str] = []
+ for prompt in unique_prompts:
+ if prompt in self.image_cache:
+ cached_results[prompt] = self.image_cache[prompt]
+ else:
+ to_generate.append(prompt)
+
+ generated_results: Dict[str, Optional[str]] = {}
+
+ if to_generate:
+ if REPLICATE_API_KEY:
+ model = "flux"
+ api_key = REPLICATE_API_KEY
+ base_url = None
+ else:
+ if not self.openai_api_key:
+ return ToolExecutionResult(
+ ok=False,
+ result={
+ "error": "No API key available for image generation."
+ },
+ summary={"error": "Missing image generation API key"},
+ )
+ model = "dalle3"
+ api_key = self.openai_api_key
+ base_url = self.openai_base_url
+
+ generated = await process_tasks(to_generate, api_key, base_url, model) # type: ignore
+ for prompt, url in zip(to_generate, generated):
+ generated_results[prompt] = url
+ if url:
+ self.image_cache[prompt] = url
+
+ merged_results = {**cached_results, **generated_results}
+ summary_items = [
+ {
+ "prompt": _summarize_text(prompt, 160),
+ "url": url,
+ "status": "ok" if url else "error",
+ }
+ for prompt, url in merged_results.items()
+ ]
+ result = {"images": merged_results}
+ summary = {"images": summary_items}
+ return ToolExecutionResult(ok=True, result=result, summary=summary)
+
+
+class AgenticRunner:
+ def __init__(
+ self,
+ send_message: Callable[[str, Optional[str], int, Optional[Dict[str, Any]], Optional[str]], Awaitable[None]],
+ variant_index: int,
+ openai_api_key: Optional[str],
+ openai_base_url: Optional[str],
+ anthropic_api_key: Optional[str],
+ gemini_api_key: Optional[str],
+ should_generate_images: bool,
+ image_cache: Dict[str, str],
+ ):
+ self.send_message = send_message
+ self.variant_index = variant_index
+ self.openai_api_key = openai_api_key
+ self.openai_base_url = openai_base_url
+ self.anthropic_api_key = anthropic_api_key
+ self.gemini_api_key = gemini_api_key
+ self.should_generate_images = should_generate_images
+ self.image_cache = image_cache
+
+ self.file_state = AgentFileState()
+ self.toolbox = AgentToolbox(
+ self.file_state,
+ self.image_cache,
+ should_generate_images,
+ openai_api_key,
+ openai_base_url,
+ )
+ self.assistant_step = 0
+ self.thinking_step = 0
+
+ def _next_event_id(self, prefix: str) -> str:
+ return f"{prefix}-{self.variant_index}-{uuid.uuid4().hex[:8]}"
+
+ async def _send(
+ self,
+ msg_type: str,
+ value: Optional[str] = None,
+ data: Optional[Dict[str, Any]] = None,
+ event_id: Optional[str] = None,
+ ) -> None:
+ await self.send_message(msg_type, value, self.variant_index, data, event_id)
+
+ def _summarize_tool_input(self, tool_call: ToolCall) -> Dict[str, Any]:
+ args = tool_call.arguments or {}
+ if tool_call.name == "create_file":
+ content = _ensure_str(args.get("content"))
+ return {
+ "path": args.get("path") or self.file_state.path,
+ "contentLength": len(content),
+ "preview": _summarize_text(content, 200),
+ }
+ if tool_call.name == "edit_file":
+ edits = args.get("edits")
+ if not edits:
+ edits = [
+ {
+ "old_text": args.get("old_text"),
+ "new_text": args.get("new_text"),
+ "count": args.get("count"),
+ }
+ ]
+ summary_edits = []
+ for edit in edits if isinstance(edits, list) else []:
+ summary_edits.append(
+ {
+ "old_text": _summarize_text(
+ _ensure_str(edit.get("old_text")), 160
+ ),
+ "new_text": _summarize_text(
+ _ensure_str(edit.get("new_text")), 160
+ ),
+ "count": edit.get("count"),
+ }
+ )
+ return {
+ "path": args.get("path") or self.file_state.path,
+ "edits": summary_edits,
+ }
+ if tool_call.name == "generate_images":
+ prompts = args.get("prompts") or []
+ if isinstance(prompts, list):
+ return {
+ "count": len(prompts),
+ "prompts": [_summarize_text(_ensure_str(p), 140) for p in prompts],
+ }
+ return args
+
+ def _tool_schemas(self) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]], List[types.Tool]]:
+ create_schema = {
+ "type": "object",
+ "properties": {
+ "path": {
+ "type": "string",
+ "description": "Path for the main HTML file. Use index.html if unsure.",
+ },
+ "content": {
+ "type": "string",
+ "description": "Full HTML for the single-file app.",
+ },
+ },
+ "required": ["content"],
+ }
+ edit_schema = {
+ "type": "object",
+ "properties": {
+ "path": {
+ "type": "string",
+ "description": "Path for the main HTML file.",
+ },
+ "old_text": {
+ "type": "string",
+ "description": "Exact text to replace. Must match the file contents.",
+ },
+ "new_text": {
+ "type": "string",
+ "description": "Replacement text.",
+ },
+ "count": {
+ "type": "integer",
+ "description": "How many occurrences to replace. Use -1 for all.",
+ },
+ "edits": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "old_text": {"type": "string"},
+ "new_text": {"type": "string"},
+ "count": {"type": "integer"},
+ },
+ "required": ["old_text", "new_text"],
+ },
+ },
+ },
+ }
+ image_schema = {
+ "type": "object",
+ "properties": {
+ "prompts": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "description": "Prompt describing a single image to generate.",
+ },
+ }
+ },
+ "required": ["prompts"],
+ }
+
+ openai_tools = [
+ {
+ "type": "function",
+ "function": {
+ "name": "create_file",
+ "description": "Create the main HTML file for the app. Use exactly once to write the full HTML. Returns the updated file content.",
+ "parameters": create_schema,
+ },
+ },
+ {
+ "type": "function",
+ "function": {
+ "name": "edit_file",
+ "description": "Edit the main HTML file using exact string replacements. Do not regenerate the entire file. Returns the updated file content.",
+ "parameters": edit_schema,
+ },
+ },
+ {
+ "type": "function",
+ "function": {
+ "name": "generate_images",
+ "description": "Generate image URLs from prompts. Use to replace placeholder images. You can pass multiple prompts at once.",
+ "parameters": image_schema,
+ },
+ },
+ ]
+
+ anthropic_tools = [
+ {
+ "name": "create_file",
+ "description": "Create the main HTML file for the app. Use exactly once to write the full HTML. Returns the updated file content.",
+ "input_schema": create_schema,
+ },
+ {
+ "name": "edit_file",
+ "description": "Edit the main HTML file using exact string replacements. Do not regenerate the entire file. Returns the updated file content.",
+ "input_schema": edit_schema,
+ },
+ {
+ "name": "generate_images",
+ "description": "Generate image URLs from prompts. Use to replace placeholder images. You can pass multiple prompts at once.",
+ "input_schema": image_schema,
+ },
+ ]
+
+ gemini_tools = [
+ types.Tool(
+ function_declarations=[
+ types.FunctionDeclaration(
+ name="create_file",
+ description="Create the main HTML file for the app. Use exactly once to write the full HTML. Returns the updated file content.",
+ parameters_json_schema=create_schema,
+ ),
+ types.FunctionDeclaration(
+ name="edit_file",
+ description="Edit the main HTML file using exact string replacements. Do not regenerate the entire file. Returns the updated file content.",
+ parameters_json_schema=edit_schema,
+ ),
+ types.FunctionDeclaration(
+ name="generate_images",
+ description="Generate image URLs from prompts. Use to replace placeholder images. You can pass multiple prompts at once.",
+ parameters_json_schema=image_schema,
+ ),
+ ]
+ )
+ ]
+
+ return openai_tools, anthropic_tools, gemini_tools
+
+ async def run(self, model: Llm, prompt_messages: List[ChatCompletionMessageParam]) -> str:
+ if model in OPENAI_MODELS:
+ return await self._run_openai(model, prompt_messages)
+ if model in ANTHROPIC_MODELS:
+ return await self._run_anthropic(model, prompt_messages)
+ if model in GEMINI_MODELS:
+ return await self._run_gemini(model, prompt_messages)
+ raise ValueError(f"Unsupported model: {model.value}")
+
+ async def _run_openai(
+ self, model: Llm, prompt_messages: List[ChatCompletionMessageParam]
+ ) -> str:
+ if not self.openai_api_key:
+ raise Exception("OpenAI API key is missing.")
+
+ client = AsyncOpenAI(api_key=self.openai_api_key, base_url=self.openai_base_url)
+ openai_tools, _, _ = self._tool_schemas()
+ messages: List[Dict[str, Any]] = [dict(m) for m in prompt_messages]
+
+ max_steps = 8
+ try:
+ for _ in range(max_steps):
+ assistant_event_id = self._next_event_id("assistant")
+ assistant_text = ""
+ tool_calls: Dict[int, Dict[str, Any]] = {}
+
+ params: Dict[str, Any] = {
+ "model": model.value,
+ "messages": messages,
+ "tools": openai_tools,
+ "tool_choice": "auto",
+ "temperature": 0,
+ "stream": True,
+ }
+ if model.value == "gpt-5.2-2025-12-11":
+ params["max_completion_tokens"] = 30000
+ else:
+ params["max_tokens"] = 30000
+
+ stream = await client.chat.completions.create(**params)
+
+ async for chunk in stream: # type: ignore
+ assert isinstance(chunk, ChatCompletionChunk)
+ if not chunk.choices:
+ continue
+ delta = chunk.choices[0].delta
+ if delta.content:
+ assistant_text += delta.content
+ await self._send(
+ "assistant", delta.content, event_id=assistant_event_id
+ )
+ if delta.tool_calls:
+ for tc in delta.tool_calls:
+ idx = tc.index or 0
+ entry = tool_calls.setdefault(
+ idx,
+ {
+ "id": tc.id or f"tool-{idx}-{uuid.uuid4().hex[:6]}",
+ "name": None,
+ "arguments": "",
+ },
+ )
+ if tc.id:
+ entry["id"] = tc.id
+ if tc.function and tc.function.name:
+ entry["name"] = tc.function.name
+ if tc.function and tc.function.arguments:
+ entry["arguments"] += tc.function.arguments
+
+ tool_call_list: List[ToolCall] = []
+ for entry in tool_calls.values():
+ args, error = _parse_json_arguments(entry.get("arguments"))
+ if error:
+ args = {"_raw": entry.get("arguments")}
+ tool_call_list.append(
+ ToolCall(
+ id=entry["id"],
+ name=entry.get("name") or "unknown_tool",
+ arguments=args,
+ )
+ )
+
+ if not tool_call_list:
+ return await self._finalize_response(assistant_text)
+
+ assistant_message = {
+ "role": "assistant",
+ "content": assistant_text or None,
+ "tool_calls": [
+ {
+ "id": tc.id,
+ "type": "function",
+ "function": {
+ "name": tc.name,
+ "arguments": json.dumps(tc.arguments),
+ },
+ }
+ for tc in tool_call_list
+ ],
+ }
+ messages.append(assistant_message)
+
+ for tool_call in tool_call_list:
+ tool_event_id = tool_call.id or self._next_event_id("tool")
+ await self._send(
+ "toolStart",
+ data={
+ "name": tool_call.name,
+ "input": self._summarize_tool_input(tool_call),
+ },
+ event_id=tool_event_id,
+ )
+ tool_result = await self.toolbox.execute(tool_call)
+ if tool_result.updated_content:
+ await self._send("setCode", tool_result.updated_content)
+
+ await self._send(
+ "toolResult",
+ data={
+ "name": tool_call.name,
+ "output": tool_result.summary,
+ "ok": tool_result.ok,
+ },
+ event_id=tool_event_id,
+ )
+
+ messages.append(
+ {
+ "role": "tool",
+ "tool_call_id": tool_call.id,
+ "content": json.dumps(tool_result.result),
+ }
+ )
+
+ raise Exception("Agent exceeded max tool turns")
+ finally:
+ await client.close()
+
+ async def _run_anthropic(
+ self, model: Llm, prompt_messages: List[ChatCompletionMessageParam]
+ ) -> str:
+ if not self.anthropic_api_key:
+ raise Exception("Anthropic API key is missing.")
+
+ from models.claude import convert_openai_messages_to_claude
+
+ system_prompt, claude_messages = convert_openai_messages_to_claude(
+ prompt_messages
+ )
+ client = AsyncAnthropic(api_key=self.anthropic_api_key)
+ _, anthropic_tools, _ = self._tool_schemas()
+
+ max_steps = 8
+ try:
+ for _ in range(max_steps):
+ assistant_event_id = self._next_event_id("assistant")
+ thinking_event_id = self._next_event_id("thinking")
+ assistant_text = ""
+
+ thinking_models = [
+ Llm.CLAUDE_4_5_SONNET_2025_09_29.value,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01.value,
+ ]
+
+ stream_kwargs: Dict[str, Any] = {
+ "model": model.value,
+ "max_tokens": 30000,
+ "system": system_prompt,
+ "messages": claude_messages,
+ "tools": anthropic_tools,
+ }
+
+ if model.value in thinking_models:
+ stream_kwargs["thinking"] = {
+ "type": "enabled",
+ "budget_tokens": 10000,
+ }
+ else:
+ stream_kwargs["temperature"] = 0.0
+
+ async with client.messages.stream(**stream_kwargs) as stream:
+ async for event in stream:
+ if event.type == "content_block_delta":
+ if event.delta.type == "thinking_delta":
+ await self._send(
+ "thinking",
+ event.delta.thinking,
+ event_id=thinking_event_id,
+ )
+ elif event.delta.type == "text_delta":
+ assistant_text += event.delta.text
+ await self._send(
+ "assistant",
+ event.delta.text,
+ event_id=assistant_event_id,
+ )
+
+ final_message = await stream.get_final_message()
+
+ tool_calls: List[ToolCall] = []
+ if final_message and final_message.content:
+ for block in final_message.content:
+ if block.type == "tool_use":
+ tool_calls.append(
+ ToolCall(
+ id=block.id,
+ name=block.name,
+ arguments=block.input,
+ )
+ )
+
+ if not tool_calls:
+ return await self._finalize_response(assistant_text)
+
+ assistant_blocks: List[Dict[str, Any]] = []
+ if assistant_text:
+ assistant_blocks.append({"type": "text", "text": assistant_text})
+ for call in tool_calls:
+ assistant_blocks.append(
+ {
+ "type": "tool_use",
+ "id": call.id,
+ "name": call.name,
+ "input": call.arguments,
+ }
+ )
+
+ claude_messages.append({"role": "assistant", "content": assistant_blocks})
+
+ tool_result_blocks: List[Dict[str, Any]] = []
+ for tool_call in tool_calls:
+ tool_event_id = tool_call.id or self._next_event_id("tool")
+ await self._send(
+ "toolStart",
+ data={
+ "name": tool_call.name,
+ "input": self._summarize_tool_input(tool_call),
+ },
+ event_id=tool_event_id,
+ )
+ tool_result = await self.toolbox.execute(tool_call)
+ if tool_result.updated_content:
+ await self._send("setCode", tool_result.updated_content)
+
+ await self._send(
+ "toolResult",
+ data={
+ "name": tool_call.name,
+ "output": tool_result.summary,
+ "ok": tool_result.ok,
+ },
+ event_id=tool_event_id,
+ )
+
+ tool_result_blocks.append(
+ {
+ "type": "tool_result",
+ "tool_use_id": tool_call.id,
+ "content": json.dumps(tool_result.result),
+ "is_error": not tool_result.ok,
+ }
+ )
+
+ claude_messages.append({"role": "user", "content": tool_result_blocks})
+
+ raise Exception("Agent exceeded max tool turns")
+ finally:
+ await client.close()
+
+ async def _run_gemini(
+ self, model: Llm, prompt_messages: List[ChatCompletionMessageParam]
+ ) -> str:
+ if not self.gemini_api_key:
+ raise Exception("Gemini API key is missing.")
+
+ from models.gemini import (
+ convert_message_to_gemini_content,
+ get_gemini_api_model_name,
+ get_thinking_level_for_model,
+ )
+
+ system_prompt = str(prompt_messages[0].get("content", ""))
+ gemini_contents: List[types.Content] = [
+ convert_message_to_gemini_content(msg) for msg in prompt_messages[1:]
+ ]
+
+ _, _, gemini_tools = self._tool_schemas()
+ client = genai.Client(api_key=self.gemini_api_key)
+
+ max_steps = 8
+ for _ in range(max_steps):
+ assistant_event_id = self._next_event_id("assistant")
+ thinking_event_id = self._next_event_id("thinking")
+ assistant_text = ""
+ tool_calls: List[ToolCall] = []
+ function_call_parts: List[types.Part] = []
+
+ thinking_level = get_thinking_level_for_model(model)
+ config = types.GenerateContentConfig(
+ temperature=0,
+ max_output_tokens=30000,
+ system_instruction=system_prompt,
+ thinking_config=types.ThinkingConfig(
+ thinking_level=thinking_level, include_thoughts=True
+ ),
+ tools=gemini_tools,
+ )
+
+ api_model_name = get_gemini_api_model_name(model)
+
+ async for chunk in await client.aio.models.generate_content_stream(
+ model=api_model_name,
+ contents=gemini_contents,
+ config=config,
+ ):
+ if not chunk.candidates:
+ continue
+ for part in chunk.candidates[0].content.parts:
+ if getattr(part, "thought", False) and part.text:
+ await self._send(
+ "thinking", part.text, event_id=thinking_event_id
+ )
+ continue
+ if part.function_call:
+ function_call_parts.append(part)
+ args = part.function_call.args or {}
+ tool_calls.append(
+ ToolCall(
+ id=part.function_call.id
+ or f"tool-{uuid.uuid4().hex[:6]}",
+ name=part.function_call.name or "unknown_tool",
+ arguments=args,
+ )
+ )
+ continue
+ if part.text:
+ assistant_text += part.text
+ await self._send(
+ "assistant", part.text, event_id=assistant_event_id
+ )
+
+ if not tool_calls:
+ return await self._finalize_response(assistant_text)
+
+ model_parts: List[Any] = []
+ if assistant_text:
+ model_parts.append({"text": assistant_text}) # type: ignore
+ model_parts.extend(function_call_parts)
+
+ model_content = types.Content(role="model", parts=model_parts)
+ gemini_contents.append(model_content)
+
+ tool_result_parts: List[types.Part] = []
+ for tool_call in tool_calls:
+ tool_event_id = tool_call.id or self._next_event_id("tool")
+ await self._send(
+ "toolStart",
+ data={
+ "name": tool_call.name,
+ "input": self._summarize_tool_input(tool_call),
+ },
+ event_id=tool_event_id,
+ )
+ tool_result = await self.toolbox.execute(tool_call)
+ if tool_result.updated_content:
+ await self._send("setCode", tool_result.updated_content)
+
+ await self._send(
+ "toolResult",
+ data={
+ "name": tool_call.name,
+ "output": tool_result.summary,
+ "ok": tool_result.ok,
+ },
+ event_id=tool_event_id,
+ )
+
+ tool_result_parts.append(
+ types.Part.from_function_response(
+ name=tool_call.name, response=tool_result.result
+ )
+ )
+
+ gemini_contents.append(types.Content(role="tool", parts=tool_result_parts))
+
+ raise Exception("Agent exceeded max tool turns")
+
+ async def _finalize_response(self, assistant_text: str) -> str:
+ if self.file_state.content:
+ return self.file_state.content
+
+ html = extract_html_content(assistant_text)
+ if html:
+ self.file_state.content = html
+ await self._send("setCode", html)
+ return self.file_state.content
diff --git a/backend/models/openai_client.py b/backend/models/openai_client.py
index e5ae8f8b..616b4eb9 100644
--- a/backend/models/openai_client.py
+++ b/backend/models/openai_client.py
@@ -31,7 +31,14 @@ async def stream_openai_response(
]:
params["temperature"] = 0
params["stream"] = True
- params["max_tokens"] = 20000
+ params["max_tokens"] = 30000
+
+ if model_name in [
+ "gpt-5.2-2025-12-11",
+ ]:
+ params["temperature"] = 0
+ params["stream"] = True
+ params["max_completion_tokens"] = 30000
stream = await client.chat.completions.create(**params) # type: ignore
full_response = ""
diff --git a/backend/prompts/agentic_instructions.py b/backend/prompts/agentic_instructions.py
new file mode 100644
index 00000000..ba276fdc
--- /dev/null
+++ b/backend/prompts/agentic_instructions.py
@@ -0,0 +1,20 @@
+TOOL_USE_INSTRUCTIONS = """
+Tooling instructions:
+- You have access to tools: create_file, edit_file, generate_images.
+- The main file is a single HTML file. Use path "index.html" unless told otherwise.
+- For a brand new app, call create_file exactly once with the full HTML.
+- For updates, call edit_file using exact string replacements. Do NOT regenerate the entire file.
+- Do not output raw HTML in chat. Any code changes must go through tools.
+- Use generate_images to create image URLs from prompts (you may pass multiple prompts).
+- Any non-tool output should be short, user-facing chat updates.
+"""
+
+
+def apply_tool_instructions(system_prompt: str, should_generate_images: bool) -> str:
+ image_flag = "enabled" if should_generate_images else "disabled"
+ return (
+ system_prompt.strip()
+ + "\n\n"
+ + TOOL_USE_INSTRUCTIONS.strip()
+ + f"\n\nImage generation is {image_flag}."
+ )
diff --git a/backend/prompts/imported_code_prompts.py b/backend/prompts/imported_code_prompts.py
index 07b12237..c146045d 100644
--- a/backend/prompts/imported_code_prompts.py
+++ b/backend/prompts/imported_code_prompts.py
@@ -14,8 +14,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IMPORTED_CODE_HTML_CSS_SYSTEM_PROMPT = """
@@ -30,8 +29,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IMPORTED_CODE_REACT_TAILWIND_SYSTEM_PROMPT = """
@@ -51,8 +49,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IMPORTED_CODE_BOOTSTRAP_SYSTEM_PROMPT = """
@@ -68,8 +65,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IMPORTED_CODE_IONIC_TAILWIND_SYSTEM_PROMPT = """
@@ -94,8 +90,7 @@ In terms of libraries,
<script nomodule src="https://cdn.jsdelivr.net/npm/ionicons/dist/esm/ionicons.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/ionicons/dist/collection/components/icon/icon.min.css" rel="stylesheet">
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IMPORTED_CODE_VUE_TAILWIND_SYSTEM_PROMPT = """
@@ -126,9 +121,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
-The return result must only include the code."""
+Follow the tool instructions in the system prompt."""
IMPORTED_CODE_SVG_SYSTEM_PROMPT = """
You are an expert at building SVGs.
@@ -138,8 +131,7 @@ You are an expert at building SVGs.
- For images, use placeholder images from https://placehold.co and include a detailed description of the image in the alt text so that an image generation AI can generate the image later.
- You can use Google Fonts
-Return only the full code in <svg></svg> tags.
-Do not include markdown "```" or "```svg" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IMPORTED_CODE_SYSTEM_PROMPTS = SystemPrompts(
diff --git a/backend/prompts/screenshot_system_prompts.py b/backend/prompts/screenshot_system_prompts.py
index f9a41c16..291ee3cb 100644
--- a/backend/prompts/screenshot_system_prompts.py
+++ b/backend/prompts/screenshot_system_prompts.py
@@ -20,8 +20,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
HTML_CSS_SYSTEM_PROMPT = """
@@ -42,8 +41,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
BOOTSTRAP_SYSTEM_PROMPT = """
@@ -65,8 +63,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
REACT_TAILWIND_SYSTEM_PROMPT = """
@@ -92,8 +89,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
IONIC_TAILWIND_SYSTEM_PROMPT = """
@@ -124,8 +120,7 @@ In terms of libraries,
<script nomodule src="https://cdn.jsdelivr.net/npm/ionicons/dist/esm/ionicons.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/ionicons/dist/collection/components/icon/icon.min.css" rel="stylesheet">
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
+Follow the tool instructions in the system prompt.
"""
VUE_TAILWIND_SYSTEM_PROMPT = """
@@ -163,9 +158,7 @@ In terms of libraries,
- You can use Google Fonts
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
-The return result must only include the code.
+Follow the tool instructions in the system prompt.
"""
@@ -182,8 +175,7 @@ padding, margin, border, etc. Match the colors and sizes exactly.
- For images, use placeholder images from https://placehold.co and include a detailed description of the image in the alt text so that an image generation AI can generate the image later.
- You can use Google Fonts
-Return only the full code in <svg></svg> tags.
-Do not include markdown "```" or "```svg" at the start or end.
+Follow the tool instructions in the system prompt.
"""
diff --git a/backend/prompts/text_prompts.py b/backend/prompts/text_prompts.py
index 178f6bbf..25dc1784 100644
--- a/backend/prompts/text_prompts.py
+++ b/backend/prompts/text_prompts.py
@@ -12,9 +12,7 @@ LIBRARY_INSTRUCTIONS = """
- Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>"""
FORMAT_INSTRUCTIONS = """
-Return only the full code in <html></html> tags.
-Do not include markdown "```" or "```html" at the start or end.
-Reply with only the code, and no text/explanation before and after the code.
+Follow the tool instructions in the system prompt. Do not output raw code directly.
"""
HTML_TAILWIND_SYSTEM_PROMPT = f"""
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index 6bfb2405..601311ab 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -58,12 +58,17 @@ MessageType = Literal[
"variantError",
"variantCount",
"thinking",
+ "assistant",
+ "toolStart",
+ "toolResult",
]
from image_generation.core import generate_images
from prompts import create_prompt
+from prompts.agentic_instructions import apply_tool_instructions
from prompts.claude_prompts import GEMINI_VIDEO_PROMPT
from prompts.types import Stack, PromptContent
from video.utils import get_video_bytes_and_mime_type
+from agentic.runner import AgenticRunner
# from utils import pprint_prompt
from ws.constants import APP_ERROR_WEB_SOCKET_CODE # type: ignore
@@ -170,8 +175,10 @@ class WebSocketCommunicator:
async def send_message(
self,
type: MessageType,
- value: str,
+ value: str | None,
variantIndex: int,
+ data: Dict[str, Any] | None = None,
+ eventId: str | None = None,
) -> None:
"""Send a message to the client with debug logging"""
if self.is_closed:
@@ -188,9 +195,14 @@ class WebSocketCommunicator:
print(f"Variant {variantIndex + 1} error: {value}")
try:
- await self.websocket.send_json(
- {"type": type, "value": value, "variantIndex": variantIndex}
- )
+ payload: Dict[str, Any] = {"type": type, "variantIndex": variantIndex}
+ if value is not None:
+ payload["value"] = value
+ if data is not None:
+ payload["data"] = data
+ if eventId is not None:
+ payload["eventId"] = eventId
+ await self.websocket.send_json(payload)
except (ConnectionClosedOK, ConnectionClosedError):
print(f"WebSocket closed by client, skipping message: {type}")
self.is_closed = True
@@ -360,11 +372,11 @@ class ModelSelectionStage:
return variant_models
except Exception:
await self.throw_error(
- "No OpenAI or Anthropic API key found. Please add the environment variable "
- "OPENAI_API_KEY or ANTHROPIC_API_KEY to backend/.env or in the settings dialog. "
+ "No OpenAI, Anthropic, or Gemini API key found. Please add the environment variable "
+ "OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY to backend/.env or in the settings dialog. "
"If you add it to .env, make sure to restart the backend server."
)
- raise Exception("No OpenAI or Anthropic key")
+ raise Exception("No API key")
def _get_variant_models(
self,
@@ -387,20 +399,42 @@ class ModelSelectionStage:
return [Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL, Llm.GEMINI_3_PRO_PREVIEW_HIGH]
# Define models based on available API keys
- if gemini_api_key and anthropic_api_key:
- # Temporary: Compare thinking models
+ if gemini_api_key and anthropic_api_key and openai_api_key:
+ models = [
+ Llm.GEMINI_3_FLASH_PREVIEW_HIGH,
+ Llm.GEMINI_3_PRO_PREVIEW_HIGH,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.GPT_5_2_2025_12_11,
+ ]
+ elif gemini_api_key and anthropic_api_key:
models = [
- Llm.GEMINI_3_FLASH_PREVIEW_HIGH, # Flash HIGH thinking
- Llm.GEMINI_3_PRO_PREVIEW_LOW, # Pro LOW thinking
- Llm.GEMINI_3_PRO_PREVIEW_HIGH, # Pro HIGH thinking
- Llm.CLAUDE_4_5_OPUS_2025_11_01, # Claude Opus 4.5 with thinking
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.GEMINI_3_FLASH_PREVIEW_HIGH,
+ Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL,
+ Llm.GEMINI_3_PRO_PREVIEW_HIGH,
+ Llm.GEMINI_3_PRO_PREVIEW_LOW,
+ ]
+ elif gemini_api_key and openai_api_key:
+ models = [
+ Llm.GPT_5_2_2025_12_11,
+ Llm.GEMINI_3_FLASH_PREVIEW_HIGH,
+ Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL,
+ Llm.GEMINI_3_PRO_PREVIEW_HIGH,
+ Llm.GEMINI_3_PRO_PREVIEW_LOW,
]
elif openai_api_key and anthropic_api_key:
- models = [Llm.CLAUDE_4_5_SONNET_2025_09_29, Llm.GPT_4_1_2025_04_14]
+ models = [Llm.CLAUDE_4_5_OPUS_2025_11_01, Llm.GPT_5_2_2025_12_11]
+ elif gemini_api_key:
+ models = [
+ Llm.GEMINI_3_FLASH_PREVIEW_HIGH,
+ Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL,
+ Llm.GEMINI_3_PRO_PREVIEW_HIGH,
+ Llm.GEMINI_3_PRO_PREVIEW_LOW,
+ ]
elif anthropic_api_key:
- models = [Llm.CLAUDE_4_5_SONNET_2025_09_29, Llm.CLAUDE_4_5_OPUS_2025_11_01]
+ models = [Llm.CLAUDE_4_5_OPUS_2025_11_01]
elif openai_api_key:
- models = [Llm.GPT_4_1_2025_04_14]
+ models = [Llm.GPT_5_2_2025_12_11]
else:
raise Exception("No OpenAI or Anthropic key")
@@ -448,7 +482,10 @@ class MockResponseStage:
def __init__(
self,
- send_message: Callable[[MessageType, str, int], Coroutine[Any, Any, None]],
+ send_message: Callable[
+ [MessageType, str | None, int, Dict[str, Any] | None, str | None],
+ Coroutine[Any, Any, None],
+ ],
):
self.send_message = send_message
@@ -503,7 +540,10 @@ class ParallelGenerationStage:
def __init__(
self,
- send_message: Callable[[MessageType, str, int], Coroutine[Any, Any, None]],
+ send_message: Callable[
+ [MessageType, str | None, int, Dict[str, Any] | None, str | None],
+ Coroutine[Any, Any, None],
+ ],
openai_api_key: str | None,
openai_base_url: str | None,
anthropic_api_key: str | None,
@@ -780,6 +820,126 @@ class ParallelGenerationStage:
await self.send_message("variantError", str(e), index)
+class AgenticGenerationStage:
+ """Handles agentic tool-calling generation for each variant."""
+
+ def __init__(
+ self,
+ send_message: Callable[[MessageType, str | None, int, Dict[str, Any] | None, str | None], Coroutine[Any, Any, None]],
+ openai_api_key: str | None,
+ openai_base_url: str | None,
+ anthropic_api_key: str | None,
+ gemini_api_key: str | None,
+ should_generate_images: bool,
+ image_cache: Dict[str, str],
+ ):
+ self.send_message = send_message
+ self.openai_api_key = openai_api_key
+ self.openai_base_url = openai_base_url
+ self.anthropic_api_key = anthropic_api_key
+ self.gemini_api_key = gemini_api_key
+ self.should_generate_images = should_generate_images
+ self.image_cache = image_cache
+
+ async def process_variants(
+ self,
+ variant_models: List[Llm],
+ prompt_messages: List[ChatCompletionMessageParam],
+ ) -> Dict[int, str]:
+ tasks: List[asyncio.Task[str]] = []
+ for index, model in enumerate(variant_models):
+ tasks.append(
+ asyncio.create_task(
+ self._run_variant(index, model, prompt_messages)
+ )
+ )
+
+ results = await asyncio.gather(*tasks, return_exceptions=True)
+ variant_completions: Dict[int, str] = {}
+ for index, result in enumerate(results):
+ if isinstance(result, BaseException):
+ print(f"Variant {index + 1} failed: {result}")
+ continue
+ if result:
+ variant_completions[index] = result
+
+ return variant_completions
+
+ async def _run_variant(
+ self,
+ index: int,
+ model: Llm,
+ prompt_messages: List[ChatCompletionMessageParam],
+ ) -> str:
+ try:
+ runner = AgenticRunner(
+ send_message=self.send_message,
+ variant_index=index,
+ openai_api_key=self.openai_api_key,
+ openai_base_url=self.openai_base_url,
+ anthropic_api_key=self.anthropic_api_key,
+ gemini_api_key=self.gemini_api_key,
+ should_generate_images=self.should_generate_images,
+ image_cache=self.image_cache,
+ )
+ completion = await runner.run(model, prompt_messages)
+ if completion:
+ await self.send_message("setCode", completion, index, None, None)
+ await self.send_message(
+ "variantComplete",
+ "Variant generation complete",
+ index,
+ None,
+ None,
+ )
+ return completion
+ except openai.AuthenticationError as e:
+ print(f"[VARIANT {index + 1}] OpenAI Authentication failed", e)
+ error_message = (
+ "Incorrect OpenAI key. Please make sure your OpenAI API key is correct, "
+ "or create a new OpenAI API key on your OpenAI dashboard."
+ + (
+ " Alternatively, you can purchase code generation credits directly on this website."
+ if IS_PROD
+ else ""
+ )
+ )
+ await self.send_message("variantError", error_message, index, None, None)
+ return ""
+ except openai.NotFoundError as e:
+ print(f"[VARIANT {index + 1}] OpenAI Model not found", e)
+ error_message = (
+ e.message
+ + ". Please make sure you have followed the instructions correctly to obtain "
+ "an OpenAI key with GPT vision access: "
+ "https://github.com/abi/screenshot-to-code/blob/main/Troubleshooting.md"
+ + (
+ " Alternatively, you can purchase code generation credits directly on this website."
+ if IS_PROD
+ else ""
+ )
+ )
+ await self.send_message("variantError", error_message, index, None, None)
+ return ""
+ except openai.RateLimitError as e:
+ print(f"[VARIANT {index + 1}] OpenAI Rate limit exceeded", e)
+ error_message = (
+ "OpenAI error - 'You exceeded your current quota, please check your plan and billing details.'"
+ + (
+ " Alternatively, you can purchase code generation credits directly on this website."
+ if IS_PROD
+ else ""
+ )
+ )
+ await self.send_message("variantError", error_message, index, None, None)
+ return ""
+ except Exception as e:
+ print(f"Error in variant {index + 1}: {e}")
+ traceback.print_exception(type(e), e, e.__traceback__)
+ await self.send_message("variantError", str(e), index, None, None)
+ return ""
+
+
# Pipeline Middleware Implementations
@@ -860,6 +1020,11 @@ class PromptCreationMiddleware(Middleware):
context.extracted_params,
)
)
+ if context.prompt_messages and context.prompt_messages[0].get("content"):
+ context.prompt_messages[0]["content"] = apply_tool_instructions(
+ str(context.prompt_messages[0]["content"]),
+ context.extracted_params.should_generate_images,
+ )
await next_func()
@@ -891,27 +1056,49 @@ class CodeGenerationMiddleware(Middleware):
gemini_api_key=GEMINI_API_KEY,
)
- # Generate code for all variants (handles video mode internally)
- generation_stage = ParallelGenerationStage(
- send_message=context.send_message,
- openai_api_key=context.extracted_params.openai_api_key,
- openai_base_url=context.extracted_params.openai_base_url,
- anthropic_api_key=context.extracted_params.anthropic_api_key,
- gemini_api_key=GEMINI_API_KEY,
- should_generate_images=context.extracted_params.should_generate_images,
- input_mode=context.extracted_params.input_mode,
- generation_type=context.extracted_params.generation_type,
- prompt=context.extracted_params.prompt,
+ is_video_create = (
+ context.extracted_params.input_mode == "video"
+ and context.extracted_params.generation_type == "create"
)
- context.variant_completions = (
- await generation_stage.process_variants(
- variant_models=context.variant_models,
- prompt_messages=context.prompt_messages,
+ if is_video_create:
+ generation_stage = ParallelGenerationStage(
+ send_message=context.send_message,
+ openai_api_key=context.extracted_params.openai_api_key,
+ openai_base_url=context.extracted_params.openai_base_url,
+ anthropic_api_key=context.extracted_params.anthropic_api_key,
+ gemini_api_key=GEMINI_API_KEY,
+ should_generate_images=context.extracted_params.should_generate_images,
+ input_mode=context.extracted_params.input_mode,
+ generation_type=context.extracted_params.generation_type,
+ prompt=context.extracted_params.prompt,
+ )
+
+ context.variant_completions = (
+ await generation_stage.process_variants(
+ variant_models=context.variant_models,
+ prompt_messages=context.prompt_messages,
+ image_cache=context.image_cache,
+ params=context.params,
+ )
+ )
+ else:
+ generation_stage = AgenticGenerationStage(
+ send_message=context.send_message,
+ openai_api_key=context.extracted_params.openai_api_key,
+ openai_base_url=context.extracted_params.openai_base_url,
+ anthropic_api_key=context.extracted_params.anthropic_api_key,
+ gemini_api_key=GEMINI_API_KEY,
+ should_generate_images=context.extracted_params.should_generate_images,
image_cache=context.image_cache,
- params=context.params,
)
- )
+
+ context.variant_completions = (
+ await generation_stage.process_variants(
+ variant_models=context.variant_models,
+ prompt_messages=context.prompt_messages,
+ )
+ )
# Check if all variants failed
if len(context.variant_completions) == 0:
diff --git a/backend/tests/test_model_selection.py b/backend/tests/test_model_selection.py
index a6b33900..ef144150 100644
--- a/backend/tests/test_model_selection.py
+++ b/backend/tests/test_model_selection.py
@@ -5,7 +5,7 @@ from llm import Llm
class TestModelSelectionAllKeys:
- """Test model selection when Gemini and Anthropic API keys are present."""
+ """Test model selection when Gemini, Anthropic, and OpenAI API keys are present."""
def setup_method(self):
"""Set up test fixtures."""
@@ -14,7 +14,7 @@ class TestModelSelectionAllKeys:
@pytest.mark.asyncio
async def test_gemini_anthropic_create(self):
- """Gemini + Anthropic: Gemini 3 Flash, Claude 4.5 Sonnet, Claude 4.5 Opus, Gemini 3 Pro"""
+ """All keys: Claude 4.5 Opus, GPT 5.2, Gemini 3 Flash (high), Gemini 3 Pro (high)"""
models = await self.model_selector.select_models(
generation_type="create",
input_mode="text",
@@ -25,15 +25,15 @@ class TestModelSelectionAllKeys:
expected = [
Llm.GEMINI_3_FLASH_PREVIEW_HIGH,
- Llm.CLAUDE_4_5_SONNET_2025_09_29,
- Llm.CLAUDE_4_5_OPUS_2025_11_01,
Llm.GEMINI_3_PRO_PREVIEW_HIGH,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.GPT_5_2_2025_12_11,
]
assert models == expected
@pytest.mark.asyncio
async def test_gemini_anthropic_update(self):
- """Gemini + Anthropic update: Same models regardless of generation_type"""
+ """All keys update: Same models regardless of generation_type"""
models = await self.model_selector.select_models(
generation_type="update",
input_mode="image",
@@ -44,9 +44,9 @@ class TestModelSelectionAllKeys:
expected = [
Llm.GEMINI_3_FLASH_PREVIEW_HIGH,
- Llm.CLAUDE_4_5_SONNET_2025_09_29,
- Llm.CLAUDE_4_5_OPUS_2025_11_01,
Llm.GEMINI_3_PRO_PREVIEW_HIGH,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.GPT_5_2_2025_12_11,
]
assert models == expected
@@ -61,7 +61,7 @@ class TestModelSelectionOpenAIAnthropic:
@pytest.mark.asyncio
async def test_openai_anthropic(self):
- """OpenAI + Anthropic: Claude 4.5 Sonnet, GPT-4.1, cycling"""
+ """OpenAI + Anthropic: Claude 4.5 Opus, GPT 5.2, cycling"""
models = await self.model_selector.select_models(
generation_type="create",
input_mode="text",
@@ -71,10 +71,10 @@ class TestModelSelectionOpenAIAnthropic:
)
expected = [
- Llm.CLAUDE_4_5_SONNET_2025_09_29,
- Llm.GPT_4_1_2025_04_14,
- Llm.CLAUDE_4_5_SONNET_2025_09_29,
- Llm.GPT_4_1_2025_04_14,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.GPT_5_2_2025_12_11,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.GPT_5_2_2025_12_11,
]
assert models == expected
@@ -89,7 +89,7 @@ class TestModelSelectionAnthropicOnly:
@pytest.mark.asyncio
async def test_anthropic_only(self):
- """Anthropic only: Claude 4.5 Sonnet, Claude 4.5 Opus, cycling"""
+ """Anthropic only: Claude 4.5 Opus only"""
models = await self.model_selector.select_models(
generation_type="create",
input_mode="text",
@@ -99,9 +99,9 @@ class TestModelSelectionAnthropicOnly:
)
expected = [
- Llm.CLAUDE_4_5_SONNET_2025_09_29,
Llm.CLAUDE_4_5_OPUS_2025_11_01,
- Llm.CLAUDE_4_5_SONNET_2025_09_29,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
+ Llm.CLAUDE_4_5_OPUS_2025_11_01,
Llm.CLAUDE_4_5_OPUS_2025_11_01,
]
assert models == expected
@@ -117,7 +117,7 @@ class TestModelSelectionOpenAIOnly:
@pytest.mark.asyncio
async def test_openai_only(self):
- """OpenAI only: GPT-4.1 only"""
+ """OpenAI only: GPT 5.2 only"""
models = await self.model_selector.select_models(
generation_type="create",
input_mode="text",
@@ -127,9 +127,10 @@ class TestModelSelectionOpenAIOnly:
)
expected = [
- Llm.GPT_4_1_2025_04_14,
- Llm.GPT_4_1_2025_04_14,
- Llm.GPT_4_1_2025_04_14,
+ Llm.GPT_5_2_2025_12_11,
+ Llm.GPT_5_2_2025_12_11,
+ Llm.GPT_5_2_2025_12_11,
+ Llm.GPT_5_2_2025_12_11,
]
assert models == expected
@@ -145,7 +146,7 @@ class TestModelSelectionNoKeys:
@pytest.mark.asyncio
async def test_no_keys_raises_error(self):
"""No keys: Should raise an exception"""
- with pytest.raises(Exception, match="No OpenAI or Anthropic key"):
+ with pytest.raises(Exception, match="No API key"):
await self.model_selector.select_models(
generation_type="create",
input_mode="text",
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 5602c4da..48cb2831 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -41,12 +41,14 @@ function App() {
removeCommit,
setHead,
appendCommitCode,
- appendVariantThinking,
setCommitCode,
resetCommits,
resetHead,
updateVariantStatus,
resizeVariants,
+ startAgentEvent,
+ appendAgentEventContent,
+ finishAgentEvent,
// Outputs
appendExecutionConsole,
@@ -72,7 +74,7 @@ function App() {
isImageGenerationEnabled: true,
editorTheme: EditorTheme.COBALT,
generatedCodeConfig: Stack.HTML_TAILWIND,
- codeGenerationModel: CodeGenerationModel.CLAUDE_4_5_SONNET_2025_09_29,
+ codeGenerationModel: CodeGenerationModel.CLAUDE_4_5_OPUS_2025_11_01,
// Only relevant for hosted version
isTermOfServiceAccepted: false,
},
@@ -80,6 +82,8 @@ function App() {
);
const wsRef = useRef<WebSocket>(null);
+ const lastThinkingEventIdRef = useRef<Record<number, string>>({});
+ const lastAssistantEventIdRef = useRef<Record<number, string>>({});
const showSelectAndEditFeature =
settings.generatedCodeConfig === Stack.HTML_TAILWIND ||
@@ -207,6 +211,9 @@ function App() {
addCommit(commit);
setHead(commit.hash);
+ lastThinkingEventIdRef.current = {};
+ lastAssistantEventIdRef.current = {};
+
generateCode(wsRef, updatedParams, {
onChange: (token, variantIndex) => {
appendCommitCode(commit.hash, variantIndex, token);
@@ -223,18 +230,100 @@ function App() {
onVariantError: (variantIndex, error) => {
console.error(`Error in variant ${variantIndex}:`, error);
updateVariantStatus(commit.hash, variantIndex, "error", error);
+ const lastThinking = lastThinkingEventIdRef.current[variantIndex];
+ if (lastThinking) {
+ finishAgentEvent(commit.hash, variantIndex, lastThinking, {
+ status: "error",
+ endedAt: Date.now(),
+ });
+ }
+ const lastAssistant = lastAssistantEventIdRef.current[variantIndex];
+ if (lastAssistant) {
+ finishAgentEvent(commit.hash, variantIndex, lastAssistant, {
+ status: "error",
+ endedAt: Date.now(),
+ });
+ }
},
onVariantCount: (count) => {
console.log(`Backend is using ${count} variants`);
resizeVariants(commit.hash, count);
},
- onThinking: (content, variantIndex) => {
- appendVariantThinking(commit.hash, variantIndex, content);
+ onThinking: (content, variantIndex, eventId) => {
+ if (!eventId) return;
+ lastThinkingEventIdRef.current[variantIndex] = eventId;
+ startAgentEvent(commit.hash, variantIndex, {
+ id: eventId,
+ type: "thinking",
+ status: "running",
+ startedAt: Date.now(),
+ });
+ appendAgentEventContent(commit.hash, variantIndex, eventId, content);
+ },
+ onAssistant: (content, variantIndex, eventId) => {
+ if (!eventId) return;
+ lastAssistantEventIdRef.current[variantIndex] = eventId;
+ startAgentEvent(commit.hash, variantIndex, {
+ id: eventId,
+ type: "assistant",
+ status: "running",
+ startedAt: Date.now(),
+ });
+ appendAgentEventContent(commit.hash, variantIndex, eventId, content);
+ },
+ onToolStart: (data, variantIndex, eventId) => {
+ if (!eventId) return;
+ const lastThinking = lastThinkingEventIdRef.current[variantIndex];
+ if (lastThinking && lastThinking !== eventId) {
+ finishAgentEvent(commit.hash, variantIndex, lastThinking, {
+ status: "complete",
+ endedAt: Date.now(),
+ });
+ }
+ const lastAssistant = lastAssistantEventIdRef.current[variantIndex];
+ if (lastAssistant && lastAssistant !== eventId) {
+ finishAgentEvent(commit.hash, variantIndex, lastAssistant, {
+ status: "complete",
+ endedAt: Date.now(),
+ });
+ }
+ startAgentEvent(commit.hash, variantIndex, {
+ id: eventId,
+ type: "tool",
+ status: "running",
+ toolName: data?.name,
+ input: data?.input,
+ startedAt: Date.now(),
+ });
+ },
+ onToolResult: (data, variantIndex, eventId) => {
+ if (!eventId) return;
+ finishAgentEvent(commit.hash, variantIndex, eventId, {
+ status: data?.ok === false ? "error" : "complete",
+ output: data?.output,
+ endedAt: Date.now(),
+ });
},
onCancel: () => {
cancelCodeGenerationAndReset(commit);
},
onComplete: () => {
+ const lastThinking = lastThinkingEventIdRef.current;
+ const lastAssistant = lastAssistantEventIdRef.current;
+ Object.keys(lastThinking).forEach((key) => {
+ const idx = Number(key);
+ finishAgentEvent(commit.hash, idx, lastThinking[idx], {
+ status: "complete",
+ endedAt: Date.now(),
+ });
+ });
+ Object.keys(lastAssistant).forEach((key) => {
+ const idx = Number(key);
+ finishAgentEvent(commit.hash, idx, lastAssistant[idx], {
+ status: "complete",
+ endedAt: Date.now(),
+ });
+ });
setAppState(AppState.CODE_READY);
},
});
diff --git a/frontend/src/components/agent/AgentActivity.tsx b/frontend/src/components/agent/AgentActivity.tsx
new file mode 100644
index 00000000..39b28606
--- /dev/null
+++ b/frontend/src/components/agent/AgentActivity.tsx
@@ -0,0 +1,288 @@
+import { useEffect, useMemo, useState } from "react";
+import { useProjectStore } from "../../store/project-store";
+import {
+ AgentEvent,
+ AgentEventStatus,
+ AgentEventType,
+} from "../commits/types";
+import {
+ BsChatDots,
+ BsChevronDown,
+ BsChevronRight,
+ BsCheckCircle,
+ BsXCircle,
+ BsLightbulb,
+ BsFileEarmarkPlus,
+ BsPencilSquare,
+ BsImage,
+} from "react-icons/bs";
+import ReactMarkdown from "react-markdown";
+
+function getPreview(text: string): string {
+ const cleaned = text.replace(/\s+/g, " ").trim();
+ if (!cleaned) return "";
+ if (cleaned.length <= 140) return cleaned;
+ return `${cleaned.slice(0, 140)}...`;
+}
+
+function formatDuration(startedAt?: number, endedAt?: number): string {
+ if (!startedAt || !endedAt) return "";
+ const seconds = Math.max(1, Math.round((endedAt - startedAt) / 1000));
+ return `${seconds}s`;
+}
+
+function formatTotalDuration(events: AgentEvent[]): string {
+ if (events.length === 0) return "";
+ const start = Math.min(...events.map((event) => event.startedAt));
+ const now = Date.now();
+ const end = Math.max(
+ ...events.map((event) => event.endedAt ?? now)
+ );
+ const seconds = Math.max(1, Math.round((end - start) / 1000));
+ return `${seconds}s`;
+}
+
+function getStatusTone(status: AgentEventStatus) {
+ if (status === "running") {
+ return "border-emerald-300 bg-emerald-50/80 dark:border-emerald-600 dark:bg-emerald-900/20";
+ }
+ if (status === "error") {
+ return "border-red-300 bg-red-50/80 dark:border-red-600 dark:bg-red-900/20";
+ }
+ return "border-gray-200 bg-white dark:border-gray-700 dark:bg-gray-900/40";
+}
+
+function renderStatusIcon(status: AgentEventStatus) {
+ if (status === "running") {
+ return (
+ <span className="flex items-center gap-2">
+ <span className="w-2 h-2 rounded-full bg-emerald-500 animate-pulse" />
+ <span className="text-xs text-emerald-600 dark:text-emerald-400">
+ running
+ </span>
+ </span>
+ );
+ }
+ if (status === "error") {
+ return <BsXCircle className="text-red-500" />;
+ }
+ return <BsCheckCircle className="text-emerald-600" />;
+}
+
+function getEventIcon(type: AgentEventType, toolName?: string) {
+ if (type === "thinking") {
+ return <BsLightbulb className="text-yellow-500" />;
+ }
+ if (type === "assistant") {
+ return <BsChatDots className="text-blue-500" />;
+ }
+ if (toolName === "create_file") {
+ return <BsFileEarmarkPlus className="text-indigo-500" />;
+ }
+ if (toolName === "edit_file") {
+ return <BsPencilSquare className="text-purple-500" />;
+ }
+ if (toolName === "generate_images") {
+ return <BsImage className="text-pink-500" />;
+ }
+ return <BsFileEarmarkPlus className="text-gray-500" />;
+}
+
+function getEventTitle(event: AgentEvent): string {
+ if (event.type === "thinking") {
+ if (event.status === "running") return "Thinking";
+ const duration = formatDuration(event.startedAt, event.endedAt);
+ return duration ? `Thought for ${duration}` : "Thought";
+ }
+ if (event.type === "assistant") {
+ return "Assistant response";
+ }
+ if (event.type === "tool") {
+ if (event.toolName === "create_file") {
+ return event.status === "running" ? "Creating file" : "Created file";
+ }
+ if (event.toolName === "edit_file") {
+ return event.status === "running" ? "Editing file" : "Edited file";
+ }
+ if (event.toolName === "generate_images") {
+ return event.status === "running"
+ ? "Generating images"
+ : "Generated images";
+ }
+ return event.status === "running" ? "Running tool" : "Tool completed";
+ }
+ return "Activity";
+}
+
+function renderToolDetails(event: AgentEvent) {
+ if (!event.input && !event.output) return null;
+
+ const renderJson = (data: unknown) => {
+ if (!data) return null;
+ let json = "";
+ try {
+ json = JSON.stringify(data, null, 2);
+ } catch {
+ json = String(data);
+ }
+ if (json.length > 900) {
+ json = json.slice(0, 900) + "...";
+ }
+ return (
+ <pre className="mt-2 rounded-md bg-gray-50 dark:bg-gray-800 p-2 text-xs text-gray-700 dark:text-gray-200 overflow-x-auto">
+ {json}
+ </pre>
+ );
+ };
+
+ const images =
+ event.output && Array.isArray((event.output as any).images)
+ ? ((event.output as any).images as Array<any>)
+ : null;
+
+ return (
+ <div className="text-sm text-gray-700 dark:text-gray-200">
+ {event.input && (
+ <div>
+ <div className="text-xs uppercase tracking-wide text-gray-400">
+ Input
+ </div>
+ {renderJson(event.input)}
+ </div>
+ )}
+ {event.output && (
+ <div className="mt-3">
+ <div className="text-xs uppercase tracking-wide text-gray-400">
+ Output
+ </div>
+ {images ? (
+ <div className="mt-2 space-y-2">
+ {images.map((item, index) => (
+ <div
+ key={`${item.prompt}-${index}`}
+ className="flex items-start justify-between gap-3 rounded-md border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900/60 p-2"
+ >
+ <div>
+ <div className="text-xs text-gray-500">Prompt</div>
+ <div className="text-sm text-gray-700 dark:text-gray-100">
+ {item.prompt}
+ </div>
+ </div>
+ <div className="text-xs text-gray-500">
+ {item.status === "ok" ? "✓" : "⚠"}
+ </div>
+ </div>
+ ))}
+ </div>
+ ) : (
+ renderJson(event.output)
+ )}
+ </div>
+ )}
+ </div>
+ );
+}
+
+function AgentEventCard({
+ event,
+ autoExpand,
+}: {
+ event: AgentEvent;
+ autoExpand?: boolean;
+}) {
+ const [expanded, setExpanded] = useState(Boolean(autoExpand));
+
+ useEffect(() => {
+ if (autoExpand) {
+ setExpanded(true);
+ }
+ }, [autoExpand]);
+
+ const isExpanded =
+ (event.type !== "thinking" && event.status === "running") || expanded;
+ const preview = useMemo(() => getPreview(event.content || ""), [event.content]);
+
+ return (
+ <div
+ className={`rounded-xl border ${getStatusTone(event.status)} transition-shadow`}
+ >
+ <button
+ onClick={() => setExpanded((prev) => !prev)}
+ className="w-full flex items-center justify-between px-3 py-2 text-left"
+ >
+ <div className="flex items-center gap-2">
+ {getEventIcon(event.type, event.toolName)}
+ <span className="text-sm font-medium text-gray-800 dark:text-gray-100">
+ {getEventTitle(event)}
+ </span>
+ </div>
+ <div className="flex items-center gap-2">
+ {renderStatusIcon(event.status)}
+ {isExpanded ? (
+ <BsChevronDown className="text-gray-400" />
+ ) : (
+ <BsChevronRight className="text-gray-400" />
+ )}
+ </div>
+ </button>
+ {isExpanded ? (
+ <div className="px-3 pb-3">
+ {event.type !== "tool" && event.content && (
+ <div className="prose prose-sm dark:prose-invert max-w-none">
+ <ReactMarkdown>{event.content}</ReactMarkdown>
+ </div>
+ )}
+ {event.type === "tool" && renderToolDetails(event)}
+ </div>
+ ) : (
+ preview &&
+ event.type !== "thinking" && (
+ <div className="px-3 pb-2 text-xs text-gray-500 dark:text-gray-400">
+ {preview}
+ </div>
+ )
+ )}
+ </div>
+ );
+}
+
+function AgentActivity() {
+ const { head, commits, latestCommitHash } = useProjectStore();
+
+ const currentCommit = head ? commits[head] : null;
+ const selectedVariant = currentCommit
+ ? currentCommit.variants[currentCommit.selectedVariantIndex]
+ : null;
+
+ const events = selectedVariant?.agentEvents || [];
+ const lastAssistantId = [...events]
+ .reverse()
+ .find((event) => event.type === "assistant")?.id;
+
+ const isLatestCommit = head === latestCommitHash;
+ if (!isLatestCommit || events.length === 0) {
+ return null;
+ }
+
+ return (
+ <div className="space-y-2 mb-3">
+ <div className="flex items-center justify-between rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900/60 px-3 py-2">
+ <div className="text-xs uppercase tracking-wide text-gray-400">
+ Agent activity
+ </div>
+ <div className="text-xs font-semibold text-gray-700 dark:text-gray-200">
+ Total time {formatTotalDuration(events)}
+ </div>
+ </div>
+ {events.map((event) => (
+ <AgentEventCard
+ key={event.id}
+ event={event}
+ autoExpand={event.type === "assistant" && event.id === lastAssistantId}
+ />
+ ))}
+ </div>
+ );
+}
+
+export default AgentActivity;
diff --git a/frontend/src/components/commits/types.ts b/frontend/src/components/commits/types.ts
index ef56a8ee..f4c85887 100644
--- a/frontend/src/components/commits/types.ts
+++ b/frontend/src/components/commits/types.ts
@@ -2,6 +2,21 @@ export type CommitHash = string;
export type VariantStatus = "generating" | "complete" | "cancelled" | "error";
+export type AgentEventStatus = "running" | "complete" | "error";
+export type AgentEventType = "thinking" | "assistant" | "tool";
+
+export type AgentEvent = {
+ id: string;
+ type: AgentEventType;
+ status: AgentEventStatus;
+ content?: string;
+ toolName?: string;
+ input?: any;
+ output?: any;
+ startedAt: number;
+ endedAt?: number;
+};
+
export type Variant = {
code: string;
status?: VariantStatus;
@@ -9,6 +24,7 @@ export type Variant = {
thinking?: string;
thinkingStartTime?: number;
thinkingDuration?: number;
+ agentEvents?: AgentEvent[];
};
export type BaseCommit = {
diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx
index 74366186..026fe846 100644
--- a/frontend/src/components/sidebar/Sidebar.tsx
+++ b/frontend/src/components/sidebar/Sidebar.tsx
@@ -12,7 +12,7 @@ import { useEffect, useRef, useState, useCallback } from "react";
import HistoryDisplay from "../history/HistoryDisplay";
import Variants from "../variants/Variants";
import UpdateImageUpload, { UpdateImagePreview } from "../UpdateImageUpload";
-import ThinkingIndicator from "../thinking/ThinkingIndicator";
+import AgentActivity from "../agent/AgentActivity";
interface SidebarProps {
showSelectAndEditFeature: boolean;
@@ -121,7 +121,7 @@ function Sidebar({
<>
<Variants />
- <ThinkingIndicator />
+ <AgentActivity />
{/* Show code preview when coding and the selected variant is not complete */}
{appState === AppState.CODING && !isSelectedVariantComplete && (
diff --git a/frontend/src/generateCode.ts b/frontend/src/generateCode.ts
index 410b0216..418c8e81 100644
--- a/frontend/src/generateCode.ts
+++ b/frontend/src/generateCode.ts
@@ -20,8 +20,13 @@ type WebSocketResponse = {
| "variantComplete"
| "variantError"
| "variantCount"
- | "thinking";
- value: string;
+ | "thinking"
+ | "assistant"
+ | "toolStart"
+ | "toolResult";
+ value?: string;
+ data?: any;
+ eventId?: string;
variantIndex: number;
};
@@ -32,7 +37,10 @@ interface CodeGenerationCallbacks {
onVariantComplete: (variantIndex: number) => void;
onVariantError: (variantIndex: number, error: string) => void;
onVariantCount: (count: number) => void;
- onThinking: (content: string, variantIndex: number) => void;
+ onThinking: (content: string, variantIndex: number, eventId?: string) => void;
+ onAssistant: (content: string, variantIndex: number, eventId?: string) => void;
+ onToolStart: (data: any, variantIndex: number, eventId?: string) => void;
+ onToolResult: (data: any, variantIndex: number, eventId?: string) => void;
onCancel: () => void;
onComplete: () => void;
}
@@ -55,22 +63,28 @@ export function generateCode(
ws.addEventListener("message", async (event: MessageEvent) => {
const response = JSON.parse(event.data) as WebSocketResponse;
if (response.type === "chunk") {
- callbacks.onChange(response.value, response.variantIndex);
+ callbacks.onChange(response.value || "", response.variantIndex);
} else if (response.type === "status") {
- callbacks.onStatusUpdate(response.value, response.variantIndex);
+ callbacks.onStatusUpdate(response.value || "", response.variantIndex);
} else if (response.type === "setCode") {
- callbacks.onSetCode(response.value, response.variantIndex);
+ callbacks.onSetCode(response.value || "", response.variantIndex);
} else if (response.type === "variantComplete") {
callbacks.onVariantComplete(response.variantIndex);
} else if (response.type === "variantError") {
- callbacks.onVariantError(response.variantIndex, response.value);
+ callbacks.onVariantError(response.variantIndex, response.value || "");
} else if (response.type === "variantCount") {
- callbacks.onVariantCount(parseInt(response.value));
+ callbacks.onVariantCount(parseInt(response.value || "1"));
} else if (response.type === "thinking") {
- callbacks.onThinking(response.value, response.variantIndex);
+ callbacks.onThinking(response.value || "", response.variantIndex, response.eventId);
+ } else if (response.type === "assistant") {
+ callbacks.onAssistant(response.value || "", response.variantIndex, response.eventId);
+ } else if (response.type === "toolStart") {
+ callbacks.onToolStart(response.data, response.variantIndex, response.eventId);
+ } else if (response.type === "toolResult") {
+ callbacks.onToolResult(response.data, response.variantIndex, response.eventId);
} else if (response.type === "error") {
console.error("Error generating code", response.value);
- toast.error(response.value);
+ toast.error(response.value || ERROR_MESSAGE);
}
});
diff --git a/frontend/src/lib/models.ts b/frontend/src/lib/models.ts
index 29a1293e..b496e8df 100644
--- a/frontend/src/lib/models.ts
+++ b/frontend/src/lib/models.ts
@@ -1,12 +1,36 @@
// Keep in sync with backend (llm.py)
// Order here matches dropdown order
export enum CodeGenerationModel {
+ GPT_5_2_2025_12_11 = "gpt-5.2-2025-12-11",
+ CLAUDE_4_5_OPUS_2025_11_01 = "claude-opus-4-5-20251101",
CLAUDE_4_5_SONNET_2025_09_29 = "claude-sonnet-4-5-20250929",
+ GEMINI_3_FLASH_PREVIEW_HIGH = "gemini-3-flash-preview (high thinking)",
+ GEMINI_3_FLASH_PREVIEW_MINIMAL = "gemini-3-flash-preview (minimal thinking)",
+ GEMINI_3_PRO_PREVIEW_HIGH = "gemini-3-pro-preview (high thinking)",
+ GEMINI_3_PRO_PREVIEW_LOW = "gemini-3-pro-preview (low thinking)",
}
// Will generate a static error if a model in the enum above is not in the descriptions
export const CODE_GENERATION_MODEL_DESCRIPTIONS: {
[key in CodeGenerationModel]: { name: string; inBeta: boolean };
} = {
- "claude-sonnet-4-5-20250929": { name: "Claude Sonnet 4.5", inBeta: false },
+ "gpt-5.2-2025-12-11": { name: "GPT 5.2", inBeta: false },
+ "claude-opus-4-5-20251101": { name: "Claude Opus 4.5", inBeta: false },
+ "claude-sonnet-4-5-20250929": { name: "Claude Sonnet 4.5", inBeta: true },
+ "gemini-3-flash-preview (high thinking)": {
+ name: "Gemini 3 Flash (high)",
+ inBeta: true,
+ },
+ "gemini-3-flash-preview (minimal thinking)": {
+ name: "Gemini 3 Flash (minimal)",
+ inBeta: true,
+ },
+ "gemini-3-pro-preview (high thinking)": {
+ name: "Gemini 3 Pro (high)",
+ inBeta: true,
+ },
+ "gemini-3-pro-preview (low thinking)": {
+ name: "Gemini 3 Pro (low)",
+ inBeta: true,
+ },
};
diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts
index b19899c1..0d3c5e58 100644
--- a/frontend/src/store/project-store.ts
+++ b/frontend/src/store/project-store.ts
@@ -1,5 +1,10 @@
import { create } from "zustand";
-import { Commit, CommitHash, VariantStatus } from "../components/commits/types";
+import {
+ AgentEvent,
+ Commit,
+ CommitHash,
+ VariantStatus,
+} from "../components/commits/types";
// Store for app-wide state
interface ProjectStore {
@@ -42,6 +47,24 @@ interface ProjectStore {
) => void;
resizeVariants: (hash: CommitHash, count: number) => void;
+ startAgentEvent: (
+ hash: CommitHash,
+ numVariant: number,
+ event: AgentEvent
+ ) => void;
+ appendAgentEventContent: (
+ hash: CommitHash,
+ numVariant: number,
+ eventId: string,
+ content: string
+ ) => void;
+ finishAgentEvent: (
+ hash: CommitHash,
+ numVariant: number,
+ eventId: string,
+ updates: Partial<AgentEvent>
+ ) => void;
+
setHead: (hash: CommitHash) => void;
resetHead: () => void;
@@ -74,6 +97,7 @@ export const useProjectStore = create<ProjectStore>((set) => ({
...variant,
status: variant.status || ("generating" as VariantStatus),
thinkingStartTime: Date.now(),
+ agentEvents: [],
})),
};
@@ -229,7 +253,7 @@ export const useProjectStore = create<ProjectStore>((set) => ({
// Resize variants array to match backend count
const currentVariants = commit.variants;
const newVariants = Array(count).fill(null).map((_, index) =>
- currentVariants[index] || { code: "", status: "generating" as VariantStatus }
+ currentVariants[index] || { code: "", status: "generating" as VariantStatus, agentEvents: [] }
);
return {
@@ -244,6 +268,79 @@ export const useProjectStore = create<ProjectStore>((set) => ({
};
}),
+ startAgentEvent: (hash, numVariant, event) =>
+ 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 events = variant.agentEvents || [];
+ const existingIndex = events.findIndex((e) => e.id === event.id);
+ if (existingIndex === -1) {
+ return { ...variant, agentEvents: [...events, event] };
+ }
+ const updatedEvents = events.map((e) =>
+ e.id === event.id
+ ? {
+ ...e,
+ ...event,
+ content: event.content ? event.content : e.content,
+ startedAt: e.startedAt || event.startedAt,
+ }
+ : e
+ );
+ return { ...variant, agentEvents: updatedEvents };
+ });
+ return {
+ commits: {
+ ...state.commits,
+ [hash]: { ...commit, variants },
+ },
+ };
+ }),
+
+ appendAgentEventContent: (hash, numVariant, eventId, content) =>
+ 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 events = variant.agentEvents || [];
+ const updatedEvents = events.map((event) =>
+ event.id === eventId
+ ? { ...event, content: (event.content || "") + content }
+ : event
+ );
+ return { ...variant, agentEvents: updatedEvents };
+ });
+ return {
+ commits: {
+ ...state.commits,
+ [hash]: { ...commit, variants },
+ },
+ };
+ }),
+
+ finishAgentEvent: (hash, numVariant, eventId, updates) =>
+ 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 events = variant.agentEvents || [];
+ const updatedEvents = events.map((event) =>
+ event.id === eventId ? { ...event, ...updates } : event
+ );
+ return { ...variant, agentEvents: updatedEvents };
+ });
+ return {
+ commits: {
+ ...state.commits,
+ [hash]: { ...commit, variants },
+ },
+ };
+ }),
+
setHead: (hash: CommitHash) => set({ head: hash }),
resetHead: () => set({ head: null }),
diff --git a/frontend/src/tests/qa.test.ts b/frontend/src/tests/qa.test.ts
index 1fbe3991..1633cb00 100644
--- a/frontend/src/tests/qa.test.ts
+++ b/frontend/src/tests/qa.test.ts
@@ -35,7 +35,7 @@ const stacks = process.env.QA_STACKS
? process.env.QA_STACKS.split(",").map((stack) => stack.trim() as Stack)
: defaultStacks;
-const defaultModels = [CodeGenerationModel.CLAUDE_4_5_SONNET_2025_09_29];
+const defaultModels = [CodeGenerationModel.CLAUDE_4_5_OPUS_2025_11_01];
const models = process.env.QA_MODELS
? process.env.QA_MODELS.split(",").map((model) => model.trim())
: defaultModels;