@@ -11,6 +11,8 @@ class PlaygroundApp {
1111 this . search = null ; // Will be initialized with PlaygroundSearch
1212 this . isConnected = false ;
1313 this . isResearching = false ;
14+ this . sharedListenerSetup = false ;
15+ this . lastConnectionMessageTime = 0 ;
1416
1517 this . init ( ) ;
1618 }
@@ -136,7 +138,12 @@ class PlaygroundApp {
136138 this . isConnected = true ;
137139 this . updateStatus ( 'aiStatus' , `Connected (${ currentProvider . provider } )` , 'success' ) ;
138140 this . updateConnectButtonText ( ) ;
139- this . addChatMessage ( 'system' , `🤖 Using existing AI connection (${ currentProvider . provider } ). Ready to help!` ) ;
141+
142+ // Only add message if we don't already have a recent connection message
143+ if ( ! this . hasRecentConnectionMessage ( ) ) {
144+ this . addChatMessage ( 'system' , `🤖 Using existing AI connection (${ currentProvider . provider } ). Ready to help!` ) ;
145+ this . lastConnectionMessageTime = Date . now ( ) ;
146+ }
140147 }
141148 } else {
142149 console . log ( '🔍 No active shared connection found after restoration check' ) ;
@@ -170,7 +177,7 @@ class PlaygroundApp {
170177 }
171178
172179 setupSharedConnectionListener ( ) {
173- if ( window . SharedAIConnection ) {
180+ if ( window . SharedAIConnection && ! this . sharedListenerSetup ) {
174181 // Listen for connection changes from other pages
175182 window . SharedAIConnection . addConnectionListener ( ( event , data ) => {
176183 console . log ( '🔄 Playground received shared connection event:' , event , data ) ;
@@ -183,7 +190,12 @@ class PlaygroundApp {
183190 this . isConnected = true ;
184191 this . updateStatus ( 'aiStatus' , `Connected (${ providerInfo . provider } )` , 'success' ) ;
185192 this . updateConnectButtonText ( ) ;
186- this . addChatMessage ( 'system' , `🤖 AI connected via ${ providerInfo . provider } . Ready to help!` ) ;
193+
194+ // Only add message if we don't already have a recent connection message
195+ if ( ! this . hasRecentConnectionMessage ( ) ) {
196+ this . addChatMessage ( 'system' , `🤖 AI connected via ${ providerInfo . provider } . Ready to help!` ) ;
197+ this . lastConnectionMessageTime = Date . now ( ) ;
198+ }
187199 } else if ( event === 'disconnected' ) {
188200 this . isConnected = false ;
189201 this . updateStatus ( 'aiStatus' , 'Ready to Connect' , 'info' ) ;
@@ -196,6 +208,8 @@ class PlaygroundApp {
196208 this . addChatMessage ( 'system' , `❌ AI connection error: ${ data . message } ` ) ;
197209 }
198210 } ) ;
211+
212+ this . sharedListenerSetup = true ;
199213 }
200214 }
201215
@@ -229,7 +243,12 @@ class PlaygroundApp {
229243 this . isConnected = true ;
230244 this . updateStatus ( 'aiStatus' , `Connected (${ currentProvider . provider } )` , 'success' ) ;
231245 this . updateConnectButtonText ( ) ;
232- this . addChatMessage ( 'system' , `🤖 Using existing AI connection (${ currentProvider . provider } ). Ready to help!` ) ;
246+
247+ // Only add message if we don't already have a recent connection message
248+ if ( ! this . hasRecentConnectionMessage ( ) ) {
249+ this . addChatMessage ( 'system' , `🤖 Using existing AI connection (${ currentProvider . provider } ). Ready to help!` ) ;
250+ this . lastConnectionMessageTime = Date . now ( ) ;
251+ }
233252
234253 connectButton . disabled = false ;
235254 return ;
@@ -250,7 +269,12 @@ class PlaygroundApp {
250269 this . isConnected = true ;
251270 this . updateStatus ( 'aiStatus' , `Connected (${ window . SharedAIConnection . currentProvider } )` , 'success' ) ;
252271 this . updateConnectButtonText ( ) ;
253- this . addChatMessage ( 'system' , `🤖 AI Assistant connected using ${ window . SharedAIConnection . currentProvider } . Ready to help!` ) ;
272+
273+ // Only add message if we don't already have a recent connection message
274+ if ( ! this . hasRecentConnectionMessage ( ) ) {
275+ this . addChatMessage ( 'system' , `🤖 AI Assistant connected using ${ window . SharedAIConnection . currentProvider } . Ready to help!` ) ;
276+ this . lastConnectionMessageTime = Date . now ( ) ;
277+ }
254278
255279 connectButton . disabled = false ;
256280 } else if ( event === 'error' ) {
@@ -323,7 +347,12 @@ class PlaygroundApp {
323347 this . isConnected = true ;
324348 this . updateStatus ( 'aiStatus' , `Connected (${ status . provider } )` , 'success' ) ;
325349 this . updateConnectButtonText ( ) ;
326- this . addChatMessage ( 'system' , `🤖 AI Assistant connected using ${ status . provider } . Ready to help!` ) ;
350+
351+ // Only add message if we don't already have a recent connection message
352+ if ( ! this . hasRecentConnectionMessage ( ) ) {
353+ this . addChatMessage ( 'system' , `🤖 AI Assistant connected using ${ status . provider } . Ready to help!` ) ;
354+ this . lastConnectionMessageTime = Date . now ( ) ;
355+ }
327356 } else if ( status . error ) {
328357 this . isConnected = false ;
329358 this . updateStatus ( 'aiStatus' , 'Connection Issue' , 'error' ) ;
@@ -491,19 +520,43 @@ Based on the research, here are the key recommendations:
491520 input . value = '' ;
492521 input . style . height = 'auto' ;
493522
494- // Check if AI is connected
495- if ( ! this . isConnected || ! this . aiAssistant ) {
523+ // Enhanced AI connection validation
524+ let aiAssistant = this . aiAssistant ;
525+
526+ // If local AI assistant is null, try to get from shared connection
527+ if ( ! aiAssistant && window . SharedAIConnection && window . SharedAIConnection . isAIConnected ( ) ) {
528+ console . log ( '🔍 Local AI assistant is null, trying shared connection...' ) ;
529+ aiAssistant = window . SharedAIConnection . getAIAssistant ( ) ;
530+ if ( aiAssistant ) {
531+ // Update our local reference
532+ this . aiAssistant = aiAssistant ;
533+ this . aiAssistant . onStatusChange = ( status ) => this . handleAIStatusChange ( status ) ;
534+ this . isConnected = true ;
535+ console . log ( '✅ Successfully retrieved AI assistant from shared connection' ) ;
536+ }
537+ }
538+
539+ // Final validation - ensure we have both connection state and valid assistant
540+ if ( ! this . isConnected || ! aiAssistant ) {
496541 this . addChatMessage ( 'system' , '⚠️ Please connect AI assistant first' ) ;
542+ console . error ( '❌ Chat blocked: isConnected =' , this . isConnected , ', aiAssistant =' , ! ! aiAssistant ) ;
497543 return ;
498544 }
499545
546+ // Additional validation for AI assistant object
547+ if ( ! aiAssistant . generateContent || typeof aiAssistant . generateContent !== 'function' ) {
548+ this . addChatMessage ( 'system' , '❌ AI assistant is not properly initialized. Please reconnect.' ) ;
549+ console . error ( '❌ AI assistant missing generateContent method:' , aiAssistant ) ;
550+ return ;
551+ }
552+
500553 try {
501554 // Check for document-related queries (RAG) using search module
502555 let response ;
503556 if ( this . search && this . search . isDocumentQuery ( message ) ) {
504557 response = await this . search . handleRAGQuery ( message ) ;
505558 } else {
506- response = await this . aiAssistant . generateContent ( message , 'general' ) ;
559+ response = await aiAssistant . generateContent ( message , 'general' ) ;
507560 }
508561
509562 this . addChatMessage ( 'assistant' , response ) ;
@@ -725,6 +778,11 @@ Based on the research, here are the key recommendations:
725778 return providerSelect ? providerSelect . value : 'AI' ;
726779 }
727780
781+ hasRecentConnectionMessage ( ) {
782+ // Check if we've added a connection message in the last 5 seconds
783+ return ( Date . now ( ) - this . lastConnectionMessageTime ) < 5000 ;
784+ }
785+
728786 clearSuccessMessages ( ) {
729787 // Clear any success banners
730788 const displayContent = document . getElementById ( 'displayContent' ) ;
0 commit comments