Skip to content

Commit 3efb1e7

Browse files
fix: regenerated image persistence (Cosmos) and update UI color/product state after successful regeneration
2 parents 20c1666 + bcfd06d commit 3efb1e7

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

content-gen/src/app/frontend/src/App.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ function App() {
116116
setAwaitingClarification(false);
117117
setConfirmedBrief(data.brief || null);
118118

119+
// Restore availableProducts so product/color name detection works
120+
// when regenerating images in a restored conversation
121+
if (data.brief) {
122+
try {
123+
const productsResponse = await fetch('/api/products');
124+
if (productsResponse.ok) {
125+
const productsData = await productsResponse.json();
126+
setAvailableProducts(productsData.products || []);
127+
}
128+
} catch (err) {
129+
console.error('Error loading products for restored conversation:', err);
130+
}
131+
}
132+
119133
if (data.generated_content) {
120134
const gc = data.generated_content;
121135
let textContent = gc.text_content;
@@ -319,13 +333,20 @@ function App() {
319333
let responseData: GeneratedContent | null = null;
320334
let messageContent = '';
321335

336+
// Detect if the user's prompt mentions a different product/color name
337+
// BEFORE the API call so the correct product is sent and persisted
338+
const mentionedProduct = availableProducts.find(p =>
339+
content.toLowerCase().includes(p.product_name.toLowerCase())
340+
);
341+
const productsForRequest = mentionedProduct ? [mentionedProduct] : selectedProducts;
342+
322343
// Get previous prompt from image_content if available
323344
const previousPrompt = generatedContent.image_content?.prompt_used;
324345

325346
for await (const response of streamRegenerateImage(
326347
content,
327348
confirmedBrief,
328-
selectedProducts,
349+
productsForRequest,
329350
previousPrompt,
330351
conversationId,
331352
userId,
@@ -350,6 +371,11 @@ function App() {
350371
};
351372
setGeneratedContent(responseData);
352373

374+
// Update the selected product/color name now that the new image is ready
375+
if (mentionedProduct) {
376+
setSelectedProducts([mentionedProduct]);
377+
}
378+
353379
// Update the confirmed brief to include the modification
354380
// This ensures subsequent "Regenerate" clicks use the updated visual guidelines
355381
const updatedBrief = {
@@ -541,7 +567,7 @@ function App() {
541567
// Trigger refresh of chat history after message is sent
542568
setHistoryRefreshTrigger(prev => prev + 1);
543569
}
544-
}, [conversationId, userId, confirmedBrief, pendingBrief, selectedProducts, generatedContent]);
570+
}, [conversationId, userId, confirmedBrief, pendingBrief, selectedProducts, generatedContent, availableProducts]);
545571

546572
const handleBriefConfirm = useCallback(async () => {
547573
if (!pendingBrief) return;

content-gen/src/backend/app.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ async def generate():
967967
except Exception as e:
968968
logger.warning(f"Failed to save regenerated image to blob: {e}")
969969

970-
# Save assistant response
970+
# Save assistant response and update persisted generated_content
971971
try:
972972
cosmos_service = await get_cosmos_service()
973973
await cosmos_service.add_message_to_conversation(
@@ -980,6 +980,31 @@ async def generate():
980980
"timestamp": datetime.now(timezone.utc).isoformat()
981981
}
982982
)
983+
984+
# Persist the regenerated image and updated products to generated_content
985+
# so the latest image and color/product name are restored on conversation reload
986+
new_image_url = response.get("image_url")
987+
new_image_prompt = response.get("image_prompt")
988+
new_image_revised_prompt = response.get("image_revised_prompt")
989+
990+
existing_conversation = await cosmos_service.get_conversation(conversation_id, user_id)
991+
raw_content = (existing_conversation or {}).get("generated_content")
992+
existing_content = raw_content if isinstance(raw_content, dict) else {}
993+
old_image_url = existing_content.get("image_url")
994+
995+
updated_content = {
996+
**existing_content,
997+
"image_url": new_image_url if new_image_url else old_image_url,
998+
"image_prompt": new_image_prompt if new_image_prompt else existing_content.get("image_prompt"),
999+
"image_revised_prompt": new_image_revised_prompt if new_image_revised_prompt else existing_content.get("image_revised_prompt"),
1000+
"selected_products": products_data if products_data else existing_content.get("selected_products", []),
1001+
}
1002+
1003+
await cosmos_service.save_generated_content(
1004+
conversation_id=conversation_id,
1005+
user_id=user_id,
1006+
generated_content=updated_content
1007+
)
9831008
except Exception as e:
9841009
logger.warning(f"Failed to save regeneration response to CosmosDB: {e}")
9851010

0 commit comments

Comments
 (0)