This commit is contained in:
parent
d5e823771b
commit
dae15410ab
3 changed files with 607 additions and 46 deletions
|
|
@ -17,7 +17,93 @@
|
|||
color: #eaeaea;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
}
|
||||
/* Sidebar styles */
|
||||
.sidebar {
|
||||
width: 280px;
|
||||
background: #16213e;
|
||||
border-right: 1px solid #0f3460;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
position: fixed;
|
||||
left: 0;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
}
|
||||
.sidebar-header {
|
||||
padding: 1rem;
|
||||
border-bottom: 1px solid #0f3460;
|
||||
}
|
||||
.sidebar-header h1 {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
.new-chat-btn {
|
||||
width: 100%;
|
||||
background: #e94560;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
padding: 0.75rem 1rem;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
.new-chat-btn:hover {
|
||||
background: #d63447;
|
||||
}
|
||||
.new-chat-btn:disabled {
|
||||
background: #555;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.conversations-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
.conversation-item {
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
margin-bottom: 0.25rem;
|
||||
transition: background 0.2s;
|
||||
border: 1px solid transparent;
|
||||
}
|
||||
.conversation-item:hover {
|
||||
background: #1a1a2e;
|
||||
}
|
||||
.conversation-item.active {
|
||||
background: #0f3460;
|
||||
border-color: #e94560;
|
||||
}
|
||||
.conversation-item .preview {
|
||||
font-size: 0.875rem;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
opacity: 0.9;
|
||||
}
|
||||
.conversation-item .meta {
|
||||
font-size: 0.75rem;
|
||||
opacity: 0.6;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
.conversation-item .message-count {
|
||||
float: right;
|
||||
font-size: 0.7rem;
|
||||
background: #0f3460;
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 10px;
|
||||
}
|
||||
.main-content {
|
||||
margin-left: 280px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
}
|
||||
header {
|
||||
background: #16213e;
|
||||
|
|
@ -119,30 +205,201 @@
|
|||
.loading {
|
||||
opacity: 0.6;
|
||||
}
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
color: #888;
|
||||
text-align: center;
|
||||
}
|
||||
.empty-state p {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
/* Responsive sidebar toggle */
|
||||
.sidebar-toggle {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 1rem;
|
||||
left: 1rem;
|
||||
z-index: 200;
|
||||
background: #e94560;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
padding: 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
@media (max-width: 768px) {
|
||||
.sidebar {
|
||||
transform: translateX(-100%);
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
.sidebar.open {
|
||||
transform: translateX(0);
|
||||
}
|
||||
.sidebar-toggle {
|
||||
display: block;
|
||||
}
|
||||
.main-content {
|
||||
margin-left: 0;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>disinto-chat</h1>
|
||||
</header>
|
||||
<main>
|
||||
<div id="messages">
|
||||
<div class="message system">
|
||||
<div class="role">system</div>
|
||||
<div class="content">Welcome to disinto-chat. Type a message to start chatting with Claude.</div>
|
||||
</div>
|
||||
<button class="sidebar-toggle" id="sidebar-toggle">☰</button>
|
||||
<aside class="sidebar" id="sidebar">
|
||||
<div class="sidebar-header">
|
||||
<h1>disinto-chat</h1>
|
||||
<button class="new-chat-btn" id="new-chat-btn">+ New Chat</button>
|
||||
</div>
|
||||
<form class="input-area">
|
||||
<textarea name="message" placeholder="Type your message..." required></textarea>
|
||||
<button type="submit" id="send-btn">Send</button>
|
||||
</form>
|
||||
</main>
|
||||
<div class="conversations-list" id="conversations-list">
|
||||
<!-- Conversations will be loaded here -->
|
||||
</div>
|
||||
</aside>
|
||||
<div class="main-content">
|
||||
<header>
|
||||
<h1>disinto-chat</h1>
|
||||
</header>
|
||||
<main>
|
||||
<div id="messages">
|
||||
<div class="message system">
|
||||
<div class="role">system</div>
|
||||
<div class="content">Welcome to disinto-chat. Type a message to start chatting with Claude.</div>
|
||||
</div>
|
||||
</div>
|
||||
<form class="input-area" id="chat-form">
|
||||
<textarea name="message" placeholder="Type your message..." required></textarea>
|
||||
<button type="submit" id="send-btn">Send</button>
|
||||
</form>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// State
|
||||
let currentConversationId = null;
|
||||
let conversations = [];
|
||||
|
||||
// DOM elements
|
||||
const messagesDiv = document.getElementById('messages');
|
||||
const sendBtn = document.getElementById('send-btn');
|
||||
const textarea = document.querySelector('textarea');
|
||||
const conversationsList = document.getElementById('conversations-list');
|
||||
const newChatBtn = document.getElementById('new-chat-btn');
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
const sidebarToggle = document.getElementById('sidebar-toggle');
|
||||
|
||||
// Load conversations list
|
||||
async function loadConversations() {
|
||||
try {
|
||||
const response = await fetch('/chat/history');
|
||||
if (response.ok) {
|
||||
conversations = await response.json();
|
||||
renderConversationsList();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load conversations:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Render conversations list
|
||||
function renderConversationsList() {
|
||||
conversationsList.innerHTML = '';
|
||||
|
||||
if (conversations.length === 0) {
|
||||
conversationsList.innerHTML = '<div style="padding: 1rem; color: #888; text-align: center; font-size: 0.875rem;">No conversations yet</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
conversations.forEach(conv => {
|
||||
const item = document.createElement('div');
|
||||
item.className = 'conversation-item';
|
||||
if (conv.id === currentConversationId) {
|
||||
item.classList.add('active');
|
||||
}
|
||||
item.dataset.conversationId = conv.id;
|
||||
|
||||
const previewDiv = document.createElement('div');
|
||||
previewDiv.className = 'preview';
|
||||
previewDiv.textContent = conv.preview || '(empty)';
|
||||
|
||||
const metaDiv = document.createElement('div');
|
||||
metaDiv.className = 'meta';
|
||||
const date = conv.created_at ? new Date(conv.created_at).toLocaleDateString() : '';
|
||||
metaDiv.innerHTML = `${date} <span class="message-count">${conv.message_count || 0} msg${conv.message_count !== 1 ? 's' : ''}</span>`;
|
||||
|
||||
item.appendChild(previewDiv);
|
||||
item.appendChild(metaDiv);
|
||||
|
||||
item.addEventListener('click', () => loadConversation(conv.id));
|
||||
conversationsList.appendChild(item);
|
||||
});
|
||||
}
|
||||
|
||||
// Load a specific conversation
|
||||
async function loadConversation(convId) {
|
||||
// Clear messages
|
||||
messagesDiv.innerHTML = '';
|
||||
|
||||
// Update active state in sidebar
|
||||
document.querySelectorAll('.conversation-item').forEach(item => {
|
||||
item.classList.remove('active');
|
||||
});
|
||||
document.querySelector(`[data-conversation-id="${convId}"]`)?.classList.add('active');
|
||||
|
||||
if (convId === currentConversationId) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentConversationId = convId;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/chat/history/${convId}`);
|
||||
if (response.ok) {
|
||||
const messages = await response.json();
|
||||
if (messages && messages.length > 0) {
|
||||
messages.forEach(msg => {
|
||||
addMessage(msg.role, msg.content);
|
||||
});
|
||||
} else {
|
||||
addSystemMessage('This conversation is empty');
|
||||
}
|
||||
} else {
|
||||
addSystemMessage('Failed to load conversation');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load conversation:', error);
|
||||
addSystemMessage('Error loading conversation');
|
||||
}
|
||||
|
||||
// Close sidebar on mobile
|
||||
if (window.innerWidth <= 768) {
|
||||
sidebar.classList.remove('open');
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new conversation
|
||||
async function createNewConversation() {
|
||||
try {
|
||||
const response = await fetch('/chat/new', { method: 'POST' });
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
currentConversationId = data.conversation_id;
|
||||
messagesDiv.innerHTML = '';
|
||||
addSystemMessage('New conversation started');
|
||||
await loadConversations();
|
||||
} else {
|
||||
addSystemMessage('Failed to create new conversation');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to create new conversation:', error);
|
||||
addSystemMessage('Error creating new conversation');
|
||||
}
|
||||
}
|
||||
|
||||
// Add message to display
|
||||
function addMessage(role, content, streaming = false) {
|
||||
const msgDiv = document.createElement('div');
|
||||
msgDiv.className = `message ${role}`;
|
||||
|
|
@ -155,6 +412,17 @@
|
|||
return msgDiv.querySelector('.content');
|
||||
}
|
||||
|
||||
function addSystemMessage(content) {
|
||||
const msgDiv = document.createElement('div');
|
||||
msgDiv.className = 'message system';
|
||||
msgDiv.innerHTML = `
|
||||
<div class="role">system</div>
|
||||
<div class="content">${escapeHtml(content)}</div>
|
||||
`;
|
||||
messagesDiv.appendChild(msgDiv);
|
||||
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
|
|
@ -162,7 +430,7 @@
|
|||
}
|
||||
|
||||
// Send message handler
|
||||
sendBtn.addEventListener('click', async () => {
|
||||
async function sendMessage() {
|
||||
const message = textarea.value.trim();
|
||||
if (!message) return;
|
||||
|
||||
|
|
@ -175,10 +443,16 @@
|
|||
addMessage('user', message);
|
||||
textarea.value = '';
|
||||
|
||||
// If no conversation ID, create one
|
||||
if (!currentConversationId) {
|
||||
await createNewConversation();
|
||||
}
|
||||
|
||||
try {
|
||||
// Use fetch with URLSearchParams for application/x-www-form-urlencoded
|
||||
const params = new URLSearchParams();
|
||||
params.append('message', message);
|
||||
params.append('conversation_id', currentConversationId);
|
||||
|
||||
const response = await fetch('/chat', {
|
||||
method: 'POST',
|
||||
|
|
@ -192,31 +466,55 @@
|
|||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
// Read the response as text and add assistant message
|
||||
const content = await response.text();
|
||||
addMessage('assistant', content);
|
||||
// Read the response as JSON (now returns JSON with response and conversation_id)
|
||||
const data = await response.json();
|
||||
addMessage('assistant', data.response);
|
||||
|
||||
} catch (error) {
|
||||
addMessage('system', `Error: ${error.message}`);
|
||||
addSystemMessage(`Error: ${error.message}`);
|
||||
} finally {
|
||||
textarea.disabled = false;
|
||||
sendBtn.disabled = false;
|
||||
sendBtn.textContent = 'Send';
|
||||
textarea.focus();
|
||||
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
||||
}
|
||||
});
|
||||
|
||||
// Handle Enter key in textarea
|
||||
// Refresh conversations list
|
||||
await loadConversations();
|
||||
}
|
||||
}
|
||||
|
||||
// Event listeners
|
||||
sendBtn.addEventListener('click', sendMessage);
|
||||
|
||||
newChatBtn.addEventListener('click', createNewConversation);
|
||||
|
||||
textarea.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
sendBtn.click();
|
||||
sendMessage();
|
||||
}
|
||||
});
|
||||
|
||||
// Sidebar toggle for mobile
|
||||
sidebarToggle.addEventListener('click', () => {
|
||||
sidebar.classList.toggle('open');
|
||||
});
|
||||
|
||||
// Close sidebar when clicking outside on mobile
|
||||
document.addEventListener('click', (e) => {
|
||||
if (window.innerWidth <= 768) {
|
||||
if (!sidebar.contains(e.target) && !sidebarToggle.contains(e.target)) {
|
||||
sidebar.classList.remove('open');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Initial focus
|
||||
textarea.focus();
|
||||
|
||||
// Load conversations on page load
|
||||
loadConversations();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue