fix(webui): support UUID generation over LAN HTTP
This commit is contained in:
parent
1c59880f64
commit
6d69ba9272
@ -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;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
<script>
|
||||
import { onMount, tick } from "svelte";
|
||||
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 ToolCallCard from "../lib/ToolCallCard.svelte";
|
||||
|
||||
@ -146,7 +146,7 @@
|
||||
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 }];
|
||||
scrollToBottom();
|
||||
}
|
||||
@ -216,7 +216,7 @@
|
||||
|
||||
function uploadFile(file) {
|
||||
if (!connected) return notify("聊天连接尚未就绪", true);
|
||||
const localId = crypto.randomUUID();
|
||||
const localId = randomId();
|
||||
const localUrl = file.type.startsWith("image/") ? URL.createObjectURL(file) : null;
|
||||
pendingUploads = [...pendingUploads, {
|
||||
localId, name: file.name, size: file.size, progress: 0, status: "uploading", localUrl
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user