2025-06-19 17:39:45 +08:00

560 lines
16 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Chat Client-ID Aware</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
color: #333;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
width: 100%;
flex: 1;
display: flex;
flex-direction: column;
}
.header {
text-align: center;
margin-bottom: 30px;
color: white;
}
.header h1 {
font-size: 2.5rem;
font-weight: 300;
margin-bottom: 10px;
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
.header p {
font-size: 1.1rem;
opacity: 0.9;
}
.chat-container {
background: white;
border-radius: 15px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
overflow: hidden;
display: flex;
flex-direction: column;
height: calc(100vh - 200px);
min-height: 500px;
}
.client-id-section {
background: #f8f9fa;
padding: 20px;
border-bottom: 1px solid #e9ecef;
display: flex;
align-items: center;
gap: 15px;
flex-wrap: wrap;
}
.client-id-input {
display: flex;
align-items: center;
gap: 10px;
flex: 1;
min-width: 300px;
}
.client-id-input input {
flex: 1;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 8px;
font-size: 14px;
transition: border-color 0.3s ease;
min-width: 200px;
}
.client-id-input input:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
white-space: nowrap;
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}
.btn-primary:active {
transform: translateY(0);
}
.client-id-display {
background: #e3f2fd;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
color: #1976d2;
font-weight: 500;
border: 1px solid #bbdefb;
}
.chat-area {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
#chatbox {
flex: 1;
padding: 20px;
overflow-y: auto;
background: #fafafa;
scroll-behavior: smooth;
}
#chatbox::-webkit-scrollbar {
width: 8px;
}
#chatbox::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
#chatbox::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}
#chatbox::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
.message {
margin-bottom: 15px;
padding: 12px 16px;
border-radius: 12px;
max-width: 80%;
word-wrap: break-word;
animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.user-msg {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin-left: auto;
text-align: right;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.2);
}
.ai-msg {
background: white;
color: #333;
margin-right: auto;
text-align: left;
border: 1px solid #e9ecef;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.server-info {
background: #fff3cd;
color: #856404;
border: 1px solid #ffeaa7;
text-align: center;
font-size: 0.9rem;
font-style: italic;
margin: 10px auto;
max-width: 90%;
}
.input-section {
padding: 20px;
background: white;
border-top: 1px solid #e9ecef;
display: flex;
gap: 15px;
align-items: center;
}
#userInput {
flex: 1;
padding: 15px 20px;
border: 2px solid #e9ecef;
border-radius: 25px;
font-size: 16px;
transition: all 0.3s ease;
min-width: 200px;
}
#userInput:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.send-btn {
padding: 15px 30px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 25px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
white-space: nowrap;
}
.send-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}
.send-btn:active {
transform: translateY(0);
}
.send-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
/* Responsive Design */
@media (max-width: 768px) {
.container {
padding: 10px;
}
.header h1 {
font-size: 2rem;
}
.client-id-section {
flex-direction: column;
align-items: stretch;
gap: 10px;
}
.client-id-input {
min-width: auto;
}
.chat-container {
height: calc(100vh - 150px);
}
.input-section {
flex-direction: column;
gap: 10px;
}
.message {
max-width: 95%;
}
}
@media (max-width: 480px) {
.header h1 {
font-size: 1.5rem;
}
.header p {
font-size: 1rem;
}
.client-id-section {
padding: 15px;
}
.btn {
padding: 10px 20px;
font-size: 13px;
}
#userInput {
padding: 12px 16px;
font-size: 14px;
}
.send-btn {
padding: 12px 24px;
font-size: 14px;
}
}
/* Loading animation */
.typing-indicator {
display: flex;
gap: 4px;
padding: 12px 16px;
background: white;
border-radius: 12px;
margin-right: auto;
max-width: 60px;
}
.typing-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #c1c1c1;
animation: typing 1.4s infinite ease-in-out;
}
.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes typing {
0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
40% { transform: scale(1); opacity: 1; }
}
/* Connection status */
.connection-status {
position: fixed;
top: 20px;
right: 20px;
padding: 8px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
z-index: 1000;
transition: all 0.3s ease;
}
.status-connected {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status-disconnected {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>AI Chat Assistant</h1>
<p>Intelligent conversation with client-aware sessions</p>
</div>
<div class="chat-container">
<div class="client-id-section">
<div class="client-id-input">
<input type="text" id="clientId" placeholder="Enter your Client ID..." />
<button class="btn btn-primary" onclick="setClientId()">Set Client ID</button>
</div>
<div class="client-id-display" id="clientIdDisplay">
Current Client ID: <span></span>
</div>
</div>
<div class="chat-area">
<div id="chatbox"></div>
</div>
<div class="input-section">
<input type="text" id="userInput" placeholder="Type your message..." />
<button class="send-btn" onclick="sendMessage()">Send</button>
</div>
</div>
</div>
<div class="connection-status" id="connectionStatus" style="display: none;"></div>
<script>
const chatbox = document.getElementById('chatbox');
const userInput = document.getElementById('userInput');
const clientIdInput = document.getElementById('clientId');
const clientIdDisplaySpan = document.querySelector('#clientIdDisplay span');
const connectionStatus = document.getElementById('connectionStatus');
const sendBtn = document.querySelector('.send-btn');
let ws; // Declare ws here, will be initialized later
let myClientId = localStorage.getItem('aiChatClientId');
function updateConnectionStatus(isConnected, message = '') {
connectionStatus.style.display = 'block';
if (isConnected) {
connectionStatus.className = 'connection-status status-connected';
connectionStatus.textContent = 'Connected';
} else {
connectionStatus.className = 'connection-status status-disconnected';
connectionStatus.textContent = message || 'Disconnected';
}
}
function initializeWebSocket(clientId) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.close();
}
// const serverAddress = 'ws://101.89.151.141:9000'; // Or 'ws://localhost:9000'
// const serverAddress = 'ws://127.0.0.1:9000'; // Or 'ws://localhost:9000'
const serverAddress = 'ws://106.15.107.142:9000'; // Or 'ws://localhost:9000'
const wsUrl = `${serverAddress}?clientId=${encodeURIComponent(clientId || 'unknown')}`;
ws = new WebSocket(wsUrl);
console.log(`Attempting to connect to: ${wsUrl}`);
ws.onopen = () => {
updateConnectionStatus(true);
addMessage(`Connected to server with Client ID: ${clientId || 'unknown'}`, "server-info");
sendBtn.disabled = false;
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
let sender = "Server";
switch (message.type) {
case 'AI_RESPONSE':
addMessage(`AI: ${message.payload.text}`, 'ai-msg');
break;
case 'ERROR':
addMessage(`${sender} Error: ${message.payload.message}`, 'server-info');
console.error("Server error:", message.payload);
break;
default:
addMessage(`Unknown message type '${message.type}': ${event.data}`, 'server-info');
}
};
ws.onclose = (event) => {
let reason = "";
if (event.code) reason += ` (Code: ${event.code}`;
if (event.reason) reason += ` Reason: ${event.reason}`;
if (reason) reason += ")";
updateConnectionStatus(false, `Disconnected${reason}`);
addMessage(`Disconnected from server.${reason}`, "server-info");
sendBtn.disabled = true;
};
ws.onerror = (error) => {
updateConnectionStatus(false, 'Connection Error');
addMessage("WebSocket error. Check console.", "server-info");
console.error('WebSocket Error:', error);
sendBtn.disabled = true;
};
}
if (myClientId) {
clientIdDisplaySpan.textContent = myClientId;
clientIdInput.value = myClientId;
initializeWebSocket(myClientId);
} else {
addMessage("Please set a Client ID to connect.", "server-info");
sendBtn.disabled = true;
}
function setClientId() {
const newClientId = clientIdInput.value.trim();
if (newClientId === '') {
alert('Please enter a valid Client ID!');
return;
}
myClientId = newClientId;
localStorage.setItem('aiChatClientId', myClientId);
clientIdDisplaySpan.textContent = myClientId;
addMessage(`Client ID set to: ${myClientId}. Reconnecting...`, "server-info");
initializeWebSocket(myClientId);
}
function sendMessage() {
if (!myClientId) {
alert('Please set a Client ID first!');
return;
}
if (!ws || ws.readyState !== WebSocket.OPEN) {
alert('Not connected to the server. Please set Client ID or check connection.');
return;
}
const text = userInput.value;
if (text.trim() === '') return;
addMessage(`You: ${text}`, 'user-msg');
const message = {
type: "USER_INPUT",
payload: {
client_id: myClientId,
text: text
}
};
ws.send(JSON.stringify(message));
userInput.value = '';
}
userInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendMessage();
}
});
clientIdInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
setClientId();
}
});
function addMessage(text, className) {
const p = document.createElement('p');
p.textContent = text;
if (className) p.className = `message ${className}`;
chatbox.appendChild(p);
chatbox.scrollTop = chatbox.scrollHeight;
}
// Handle window resize
window.addEventListener('resize', function() {
// The CSS will handle most responsive behavior automatically
// This is just for any additional JavaScript-based responsive features
});
</script>
</body>
</html>