File size: 7,985 Bytes
fccc18d 6db7f7f fccc18d |
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 |
// events.js
import { addConversation, attachedFiles, updateFileAttachments } from './events_helper.js';
import { sessions, currentSessionIndex, getCurrentCardIndex, setCurrentCardIndex, updateCarousel } from './sessions.js';
import { updateHamburgerPosition, toggleLayout } from './navigation.js';
import { updateLastMessage } from './utils.js';
// Global variables for streaming state
window.isStreaming = false;
window.currentStreamController = null;
// File attachment events
const attachBtn = document.getElementById('attachBtn');
const fileInput = document.getElementById('fileInput');
attachBtn.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', () => {
for (const file of fileInput.files) {
attachedFiles.push(file);
}
fileInput.value = "";
updateFileAttachments();
});
// Send button event
const chatInput = document.getElementById('chatInput');
const sendBtn = document.getElementById('sendBtn');
sendBtn.addEventListener('click', async () => {
if (window.isStreaming) {
if (window.currentStreamController) {
window.currentStreamController.abort();
}
const session = sessions[currentSessionIndex];
const lastIndex = session.messages.length - 1;
const currentContent = session.messages[lastIndex].aiResponse;
// Use the imported updateLastMessage instead of window.updateLastMessage
const cleanedContent = currentContent.replace(`<span class="blinking-cursor"></span>`, '');
updateLastMessage(cleanedContent, false);
sendBtn.innerHTML = `<img src="assets/send.svg" alt="Send Icon" class="svg-icon-non-white">`;
window.isStreaming = false;
return;
}
const text = chatInput.value;
if (text.trim() !== '') {
await addConversation(text);
chatInput.value = '';
chatInput.style.height = '36px';
}
});
chatInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
sendBtn.click();
}
});
// Navigation events
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
prevBtn.addEventListener('click', () => {
if (getCurrentCardIndex() > 0) {
setCurrentCardIndex(getCurrentCardIndex() -1);
}
});
nextBtn.addEventListener('click', () => {
const cards = document.querySelectorAll('.card');
if (getCurrentCardIndex() < cards.length - 1) {
setCurrentCardIndex(getCurrentCardIndex() + 1);
}
});
// Hamburger toggle
const hamburgerBtn = document.getElementById('hamburgerBtn');
const navBar = document.getElementById('navBar');
hamburgerBtn.addEventListener('click', (e) => {
e.stopPropagation();
navBar.classList.toggle('hidden');
updateHamburgerPosition();
});
// Summary overlay events
const summaryOverlay = document.getElementById('summaryOverlay');
const closeSummaryBtn = document.getElementById('closeSummaryBtn');
const downloadSummaryBtn = document.getElementById('downloadSummary');
closeSummaryBtn.addEventListener('click', () => {
summaryOverlay.classList.remove('active');
});
downloadSummaryBtn.addEventListener('click', () => {
const blob = new Blob([sessions[currentSessionIndex].summary], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "summary.md";
a.click();
URL.revokeObjectURL(url);
});
// Settings overlay events
const settingsOverlay = document.getElementById('settingsOverlay');
const closeSettingsBtn = document.getElementById('closeSettingsBtn');
const temperatureInput = document.getElementById('temperature');
const temperatureValue = document.getElementById('temperatureValue');
const maxTokensInput = document.getElementById('maxTokens');
const personaSelect = document.getElementById('persona');
const saveSettingsBtn = document.getElementById('saveSettings');
const modelSelect = document.getElementById('modelSelect');
closeSettingsBtn.addEventListener('click', () => {
settingsOverlay.classList.remove('active');
});
temperatureInput.addEventListener('input', () => {
temperatureValue.textContent = temperatureInput.value;
});
saveSettingsBtn.addEventListener('click', () => {
const sessionSettings = sessions[currentSessionIndex].settings;
sessionSettings.temperature = parseFloat(temperatureInput.value);
sessionSettings.maxTokens = parseInt(maxTokensInput.value);
sessionSettings.persona = personaSelect.value;
sessionSettings.model = modelSelect.value;
console.log('Session settings saved:', sessions[currentSessionIndex].settings);
settingsOverlay.classList.remove('active');
const preset1 = document.getElementById('preset1');
const preset2 = document.getElementById('preset2');
preset1.value = "gpt-4o-mini";
preset2.value = "gpt-4o-mini";
preset2.classList.remove('active');
preset1.classList.remove('active');
});
const editTitleBtn = document.getElementById('editTitleBtn');
editTitleBtn.addEventListener('click', () => {
const currentTitle = sessions[currentSessionIndex].title;
const newTitle = prompt("Enter new chat title:", currentTitle);
if (newTitle !== null && newTitle.trim() !== "") {
sessions[currentSessionIndex].title = newTitle.trim();
document.getElementById('chatTitle').textContent = newTitle.trim();
}
});
// Custom buttons for summary and settings
const customBtn1 = document.getElementById('customBtn1');
const customBtn2 = document.getElementById('customBtn2');
customBtn1.addEventListener('click', (e) => {
e.stopPropagation();
document.getElementById('summaryContent').innerHTML = marked.parse(sessions[currentSessionIndex].summary);
summaryOverlay.classList.add('active');
settingsOverlay.classList.remove('active');
});
customBtn2.addEventListener('click', (e) => {
e.stopPropagation();
const settings = sessions[currentSessionIndex].settings;
temperatureInput.value = settings.temperature;
temperatureValue.textContent = settings.temperature;
maxTokensInput.value = settings.maxTokens;
personaSelect.value = settings.persona;
settingsOverlay.classList.add('active');
summaryOverlay.classList.remove('active');
openSettingsForCurrentSession();
});
function openSettingsForCurrentSession() {
const settings = sessions[currentSessionIndex].settings;
modelSelect.value = settings.model;
}
// Global keyboard navigation
document.addEventListener('keydown', (e) => {
const chatInput = document.getElementById('chatInput');
// Only trigger when the chat input is not focused
if (document.activeElement !== chatInput) {
if (e.key === 'ArrowLeft' && getCurrentCardIndex() > 0) {
setCurrentCardIndex(getCurrentCardIndex() - 1);
} else if (e.key === 'ArrowRight') {
const cards = document.querySelectorAll('.card');
if (getCurrentCardIndex() < cards.length - 1) {
setCurrentCardIndex(getCurrentCardIndex() + 1);
}
}
}
});
// Auto-dismiss overlays when clicking outside
document.addEventListener('click', (e) => {
if (summaryOverlay.classList.contains('active') && !summaryOverlay.contains(e.target)) {
summaryOverlay.classList.remove('active');
}
if (settingsOverlay.classList.contains('active') && !settingsOverlay.contains(e.target)) {
settingsOverlay.classList.remove('active');
}
});
const toggleLayoutBtn = document.getElementById('toggleLayoutBtn');
toggleLayoutBtn.addEventListener('click', () => {
toggleLayout();
// If needed, update any other UI parts (like turn labels)
});
const summarizeToggleBtn = document.getElementById('customBtn3');
summarizeToggleBtn.addEventListener('click', () => {
// Toggle the summarization flag for the current session.
const currentState = sessions[currentSessionIndex].settings.enableSummarization;
console.log('Current state:', currentState);
sessions[currentSessionIndex].settings.enableSummarization = !currentState;
// Update the button's visual state.
summarizeToggleBtn.classList.toggle('active', !currentState);
});
|