@@ -57,6 +57,7 @@ class ChatStore {
5757	maxContextError  =  $state < {  message : string ;  estimatedTokens : number ;  maxContext : number  }  |  null > ( 
5858		null 
5959	) ; 
60+ 	titleUpdateConfirmationCallback ?: ( currentTitle : string ,  newTitle : string )  =>  Promise < boolean > ; 
6061
6162	constructor ( )  { 
6263		if  ( browser )  { 
@@ -622,9 +623,22 @@ class ChatStore {
622623				return ; 
623624			} 
624625
626+ 			const  allMessages  =  await  DatabaseStore . getConversationMessages ( this . activeConversation . id ) ; 
627+ 			const  rootMessage  =  allMessages . find ( ( m )  =>  m . type  ===  'root'  &&  m . parent  ===  null ) ; 
628+ 			const  isFirstUserMessage  =  rootMessage  &&  messageToUpdate . parent  ===  rootMessage . id  &&  messageToUpdate . role  ===  'user' ; 
629+ 
625630			this . updateMessageAtIndex ( messageIndex ,  {  content : newContent  } ) ; 
626631			await  DatabaseStore . updateMessage ( messageId ,  {  content : newContent  } ) ; 
627632
633+ 			// If this is the first user message, update the conversation title with confirmation if needed 
634+ 			if  ( isFirstUserMessage  &&  newContent . trim ( ) )  { 
635+ 				await  this . updateConversationTitleWithConfirmation ( 
636+ 					this . activeConversation . id ,  
637+ 					newContent . trim ( ) , 
638+ 					this . titleUpdateConfirmationCallback 
639+ 				) ; 
640+ 			} 
641+ 
628642			const  messagesToRemove  =  this . activeMessages . slice ( messageIndex  +  1 ) ; 
629643			for  ( const  message  of  messagesToRemove )  { 
630644				await  DatabaseStore . deleteMessage ( message . id ) ; 
@@ -725,6 +739,7 @@ class ChatStore {
725739		} 
726740	} 
727741
742+ 
728743	/** 
729744	 * Updates the name of a conversation 
730745	 * @param  convId - The conversation ID to update 
@@ -748,6 +763,45 @@ class ChatStore {
748763		} 
749764	} 
750765
766+ 	/** 
767+ 	 * Sets the callback function for title update confirmations 
768+ 	 * @param  callback - Function to call when confirmation is needed 
769+ 	 */ 
770+ 	setTitleUpdateConfirmationCallback ( 
771+ 		callback : ( currentTitle : string ,  newTitle : string )  =>  Promise < boolean > 
772+ 	) : void { 
773+ 		this . titleUpdateConfirmationCallback  =  callback ; 
774+ 	} 
775+ 
776+ 	/** 
777+ 	 * Updates conversation title with confirmation dialog 
778+ 	 * @param  convId - The conversation ID to update 
779+ 	 * @param  newTitle - The new title content 
780+ 	 * @param  onConfirmationNeeded - Callback when user confirmation is needed 
781+ 	 * @returns  Promise<boolean> - True if title was updated, false if cancelled 
782+ 	 */ 
783+ 	async  updateConversationTitleWithConfirmation ( 
784+ 		convId : string ,  
785+ 		newTitle : string , 
786+ 		onConfirmationNeeded ?: ( currentTitle : string ,  newTitle : string )  =>  Promise < boolean > 
787+ 	) : Promise < boolean >  { 
788+ 		try  { 
789+ 			if  ( onConfirmationNeeded )  { 
790+ 				const  conversation  =  await  DatabaseStore . getConversation ( convId ) ; 
791+ 				if  ( ! conversation )  return  false ; 
792+ 
793+ 				const  shouldUpdate  =  await  onConfirmationNeeded ( conversation . name ,  newTitle ) ; 
794+ 				if  ( ! shouldUpdate )  return  false ; 
795+ 			} 
796+ 
797+ 			await  this . updateConversationName ( convId ,  newTitle ) ; 
798+ 			return  true ; 
799+ 		}  catch  ( error )  { 
800+ 			console . error ( 'Failed to update conversation title with confirmation:' ,  error ) ; 
801+ 			return  false ; 
802+ 		} 
803+ 	} 
804+ 
751805	/** 
752806	 * Deletes a conversation and all its messages 
753807	 * @param  convId - The conversation ID to delete 
@@ -915,9 +969,40 @@ class ChatStore {
915969	async  navigateToSibling ( siblingId : string ) : Promise < void >  { 
916970		if  ( ! this . activeConversation )  return ; 
917971
972+ 		// Get the current first user message before navigation 
973+ 		const  allMessages  =  await  DatabaseStore . getConversationMessages ( this . activeConversation . id ) ; 
974+ 		const  rootMessage  =  allMessages . find ( ( m )  =>  m . type  ===  'root'  &&  m . parent  ===  null ) ; 
975+ 		const  currentFirstUserMessage  =  this . activeMessages . find ( 
976+ 			( m )  =>  m . role  ===  'user'  &&  m . parent  ===  rootMessage ?. id 
977+ 		) ; 
978+ 
918979		await  DatabaseStore . updateCurrentNode ( this . activeConversation . id ,  siblingId ) ; 
919980		this . activeConversation . currNode  =  siblingId ; 
920981		await  this . refreshActiveMessages ( ) ; 
982+ 
983+ 		// Only show title dialog if we're navigating between different first user message siblings 
984+ 		if  ( rootMessage  &&  this . activeMessages . length  >  0 )  { 
985+ 			// Find the first user message in the new active path 
986+ 			const  newFirstUserMessage  =  this . activeMessages . find ( 
987+ 				( m )  =>  m . role  ===  'user'  &&  m . parent  ===  rootMessage . id 
988+ 			) ; 
989+ 			
990+ 			// Only show dialog if: 
991+ 			// 1. We have a new first user message 
992+ 			// 2. It's different from the previous one (different ID or content) 
993+ 			// 3. The new message has content 
994+ 			if  ( newFirstUserMessage  &&  
995+ 				newFirstUserMessage . content . trim ( )  && 
996+ 				( ! currentFirstUserMessage  ||  
997+ 				 newFirstUserMessage . id  !==  currentFirstUserMessage . id  || 
998+ 				 newFirstUserMessage . content . trim ( )  !==  currentFirstUserMessage . content . trim ( ) ) )  { 
999+ 				await  this . updateConversationTitleWithConfirmation ( 
1000+ 					this . activeConversation . id ,  
1001+ 					newFirstUserMessage . content . trim ( ) , 
1002+ 					this . titleUpdateConfirmationCallback 
1003+ 				) ; 
1004+ 			} 
1005+ 		} 
9211006	} 
9221007	/** 
9231008	 * Edits a message by creating a new branch with the edited content 
@@ -940,10 +1025,15 @@ class ChatStore {
9401025				return ; 
9411026			} 
9421027
1028+ 			// Check if this is the first user message in the conversation 
1029+ 			// First user message is one that has the root message as its parent 
1030+ 			const  allMessages  =  await  DatabaseStore . getConversationMessages ( this . activeConversation . id ) ; 
1031+ 			const  rootMessage  =  allMessages . find ( ( m )  =>  m . type  ===  'root'  &&  m . parent  ===  null ) ; 
1032+ 			const  isFirstUserMessage  =  rootMessage  &&  messageToEdit . parent  ===  rootMessage . id  &&  messageToEdit . role  ===  'user' ; 
1033+ 
9431034			let  parentId  =  messageToEdit . parent ; 
9441035
9451036			if  ( parentId  ===  undefined  ||  parentId  ===  null )  { 
946- 				const  allMessages  =  await  DatabaseStore . getConversationMessages ( this . activeConversation . id ) ; 
9471037				const  rootMessage  =  allMessages . find ( ( m )  =>  m . type  ===  'root'  &&  m . parent  ===  null ) ; 
9481038				if  ( rootMessage )  { 
9491039					parentId  =  rootMessage . id ; 
@@ -970,6 +1060,16 @@ class ChatStore {
9701060			await  DatabaseStore . updateCurrentNode ( this . activeConversation . id ,  newMessage . id ) ; 
9711061			this . activeConversation . currNode  =  newMessage . id ; 
9721062			this . updateConversationTimestamp ( ) ; 
1063+ 
1064+ 			// If this is the first user message, update the conversation title with confirmation if needed 
1065+ 			if  ( isFirstUserMessage  &&  newContent . trim ( ) )  { 
1066+ 				await  this . updateConversationTitleWithConfirmation ( 
1067+ 					this . activeConversation . id ,  
1068+ 					newContent . trim ( ) , 
1069+ 					this . titleUpdateConfirmationCallback 
1070+ 				) ; 
1071+ 			} 
1072+ 
9731073			await  this . refreshActiveMessages ( ) ; 
9741074
9751075			if  ( messageToEdit . role  ===  'user' )  { 
@@ -1118,6 +1218,7 @@ export const regenerateMessageWithBranching =
11181218export  const  deleteMessage  =  chatStore . deleteMessage . bind ( chatStore ) ; 
11191219export  const  getDeletionInfo  =  chatStore . getDeletionInfo . bind ( chatStore ) ; 
11201220export  const  updateConversationName  =  chatStore . updateConversationName . bind ( chatStore ) ; 
1221+ export  const  setTitleUpdateConfirmationCallback  =  chatStore . setTitleUpdateConfirmationCallback . bind ( chatStore ) ; 
11211222
11221223export  function  stopGeneration ( )  { 
11231224	chatStore . stopGeneration ( ) ; 
0 commit comments