diff --git a/content-gen/src/app/frontend/src/App.tsx b/content-gen/src/app/frontend/src/App.tsx index 952942d72..a52e3a026 100644 --- a/content-gen/src/app/frontend/src/App.tsx +++ b/content-gen/src/app/frontend/src/App.tsx @@ -360,8 +360,21 @@ function App() { // Update generatedContent with new image if (parsedContent.image_url || parsedContent.image_base64) { + // Replace old color/product name in text_content when switching products + const oldName = selectedProducts[0]?.product_name; + const newName = mentionedProduct?.product_name; + const nameRegex = oldName + ? new RegExp(oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi') + : undefined; + const swapName = (s?: string) => { + if (!s || !oldName || !newName || oldName === newName || !nameRegex) return s; + return s.replace(nameRegex, () => newName); + }; + const tc = generatedContent.text_content; + responseData = { ...generatedContent, + text_content: mentionedProduct ? { ...tc, headline: swapName(tc?.headline), body: swapName(tc?.body), tagline: swapName(tc?.tagline), cta_text: swapName(tc?.cta_text) } : tc, image_content: { ...generatedContent.image_content, image_url: parsedContent.image_url || generatedContent.image_content?.image_url, diff --git a/content-gen/src/backend/app.py b/content-gen/src/backend/app.py index c328f7d15..d12589e31 100644 --- a/content-gen/src/backend/app.py +++ b/content-gen/src/backend/app.py @@ -9,6 +9,7 @@ import json import logging import os +import re import uuid from datetime import datetime, timezone from typing import Dict, Any @@ -992,12 +993,28 @@ async def generate(): existing_content = raw_content if isinstance(raw_content, dict) else {} old_image_url = existing_content.get("image_url") + # Replace old color/product name in text_content when product changes + old_products = existing_content.get("selected_products", []) + old_name = old_products[0].get("product_name", "") if old_products else "" + new_name = products_data[0].get("product_name", "") if products_data else "" + existing_text = existing_content.get("text_content") + if existing_text and old_name and new_name and old_name != new_name: + pat = re.compile(re.escape(old_name), re.IGNORECASE) + if isinstance(existing_text, dict): + existing_text = { + k: pat.sub(lambda _m: new_name, v) if isinstance(v, str) else v + for k, v in existing_text.items() + } + elif isinstance(existing_text, str): + existing_text = pat.sub(lambda _m: new_name, existing_text) + updated_content = { **existing_content, "image_url": new_image_url if new_image_url else old_image_url, "image_prompt": new_image_prompt if new_image_prompt else existing_content.get("image_prompt"), "image_revised_prompt": new_image_revised_prompt if new_image_revised_prompt else existing_content.get("image_revised_prompt"), "selected_products": products_data if products_data else existing_content.get("selected_products", []), + **(({"text_content": existing_text} if existing_text is not None else {})), } await cosmos_service.save_generated_content(