@@ -224,74 +224,75 @@ export default function ChatbotInterface() {
224224 e . preventDefault ( ) ;
225225 if ( ! input . trim ( ) ) return ;
226226
227- // Add user message
228227 const userMessage = { role : 'user' , content : input } ;
229228 setMessages ( msgs => [ ...msgs , userMessage ] ) ;
230229 setInput ( '' ) ;
231230 setIsLoading ( true ) ;
232- setShowSampleQuestions ( true ) ;
233231
234232 try {
235- // Try streaming first
236- await handleStreamedChat ( input , messages ) ;
237- } catch ( error ) {
238- console . error ( 'Streaming failed, falling back to regular chat:' , error ) ;
239-
240- // Fallback to non-streaming implementation
241- try {
242- const response = await fetch ( 'https://mongodb-rag-docs-backend.vercel.app/api/chat' , {
243- method : 'POST' ,
244- headers : {
245- 'Content-Type' : 'application/json' ,
246- 'Accept' : 'application/json' // Request JSON response
247- } ,
248- body : JSON . stringify ( {
249- query : input ,
250- sessionId : sessionId ,
251- history : ! sessionId ? messages : undefined ,
252- stream : false
253- } )
254- } ) ;
233+ const response = await fetch ( 'https://mongodb-rag-docs-backend.vercel.app/api/chat' , {
234+ method : 'POST' ,
235+ headers : { 'Content-Type' : 'application/json' } ,
236+ body : JSON . stringify ( {
237+ query : input ,
238+ sessionId : sessionId ,
239+ history : ! sessionId ? messages : undefined
240+ } ) ,
241+ // Add request timeout
242+ signal : AbortSignal . timeout ( 45000 ) // 45 second timeout
243+ } ) ;
255244
256- if ( ! response . ok ) {
257- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
258- }
245+ if ( ! response . ok ) {
246+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
247+ }
259248
260- const text = await response . text ( ) ;
261- const data = safeJsonParse ( text ) ;
262-
263- if ( ! data ) {
264- throw new Error ( 'Invalid response format' ) ;
265- }
249+ const data = await response . json ( ) ;
250+
251+ if ( data . error ) {
252+ throw new Error ( data . error ) ;
253+ }
266254
267- if ( data . sessionId && ! sessionId ) {
268- setSessionId ( data . sessionId ) ;
255+ setMessages ( msgs => [
256+ ...msgs ,
257+ {
258+ role : 'assistant' ,
259+ content : data . answer ,
260+ sources : data . sources
269261 }
270-
271- setMessages ( msgs => [
272- ...msgs ,
273- {
274- role : 'assistant' ,
275- content : data . answer || 'Sorry, I received an invalid response.' ,
276- sources : data . sources
277- }
278- ] ) ;
279- } catch ( error ) {
280- console . error ( 'Chat error:' , error ) ;
281- setMessages ( msgs => [
282- ...msgs ,
283- {
284- role : 'assistant' ,
285- content : `Sorry, I encountered an error: ${ error . message } `
286- }
287- ] ) ;
288- }
262+ ] ) ;
263+ } catch ( error ) {
264+ handleError ( error ) ;
289265 } finally {
290266 setIsLoading ( false ) ;
291- setCurrentStreamedMessage ( '' ) ;
292267 }
293268 } ;
269+
270+ const handleError = ( error , fallback = true ) => {
271+ console . error ( 'Chat error:' , error ) ;
272+
273+ let errorMessage = 'Sorry, I encountered an error. Please try again later.' ;
274+
275+ if ( error . response ?. status === 504 || error . message ?. includes ( 'timed out' ) ) {
276+ errorMessage = 'The search is taking longer than expected. You might try:' +
277+ '\n1. Rephrasing your question to be more specific' +
278+ '\n2. Breaking it into smaller parts' +
279+ '\n3. Trying again in a moment' ;
280+ }
281+
282+ setMessages ( msgs => [
283+ ...msgs ,
284+ {
285+ role : 'assistant' ,
286+ content : errorMessage
287+ }
288+ ] ) ;
294289
290+ if ( fallback && ! error . message ?. includes ( 'timed out' ) ) {
291+ // Optionally try fallback handling for non-timeout errors
292+ handleFallbackChat ( input , messages ) ;
293+ }
294+ } ;
295+
295296 const clearChat = ( ) => {
296297 // Reset the chat
297298 setMessages ( [
0 commit comments