Skip to content

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

Python

Convert remove_background tool to batch processing

Claudeee482997e956
diff --git a/backend/agent/tools/definitions.py b/backend/agent/tools/definitions.py
index b9a4c17c..d29f4c79 100644
--- a/backend/agent/tools/definitions.py
+++ b/backend/agent/tools/definitions.py
@@ -76,12 +76,15 @@ def _remove_background_schema() -> Dict[str, Any]:
return {
"type": "object",
"properties": {
- "image_url": {
- "type": "string",
- "description": "URL of the image to remove the background from.",
+ "image_urls": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "description": "URL of an image to remove the background from.",
+ },
},
},
- "required": ["image_url"],
+ "required": ["image_urls"],
}
@@ -135,8 +138,9 @@ def canonical_tool_definitions(
CanonicalToolDefinition(
name="remove_background",
description=(
- "Remove the background from an image. Returns a URL to the processed "
- "image with transparent background."
+ "Remove the background from one or more images. You can pass multiple "
+ "image URLs at once. Returns URLs to the processed images with "
+ "transparent backgrounds."
),
parameters=_remove_background_schema(),
),
diff --git a/backend/agent/tools/runtime.py b/backend/agent/tools/runtime.py
index a846d219..ba031c83 100644
--- a/backend/agent/tools/runtime.py
+++ b/backend/agent/tools/runtime.py
@@ -1,5 +1,6 @@
# pyright: reportUnknownVariableType=false
-from typing import Any, Dict, List, Optional, Tuple
+import asyncio
+from typing import Any, Dict, List, Optional, Tuple, Union
from codegen.utils import extract_html_content
from config import REPLICATE_API_KEY
@@ -241,30 +242,56 @@ class AgentToolRuntime:
summary={"error": "Missing Replicate API key"},
)
- image_url = ensure_str(args.get("image_url"))
- if not image_url:
+ image_urls = args.get("image_urls") or []
+ if not isinstance(image_urls, list) or not image_urls:
return ToolExecutionResult(
ok=False,
- result={"error": "remove_background requires an image_url"},
- summary={"error": "Missing image_url"},
+ result={
+ "error": "remove_background requires a non-empty image_urls list"
+ },
+ summary={"error": "Missing image_urls"},
)
- try:
- result_url = await remove_background(image_url, REPLICATE_API_KEY)
- result = {"image_url": image_url, "result_url": result_url}
- summary = {
- "image_url": summarize_text(image_url, 100),
- "result_url": result_url,
- "status": "ok",
- }
- return ToolExecutionResult(ok=True, result=result, summary=summary)
- except Exception as exc:
+ cleaned = [url.strip() for url in image_urls if isinstance(url, str)]
+ unique_urls = list(dict.fromkeys([u for u in cleaned if u]))
+ if not unique_urls:
return ToolExecutionResult(
ok=False,
- result={"error": str(exc), "image_url": image_url},
- summary={"error": summarize_text(str(exc), 100)},
+ result={"error": "No valid image URLs provided"},
+ summary={"error": "No valid image_urls"},
)
+ tasks = [
+ remove_background(url, REPLICATE_API_KEY) for url in unique_urls
+ ]
+ raw_results = await asyncio.gather(*tasks, return_exceptions=True)
+
+ results: List[Dict[str, Any]] = []
+ for url, raw in zip(unique_urls, raw_results):
+ if isinstance(raw, BaseException):
+ print(f"Background removal failed for {url}: {raw}")
+ results.append(
+ {"image_url": url, "result_url": None, "status": "error"}
+ )
+ else:
+ results.append(
+ {"image_url": url, "result_url": raw, "status": "ok"}
+ )
+
+ summary_items = [
+ {
+ "image_url": summarize_text(r["image_url"], 100),
+ "result_url": r["result_url"],
+ "status": r["status"],
+ }
+ for r in results
+ ]
+ return ToolExecutionResult(
+ ok=True,
+ result={"images": results},
+ summary={"images": summary_items},
+ )
+
def _retrieve_option(self, args: Dict[str, Any]) -> ToolExecutionResult:
raw_option_number = args.get("option_number")
raw_index = args.get("index")
diff --git a/backend/agent/tools/summaries.py b/backend/agent/tools/summaries.py
index 71c345db..c87ba88e 100644
--- a/backend/agent/tools/summaries.py
+++ b/backend/agent/tools/summaries.py
@@ -55,8 +55,13 @@ def summarize_tool_input(tool_call: ToolCall, file_state: AgentFileState) -> Dic
}
if tool_call.name == "remove_background":
- image_url = ensure_str(args.get("image_url"))
- return {"image_url": summarize_text(image_url, 100)}
+ image_urls = args.get("image_urls") or []
+ if isinstance(image_urls, list):
+ return {
+ "count": len(image_urls),
+ "image_urls": [ensure_str(u) for u in image_urls],
+ }
+ return {"image_urls": []}
if tool_call.name == "retrieve_option":
return {
diff --git a/backend/prompts/system_prompt.py b/backend/prompts/system_prompt.py
index 29157c76..0fa1d9b9 100644
--- a/backend/prompts/system_prompt.py
+++ b/backend/prompts/system_prompt.py
@@ -16,7 +16,7 @@ You are a coding agent that's an expert at building front-ends.
- 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.
- When available, use generate_images to create image URLs from prompts (you may pass multiple prompts). The image generation AI is not capable of generating images with a transparent background.
-- Use remove_background to remove the background from a provided image URL when needed.
+- Use remove_background to remove backgrounds from provided image URLs when needed (you may pass multiple image URLs).
- Use retrieve_option to fetch the full HTML for a specific option (1-based option_number) when a user references another option.
diff --git a/frontend/src/components/agent/AgentActivity.tsx b/frontend/src/components/agent/AgentActivity.tsx
index a0a20fc1..d01af32d 100644
--- a/frontend/src/components/agent/AgentActivity.tsx
+++ b/frontend/src/components/agent/AgentActivity.tsx
@@ -136,9 +136,13 @@ function getEventTitle(event: AgentEvent): string {
return count ? `Generated ${count} image${count !== 1 ? "s" : ""}` : "Generated images";
}
if (event.toolName === "remove_background") {
- return event.status === "running"
- ? "Removing background"
- : "Background removed";
+ const rbInput = event.input as any;
+ const rbOutput = event.output as any;
+ const rbCount = rbOutput?.images?.length || rbInput?.image_urls?.length || 0;
+ if (event.status === "running") {
+ return rbCount > 1 ? `Removing ${rbCount} backgrounds` : "Removing background";
+ }
+ return rbCount > 1 ? `Removed ${rbCount} backgrounds` : "Background removed";
}
if (event.toolName === "retrieve_option") {
return event.status === "running"
@@ -279,53 +283,63 @@ function renderToolDetails(event: AgentEvent, variantCode?: string) {
{event.toolName === "remove_background" && !hasError && (
<div>
- {/* While running: show the source image */}
- {event.status === "running" && (input?.image_url || output?.image_url) && (
- <img
- src={input?.image_url || output?.image_url}
- alt="Original image"
- className="w-full rounded object-cover"
- loading="lazy"
- />
+ {/* While running: show the source images */}
+ {event.status === "running" && input?.image_urls && Array.isArray(input.image_urls) && (
+ <div className="divide-y divide-gray-100 dark:divide-gray-800">
+ {input.image_urls.map((url: string, index: number) => (
+ <div key={index} className="py-2">
+ <img
+ src={url}
+ alt={`Original image ${index + 1}`}
+ className="w-full rounded object-cover"
+ loading="lazy"
+ />
+ </div>
+ ))}
+ </div>
)}
- {/* After complete: before/after side by side */}
- {event.status !== "running" && output?.image_url && (
- <div className="flex gap-2">
- <div className="w-1/2">
- <div className="text-xs text-gray-500 dark:text-gray-400 mb-1">Before</div>
- <img
- src={output.image_url}
- alt="Original image"
- className="w-full rounded object-cover"
- loading="lazy"
- />
- </div>
- <div className="w-1/2">
- <div className="text-xs text-gray-500 dark:text-gray-400 mb-1">After</div>
- {output?.result_url ? (
- <div className="relative">
- <div
- className="absolute inset-0 rounded"
- style={{
- backgroundImage:
- "linear-gradient(45deg, #e5e7eb 25%, transparent 25%), linear-gradient(-45deg, #e5e7eb 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #e5e7eb 75%), linear-gradient(-45deg, transparent 75%, #e5e7eb 75%)",
- backgroundSize: "10px 10px",
- backgroundPosition: "0 0, 0 5px, 5px -5px, -5px 0px",
- }}
- />
+ {/* After complete: before/after side by side for each image */}
+ {event.status !== "running" && output?.images && Array.isArray(output.images) && (
+ <div className="divide-y divide-gray-100 dark:divide-gray-800">
+ {output.images.map((item: any, index: number) => (
+ <div key={`${item.image_url}-${index}`} className="flex gap-2 py-2">
+ <div className="w-1/2">
+ <div className="text-xs text-gray-500 dark:text-gray-400 mb-1">Before</div>
<img
- src={output.result_url}
- alt="Background removed"
- className="relative w-full rounded"
+ src={item.image_url}
+ alt={`Original image ${index + 1}`}
+ className="w-full rounded object-cover"
loading="lazy"
/>
</div>
- ) : (
- <div className="aspect-square rounded bg-gray-100 dark:bg-gray-800 flex items-center justify-center text-xs text-gray-400">
- Failed
+ <div className="w-1/2">
+ <div className="text-xs text-gray-500 dark:text-gray-400 mb-1">After</div>
+ {item.result_url ? (
+ <div className="relative">
+ <div
+ className="absolute inset-0 rounded"
+ style={{
+ backgroundImage:
+ "linear-gradient(45deg, #e5e7eb 25%, transparent 25%), linear-gradient(-45deg, #e5e7eb 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #e5e7eb 75%), linear-gradient(-45deg, transparent 75%, #e5e7eb 75%)",
+ backgroundSize: "10px 10px",
+ backgroundPosition: "0 0, 0 5px, 5px -5px, -5px 0px",
+ }}
+ />
+ <img
+ src={item.result_url}
+ alt="Background removed"
+ className="relative w-full rounded"
+ loading="lazy"
+ />
+ </div>
+ ) : (
+ <div className="aspect-square rounded bg-gray-100 dark:bg-gray-800 flex items-center justify-center text-xs text-gray-400">
+ Failed
+ </div>
+ )}
</div>
- )}
- </div>
+ </div>
+ ))}
</div>
)}
</div>