File size: 2,544 Bytes
3ef46ab |
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 |
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();
}
}); |