disinto/docker/chat/ui/index.html
Claude 6d148d669b
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful
ci/woodpecker/pr/smoke-init Pipeline was successful
fix: address AI review feedback - early-return guard and unused volume
2026-04-12 03:38:46 +00:00

521 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>disinto-chat</title>
<script src="/static/htmx.min.js"></script>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
background: #1a1a2e;
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;
padding: 1rem 2rem;
border-bottom: 1px solid #0f3460;
}
header h1 {
font-size: 1.25rem;
font-weight: 600;
}
main {
flex: 1;
display: flex;
flex-direction: column;
max-width: 900px;
margin: 0 auto;
width: 100%;
padding: 1rem;
}
#messages {
flex: 1;
overflow-y: auto;
padding: 1rem;
background: #16213e;
border-radius: 8px;
margin-bottom: 1rem;
}
.message {
margin-bottom: 1rem;
padding: 0.75rem 1rem;
border-radius: 8px;
line-height: 1.5;
}
.message.user {
background: #0f3460;
margin-left: 2rem;
}
.message.assistant {
background: #1a1a2e;
margin-right: 2rem;
}
.message.system {
background: #1a1a2e;
font-style: italic;
color: #888;
text-align: center;
}
.message .role {
font-weight: 600;
font-size: 0.875rem;
margin-bottom: 0.25rem;
opacity: 0.8;
}
.message .content {
white-space: pre-wrap;
word-wrap: break-word;
}
.input-area {
display: flex;
gap: 0.5rem;
padding: 1rem;
background: #16213e;
border-radius: 8px;
}
textarea {
flex: 1;
background: #1a1a2e;
border: 1px solid #0f3460;
border-radius: 6px;
padding: 0.75rem;
color: #eaeaea;
font-family: inherit;
font-size: 1rem;
resize: none;
min-height: 80px;
}
textarea:focus {
outline: none;
border-color: #e94560;
}
button {
background: #e94560;
color: white;
border: none;
border-radius: 6px;
padding: 0.75rem 1.5rem;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #d63447;
}
button:disabled {
background: #555;
cursor: not-allowed;
}
.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>
<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>
<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) {
// Early-return if already showing this conversation
if (convId === currentConversationId) {
return;
}
// 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');
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}`;
msgDiv.innerHTML = `
<div class="role">${role}</div>
<div class="content${streaming ? ' streaming' : ''}">${escapeHtml(content)}</div>
`;
messagesDiv.appendChild(msgDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
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;
return div.innerHTML.replace(/\n/g, '<br>');
}
// Send message handler
async function sendMessage() {
const message = textarea.value.trim();
if (!message) return;
// Disable input
textarea.disabled = true;
sendBtn.disabled = true;
sendBtn.textContent = 'Sending...';
// Add user message
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',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
// Read the response as JSON (now returns JSON with response and conversation_id)
const data = await response.json();
addMessage('assistant', data.response);
} catch (error) {
addSystemMessage(`Error: ${error.message}`);
} finally {
textarea.disabled = false;
sendBtn.disabled = false;
sendBtn.textContent = 'Send';
textarea.focus();
messagesDiv.scrollTop = messagesDiv.scrollHeight;
// 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();
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>