Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (124 loc) · 5.17 KB
/
Copy pathscript.js
File metadata and controls
136 lines (124 loc) · 5.17 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
const apiKey =
"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";
function getAnswer(question) {
if (!navigator.onLine) {
Swal.fire({
title: "No Internet!",
text: "You are offline. Reconnect and try again.",
icon: "error",
confirmButtonText: "Retry",
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
} else {
const fetchData = fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
// model: "deepseek/deepseek-r1-zero:free",
// model: "deepseek/deepseek-r1-distill-llama-70b:free",
model: "deepseek/deepseek-r1-distill-llama-70b:free",
messages: [
{
role: "user",
content: question,
},
],
}),
});
fetchData
.then((response) => response.json())
.then((response) => {
const resultData = response.choices[0].message.content;
isAnswerLoading = false;
addAnswerDiv(resultData);
})
.catch((err) => {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!",
confirmButtonText: "Retry",
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
console.log(err);
})
.finally(() => {
sendBtn.classList.remove("disabled");
scrollToBottom();
});
}
}
let isAnswerLoading = false;
let answerSectionId = 0;
// selecting elements
const sendBtn = document.getElementById("sendBtn");
const chatInput = document.getElementById("chatInput");
const content = document.getElementById("content");
const chatContainer = document.querySelector(".chat-form");
function handleMessage() {
const question = chatInput.value.trim();
if (question === "" || isAnswerLoading) return;
sendBtn.classList.add("disabled");
addQuestionDiv(question);
chatInput.value = "";
}
document.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
event.preventDefault();
handleMessage();
}
});
chatInput.addEventListener("input", () => {
const availableHeight = window.innerHeight - chatContainer.clientHeight - 20;
content.style.height = availableHeight + "px";
scrollToBottom();
});
sendBtn.addEventListener("click", handleMessage);
function addQuestionDiv(message) {
isAnswerLoading = true;
const newDiv = document.createElement("div");
newDiv.className = "question-section";
newDiv.textContent = message;
content.appendChild(newDiv);
addAnswerDiv(message);
scrollToBottom();
}
function addAnswerDiv(message) {
if (isAnswerLoading) {
answerSectionId++; // Increment answer ID for tracking
const answerDiv = document.createElement("div");
answerDiv.classList = "answer-section";
answerDiv.innerHTML = getLoadingAnimnation(); // Show loading animation
answerDiv.id = answerSectionId; // Assign ID
content.appendChild(answerDiv);
getAnswer(message); // Fetch AI response
} else {
const answerDivElement = document.getElementById(answerSectionId);
if (answerDivElement) {
answerDivElement.innerHTML = marked.parse(message); // Convert Markdown to HTML
// Apply syntax highlighting
answerDivElement.querySelectorAll("pre code").forEach((block) => {
hljs.highlightElement(block);
});
}
}
}
function getLoadingAnimnation() {
return '<svg style="height: 28px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><circle fill="#7981A1" stroke="#7981A1" stroke-width="15" r="15" cx="35" cy="100"><animate attributeName="cx" calcMode="spline" dur="2" values="35;165;165;35;35" keySplines="0 .1 .5 1;0 .1 .5 1;0 .1 .5 1;0 .1 .5 1" repeatCount="indefinite" begin="0"></animate></circle><circle fill="#7981A1" stroke="#7981A1" stroke-width="15" opacity=".8" r="15" cx="35" cy="100"><animate attributeName="cx" calcMode="spline" dur="2" values="35;165;165;35;35" keySplines="0 .1 .5 1;0 .1 .5 1;0 .1 .5 1;0 .1 .5 1" repeatCount="indefinite" begin="0.05"></animate></circle><circle fill="#7981A1" stroke="#7981A1" stroke-width="15" opacity=".6" r="15" cx="35" cy="100"><animate attributeName="cx" calcMode="spline" dur="2" values="35;165;165;35;35" keySplines="0 .1 .5 1;0 .1 .5 1;0 .1 .5 1;0 .1 .5 1" repeatCount="indefinite" begin=".1"></animate></circle><circle fill="#7981A1" stroke="#7981A1" stroke-width="15" opacity=".4" r="15" cx="35" cy="100"><animate attributeName="cx" calcMode="spline" dur="2" values="35;165;165;35;35" keySplines="0 .1 .5 1;0 .1 .5 1;0 .1 .5 1;0 .1 .5 1" repeatCount="indefinite" begin=".15"></animate></circle><circle fill="#7981A1" stroke="#7981A1" stroke-width="15" opacity=".2" r="15" cx="35" cy="100"><animate attributeName="cx" calcMode="spline" dur="2" values="35;165;165;35;35" keySplines="0 .1 .5 1;0 .1 .5 1;0 .1 .5 1;0 .1 .5 1" repeatCount="indefinite" begin=".2"></animate></circle></svg>';
// return '<span class="loader"></span>';
}
function scrollToBottom() {
content.scrollTo({
top: content.scrollHeight,
behavior: "smooth",
});
}