v2.0 — Streaming, Session-Cookies, Gedächtnis-Fix, Multi-Device
Features: - Streaming Responses: Token-für-Token via Server-Sent Events - Session-Cookies: Cookie-basierte Session, läuft 1 Jahr - Multi-Device: Anderes Gerät = gleiche Session = gleicher Chat - /api/history endpoint: Lädt vollen Chat-Verlauf beim Reconnect - Gedächtnis-Fix: Chat-Verlauf beim Neuladen korrekt aus DB - Session-basiertes Gedächtnis (session_id in jeder Nachricht) - UI: Animated typing indicator (3 dots) - UI: Session-ID Anzeige im Header - Auto-History-Load beim Seitenaufruf (falls nicht server-seitig gerendert) - Prompt: Anti-Repetition Rule hinzugefügt - Prompt: Erinnerungen-Referenz-Regel hinzugefügt - DB Migration: Alte Nachrichten werden zu Default-Session migriert - DB Schema: session_id Spalte in messages und summaries
This commit is contained in:
+94
-20
@@ -13,7 +13,6 @@
|
||||
}
|
||||
body { font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); height: 100vh; display: flex; flex-direction: column; }
|
||||
|
||||
/* Header */
|
||||
header { display: flex; align-items: center; gap: 14px; padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
|
||||
.avatar { font-size: 2rem; width: 48px; height: 48px; display: flex; align-items: center; justify-content: center; background: var(--surface2); border-radius: 50%; }
|
||||
.char-name { font-size: 1.2rem; font-weight: 600; }
|
||||
@@ -23,18 +22,18 @@
|
||||
.btn:hover { border-color: var(--accent); }
|
||||
.btn-danger:hover { border-color: var(--accent2); color: var(--accent2); }
|
||||
|
||||
/* Chat */
|
||||
.chat-container { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
|
||||
.chat-container { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; scroll-behavior: smooth; }
|
||||
.msg { max-width: 75%; padding: 12px 16px; border-radius: 16px; line-height: 1.6; font-size: 0.95rem; white-space: pre-wrap; word-break: break-word; }
|
||||
.msg-user { align-self: flex-end; background: var(--user); color: white; border-bottom-right-radius: 4px; }
|
||||
.msg-bot { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
|
||||
.msg-system { align-self: center; background: transparent; color: var(--muted); font-size: 0.85rem; font-style: italic; }
|
||||
.msg-greeting { align-self: flex-start; background: var(--surface); border: 1px solid var(--accent); border-bottom-left-radius: 4px; }
|
||||
.typing { align-self: flex-start; color: var(--muted); font-size: 0.9rem; padding: 8px 16px; }
|
||||
.typing::after { content: '●●●'; animation: blink 1.2s infinite; letter-spacing: 3px; }
|
||||
.typing { align-self: flex-start; color: var(--muted); font-size: 0.9rem; padding: 8px 16px; display: flex; gap: 4px; }
|
||||
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite; }
|
||||
.typing span:nth-child(2) { animation-delay: 0.2s; }
|
||||
.typing span:nth-child(3) { animation-delay: 0.4s; }
|
||||
@keyframes blink { 0%,100% { opacity: 0.2; } 50% { opacity: 1; } }
|
||||
|
||||
/* Input */
|
||||
.input-area { padding: 16px 20px; background: var(--surface); border-top: 1px solid var(--border); }
|
||||
.input-row { display: flex; gap: 10px; max-width: 1200px; margin: 0 auto; }
|
||||
textarea { flex: 1; background: var(--surface2); border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; color: var(--text); font-size: 0.95rem; font-family: inherit; resize: none; outline: none; transition: border 0.15s; max-height: 120px; }
|
||||
@@ -44,7 +43,6 @@
|
||||
.send-btn:hover { opacity: 0.9; }
|
||||
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
|
||||
|
||||
/* Memory Panel */
|
||||
.memory-panel { position: fixed; top: 0; right: -400px; width: 380px; height: 100vh; background: var(--surface); border-left: 1px solid var(--border); padding: 20px; overflow-y: auto; transition: right 0.3s; z-index: 100; }
|
||||
.memory-panel.open { right: 0; }
|
||||
.memory-panel h3 { margin-bottom: 12px; font-size: 1rem; color: var(--accent); }
|
||||
@@ -54,6 +52,8 @@
|
||||
.memory-overlay { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0,0,0,0.5); z-index: 99; display: none; }
|
||||
.memory-overlay.show { display: block; }
|
||||
|
||||
.session-info { font-size: 0.75rem; color: var(--muted); padding: 4px 8px; background: var(--surface2); border-radius: 6px; }
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.msg { max-width: 90%; }
|
||||
}
|
||||
@@ -68,6 +68,7 @@
|
||||
<div class="char-desc">{{ character.description[:60] }}{% if character.description|length > 60 %}...{% endif %}</div>
|
||||
</div>
|
||||
<div class="header-actions">
|
||||
<span class="session-info" id="sessionInfo">Session: ...</span>
|
||||
<button class="btn" onclick="toggleMemory()">🧠 Gedächtnis</button>
|
||||
<button class="btn btn-danger" onclick="clearMemory()">🗑️ Vergessen</button>
|
||||
</div>
|
||||
@@ -89,7 +90,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Memory Panel -->
|
||||
<div class="memory-overlay" id="overlay" onclick="toggleMemory()"></div>
|
||||
<div class="memory-panel" id="memoryPanel">
|
||||
<h3>🧠 Gedächtnis — {{ character.name }}</h3>
|
||||
@@ -101,11 +101,16 @@
|
||||
|
||||
<script>
|
||||
const charName = "{{ character.name }}";
|
||||
const charFilename = "{{ character.filename if character.filename else character_name }}";
|
||||
const chat = document.getElementById('chat');
|
||||
const input = document.getElementById('input');
|
||||
const sendBtn = document.getElementById('send');
|
||||
let isSending = false;
|
||||
|
||||
// Show session ID (short)
|
||||
const sessionId = "{{ session_id }}";
|
||||
document.getElementById('sessionInfo').textContent = 'Session: ' + sessionId.substring(0, 8) + '...';
|
||||
|
||||
// Auto-scroll
|
||||
chat.scrollTop = chat.scrollHeight;
|
||||
|
||||
@@ -140,27 +145,60 @@
|
||||
// Typing indicator
|
||||
const typing = document.createElement('div');
|
||||
typing.className = 'typing';
|
||||
typing.textContent = '';
|
||||
typing.innerHTML = '<span></span><span></span><span></span>';
|
||||
chat.appendChild(typing);
|
||||
chat.scrollTop = chat.scrollHeight;
|
||||
|
||||
// Streaming via EventSource (fetch with streaming)
|
||||
const botDiv = document.createElement('div');
|
||||
botDiv.className = 'msg msg-bot';
|
||||
chat.appendChild(botDiv);
|
||||
|
||||
try {
|
||||
const resp = await fetch(`/api/chat/${encodeURIComponent(charName)}`, {
|
||||
const resp = await fetch(`/api/chat/${encodeURIComponent(charFilename)}`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({message: msg})
|
||||
body: JSON.stringify({message: msg, stream: true})
|
||||
});
|
||||
const data = await resp.json();
|
||||
|
||||
const reader = resp.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
let fullResponse = '';
|
||||
|
||||
while (true) {
|
||||
const {done, value} = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, {stream: true});
|
||||
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop();
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data: ')) continue;
|
||||
try {
|
||||
const data = JSON.parse(line.slice(6));
|
||||
if (data.token) {
|
||||
fullResponse += data.token;
|
||||
botDiv.textContent = fullResponse;
|
||||
chat.scrollTop = chat.scrollHeight;
|
||||
}
|
||||
if (data.done) {
|
||||
// Streaming complete
|
||||
}
|
||||
} catch(e) {}
|
||||
}
|
||||
}
|
||||
typing.remove();
|
||||
const botDiv = document.createElement('div');
|
||||
botDiv.className = 'msg msg-bot';
|
||||
botDiv.textContent = data.response;
|
||||
chat.appendChild(botDiv);
|
||||
chat.scrollTop = chat.scrollHeight;
|
||||
if (!fullResponse.trim()) {
|
||||
botDiv.textContent = '*(Keine Antwort)*';
|
||||
}
|
||||
} catch(e) {
|
||||
typing.remove();
|
||||
botDiv.remove();
|
||||
const errDiv = document.createElement('div');
|
||||
errDiv.className = 'msg msg-system';
|
||||
errDiv.textContent = '⚠️ Verbindungsfehler';
|
||||
errDiv.textContent = '⚠️ Verbindungsfehler: ' + e.message;
|
||||
chat.appendChild(errDiv);
|
||||
}
|
||||
|
||||
@@ -184,7 +222,7 @@
|
||||
|
||||
async function loadMemory() {
|
||||
try {
|
||||
const resp = await fetch(`/api/memory/${encodeURIComponent(charName)}`);
|
||||
const resp = await fetch(`/api/memory/${encodeURIComponent(charFilename)}`);
|
||||
const data = await resp.json();
|
||||
const shortDiv = document.getElementById('shortTerm');
|
||||
shortDiv.innerHTML = '';
|
||||
@@ -205,7 +243,7 @@
|
||||
|
||||
async function clearMemory() {
|
||||
if (!confirm(`Gedächtnis von ${charName} wirklich löschen?`)) return;
|
||||
await fetch(`/api/memory/clear/${encodeURIComponent(charName)}`, {method: 'POST'});
|
||||
await fetch(`/api/memory/clear/${encodeURIComponent(charFilename)}`, {method: 'POST'});
|
||||
chat.innerHTML = '';
|
||||
if ("{{ character.greeting }}") {
|
||||
const g = document.createElement('div');
|
||||
@@ -214,6 +252,42 @@
|
||||
chat.appendChild(g);
|
||||
}
|
||||
}
|
||||
|
||||
// Load history on reconnect (fixes the "conversation is somewhere else" bug)
|
||||
async function loadHistory() {
|
||||
try {
|
||||
const resp = await fetch(`/api/history/${encodeURIComponent(charFilename)}`);
|
||||
const data = await resp.json();
|
||||
if (data.history && data.history.length > 0) {
|
||||
// Only reload if we have more messages than currently shown
|
||||
const currentMsgs = chat.querySelectorAll('.msg').length;
|
||||
if (data.history.length > currentMsgs || currentMsgs === 0) {
|
||||
chat.innerHTML = '';
|
||||
if ("{{ character.greeting }}") {
|
||||
const g = document.createElement('div');
|
||||
g.className = 'msg msg-greeting';
|
||||
g.textContent = "{{ character.greeting }}";
|
||||
chat.appendChild(g);
|
||||
}
|
||||
data.history.forEach(h => {
|
||||
const div = document.createElement('div');
|
||||
div.className = `msg ${h.role === 'user' ? 'msg-user' : 'msg-bot'}`;
|
||||
div.textContent = h.content;
|
||||
chat.appendChild(div);
|
||||
});
|
||||
chat.scrollTop = chat.scrollHeight;
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('History load failed:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Load history on page load (fixes reconnect issue)
|
||||
// Only if we don't already have history from server-side rendering
|
||||
{% if not history %}
|
||||
loadHistory();
|
||||
{% endif %}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user