-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (79 loc) · 3 KB
/
Copy pathscript.js
File metadata and controls
90 lines (79 loc) · 3 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
let aborter = new AbortController();
let autoAbortTimeout = null;
const outputRef = document.querySelector("#outputRef");
const loadingRef = document.querySelector("#loadingRef");
const userQueryRef = document.querySelector("#userQueryRef");
const cancelQueryRef = document.querySelector("#cancelQueryRef");
const submitQueryRef = document.querySelector("#submitQueryRef");
const statusLabelRef = document.querySelector("#statusLabelRef");
userQueryRef.value =
"Based on the latest financial data and current stock market trends, can you provide a detailed analysis of Microsoft's current state? Please include insights into their recent performance, market position, and future outlook. Additionally, retrieve and include the latest closing price of Microsoft's stock using its ticker symbol (MSFT). Send me the full analysis by email.";
cancelQueryRef.addEventListener("click", () => {
aborter.abort();
aborter = new AbortController();
cancelQueryRef.classList.add("hidden");
submitQueryRef.classList.remove("hidden");
loadingRef.classList.add("hidden");
});
submitQueryRef
.addEventListener("click", async (event) => {
const { value } = userQueryRef;
if (value) {
statusLabelRef.innerHTML = "waiting";
outputRef.innerHTML = "";
loadingRef.classList.remove("hidden");
outputRef.classList.add("hidden");
cancelQueryRef.classList.remove("hidden");
submitQueryRef.classList.add("hidden");
autoTimeout();
submitQuery(value);
}
});
function autoTimeout() {
autoAbortTimeout = setTimeout(() => {
cancelQueryRef.click();
outputRef.classList.remove("hidden");
if (outputRef.innerHTML === "") {
outputRef.innerHTML = "Your Assistant could not fetch data. Please try again!"
}
}, 60_000); // cancel request if it times out
}
function submitQuery(body) {
const { API_URL = 'http://localhost:7071' } = import.meta.env;
fetch(`${API_URL}/api/assistant`, {
body,
method: "POST",
signal: aborter.signal
}).then(response => response.body)
.then(processReadableStream);
}
function processReadableStream(stream) {
stream.pipeTo(new WritableStream({
write(chunk, controller) {
const value = new TextDecoder().decode(chunk);
if (value.startsWith('@')) {
statusLabelRef.innerHTML = value.replace('@', '');
return;
}
loadingRef.classList.add("hidden");
outputRef.classList.remove("hidden");
outputRef.innerHTML += value;
outputRef.scrollTop = outputRef.scrollHeight; // scroll to bottom
},
start(controller) {
clearTimeout(autoAbortTimeout); // cancel
},
close(controller) {
cancelQueryRef.classList.add("hidden");
submitQueryRef.classList.remove("hidden");
loadingRef.classList.add("hidden");
outputRef.classList.remove("hidden");
if (outputRef.innerHTML === "") {
outputRef.innerHTML = "Whoops, something went wrong. Please try again!"
}
},
abort(reason) {
console.log(reason);
},
})).catch(console.error);
}