fix(desktop): 替换 require() 为 ES import — 修复生产构建崩溃
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled

- connectionStore: 2 处 require() → loadConversationStore() 异步预加载 + 闭包引用
- saasStore: 1 处 require() → await import()(logout 是 async)
- llm-service: 1 处 require() → 顶层 import(无循环依赖)
- streamStore: 移除重复动态导入,统一使用顶层 useConnectionStore
- tsc --noEmit 0 errors
This commit is contained in:
iven
2026-04-15 00:47:29 +08:00
parent a8a0751005
commit 02a4ba5e75
4 changed files with 18 additions and 13 deletions

View File

@@ -30,6 +30,16 @@ import { useConfigStore } from './configStore';
import { createLogger } from '../lib/logger';
import { secureStorage } from '../lib/secure-storage';
// 延迟加载 conversationStore 避免循环依赖
// connect() 是 async 函数,在其中 await import() 是安全的
let _conversationStore: typeof import('./chat/conversationStore') | null = null;
async function loadConversationStore() {
if (!_conversationStore) {
try { _conversationStore = await import('./chat/conversationStore'); } catch { /* not loaded yet */ }
}
return _conversationStore;
}
const log = createLogger('ConnectionStore');
// === Mode Selection ===
@@ -492,8 +502,8 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => {
// 优先使用 conversationStore 的 currentModel如果设置了的话
let preferredModel: string | undefined;
try {
const { useConversationStore } = require('./chat/conversationStore');
preferredModel = useConversationStore.getState().currentModel;
const cs = await loadConversationStore();
preferredModel = cs?.useConversationStore.getState().currentModel;
} catch {
// conversationStore 可能尚未初始化
}
@@ -553,13 +563,9 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => {
);
const relayClient = createSaaSRelayGatewayClient(session.saasUrl, () => {
// 每次调用时读取 conversationStore 的 currentModelfallback 到第一个可用模型
try {
const { useConversationStore } = require('./chat/conversationStore');
const current = useConversationStore.getState().currentModel;
return (current && validBrowserModelIds.has(current)) ? current : fallbackModelId;
} catch {
return fallbackModelId;
}
// 注意:这里不能用 await同步回调但 conversationStore 已在上方 loadConversationStore() 中加载
const current = _conversationStore?.useConversationStore.getState().currentModel;
return (current && validBrowserModelIds.has(current)) ? current : fallbackModelId;
});
set({