swaroop77 commited on
Commit
d9c7292
·
verified ·
1 Parent(s): 9916ba1

Delete templates

Browse files
Files changed (2) hide show
  1. templates/script.js +0 -142
  2. templates/style.css +0 -120
templates/script.js DELETED
@@ -1,142 +0,0 @@
1
- document.addEventListener('DOMContentLoaded', () => {
2
- const chatbox = document.getElementById('chatbox');
3
- const userInput = document.getElementById('userInput');
4
- const sendBtn = document.getElementById('sendBtn');
5
- const clearMemoryBtn = document.getElementById('clearMemoryBtn'); // Get the new button
6
-
7
- // Function to append a message to the chatbox
8
- function appendMessage(sender, text) {
9
- const messageElement = document.createElement('div');
10
- messageElement.classList.add('message', `${sender}-message`);
11
-
12
- const p = document.createElement('p');
13
- // Use textContent to avoid HTML injection
14
- p.textContent = text;
15
-
16
- messageElement.appendChild(p);
17
- chatbox.appendChild(messageElement);
18
-
19
- // Auto-scroll to the bottom
20
- chatbox.scrollTop = chatbox.scrollHeight;
21
- }
22
-
23
- // Function to send message to backend
24
- async function sendMessage() {
25
- const messageText = userInput.value.trim();
26
- if (messageText === "") {
27
- return; // Don't send empty messages
28
- }
29
-
30
- // Append user message to chatbox
31
- appendMessage('user', messageText);
32
-
33
- // Clear input and disable buttons
34
- userInput.value = '';
35
- userInput.disabled = true;
36
- sendBtn.disabled = true;
37
- if (clearMemoryBtn) clearMemoryBtn.disabled = true;
38
-
39
-
40
- // Optional: Add a loading indicator
41
- const loadingElement = document.createElement('div');
42
- loadingElement.classList.add('message', 'assistant-message', 'loading-indicator');
43
- loadingElement.textContent = 'AI is thinking...';
44
- chatbox.appendChild(loadingElement);
45
- chatbox.scrollTop = chatbox.scrollHeight; // Scroll to show indicator
46
-
47
-
48
- try {
49
- const response = await fetch('/chat', {
50
- method: 'POST',
51
- headers: {
52
- 'Content-Type': 'application/json',
53
- },
54
- body: JSON.stringify({ message: messageText }),
55
- });
56
-
57
- // Remove loading indicator
58
- if (chatbox.contains(loadingElement)) {
59
- chatbox.removeChild(loadingElement);
60
- }
61
-
62
-
63
- if (!response.ok) {
64
- const errorData = await response.json().catch(() => ({})); // Try parsing JSON, fallback to empty object
65
- const errorMessage = errorData.response || `Server returned status ${response.status}: ${response.statusText}`;
66
- throw new Error(`Chat request failed: ${errorMessage}`);
67
- }
68
-
69
- const data = await response.json();
70
- appendMessage('assistant', data.response);
71
-
72
- } catch (error) {
73
- console.error('Error sending message:', error);
74
- // Remove loading indicator if still present
75
- if (chatbox.contains(loadingElement)) {
76
- chatbox.removeChild(loadingElement);
77
- }
78
- appendMessage('assistant', `Error: Could not get a response. ${error.message}`);
79
- } finally {
80
- // Re-enable input and buttons
81
- userInput.disabled = false;
82
- sendBtn.disabled = false;
83
- if (clearMemoryBtn) clearMemoryBtn.disabled = false;
84
- userInput.focus(); // Put focus back on input field
85
- }
86
- }
87
-
88
- // Function to clear memory
89
- async function clearMemory() {
90
- if (confirm("Are you sure you want to clear the chat history? This will start a new conversation.")) {
91
- // Disable buttons during clear
92
- userInput.disabled = true;
93
- sendBtn.disabled = true;
94
- if (clearMemoryBtn) clearMemoryBtn.disabled = true;
95
-
96
- try {
97
- const response = await fetch('/clear_memory', {
98
- method: 'POST',
99
- });
100
-
101
- if (!response.ok) {
102
- const errorData = await response.json().catch(() => ({}));
103
- const errorMessage = errorData.status || `Server returned status ${response.status}: ${response.statusText}`;
104
- throw new Error(`Clear memory request failed: ${errorMessage}`);
105
- }
106
-
107
- // Clear the chatbox UI and add initial message
108
- chatbox.innerHTML = ''; // Clear all messages
109
- appendMessage('assistant', 'Chat history cleared. Starting a new conversation!');
110
- appendMessage('assistant', 'Hello! I\'m an AI chatbot with memory. How can I help you today?');
111
-
112
-
113
- } catch (error) {
114
- console.error('Error clearing memory:', error);
115
- appendMessage('assistant', `Error: Could not clear chat history. ${error.message}`);
116
- } finally {
117
- // Re-enable buttons
118
- userInput.disabled = false;
119
- sendBtn.disabled = false;
120
- if (clearMemoryBtn) clearMemoryBtn.disabled = false;
121
- userInput.focus();
122
- }
123
- }
124
- }
125
-
126
-
127
- // Event listeners
128
- sendBtn.addEventListener('click', sendMessage);
129
- userInput.addEventListener('keypress', function(event) {
130
- if (event.key === 'Enter') {
131
- event.preventDefault(); // Prevent newline in input
132
- sendMessage();
133
- }
134
- });
135
-
136
- // Add event listener for the clear memory button
137
- if (clearMemoryBtn) {
138
- clearMemoryBtn.addEventListener('click', clearMemory);
139
- }
140
-
141
- // Initial welcome message is already in index.html
142
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
templates/style.css DELETED
@@ -1,120 +0,0 @@
1
- body {
2
- font-family: sans-serif;
3
- margin: 0;
4
- padding: 0;
5
- background-color: #f4f4f4;
6
- display: flex;
7
- flex-direction: column;
8
- min-height: 100vh;
9
- }
10
-
11
- header {
12
- background-color: #007bff;
13
- color: white;
14
- padding: 10px 20px;
15
- text-align: center;
16
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
17
- }
18
-
19
- header h1 {
20
- margin: 0;
21
- font-size: 1.5em;
22
- }
23
-
24
- .chat-container {
25
- flex-grow: 1;
26
- display: flex;
27
- flex-direction: column;
28
- max-width: 800px;
29
- margin: 20px auto;
30
- background-color: #fff;
31
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
32
- border-radius: 8px;
33
- overflow: hidden;
34
- }
35
-
36
- .chatbox {
37
- flex-grow: 1;
38
- padding: 20px;
39
- overflow-y: auto;
40
- max-height: 60vh; /* Adjust as needed */
41
- display: flex;
42
- flex-direction: column;
43
- }
44
-
45
- .message {
46
- margin-bottom: 15px;
47
- padding: 10px 15px;
48
- border-radius: 5px;
49
- max-width: 80%;
50
- word-wrap: break-word; /* Ensure long words wrap */
51
- }
52
-
53
- .user-message {
54
- align-self: flex-end;
55
- background-color: #dcf8c6;
56
- color: #333;
57
- }
58
-
59
- .assistant-message {
60
- align-self: flex-start;
61
- background-color: #e9e9eb;
62
- color: #333;
63
- }
64
-
65
- .input-area {
66
- display: flex;
67
- padding: 15px 20px;
68
- border-top: 1px solid #eee;
69
- gap: 10px; /* Space between input and buttons */
70
- }
71
-
72
- .input-area input[type="text"] {
73
- flex-grow: 1;
74
- padding: 10px;
75
- border: 1px solid #ccc;
76
- border-radius: 4px;
77
- font-size: 1em;
78
- }
79
-
80
- .input-area button {
81
- padding: 10px 15px;
82
- background-color: #007bff;
83
- color: white;
84
- border: none;
85
- border-radius: 4px;
86
- cursor: pointer;
87
- font-size: 1em;
88
- transition: background-color 0.2s ease;
89
- }
90
-
91
- .input-area button:hover {
92
- background-color: #0056b3;
93
- }
94
-
95
- .input-area button:disabled {
96
- background-color: #cccccc;
97
- cursor: not-allowed;
98
- }
99
-
100
-
101
- footer {
102
- margin-top: auto; /* Push footer to the bottom */
103
- background-color: #333;
104
- color: white;
105
- text-align: center;
106
- padding: 10px 20px;
107
- font-size: 0.9em;
108
- }
109
-
110
- footer p {
111
- margin: 0;
112
- }
113
-
114
- /* Optional: Styling for loading indicator */
115
- .loading-indicator {
116
- font-style: italic;
117
- color: #888;
118
- align-self: flex-start;
119
- margin-left: 10px;
120
- }