Commit 8ce2079e authored by AI-甘富林's avatar AI-甘富林

多轮对话记忆前端修复

parent 50510818
...@@ -27,11 +27,17 @@ type LocalConversationStore = { ...@@ -27,11 +27,17 @@ type LocalConversationStore = {
const LOCAL_CONVERSATION_STORE_KEY = 'qianjiang_guest_conversations_v1'; const LOCAL_CONVERSATION_STORE_KEY = 'qianjiang_guest_conversations_v1';
const createLocalId = (prefix: string) => { const createLocalId = (_prefix: string) => {
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) { try {
return crypto.randomUUID(); if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
} return crypto.randomUUID();
return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`; }
} catch { /* 非 HTTPS 环境,使用降级方案 */ }
// Fallback: 生成合法 UUID v4 格式(与 makeUUID 一致,保证 DB 兼容)
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}; };
const readLocalStore = (): LocalConversationStore => { const readLocalStore = (): LocalConversationStore => {
......
...@@ -46,16 +46,23 @@ import { Label } from "@/components/ui/label"; ...@@ -46,16 +46,23 @@ import { Label } from "@/components/ui/label";
const GUEST_ID_KEY = 'qianjiang_guest_uuid'; const GUEST_ID_KEY = 'qianjiang_guest_uuid';
let _guestIdMemoryFallback: string | null = null; let _guestIdMemoryFallback: string | null = null;
let _activeConvId: string | null = null; // 模块级变量,绕过 React 闭包 let _activeConvId: string | null = null; // 模块级变量,绕过 React 闭包
function makeUUID(): string {
try { return crypto.randomUUID(); } catch { /* 非 HTTPS 环境 */ }
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
function getOrCreateGuestId(): string { function getOrCreateGuestId(): string {
try { try {
const existing = localStorage.getItem(GUEST_ID_KEY); const existing = localStorage.getItem(GUEST_ID_KEY);
if (existing) return existing; if (existing) return existing;
const id = crypto.randomUUID(); const id = makeUUID();
localStorage.setItem(GUEST_ID_KEY, id); localStorage.setItem(GUEST_ID_KEY, id);
return id; return id;
} catch { } catch {
// 无痕浏览等场景下 localStorage 不可用,用内存兜底保证同一会话内 ID 不变 // 无痕浏览等场景下 localStorage 不可用,用内存兜底保证同一会话内 ID 不变
if (!_guestIdMemoryFallback) _guestIdMemoryFallback = crypto.randomUUID(); if (!_guestIdMemoryFallback) _guestIdMemoryFallback = makeUUID();
return _guestIdMemoryFallback; return _guestIdMemoryFallback;
} }
} }
......
...@@ -27,15 +27,22 @@ import { callCozeEdge } from "@/utils/cozeClient"; ...@@ -27,15 +27,22 @@ import { callCozeEdge } from "@/utils/cozeClient";
// ─── 访客 ID + 会话 ID 模块级变量(绕过 React 闭包)─── // ─── 访客 ID + 会话 ID 模块级变量(绕过 React 闭包)───
const GUEST_ID_KEY = 'qianjiang_guest_uuid'; const GUEST_ID_KEY = 'qianjiang_guest_uuid';
let _guestIdMemoryFallback: string | null = null; let _guestIdMemoryFallback: string | null = null;
function makeUUID(): string {
try { return crypto.randomUUID(); } catch { /* 非 HTTPS 环境 */ }
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
function getOrCreateGuestId(): string { function getOrCreateGuestId(): string {
try { try {
const existing = localStorage.getItem(GUEST_ID_KEY); const existing = localStorage.getItem(GUEST_ID_KEY);
if (existing) return existing; if (existing) return existing;
const id = crypto.randomUUID(); const id = makeUUID();
localStorage.setItem(GUEST_ID_KEY, id); localStorage.setItem(GUEST_ID_KEY, id);
return id; return id;
} catch { } catch {
if (!_guestIdMemoryFallback) _guestIdMemoryFallback = crypto.randomUUID(); if (!_guestIdMemoryFallback) _guestIdMemoryFallback = makeUUID();
return _guestIdMemoryFallback; return _guestIdMemoryFallback;
} }
} }
......
...@@ -3,9 +3,14 @@ ...@@ -3,9 +3,14 @@
* 优先使用 crypto.randomUUID(),不可用时回退到时间戳+随机数 * 优先使用 crypto.randomUUID(),不可用时回退到时间戳+随机数
*/ */
export function generateUUID(): string { export function generateUUID(): string {
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) { try {
return crypto.randomUUID(); if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
} return crypto.randomUUID();
// Fallback: timestamp + random string }
return `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`; } catch { /* 非 HTTPS 环境,使用降级方案 */ }
// Fallback: UUID v4 格式(保证 DB UUID 列兼容)
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment