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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Agent Chat</title>
    <link rel="icon" href="data:," />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.4/socket.io.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script type="module" src="https://cdn.jsdelivr.net/npm/emoji-picker-element"></script>
    <emoji-picker id="emoji-picker" style="position: absolute; bottom: 60px; right: 20px; display: none;"></emoji-picker>
    <style>

      * {

        box-sizing: border-box;

      }

      

      body {

        background: linear-gradient(135deg, rgb(44, 65, 112), #151f35);

        color: #f0f0f0;

        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

        margin: 0;

        padding: 0;

        overflow-x: hidden;

      }

      

      .container {

        max-width: 900px;

        margin: 40px auto;

        padding: 20px;

        border-radius: 20px;

        backdrop-filter: blur(12px);

        background: rgba(255, 255, 255, 0.04);

        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);

        transition: all 0.3s ease-in-out;

        width:90%;

      }

      

      h1 {

        text-align: center;

        font-size: 2.2rem;

        margin-bottom: 30px;

        color: #7ee8fa;

        letter-spacing: 1px;

        text-shadow: 0 2px 4px #000;

      }

      

      .chat-container {

        height: 450px;

        overflow-y: auto;

        padding: 20px;

        background: rgba(255, 255, 255, 0.05);

        border-radius: 15px;

        border: 1px solid rgba(255, 255, 255, 0.05);

        box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.25);

        backdrop-filter: blur(8px);

        animation: fadeInUp 0.6s ease;

      }

      @keyframes fadeInUp {

        from {

          opacity: 0;

          transform: translateY(15px);

        }

        to {

          opacity: 1;

          transform: translateY(0);

        }

      }

      /* Custom scroll bar styling */

      .chat-container::-webkit-scrollbar {

        width: 8px;

      }

      .chat-container::-webkit-scrollbar-track {

        background: #0f1b33;

        border-radius: 4px;

      }

      .chat-container::-webkit-scrollbar-thumb {

        background: #444;

        border-radius: 4px;

      }

      .chat-container::-webkit-scrollbar-thumb:hover {

        background: #555;

      }

      .chat-container {

        scrollbar-width: thin;

        scrollbar-color: #444 #0f1b33;

      }

      .message {

        margin-bottom: 15px;

        display: flex;

        align-items: flex-start;

        opacity: 0;

        animation: fadeIn 0.5s ease forwards;

      }

      .message.user {

        justify-content: flex-end;

      }

      .message.agent {

        justify-content: flex-start;

      }

      

      .bubble {

        padding: 8px 14px;

        font-size: 14px;

        line-height: 1.2;

        border-radius: 14px;

        max-width: 70%;

        word-wrap: break-word;

        position: relative;

        line-height: 1.5;

        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);

        transition: all 0.3s ease;

      }

      

      .bubble.user {

        background: linear-gradient(145deg, #007bff, #0056b3);

        color: white;

        border-bottom-right-radius: 0;

      }

      

      .bubble.agent {

        background: linear-gradient(145deg, #2d3a55, #1b2a3a);

        color: #f3f3f3;

        border-bottom-left-radius: 0;

      }

      

      .bubble.agent.thought-bubble {

        background: rgba(16, 52, 74, 0.8);

        font-style: italic;

        border-left: 4px solid #00e0ff;

        color: #7ee8fa;

      }

      

      .thought-toggle {

        color: #ff8800;

        font-size: 0.85rem;

        cursor: pointer;

        display: inline-block;

        margin-top: 5px;

      }

      

      .input-area {

        display: flex;

        gap: 10px;

        margin-top: 15px;

        align-items: center;

      }

      

      .input-area textarea {

        flex: 1;

        padding: 12px;

        border-radius: 10px;

        background: rgba(255, 255, 255, 0.08);

        color: #eaeaea;

        border: 1px solid rgba(255, 255, 255, 0.1);

        resize: none;

        transition: box-shadow 0.3s ease;

      }

      

      .input-area textarea:focus {

        outline: none;

        box-shadow: 0 0 8px #00e0ff;

      }

      

      .input-area button {

        padding: 12px 20px;

        border-radius: 10px;

        border: none;

        background: #00a8ff;

        color: #fff;

        cursor: pointer;

        transition: background 0.3s;

      }

      #upload-db {

        background: none;

        border: none;

        cursor: pointer;

        /* margin-left: 5px; */

      }

      

      #upload-db img {

        width: 28px;

        height: 28px;

      }

      .input-area button:disabled {

        background-color: #555;

        cursor: not-allowed;

      }

      

      .input-area button:hover:not(:disabled) {

        background: #0077c2;

      }

      

      .toast {

        position: fixed;

        bottom: 25px;

        right: 25px;

        background: #2c3e50;

        padding: 12px 22px;

        border-radius: 12px;

        font-size: 0.95rem;

        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);

        opacity: 0.95;

        display: none;

        z-index: 1000;

        transition: all 0.3s ease-in-out;

      }

      /* Typing animation dots */

      .dot {

        animation: blink 1.2s infinite;

        display: inline-block;

        margin-left: 2px;

      }

      .dot:nth-child(2) {

        animation-delay: 0.2s;

      }

      .dot:nth-child(3) {

        animation-delay: 0.4s;

      }

      

      @keyframes blink {

        0% {

          opacity: 0.2;

        }

        20% {

          opacity: 1;

        }

        100% {

          opacity: 0.2;

        }

      }

      

      @keyframes fadeIn {

        to {

          opacity: 1;

        }

      }

      .thought-toggle {

        margin-top: 4px;

        font-size: 0.8rem;

        color: #ffbb00;

        cursor: pointer;

        transition: color 0.3s;

      }

      .thought-toggle:hover {

        color: #fff000;

      }

    </style>
  </head>
  <body>
    <div class="container">
      <h1>Agent Chat</h1>
      <div class="chat-container" id="chat"></div>
      <div class="input-area">
        <textarea id="prompt" rows="2" placeholder="Type your message…"></textarea>
        <button id="upload-db" title="Upload Database"><img src="/static/db_icon.svg" alt="DB" /></button>
        <button id="send">Send</button>
        <!--<button id="emoji-button">😊</button>-->
        <input type="file" id="file-input" accept=".db" style="display:none;" />
      </div>
    </div>
    <div id="toast" class="toast"></div>

    <script>

      const socket = io()

      const chat = document.getElementById('chat')

      const toast = document.getElementById('toast')

      const sendBtn = document.getElementById('send')

      const promptTA = document.getElementById('prompt')

      const uploadBtn = document.getElementById('upload-db')

      const fileInput = document.getElementById('file-input')

      

      let typingBubble = null

      let agentBubble = null

      

      function addMessage(sender, html) {

        const msg = document.createElement('div')

        msg.className = `message ${sender}`

        const bubble = document.createElement('div')

        bubble.className = `bubble ${sender}`

        bubble.innerHTML = marked.parse(html)

        msg.appendChild(bubble)

        chat.appendChild(msg)

        chat.scrollTop = chat.scrollHeight

      }

      

      function showToast(text, duration = 3000) {

        toast.textContent = text

        toast.style.display = 'block'

        setTimeout(() => (toast.style.display = 'none'), duration)

      }

      

      // Upload DB

      uploadBtn.addEventListener('click', () => fileInput.click())

      fileInput.addEventListener('change', e => handleFile(e.target.files[0]));

      

      function handleFile(file) {

        if (!file) return // Get the uploaded file name

        showToast('Uploading database…')

        const fd = new FormData()

        fd.append('file', file)

        fetch('/upload_db', { method: 'POST', body: fd })

          .then((r) => r.json())

          .then((data) => {

            if (data.success || data.status === 'ok' || data.success === true) {

              showToast('Database initialized!', 2000)

              addMessage('agent', 'File uploaded and processed successfully!')

            } else {

              addMessage('agent', `Error: ${data.message}`)

            }

          })

          .catch((err) => addMessage('agent', `Upload error: ${err}`))

      }

      

      // Send Prompt

      function sendPrompt() {

        const text = promptTA.value.trim()

        if (!text || sendBtn.disabled) return

        addMessage('user', text)

        promptTA.value = ''

        sendBtn.disabled = true

      

        // typing indicator

        typingBubble = document.createElement('div')

        typingBubble.className = 'message agent'

        typingBubble.innerHTML = `<div class="bubble agent">Thinking<span class="dot">.</span><span class="dot">.</span><span class="dot">.</span></div>`

        chat.appendChild(typingBubble)

        chat.scrollTop = chat.scrollHeight

      

        fetch('/generate', {

          method: 'POST',

          headers: { 'Content-Type': 'application/json' },

          body: JSON.stringify({ prompt: text })

        }).catch((err) => {

          addMessage('agent', `Error: ${err}`)

          sendBtn.disabled = false

          if (typingBubble) chat.removeChild(typingBubble)

        })

      }

      

      // Send message

      sendBtn.addEventListener('click', () => {

        const text = promptTA.value.trim()

        if (!text) return

        addMessage('user', text)

        socket.emit('prompt', text)

        promptTA.value = ''

      })

      // Handle agent reply

      socket.on('response', (text) => {

        addMessage('agent', text)

      })

      // Handle agent thoughts

      socket.on('thought', (text) => {

        if (agentBubble) agentBubble.remove()

      

        const msg = document.createElement('div')

        msg.className = 'message agent'

      

        agentBubble = document.createElement('div')

        agentBubble.className = 'bubble agent thought-bubble'

        agentBubble.textContent = text

        msg.appendChild(agentBubble)

        chat.appendChild(msg)

        chat.scrollTop = chat.scrollHeight

      })

      // Enter to send when not processing

      promptTA.addEventListener('keydown', (e) => {

        if (e.key === 'Enter' && !e.shiftKey) {

          e.preventDefault()

          sendPrompt()

        }

      })

      

      // Socket.IO handlers

      socket.on('final_stream', (data) => {

        if (!agentBubble) {

          agentBubble = document.createElement('div')

          agentBubble.className = 'message agent'

          const b = document.createElement('div')

          b.className = 'bubble agent'

          agentBubble.appendChild(b)

          chat.appendChild(agentBubble)

        }

        const b = agentBubble.querySelector('.bubble')

        b.textContent += data.message

        chat.scrollTop = chat.scrollHeight

      })

      

      socket.on('final', (data) => {

        if (typingBubble) {

          chat.removeChild(typingBubble)

          typingBubble = null

        }

        agentBubble = null

        sendBtn.disabled = false

      

        // Typing animation

        const msg = document.createElement('div')

        msg.className = 'message agent'

        const bubble = document.createElement('div')

        bubble.className = 'bubble agent'

        msg.appendChild(bubble)

        chat.appendChild(msg)

        chat.scrollTop = chat.scrollHeight

      

        const text = data.message

        let index = 0

        const typingInterval = setInterval(() => {

          if (index < text.length) {

            bubble.textContent += text.charAt(index)

            index++

            chat.scrollTop = chat.scrollHeight

          } else {

            clearInterval(typingInterval)

          }

        }, 50) // Adjust typing speed here

      })

      function scrollToBottom() {

        chat.scrollTop = chat.scrollHeight

        // Alternatively, for smooth scrolling:

        // chat.lastElementChild.scrollIntoView({ behavior: 'smooth' });

      }

      /*const emojiPicker = document.getElementById('emoji-picker');

const emojiButton = document.getElementById('emoji-button');



// Toggle emoji picker

emojiButton.addEventListener('click', () => {

  emojiPicker.style.display = emojiPicker.style.display === 'none' ? 'block' : 'none';

});



// Insert emoji into textarea when selected

emojiPicker.addEventListener('emoji-click', event => {

  const emoji = event.detail.unicode;

  const cursorPos = promptTA.selectionStart;

  const text = promptTA.value;

  promptTA.value = text.slice(0, cursorPos) + emoji + text.slice(cursorPos);

  promptTA.focus();

}); */

    </script>
  </body>
</html>