forked from ChelseaKR/sprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
163 lines (145 loc) · 5.74 KB
/
Copy pathapp.js
File metadata and controls
163 lines (145 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Sprout's stateless reference surface. Sentence-grained SSE means the live region
// only receives citation-verified text; ungrounded text is never streamed and retracted.
"use strict";
(function () {
const form = document.getElementById("ask-form");
const input = document.getElementById("q");
const statusEl = document.getElementById("status");
const answerEl = document.getElementById("answer");
const answerHeading = document.getElementById("answer-h");
const evidencePanel = document.querySelector(".evidence-panel");
const safetyEl = document.getElementById("safety");
const disagreementsEl = document.getElementById("disagreements");
const sourcesWrap = document.getElementById("sources-wrap");
const citationsEl = document.getElementById("citations");
const metaEl = document.getElementById("meta");
const askBtn = document.getElementById("ask-btn");
let source = null;
function selectedLanguage() {
const checked = document.querySelector('input[name="lang"]:checked');
return checked ? checked.value : "en";
}
function setBusy(isBusy) {
evidencePanel.setAttribute("aria-busy", String(isBusy));
askBtn.disabled = isBusy;
}
function resetResult() {
if (source) {
source.close();
source = null;
}
answerEl.textContent = "";
citationsEl.textContent = "";
metaEl.textContent = "";
safetyEl.hidden = true;
safetyEl.textContent = "";
disagreementsEl.hidden = true;
disagreementsEl.textContent = "";
sourcesWrap.hidden = true;
answerHeading.textContent = "Checking the evidence";
}
function addCitation(label, url) {
for (const item of citationsEl.children) {
if (item.dataset.label === label) return;
}
const item = document.createElement("li");
item.dataset.label = label;
if (url && /^https?:\/\//.test(url)) {
const link = document.createElement("a");
link.href = url;
link.textContent = label;
link.rel = "noopener noreferrer";
item.appendChild(link);
} else {
item.textContent = label;
}
citationsEl.appendChild(item);
sourcesWrap.hidden = false;
}
function ask(question) {
resetResult();
setBusy(true);
statusEl.textContent = "Searching the dated corpus and verifying each sentence…";
const url = "/api/chat/stream?q=" + encodeURIComponent(question) +
"&language=" + encodeURIComponent(selectedLanguage());
source = new EventSource(url);
source.addEventListener("sentence", function (event) {
const data = JSON.parse(event.data);
const paragraph = document.createElement("p");
paragraph.textContent = data.text + " ";
const marker = document.createElement("span");
marker.className = "cite-marker";
marker.textContent = "[" + data.citation + "]";
paragraph.appendChild(marker);
answerEl.appendChild(paragraph);
addCitation(data.citation, data.url);
answerHeading.textContent = "Verified answer";
statusEl.textContent = "";
});
source.addEventListener("refusal", function (event) {
const data = JSON.parse(event.data);
const paragraph = document.createElement("p");
paragraph.textContent = data.text;
answerEl.appendChild(paragraph);
answerHeading.textContent = "Honest refusal";
statusEl.textContent = "The corpus did not support an answer.";
});
source.addEventListener("safety", function (event) {
const data = JSON.parse(event.data);
safetyEl.textContent = data.text;
safetyEl.hidden = false;
});
// EXP-02: when retrieved sources give a conflicting numeric care cadence, both
// citations are rendered here plainly rather than one being silently dropped.
source.addEventListener("disagreement", function (e) {
const data = JSON.parse(e.data);
const p = document.createElement("p");
p.textContent = data.text;
disagreementsEl.appendChild(p);
disagreementsEl.hidden = false;
});
source.addEventListener("done", function (event) {
const data = JSON.parse(event.data);
const details = [];
if (typeof data.confidence === "number") {
// The verbalized band leads, the raw number follows in parentheses — never the
// reverse, and never the band alone — so a screen reader announces the
// calibrated language first while the number stays available to anyone who
// wants it (EXP-06). Bands come from confidence.py, derived from the
// committed reliability diagram, not invented client-side.
const bandLabel = data.confidence_band_label || null;
details.push("Confidence: " + (bandLabel ? bandLabel + " " : "") +
"(" + data.confidence.toFixed(2) + ")" +
(data.low_confidence && !data.refused ? " — low; check another source" : ""));
}
if (data.as_of) details.push("References current to " + data.as_of);
if (data.disclosure) details.push(data.disclosure);
metaEl.textContent = details.join(" · ");
setBusy(false);
if (source) {
source.close();
source = null;
}
});
source.onerror = function () {
answerHeading.textContent = "Reference unavailable";
statusEl.textContent = "The answer service could not be reached. Check the server and try again.";
setBusy(false);
if (source) {
source.close();
source = null;
}
};
}
form.addEventListener("submit", function (event) {
event.preventDefault();
const question = input.value.trim();
if (question) ask(question);
});
document.getElementById("examples").addEventListener("click", function (event) {
const button = event.target.closest("button[data-q]");
if (!button) return;
input.value = button.dataset.q;
ask(button.dataset.q);
});
})();