@@ -4,6 +4,7 @@ const input = document.getElementById("user-input");
44
55let threadId = null ;
66
7+ // Openingsbericht bij het laden van de pagina
78window . onload = ( ) => {
89 const welkomstHTML = `
910 Welkom bij de <strong>AI Indicatiehulp</strong>!<br>
@@ -32,49 +33,34 @@ form.addEventListener("submit", async (e) => {
3233 appendMessage ( "user-message" , message ) ;
3334 input . value = "" ;
3435
35- const msgElem = appendMessage ( "agent-message" , "" ) ;
36-
3736 try {
3837 const response = await fetch ( "https://chatproxy.azurewebsites.net/api/chatproxy" , {
3938 method : "POST" ,
4039 headers : { "Content-Type" : "application/json" } ,
4140 body : JSON . stringify ( { message, thread_id : threadId } )
4241 } ) ;
4342
44- if ( ! response . ok || ! response . body ) {
45- msgElem . textContent = "⚠️ Fout bij ophalen van antwoord." ;
46- return ;
43+ if ( ! response . ok ) {
44+ const errorText = await response . text ( ) ;
45+ console . error ( "Responsetekst:" , errorText ) ;
46+ throw new Error ( `Serverfout: ${ response . status } ` ) ;
4747 }
4848
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- }
49+ const data = await response . json ( ) ;
50+ threadId = data . thread_id ;
51+ streamMessage ( "agent-message" , data . reply ) ;
6552 } catch ( err ) {
66- msgElem . textContent = "⚠️ Er ging iets mis.";
53+ streamMessage ( "agent-message" , " Er ging iets mis.") ;
6754 console . error ( "Fout in fetch:" , err ) ;
6855 }
6956} ) ;
7057
7158function appendMessage ( cssClass , text ) {
7259 const msg = document . createElement ( "div" ) ;
7360 msg . classList . add ( "message" , cssClass ) ;
74- msg . textContent = text || "" ;
61+ msg . textContent = text ;
7562 chat . appendChild ( msg ) ;
7663 chat . scrollTop = chat . scrollHeight ;
77- return msg ;
7864}
7965
8066function appendFormattedMessage ( cssClass , htmlContent ) {
@@ -84,3 +70,42 @@ function appendFormattedMessage(cssClass, htmlContent) {
8470 chat . appendChild ( msg ) ;
8571 chat . scrollTop = chat . scrollHeight ;
8672}
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