From 6d69ba9272963f0d4c8ae991519ef57fb7fef17a Mon Sep 17 00:00:00 2001 From: xiaoxixi Date: Thu, 16 Jul 2026 23:19:34 +0800 Subject: [PATCH] fix(webui): support UUID generation over LAN HTTP --- webui/src/lib/api.js | 21 ++++++++++++++++++++- webui/src/pages/ChatPage.svelte | 6 +++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/webui/src/lib/api.js b/webui/src/lib/api.js index 911fd5a..cd720a3 100644 --- a/webui/src/lib/api.js +++ b/webui/src/lib/api.js @@ -21,11 +21,30 @@ export function formatTime(value) { return Number.isNaN(date.getTime()) ? "—" : date.toLocaleString(); } +export function randomId() { + if (typeof globalThis.crypto?.randomUUID === "function") { + return globalThis.crypto.randomUUID(); + } + + const bytes = new Uint8Array(16); + if (typeof globalThis.crypto?.getRandomValues === "function") { + globalThis.crypto.getRandomValues(bytes); + } else { + for (let index = 0; index < bytes.length; index += 1) { + bytes[index] = Math.floor(Math.random() * 256); + } + } + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""); + return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; +} + export function clientId() { const key = "picobot_web_client_id"; let id = localStorage.getItem(key); if (!id) { - id = `web_${crypto.randomUUID().replaceAll("-", "").slice(0, 24)}`; + id = `web_${randomId().replaceAll("-", "").slice(0, 24)}`; localStorage.setItem(key, id); } return id; diff --git a/webui/src/pages/ChatPage.svelte b/webui/src/pages/ChatPage.svelte index 442eefd..c3ee14b 100644 --- a/webui/src/pages/ChatPage.svelte +++ b/webui/src/pages/ChatPage.svelte @@ -1,7 +1,7 @@