From 4fa9b0456ae711b44f9e016400384ab6091f0636 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Fri, 10 Apr 2026 04:19:45 +0000 Subject: [PATCH] fix: add fallback UUID generator for crypto.randomUUID compatibility crypto.randomUUID() is not available in all environments (e.g., older browsers, non-secure contexts). Added a fallback UUID v4 implementation. --- src/frontend/src/lib/stores/chatStore.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/frontend/src/lib/stores/chatStore.ts b/src/frontend/src/lib/stores/chatStore.ts index 58961a6..ffb2d63 100644 --- a/src/frontend/src/lib/stores/chatStore.ts +++ b/src/frontend/src/lib/stores/chatStore.ts @@ -8,12 +8,25 @@ export interface ChatMessage { timestamp: Date; } +// Fallback UUID generator for environments where crypto.randomUUID is not available +function generateId(): string { + if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { + return crypto.randomUUID(); + } + // Fallback: simple UUID v4 implementation + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const r = (Math.random() * 16) | 0; + const v = c === 'x' ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); +} + export const chatStore = writable([]); export function addMessage(message: Omit) { const newMessage: ChatMessage = { ...message, - id: crypto.randomUUID(), + id: generateId(), timestamp: new Date() }; chatStore.update(messages => [...messages, newMessage]);