-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
338 lines (277 loc) · 10.5 KB
/
Copy pathscript.js
File metadata and controls
338 lines (277 loc) · 10.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const menuToggle = document.getElementById("menu-toggle");
const sidebar = document.getElementById("sidebar");
const overlay = document.getElementById("overlay");
const chatForm = document.getElementById("chat-form");
const chatInput = document.getElementById("chat-input");
const chatSend = document.getElementById("chat-send");
const chatThread = document.getElementById("chat-thread");
const chatTooltip = document.getElementById("chat-tooltip");
const tooltipBackdrop = document.getElementById("tooltip-backdrop");
const tooltipCta = document.getElementById("tooltip-cta");
const tooltipTitle = document.getElementById("tooltip-title");
const tooltipCaption = document.getElementById("tooltip-caption");
const demoNotice =
"This is a demo for the State Product Challenge so this functionality doesn't actually work";
const tooltipContent = {
first: {
title: "Ask State AI a Question",
caption:
"You can ask State AI a question to see what your citizens think about any topic.",
cta: "Ask a question",
},
second: {
title: "Ask a Follow-Up",
caption:
"You can follow up on anything the State AI responds with for more info or sources",
cta: "Send Follow Up",
},
};
const scriptedQuestion =
"What are the most common frustrations being voiced by people at the moment?";
const followUpQuestion =
"Can you tell me more about what people are saying on the housing developments?";
const scriptedResponse = `There are several frustrations being voiced by your citizens recently.
These include global/national topics such as:
- Immigration - this is an ongoing debate with people increasingly voicing frustrations on this topic and its handling by the government
- Foreign Policy - people are concerned that our relationship with the US is fracturing
- Defence - people are worried about the increasing number of conflicts across the globe and the potential for this to spiral into a broader multi-nation war
And more regional topics in your constituancy like:
- Housing - people are frustrated by the recent developments that have been approved with general sentiment that the developers won't do enough to offset negative impacts e.g. increased traffic or load on public services
- Pot Holes - there are quite a few people reporting pot holes damaging their cars and not being sorted even weeks after being reported
Would you like sources for any of the above or to further explore any specific topics?`;
const followUpResponse = `Across the local Berkhamsted social media groups there are several people complaining about the housing developments that were just approved in Northchurch. They worry that these will be...
- too dense for the local road network, with peak-time traffic already strained around key junctions
- under-supported by GP capacity, school places, and other local public services
- weak on infrastructure guarantees, with concerns that improvements may lag behind construction
- damaging to local character, green space, and the existing feel of nearby neighbourhoods
There are also residents who support more homes in principle, but they want clearer phasing plans, stronger developer accountability, and regular public updates so the impact feels managed rather than imposed.
If useful, I can break this down by recurring themes, sentiment direction, and representative local posts.`;
let shouldUseScriptedResponse = false;
let isDemoSequenceRunning = false;
let activeTooltipStep = "first";
let pendingAfterScriptedResponse = null;
const INPUT_TYPE_DELAY_MS = 9;
const RESPONSE_TYPE_DELAY_MS = 7;
const RESPONSE_NEWLINE_DELAY_MS = 12;
const TYPING_BUBBLE_DELAY_MS = 520;
function setSidebarOpen(isOpen) {
sidebar.classList.toggle("open", isOpen);
overlay.hidden = !isOpen;
menuToggle.setAttribute("aria-expanded", String(isOpen));
}
if (menuToggle && sidebar && overlay) {
menuToggle.addEventListener("click", () => {
const isOpen = !sidebar.classList.contains("open");
setSidebarOpen(isOpen);
});
overlay.addEventListener("click", () => setSidebarOpen(false));
window.addEventListener("resize", () => {
if (window.innerWidth > 920) {
setSidebarOpen(false);
}
});
}
if (sidebar) {
sidebar.addEventListener("click", (event) => {
const target = event.target;
if (!(target instanceof Element)) return;
const clickable = target.closest("a, button");
if (!clickable) return;
event.preventDefault();
window.alert(demoNotice);
});
}
function sleep(ms) {
return new Promise((resolve) => window.setTimeout(resolve, ms));
}
function resizeChatInput() {
if (!chatInput) return;
chatInput.style.height = "auto";
chatInput.style.height = `${Math.min(chatInput.scrollHeight, 160)}px`;
}
function setTooltipStep(step) {
if (!tooltipTitle || !tooltipCaption || !tooltipCta) return;
const content = tooltipContent[step];
if (!content) return;
tooltipTitle.textContent = content.title;
tooltipCaption.textContent = content.caption;
tooltipCta.textContent = content.cta;
activeTooltipStep = step;
}
function showChatTooltip(step = activeTooltipStep) {
if (!chatTooltip) return;
if (!tooltipContent[step]) return;
setTooltipStep(step);
if (tooltipBackdrop) {
tooltipBackdrop.hidden = false;
tooltipBackdrop.classList.add("visible");
}
chatTooltip.hidden = false;
window.requestAnimationFrame(() => {
chatTooltip.classList.add("visible");
});
}
function hideChatTooltip() {
if (!chatTooltip) return;
chatTooltip.classList.remove("visible");
if (tooltipBackdrop) {
tooltipBackdrop.classList.remove("visible");
}
window.setTimeout(() => {
if (!chatTooltip.classList.contains("visible")) {
chatTooltip.hidden = true;
}
if (tooltipBackdrop && !chatTooltip.classList.contains("visible")) {
tooltipBackdrop.hidden = true;
}
}, 200);
}
if (chatTooltip) {
window.setTimeout(() => showChatTooltip("first"), 2000);
}
async function runScriptedPrompt(question, response) {
if (!chatForm || !chatSend || !chatInput) return;
isDemoSequenceRunning = true;
hideChatTooltip();
chatInput.focus();
chatInput.value = "";
resizeChatInput();
for (const char of question) {
chatInput.value += char;
resizeChatInput();
await sleep(INPUT_TYPE_DELAY_MS);
}
chatSend.classList.remove("flash-send");
void chatSend.offsetWidth;
chatSend.classList.add("flash-send");
await sleep(360);
shouldUseScriptedResponse = true;
chatForm.dataset.scriptedResponse = response;
chatForm.requestSubmit();
isDemoSequenceRunning = false;
}
if (tooltipCta && chatInput) {
tooltipCta.addEventListener("click", async (event) => {
event.stopPropagation();
if (!chatForm || !chatSend || isDemoSequenceRunning) return;
if (activeTooltipStep === "first") {
activeTooltipStep = "awaiting-second";
pendingAfterScriptedResponse = async () => {
await sleep(1000);
showChatTooltip("second");
};
await runScriptedPrompt(scriptedQuestion, scriptedResponse);
return;
}
if (activeTooltipStep === "second") {
activeTooltipStep = "done";
await runScriptedPrompt(followUpQuestion, followUpResponse);
}
});
}
function appendMessage(type, author, content) {
if (!chatThread) return;
const message = document.createElement("article");
message.className = `message ${type}`;
const contentWrap = document.createElement("div");
contentWrap.className = "message-content";
const label = document.createElement("div");
label.className = "message-label";
label.textContent = author;
const text = document.createElement("p");
text.textContent = content;
contentWrap.append(label, text);
if (type === "assistant" || type === "typing") {
const avatar = document.createElement("div");
avatar.className = "message-avatar";
avatar.setAttribute("aria-hidden", "true");
avatar.textContent = "AI";
message.append(avatar);
}
message.append(contentWrap);
chatThread.appendChild(message);
chatThread.scrollTop = chatThread.scrollHeight;
return { message, text };
}
function appendTypingMessage() {
const typing = appendMessage("assistant typing", "State AI", "");
if (!typing) return null;
typing.text.innerHTML =
'<span class="typing-dots"><span></span><span></span><span></span></span>';
return typing.message;
}
async function streamAssistantResponse(content) {
const typingBubble = appendTypingMessage();
await sleep(TYPING_BUBBLE_DELAY_MS);
if (typingBubble) {
typingBubble.remove();
}
const assistant = appendMessage("assistant", "State AI", "");
if (!assistant) return;
for (const char of content) {
assistant.text.textContent += char;
if (chatThread) {
chatThread.scrollTop = chatThread.scrollHeight;
}
await sleep(char === "\n" ? RESPONSE_NEWLINE_DELAY_MS : RESPONSE_TYPE_DELAY_MS);
}
}
function shouldShowGuidedTooltip() {
if (isDemoSequenceRunning) return false;
return activeTooltipStep === "first" || activeTooltipStep === "second";
}
if (chatForm && chatInput && chatThread) {
chatInput.readOnly = true;
chatInput.addEventListener("focus", () => {
if (shouldShowGuidedTooltip()) {
showChatTooltip(activeTooltipStep);
}
});
chatInput.addEventListener("click", () => {
if (shouldShowGuidedTooltip()) {
showChatTooltip(activeTooltipStep);
}
});
if (chatSend) {
chatSend.addEventListener("click", () => {
if (shouldShowGuidedTooltip()) {
showChatTooltip(activeTooltipStep);
}
});
}
chatInput.addEventListener("input", resizeChatInput);
chatInput.addEventListener("keydown", (event) => {
event.preventDefault();
});
chatForm.addEventListener("submit", (event) => {
event.preventDefault();
if (!shouldUseScriptedResponse) {
return;
}
const prompt = chatInput.value.trim();
if (!prompt) return;
appendMessage("user", "You", prompt);
chatInput.value = "";
resizeChatInput();
if (shouldUseScriptedResponse) {
shouldUseScriptedResponse = false;
const responseToStream = chatForm.dataset.scriptedResponse || scriptedResponse;
const afterResponse = pendingAfterScriptedResponse;
pendingAfterScriptedResponse = null;
void (async () => {
await streamAssistantResponse(responseToStream);
if (afterResponse) {
await afterResponse();
}
})();
return;
}
window.setTimeout(() => {
appendMessage(
"assistant",
"State AI",
"Demo mode: connect this input to your backend endpoint to return real citizen sentiment insights."
);
}, 420);
});
}