Devmightwin commited on
Commit
43cfeb6
Β·
verified Β·
1 Parent(s): dfc6d91

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +131 -99
index.html CHANGED
@@ -534,111 +534,143 @@ ${userPersona ? "Strict Personalization: " + userPersona : ""}`.trim();
534
  }
535
 
536
  async function handleAudioSend(audioBlob, mimeType) {
537
- if (isGenerating) return;
538
-
539
- const welcome = document.getElementById('welcome-screen');
540
- if (welcome) welcome.remove();
541
-
542
- appendMessage('user', 'πŸŽ™οΈ [Voice Note]');
543
- currentChatMessages.push({ role: 'user', text: '[Voice Note]' });
544
- setGeneratingState(true);
545
-
546
- thinking.classList.remove('hidden');
547
- chat.scrollTop = chat.scrollHeight;
548
-
549
- const base64Audio = await blobToBase64(audioBlob);
550
-
551
- const scriptConstraint = currentScript === 'roman'
552
- ? "\n[Constraint: Reply ONLY in Romanized Bodo ending in 'i' not 'y'. No Devanagari.]"
553
- : "\n[Constraint: Reply ONLY in Devanagari Bodo. No Roman script.]";
554
-
555
- const apiHistory = currentChatMessages.slice(-6).map((msg) => ({
556
- role: msg.role === 'user' ? 'user' : 'model',
557
- parts: [{ text: msg.text }]
558
- }));
559
-
560
- apiHistory[apiHistory.length - 1] = {
561
- role: 'user',
562
- parts: [
563
- {
564
- inlineData: {
565
- mimeType: mimeType,
566
- data: base64Audio
567
- }
568
- },
569
- { text: `Listen carefully to this spoken voice note and respond naturally following all your persona rules.${scriptConstraint}` }
570
- ]
571
- };
572
-
573
- abortController = new AbortController();
574
- let fullAiText = '';
575
-
576
- try {
577
- const res = await fetch(STREAM_API_URL, {
578
- method: 'POST',
579
- headers: { 'Content-Type': 'application/json' },
580
- signal: abortController.signal,
581
- body: JSON.stringify({
582
- system_instruction: { parts: [{ text: getSystemInstruction() }] },
583
- contents: apiHistory,
584
- generationConfig: { temperature: 0.5, maxOutputTokens: 2000 }
585
- })
586
- });
587
-
588
- if (!res.ok) {
589
- const errData = await res.json().catch(() => ({}));
590
- throw new Error(errData.error?.message || `HTTP ${res.status}`);
591
  }
592
-
593
- thinking.classList.add('hidden');
594
- const aiBubble = createBubbleElement('model');
595
-
596
- const reader = res.body.getReader();
597
- const decoder = new TextDecoder('utf-8');
598
- let buffer = '';
599
-
600
- while (true) {
601
- const { done, value } = await reader.read();
602
- if (done) break;
603
-
604
- buffer += decoder.decode(value, { stream: true });
605
- const lines = buffer.split('\n');
606
- buffer = lines.pop();
607
-
608
- for (const line of lines) {
609
- const trimmed = line.trim();
610
- if (trimmed.startsWith('data: ')) {
611
- const jsonStr = trimmed.slice(6).trim();
612
- if (!jsonStr) continue;
613
- try {
614
- const parsed = JSON.parse(jsonStr);
615
- const chunk = parsed.candidates?.[0]?.content?.parts?.[0]?.text || '';
616
- if (chunk) {
617
- fullAiText += chunk;
618
- aiBubble.innerHTML = renderMarkdown(fullAiText);
619
- chat.scrollTop = chat.scrollHeight;
620
- }
621
- } catch (e) {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
622
  }
623
- }
624
- }
625
-
626
- if (fullAiText.trim()) {
627
- currentChatMessages.push({ role: 'model', text: fullAiText.trim() });
628
- await saveCurrentThreadToCloud();
629
  }
630
- } catch (err) {
631
- if (err.name !== 'AbortError') {
632
- thinking.classList.add('hidden');
633
- appendMessage('model', `Voice processing error: ${err.message}`);
634
- }
635
- } finally {
636
- thinking.classList.add('hidden');
637
- setGeneratingState(false);
638
- abortController = null;
639
  }
640
  }
641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
642
  // TEXT SEND
643
  async function handleSend() {
644
  if (isGenerating) {
 
534
  }
535
 
536
  async function handleAudioSend(audioBlob, mimeType) {
537
+ if (isGenerating) return;
538
+
539
+ const welcome = document.getElementById('welcome-screen');
540
+ if (welcome) welcome.remove();
541
+
542
+ // Create user bubble with loading state
543
+ const userBubble = createBubbleElement('user');
544
+ userBubble.textContent = 'πŸŽ™οΈ Transcribing...';
545
+ currentChatMessages.push({ role: 'user', text: '[Voice Note]' });
546
+ setGeneratingState(true);
547
+
548
+ thinking.classList.remove('hidden');
549
+ chat.scrollTop = chat.scrollHeight;
550
+
551
+ const base64Audio = await blobToBase64(audioBlob);
552
+
553
+ const scriptConstraint = currentScript === 'roman'
554
+ ? "Reply ONLY in Romanized Bodo ending in 'i' not 'y'."
555
+ : "Reply ONLY in Devanagari Bodo.";
556
+
557
+ const apiHistory = currentChatMessages.slice(-6).map((msg) => ({
558
+ role: msg.role === 'user' ? 'user' : 'model',
559
+ parts: [{ text: msg.text }]
560
+ }));
561
+
562
+ // Force Gemini to split transcription and response
563
+ apiHistory[apiHistory.length - 1] = {
564
+ role: 'user',
565
+ parts: [
566
+ {
567
+ inlineData: {
568
+ mimeType: mimeType,
569
+ data: base64Audio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
  }
571
+ },
572
+ {
573
+ text: `1. Transcribe what was said in the audio accurately.
574
+ 2. Formulate your conversational reply as Orki.
575
+ Constraint: ${scriptConstraint}
576
+
577
+ Your output MUST strictly follow this exact format:
578
+ TRANSCRIPTION: <exact words the user spoke>
579
+ RESPONSE: <your witty reply>`
580
+ }
581
+ ]
582
+ };
583
+
584
+ abortController = new AbortController();
585
+ let fullAiText = '';
586
+
587
+ try {
588
+ const res = await fetch(STREAM_API_URL, {
589
+ method: 'POST',
590
+ headers: { 'Content-Type': 'application/json' },
591
+ signal: abortController.signal,
592
+ body: JSON.stringify({
593
+ system_instruction: { parts: [{ text: getSystemInstruction() }] },
594
+ contents: apiHistory,
595
+ generationConfig: { temperature: 0.4, maxOutputTokens: 2000 }
596
+ })
597
+ });
598
+
599
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
600
+
601
+ thinking.classList.add('hidden');
602
+ const aiBubble = createBubbleElement('model');
603
+
604
+ const reader = res.body.getReader();
605
+ const decoder = new TextDecoder('utf-8');
606
+ let buffer = '';
607
+
608
+ while (true) {
609
+ const { done, value } = await reader.read();
610
+ if (done) break;
611
+
612
+ buffer += decoder.decode(value, { stream: true });
613
+ const lines = buffer.split('\n');
614
+ buffer = lines.pop();
615
+
616
+ for (const line of lines) {
617
+ const trimmed = line.trim();
618
+ if (trimmed.startsWith('data: ')) {
619
+ const jsonStr = trimmed.slice(6).trim();
620
+ if (!jsonStr) continue;
621
+ try {
622
+ const parsed = JSON.parse(jsonStr);
623
+ const chunk = parsed.candidates?.[0]?.content?.parts?.[0]?.text || '';
624
+ if (chunk) {
625
+ fullAiText += chunk;
626
+
627
+ // Parse transcript vs reply during stream
628
+ if (fullAiText.includes('RESPONSE:')) {
629
+ const parts = fullAiText.split('RESPONSE:');
630
+ const transcript = parts[0].replace('TRANSCRIPTION:', '').trim();
631
+ const reply = parts.slice(1).join('RESPONSE:').trimStart();
632
+
633
+ userBubble.textContent = transcript ? `πŸŽ™οΈ "${transcript}"` : 'πŸŽ™οΈ [Voice Note]';
634
+ aiBubble.innerHTML = renderMarkdown(reply);
635
+ } else if (fullAiText.includes('TRANSCRIPTION:')) {
636
+ const liveTranscript = fullAiText.replace('TRANSCRIPTION:', '').trim();
637
+ userBubble.textContent = liveTranscript ? `πŸŽ™οΈ "${liveTranscript}..."` : 'πŸŽ™οΈ Transcribing...';
638
+ } else {
639
+ aiBubble.innerHTML = renderMarkdown(fullAiText);
640
+ }
641
+ chat.scrollTop = chat.scrollHeight;
642
  }
643
+ } catch (e) {}
 
 
 
 
 
644
  }
 
 
 
 
 
 
 
 
 
645
  }
646
  }
647
 
648
+ // Save final cleanly parsed text to Firestore history
649
+ if (fullAiText.includes('RESPONSE:')) {
650
+ const parts = fullAiText.split('RESPONSE:');
651
+ const finalTranscript = parts[0].replace('TRANSCRIPTION:', '').trim();
652
+ const finalReply = parts.slice(1).join('RESPONSE:').trim();
653
+
654
+ currentChatMessages[currentChatMessages.length - 1].text = finalTranscript || '[Voice Note]';
655
+ currentChatMessages.push({ role: 'model', text: finalReply });
656
+ await saveCurrentThreadToCloud();
657
+ } else if (fullAiText.trim()) {
658
+ currentChatMessages.push({ role: 'model', text: fullAiText.trim() });
659
+ await saveCurrentThreadToCloud();
660
+ }
661
+ } catch (err) {
662
+ if (err.name !== 'AbortError') {
663
+ thinking.classList.add('hidden');
664
+ userBubble.textContent = 'πŸŽ™οΈ [Failed Voice Note]';
665
+ appendMessage('model', `Voice processing error: ${err.message}`);
666
+ }
667
+ } finally {
668
+ thinking.classList.add('hidden');
669
+ setGeneratingState(false);
670
+ abortController = null;
671
+ }
672
+ }
673
+
674
  // TEXT SEND
675
  async function handleSend() {
676
  if (isGenerating) {