@@ -2,19 +2,22 @@ const chat = document.getElementById("chat");
22const form = document . getElementById ( "input-form" ) ;
33const input = document . getElementById ( "user-input" ) ;
44
5+ const messageHistory = [ ] ;
6+
57form . addEventListener ( "submit" , async ( e ) => {
68 e . preventDefault ( ) ;
79 const message = input . value . trim ( ) ;
810 if ( ! message ) return ;
911
1012 appendMessage ( "user" , message ) ;
13+ messageHistory . push ( { role : "user" , content : message } ) ;
1114 input . value = "" ;
1215
1316 try {
1417 const response = await fetch ( "https://chatproxy.azurewebsites.net/api/chatproxy" , {
1518 method : "POST" ,
1619 headers : { "Content-Type" : "application/json" } ,
17- body : JSON . stringify ( { message } )
20+ body : JSON . stringify ( { messages : messageHistory } )
1821 } ) ;
1922
2023 if ( ! response . ok ) {
@@ -23,8 +26,10 @@ form.addEventListener("submit", async (e) => {
2326 throw new Error ( `Serverfout: ${ response . status } ` ) ;
2427 }
2528
26- const fullText = await response . text ( ) ;
27- simulateTyping ( "assistant" , fullText ) ;
29+ const text = await response . text ( ) ;
30+ appendMessage ( "assistant" , text ) ;
31+ messageHistory . push ( { role : "assistant" , content : text } ) ;
32+
2833 } catch ( err ) {
2934 appendMessage ( "assistant" , "Er ging iets mis." ) ;
3035 console . error ( "Fout in fetch:" , err ) ;
@@ -33,24 +38,8 @@ form.addEventListener("submit", async (e) => {
3338
3439function appendMessage ( role , text ) {
3540 const msg = document . createElement ( "div" ) ;
36- msg . classList . add ( "message" ) ;
37- msg . classList . add ( role === "user" ? "user-message" : "agent-message" ) ;
38- msg . innerText = `${ text } ` ;
41+ msg . className = "message " + ( role === "user" ? "user-message" : "agent-message" ) ;
42+ msg . textContent = text ;
3943 chat . appendChild ( msg ) ;
4044 chat . scrollTop = chat . scrollHeight ;
4145}
42-
43- function simulateTyping ( role , fullText ) {
44- const msg = document . createElement ( "div" ) ;
45- msg . classList . add ( "message" ) ;
46- msg . classList . add ( role === "user" ? "user-message" : "agent-message" ) ;
47- chat . appendChild ( msg ) ;
48-
49- let index = 0 ;
50- const interval = setInterval ( ( ) => {
51- msg . innerText = fullText . slice ( 0 , index ) ;
52- chat . scrollTop = chat . scrollHeight ;
53- index ++ ;
54- if ( index > fullText . length ) clearInterval ( interval ) ;
55- } , 20 ) ; // snelheid: 20ms per karakter
56- }
0 commit comments