@@ -4,7 +4,6 @@ const input = document.getElementById("user-input");
44
55let threadId = null ;
66
7- // Openingsbericht bij het laden van de pagina
87window . onload = ( ) => {
98 const welkomstHTML = `
109 Welkom bij de <strong>AI Indicatiehulp</strong>!<br>
@@ -33,34 +32,49 @@ form.addEventListener("submit", async (e) => {
3332 appendMessage ( "user-message" , message ) ;
3433 input . value = "" ;
3534
35+ const msgElem = appendMessage ( "agent-message" , "" ) ;
36+
3637 try {
3738 const response = await fetch ( "https://chatproxy.azurewebsites.net/api/chatproxy" , {
3839 method : "POST" ,
3940 headers : { "Content-Type" : "application/json" } ,
4041 body : JSON . stringify ( { message, thread_id : threadId } )
4142 } ) ;
4243
43- if ( ! response . ok ) {
44- const errorText = await response . text ( ) ;
45- console . error ( "Responsetekst:" , errorText ) ;
46- throw new Error ( `Serverfout: ${ response . status } ` ) ;
44+ if ( ! response . ok || ! response . body ) {
45+ msgElem . textContent = "⚠️ Fout bij ophalen van antwoord." ;
46+ return ;
4747 }
4848
49- const data = await response . json ( ) ;
50- threadId = data . thread_id ;
51- streamMessage ( "agent-message" , data . reply ) ;
49+ const tid = response . headers . get ( "x-thread-id" ) ;
50+ if ( tid ) threadId = tid ;
51+
52+ const reader = response . body . getReader ( ) ;
53+ const decoder = new TextDecoder ( "utf-8" ) ;
54+ let fullText = "" ;
55+
56+ while ( true ) {
57+ const { value, done } = await reader . read ( ) ;
58+ if ( done ) break ;
59+
60+ const chunk = decoder . decode ( value , { stream : true } ) ;
61+ fullText += chunk ;
62+ msgElem . textContent = fullText ;
63+ chat . scrollTop = chat . scrollHeight ;
64+ }
5265 } catch ( err ) {
53- streamMessage ( "agent-message" , " Er ging iets mis.") ;
66+ msgElem . textContent = "⚠️ Er ging iets mis.";
5467 console . error ( "Fout in fetch:" , err ) ;
5568 }
5669} ) ;
5770
5871function appendMessage ( cssClass , text ) {
5972 const msg = document . createElement ( "div" ) ;
6073 msg . classList . add ( "message" , cssClass ) ;
61- msg . textContent = text ;
74+ msg . textContent = text || "" ;
6275 chat . appendChild ( msg ) ;
6376 chat . scrollTop = chat . scrollHeight ;
77+ return msg ;
6478}
6579
6680function appendFormattedMessage ( cssClass , htmlContent ) {
@@ -70,42 +84,3 @@ function appendFormattedMessage(cssClass, htmlContent) {
7084 chat . appendChild ( msg ) ;
7185 chat . scrollTop = chat . scrollHeight ;
7286}
73-
74- function streamMessage ( cssClass , text ) {
75- const msg = document . createElement ( "div" ) ;
76- msg . classList . add ( "message" , cssClass ) ;
77- chat . appendChild ( msg ) ;
78-
79- const lines = text . split ( "\n" ) . filter ( line => line . trim ( ) !== "" ) ;
80-
81- const isNumberedList = lines . length > 1 && lines . every ( line => / ^ \d + \. \s + / . test ( line . trim ( ) ) ) ;
82- const isBulletedList = lines . length > 1 && lines . every ( line => / ^ [ - * • ] \s + / . test ( line . trim ( ) ) ) ;
83-
84- if ( isNumberedList || isBulletedList ) {
85- const listElement = document . createElement ( isNumberedList ? "ol" : "ul" ) ;
86- msg . appendChild ( listElement ) ;
87- let i = 0 ;
88-
89- const interval = setInterval ( ( ) => {
90- if ( i < lines . length ) {
91- const li = document . createElement ( "li" ) ;
92- li . textContent = lines [ i ] . replace ( / ^ ( \d + \. \s + | [ - * • ] \s + ) / , "" ) . trim ( ) ;
93- listElement . appendChild ( li ) ;
94- chat . scrollTop = chat . scrollHeight ;
95- i ++ ;
96- } else {
97- clearInterval ( interval ) ;
98- }
99- } , 200 ) ;
100- } else {
101- let index = 0 ;
102- const interval = setInterval ( ( ) => {
103- if ( index < text . length ) {
104- msg . textContent += text . charAt ( index ++ ) ;
105- chat . scrollTop = chat . scrollHeight ;
106- } else {
107- clearInterval ( interval ) ;
108- }
109- } , 15 ) ;
110- }
111- }
0 commit comments