599 lines
20 KiB
HTML
599 lines
20 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Conversation Viewer</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
height: 100vh;
|
|
display: flex;
|
|
background-color: #f5f5f5;
|
|
}
|
|
|
|
/* Left Sidebar - Conversation List */
|
|
.sidebar {
|
|
width: 300px;
|
|
background-color: #2c3e50;
|
|
color: white;
|
|
display: flex;
|
|
flex-direction: column;
|
|
border-right: 1px solid #34495e;
|
|
}
|
|
|
|
.sidebar-header {
|
|
padding: 20px;
|
|
background-color: #34495e;
|
|
border-bottom: 1px solid #2c3e50;
|
|
}
|
|
|
|
.sidebar-header h1 {
|
|
font-size: 20px;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.connection-status {
|
|
font-size: 11px;
|
|
margin-top: 8px;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
}
|
|
|
|
.status-indicator {
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
display: inline-block;
|
|
}
|
|
|
|
.status-indicator.connected {
|
|
background-color: #2ecc71;
|
|
box-shadow: 0 0 4px #2ecc71;
|
|
}
|
|
|
|
.status-indicator.disconnected {
|
|
background-color: #e74c3c;
|
|
}
|
|
|
|
.status-indicator.connecting {
|
|
background-color: #f39c12;
|
|
}
|
|
|
|
.conversation-list {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 10px;
|
|
}
|
|
|
|
.conversation-item {
|
|
padding: 12px 15px;
|
|
margin-bottom: 8px;
|
|
background-color: #34495e;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
border: 2px solid transparent;
|
|
}
|
|
|
|
.conversation-item:hover {
|
|
background-color: #3d566e;
|
|
transform: translateX(2px);
|
|
}
|
|
|
|
.conversation-item.active {
|
|
background-color: #3498db;
|
|
border-color: #2980b9;
|
|
}
|
|
|
|
.conversation-id {
|
|
font-weight: 600;
|
|
font-size: 14px;
|
|
margin-bottom: 4px;
|
|
word-break: break-all;
|
|
}
|
|
|
|
.conversation-meta {
|
|
font-size: 12px;
|
|
opacity: 0.8;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
}
|
|
|
|
.message-count {
|
|
background-color: rgba(255, 255, 255, 0.2);
|
|
padding: 2px 8px;
|
|
border-radius: 12px;
|
|
font-size: 11px;
|
|
}
|
|
|
|
/* Main Content Area */
|
|
.main-content {
|
|
flex: 1;
|
|
display: flex;
|
|
flex-direction: column;
|
|
background-color: #ffffff;
|
|
}
|
|
|
|
.chat-header {
|
|
padding: 20px;
|
|
background-color: #ecf0f1;
|
|
border-bottom: 1px solid #bdc3c7;
|
|
}
|
|
|
|
.chat-header h2 {
|
|
font-size: 18px;
|
|
color: #2c3e50;
|
|
}
|
|
|
|
.chat-header .conversation-id-display {
|
|
font-size: 14px;
|
|
color: #7f8c8d;
|
|
margin-top: 4px;
|
|
}
|
|
|
|
.messages-container {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 20px;
|
|
background: linear-gradient(to bottom, #f8f9fa, #ffffff);
|
|
}
|
|
|
|
.message {
|
|
margin-bottom: 16px;
|
|
display: flex;
|
|
animation: fadeIn 0.3s ease-in;
|
|
}
|
|
|
|
@keyframes fadeIn {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(10px);
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
}
|
|
|
|
.message.human {
|
|
justify-content: flex-end;
|
|
}
|
|
|
|
.message.ai,
|
|
.message.tool {
|
|
justify-content: flex-start;
|
|
}
|
|
|
|
.message-bubble {
|
|
max-width: 70%;
|
|
padding: 12px 16px;
|
|
border-radius: 18px;
|
|
word-wrap: break-word;
|
|
position: relative;
|
|
}
|
|
|
|
.message.human .message-bubble {
|
|
background-color: #3498db;
|
|
color: white;
|
|
border-bottom-right-radius: 4px;
|
|
}
|
|
|
|
.message.ai .message-bubble {
|
|
background-color: #e8f5e9;
|
|
color: #2c3e50;
|
|
border-bottom-left-radius: 4px;
|
|
}
|
|
|
|
.message.tool .message-bubble {
|
|
background-color: #fff3e0;
|
|
color: #2c3e50;
|
|
border-bottom-left-radius: 4px;
|
|
border-left: 3px solid #ff9800;
|
|
}
|
|
|
|
.message-type-label {
|
|
font-size: 11px;
|
|
font-weight: 600;
|
|
text-transform: uppercase;
|
|
margin-bottom: 4px;
|
|
opacity: 0.7;
|
|
}
|
|
|
|
.message-content {
|
|
font-size: 14px;
|
|
line-height: 1.5;
|
|
white-space: pre-wrap;
|
|
}
|
|
|
|
.message-time {
|
|
font-size: 11px;
|
|
opacity: 0.6;
|
|
margin-top: 4px;
|
|
}
|
|
|
|
.empty-state {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 100%;
|
|
color: #95a5a6;
|
|
}
|
|
|
|
.empty-state-icon {
|
|
font-size: 48px;
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
.loading {
|
|
text-align: center;
|
|
padding: 40px;
|
|
color: #7f8c8d;
|
|
}
|
|
|
|
.error {
|
|
background-color: #fee;
|
|
color: #c33;
|
|
padding: 12px;
|
|
margin: 10px;
|
|
border-radius: 4px;
|
|
border-left: 4px solid #c33;
|
|
}
|
|
|
|
/* Scrollbar styling */
|
|
.conversation-list::-webkit-scrollbar,
|
|
.messages-container::-webkit-scrollbar {
|
|
width: 8px;
|
|
}
|
|
|
|
.conversation-list::-webkit-scrollbar-track,
|
|
.messages-container::-webkit-scrollbar-track {
|
|
background: #2c3e50;
|
|
}
|
|
|
|
.conversation-list::-webkit-scrollbar-thumb {
|
|
background: #34495e;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
.messages-container::-webkit-scrollbar-track {
|
|
background: #f8f9fa;
|
|
}
|
|
|
|
.messages-container::-webkit-scrollbar-thumb {
|
|
background: #bdc3c7;
|
|
border-radius: 4px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<!-- Left Sidebar -->
|
|
<div class="sidebar">
|
|
<div class="sidebar-header">
|
|
<h1>💬 Conversations</h1>
|
|
<div class="connection-status">
|
|
<span class="status-indicator" id="statusIndicator"></span>
|
|
<span id="statusText">Connecting...</span>
|
|
</div>
|
|
</div>
|
|
<div class="conversation-list" id="conversationList">
|
|
<div class="loading">Loading conversations...</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Main Content -->
|
|
<div class="main-content">
|
|
<div class="chat-header">
|
|
<h2>Conversation Messages</h2>
|
|
<div class="conversation-id-display" id="conversationIdDisplay">Select a conversation to view messages</div>
|
|
</div>
|
|
<div class="messages-container" id="messagesContainer">
|
|
<div class="empty-state">
|
|
<div class="empty-state-icon">👈</div>
|
|
<div>Select a conversation from the left to view messages</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const API_BASE = window.location.origin;
|
|
let currentConversationId = null;
|
|
let eventSource = null;
|
|
let conversationsMap = new Map(); // Track conversations for efficient updates
|
|
|
|
// Update connection status UI
|
|
function updateConnectionStatus(status, text) {
|
|
const indicator = document.getElementById('statusIndicator');
|
|
const statusText = document.getElementById('statusText');
|
|
|
|
indicator.className = 'status-indicator ' + status;
|
|
statusText.textContent = text;
|
|
}
|
|
|
|
// Connect to SSE endpoint
|
|
function connectSSE() {
|
|
if (eventSource) {
|
|
eventSource.close();
|
|
}
|
|
|
|
updateConnectionStatus('connecting', 'Connecting...');
|
|
|
|
eventSource = new EventSource(`${API_BASE}/api/events`);
|
|
|
|
eventSource.onopen = () => {
|
|
updateConnectionStatus('connected', 'Live updates active');
|
|
};
|
|
|
|
eventSource.onerror = () => {
|
|
updateConnectionStatus('disconnected', 'Connection lost - reconnecting...');
|
|
// EventSource will automatically try to reconnect
|
|
};
|
|
|
|
eventSource.onmessage = (event) => {
|
|
try {
|
|
const data = JSON.parse(event.data);
|
|
handleSSEEvent(data);
|
|
} catch (error) {
|
|
console.error('Error parsing SSE event:', error);
|
|
}
|
|
};
|
|
}
|
|
|
|
// Handle SSE events
|
|
function handleSSEEvent(data) {
|
|
if (data.type === 'error') {
|
|
console.error('SSE error:', data.message);
|
|
return;
|
|
}
|
|
|
|
if (data.type === 'conversation_new') {
|
|
// Add new conversation to the list
|
|
addConversationToList(data.conversation);
|
|
} else if (data.type === 'conversation_updated') {
|
|
// Update existing conversation
|
|
updateConversationInList(data.conversation);
|
|
|
|
// If this is the currently viewed conversation, refresh messages
|
|
if (currentConversationId === data.conversation.conversation_id) {
|
|
selectConversation(data.conversation.conversation_id, true); // true = silent refresh
|
|
}
|
|
} else if (data.type === 'conversation_deleted') {
|
|
// Remove conversation from list
|
|
removeConversationFromList(data.conversation_id);
|
|
}
|
|
}
|
|
|
|
// Add a new conversation to the list
|
|
function addConversationToList(conversation) {
|
|
const listEl = document.getElementById('conversationList');
|
|
const existingItem = listEl.querySelector(`[data-id="${conversation.conversation_id}"]`);
|
|
|
|
if (existingItem) {
|
|
// Already exists, just update it
|
|
updateConversationInList(conversation);
|
|
return;
|
|
}
|
|
|
|
conversationsMap.set(conversation.conversation_id, conversation);
|
|
|
|
// Create new item
|
|
const item = document.createElement('div');
|
|
item.className = 'conversation-item';
|
|
item.dataset.id = conversation.conversation_id;
|
|
item.innerHTML = `
|
|
<div class="conversation-id">${conversation.conversation_id}</div>
|
|
<div class="conversation-meta">
|
|
<span>${formatDate(conversation.last_updated)}</span>
|
|
<span class="message-count">${conversation.message_count} msgs</span>
|
|
</div>
|
|
`;
|
|
|
|
item.addEventListener('click', () => {
|
|
selectConversation(conversation.conversation_id);
|
|
});
|
|
|
|
// Insert at the top (most recent first)
|
|
if (listEl.firstChild) {
|
|
listEl.insertBefore(item, listEl.firstChild);
|
|
} else {
|
|
listEl.appendChild(item);
|
|
}
|
|
}
|
|
|
|
// Update an existing conversation in the list
|
|
function updateConversationInList(conversation) {
|
|
conversationsMap.set(conversation.conversation_id, conversation);
|
|
|
|
const listEl = document.getElementById('conversationList');
|
|
const item = listEl.querySelector(`[data-id="${conversation.conversation_id}"]`);
|
|
|
|
if (item) {
|
|
item.querySelector('.conversation-meta span:first-child').textContent = formatDate(conversation.last_updated);
|
|
item.querySelector('.message-count').textContent = `${conversation.message_count} msgs`;
|
|
|
|
// Move to top if it was updated
|
|
if (item !== listEl.firstChild) {
|
|
listEl.insertBefore(item, listEl.firstChild);
|
|
}
|
|
} else {
|
|
// Doesn't exist yet, add it
|
|
addConversationToList(conversation);
|
|
}
|
|
}
|
|
|
|
// Remove a conversation from the list
|
|
function removeConversationFromList(conversationId) {
|
|
conversationsMap.delete(conversationId);
|
|
|
|
const listEl = document.getElementById('conversationList');
|
|
const item = listEl.querySelector(`[data-id="${conversationId}"]`);
|
|
|
|
if (item) {
|
|
item.remove();
|
|
|
|
// If this was the current conversation, clear the view
|
|
if (currentConversationId === conversationId) {
|
|
currentConversationId = null;
|
|
document.getElementById('conversationIdDisplay').textContent = 'Select a conversation to view messages';
|
|
document.getElementById('messagesContainer').innerHTML = '<div class="empty-state"><div>Select a conversation from the left to view messages</div></div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Load conversations on page load
|
|
async function loadConversations() {
|
|
const listEl = document.getElementById('conversationList');
|
|
try {
|
|
const response = await fetch(`${API_BASE}/api/conversations`);
|
|
if (!response.ok) throw new Error('Failed to load conversations');
|
|
|
|
const conversations = await response.json();
|
|
|
|
// Store conversations in map
|
|
conversationsMap.clear();
|
|
conversations.forEach(conv => {
|
|
conversationsMap.set(conv.conversation_id, conv);
|
|
});
|
|
|
|
if (conversations.length === 0) {
|
|
listEl.innerHTML = '<div class="loading">No conversations found</div>';
|
|
return;
|
|
}
|
|
|
|
listEl.innerHTML = conversations.map(conv => `
|
|
<div class="conversation-item" data-id="${conv.conversation_id}">
|
|
<div class="conversation-id">${conv.conversation_id}</div>
|
|
<div class="conversation-meta">
|
|
<span>${formatDate(conv.last_updated)}</span>
|
|
<span class="message-count">${conv.message_count} msgs</span>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
|
|
// Add click handlers
|
|
listEl.querySelectorAll('.conversation-item').forEach(item => {
|
|
item.addEventListener('click', () => {
|
|
const convId = item.dataset.id;
|
|
selectConversation(convId);
|
|
});
|
|
});
|
|
|
|
// Select first conversation by default
|
|
if (conversations.length > 0) {
|
|
selectConversation(conversations[0].conversation_id);
|
|
}
|
|
} catch (error) {
|
|
listEl.innerHTML = `<div class="error">Error loading conversations: ${error.message}</div>`;
|
|
console.error('Error loading conversations:', error);
|
|
}
|
|
}
|
|
|
|
// Select a conversation and load its messages
|
|
async function selectConversation(conversationId, silentRefresh = false) {
|
|
currentConversationId = conversationId;
|
|
|
|
// Update UI
|
|
document.querySelectorAll('.conversation-item').forEach(item => {
|
|
item.classList.toggle('active', item.dataset.id === conversationId);
|
|
});
|
|
|
|
document.getElementById('conversationIdDisplay').textContent = conversationId;
|
|
|
|
const container = document.getElementById('messagesContainer');
|
|
|
|
// Only show loading if not a silent refresh
|
|
if (!silentRefresh) {
|
|
container.innerHTML = '<div class="loading">Loading messages...</div>';
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/api/conversations/${conversationId}/messages`);
|
|
if (!response.ok) throw new Error('Failed to load messages');
|
|
|
|
const messages = await response.json();
|
|
|
|
if (messages.length === 0) {
|
|
container.innerHTML = '<div class="empty-state"><div>No messages in this conversation</div></div>';
|
|
return;
|
|
}
|
|
|
|
// Check if we need to preserve scroll position (for silent refresh)
|
|
const wasAtBottom = container.scrollHeight - container.scrollTop <= container.clientHeight + 50;
|
|
const oldScrollHeight = container.scrollHeight;
|
|
|
|
container.innerHTML = messages.map(msg => {
|
|
const isHuman = msg.message_type === 'human';
|
|
const isTool = msg.message_type === 'tool';
|
|
const alignClass = isHuman ? 'human' : (isTool ? 'tool' : 'ai');
|
|
const typeLabel = isTool ? 'Tool' : (isHuman ? 'You' : 'AI');
|
|
|
|
return `
|
|
<div class="message ${alignClass}">
|
|
<div class="message-bubble">
|
|
<div class="message-type-label">${typeLabel}</div>
|
|
<div class="message-content">${escapeHtml(msg.content)}</div>
|
|
<div class="message-time">${formatDate(msg.created_at)}</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
|
|
// Scroll to bottom if user was at bottom, or if it's a new selection
|
|
if (wasAtBottom || !silentRefresh) {
|
|
container.scrollTop = container.scrollHeight;
|
|
} else {
|
|
// Preserve scroll position relative to bottom
|
|
const newScrollHeight = container.scrollHeight;
|
|
const scrollDiff = newScrollHeight - oldScrollHeight;
|
|
container.scrollTop = container.scrollTop + scrollDiff;
|
|
}
|
|
} catch (error) {
|
|
container.innerHTML = `<div class="error">Error loading messages: ${error.message}</div>`;
|
|
console.error('Error loading messages:', error);
|
|
}
|
|
}
|
|
|
|
// Utility functions
|
|
function formatDate(dateString) {
|
|
if (!dateString) return 'Unknown';
|
|
const date = new Date(dateString);
|
|
return date.toLocaleString('en-US', {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
});
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
// Initialize on page load
|
|
loadConversations();
|
|
|
|
// Connect to SSE for live updates
|
|
connectSSE();
|
|
|
|
// Cleanup on page unload
|
|
window.addEventListener('beforeunload', () => {
|
|
if (eventSource) {
|
|
eventSource.close();
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|