|
1 | | -const chat = document.getElementById("chat"); |
2 | | -const form = document.getElementById("input-form"); |
3 | | -const input = document.getElementById("user-input"); |
| 1 | +const form = document.querySelector('form'); |
| 2 | +const input = document.querySelector('#message'); |
| 3 | +const chatbox = document.querySelector('#chatbox'); |
4 | 4 |
|
5 | | -form.addEventListener("submit", async (e) => { |
| 5 | +let thread_id = null; |
| 6 | + |
| 7 | +form.addEventListener('submit', async (e) => { |
6 | 8 | e.preventDefault(); |
7 | 9 | const message = input.value.trim(); |
8 | 10 | if (!message) return; |
9 | 11 |
|
10 | | - appendMessage("Gebruiker", message); |
11 | | - input.value = ""; |
| 12 | + appendMessage('user', message); |
| 13 | + input.value = ''; |
12 | 14 |
|
13 | 15 | try { |
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 }) |
| 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 }) |
18 | 20 | }); |
19 | 21 |
|
20 | | - if (!response.ok) { |
21 | | - const errorText = await response.text(); |
22 | | - console.error("Responsetekst:", errorText); |
23 | | - throw new Error(`Serverfout: ${response.status}`); |
| 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); |
24 | 28 | } |
25 | | - |
26 | | - const text = await response.text(); |
27 | | - appendMessage("Agent", text); |
28 | 29 | } catch (err) { |
29 | | - appendMessage("Agent", "Er ging iets mis."); |
30 | | - console.error("Fout in fetch:", err); |
| 30 | + appendMessage('system', 'Verbinding mislukt: ' + err.message); |
31 | 31 | } |
32 | 32 | }); |
33 | 33 |
|
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; |
| 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; |
39 | 40 | } |
0 commit comments