@@ -64,6 +64,8 @@ class ChatApp {
6464 private debugEventsList : DebugEvent [ ] = [ ] ;
6565 private isCreatingSession : boolean = false ;
6666 private isLoadingTools : boolean = false ;
67+ private isSendingMessage : boolean = false ;
68+ private isVerifyingSession : boolean = false ;
6769
6870 private apiBaseUrl = 'http://localhost:5555/api' ;
6971
@@ -96,16 +98,25 @@ class ChatApp {
9698 this . loadChatHistory ( ) ;
9799 this . setupEventListeners ( ) ;
98100
101+ console . log ( 'ChatApp initialized with' , this . sessions . length , 'sessions loaded.' ) ;
102+
99103 // Only create a new session if no sessions exist
100104 if ( this . sessions . length === 0 ) {
101105 await this . createNewSession ( ) ;
102106 } else {
103107 // Load the most recent session
104108 this . currentSession = this . sessions [ 0 ] ;
105109
106- // If the session doesn't have a backend sessionId, we'll create one when user sends a message
107- if ( ! this . currentSession . sessionId ) {
108- console . log ( 'Current session has no backend sessionId - will create when needed' ) ;
110+ console . log ( 'Loading existing session:' , this . currentSession . sessionId ) ;
111+ // If the session has a backend sessionId, verify it exists
112+ if ( this . currentSession . sessionId ) {
113+ this . isVerifyingSession = true ;
114+ this . updateSendButtonState ( ) ;
115+ this . showSessionVerificationLoading ( ) ;
116+ await this . verifyBackendSession ( this . currentSession . sessionId ) ;
117+ this . isVerifyingSession = false ;
118+ this . updateSendButtonState ( ) ;
119+ this . hideSessionVerificationLoading ( ) ;
109120 }
110121
111122 await this . loadSession ( this . currentSession . id ) ;
@@ -169,7 +180,19 @@ class ChatApp {
169180 private updateSendButtonState ( ) : void {
170181 const hasContent = this . messageInput . value . trim ( ) . length > 0 ;
171182 const hasActiveSession = this . currentSession !== null ;
172- this . sendBtn . disabled = ! hasContent || ! hasActiveSession ;
183+ const isLoading = this . isLoadingTools || this . isSendingMessage || this . isCreatingSession || this . isVerifyingSession ;
184+
185+ this . sendBtn . disabled = ! hasContent || ! hasActiveSession || isLoading ;
186+ this . messageInput . disabled = isLoading ;
187+
188+ // Add visual feedback for loading state
189+ if ( isLoading ) {
190+ this . messageInput . placeholder = 'Please wait...' ;
191+ this . messageInput . classList . add ( 'loading' ) ;
192+ } else {
193+ this . messageInput . placeholder = 'Type your message...' ;
194+ this . messageInput . classList . remove ( 'loading' ) ;
195+ }
173196 }
174197
175198 private updateNewChatButtonState ( ) : void {
@@ -202,12 +225,15 @@ class ChatApp {
202225 const content = this . messageInput . value . trim ( ) ;
203226 if ( ! content || ! this . currentSession ) return ;
204227
205- // If the current session doesn't have a backend session ID, create a new session
228+ this . isSendingMessage = true ;
229+ this . updateSendButtonState ( ) ;
230+
231+ // If the current session doesn't have a backend session ID, create one
206232 if ( ! this . currentSession . sessionId ) {
207233 console . log ( 'Creating backend session for existing frontend session...' ) ;
208234 try {
209235 const response = await fetch ( `${ this . apiBaseUrl } /session/new` , {
210- method : 'POST ' ,
236+ method : 'GET ' ,
211237 headers : {
212238 'Content-Type' : 'application/json'
213239 }
@@ -222,11 +248,15 @@ class ChatApp {
222248 await this . loadTools ( ) ;
223249 } else {
224250 this . showError ( 'Failed to create backend session. Please try again.' ) ;
251+ this . isSendingMessage = false ;
252+ this . updateSendButtonState ( ) ;
225253 return ;
226254 }
227255 } catch ( error ) {
228256 console . error ( 'Failed to create backend session:' , error ) ;
229257 this . showError ( 'Failed to create backend session. Please try again.' ) ;
258+ this . isSendingMessage = false ;
259+ this . updateSendButtonState ( ) ;
230260 return ;
231261 }
232262 }
@@ -273,6 +303,9 @@ class ChatApp {
273303 this . hideTypingIndicator ( ) ;
274304 this . showError ( 'Failed to get response. Please try again.' ) ;
275305 console . error ( 'API Error:' , error ) ;
306+ } finally {
307+ this . isSendingMessage = false ;
308+ this . updateSendButtonState ( ) ;
276309 }
277310 }
278311
@@ -428,14 +461,15 @@ class ChatApp {
428461
429462 this . isCreatingSession = true ;
430463 this . updateNewChatButtonState ( ) ;
464+ this . updateSendButtonState ( ) ;
431465
432466 // Show loading state immediately
433467 this . clearMessages ( ) ;
434468
435469 try {
436470 // Create a new backend session
437471 const response = await fetch ( `${ this . apiBaseUrl } /session/new` , {
438- method : 'POST ' ,
472+ method : 'GET ' ,
439473 headers : {
440474 'Content-Type' : 'application/json'
441475 }
@@ -478,6 +512,7 @@ class ChatApp {
478512 } finally {
479513 this . isCreatingSession = false ;
480514 this . updateNewChatButtonState ( ) ;
515+ this . updateSendButtonState ( ) ;
481516 // Clear the loading message and show the normal welcome message
482517 this . clearMessages ( ) ;
483518 }
@@ -716,6 +751,7 @@ class ChatApp {
716751
717752 this . isLoadingTools = true ;
718753 this . renderToolsLoading ( ) ;
754+ this . updateSendButtonState ( ) ;
719755
720756 const toolsUrl = `${ this . apiBaseUrl } /${ this . currentSession . sessionId } /tools` ;
721757 console . log ( `Loading tools from: ${ toolsUrl } ` ) ;
@@ -741,6 +777,7 @@ class ChatApp {
741777 } finally {
742778 this . isLoadingTools = false ;
743779 this . renderTools ( ) ;
780+ this . updateSendButtonState ( ) ;
744781 }
745782 }
746783
@@ -1258,9 +1295,9 @@ class ChatApp {
12581295 } ) ;
12591296 }
12601297
1298+ this . updateSendButtonState ( ) ;
12611299 this . messageInput . disabled = true ;
12621300 this . messageInput . placeholder = "Create a new chat to start messaging..." ;
1263- this . updateSendButtonState ( ) ;
12641301
12651302 // Clear tools and debug state for no active session
12661303 this . tools = [ ] ;
@@ -1279,6 +1316,59 @@ class ChatApp {
12791316 this . messageInput . placeholder = "Message AI Agent..." ;
12801317 this . updateSendButtonState ( ) ;
12811318 }
1319+
1320+ private async verifyBackendSession ( sessionId : string ) : Promise < void > {
1321+ try {
1322+ console . log ( `Verifying backend session: ${ sessionId } ` ) ;
1323+ const response = await fetch ( `${ this . apiBaseUrl } /session/${ sessionId } ` , {
1324+ method : 'GET' ,
1325+ headers : {
1326+ 'Content-Type' : 'application/json'
1327+ }
1328+ } ) ;
1329+
1330+ if ( response . ok ) {
1331+ const sessionData = await response . json ( ) ;
1332+ console . log ( `Backend session ${ sessionId } verified and reinitialized` ) ;
1333+ // Session exists and is reinitialized
1334+ return ;
1335+ } else {
1336+ console . warn ( `Backend session ${ sessionId } not found, will create new session when needed` ) ;
1337+ // Clear the sessionId since backend session doesn't exist
1338+ if ( this . currentSession ) {
1339+ this . currentSession . sessionId = undefined ;
1340+ this . saveChatHistory ( ) ;
1341+ }
1342+ }
1343+ } catch ( error ) {
1344+ console . error ( `Failed to verify backend session ${ sessionId } :` , error ) ;
1345+ // Clear the sessionId since we can't verify
1346+ if ( this . currentSession ) {
1347+ this . currentSession . sessionId = undefined ;
1348+ this . saveChatHistory ( ) ;
1349+ }
1350+ }
1351+ }
1352+
1353+ private showSessionVerificationLoading ( ) : void {
1354+ this . messagesContainer . innerHTML = `
1355+ <div class="welcome-message">
1356+ <div class="session-verification-loading">
1357+ <div class="loading-spinner"></div>
1358+ <h1>Verifying session...</h1>
1359+ <p>Checking if your previous session is still available.</p>
1360+ </div>
1361+ </div>
1362+ ` ;
1363+ }
1364+
1365+ private hideSessionVerificationLoading ( ) : void {
1366+ // Clear the loading message - it will be replaced by the actual session content
1367+ const loadingEl = this . messagesContainer . querySelector ( '.session-verification-loading' ) ;
1368+ if ( loadingEl ) {
1369+ loadingEl . parentElement ?. remove ( ) ;
1370+ }
1371+ }
12821372}
12831373
12841374document . addEventListener ( 'DOMContentLoaded' , ( ) => {
0 commit comments