chat.html•13.5 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RAGmonsters Chat</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<!-- Add marked.js for Markdown rendering -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
body {
padding-top: 2rem;
background-color: #f8f9fa;
}
.chat-container {
height: calc(100vh - 200px);
display: flex;
flex-direction: column;
}
.chat-messages {
flex-grow: 1;
overflow-y: auto;
padding: 1rem;
background-color: white;
border: 1px solid #dee2e6;
border-radius: 0.25rem;
margin-bottom: 1rem;
}
.message {
margin-bottom: 1rem;
padding: 0.75rem;
border-radius: 0.5rem;
max-width: 80%;
}
.user-message {
background-color: #d1ecf1;
margin-left: auto;
text-align: right;
}
.assistant-message {
background-color: #f8f9fa;
margin-right: auto;
}
.tool-call {
background-color: #fff3cd;
border: 1px solid #ffeeba;
border-radius: 0.25rem;
padding: 0.5rem;
margin: 0.5rem 0;
font-family: monospace;
white-space: pre-wrap;
font-size: 0.9rem;
}
.tool-result {
background-color: #d4edda;
border: 1px solid #c3e6cb;
border-radius: 0.25rem;
padding: 0.5rem;
margin: 0.5rem 0;
font-family: monospace;
white-space: pre-wrap;
font-size: 0.9rem;
}
.loading {
display: flex;
justify-content: center;
align-items: center;
padding: 1rem;
}
.message-time {
font-size: 0.75rem;
color: #6c757d;
margin-top: 0.25rem;
}
.nav-link.active {
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<header class="pb-3 mb-4 border-bottom">
<div class="d-flex align-items-center">
<h1 class="display-5 fw-bold">RAGmonsters Chat</h1>
<span class="ms-auto badge bg-primary">MCP + LLM</span>
</div>
<nav class="mt-2">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link" href="index.html">Explorer</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="chat.html">Chat</a>
</li>
</ul>
</nav>
</header>
<div class="row">
<div class="col-md-12">
<div class="chat-container">
<div class="chat-messages" id="chat-messages">
<div class="message assistant-message">
<div class="message-content">
Hello! I'm your RAGmonsters assistant. I can help you explore the monster database. What would you like to know?
</div>
<div class="message-time">
Just now
</div>
</div>
</div>
<div class="input-group">
<input type="text" id="message-input" class="form-control" placeholder="Type your message here..." aria-label="Message">
<button class="btn btn-primary" type="button" id="send-button">
<i class="bi bi-send"></i> Send
</button>
</div>
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h5>Example Questions</h5>
</div>
<div class="card-body">
<p>Try asking questions like:</p>
<ul>
<li>What monsters live in the Volcanic Mountains?</li>
<li>Tell me about the monster called Abyssalurk</li>
<li>What are the strongest monsters in the database?</li>
<li>Which monsters are rare?</li>
<li>What are the abilities of Flameburst?</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<script>
// DOM elements
const chatMessages = document.getElementById('chat-messages');
const messageInput = document.getElementById('message-input');
const sendButton = document.getElementById('send-button');
// Conversation state
let conversationId = null;
let isProcessing = false;
// Event listeners
document.addEventListener('DOMContentLoaded', initialize);
sendButton.addEventListener('click', sendMessage);
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
// Initialize the chat
function initialize() {
// Scroll to bottom of chat
scrollToBottom();
// Focus on input
messageInput.focus();
}
// Send a message to the API
async function sendMessage() {
// Get message text
const messageText = messageInput.value.trim();
// Don't send empty messages
if (!messageText || isProcessing) {
return;
}
// Clear input
messageInput.value = '';
// Add user message to chat
addMessage(messageText, 'user');
// Show loading indicator
showLoading();
// Set processing flag
isProcessing = true;
try {
// Send message to API
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: messageText,
conversationId
})
});
// Remove loading indicator
removeLoading();
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
// Update conversation ID
conversationId = data.conversationId;
// If there were tool calls, show them
if (data.toolCalls && data.toolCalls.length > 0) {
// Add a message explaining the tool usage
addMessage('I need to look that up in the monster database...', 'assistant');
// Show each tool call and its result
data.toolCalls.forEach((toolCall, index) => {
const toolResult = data.toolResults[index];
// Add tool call
const toolCallElement = document.createElement('div');
toolCallElement.className = 'tool-call';
toolCallElement.innerHTML = `<strong>Using tool:</strong> ${toolCall.function.name}
<strong>With arguments:</strong> ${toolCall.function.arguments}`;
chatMessages.appendChild(toolCallElement);
// Add tool result
const toolResultElement = document.createElement('div');
toolResultElement.className = 'tool-result';
if (toolResult.error) {
toolResultElement.innerHTML = `<strong>Error:</strong> ${toolResult.error}`;
} else {
let resultContent = '';
if (toolResult.result.content && Array.isArray(toolResult.result.content)) {
// Parse JSON content if possible
try {
const parsedResults = toolResult.result.content.map(item => {
try {
return JSON.parse(item.text);
} catch (e) {
return item.text;
}
});
resultContent = `<strong>Result:</strong> ${JSON.stringify(parsedResults, null, 2)}`;
} catch (e) {
resultContent = `<strong>Result:</strong> ${JSON.stringify(toolResult.result, null, 2)}`;
}
} else {
resultContent = `<strong>Result:</strong> ${JSON.stringify(toolResult.result, null, 2)}`;
}
toolResultElement.innerHTML = resultContent;
}
chatMessages.appendChild(toolResultElement);
});
}
// Add assistant response
addMessage(data.message, 'assistant');
} catch (error) {
console.error('Error sending message:', error);
removeLoading();
addMessage(`Sorry, there was an error processing your request: ${error.message}`, 'assistant');
} finally {
// Reset processing flag
isProcessing = false;
// Scroll to bottom
scrollToBottom();
}
}
// Add a message to the chat
function addMessage(text, role) {
const messageElement = document.createElement('div');
messageElement.className = `message ${role}-message`;
const contentElement = document.createElement('div');
contentElement.className = 'message-content';
contentElement.innerHTML = formatMessage(text);
messageElement.appendChild(contentElement);
const timeElement = document.createElement('div');
timeElement.className = 'message-time';
timeElement.textContent = new Date().toLocaleTimeString();
messageElement.appendChild(timeElement);
chatMessages.appendChild(messageElement);
scrollToBottom();
}
// Format message text (convert URLs to links and render Markdown)
function formatMessage(text) {
try {
// First convert URLs to links
const urlRegex = /(https?:\/\/[^\s]+)/g;
const textWithLinks = text.replace(urlRegex, url => `<a href="${url}" target="_blank">${url}</a>`);
// Then render Markdown
return marked.parse(textWithLinks);
} catch (error) {
console.error('Error rendering Markdown:', error);
// Fallback to basic URL conversion if Markdown rendering fails
const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, url => `<a href="${url}" target="_blank">${url}</a>`);
}
}
// Show loading indicator
function showLoading() {
const loadingElement = document.createElement('div');
loadingElement.className = 'loading';
loadingElement.id = 'loading-indicator';
loadingElement.innerHTML = `
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
`;
chatMessages.appendChild(loadingElement);
scrollToBottom();
}
// Remove loading indicator
function removeLoading() {
const loadingElement = document.getElementById('loading-indicator');
if (loadingElement) {
loadingElement.remove();
}
}
// Scroll to bottom of chat
function scrollToBottom() {
chatMessages.scrollTop = chatMessages.scrollHeight;
}
</script>
</body>
</html>