(self, args: Dict[str, Any])
| 394 | ) |
| 395 | |
| 396 | async def _edit_image(self, args: Dict[str, Any]) -> ToolExecutionResult: |
| 397 | replicate_api_key = self._effective_replicate_api_key() |
| 398 | if not replicate_api_key: |
| 399 | return ToolExecutionResult( |
| 400 | ok=False, |
| 401 | result={"error": "Image editing requires REPLICATE_API_KEY."}, |
| 402 | summary={"error": "Missing Replicate API key"}, |
| 403 | ) |
| 404 | |
| 405 | prompt = ensure_str(args.get("prompt")).strip() |
| 406 | if not prompt: |
| 407 | return ToolExecutionResult( |
| 408 | ok=False, |
| 409 | result={"error": "edit_image requires a non-empty prompt"}, |
| 410 | summary={"error": "Missing prompt"}, |
| 411 | ) |
| 412 | |
| 413 | image_urls = args.get("image_urls") or args.get("images") or [] |
| 414 | if not isinstance(image_urls, list) or not image_urls: |
| 415 | return ToolExecutionResult( |
| 416 | ok=False, |
| 417 | result={"error": "edit_image requires a non-empty image_urls list"}, |
| 418 | summary={"error": "Missing image_urls"}, |
| 419 | ) |
| 420 | |
| 421 | cleaned = [url.strip() for url in image_urls if isinstance(url, str)] |
| 422 | unique_urls = list(dict.fromkeys([u for u in cleaned if u])) |
| 423 | if not unique_urls: |
| 424 | return ToolExecutionResult( |
| 425 | ok=False, |
| 426 | result={"error": "No valid image URLs provided"}, |
| 427 | summary={"error": "No valid image_urls"}, |
| 428 | ) |
| 429 | |
| 430 | aspect_ratio_value = ensure_str(args.get("aspect_ratio") or "match_input_image") |
| 431 | if aspect_ratio_value not in P_IMAGE_EDIT_ASPECT_RATIOS: |
| 432 | aspect_ratio_value = "match_input_image" |
| 433 | aspect_ratio = cast(PImageEditAspectRatio, aspect_ratio_value) |
| 434 | |
| 435 | try: |
| 436 | result_url = await edit_image( |
| 437 | prompt=prompt, |
| 438 | image_urls=[local_asset_url_to_data_url(url) for url in unique_urls], |
| 439 | api_token=replicate_api_key, |
| 440 | aspect_ratio=aspect_ratio, |
| 441 | ) |
| 442 | except Exception as exc: |
| 443 | print(f"Image edit failed for {unique_urls}: {exc}") |
| 444 | return ToolExecutionResult( |
| 445 | ok=True, |
| 446 | result={ |
| 447 | "image": { |
| 448 | "prompt": prompt, |
| 449 | "image_urls": unique_urls, |
| 450 | "result_url": None, |
| 451 | "status": "error", |
| 452 | } |
| 453 | }, |
no test coverage detected