forked from snxraven/ravenscott-blog
update AI page
This commit is contained in:
parent
e745cc0cff
commit
9a1bd49f27
@ -1,207 +1,201 @@
|
|||||||
<!DOCTYPE html>
|
// Handles key down event to send message on Enter
|
||||||
<html lang="en">
|
function handleKeyDown(event) {
|
||||||
|
if (event.key === 'Enter' && !event.shiftKey) {
|
||||||
|
event.preventDefault();
|
||||||
|
sendMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
<head>
|
// Sends a message to the chat API
|
||||||
<!-- Meta and Title -->
|
async function sendMessage() {
|
||||||
<meta charset="UTF-8">
|
const messageInput = document.getElementById('messageInput');
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
let message = messageInput.value.trim();
|
||||||
<meta name="description" content="Chat with <%= process.env.OWNER_NAME %>'s AI assistant">
|
|
||||||
|
|
||||||
<title>Chat with AI - <%= process.env.OWNER_NAME %></title>
|
if (message === '') return;
|
||||||
<!-- Bootstrap CSS -->
|
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
|
|
||||||
<!-- Font Awesome CSS for Icons -->
|
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
|
||||||
<!-- Custom CSS -->
|
|
||||||
<link rel="stylesheet" href="<%= process.env.HOST_URL %>/css/styles.css">
|
|
||||||
<link rel="apple-touch-icon" sizes="180x180" href="<%= process.env.HOST_URL %>/apple-touch-icon.png">
|
|
||||||
<link rel="icon" type="image/png" sizes="32x32" href="<%= process.env.HOST_URL %>/favicon-32x32.png">
|
|
||||||
<link rel="icon" type="image/png" sizes="16x16" href="<%= process.env.HOST_URL %>/favicon-16x16.png">
|
|
||||||
<link rel="manifest" href="<%= process.env.HOST_URL %>/site.webmanifest">
|
|
||||||
<meta name="msapplication-TileColor" content="#da532c">
|
|
||||||
<meta name="theme-color" content="#ffffff">
|
|
||||||
<!-- reCAPTCHA API -->
|
|
||||||
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body class="bg-dark text-white">
|
// Encode the message to avoid XSS attacks
|
||||||
|
message = he.encode(message);
|
||||||
|
|
||||||
<!-- Navbar -->
|
// Display the user's message in the chat
|
||||||
<nav class="navbar navbar-expand-lg navbar-dark">
|
displayMessage(message, 'user');
|
||||||
<div class="container-fluid">
|
messageInput.value = ''; // Clear the input
|
||||||
<a class="navbar-brand" href="<%= process.env.HOST_URL %>"><%= process.env.SITE_NAME %></a>
|
toggleLoading(true); // Show loading indicator
|
||||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
|
||||||
<span class="navbar-toggler-icon"></span>
|
|
||||||
</button>
|
|
||||||
<div class="collapse navbar-collapse" id="navbarNav">
|
|
||||||
<ul class="navbar-nav ms-auto">
|
|
||||||
<% menuItems.forEach(item => { %>
|
|
||||||
<li class="nav-item">
|
|
||||||
<a class="nav-link" href="<%= item.url %>" <%= item.openNewPage ? 'target="_blank"' : '' %>><%= item.title %></a>
|
|
||||||
</li>
|
|
||||||
<% }) %>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<!-- Chat Section -->
|
try {
|
||||||
<header class="d-flex align-items-center justify-content-center text-center py-5">
|
const response = await fetch('https://infer.x64.world/chat', {
|
||||||
<div class="container">
|
method: 'POST',
|
||||||
<h2 class="mb-4 text-white">Chat with <%= process.env.OWNER_NAME %>'s AI</h2>
|
headers: {
|
||||||
<p class="lead text-white">Ask any question, and the AI will respond to you!</p>
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ message: message })
|
||||||
|
});
|
||||||
|
|
||||||
<!-- Chat Box -->
|
if (response.ok) {
|
||||||
<div id="chatBox" class="bg-light p-4 rounded text-dark mb-4">
|
const data = await response.json();
|
||||||
<div id="messages" class="mb-3" style="max-height: 300px; overflow-y: scroll;"></div>
|
displayMessage(data.content, 'assistant');
|
||||||
|
} else {
|
||||||
|
handleErrorResponse(response.status);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
displayMessage('Error: ' + error.message, 'assistant');
|
||||||
|
} finally {
|
||||||
|
toggleLoading(false); // Hide loading indicator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
<textarea id="messageInput" class="form-control mb-2 bg-dark text-white" rows="3" placeholder="Type your message here..." onkeydown="handleKeyDown(event)"></textarea>
|
// Toggles the loading indicator
|
||||||
<button class="btn btn-primary w-100" onclick="sendMessage()">Send Message</button>
|
function toggleLoading(show) {
|
||||||
|
const loadingElement = document.getElementById('loading');
|
||||||
|
loadingElement.style.display = show ? 'block' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
<!-- Loading Indicator -->
|
// Displays a message in the chat window
|
||||||
<div id="loading" class="text-center mt-3" style="display: none;">
|
function displayMessage(content, sender) {
|
||||||
<div class="spinner-border text-primary" role="status">
|
const messages = document.getElementById('messages');
|
||||||
<span class="visually-hidden">Loading...</span>
|
const messageElement = document.createElement('div');
|
||||||
</div>
|
messageElement.classList.add('message', sender);
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Alert Messages -->
|
// Decode HTML entities and render Markdown
|
||||||
<div id="success-alert" class="alert alert-success mt-3" style="display: none;"></div>
|
const decodedContent = he.decode(content);
|
||||||
<div id="error-alert" class="alert alert-danger mt-3" style="display: none;"></div>
|
const htmlContent = marked(decodedContent);
|
||||||
|
messageElement.innerHTML = htmlContent;
|
||||||
|
|
||||||
</div>
|
messages.appendChild(messageElement);
|
||||||
</header>
|
messages.scrollTop = messages.scrollHeight; // Scroll to the bottom of the chat
|
||||||
|
|
||||||
<!-- Footer -->
|
// Highlight code blocks if any
|
||||||
<footer class="text-white text-center py-4">
|
document.querySelectorAll('pre code').forEach((block) => {
|
||||||
<div class="container">
|
hljs.highlightElement(block);
|
||||||
<p class="mb-0">© 2024 <%= process.env.OWNER_NAME %>. All rights reserved.</p>
|
if (sender === 'assistant') {
|
||||||
</div>
|
addCopyButton(block); // Add copy button to code blocks
|
||||||
</footer>
|
}
|
||||||
|
});
|
||||||
|
|
||||||
<!-- Bootstrap JS Bundle -->
|
// Add "Copy Full Response" button after each assistant response
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
|
if (sender === 'assistant') {
|
||||||
<!-- Additional Libraries -->
|
addCopyFullResponseButton(messages, messageElement);
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/3.0.7/marked.min.js"></script>
|
}
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
|
}
|
||||||
<script src="https://cdn.jsdelivr.net/npm/he@1.2.0/he.min.js"></script>
|
|
||||||
|
|
||||||
<!-- Inline JavaScript -->
|
// Adds a copy button to a code block
|
||||||
<script>
|
function addCopyButton(block) {
|
||||||
// Handles key down event to send message on Enter
|
const button = document.createElement('button');
|
||||||
function handleKeyDown(event) {
|
button.classList.add('copy-button');
|
||||||
if (event.key === 'Enter' && !event.shiftKey) {
|
button.textContent = 'Copy';
|
||||||
event.preventDefault();
|
button.addEventListener('click', () => copyToClipboard(block));
|
||||||
sendMessage();
|
block.parentNode.appendChild(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds "Copy Full Response" button below the assistant response
|
||||||
|
function addCopyFullResponseButton(messagesContainer, messageElement) {
|
||||||
|
const copyFullResponseButton = document.createElement('button');
|
||||||
|
copyFullResponseButton.classList.add('copy-button');
|
||||||
|
copyFullResponseButton.textContent = 'Copy Full Response';
|
||||||
|
copyFullResponseButton.addEventListener('click', () => copyFullResponse(messageElement));
|
||||||
|
|
||||||
|
messagesContainer.appendChild(copyFullResponseButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copies code block content to the clipboard
|
||||||
|
function copyToClipboard(block) {
|
||||||
|
const text = block.innerText;
|
||||||
|
navigator.clipboard.writeText(text).then(() => {
|
||||||
|
displayAlert('success', 'The code block was copied to the clipboard!');
|
||||||
|
}).catch((err) => {
|
||||||
|
displayAlert('error', 'Failed to copy code: ' + err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copies the full response content to the clipboard in Markdown format
|
||||||
|
function copyFullResponse(messageElement) {
|
||||||
|
const markdownContent = convertToMarkdown(messageElement);
|
||||||
|
navigator.clipboard.writeText(markdownContent).then(() => {
|
||||||
|
displayAlert('success', 'Full response copied to clipboard!');
|
||||||
|
}).catch((err) => {
|
||||||
|
displayAlert('error', 'Failed to copy response: ' + err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts the HTML content of the response to Markdown, including language identifier
|
||||||
|
function convertToMarkdown(element) {
|
||||||
|
let markdown = '';
|
||||||
|
const nodes = element.childNodes;
|
||||||
|
|
||||||
|
nodes.forEach(node => {
|
||||||
|
if (node.nodeName === 'P') {
|
||||||
|
markdown += `${node.innerText}\n\n`;
|
||||||
|
} else if (node.nodeName === 'PRE') {
|
||||||
|
const codeBlock = node.querySelector('code');
|
||||||
|
const languageClass = codeBlock.className.match(/language-(\w+)/); // Extract language from class if available
|
||||||
|
const language = languageClass ? languageClass[1] : ''; // Default to empty if no language found
|
||||||
|
const codeText = codeBlock.innerText;
|
||||||
|
|
||||||
|
// Add language identifier to the Markdown code block
|
||||||
|
markdown += `\`\`\`${language}\n${codeText}\n\`\`\`\n\n`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return markdown;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Displays an alert message with animation
|
||||||
|
function displayAlert(type, message) {
|
||||||
|
const alertElement = document.getElementById(`${type}-alert`);
|
||||||
|
alertElement.textContent = message;
|
||||||
|
alertElement.style.display = 'flex'; // Show the alert
|
||||||
|
alertElement.classList.remove('fade-out'); // Remove fade-out class if present
|
||||||
|
alertElement.style.opacity = '1'; // Ensure it's fully visible
|
||||||
|
|
||||||
|
// Automatically hide the alert after 3 seconds with animation
|
||||||
|
setTimeout(() => {
|
||||||
|
alertElement.classList.add('fade-out'); // Add fade-out class to trigger animation
|
||||||
|
setTimeout(() => {
|
||||||
|
alertElement.style.display = 'none'; // Hide after animation finishes
|
||||||
|
}, 500); // Match this time to the animation duration
|
||||||
|
}, 3000); // Show the alert for 3 seconds before hiding
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles error responses based on status code
|
||||||
|
function handleErrorResponse(status) {
|
||||||
|
if (status === 429) {
|
||||||
|
displayAlert('error', 'Sorry, I am currently too busy at the moment!');
|
||||||
|
} else {
|
||||||
|
displayMessage('Error: ' + status, 'assistant');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to reset the conversation on the server
|
||||||
|
async function sendResetRequest() {
|
||||||
|
const response = await fetch('https://infer.x64.world/reset-conversation', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resets the chat messages and optionally displays a success message
|
||||||
|
async function resetChat(displaySuccessMessage = true) {
|
||||||
|
try {
|
||||||
|
const response = await sendResetRequest();
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const messagesContainer = document.getElementById('messages');
|
||||||
|
messagesContainer.innerHTML = ''; // Clear all messages
|
||||||
|
|
||||||
|
if (displaySuccessMessage) {
|
||||||
|
displayAlert('success', 'Messages Cleared!');
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
displayAlert('error', 'Failed to reset conversation. Please try again.');
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
displayAlert('error', 'An error occurred while resetting the conversation.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Sends a message to the chat API
|
// Resets the chat on page load without displaying the success message
|
||||||
async function sendMessage() {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
const messageInput = document.getElementById('messageInput');
|
resetChat(false);
|
||||||
let message = messageInput.value.trim();
|
});
|
||||||
|
|
||||||
if (message === '') return;
|
|
||||||
|
|
||||||
// Encode the message to avoid XSS attacks
|
|
||||||
message = he.encode(message);
|
|
||||||
|
|
||||||
// Display the user's message in the chat
|
|
||||||
displayMessage(message, 'user');
|
|
||||||
messageInput.value = ''; // Clear the input
|
|
||||||
toggleLoading(true); // Show loading indicator
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('https://infer.x64.world/chat', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ message: message })
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
displayMessage(data.content, 'assistant');
|
|
||||||
} else {
|
|
||||||
handleErrorResponse(response.status);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
displayMessage('Error: ' + error.message, 'assistant');
|
|
||||||
} finally {
|
|
||||||
toggleLoading(false); // Hide loading indicator
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggles the loading indicator
|
|
||||||
function toggleLoading(show) {
|
|
||||||
const loadingElement = document.getElementById('loading');
|
|
||||||
loadingElement.style.display = show ? 'block' : 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Displays a message in the chat window
|
|
||||||
function displayMessage(content, sender) {
|
|
||||||
const messages = document.getElementById('messages');
|
|
||||||
const messageElement = document.createElement('div');
|
|
||||||
messageElement.classList.add('message', sender);
|
|
||||||
|
|
||||||
// Decode HTML entities and render Markdown
|
|
||||||
const decodedContent = he.decode(content);
|
|
||||||
const htmlContent = marked(decodedContent);
|
|
||||||
messageElement.innerHTML = htmlContent;
|
|
||||||
|
|
||||||
messages.appendChild(messageElement);
|
|
||||||
messages.scrollTop = messages.scrollHeight; // Scroll to the bottom of the chat
|
|
||||||
|
|
||||||
// Highlight code blocks if any
|
|
||||||
document.querySelectorAll('pre code').forEach((block) => {
|
|
||||||
hljs.highlightBlock(block);
|
|
||||||
if (sender === 'assistant') {
|
|
||||||
addCopyButton(block); // Add copy button to code blocks
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adds a copy button to a code block
|
|
||||||
function addCopyButton(block) {
|
|
||||||
const button = document.createElement('button');
|
|
||||||
button.classList.add('copy-button');
|
|
||||||
button.textContent = 'Copy';
|
|
||||||
button.addEventListener('click', () => copyToClipboard(block));
|
|
||||||
block.parentNode.appendChild(button);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copies code block content to the clipboard
|
|
||||||
function copyToClipboard(block) {
|
|
||||||
const text = block.innerText;
|
|
||||||
navigator.clipboard.writeText(text).then(() => {
|
|
||||||
displayAlert('success', 'The code block was copied to the clipboard!');
|
|
||||||
}).catch((err) => {
|
|
||||||
displayAlert('error', 'Failed to copy code: ' + err);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Displays an alert message
|
|
||||||
function displayAlert(type, message) {
|
|
||||||
const alertElement = document.getElementById(`${type}-alert`);
|
|
||||||
alertElement.textContent = message;
|
|
||||||
alertElement.style.display = 'block';
|
|
||||||
setTimeout(() => {
|
|
||||||
alertElement.style.display = 'none';
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handles error responses based on status code
|
|
||||||
function handleErrorResponse(status) {
|
|
||||||
if (status === 429) {
|
|
||||||
displayAlert('error', 'Sorry, I am currently too busy at the moment!');
|
|
||||||
} else {
|
|
||||||
displayMessage('Error: ' + status, 'assistant');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
205
views/chat.ejs
205
views/chat.ejs
@ -92,210 +92,7 @@
|
|||||||
<script src="https://cdn.jsdelivr.net/npm/he@1.2.0/he.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/he@1.2.0/he.min.js"></script>
|
||||||
|
|
||||||
<!-- Inline JavaScript -->
|
<!-- Inline JavaScript -->
|
||||||
<script>
|
<script src="<%= process.env.HOST_URL %>/js/chat.js"></script>
|
||||||
// Handles key down event to send message on Enter
|
|
||||||
function handleKeyDown(event) {
|
|
||||||
if (event.key === 'Enter' && !event.shiftKey) {
|
|
||||||
event.preventDefault();
|
|
||||||
sendMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sends a message to the chat API
|
|
||||||
async function sendMessage() {
|
|
||||||
const messageInput = document.getElementById('messageInput');
|
|
||||||
let message = messageInput.value.trim();
|
|
||||||
|
|
||||||
if (message === '') return;
|
|
||||||
|
|
||||||
// Encode the message to avoid XSS attacks
|
|
||||||
message = he.encode(message);
|
|
||||||
|
|
||||||
// Display the user's message in the chat
|
|
||||||
displayMessage(message, 'user');
|
|
||||||
messageInput.value = ''; // Clear the input
|
|
||||||
toggleLoading(true); // Show loading indicator
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('https://infer.x64.world/chat', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ message: message })
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
displayMessage(data.content, 'assistant');
|
|
||||||
} else {
|
|
||||||
handleErrorResponse(response.status);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
displayMessage('Error: ' + error.message, 'assistant');
|
|
||||||
} finally {
|
|
||||||
toggleLoading(false); // Hide loading indicator
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggles the loading indicator
|
|
||||||
function toggleLoading(show) {
|
|
||||||
const loadingElement = document.getElementById('loading');
|
|
||||||
loadingElement.style.display = show ? 'block' : 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Displays a message in the chat window
|
|
||||||
function displayMessage(content, sender) {
|
|
||||||
const messages = document.getElementById('messages');
|
|
||||||
const messageElement = document.createElement('div');
|
|
||||||
messageElement.classList.add('message', sender);
|
|
||||||
|
|
||||||
// Decode HTML entities and render Markdown
|
|
||||||
const decodedContent = he.decode(content);
|
|
||||||
const htmlContent = marked(decodedContent);
|
|
||||||
messageElement.innerHTML = htmlContent;
|
|
||||||
|
|
||||||
messages.appendChild(messageElement);
|
|
||||||
messages.scrollTop = messages.scrollHeight; // Scroll to the bottom of the chat
|
|
||||||
|
|
||||||
// Highlight code blocks if any
|
|
||||||
document.querySelectorAll('pre code').forEach((block) => {
|
|
||||||
hljs.highlightElement(block);
|
|
||||||
if (sender === 'assistant') {
|
|
||||||
addCopyButton(block); // Add copy button to code blocks
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add "Copy Full Response" button after each assistant response
|
|
||||||
if (sender === 'assistant') {
|
|
||||||
addCopyFullResponseButton(messages, messageElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adds a copy button to a code block
|
|
||||||
function addCopyButton(block) {
|
|
||||||
const button = document.createElement('button');
|
|
||||||
button.classList.add('copy-button');
|
|
||||||
button.textContent = 'Copy';
|
|
||||||
button.addEventListener('click', () => copyToClipboard(block));
|
|
||||||
block.parentNode.appendChild(button);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adds "Copy Full Response" button below the assistant response
|
|
||||||
function addCopyFullResponseButton(messagesContainer, messageElement) {
|
|
||||||
const copyFullResponseButton = document.createElement('button');
|
|
||||||
copyFullResponseButton.classList.add('copy-button');
|
|
||||||
copyFullResponseButton.textContent = 'Copy Full Response';
|
|
||||||
copyFullResponseButton.addEventListener('click', () => copyFullResponse(messageElement));
|
|
||||||
|
|
||||||
messagesContainer.appendChild(copyFullResponseButton);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copies code block content to the clipboard
|
|
||||||
function copyToClipboard(block) {
|
|
||||||
const text = block.innerText;
|
|
||||||
navigator.clipboard.writeText(text).then(() => {
|
|
||||||
displayAlert('success', 'The code block was copied to the clipboard!');
|
|
||||||
}).catch((err) => {
|
|
||||||
displayAlert('error', 'Failed to copy code: ' + err);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copies the full response content to the clipboard in Markdown format
|
|
||||||
function copyFullResponse(messageElement) {
|
|
||||||
const markdownContent = convertToMarkdown(messageElement);
|
|
||||||
navigator.clipboard.writeText(markdownContent).then(() => {
|
|
||||||
displayAlert('success', 'Full response copied to clipboard!');
|
|
||||||
}).catch((err) => {
|
|
||||||
displayAlert('error', 'Failed to copy response: ' + err);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converts the HTML content of the response to Markdown, including language identifier
|
|
||||||
function convertToMarkdown(element) {
|
|
||||||
let markdown = '';
|
|
||||||
const nodes = element.childNodes;
|
|
||||||
|
|
||||||
nodes.forEach(node => {
|
|
||||||
if (node.nodeName === 'P') {
|
|
||||||
markdown += `${node.innerText}\n\n`;
|
|
||||||
} else if (node.nodeName === 'PRE') {
|
|
||||||
const codeBlock = node.querySelector('code');
|
|
||||||
const languageClass = codeBlock.className.match(/language-(\w+)/); // Extract language from class if available
|
|
||||||
const language = languageClass ? languageClass[1] : ''; // Default to empty if no language found
|
|
||||||
const codeText = codeBlock.innerText;
|
|
||||||
|
|
||||||
// Add language identifier to the Markdown code block
|
|
||||||
markdown += `\`\`\`${language}\n${codeText}\n\`\`\`\n\n`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return markdown;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Displays an alert message with animation
|
|
||||||
function displayAlert(type, message) {
|
|
||||||
const alertElement = document.getElementById(`${type}-alert`);
|
|
||||||
alertElement.textContent = message;
|
|
||||||
alertElement.style.display = 'flex'; // Show the alert
|
|
||||||
alertElement.classList.remove('fade-out'); // Remove fade-out class if present
|
|
||||||
alertElement.style.opacity = '1'; // Ensure it's fully visible
|
|
||||||
|
|
||||||
// Automatically hide the alert after 3 seconds with animation
|
|
||||||
setTimeout(() => {
|
|
||||||
alertElement.classList.add('fade-out'); // Add fade-out class to trigger animation
|
|
||||||
setTimeout(() => {
|
|
||||||
alertElement.style.display = 'none'; // Hide after animation finishes
|
|
||||||
}, 500); // Match this time to the animation duration
|
|
||||||
}, 3000); // Show the alert for 3 seconds before hiding
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handles error responses based on status code
|
|
||||||
function handleErrorResponse(status) {
|
|
||||||
if (status === 429) {
|
|
||||||
displayAlert('error', 'Sorry, I am currently too busy at the moment!');
|
|
||||||
} else {
|
|
||||||
displayMessage('Error: ' + status, 'assistant');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to reset the conversation on the server
|
|
||||||
async function sendResetRequest() {
|
|
||||||
const response = await fetch('https://infer.x64.world/reset-conversation', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resets the chat messages and optionally displays a success message
|
|
||||||
async function resetChat(displaySuccessMessage = true) {
|
|
||||||
try {
|
|
||||||
const response = await sendResetRequest();
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const messagesContainer = document.getElementById('messages');
|
|
||||||
messagesContainer.innerHTML = ''; // Clear all messages
|
|
||||||
|
|
||||||
if (displaySuccessMessage) {
|
|
||||||
displayAlert('success', 'Messages Cleared!');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
displayAlert('error', 'Failed to reset conversation. Please try again.');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
displayAlert('error', 'An error occurred while resetting the conversation.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resets the chat on page load without displaying the success message
|
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
|
||||||
resetChat(false);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user