@@ -309,6 +309,20 @@ export const useChat = (initialChatId?: string, selectedProvider?: string, selec
309309 setCurrentChatId ( result . id ) ;
310310 setIsChatIdValidated ( true ) ;
311311
312+ // Trigger image generation if messages were created
313+ if ( result . messages ) {
314+ const assistantMessage = result . messages . find ( ( msg : any ) => msg . role === "assistant" ) ;
315+ if ( assistantMessage ?. generation ?. id ) {
316+ // Mark as generating before triggering
317+ assistantMessage . generation . status = "generating" ;
318+
319+ // Fire and forget - don't await, let it run in background
320+ chatService . createMessageGenerate ( { generationId : assistantMessage . generation . id } ) . catch ( ( error ) => {
321+ console . error ( "Error triggering image generation:" , error ) ;
322+ } ) ;
323+ }
324+ }
325+
312326 // Return the chat ID - no need to send another message since createChat already handled it
313327 return result . id ;
314328 } catch ( error ) {
@@ -418,6 +432,11 @@ export const useChat = (initialChatId?: string, selectedProvider?: string, selec
418432
419433 // Use returned messages to update the chat data instead of revalidating
420434 if ( result ?. messages ) {
435+ // Trigger image generation in browser first (not blocked by CF Worker timeout)
436+ const assistantMessage = result . messages . find ( ( msg ) => msg . role === "assistant" ) ;
437+ const shouldTriggerGeneration = assistantMessage ?. generation ?. id ;
438+
439+ // Update UI - mark generation as "generating" to avoid immediate polling
421440 currentChatMutate ( ( currentData ) => {
422441 if ( ! currentData ) return currentData ;
423442
@@ -426,13 +445,35 @@ export const useChat = (initialChatId?: string, selectedProvider?: string, selec
426445 const existingMessages =
427446 currentData . messages ?. filter ( ( msg : any ) => msg . id !== optimisticUserMessage . id ) || [ ] ;
428447
429- // Append new messages from server
448+ // Append new messages from server, but mark as "generating" if we're about to trigger
449+ const newMessages = shouldTriggerGeneration
450+ ? result . messages . map ( ( msg : any ) => {
451+ if ( msg . role === "assistant" && msg . generation ) {
452+ return {
453+ ...msg ,
454+ generation : {
455+ ...msg . generation ,
456+ status : "generating" as const ,
457+ } ,
458+ } ;
459+ }
460+ return msg ;
461+ } )
462+ : result . messages ;
463+
430464 return {
431465 ...currentData ,
432- messages : [ ...existingMessages , ...result . messages ] ,
466+ messages : [ ...existingMessages , ...newMessages ] ,
433467 updatedAt : new Date ( ) . toISOString ( ) ,
434468 } ;
435469 } , false ) ;
470+
471+ if ( shouldTriggerGeneration ) {
472+ // Fire and forget - don't await, let it run in background
473+ chatService . createMessageGenerate ( { generationId : assistantMessage ! . generation ! . id } ) . catch ( ( error ) => {
474+ console . error ( "Error triggering image generation:" , error ) ;
475+ } ) ;
476+ }
436477 } else {
437478 // Fallback: revalidate if no messages returned
438479 await currentChatMutate ( ) ;
@@ -546,7 +587,7 @@ export const useChat = (initialChatId?: string, selectedProvider?: string, selec
546587 ...msg ,
547588 generation : {
548589 ...msg . generation ,
549- status : "pending " as const ,
590+ status : "generating " as const ,
550591 fileIds : null ,
551592 errorReason : null ,
552593 } ,
@@ -558,7 +599,15 @@ export const useChat = (initialChatId?: string, selectedProvider?: string, selec
558599 } , false ) ;
559600
560601 // Call the API
561- await chatService . regenerateMessage ( { messageId } ) ;
602+ const result = await chatService . regenerateMessage ( { messageId } ) ;
603+
604+ // Trigger image generation in browser (not blocked by CF Worker timeout)
605+ if ( result ?. generationId ) {
606+ // Fire and forget - don't await, let it run in background
607+ chatService . createMessageGenerate ( { generationId : result . generationId } ) . catch ( ( error ) => {
608+ console . error ( "Error triggering image generation:" , error ) ;
609+ } ) ;
610+ }
562611
563612 // The polling mechanism in ChatMessageItem will handle updating the UI
564613 } catch ( error ) {
0 commit comments