@@ -67,6 +67,7 @@ export interface InboundChatApi {
6767 listConversations ( params : ListConversationsResult ) : void
6868 conversationClicked ( params : ConversationClickResult ) : void
6969 getSerializedChat ( requestId : string , params : GetSerializedChatParams ) : void
70+ createTabId ( openTab ?: boolean ) : string | undefined
7071}
7172
7273type ContextCommandGroups = MynahUIDataModel [ 'contextCommands' ]
@@ -83,6 +84,8 @@ const getTabPairProgrammingMode = (mynahUi: MynahUI, tabId: string) => {
8384 return promptInputOptions . find ( item => item . id === 'pair-programmer-mode' ) ?. value === 'true'
8485}
8586
87+ const openTabKey = 'openTab'
88+
8689export const handlePromptInputChange = ( mynahUi : MynahUI , tabId : string , optionsValues : Record < string , string > ) => {
8790 const promptTypeValue = optionsValues [ 'pair-programmer-mode' ]
8891
@@ -170,7 +173,6 @@ export const createMynahUi = (
170173 customChatClientAdapter ?: ChatClientAdapter ,
171174 featureConfig ?: Map < string , any >
172175) : [ MynahUI , InboundChatApi ] => {
173- const initialTabId = TabFactory . generateUniqueId ( )
174176 let disclaimerCardActive = ! disclaimerAcknowledged
175177 let programmingModeCardActive = ! pairProgrammingCardAcknowledged
176178 let contextCommandGroups : ContextCommandGroups | undefined
@@ -227,7 +229,6 @@ export const createMynahUi = (
227229 } ,
228230 onReady : ( ) => {
229231 messager . onUiReady ( )
230- messager . onTabAdd ( initialTabId )
231232 } ,
232233 onFileClick : ( tabId , filePath , deleted , messageId , eventId , fileDetails ) => {
233234 messager . onFileClick ( { tabId, filePath, messageId, fullPath : fileDetails ?. data ?. [ 'fullPath' ] } )
@@ -250,6 +251,16 @@ export const createMynahUi = (
250251 ] ,
251252 ...( disclaimerCardActive ? { promptInputStickyCard : disclaimerCard } : { } ) ,
252253 }
254+
255+ const tabStore = mynahUi . getTabData ( tabId ) . getStore ( )
256+
257+ // Tabs can be opened through different methods, including server-initiated 'openTab' requests.
258+ // The 'openTab' request is specifically used for loading historical chat sessions with pre-existing messages.
259+ // We check if tabMetadata.openTabKey exists - if it does and is set to true, we skip showing welcome messages
260+ // since this indicates we're loading a previous chat session rather than starting a new one.
261+ if ( ! tabStore ?. tabMetadata || ! tabStore . tabMetadata . openTabKey ) {
262+ defaultTabConfig . chatItems = tabFactory . getChatItems ( true , programmingModeCardActive , [ ] )
263+ }
253264 mynahUi . updateStore ( tabId , defaultTabConfig )
254265 messager . onTabAdd ( tabId )
255266 } ,
@@ -446,7 +457,7 @@ export const createMynahUi = (
446457 // Update the tab defaults to hide the programmer mode card for new tabs
447458 mynahUi . updateTabDefaults ( {
448459 store : {
449- chatItems : tabFactory . createTab ( true , disclaimerCardActive , false ) . chatItems ,
460+ chatItems : tabFactory . getChatItems ( true , false ) ,
450461 } ,
451462 } )
452463 }
@@ -464,14 +475,9 @@ export const createMynahUi = (
464475 }
465476
466477 const mynahUiProps : MynahUIProps = {
467- tabs : {
468- [ initialTabId ] : {
469- isSelected : true ,
470- store : tabFactory . createTab ( true , disclaimerCardActive , programmingModeCardActive ) ,
471- } ,
472- } ,
478+ tabs : { } ,
473479 defaults : {
474- store : tabFactory . createTab ( true , false , programmingModeCardActive ) ,
480+ store : tabFactory . createTab ( false ) ,
475481 } ,
476482 config : {
477483 maxTabs : 10 ,
@@ -495,11 +501,14 @@ export const createMynahUi = (
495501 return tabId ? mynahUi . getAllTabs ( ) [ tabId ] ?. store : undefined
496502 }
497503
498- const createTabId = ( needWelcomeMessages : boolean = false , chatMessages ?: ChatMessage [ ] ) => {
499- const tabId = mynahUi . updateStore (
500- '' ,
501- tabFactory . createTab ( needWelcomeMessages , disclaimerCardActive , programmingModeCardActive , chatMessages )
502- )
504+ // The 'openTab' parameter indicates whether this tab creation is initiated by 'openTab' server request
505+ // to restore a previous chat session (true) or if it's a new client-side tab creation (false/undefined).
506+ // This distinction helps maintain consistent tab behavior between fresh conversations and restored sessions.
507+ const createTabId = ( openTab ?: boolean ) => {
508+ const tabId = mynahUi . updateStore ( '' , {
509+ ...tabFactory . createTab ( disclaimerCardActive ) ,
510+ tabMetadata : { openTabKey : openTab ? true : false } ,
511+ } )
503512 if ( tabId === undefined ) {
504513 mynahUi . notify ( {
505514 content : uiComponentsTexts . noMoreTabsTooltip ,
@@ -789,8 +798,11 @@ ${params.message}`,
789798 messager . onOpenTab ( requestId , { tabId : params . tabId } )
790799 } else {
791800 const messages = params . newTabOptions ?. data ?. messages
792- const tabId = createTabId ( messages ? false : true , messages )
801+ const tabId = createTabId ( true )
793802 if ( tabId ) {
803+ mynahUi . updateStore ( tabId , {
804+ chatItems : tabFactory . getChatItems ( messages ? false : true , programmingModeCardActive , messages ) ,
805+ } )
794806 messager . onOpenTab ( requestId , { tabId } )
795807 } else {
796808 messager . onOpenTab ( requestId , {
@@ -902,6 +914,7 @@ ${params.message}`,
902914 listConversations : listConversations ,
903915 conversationClicked : conversationClicked ,
904916 getSerializedChat : getSerializedChat ,
917+ createTabId : createTabId ,
905918 }
906919
907920 return [ mynahUi , api ]
0 commit comments