@@ -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 ;
0 commit comments