// static/js/state.js export let chatSessions = []; export let activeChatId = null; export let attachedFile = null; export let isGenerating = false; export let globalAbortController = null; export let currentUploadXHR = null; // این متغیر دیگر برای آپلود استفاده نمی‌شود اما برای لغو پردازش فایل نگه داشته می‌شود export let activeToolPrefix = null; export let activeTool = null; // *** START: ADDED FOR MESSAGE LIMIT *** const DAILY_MESSAGE_LIMIT = 10; const MESSAGE_LIMIT_KEY = 'alphaChatDailyLimit'; /** * Gets the current date as a formatted string (YYYY-MM-DD). * @returns {string} The formatted date string. */ function getTodayDateString() { const today = new Date(); const year = today.getFullYear(); const month = String(today.getMonth() + 1).padStart(2, '0'); const day = String(today.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } /** * Retrieves usage data from localStorage. * @returns {{count: number, date: string}} The usage data object. */ function getUsageData() { try { const saved = localStorage.getItem(MESSAGE_LIMIT_KEY); return saved ? JSON.parse(saved) : { count: 0, date: getTodayDateString() }; } catch (e) { console.error("Failed to parse usage data from localStorage:", e); return { count: 0, date: getTodayDateString() }; } } /** * Saves usage data to localStorage. * @param {{count: number, date: string}} data The usage data object to save. */ function saveUsageData(data) { try { localStorage.setItem(MESSAGE_LIMIT_KEY, JSON.stringify(data)); } catch (e) { console.error("Failed to save usage data to localStorage:", e); } } /** * Checks if the daily message limit has been reached for a free user. * @param {boolean} isPremium - Whether the current user is premium. * @returns {boolean} - True if the limit is reached, otherwise false. */ export function isMessageLimitReached(isPremium) { if (isPremium) { return false; // Premium users have no limit. } let usageData = getUsageData(); const today = getTodayDateString(); if (usageData.date !== today) { // It's a new day, reset the counter. usageData = { count: 0, date: today }; saveUsageData(usageData); } return usageData.count >= DAILY_MESSAGE_LIMIT; } /** * Increments the message count for a free user for the current day. * @param {boolean} isPremium - Whether the current user is premium. */ export function incrementMessageCount(isPremium) { if (isPremium) { return; // Don't track for premium users. } let usageData = getUsageData(); const today = getTodayDateString(); if (usageData.date !== today) { // If the day changed between the check and the increment, reset first. usageData = { count: 1, date: today }; } else { usageData.count += 1; } saveUsageData(usageData); } // *** END: ADDED FOR MESSAGE LIMIT *** export function setActiveTool(toolName) { activeTool = toolName; } export function getActiveTool() { return activeTool; } export function setActiveToolPrefix(prefix) { activeToolPrefix = prefix; } export function getActiveToolPrefix() { return activeToolPrefix; } export function setChatSessions(newSessions) { chatSessions = newSessions; } export function setActiveChatId(id) { activeChatId = id; } export function setAttachedFile(file) { attachedFile = file; } export function setGenerating(status) { isGenerating = status; } export function setGlobalAbortController(controller) { globalAbortController = controller; } export function setCurrentUploadXHR(xhr) { currentUploadXHR = xhr; } export function getActiveChat() { return chatSessions.find(s => s.id === activeChatId); } export function saveSessions() { try { const sessionsToSave = JSON.parse(JSON.stringify(chatSessions)); sessionsToSave.forEach(session => { session.messages.forEach(message => { if (message.parts) { message.parts.forEach(part => { if (part.base64Data) { delete part.base64Data; } if (part.blobUrl) { delete part.blobUrl; } }); } }); }); localStorage.setItem('alphaChatSessions', JSON.stringify(sessionsToSave)); } catch (e) { console.error("Failed to save sessions to localStorage:", e); } } export function loadSessions() { try { const saved = localStorage.getItem('alphaChatSessions'); chatSessions = saved ? JSON.parse(saved) : []; chatSessions.forEach(session => { if (session.showThoughts === undefined) { session.showThoughts = false; } // *** START: MODIFIED - افزودن فیلد جدید به پیام‌های قدیمی برای سازگاری *** session.messages.forEach(message => { if (message.role === 'assistant' && message.wasGeneratedWithThoughts === undefined) { message.wasGeneratedWithThoughts = false; } }); // *** END: MODIFIED *** }); } catch (e) { console.error("Failed to load sessions from localStorage:", e); chatSessions = []; } } export function findLastIndex(array, predicate) { for (let i = array.length - 1; i >= 0; i--) { if (predicate(array[i])) { return i; } } return -1; } export function deleteMessage(chatId, messageIndex) { const chat = chatSessions.find(s => s.id === chatId); if (chat && chat.messages[messageIndex]) { chat.messages.splice(messageIndex, 1); saveSessions(); } }