@@ -416,8 +416,9 @@ <h5>OpenAI</h5>
416416< script src ="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js "> </ script >
417417< script src ="
https://cdn.jsdelivr.net/npm/[email protected] /dist/js/bootstrap.bundle.min.js "
> </ script > 418418< script >
419+
420+ // Animate cards when page is loaded (only design, no core functionality)
419421 document . addEventListener ( 'DOMContentLoaded' , ( ) => {
420- // Animate cards
421422 anime ( {
422423 targets : '.endpoint-card' ,
423424 translateY : [ 50 , 0 ] ,
@@ -427,58 +428,47 @@ <h5>OpenAI</h5>
427428 } ) ;
428429 } ) ;
429430
431+
430432 // Get the modal instance
431433 const modalElement = document . getElementById ( "exampleModal" ) ;
432434 const modalInstance = new bootstrap . Modal ( modalElement ) ;
433435 const modalBody = modalElement . querySelector ( '.modal-body' ) ;
434436 const modalTitle = modalElement . querySelector ( '.modal-title' ) ;
435437 const modalHeader = modalElement . querySelector ( '.modal-header' ) ;
436438
439+
437440 document . querySelectorAll ( '.endpoint' ) . forEach ( link => {
438441 link . addEventListener ( 'click' , async ( event ) => {
439- event . preventDefault ( ) ; // Prevent the default link behavior
442+ const spinner = document . getElementById ( "loading-spinner" ) ;
443+ spinner . classList . remove ( "d-none" ) ;
444+
445+ event . preventDefault ( ) ;
446+ const endpoint = event . target . parentElement . href ;
440447
441- const endpoint = event . target . parentElement . href ; // Get the endpoint
442448 try {
443- if ( endpoint . includes ( 'streamChatCompletion' ) ) {
444- await handleStreamingResponse ( endpoint , event . target . innerHTML ) ;
449+ // Make initial request and check content type
450+ const response = await fetch ( endpoint ) ;
451+ if ( ! response . ok ) throw new Error ( "Error occurred with status code: " + response . status ) ;
452+
453+ const contentType = response . headers . get ( 'content-type' ) ;
454+
455+ if ( contentType && contentType . includes ( 'text/event-stream' ) ) {
456+ await handleStreamingResponse ( response , event . target . innerHTML ) ;
445457 } else {
446- const data = await fetchData ( endpoint ) ; // Fetch the JSON data from the backend
447- updateDisplay ( data , event . target . innerHTML , endpoint , false ) ; // Display the data in the modal
458+ const data = await response . json ( ) ;
459+ updateDisplay ( data , event . target . innerHTML , endpoint , false ) ;
448460 }
449461 } catch ( error ) {
450462 updateDisplay ( error . message , event . target . innerHTML , endpoint , true ) ;
463+ } finally {
464+ // Hide the spinner
465+ spinner . classList . add ( "d-none" ) ;
451466 }
452467 } ) ;
453468 } ) ;
454469
455- async function fetchData ( endpoint ) {
456- const spinner = document . getElementById ( "loading-spinner" ) ; // Get spinner element
457-
458- try {
459- // Show the spinner
460- spinner . classList . remove ( "d-none" ) ;
461-
462- const response = await fetch ( endpoint ) ;
463- if ( ! response . ok ) throw new Error ( "Error occurred with status code: " + response . status ) ;
464-
465- return await response . json ( ) ;
466- } catch ( error ) {
467- console . error ( "Error fetching data:" , error ) ;
468- throw error ;
469- } finally {
470- // Hide the spinner
471- spinner . classList . add ( "d-none" ) ;
472- }
473- }
474-
475- async function handleStreamingResponse ( endpoint , endpointName ) {
476- const spinner = document . getElementById ( "loading-spinner" ) ;
477-
470+ async function handleStreamingResponse ( response , endpointName ) {
478471 try {
479- // Show the spinner
480- spinner . classList . remove ( "d-none" ) ;
481-
482472 // Set the modal title
483473 modalTitle . innerHTML = `<code>${ endpointName } </code>` ;
484474 const existingToggleGroup = modalHeader . querySelector ( '.btn-group' ) ;
@@ -494,13 +484,6 @@ <h5>OpenAI</h5>
494484 ` ;
495485 const streamContainer = modalBody . querySelector ( '.stream-container' ) ;
496486
497- // Fetch the streaming response
498- const response = await fetch ( endpoint ) ;
499-
500- if ( ! response . ok ) {
501- throw new Error ( `Error occurred with status code: ${ response . status } ` ) ;
502- }
503-
504487 // Check if the response is a ReadableStream
505488 if ( response . body ) {
506489 const reader = response . body . getReader ( ) ;
@@ -528,9 +511,6 @@ <h5>OpenAI</h5>
528511 }
529512 } catch ( error ) {
530513 updateDisplay ( error . message , endpointName , endpoint , true ) ;
531- } finally {
532- // Hide the spinner
533- spinner . classList . add ( "d-none" ) ;
534514 }
535515 }
536516
0 commit comments