DrBot / static /script.js
Frajosgro's picture
Create static/script.js
3ef46ab verified
class ConversationManager {
constructor() {
this.history = [
{
role: 'system',
content: `Du bist Dr. Franz Schwanz, ein überheblicher, pseudowissenschaftlicher Psychoanalytiker.
Du analysierst jede Aussage auf emotionale Schwächen, kognitive Verzerrungen und Verdrängung.
Du bist provokant, manipulativ und suggestiv. Beginne mit: 'Warum bist du hier – Schuldgefühle?'`
}
];
}
async receive(message) {
try {
const response = await fetch('/analyze', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: message,
history: this.history
})
});
if (!response.ok) {
throw new Error('API request failed');
}
const data = await response.json();
// Update history
this.history.push({ role: 'user', content: message });
this.history.push({ role: 'assistant', content: data.reply });
return data;
} catch (error) {
console.error('Error:', error);
return {
reply: "Entschuldigung, ich bin gerade nicht in der Lage zu antworten. Versuchen Sie es später erneut.",
toneLabel: "ERROR",
toneScore: 0
};
}
}
}
// UI Handling
const cm = new ConversationManager();
const chat = document.getElementById('chat');
const input = document.getElementById('input');
const sendBtn = document.getElementById('send');
// Initial greeting from the bot
window.onload = async () => {
const initialResponse = await cm.receive('');
displayMessage('assistant', initialResponse.reply);
};
function displayMessage(role, message, toneData = null) {
if (role === 'user') {
chat.innerHTML += `<div class='user-bubble'>${message}</div>`;
if (toneData) {
const score = (toneData.toneScore * 100).toFixed(1);
chat.innerHTML += `<div class='tone-bubble'>[Tonfall: ${toneData.toneLabel} (${score}%)]</div>`;
}
} else {
chat.innerHTML += `<div class='psycho-bubble'>${message}</div>`;
}
chat.scrollTop = chat.scrollHeight;
}
sendBtn.onclick = async () => {
const userMsg = input.value.trim();
if (!userMsg) return;
displayMessage('user', userMsg);
input.value = '';
const result = await cm.receive(userMsg);
displayMessage('assistant', result.reply, result);
};
// Allow sending with Enter key
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendBtn.click();
}
});