Create static/script.js
Browse files- static/script.js +90 -0
static/script.js
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class ConversationManager {
|
2 |
+
constructor() {
|
3 |
+
this.history = [
|
4 |
+
{
|
5 |
+
role: 'system',
|
6 |
+
content: `Du bist Dr. Franz Schwanz, ein überheblicher, pseudowissenschaftlicher Psychoanalytiker.
|
7 |
+
Du analysierst jede Aussage auf emotionale Schwächen, kognitive Verzerrungen und Verdrängung.
|
8 |
+
Du bist provokant, manipulativ und suggestiv. Beginne mit: 'Warum bist du hier – Schuldgefühle?'`
|
9 |
+
}
|
10 |
+
];
|
11 |
+
}
|
12 |
+
|
13 |
+
async receive(message) {
|
14 |
+
try {
|
15 |
+
const response = await fetch('/analyze', {
|
16 |
+
method: 'POST',
|
17 |
+
headers: {
|
18 |
+
'Content-Type': 'application/json',
|
19 |
+
},
|
20 |
+
body: JSON.stringify({
|
21 |
+
text: message,
|
22 |
+
history: this.history
|
23 |
+
})
|
24 |
+
});
|
25 |
+
|
26 |
+
if (!response.ok) {
|
27 |
+
throw new Error('API request failed');
|
28 |
+
}
|
29 |
+
|
30 |
+
const data = await response.json();
|
31 |
+
|
32 |
+
// Update history
|
33 |
+
this.history.push({ role: 'user', content: message });
|
34 |
+
this.history.push({ role: 'assistant', content: data.reply });
|
35 |
+
|
36 |
+
return data;
|
37 |
+
} catch (error) {
|
38 |
+
console.error('Error:', error);
|
39 |
+
return {
|
40 |
+
reply: "Entschuldigung, ich bin gerade nicht in der Lage zu antworten. Versuchen Sie es später erneut.",
|
41 |
+
toneLabel: "ERROR",
|
42 |
+
toneScore: 0
|
43 |
+
};
|
44 |
+
}
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
// UI Handling
|
49 |
+
const cm = new ConversationManager();
|
50 |
+
const chat = document.getElementById('chat');
|
51 |
+
const input = document.getElementById('input');
|
52 |
+
const sendBtn = document.getElementById('send');
|
53 |
+
|
54 |
+
// Initial greeting from the bot
|
55 |
+
window.onload = async () => {
|
56 |
+
const initialResponse = await cm.receive('');
|
57 |
+
displayMessage('assistant', initialResponse.reply);
|
58 |
+
};
|
59 |
+
|
60 |
+
function displayMessage(role, message, toneData = null) {
|
61 |
+
if (role === 'user') {
|
62 |
+
chat.innerHTML += `<div class='user-bubble'>${message}</div>`;
|
63 |
+
if (toneData) {
|
64 |
+
const score = (toneData.toneScore * 100).toFixed(1);
|
65 |
+
chat.innerHTML += `<div class='tone-bubble'>[Tonfall: ${toneData.toneLabel} (${score}%)]</div>`;
|
66 |
+
}
|
67 |
+
} else {
|
68 |
+
chat.innerHTML += `<div class='psycho-bubble'>${message}</div>`;
|
69 |
+
}
|
70 |
+
chat.scrollTop = chat.scrollHeight;
|
71 |
+
}
|
72 |
+
|
73 |
+
sendBtn.onclick = async () => {
|
74 |
+
const userMsg = input.value.trim();
|
75 |
+
if (!userMsg) return;
|
76 |
+
|
77 |
+
displayMessage('user', userMsg);
|
78 |
+
input.value = '';
|
79 |
+
|
80 |
+
const result = await cm.receive(userMsg);
|
81 |
+
displayMessage('assistant', result.reply, result);
|
82 |
+
};
|
83 |
+
|
84 |
+
// Allow sending with Enter key
|
85 |
+
input.addEventListener('keypress', (e) => {
|
86 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
87 |
+
e.preventDefault();
|
88 |
+
sendBtn.click();
|
89 |
+
}
|
90 |
+
});
|