fix(webui): support UUID generation over LAN HTTP

This commit is contained in:
xiaoxixi 2026-07-16 23:19:34 +08:00
parent 1c59880f64
commit 6d69ba9272
2 changed files with 23 additions and 4 deletions

View File

@ -21,11 +21,30 @@ export function formatTime(value) {
return Number.isNaN(date.getTime()) ? "—" : date.toLocaleString(); 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() { export function clientId() {
const key = "picobot_web_client_id"; const key = "picobot_web_client_id";
let id = localStorage.getItem(key); let id = localStorage.getItem(key);
if (!id) { if (!id) {
id = `web_${crypto.randomUUID().replaceAll("-", "").slice(0, 24)}`; id = `web_${randomId().replaceAll("-", "").slice(0, 24)}`;
localStorage.setItem(key, id); localStorage.setItem(key, id);
} }
return id; return id;

View File

@ -1,7 +1,7 @@
<script> <script>
import { onMount, tick } from "svelte"; import { onMount, tick } from "svelte";
import { Tooltip } from "bits-ui"; import { Tooltip } from "bits-ui";
import { clientId, formatTime } from "../lib/api.js"; import { clientId, formatTime, randomId } from "../lib/api.js";
import Markdown from "../lib/Markdown.svelte"; import Markdown from "../lib/Markdown.svelte";
import ToolCallCard from "../lib/ToolCallCard.svelte"; import ToolCallCard from "../lib/ToolCallCard.svelte";
@ -146,7 +146,7 @@
if (messageBox) messageBox.scrollTop = messageBox.scrollHeight; if (messageBox) messageBox.scrollTop = messageBox.scrollHeight;
} }
function appendMessage(role, content, attachments = [], id = crypto.randomUUID()) { function appendMessage(role, content, attachments = [], id = randomId()) {
messages = [...messages, { id, role, content, attachments }]; messages = [...messages, { id, role, content, attachments }];
scrollToBottom(); scrollToBottom();
} }
@ -216,7 +216,7 @@
function uploadFile(file) { function uploadFile(file) {
if (!connected) return notify("聊天连接尚未就绪", true); if (!connected) return notify("聊天连接尚未就绪", true);
const localId = crypto.randomUUID(); const localId = randomId();
const localUrl = file.type.startsWith("image/") ? URL.createObjectURL(file) : null; const localUrl = file.type.startsWith("image/") ? URL.createObjectURL(file) : null;
pendingUploads = [...pendingUploads, { pendingUploads = [...pendingUploads, {
localId, name: file.name, size: file.size, progress: 0, status: "uploading", localUrl localId, name: file.name, size: file.size, progress: 0, status: "uploading", localUrl