File size: 23,527 Bytes
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
1631029
fc03084
 
 
 
 
 
1631029
 
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
1631029
fc03084
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
1631029
 
fc03084
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
 
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
 
 
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1631029
fc03084
 
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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
document.addEventListener("DOMContentLoaded", () => {
  // Auto-resize textarea
  const textarea = document.getElementById("user-input")
  textarea.addEventListener("input", autoResizeTextarea)

  // Feature button selection
  const featureButtons = document.querySelectorAll(".feature-btn")
  const featureOptions = document.getElementById("feature-options")
  const fileUploadLabel = document.getElementById("file-upload-label")

  featureButtons.forEach((button) => {
    button.addEventListener("click", () => {
      // Remove active class from all buttons
      featureButtons.forEach((btn) => btn.classList.remove("active"))

      // Add active class to clicked button
      button.classList.add("active")

      // Show/hide file upload button based on feature
      const feature = button.dataset.feature
      if (feature === "document" || feature === "image") {
        fileUploadLabel.classList.remove("hidden")
      } else {
        fileUploadLabel.classList.add("hidden")
      }

      // Update feature options
      updateFeatureOptions(feature)

      // Save active feature to localStorage
      localStorage.setItem("activeFeature", feature)
    })
  })

  // Restore active feature from localStorage
  const activeFeature = localStorage.getItem("activeFeature") || "chat"
  document.querySelector(`.feature-btn[data-feature="${activeFeature}"]`)?.click()

  // File input handling
  const fileInput = document.getElementById("file-upload")
  fileInput.addEventListener("change", handleFileSelection)

  // Chat form submission
  const chatForm = document.getElementById("chat-form")
  chatForm.addEventListener("submit", handleChatSubmit)

  // History panel toggle
  const historyButton = document.createElement("button")
  historyButton.id = "historyButton"
  historyButton.className = "absolute left-4 top-4 p-2 rounded-full bg-white/20 hover:bg-white/30 transition-colors"
  historyButton.innerHTML = `
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
    </svg>
  `
  document.querySelector("header .container").appendChild(historyButton)

  historyButton.addEventListener("click", toggleHistoryPanel)
  document.getElementById("closeHistory").addEventListener("click", toggleHistoryPanel)

  // Theme toggle functionality
  const themeToggle = document.getElementById("theme-toggle")
  const sunIcon = document.getElementById("sun-icon")
  const moonIcon = document.getElementById("moon-icon")

  // Check for saved theme preference or use system preference
  const savedTheme = localStorage.getItem("theme")
  const systemPrefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches

  if (savedTheme === "dark" || (!savedTheme && systemPrefersDark)) {
    document.documentElement.classList.add("dark")
    sunIcon.classList.remove("hidden")
    moonIcon.classList.add("hidden")
  }

  // Toggle theme when button is clicked
  themeToggle.addEventListener("click", () => {
    const isDark = document.documentElement.classList.toggle("dark")

    // Toggle icons
    if (isDark) {
      sunIcon.classList.remove("hidden")
      moonIcon.classList.add("hidden")
      localStorage.setItem("theme", "dark")
    } else {
      sunIcon.classList.add("hidden")
      moonIcon.classList.remove("hidden")
      localStorage.setItem("theme", "light")
    }

    // Show notification
    showNotification(`Switched to ${isDark ? "dark" : "light"} mode`, "info")
  })

  // Load history from localStorage
  loadHistory()
})

// Auto-resize textarea
function autoResizeTextarea() {
  this.style.height = "auto"
  this.style.height = this.scrollHeight + "px"
}

// Update feature options based on selected feature
function updateFeatureOptions(feature) {
  const optionsContainer = document.getElementById("feature-options")

  // Clear previous options
  optionsContainer.innerHTML = ""

  switch (feature) {
    case "chat":
      // No special options for chat
      optionsContainer.classList.add("hidden")
      break

    case "translate":
      optionsContainer.classList.remove("hidden")
      optionsContainer.innerHTML = `
        <div class="language-selector">
          <div>
            <label for="sourceLanguage" class="block text-sm font-medium text-gray-700 mb-1">From:</label>
            <select id="sourceLanguage" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500 bg-white text-gray-900">
              <option value="ar">Arabic</option>
              <option value="en" selected>English</option>
              <option value="fr">French</option>
              <option value="es">Spanish</option>
              <option value="zh">Chinese</option>
            </select>
          </div>
          <div>
            <label for="targetLanguage" class="block text-sm font-medium text-gray-700 mb-1">To:</label>
            <select id="targetLanguage" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500 bg-white text-gray-900">
              <option value="ar" selected>Arabic</option>
              <option value="en">English</option>
              <option value="fr">French</option>
              <option value="es">Spanish</option>
              <option value="zh">Chinese</option>
            </select>
          </div>
        </div>
      `
      break

    case "qa":
      optionsContainer.classList.remove("hidden")
      optionsContainer.innerHTML = `
        <div class="mb-2">
          <label for="contextInput" class="block text-sm font-medium text-gray-700 mb-1">Reference Text:</label>
          <textarea id="contextInput" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500 bg-white text-gray-900 resize-none" rows="2" placeholder="Enter reference text here..."></textarea>
        </div>
      `
      // Auto-resize for the new textarea
      document.getElementById("contextInput").addEventListener("input", autoResizeTextarea)
      break

    default:
      optionsContainer.classList.add("hidden")
      break
  }
}

// Handle file selection
function handleFileSelection() {
  const fileInput = document.getElementById("file-upload")
  const userInput = document.getElementById("user-input")

  if (fileInput.files.length > 0) {
    const file = fileInput.files[0]

    // Create file preview element
    const filePreview = document.createElement("div")
    filePreview.className = "file-preview"
    filePreview.innerHTML = `
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" />
      </svg>
      <span class="file-preview-name">${file.name}</span>
      <span class="file-preview-remove" onclick="removeFile()">×</span>
    `

    // Add file preview after textarea
    const inputContainer = userInput.parentElement

    // Remove any existing file preview
    const existingPreview = inputContainer.querySelector(".file-preview")
    if (existingPreview) {
      existingPreview.remove()
    }

    inputContainer.appendChild(filePreview)

    // Update placeholder text
    userInput.placeholder = "Add a message about this file..."
  }
}

// Remove selected file
const removeFile = () => {
  const fileInput = document.getElementById("file-upload")
  const userInput = document.getElementById("user-input")
  const filePreview = document.querySelector(".file-preview")

  // Reset file input
  fileInput.value = ""

  // Remove file preview
  if (filePreview) {
    filePreview.remove()
  }

  // Reset placeholder
  userInput.placeholder = "Type your message..."
}

window.removeFile = removeFile

// Handle chat form submission
function handleChatSubmit(e) {
  e.preventDefault()

  const userInput = document.getElementById("user-input")
  const fileInput = document.getElementById("file-upload")
  const activeFeature = document.querySelector(".feature-btn.active").dataset.feature

  // Get user message
  const userMessage = userInput.value.trim()

  // Check if there's a message or file
  if (!userMessage && fileInput.files.length === 0) {
    showNotification("Please enter a message or select a file", "warning")
    return
  }

  // Add user message to chat
  if (userMessage) {
    addMessageToChat("user", userMessage)
  }

  // Process based on active feature
  processRequest(activeFeature, userMessage, fileInput.files[0])

  // Clear input and file
  userInput.value = ""
  userInput.style.height = "auto"

  if (fileInput.files.length > 0) {
    removeFile()
  }

  // Scroll to bottom
  scrollToBottom()
}

// Process request based on feature
function processRequest(feature, message, file) {
  // Show loading indicator
  showLoadingIndicator()

  // Determine which API endpoint to use
  let endpoint
  const formData = new FormData()

  switch (feature) {
    case "summarize":
      endpoint = "summarize"
      formData.append("text", message)
      break

    case "translate":
      endpoint = "translate"
      formData.append("text", message)
      formData.append("source_lang", document.getElementById("sourceLanguage").value)
      formData.append("target_lang", document.getElementById("targetLanguage").value)
      break

    case "qa":
      endpoint = "qa/"
      // Use fetch with JSON instead of FormData for QA
      const context = document.getElementById("contextInput").value.trim()
      if (!context) {
        hideLoadingIndicator()
        showNotification("Please enter reference text", "warning")
        return
      }
      // We'll handle this differently below
      break

    case "code":
      endpoint = "generate_code/"
      formData.append("prompt", message)
      break

    case "document":
      if (!file) {
        hideLoadingIndicator()
        showNotification("Please select a document file", "warning")
        return
      }
      endpoint = "extract_text"
      formData.append("file", file)
      break

    case "image":
      if (!file) {
        hideLoadingIndicator()
        showNotification("Please select an image file", "warning")
        return
      }
      endpoint = "image_caption"
      formData.append("file", file)
      break

    default:
      // For regular chat, we'll simulate a response
      setTimeout(() => {
        const responses = [
          "I'm here to help! What would you like to know?",
          "That's an interesting question. Let me think about it...",
          "I can assist with summarizing text, translating languages, answering questions, generating code, and analyzing documents and images. What would you like me to help you with?",
          "I understand. Is there anything specific you'd like me to explain further?",
          "I'm your AI assistant. I'm designed to be helpful, harmless, and honest.",
        ]
        const randomResponse = responses[Math.floor(Math.random() * responses.length)]
        addMessageToChat("assistant", randomResponse)
        hideLoadingIndicator()
        scrollToBottom()

        // Add to history
        addToHistory("Chat", message.substring(0, 30) + (message.length > 30 ? "..." : ""))
      }, 1000)
      return
  }

  // Special handling for QA
  if (feature === "qa") {
    const context = document.getElementById("contextInput").value.trim()
    const question = message

    fetch("https://soltane777-textgeneration.hf.space/qa/", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ context: context, question: question }),
    })
      .then((response) => {
        if (!response.ok) {
          throw new Error(`Server responded with status: ${response.status}`)
        }
        return response.json()
      })
      .then((data) => {
        const answer = data.answer || "Sorry, I couldn't find an answer to that question."
        addMessageToChat("assistant", answer)
        addToHistory("Question Answered", question.substring(0, 30) + (question.length > 30 ? "..." : ""))
      })
      .catch((error) => {
        console.error("Error:", error)
        addMessageToChat("assistant", `Error: ${error.message || "Failed to get an answer"}`)
        showNotification("Error getting answer: " + (error.message || "Unknown error"), "error")
      })
      .finally(() => {
        hideLoadingIndicator()
        scrollToBottom()
      })

    return
  }

  // Process other requests
  fetch(`https://soltane777-textgeneration.hf.space/${endpoint}`, {
    method: "POST",
    body: formData,
  })
    .then((response) => {
      if (!response.ok) {
        throw new Error(`Server responded with status: ${response.status}`)
      }
      return response.json()
    })
    .then((data) => {
      let result

      switch (feature) {
        case "summarize":
          result = data.summary || "Sorry, I couldn't summarize that text."
          addToHistory("Summarization", "Text summarized")
          break

        case "translate":
          const sourceLang = getLanguageName(document.getElementById("sourceLanguage").value)
          const targetLang = getLanguageName(document.getElementById("targetLanguage").value)
          result = data.translation || "Sorry, I couldn't translate that text."
          addToHistory("Translation", `${sourceLang} to ${targetLang}`)
          break

        case "code":
          result = formatCodeResponse(data)
          addToHistory("Code Generation", message.substring(0, 30) + (message.length > 30 ? "..." : ""))
          break

        case "document":
          result = data.text || "Sorry, I couldn't extract text from that document."
          addToHistory("Text Extraction", "Extracted from file")
          break

        case "image":
          result = data.caption || "Sorry, I couldn't analyze that image."
          addToHistory("Image Analysis", "Image analyzed")
          break

        default:
          result = JSON.stringify(data)
      }

      addMessageToChat("assistant", result)
    })
    .catch((error) => {
      console.error("Error:", error)
      addMessageToChat("assistant", `Error: ${error.message || "Failed to process request"}`)
      showNotification("Error: " + (error.message || "Unknown error"), "error")
    })
    .finally(() => {
      hideLoadingIndicator()
      scrollToBottom()
    })
}

// Format code response
function formatCodeResponse(data) {
  if (!data.code && !data[0]) {
    return "Sorry, I couldn't generate code for that prompt."
  }

  const code = data.code || data[0].generated_text || JSON.stringify(data)

  // Format as code block
  return "\`\`\`\n" + code + "\n\`\`\`"
}

// Add message to chat
function addMessageToChat(sender, message) {
  const chatMessages = document.getElementById("chat-messages")
  const messageDiv = document.createElement("div")

  if (sender === "user") {
    messageDiv.className = "flex justify-end mb-4"
    messageDiv.innerHTML = `
      <div class="max-w-3xl user-message p-4 shadow-sm">
        <p class="text-gray-800 whitespace-pre-wrap">${formatMessage(message)}</p>
      </div>
    `
  } else {
    messageDiv.className = "flex items-start mb-4"
    messageDiv.innerHTML = `
      <div class="flex-shrink-0 bg-purple-600 rounded-full p-2 mr-3">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
        </svg>
      </div>
      <div class="max-w-3xl assistant-message p-4 shadow-sm">
        <div class="text-gray-800 whitespace-pre-wrap">${formatMessage(message)}</div>
        <div class="message-actions">
          <button class="message-action-btn copy-btn" onclick="copyToClipboard(this)">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 mr-1 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
            </svg>
            Copy
          </button>
        </div>
      </div>
    `
  }

  chatMessages.appendChild(messageDiv)
}

// Format message with code blocks and links
function formatMessage(message) {
  // Replace code blocks
  message = message.replace(/\`\`\`([\s\S]*?)\`\`\`/g, (match, code) => {
    return `<pre class="code-content">${escapeHtml(code)}</pre>`
  })

  // Replace inline code
  message = message.replace(/`([^`]+)`/g, (match, code) => {
    return `<code class="bg-gray-100 px-1 py-0.5 rounded text-sm font-mono">${escapeHtml(code)}</code>`
  })

  // Replace URLs with links
  message = message.replace(/(https?:\/\/[^\s]+)/g, (url) => {
    return `<a href="${url}" target="_blank" class="text-purple-600 hover:underline">${url}</a>`
  })

  return message
}

// Escape HTML
function escapeHtml(unsafe) {
  return unsafe
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&#039;")
}

// Copy to clipboard
window.copyToClipboard = (button) => {
  const messageDiv = button.closest(".assistant-message")
  const textContent = messageDiv.querySelector(".whitespace-pre-wrap").innerText

  navigator.clipboard
    .writeText(textContent)
    .then(() => {
      // Change button text temporarily
      const originalText = button.innerHTML
      button.innerHTML = `
        <svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 mr-1 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
        </svg>
        Copied!
      `

      setTimeout(() => {
        button.innerHTML = originalText
      }, 2000)

      showNotification("Text copied to clipboard!")
    })
    .catch((err) => {
      console.error("Failed to copy text: ", err)
      showNotification("Failed to copy text: " + err.message, "error")
    })
}

// Show loading indicator
function showLoadingIndicator() {
  const chatMessages = document.getElementById("chat-messages")

  // Create loading message
  const loadingDiv = document.createElement("div")
  loadingDiv.id = "loading-indicator"
  loadingDiv.className = "flex items-start mb-4"
  loadingDiv.innerHTML = `
    <div class="flex-shrink-0 bg-purple-600 rounded-full p-2 mr-3">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
      </svg>
    </div>
    <div class="max-w-3xl assistant-message p-4 shadow-sm flex items-center">
      <div class="spinner mr-3"></div>
      <p class="text-gray-600">Thinking...</p>
    </div>
  `

  chatMessages.appendChild(loadingDiv)
  scrollToBottom()
}

// Hide loading indicator
function hideLoadingIndicator() {
  const loadingIndicator = document.getElementById("loading-indicator")
  if (loadingIndicator) {
    loadingIndicator.remove()
  }
}

// Scroll to bottom of chat
function scrollToBottom() {
  const chatMessages = document.getElementById("chat-messages")
  chatMessages.scrollTop = chatMessages.scrollHeight
}

// Show notification
function showNotification(message, type = "success") {
  const container = document.getElementById("notification-container")

  // Create notification element
  const notification = document.createElement("div")

  // Set class based on notification type
  if (type === "success") {
    notification.className =
      "notification mb-2 bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded shadow-md"
  } else if (type === "error") {
    notification.className = "notification mb-2 bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded shadow-md"
  } else if (type === "warning") {
    notification.className =
      "notification mb-2 bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700 p-4 rounded shadow-md"
  } else if (type === "info") {
    notification.className =
      "notification mb-2 bg-blue-100 border-l-4 border-blue-500 text-blue-700 p-4 rounded shadow-md"
  }

  notification.innerHTML = message

  // Add to container
  container.appendChild(notification)

  // Remove after 3 seconds
  setTimeout(() => {
    notification.style.opacity = "0"
    notification.style.transform = "translateY(-20px)"
    notification.style.transition = "opacity 0.5s, transform 0.5s"

    setTimeout(() => {
      notification.remove()
    }, 500)
  }, 3000)
}

// History panel functions
function toggleHistoryPanel() {
  const historyPanel = document.getElementById("historyPanel")
  if (historyPanel.classList.contains("translate-x-full")) {
    historyPanel.classList.remove("translate-x-full")
  } else {
    historyPanel.classList.add("translate-x-full")
  }
}

function addToHistory(action, details) {
  // Get existing history or initialize empty array
  const history = JSON.parse(localStorage.getItem("aiAppHistory") || "[]")

  // Add new item
  history.unshift({
    action,
    details,
    timestamp: new Date().toISOString(),
  })

  // Keep only the last 20 items
  if (history.length > 20) {
    history.pop()
  }

  // Save back to localStorage
  localStorage.setItem("aiAppHistory", JSON.stringify(history))

  // Update UI if history panel exists
  loadHistory()
}

function loadHistory() {
  const historyItemsContainer = document.getElementById("historyItems")
  if (!historyItemsContainer) return

  // Clear existing items
  historyItemsContainer.innerHTML = ""

  // Get history from localStorage
  const history = JSON.parse(localStorage.getItem("aiAppHistory") || "[]")

  if (history.length === 0) {
    historyItemsContainer.innerHTML = `<p class="text-gray-500 text-center">No history yet</p>`
    return
  }

  // Add each history item
  history.forEach((item) => {
    const historyItem = document.createElement("div")
    historyItem.className = "bg-gray-100 p-3 rounded-lg"

    const date = new Date(item.timestamp)
    const formattedDate = date.toLocaleDateString() + " " + date.toLocaleTimeString()

    historyItem.innerHTML = `
      <div class="flex justify-between items-start">
        <h4 class="font-medium text-gray-800">${item.action}</h4>
        <span class="text-xs text-gray-500">${formattedDate}</span>
      </div>
      <p class="text-sm text-gray-600 mt-1">${item.details}</p>
    `

    historyItemsContainer.appendChild(historyItem)
  })
}

// Helper function to get language name from code
function getLanguageName(code) {
  const languages = {
    ar: "Arabic",
    en: "English",
    fr: "French",
    es: "Spanish",
    zh: "Chinese",
  }
  return languages[code] || code
}