|
|
@@ -70,10 +70,31 @@ const blankMeta = () => ({
|
|
|
isStreaming: false,
|
|
|
});
|
|
|
|
|
|
+// crypto.randomUUID() 在低版本 WebView 或非 HTTPS 环境不可用,提供回退方案
|
|
|
+function generateUUID(): string {
|
|
|
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
|
|
+ return crypto.randomUUID();
|
|
|
+ }
|
|
|
+ // fallback: 用 crypto.getRandomValues 生成 UUID v4
|
|
|
+ if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
|
+ const bytes = new Uint8Array(16);
|
|
|
+ crypto.getRandomValues(bytes);
|
|
|
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
|
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
|
+ const hex = Array.from(bytes).map(b => b.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)}`;
|
|
|
+ }
|
|
|
+ // 最终回退: Math.random
|
|
|
+ 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);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
export const useChatStore = defineStore('chat', () => {
|
|
|
const messages = ref<Message[]>([]);
|
|
|
const isLoading = ref(false);
|
|
|
- const sessionId = ref<string>(crypto.randomUUID());
|
|
|
+ const sessionId = ref<string>(generateUUID());
|
|
|
const sessions = ref<Session[]>([]);
|
|
|
const sidebarOpen = ref(false);
|
|
|
|
|
|
@@ -101,7 +122,7 @@ export const useChatStore = defineStore('chat', () => {
|
|
|
};
|
|
|
|
|
|
const newConversation = () => {
|
|
|
- sessionId.value = crypto.randomUUID();
|
|
|
+ sessionId.value = generateUUID();
|
|
|
messages.value = [];
|
|
|
sidebarOpen.value = false;
|
|
|
};
|