|
1 | | -const form = document.querySelector('form'); |
2 | | -const input = document.querySelector('#message'); |
3 | | -const chatbox = document.querySelector('#chatbox'); |
| 1 | +const chat = document.getElementById("chat"); |
| 2 | +const form = document.getElementById("input-form"); |
| 3 | +const input = document.getElementById("user-input"); |
4 | 4 |
|
5 | | -let thread_id = null; |
6 | | - |
7 | | -form.addEventListener('submit', async (e) => { |
| 5 | +form.addEventListener("submit", async (e) => { |
8 | 6 | e.preventDefault(); |
9 | 7 | const message = input.value.trim(); |
10 | 8 | if (!message) return; |
11 | 9 |
|
12 | | - appendMessage('user', message); |
13 | | - input.value = ''; |
| 10 | + appendMessage("Gebruiker", message); |
| 11 | + input.value = ""; |
14 | 12 |
|
15 | 13 | try { |
16 | | - const response = await fetch('https://chatproxy.azurewebsites.net/api/chatproxy', { |
17 | | - method: 'POST', |
18 | | - headers: { 'Content-Type': 'application/json' }, |
19 | | - body: JSON.stringify({ message, thread_id }) |
| 14 | + const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
| 15 | + method: "POST", |
| 16 | + headers: { "Content-Type": "application/json" }, |
| 17 | + body: JSON.stringify({ message }) |
20 | 18 | }); |
21 | 19 |
|
22 | | - const data = await response.json(); |
23 | | - if (response.ok) { |
24 | | - thread_id = data.thread_id; |
25 | | - appendMessage('assistant', data.response); |
26 | | - } else { |
27 | | - appendMessage('system', 'Fout: ' + data.error); |
| 20 | + if (!response.ok) { |
| 21 | + const errorText = await response.text(); |
| 22 | + console.error("Responsetekst:", errorText); |
| 23 | + throw new Error(`Serverfout: ${response.status}`); |
28 | 24 | } |
| 25 | + |
| 26 | + const text = await response.text(); |
| 27 | + appendMessage("Agent", text); |
29 | 28 | } catch (err) { |
30 | | - appendMessage('system', 'Verbinding mislukt: ' + err.message); |
| 29 | + appendMessage("Agent", "Er ging iets mis."); |
| 30 | + console.error("Fout in fetch:", err); |
31 | 31 | } |
32 | 32 | }); |
33 | 33 |
|
34 | | -function appendMessage(role, content) { |
35 | | - const div = document.createElement('div'); |
36 | | - div.className = role; |
37 | | - div.textContent = `${role}: ${content}`; |
38 | | - chatbox.appendChild(div); |
39 | | - chatbox.scrollTop = chatbox.scrollHeight; |
| 34 | +function appendMessage(sender, text) { |
| 35 | + const msg = document.createElement("div"); |
| 36 | + msg.innerHTML = `<div class="user">${sender}:</div><div class="agent">${text}</div>`; |
| 37 | + chat.appendChild(msg); |
| 38 | + chat.scrollTop = chat.scrollHeight; |
40 | 39 | } |
0 commit comments