Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Reorganize provider streaming logic under agentic adapters
Abi Raja399e5c724eb6
diff --git a/backend/agentic/streaming/anthropic.py b/backend/agentic/streaming/anthropic.pyindex 600e389c..cc951aa4 100644--- a/backend/agentic/streaming/anthropic.py+++ b/backend/agentic/streaming/anthropic.py@@ -8,7 +8,9 @@ from openai.types.chat import ChatCompletionMessageParamfrom agentic.streaming.types import EventSink, ExecutedToolCall, StepResult, StreamEventfrom agentic.tools import ToolCallfrom llm import Llm-from models.claude import convert_openai_messages_to_claude+from agentic.streaming.providers.anthropic.transform import (+ convert_openai_messages_to_claude,+)THINKING_MODELS = {diff --git a/backend/agentic/streaming/gemini.py b/backend/agentic/streaming/gemini.pyindex 8aafdeca..ac4f5245 100644--- a/backend/agentic/streaming/gemini.py+++ b/backend/agentic/streaming/gemini.py@@ -9,7 +9,7 @@ from openai.types.chat import ChatCompletionMessageParamfrom agentic.streaming.types import EventSink, ExecutedToolCall, StepResult, StreamEventfrom agentic.tools import ToolCallfrom llm import Llm-from models.gemini import (+from agentic.streaming.providers.gemini.transform import (convert_message_to_gemini_content,get_gemini_api_model_name,get_thinking_level_for_model,diff --git a/backend/agentic/streaming/providers/__init__.py b/backend/agentic/streaming/providers/__init__.pynew file mode 100644index 00000000..ab121ed5--- /dev/null+++ b/backend/agentic/streaming/providers/__init__.py@@ -0,0 +1 @@+# Provider-specific streaming helpers.diff --git a/backend/agentic/streaming/providers/anthropic/__init__.py b/backend/agentic/streaming/providers/anthropic/__init__.pynew file mode 100644index 00000000..d3c93a69--- /dev/null+++ b/backend/agentic/streaming/providers/anthropic/__init__.py@@ -0,0 +1,5 @@+from agentic.streaming.providers.anthropic.transform import (+ convert_openai_messages_to_claude,+)++__all__ = ["convert_openai_messages_to_claude"]diff --git a/backend/agentic/streaming/providers/anthropic/transform.py b/backend/agentic/streaming/providers/anthropic/transform.pynew file mode 100644index 00000000..8f91c4db--- /dev/null+++ b/backend/agentic/streaming/providers/anthropic/transform.py@@ -0,0 +1,38 @@+import copy+from typing import Any, Dict, List, Tuple, cast++from openai.types.chat import ChatCompletionMessageParam++from image_processing.utils import process_image+++def convert_openai_messages_to_claude(+ messages: List[ChatCompletionMessageParam],+) -> Tuple[str, List[Dict[str, Any]]]:+ """+ Convert OpenAI-format messages to Claude-format messages.+ """+ cloned_messages = copy.deepcopy(messages)++ system_prompt = cast(str, cloned_messages[0].get("content"))+ claude_messages = [dict(message) for message in cloned_messages[1:]]++ for message in claude_messages:+ if not isinstance(message["content"], list):+ continue++ for content in message["content"]: # type: ignore+ if content["type"] != "image_url":+ continue++ content["type"] = "image"+ image_data_url = cast(str, content["image_url"]["url"])+ media_type, base64_data = process_image(image_data_url)+ del content["image_url"]+ content["source"] = {+ "type": "base64",+ "media_type": media_type,+ "data": base64_data,+ }++ return system_prompt, claude_messagesdiff --git a/backend/agentic/streaming/providers/gemini/__init__.py b/backend/agentic/streaming/providers/gemini/__init__.pynew file mode 100644index 00000000..484668e8--- /dev/null+++ b/backend/agentic/streaming/providers/gemini/__init__.py@@ -0,0 +1,11 @@+from agentic.streaming.providers.gemini.transform import (+ convert_message_to_gemini_content,+ get_gemini_api_model_name,+ get_thinking_level_for_model,+)++__all__ = [+ "convert_message_to_gemini_content",+ "get_gemini_api_model_name",+ "get_thinking_level_for_model",+]diff --git a/backend/agentic/streaming/providers/gemini/transform.py b/backend/agentic/streaming/providers/gemini/transform.pynew file mode 100644index 00000000..9385e10d--- /dev/null+++ b/backend/agentic/streaming/providers/gemini/transform.py@@ -0,0 +1,136 @@+import base64+from typing import Any, Dict, List++from google.genai import types+from openai.types.chat import ChatCompletionMessageParam++from llm import Llm+++DEFAULT_VIDEO_FPS = 10+++def get_gemini_api_model_name(model: Llm) -> str:+ if model in [Llm.GEMINI_3_FLASH_PREVIEW_HIGH, Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL]:+ return "gemini-3-flash-preview"+ if model in [Llm.GEMINI_3_PRO_PREVIEW_HIGH, Llm.GEMINI_3_PRO_PREVIEW_LOW]:+ return "gemini-3-pro-preview"+ return model.value+++def get_thinking_level_for_model(model: Llm) -> str:+ if model in [Llm.GEMINI_3_FLASH_PREVIEW_HIGH, Llm.GEMINI_3_PRO_PREVIEW_HIGH]:+ return "high"+ if model == Llm.GEMINI_3_PRO_PREVIEW_LOW:+ return "low"+ if model == Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL:+ return "minimal"+ return "high"+++def _extract_text_from_content(content: str | List[Dict[str, Any]]) -> str:+ if isinstance(content, str):+ return content++ for content_part in content:+ if content_part.get("type") == "text":+ return content_part.get("text", "")++ return ""+++def _detect_mime_type_from_base64(base64_data: str) -> str | None:+ try:+ decoded = base64.b64decode(base64_data[:32])++ if decoded[:8] == b"\x89PNG\r\n\x1a\n":+ return "image/png"+ if decoded[:2] == b"\xff\xd8":+ return "image/jpeg"+ if decoded[:6] in (b"GIF87a", b"GIF89a"):+ return "image/gif"+ if decoded[:4] == b"RIFF" and decoded[8:12] == b"WEBP":+ return "image/webp"++ if decoded[4:8] == b"ftyp":+ return "video/mp4"+ if decoded[:4] == b"\x1aE\xdf\xa3":+ return "video/webm"+ except Exception:+ pass++ return None+++def _extract_images_from_content(content: str | List[Dict[str, Any]]) -> List[Dict[str, str]]:+ if isinstance(content, str):+ return []++ images: List[Dict[str, str]] = []+ for content_part in content:+ if content_part.get("type") != "image_url":+ continue++ image_url = content_part["image_url"]["url"]+ if image_url.startswith("data:"):+ mime_type = image_url.split(";")[0].split(":")[1]+ base64_data = image_url.split(",")[1]++ if mime_type == "application/octet-stream":+ detected_mime = _detect_mime_type_from_base64(base64_data)+ if detected_mime:+ mime_type = detected_mime+ else:+ print("Warning: Could not detect MIME type for data URL, skipping")+ continue++ images.append({"mime_type": mime_type, "data": base64_data})+ continue++ images.append({"uri": image_url})++ return images+++def convert_message_to_gemini_content(+ message: ChatCompletionMessageParam,+) -> types.Content:+ role = message.get("role", "user")+ content = message.get("content", "")+ gemini_role = "model" if role == "assistant" else "user"++ parts: List[types.Part | Dict[str, str]] = []++ text = _extract_text_from_content(content) # type: ignore+ image_data_list = _extract_images_from_content(content) # type: ignore++ if text:+ parts.append({"text": text})++ for image_data in image_data_list:+ if "data" in image_data:+ mime_type = image_data["mime_type"]+ media_bytes = base64.b64decode(image_data["data"])+ if mime_type.startswith("video/"):+ parts.append(+ types.Part(+ inline_data=types.Blob(data=media_bytes, mime_type=mime_type),+ video_metadata=types.VideoMetadata(fps=DEFAULT_VIDEO_FPS),+ media_resolution=types.PartMediaResolutionLevel.MEDIA_RESOLUTION_HIGH,+ )+ )+ continue++ parts.append(+ types.Part.from_bytes(+ data=media_bytes,+ mime_type=mime_type,+ media_resolution=types.PartMediaResolutionLevel.MEDIA_RESOLUTION_ULTRA_HIGH,+ )+ )+ continue++ if "uri" in image_data:+ parts.append({"file_uri": image_data["uri"]})++ return types.Content(role=gemini_role, parts=parts) # type: ignorediff --git a/backend/models/__init__.py b/backend/models/__init__.pydeleted file mode 100644index 0b54c5fa..00000000--- a/backend/models/__init__.py+++ /dev/null@@ -1,14 +0,0 @@-from .claude import stream_claude_response, stream_claude_response_native-from .openai_client import stream_openai_response-from .gemini import stream_gemini_response, stream_gemini_response_video-from llm import Completion--# Re-export the functions for backward compatibility-__all__ = [- "stream_claude_response",- "stream_claude_response_native",- "stream_openai_response",- "stream_gemini_response",- "stream_gemini_response_video",- "Completion",-]diff --git a/backend/models/claude.py b/backend/models/claude.pydeleted file mode 100644index 02505f42..00000000--- a/backend/models/claude.py+++ /dev/null@@ -1,259 +0,0 @@-import copy-import time-from typing import Any, Awaitable, Callable, Dict, List, Tuple, cast-from anthropic import AsyncAnthropic-from openai.types.chat import ChatCompletionMessageParam-from config import IS_DEBUG_ENABLED-from debug.DebugFileWriter import DebugFileWriter-from image_processing.utils import process_image-from utils import pprint_prompt-from llm import Completion, Llm---def convert_openai_messages_to_claude(- messages: List[ChatCompletionMessageParam],-) -> Tuple[str, List[Dict[str, Any]]]:- """- Convert OpenAI format messages to Claude format, handling image content properly.-- Args:- messages: List of messages in OpenAI format-- Returns:- Tuple of (system_prompt, claude_messages)- """- # Deep copy messages to avoid modifying the original list- cloned_messages = copy.deepcopy(messages)-- system_prompt = cast(str, cloned_messages[0].get("content"))- claude_messages = [dict(message) for message in cloned_messages[1:]]-- for message in claude_messages:- if not isinstance(message["content"], list):- continue-- for content in message["content"]: # type: ignore- if content["type"] == "image_url":- content["type"] = "image"-- # Extract base64 data and media type from data URL- # Example base64 data URL: data:image/png;base64,iVBOR...- image_data_url = cast(str, content["image_url"]["url"])-- # Process image and split media type and data- # so it works with Claude (under 5mb in base64 encoding)- (media_type, base64_data) = process_image(image_data_url)-- # Remove OpenAI parameter- del content["image_url"]-- content["source"] = {- "type": "base64",- "media_type": media_type,- "data": base64_data,- }-- return system_prompt, claude_messages---async def stream_claude_response(- messages: List[ChatCompletionMessageParam],- api_key: str,- callback: Callable[[str], Awaitable[None]],- model_name: str,- thinking_callback: Callable[[str], Awaitable[None]] | None = None,-) -> Completion:- start_time = time.time()- client = AsyncAnthropic(api_key=api_key)-- # Base parameters- max_tokens = 8192- temperature = 0.0-- # Translate OpenAI messages to Claude messages-- # Convert OpenAI format messages to Claude format- system_prompt, claude_messages = convert_openai_messages_to_claude(messages)-- response = ""-- # Models that support extended thinking- thinking_models = [- Llm.CLAUDE_4_5_SONNET_2025_09_29.value,- Llm.CLAUDE_4_5_OPUS_2025_11_01.value,- ]-- # Models that use adaptive thinking with effort- adaptive_thinking_models = [- Llm.CLAUDE_OPUS_4_6.value,- ]-- if model_name in adaptive_thinking_models:- print(f"Using {model_name} with adaptive thinking (effort: max)")- thinking_started = False- async with client.messages.stream(- model=model_name,- thinking={"type": "adaptive"},- max_tokens=30000,- system=system_prompt,- messages=claude_messages, # type: ignore- extra_body={"output_config": {"effort": "max"}},- ) as stream:- async for event in stream:- if event.type == "content_block_start":- if event.content_block.type == "thinking":- thinking_started = False- elif event.type == "content_block_delta":- if event.delta.type == "thinking_delta":- if not thinking_started:- thinking_started = True- if thinking_callback:- await thinking_callback(event.delta.thinking)- elif event.delta.type == "text_delta":- response += event.delta.text- await callback(event.delta.text)- elif event.type == "content_block_stop":- if thinking_started:- thinking_started = False-- elif model_name in thinking_models:- print(f"Using {model_name} with thinking")- thinking_started = False- # Thinking is not compatible with temperature- async with client.messages.stream(- model=model_name,- thinking={"type": "enabled", "budget_tokens": 10000},- max_tokens=30000,- system=system_prompt,- messages=claude_messages, # type: ignore- ) as stream:- async for event in stream:- if event.type == "content_block_start":- if event.content_block.type == "thinking":- thinking_started = False- elif event.type == "content_block_delta":- if event.delta.type == "thinking_delta":- if not thinking_started:- thinking_started = True- if thinking_callback:- await thinking_callback(event.delta.thinking)- elif event.delta.type == "text_delta":- response += event.delta.text- await callback(event.delta.text)- elif event.type == "content_block_stop":- if thinking_started:- thinking_started = False-- else:- # Stream Claude response- async with client.beta.messages.stream(- model=model_name,- max_tokens=max_tokens,- temperature=temperature,- system=system_prompt,- messages=claude_messages, # type: ignore- betas=["output-128k-2025-02-19"],- ) as stream:- async for text in stream.text_stream:- response += text- await callback(text)-- # Close the Anthropic client- await client.close()-- completion_time = time.time() - start_time- return {"duration": completion_time, "code": response}---async def stream_claude_response_native(- system_prompt: str,- messages: list[Any],- api_key: str,- callback: Callable[[str], Awaitable[None]],- include_thinking: bool = False,- model_name: str = Llm.CLAUDE_4_5_SONNET_2025_09_29.value,-) -> Completion:- start_time = time.time()- client = AsyncAnthropic(api_key=api_key)-- # Base model parameters- max_tokens = 4096- temperature = 0.0-- # Multi-pass flow- current_pass_num = 1- max_passes = 2-- prefix = "<thinking>"- response = None-- # For debugging- full_stream = ""- debug_file_writer = DebugFileWriter()-- while current_pass_num <= max_passes:- current_pass_num += 1-- # Set up message depending on whether we have a <thinking> prefix- messages_to_send = (- messages + [{"role": "assistant", "content": prefix}]- if include_thinking- else messages- )-- pprint_prompt(messages_to_send)-- async with client.messages.stream(- model=model_name,- max_tokens=max_tokens,- temperature=temperature,- system=system_prompt,- messages=messages_to_send, # type: ignore- ) as stream:- async for text in stream.text_stream:- print(text, end="", flush=True)- full_stream += text- await callback(text)-- response = await stream.get_final_message()- response_text = response.content[0].text-- # Write each pass's code to .html file and thinking to .txt file- if IS_DEBUG_ENABLED:- debug_file_writer.write_to_file(- f"pass_{current_pass_num - 1}.html",- debug_file_writer.extract_html_content(response_text),- )- debug_file_writer.write_to_file(- f"thinking_pass_{current_pass_num - 1}.txt",- response_text.split("</thinking>")[0],- )-- # Set up messages array for next pass- messages += [- {"role": "assistant", "content": str(prefix) + response.content[0].text},- {- "role": "user",- "content": "You've done a good job with a first draft. Improve this further based on the original instructions so that the app is fully functional and looks like the original video of the app we're trying to replicate.",- },- ]-- print(- f"Token usage: Input Tokens: {response.usage.input_tokens}, Output Tokens: {response.usage.output_tokens}"- )-- # Close the Anthropic client- await client.close()-- completion_time = time.time() - start_time-- if IS_DEBUG_ENABLED:- debug_file_writer.write_to_file("full_stream.txt", full_stream)-- if not response:- raise Exception("No HTML response found in AI response")- else:- return {- "duration": completion_time,- "code": response.content[0].text, # type: ignore- }diff --git a/backend/models/gemini.py b/backend/models/gemini.pydeleted file mode 100644index 0942766f..00000000--- a/backend/models/gemini.py+++ /dev/null@@ -1,463 +0,0 @@-import base64-import time-from typing import Any, Awaitable, Callable, Dict, List-from openai.types.chat import ChatCompletionMessageParam-from google import genai-from google.genai import types-from llm import Completion, Llm-from video.cost_estimation import (- calculate_cost,- estimate_video_generation_cost,- format_cost_estimate,- format_detailed_input_estimate,- get_video_duration_from_bytes,- MediaResolution,-)--# Set to True to print debug messages for Gemini requests-DEBUG_GEMINI = False-DEFAULT_VIDEO_FPS = 10---def get_gemini_api_model_name(model: Llm) -> str:- if model in [Llm.GEMINI_3_FLASH_PREVIEW_HIGH, Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL]:- return "gemini-3-flash-preview"- elif model in [Llm.GEMINI_3_PRO_PREVIEW_HIGH, Llm.GEMINI_3_PRO_PREVIEW_LOW]:- return "gemini-3-pro-preview"- return model.value---def get_thinking_level_for_model(model: Llm) -> str:- if model in [Llm.GEMINI_3_FLASH_PREVIEW_HIGH, Llm.GEMINI_3_PRO_PREVIEW_HIGH]:- return "high"- elif model == Llm.GEMINI_3_PRO_PREVIEW_LOW:- return "low"- elif model == Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL:- return "minimal"- return "high"---def extract_text_from_content(content: str | List[Dict[str, Any]]) -> str:- """- Extracts text from message content, handling both string and list formats.- """- if isinstance(content, str):- return content-- # Content is a list of content parts - find the text part- for content_part in content:- if content_part.get("type") == "text":- return content_part.get("text", "")-- return ""---def detect_mime_type_from_base64(base64_data: str) -> str | None:- """- Detect MIME type from base64 data by checking magic bytes.- Returns detected MIME type or None if unknown.- """- try:- # Decode first few bytes to check magic numbers- decoded = base64.b64decode(base64_data[:32])-- # Check for common image formats- if decoded[:8] == b'\x89PNG\r\n\x1a\n':- return "image/png"- if decoded[:2] == b'\xff\xd8':- return "image/jpeg"- if decoded[:6] in (b'GIF87a', b'GIF89a'):- return "image/gif"- if decoded[:4] == b'RIFF' and decoded[8:12] == b'WEBP':- return "image/webp"-- # Check for video formats- if decoded[4:8] == b'ftyp':- # MP4/MOV file- return "video/mp4"- if decoded[:4] == b'\x1aE\xdf\xa3':- # WebM file- return "video/webm"- except Exception:- pass-- return None---def extract_images_from_content(- content: str | List[Dict[str, Any]]-) -> List[Dict[str, str]]:- """- Extracts image data from message content.-- Args:- content: Message content (string or list of content parts)-- Returns:- List of image data dictionaries (mime_type+data or uri).- """- # If content is a string, there's no image- if isinstance(content, str):- return []-- # Content is a list of content parts- images: List[Dict[str, str]] = []- for content_part in content:- if content_part.get("type") == "image_url":- image_url = content_part["image_url"]["url"]- if image_url.startswith("data:"):- # Extract base64 data and mime type for data URLs- mime_type = image_url.split(";")[0].split(":")[1]- base64_data = image_url.split(",")[1]-- # If MIME type is generic, try to detect from data- if mime_type == "application/octet-stream":- detected_mime = detect_mime_type_from_base64(base64_data)- if detected_mime:- mime_type = detected_mime- else:- # Skip this content if we can't determine the type- print(f"Warning: Could not detect MIME type for data URL, skipping")- continue-- images.append({"mime_type": mime_type, "data": base64_data})- else:- # Handle regular URLs- images.append({"uri": image_url})-- return images---def convert_message_to_gemini_content(- message: ChatCompletionMessageParam,-) -> types.Content:- """- Convert an OpenAI-style message to Gemini Content format.- """- role = message.get("role", "user")- content = message.get("content", "")-- # Map roles: OpenAI uses "assistant", Gemini uses "model"- gemini_role = "model" if role == "assistant" else "user"-- parts: List[types.Part | Dict[str, str]] = []-- # Extract text and image from content- text = extract_text_from_content(content) # type: ignore- image_data_list = extract_images_from_content(content) # type: ignore-- if text:- parts.append({"text": text})- for image_data in image_data_list:- if "data" in image_data:- mime_type = image_data["mime_type"]- media_bytes = base64.b64decode(image_data["data"])- if mime_type.startswith("video/"):- # Gemini video inputs require inline video data plus metadata.- parts.append(- types.Part(- inline_data=types.Blob(data=media_bytes, mime_type=mime_type),- video_metadata=types.VideoMetadata(fps=DEFAULT_VIDEO_FPS),- media_resolution=types.PartMediaResolutionLevel.MEDIA_RESOLUTION_HIGH,- )- )- continue-- parts.append(- types.Part.from_bytes(- data=media_bytes,- mime_type=mime_type,- media_resolution=types.PartMediaResolutionLevel.MEDIA_RESOLUTION_ULTRA_HIGH,- )- )- elif "uri" in image_data:- parts.append({"file_uri": image_data["uri"]})-- return types.Content(role=gemini_role, parts=parts) # type: ignore---async def stream_gemini_response(- messages: List[ChatCompletionMessageParam],- api_key: str,- callback: Callable[[str], Awaitable[None]],- model: Llm,- thinking_callback: Callable[[str], Awaitable[None]] | None = None,-) -> Completion:- """- Stream a response from Gemini.-- Message format (same as OpenAI/Claude):- - messages[0]: System message with role="system", content=<system prompt string>- - messages[1:]: User/assistant messages, where user messages may contain images-- This mirrors how Claude handles messages:- - System prompt extracted from messages[0]- - All conversation history from messages[1:] is converted to Gemini format- """- start_time = time.time()-- # Extract system prompt from first message (same as Claude)- system_prompt = str(messages[0].get("content", ""))-- # Convert all messages after the system prompt to Gemini format- # This includes the full conversation history for edits- gemini_contents: List[types.Content] = []- for msg in messages[1:]:- gemini_contents.append(convert_message_to_gemini_content(msg))-- # Debug: print truncated message info- if DEBUG_GEMINI:- print(f"\n=== Gemini Request Debug ({model.value}) ===")- print(f"System prompt (first 200 chars): {system_prompt[:200]}...")- print(f"Number of conversation messages: {len(gemini_contents)}")- for i, content in enumerate(gemini_contents):- role = content.role- text_preview = ""- has_image = False- for part in content.parts:- if hasattr(part, "text") and part.text:- text_preview = part.text[:100]- if hasattr(part, "inline_data") and part.inline_data:- has_image = True- print(f" [{i}] {role}: {text_preview}... (has_image={has_image})")- print("=" * 50)-- client = genai.Client(api_key=api_key)- full_response = ""-- if model == Llm.GEMINI_3_FLASH_PREVIEW_HIGH:- # Gemini 3 Flash with HIGH thinking- config = types.GenerateContentConfig(- temperature=0,- max_output_tokens=30000,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="high", include_thoughts=True- ),- )- elif model == Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL:- # Gemini 3 Flash with MINIMAL thinking- config = types.GenerateContentConfig(- temperature=0,- max_output_tokens=30000,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="minimal", include_thoughts=True- ),- )- elif model == Llm.GEMINI_3_PRO_PREVIEW_HIGH:- # Gemini 3 Pro with HIGH thinking- config = types.GenerateContentConfig(- temperature=0,- max_output_tokens=30000,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="high", include_thoughts=True- ),- )- elif model == Llm.GEMINI_3_PRO_PREVIEW_LOW:- # Gemini 3 Pro with LOW thinking- config = types.GenerateContentConfig(- temperature=0,- max_output_tokens=30000,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="low", include_thoughts=True- ),- )- else:- config = types.GenerateContentConfig(- temperature=0,- max_output_tokens=8000,- system_instruction=system_prompt,- )-- # Map variant model names to actual API model names- 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 chunk.candidates and len(chunk.candidates) > 0:- for part in chunk.candidates[0].content.parts:- if not part.text:- continue- elif part.thought:- if thinking_callback:- await thinking_callback(part.text)- else:- print(f"\n=== Gemini Thinking Summary ({model.value}) ===")- print(part.text)- print("=" * 50)- else:- full_response += part.text- await callback(part.text)-- completion_time = time.time() - start_time- return {"duration": completion_time, "code": full_response}---async def stream_gemini_response_video(- video_bytes: bytes,- video_mime_type: str,- system_prompt: str,- api_key: str,- callback: Callable[[str], Awaitable[None]],- model: Llm,- thinking_callback: Callable[[str], Awaitable[None]] | None = None,-) -> Completion:- start_time = time.time()-- # Video generation settings- VIDEO_FPS = 10- MAX_OUTPUT_TOKENS = 50000-- # Get video duration and estimate input tokens- video_duration = get_video_duration_from_bytes(video_bytes)- estimated_input_tokens = None- if video_duration:- estimated_cost = estimate_video_generation_cost(- video_duration_seconds=video_duration,- model=model,- fps=VIDEO_FPS,- media_resolution=MediaResolution.HIGH,- )- estimated_input_tokens = estimated_cost.input_tokens- print(f"\n=== Video Generation Input Estimate ({model.value}) ===")- print(format_detailed_input_estimate(video_duration, VIDEO_FPS, MediaResolution.HIGH, model))- print("=" * 50)- else:- print("Warning: Could not determine video duration for cost estimation")-- client = genai.Client(api_key=api_key)- full_response = ""-- # Create content with video inline data at specified FPS for better fidelity- contents = types.Content(- role="user",- parts=[- types.Part(- inline_data=types.Blob(data=video_bytes, mime_type=video_mime_type),- video_metadata=types.VideoMetadata(fps=VIDEO_FPS),- media_resolution=types.PartMediaResolutionLevel.MEDIA_RESOLUTION_HIGH- ),- types.Part(text="Analyze this video and generate the code."),- ],- )-- # Configure based on model- if model == Llm.GEMINI_3_FLASH_PREVIEW_HIGH:- config = types.GenerateContentConfig(- temperature=1.0,- max_output_tokens=MAX_OUTPUT_TOKENS,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="high", include_thoughts=True- ),- )- elif model == Llm.GEMINI_3_FLASH_PREVIEW_MINIMAL:- config = types.GenerateContentConfig(- temperature=1.0,- max_output_tokens=MAX_OUTPUT_TOKENS,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="minimal", include_thoughts=True- ),- media_resolution=types.MediaResolution.MEDIA_RESOLUTION_HIGH,- )- elif model == Llm.GEMINI_3_PRO_PREVIEW_HIGH:- config = types.GenerateContentConfig(- temperature=1.0,- max_output_tokens=MAX_OUTPUT_TOKENS,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="high", include_thoughts=True- ),- # media_resolution=types.MediaResolution.MEDIA_RESOLUTION_HIGH,- )- elif model == Llm.GEMINI_3_PRO_PREVIEW_LOW:- config = types.GenerateContentConfig(- temperature=1.0,- max_output_tokens=MAX_OUTPUT_TOKENS,- system_instruction=system_prompt,- thinking_config=types.ThinkingConfig(- thinking_level="low", include_thoughts=True- ),- media_resolution=types.MediaResolution.MEDIA_RESOLUTION_HIGH,- )- else:- # Default config for other models- config = types.GenerateContentConfig(- temperature=1.0,- max_output_tokens=MAX_OUTPUT_TOKENS,- system_instruction=system_prompt,- )-- api_model_name = get_gemini_api_model_name(model)-- if DEBUG_GEMINI:- print(f"\n=== Gemini Video Request Debug ({api_model_name}) ===")- print(f"Video MIME type: {video_mime_type}")- print(f"Video size: {len(video_bytes)} bytes")- print(f"System prompt (first 200 chars): {system_prompt[:200]}...")- print("=" * 50)-- # Track actual token usage from the response- # usage_metadata fields: prompt_token_count, candidates_token_count, total_token_count- usage_metadata = None-- async for chunk in await client.aio.models.generate_content_stream(- model=api_model_name,- contents=contents,- config=config,- ):- # Capture usage metadata (available in final chunks)- if chunk.usage_metadata:- usage_metadata = chunk.usage_metadata-- if chunk.candidates and len(chunk.candidates) > 0:- for part in chunk.candidates[0].content.parts:- if not part.text:- continue- elif part.thought:- if thinking_callback:- await thinking_callback(part.text)- else:- print(f"\n=== Gemini Video Thinking Summary ({api_model_name}) ===")- print(part.text)- print("=" * 50)- else:- full_response += part.text- await callback(part.text)-- completion_time = time.time() - start_time-- # Log actual token usage and cost- if usage_metadata:- input_tokens = usage_metadata.prompt_token_count or 0- thinking_tokens = usage_metadata.thoughts_token_count or 0- output_tokens = usage_metadata.candidates_token_count or 0- total_tokens = usage_metadata.total_token_count or 0- cached_tokens = getattr(usage_metadata, 'cached_content_token_count', 0) or 0-- # Thinking tokens are billed at the same rate as output tokens- billable_output_tokens = thinking_tokens + output_tokens- actual_cost = calculate_cost(input_tokens, billable_output_tokens, model)-- print(f"\n=== Video Generation Actual Usage ({model.value}) ===")- print(f"Input tokens: {input_tokens:,} (${actual_cost.input_cost:.4f})")- print(f"Cached tokens: {cached_tokens:,}")- if estimated_input_tokens:- diff_pct = ((input_tokens - estimated_input_tokens) / estimated_input_tokens) * 100- print(f"Est vs Actual: {estimated_input_tokens:,} → {input_tokens:,} ({diff_pct:+.1f}%)")- print(f"Thinking tokens: {thinking_tokens:,}")- print(f"Output tokens: {output_tokens:,}")- print(f"Billable output: {billable_output_tokens:,} (${actual_cost.output_cost:.4f})")- print(f"Total tokens: {total_tokens:,}")- print(f"Total cost: ${actual_cost.total_cost:.4f}")- print(f"Generation time: {completion_time:.2f} seconds")- print("=" * 50)-- return {"duration": completion_time, "code": full_response}diff --git a/backend/models/openai_client.py b/backend/models/openai_client.pydeleted file mode 100644index e5de718f..00000000--- a/backend/models/openai_client.py+++ /dev/null@@ -1,81 +0,0 @@-import time-from typing import Awaitable, Callable, List, Any, Dict-from openai import AsyncOpenAI-from openai.types.chat import ChatCompletionMessageParam-from llm import Completion, Llm, get_openai_api_name, get_openai_reasoning_effort---def _convert_message_to_responses_input(- message: ChatCompletionMessageParam,-) -> Dict[str, Any]:- role = message.get("role", "user")- content = message.get("content", "")-- if isinstance(content, str):- return {"role": role, "content": content}-- parts: List[Dict[str, Any]] = []- if isinstance(content, list):- for part in content:- if not isinstance(part, dict):- continue- if part.get("type") == "text":- parts.append({"type": "input_text", "text": part.get("text", "")})- elif part.get("type") == "image_url":- image_url = part.get("image_url", {})- parts.append(- {- "type": "input_image",- "image_url": image_url.get("url", ""),- "detail": image_url.get("detail", "auto"),- }- )-- return {"role": role, "content": parts}---async def stream_openai_response(- messages: List[ChatCompletionMessageParam],- api_key: str,- base_url: str | None,- callback: Callable[[str], Awaitable[None]],- model: Llm,-) -> Completion:- start_time = time.time()- client = AsyncOpenAI(api_key=api_key, base_url=base_url)-- full_response = ""- input_items = [_convert_message_to_responses_input(msg) for msg in messages]- reasoning_effort = get_openai_reasoning_effort(model)- params: Dict[str, Any] = {- "model": get_openai_api_name(model),- "input": input_items,- "stream": True,- "max_output_tokens": 30000,- }- if reasoning_effort:- params["reasoning"] = {"effort": reasoning_effort}-- responses_client = getattr(client, "responses", None)- if responses_client is None:- raise Exception(- "OpenAI SDK is too old for GPT-5.2 Codex. Please upgrade the 'openai' package to a version that supports the Responses API."- )- stream = await responses_client.create(**params) # type: ignore- async for event in stream: # type: ignore- event_type = getattr(event, "type", None)- if event_type is None and isinstance(event, dict):- event_type = event.get("type")- if event_type == "response.output_text.delta":- delta = getattr(event, "delta", None)- if delta is None and isinstance(event, dict):- delta = event.get("delta", "")- if not delta:- continue- full_response += delta- await callback(delta)-- await client.close()-- completion_time = time.time() - start_time- return {"duration": completion_time, "code": full_response}