|
1 | | -const chat = document.getElementById("chat"); |
2 | | -const form = document.getElementById("input-form"); |
3 | | -const input = document.getElementById("user-input"); |
4 | | -const streamOutput = document.getElementById("stream-output"); |
5 | | - |
6 | | -let threadId = null; |
7 | | - |
8 | | -window.onload = () => { |
9 | | - const welkomstHTML = ` |
10 | | - Welkom bij de <strong>AI Indicatiehulp</strong>!<br> |
11 | | - Ik ben jouw digitale adviseur voor:<br> |
12 | | - het stellen van de juiste indicatie en het opstellen van een conceptadvies voor de zorgexpert (Kim Brand).<br><br> |
13 | | -
|
14 | | - <strong>Kies een optie om te starten:</strong><br> |
15 | | - 1. In kaart brengen cliëntsituatie<br> |
16 | | - 2. Indicatiestelling extramuraal (zorg thuis)<br> |
17 | | - 3. Indicatiestelling intramuraal (verpleeghuis)<br><br> |
18 | | -
|
19 | | - Wil je direct een indicatieadvies laten opstellen? Dan heb ik meer informatie nodig over de cliënt.<br> |
20 | | - Geef bij voorkeur ook je naam en een e-mailadres of telefoonnummer,<br> |
21 | | - zodat we het conceptadvies voor beoordeling kunnen indienen.<br><br> |
| 1 | +async function startSignalR() { |
| 2 | + try { |
| 3 | + const negotiateResponse = await fetch("https://chatproxy.azurewebsites.net/api/negotiate", { |
| 4 | + method: "POST" |
| 5 | + }); |
22 | 6 |
|
23 | | - <em>Met welke optie wil je verder?</em> |
24 | | - `; |
25 | | - appendFormattedMessage("agent-message", welkomstHTML); |
26 | | -}; |
| 7 | + if (!negotiateResponse.ok) { |
| 8 | + throw new Error(`Negotiate failed: ${negotiateResponse.status}`); |
| 9 | + } |
27 | 10 |
|
28 | | -form.addEventListener("submit", async (e) => { |
29 | | - e.preventDefault(); |
30 | | - const message = input.value.trim(); |
31 | | - if (!message) return; |
| 11 | + const negotiateData = await negotiateResponse.json(); |
32 | 12 |
|
33 | | - appendMessage("user-message", message); |
34 | | - input.value = ""; |
35 | | - streamOutput.textContent = ""; |
| 13 | + const connection = new signalR.HubConnectionBuilder() |
| 14 | + .withUrl(negotiateData.url, { |
| 15 | + accessTokenFactory: () => negotiateData.accessToken |
| 16 | + }) |
| 17 | + .configureLogging(signalR.LogLevel.Information) |
| 18 | + .build(); |
36 | 19 |
|
37 | | - try { |
38 | | - const response = await fetch("https://chatproxy.azurewebsites.net/api/chatproxy", { |
39 | | - method: "POST", |
40 | | - headers: { "Content-Type": "application/json" }, |
41 | | - body: JSON.stringify({ message, thread_id: threadId }) |
| 20 | + connection.on("newToken", token => { |
| 21 | + streamOutput.textContent += token; |
| 22 | + chat.scrollTop = chat.scrollHeight; |
42 | 23 | }); |
43 | 24 |
|
44 | | - const data = await response.json(); |
45 | | - threadId = data.thread_id; |
| 25 | + await connection.start(); |
| 26 | + console.log("✅ Verbonden met SignalR"); |
46 | 27 | } catch (err) { |
47 | | - appendMessage("agent-message", "⚠️ Er ging iets mis bij het ophalen van een antwoord."); |
48 | | - console.error("Fout in fetch:", err); |
| 28 | + console.error("❌ SignalR fout:", err); |
49 | 29 | } |
50 | | -}); |
51 | | - |
52 | | -function appendMessage(cssClass, text) { |
53 | | - const msg = document.createElement("div"); |
54 | | - msg.classList.add("message", cssClass); |
55 | | - msg.textContent = text; |
56 | | - chat.appendChild(msg); |
57 | | - chat.scrollTop = chat.scrollHeight; |
58 | | -} |
59 | | - |
60 | | -function appendFormattedMessage(cssClass, htmlContent) { |
61 | | - const msg = document.createElement("div"); |
62 | | - msg.classList.add("message", cssClass); |
63 | | - msg.innerHTML = htmlContent; |
64 | | - chat.appendChild(msg); |
65 | | - chat.scrollTop = chat.scrollHeight; |
66 | 30 | } |
67 | 31 |
|
68 | | -// ✅ SignalR setup |
69 | | -const connection = new signalR.HubConnectionBuilder() |
70 | | - .withUrl("https://chatproxy.azurewebsites.net/api/negotiate", { |
71 | | - withCredentials: false // ✅ voorkom CORS-misconfiguratie |
72 | | - }) |
73 | | - .configureLogging(signalR.LogLevel.Information) |
74 | | - .build(); |
75 | | - |
76 | | -connection.on("newToken", token => { |
77 | | - streamOutput.textContent += token; |
78 | | - chat.scrollTop = chat.scrollHeight; |
79 | | -}); |
80 | | - |
81 | | -connection |
82 | | - .start() |
83 | | - .then(() => console.log("✅ Verbonden met SignalR")) |
84 | | - .catch(err => console.error("❌ SignalR fout:", err)); |
| 32 | +startSignalR(); |
0 commit comments