@@ -2,22 +2,21 @@ const chat = document.getElementById("chat");
22const form = document . getElementById ( "input-form" ) ;
33const input = document . getElementById ( "user-input" ) ;
44
5- const messageHistory = [ ] ;
5+ let threadId = null ; // Slaat de thread_id lokaal op
66
77form . addEventListener ( "submit" , async ( e ) => {
88 e . preventDefault ( ) ;
99 const message = input . value . trim ( ) ;
1010 if ( ! message ) return ;
1111
12- appendMessage ( "user" , message ) ;
13- messageHistory . push ( { role : "user" , content : message } ) ;
12+ appendMessage ( "Gebruiker" , message ) ;
1413 input . value = "" ;
1514
1615 try {
1716 const response = await fetch ( "https://chatproxy.azurewebsites.net/api/chatproxy" , {
1817 method : "POST" ,
1918 headers : { "Content-Type" : "application/json" } ,
20- body : JSON . stringify ( { messages : messageHistory } )
19+ body : JSON . stringify ( { message , thread_id : threadId } )
2120 } ) ;
2221
2322 if ( ! response . ok ) {
@@ -26,20 +25,19 @@ form.addEventListener("submit", async (e) => {
2625 throw new Error ( `Serverfout: ${ response . status } ` ) ;
2726 }
2827
29- const text = await response . text ( ) ;
30- appendMessage ( "assistant" , text ) ;
31- messageHistory . push ( { role : "assistant" , content : text } ) ;
32-
28+ const data = await response . json ( ) ;
29+ appendMessage ( "Agent" , data . reply ) ;
30+ threadId = data . thread_id ; // Bewaar thread_id voor vervolgvragen
3331 } catch ( err ) {
34- appendMessage ( "assistant " , "Er ging iets mis." ) ;
32+ appendMessage ( "Agent " , "Er ging iets mis." ) ;
3533 console . error ( "Fout in fetch:" , err ) ;
3634 }
3735} ) ;
3836
39- function appendMessage ( role , text ) {
37+ function appendMessage ( sender , text ) {
4038 const msg = document . createElement ( "div" ) ;
41- msg . className = "message " + ( role === "user " ? "user-message" : "agent-message" ) ;
42- msg . textContent = text ;
39+ msg . classList . add ( "message" , sender === "Gebruiker " ? "user-message" : "agent-message" ) ;
40+ msg . innerHTML = `<div><strong> ${ sender } :</strong> ${ text } </div>` ;
4341 chat . appendChild ( msg ) ;
4442 chat . scrollTop = chat . scrollHeight ;
4543}
0 commit comments