feat(id): replace crypto.randomUUID with nanoid for unique ID generation

This commit is contained in:
HouYunFei
2026-07-15 11:55:36 +08:00
parent a4dcc679c9
commit ee8f126da7
5 changed files with 24 additions and 4 deletions
@@ -5,6 +5,7 @@ import copyToClipboard from "copy-to-clipboard";
import { Copy, FolderOpen, History, KeyRound, Link2, LoaderCircle, PlugZap, Plus, RefreshCw, Square, Terminal, Trash2 } from "lucide-react";
import { canvasThemes } from "@/lib/canvas-theme";
import { randomId } from "@/lib/utils";
import { useThemeStore } from "@/stores/use-theme-store";
import { useUserStore } from "@/stores/use-user-store";
import { useAgentStore, type AgentAttachment, type AgentChatItem, type AgentEventLog, type AgentPanelTab, type AgentPendingToolCall, type AgentThreadSummary } from "@/stores/use-agent-store";
@@ -54,7 +55,7 @@ export function CanvasLocalAgentPanel({ embedded, headless, autoConnect }: { emb
const connectedRef = useRef(false);
const errorLoggedRef = useRef(false);
const attachmentUrlsRef = useRef(new Set<string>());
const clientIdRef = useRef(typeof crypto === "undefined" ? `${Date.now()}` : crypto.randomUUID());
const clientIdRef = useRef(randomId());
const endpoint = useMemo(() => url.trim().replace(/\/$/, ""), [url]);
const urlAgentAutoConnect = searchParams.has("agentUrl") && searchParams.has("agentToken");
const loadThreads = useCallback(async () => {
@@ -1045,7 +1046,7 @@ function formatThreadTime(value?: number) {
}
function createId() {
return typeof crypto === "undefined" ? `${Date.now()}-${Math.random()}` : crypto.randomUUID();
return randomId();
}
function clamp(value: number, min: number, max: number) {
+12
View File
@@ -1,6 +1,18 @@
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import { nanoid } from "nanoid";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
/**
* Generate a unique id.
*
* Avoid `crypto.randomUUID`, which is only exposed in secure contexts (HTTPS or
* localhost) — over plain HTTP it is undefined and throws. `nanoid` works in any
* context.
*/
export function randomId(): string {
return nanoid();
}