Update templates/index.html
Browse files- templates/index.html +39 -18
templates/index.html
CHANGED
@@ -47,15 +47,9 @@
|
|
47 |
<span id="fileName" class="ml-2 text-gray-600"></span>
|
48 |
</div>
|
49 |
|
50 |
-
<!-- Chat Messages
|
51 |
<div id="chatMessages" class="flex-1 overflow-y-auto mb-4 space-y-4 bg-gray-50 p-4 rounded-lg">
|
52 |
-
|
53 |
-
<div class="flex {% if message.role == 'user' %}justify-end{% endif %}">
|
54 |
-
<div class="max-w-3/4 p-3 rounded-lg {% if message.role == 'user' %}bg-blue-500 text-white{% else %}bg-gray-200{% endif %}">
|
55 |
-
{{ message.content }}
|
56 |
-
</div>
|
57 |
-
</div>
|
58 |
-
{% endfor %}
|
59 |
</div>
|
60 |
|
61 |
<!-- Input Area -->
|
@@ -75,6 +69,7 @@
|
|
75 |
</div>
|
76 |
|
77 |
<script>
|
|
|
78 |
const messageInput = document.getElementById('messageInput');
|
79 |
const chatMessages = document.getElementById('chatMessages');
|
80 |
const webSearchToggle = document.getElementById('webSearchToggle');
|
@@ -85,6 +80,7 @@
|
|
85 |
let conversations = [];
|
86 |
let currentConversationId = null;
|
87 |
|
|
|
88 |
function startNewConversation() {
|
89 |
const conversationId = Date.now().toString();
|
90 |
const conversation = {
|
@@ -111,15 +107,28 @@
|
|
111 |
}
|
112 |
|
113 |
function loadConversation(conversationId) {
|
|
|
|
|
114 |
currentConversationId = conversationId;
|
115 |
const conversation = conversations.find(c => c.id === conversationId);
|
116 |
updateConversationsList();
|
117 |
|
118 |
if (conversation) {
|
119 |
clearChatMessages();
|
|
|
120 |
conversation.messages.forEach(msg => {
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
});
|
|
|
|
|
123 |
}
|
124 |
}
|
125 |
|
@@ -139,7 +148,6 @@
|
|
139 |
chatMessages.appendChild(messageDiv);
|
140 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
141 |
|
142 |
-
// Save message to current conversation
|
143 |
if (currentConversationId) {
|
144 |
const conversation = conversations.find(c => c.id === currentConversationId);
|
145 |
if (conversation) {
|
@@ -148,6 +156,7 @@
|
|
148 |
}
|
149 |
}
|
150 |
|
|
|
151 |
async function sendMessage() {
|
152 |
const message = messageInput.value.trim();
|
153 |
if (!message) return;
|
@@ -183,12 +192,7 @@
|
|
183 |
}
|
184 |
}
|
185 |
|
186 |
-
|
187 |
-
if (e.key === 'Enter') {
|
188 |
-
sendMessage();
|
189 |
-
}
|
190 |
-
});
|
191 |
-
|
192 |
fileUpload.addEventListener('change', async (e) => {
|
193 |
const file = e.target.files[0];
|
194 |
if (!file) return;
|
@@ -214,9 +218,19 @@
|
|
214 |
}
|
215 |
});
|
216 |
|
|
|
217 |
async function clearChat() {
|
218 |
try {
|
219 |
-
await fetch('/clear_chat', {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
if (currentConversationId) {
|
221 |
const conversation = conversations.find(c => c.id === currentConversationId);
|
222 |
if (conversation) {
|
@@ -229,7 +243,14 @@
|
|
229 |
}
|
230 |
}
|
231 |
|
232 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
startNewConversation();
|
234 |
</script>
|
235 |
</body>
|
|
|
47 |
<span id="fileName" class="ml-2 text-gray-600"></span>
|
48 |
</div>
|
49 |
|
50 |
+
<!-- Chat Messages -->
|
51 |
<div id="chatMessages" class="flex-1 overflow-y-auto mb-4 space-y-4 bg-gray-50 p-4 rounded-lg">
|
52 |
+
<!-- Messages will be displayed here -->
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
</div>
|
54 |
|
55 |
<!-- Input Area -->
|
|
|
69 |
</div>
|
70 |
|
71 |
<script>
|
72 |
+
// Global variables
|
73 |
const messageInput = document.getElementById('messageInput');
|
74 |
const chatMessages = document.getElementById('chatMessages');
|
75 |
const webSearchToggle = document.getElementById('webSearchToggle');
|
|
|
80 |
let conversations = [];
|
81 |
let currentConversationId = null;
|
82 |
|
83 |
+
// Conversation Management
|
84 |
function startNewConversation() {
|
85 |
const conversationId = Date.now().toString();
|
86 |
const conversation = {
|
|
|
107 |
}
|
108 |
|
109 |
function loadConversation(conversationId) {
|
110 |
+
if (conversationId === currentConversationId) return;
|
111 |
+
|
112 |
currentConversationId = conversationId;
|
113 |
const conversation = conversations.find(c => c.id === conversationId);
|
114 |
updateConversationsList();
|
115 |
|
116 |
if (conversation) {
|
117 |
clearChatMessages();
|
118 |
+
|
119 |
conversation.messages.forEach(msg => {
|
120 |
+
const messageDiv = document.createElement('div');
|
121 |
+
messageDiv.className = `flex ${msg.isUser ? 'justify-end' : ''}`;
|
122 |
+
|
123 |
+
const innerDiv = document.createElement('div');
|
124 |
+
innerDiv.className = `max-w-3/4 p-3 rounded-lg ${msg.isUser ? 'bg-blue-500 text-white' : 'bg-gray-200'}`;
|
125 |
+
innerDiv.innerHTML = marked.parse(msg.content);
|
126 |
+
|
127 |
+
messageDiv.appendChild(innerDiv);
|
128 |
+
chatMessages.appendChild(messageDiv);
|
129 |
});
|
130 |
+
|
131 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
132 |
}
|
133 |
}
|
134 |
|
|
|
148 |
chatMessages.appendChild(messageDiv);
|
149 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
150 |
|
|
|
151 |
if (currentConversationId) {
|
152 |
const conversation = conversations.find(c => c.id === currentConversationId);
|
153 |
if (conversation) {
|
|
|
156 |
}
|
157 |
}
|
158 |
|
159 |
+
// Message Handling
|
160 |
async function sendMessage() {
|
161 |
const message = messageInput.value.trim();
|
162 |
if (!message) return;
|
|
|
192 |
}
|
193 |
}
|
194 |
|
195 |
+
// File Upload Handling
|
|
|
|
|
|
|
|
|
|
|
196 |
fileUpload.addEventListener('change', async (e) => {
|
197 |
const file = e.target.files[0];
|
198 |
if (!file) return;
|
|
|
218 |
}
|
219 |
});
|
220 |
|
221 |
+
// Chat Clear Handling
|
222 |
async function clearChat() {
|
223 |
try {
|
224 |
+
await fetch('/clear_chat', {
|
225 |
+
method: 'POST',
|
226 |
+
headers: {
|
227 |
+
'Content-Type': 'application/json'
|
228 |
+
},
|
229 |
+
body: JSON.stringify({
|
230 |
+
conversation_id: currentConversationId
|
231 |
+
})
|
232 |
+
});
|
233 |
+
|
234 |
if (currentConversationId) {
|
235 |
const conversation = conversations.find(c => c.id === currentConversationId);
|
236 |
if (conversation) {
|
|
|
243 |
}
|
244 |
}
|
245 |
|
246 |
+
// Event Listeners
|
247 |
+
messageInput.addEventListener('keypress', (e) => {
|
248 |
+
if (e.key === 'Enter') {
|
249 |
+
sendMessage();
|
250 |
+
}
|
251 |
+
});
|
252 |
+
|
253 |
+
// Initialize
|
254 |
startNewConversation();
|
255 |
</script>
|
256 |
</body>
|