add AI page

This commit is contained in:
Raven Scott 2024-10-02 03:32:27 -04:00
parent 775e146a68
commit c602d5b8f8
3 changed files with 633 additions and 0 deletions

10
app.js
View File

@ -175,6 +175,16 @@ app.get('/contact', (req, res) => {
}); });
}); });
// Contact Route (Render the contact form)
app.get('/chat', (req, res) => {
res.render('chat', {
title: `Chat ${process.env.OWNER_NAME}`,
msg: undefined,
menuItems // Pass the menu items to the view
});
});
// Blog Post Route // Blog Post Route
app.get('/blog/:slug', (req, res) => { app.get('/blog/:slug', (req, res) => {
const slug = req.params.slug; const slug = req.params.slug;

207
public/js/chat.js Normal file
View File

@ -0,0 +1,207 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta and Title -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Chat with <%= process.env.OWNER_NAME %>'s AI assistant">
<title>Chat with AI - <%= process.env.OWNER_NAME %></title>
<!-- 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">
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="<%= process.env.HOST_URL %>"><%= process.env.SITE_NAME %></a>
<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 -->
<header class="d-flex align-items-center justify-content-center text-center py-5">
<div class="container">
<h2 class="mb-4 text-white">Chat with <%= process.env.OWNER_NAME %>'s AI</h2>
<p class="lead text-white">Ask any question, and the AI will respond to you!</p>
<!-- Chat Box -->
<div id="chatBox" class="bg-light p-4 rounded text-dark mb-4">
<div id="messages" class="mb-3" style="max-height: 300px; overflow-y: scroll;"></div>
<textarea id="messageInput" class="form-control mb-2 bg-dark text-white" rows="3" placeholder="Type your message here..." onkeydown="handleKeyDown(event)"></textarea>
<button class="btn btn-primary w-100" onclick="sendMessage()">Send Message</button>
<!-- Loading Indicator -->
<div id="loading" class="text-center mt-3" style="display: none;">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
<!-- Alert Messages -->
<div id="success-alert" class="alert alert-success mt-3" style="display: none;"></div>
<div id="error-alert" class="alert alert-danger mt-3" style="display: none;"></div>
</div>
</header>
<!-- Footer -->
<footer class="text-white text-center py-4">
<div class="container">
<p class="mb-0">&copy; 2024 <%= process.env.OWNER_NAME %>. All rights reserved.</p>
</div>
</footer>
<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<!-- Additional Libraries -->
<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 -->
<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.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>

416
views/chat.ejs Normal file
View File

@ -0,0 +1,416 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta and Title -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Chat with <%= process.env.OWNER_NAME %>'s AI assistant">
<title>Chat with AI - <%= process.env.OWNER_NAME %>
</title>
<!-- 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">
<!-- Highlight.js CSS for Dark Theme -->
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/atom-one-dark.min.css">
<!-- Custom CSS -->
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
/* Prevent page scroll */
background-color: #121212;
color: white;
font-family: Arial, sans-serif;
}
.bg-dark {
background-color: #121212 !important;
}
textarea::placeholder {
color: #007bff;
/* Blue color to match the button */
opacity: 0;
/* Ensures the placeholder text is fully opaque */
}
.chat-container {
display: flex;
flex-direction: column;
height: 100%;
}
.navbar {
flex-shrink: 0;
}
.chat-box {
display: flex;
flex-direction: column;
flex-grow: 1;
background-color: #1e1e1e;
overflow: hidden;
/* Ensures no overflow beyond screen */
}
.messages {
flex-grow: 1;
overflow-y: auto;
/* Enable scrolling */
padding: 20px;
background-color: #2e2e2e;
border-radius: 5px;
display: flex;
flex-direction: column;
}
.message {
margin-bottom: 10px;
padding: 10px;
border-radius: 5px;
}
.message.user {
background-color: #3a3a3a;
color: white;
}
.message.assistant {
background-color: #282828;
color: #f1f1f1;
}
.form-control {
background-color: #2e2e2e;
color: white;
border-color: #444;
}
.form-control:focus {
background-color: #2e2e2e;
color: white;
border-color: #888;
}
pre code {
background-color: #1e1e1e;
color: #f8f8f2;
padding: 10px;
border-radius: 5px;
display: block;
white-space: pre-wrap;
word-wrap: break-word;
}
.copy-button {
background-color: #444;
color: white;
border: none;
cursor: pointer;
padding: 5px;
margin-top: 5px;
display: inline-block;
}
.copy-button:hover {
background-color: #555;
}
.alert {
margin-top: 10px;
}
/* Buttons side-by-side with sticky behavior */
.input-area {
background-color: #1e1e1e;
padding: 10px 20px;
z-index: 10;
flex-shrink: 0;
color: white;
/* Prevents shrinking */
}
.alert {
margin-top: 10px;
text-align: center;
/* Center the text content */
display: flex;
/* Make sure the alert is a flex container */
justify-content: center;
/* Center flex items horizontally */
align-items: center;
/* Center flex items vertically (if needed) */
}
/* WebKit-based browsers (Chrome, Safari, Edge) */
.messages::-webkit-scrollbar {
width: 8px;
/* Make the scrollbar thin */
}
.messages::-webkit-scrollbar-track {
background: #2e2e2e;
/* Background color for the scrollbar track */
}
.messages::-webkit-scrollbar-thumb {
background-color: #007bff;
/* Blue color for the scrollbar */
border-radius: 10px;
/* Rounded corners for the scrollbar thumb */
border: 2px solid #2e2e2e;
/* Adds space around the thumb */
}
/* Firefox */
.messages {
scrollbar-width: thin;
/* Make the scrollbar thin in Firefox */
scrollbar-color: #007bff #2e2e2e;
/* Blue thumb with dark track */
}
.input-area .btn-primary,
.input-area .btn-secondary {
width: 49%;
}
.btn-primary {
background-color: #007bff;
border-color: #007bff;
}
.btn-primary:hover {
background-color: #0056b3;
border-color: #0056b3;
}
.btn-secondary {
background-color: #6c757d;
border-color: #6c757d;
}
.btn-secondary:hover {
background-color: #5a6268;
border-color: #545b62;
}
.footer {
flex-shrink: 0;
text-align: center;
padding: 10px;
background-color: #121212;
}
</style>
<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">
<div class="chat-container">
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="<%= process.env.HOST_URL %>">
<%= process.env.SITE_NAME %>
</a>
<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>
<!-- Alert Messages -->
<div id="success-alert" class="alert alert-success mt-3" style="display: none;"></div>
<div id="error-alert" class="alert alert-danger mt-3" style="display: none;"></div>
<!-- Chat Box -->
<div class="chat-box">
<div id="messages" class="messages"></div>
<!-- Input area with sticky behavior -->
<div class="input-area">
<textarea id="messageInput" class="form-control mb-2" rows="3" placeholder=""
onkeydown="handleKeyDown(event)"></textarea>
<!-- Buttons side by side -->
<div class="d-flex justify-content-between">
<button class="btn btn-primary" onclick="sendMessage()">Send Message</button>
<button class="btn btn-secondary" onclick="resetChat()">Reset Chat</button>
</div>
</div>
<!-- Loading Indicator -->
<div id="loading" class="text-center mt-3" style="display: none;">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
<!-- Footer -->
<footer class="footer">
<p class="mb-0">&copy; 2024 <%= process.env.OWNER_NAME %>. All rights reserved.</p>
</footer>
</div>
<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<!-- Additional Libraries -->
<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 -->
<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
}
});
}
// 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');
}
}
// Resets the chat messages
function resetChat() {
const messagesContainer = document.getElementById('messages');
messagesContainer.innerHTML = ''; // Clear all messages
displayAlert('success', 'Messages Cleared!');
}
</script>
</body>
</html>