From 0f975638266d4bf0ff63270fb8f0ff886e4e247d Mon Sep 17 00:00:00 2001 From: "Niraj Chaudhari (Persistent Systems Inc)" Date: Fri, 20 Feb 2026 08:56:53 +0530 Subject: [PATCH 1/3] Fix for text-context updation after regenerating image --- content-gen/src/app/frontend/src/App.tsx | 10 ++++++++++ content-gen/src/backend/app.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/content-gen/src/app/frontend/src/App.tsx b/content-gen/src/app/frontend/src/App.tsx index 952942d72..c83f8c6f8 100644 --- a/content-gen/src/app/frontend/src/App.tsx +++ b/content-gen/src/app/frontend/src/App.tsx @@ -360,8 +360,18 @@ 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 swapName = (s?: string) => { + if (!s || !oldName || !newName || oldName === newName) return s; + return s.replace(new RegExp(oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), 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..464295768 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,25 @@ 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(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(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( From 4bafc33001411664496b3dec3ef5e62b9f123f48 Mon Sep 17 00:00:00 2001 From: NirajC-Microsoft Date: Fri, 20 Feb 2026 09:05:03 +0530 Subject: [PATCH 2/3] Update content-gen/src/backend/app.py Accepting Copilot PR reviewer suggestion to use lambda functions Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content-gen/src/backend/app.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/content-gen/src/backend/app.py b/content-gen/src/backend/app.py index 464295768..d12589e31 100644 --- a/content-gen/src/backend/app.py +++ b/content-gen/src/backend/app.py @@ -1001,9 +1001,12 @@ async def generate(): 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(new_name, v) if isinstance(v, str) else v for k, v in existing_text.items()} + 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(new_name, existing_text) + existing_text = pat.sub(lambda _m: new_name, existing_text) updated_content = { **existing_content, From b6ed0f4d58e81e374eb767f01c62d3d3234ad225 Mon Sep 17 00:00:00 2001 From: NirajC-Microsoft Date: Fri, 20 Feb 2026 11:26:08 +0530 Subject: [PATCH 3/3] Update content-gen/src/app/frontend/src/App.tsx Accepted the Suggestion by Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content-gen/src/app/frontend/src/App.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/content-gen/src/app/frontend/src/App.tsx b/content-gen/src/app/frontend/src/App.tsx index c83f8c6f8..a52e3a026 100644 --- a/content-gen/src/app/frontend/src/App.tsx +++ b/content-gen/src/app/frontend/src/App.tsx @@ -363,9 +363,12 @@ function App() { // 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) return s; - return s.replace(new RegExp(oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), newName); + if (!s || !oldName || !newName || oldName === newName || !nameRegex) return s; + return s.replace(nameRegex, () => newName); }; const tc = generatedContent.text_content;