Spaces:
Running
Running
File size: 14,200 Bytes
ca7e0e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DeepSeek Chat Embed</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<script src="https://unpkg.com/feather-icons"></script>
<style>
.chat-container {
height: calc(100vh - 64px);
}
.message-stream {
height: calc(100% - 72px);
}
.markdown-content pre {
background-color: #1e293b;
color: #f8fafc;
padding: 1rem;
border-radius: 0.5rem;
overflow-x: auto;
}
.markdown-content code {
background-color: #1e293b;
color: #f8fafc;
padding: 0.2rem 0.4rem;
border-radius: 0.25rem;
font-family: monospace;
}
</style>
</head>
<body class="bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-gray-100">
<div id="app" class="flex flex-col h-screen">
<!-- Header (optional, can be hidden) -->
<header id="chat-header" class="bg-white dark:bg-gray-800 shadow-sm">
<div class="px-4 py-3 flex justify-between items-center">
<div class="flex items-center space-x-2">
<div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center">
<i data-feather="message-square" class="text-white"></i>
</div>
<h1 class="text-lg font-bold">DeepSeek Chat</h1>
</div>
<button id="close-embed" class="p-2 rounded-full hover:bg-gray-100 dark:hover:bg-gray-700">
<i data-feather="x"></i>
</button>
</div>
</header>
<!-- Chat Area -->
<main class="flex-1 flex flex-col">
<div id="chat-container" class="chat-container flex-1 flex flex-col">
<!-- Messages -->
<div id="message-stream" class="message-stream overflow-y-auto p-4 space-y-6">
<!-- Messages will be loaded here -->
<div class="text-center py-12 text-gray-500 dark:text-gray-400">
<div class="mx-auto w-16 h-16 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-4">
<i data-feather="message-square" class="w-8 h-8"></i>
</div>
<h3 class="text-lg font-medium">Start a new conversation</h3>
<p class="mt-1">Ask anything or try one of the example prompts</p>
</div>
</div>
<!-- Input Area -->
<div class="p-4 border-t border-gray-200 dark:border-gray-700">
<form id="message-form" class="relative">
<textarea id="message-input" rows="1" placeholder="Type your message..."
class="w-full px-4 py-3 pr-12 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none"></textarea>
<button type="submit" id="send-btn" class="absolute right-3 bottom-3 p-1 rounded-md text-blue-500 hover:bg-blue-50 dark:hover:bg-gray-700">
<i data-feather="send"></i>
</button>
<button type="button" id="stop-btn" class="absolute right-3 bottom-3 p-1 rounded-md text-red-500 hover:bg-red-50 dark:hover:bg-gray-700 hidden">
<i data-feather="square"></i>
</button>
</form>
</div>
</div>
</main>
</div>
<script>
// Initialize Feather Icons
feather.replace();
// Get URL parameters
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
const theme = urlParams.get('theme') || 'auto';
const lang = urlParams.get('lang') || 'en';
const hideHeader = urlParams.get('hideHeader') === '1';
const templateId = urlParams.get('templateId');
const prompt = urlParams.get('prompt');
// Apply theme
if (theme === 'dark' || (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
}
// Hide header if requested
if (hideHeader) {
document.getElementById('chat-header').classList.add('hidden');
}
// DOM Elements
const messageForm = document.getElementById('message-form');
const messageInput = document.getElementById('message-input');
const sendBtn = document.getElementById('send-btn');
const stopBtn = document.getElementById('stop-btn');
const messageStream = document.getElementById('message-stream');
const closeEmbed = document.getElementById('close-embed');
// Event Listeners
messageForm.addEventListener('submit', async (e) => {
e.preventDefault();
const message = messageInput.value.trim();
if (!message) return;
// Add user message to chat
addMessage('user', message);
messageInput.value = '';
// Show stop button, hide send button
sendBtn.classList.add('hidden');
stopBtn.classList.remove('hidden');
// Create assistant message placeholder
const assistantMessageId = 'msg-' + Date.now();
addMessage('assistant', '', assistantMessageId);
// Scroll to bottom
messageStream.scrollTop = messageStream.scrollHeight;
try {
// Call API with token
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [
{role: 'system', content: prompt || 'You are a helpful AI assistant.'},
{role: 'user', content: message}
],
temperature: 0.7,
max_tokens: 1024,
stream: true
})
});
if (!response.ok) {
throw new Error('API request failed');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let assistantMessage = '';
while (true) {
const {done, value} = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(line => line.trim() !== '');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.substring(6);
if (data === '[DONE]') break;
try {
const parsed = JSON.parse(data);
if (parsed.choices && parsed.choices[0].delta.content) {
assistantMessage += parsed.choices[0].delta.content;
updateMessage(assistantMessageId, assistantMessage);
messageStream.scrollTop = messageStream.scrollHeight;
// Notify parent of height change
window.parent.postMessage({
type: 'heightChange',
height: document.body.scrollHeight
}, '*');
}
} catch (e) {
console.error('Error parsing JSON:', e);
}
}
}
}
// Notify parent of message received
window.parent.postMessage({
type: 'messageReceived',
content: assistantMessage
}, '*');
} catch (error) {
console.error('Error:', error);
updateMessage(assistantMessageId, 'Sorry, there was an error processing your request.');
// Notify parent of error
window.parent.postMessage({
type: 'error',
message: error.message
}, '*');
} finally {
sendBtn.classList.remove('hidden');
stopBtn.classList.add('hidden');
messageStream.scrollTop = messageStream.scrollHeight;
// Notify parent of height change
window.parent.postMessage({
type: 'heightChange',
height: document.body.scrollHeight
}, '*');
}
});
stopBtn.addEventListener('click', () => {
// TODO: Implement abort controller for streaming
sendBtn.classList.remove('hidden');
stopBtn.classList.add('hidden');
});
closeEmbed.addEventListener('click', () => {
window.parent.postMessage({
type: 'closeChat'
}, '*');
});
// Listen for messages from parent
window.addEventListener('message', (event) => {
if (event.data.type === 'setTheme') {
if (event.data.theme === 'dark') {
document.documentElement.classList.add('dark');
} else if (event.data.theme === 'light') {
document.documentElement.classList.remove('dark');
}
} else if (event.data.type === 'setLang') {
// TODO: Implement language change
} else if (event.data.type === 'setTemplate') {
// TODO: Implement template change
} else if (event.data.type === 'focus') {
messageInput.focus();
} else if (event.data.type === 'resize') {
// TODO: Handle resize if needed
}
});
// Notify parent that embed is ready
window.parent.postMessage({
type: 'ready'
}, '*');
// Initial height notification
window.parent.postMessage({
type: 'heightChange',
height: document.body.scrollHeight
}, '*');
// Helper functions
function addMessage(role, content, id = '') {
if (role === 'user') {
const messageDiv = document.createElement('div');
messageDiv.className = 'flex justify-end';
messageDiv.innerHTML = `
<div class="max-w-3xl bg-blue-500 text-white rounded-lg p-4">
<div class="markdown-content">${escapeHtml(content)}</div>
</div>
`;
messageStream.appendChild(messageDiv);
} else if (role === 'assistant') {
const messageDiv = document.createElement('div');
messageDiv.className = 'flex justify-start';
messageDiv.id = id;
messageDiv.innerHTML = `
<div class="max-w-3xl bg-gray-100 dark:bg-gray-700 rounded-lg p-4">
<div class="markdown-content">${escapeHtml(content)}</div>
</div>
`;
messageStream.appendChild(messageDiv);
}
feather.replace();
// Notify parent of message sent
if (role === 'user') {
window.parent.postMessage({
type: 'messageSent',
content: content
}, '*');
}
}
function updateMessage(id, content) {
const messageDiv = document.getElementById(id);
if (messageDiv) {
const contentDiv = messageDiv.querySelector('.markdown-content');
if (contentDiv) {
contentDiv.innerHTML = markdownToHtml(content);
}
}
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function markdownToHtml(markdown) {
// Simple markdown to HTML conversion (basic support for **bold** and `code`)
let html = escapeHtml(markdown);
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
html = html.replace(/`(.*?)`/g, '<code>$1</code>');
html = html.replace(/\n/g, '<br>');
return html;
}
// Auto-resize textarea
messageInput.addEventListener('input', function() {
this.style.height = 'auto';
this.style.height = (this.scrollHeight) + 'px';
// Notify parent of height change
window.parent.postMessage({
type: 'heightChange',
height: document.body.scrollHeight
}, '*');
});
</script>
</body>
</html>
|