| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- /**
- * Zustand Store —— 全局状态管理
- *
- * 需要跨窗口同步的状态通过 BroadcastService 广播。
- * 广播频道前缀 "state:" 表示状态同步类消息。
- */
- import { create } from "zustand";
- import type { IBroadcastService, BroadcastMessage } from "@zcode/services";
- import type { OAuthProviderId, UserInfo } from "@zcode/shared";
- import type { CodingPlanResetType } from "@zcode/shared";
- import type { CodePreviewSettings } from "@/lib/codePreviewSettings.js";
- import type {
- CodingPlanQuotaResetUiEntries,
- CodingPlanQuotaResetUiEntry,
- } from "@/lib/codingPlanQuotaResetUi.js";
- import {
- applyCodingPlanQuotaResetAutoPlayedBroadcast,
- createCodingPlanQuotaResetStoreActions,
- parseCodingPlanQuotaResetAutoPlayedBroadcastMessage,
- type CodingPlanQuotaResetAutomaticObservations,
- type CodingPlanQuotaResetAutoPlayReservation,
- type CodingPlanQuotaResetAutoPlayReservationAttempt,
- type CodingPlanQuotaResetAutoPlayedSlot,
- } from "@/store/codingPlanQuotaResetState.js";
- import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
- import { readSafeLocalStorage, writeSafeLocalStorage } from "@/lib/browserEnvironment.js";
- import {
- applyUiFontSizePx,
- loadUiFontSizePx,
- normalizeUiFontSizePx,
- UI_FONT_SIZE_STORAGE_KEY,
- } from "@/lib/uiFontSize.js";
- import {
- isTaskNotificationEnabled,
- isTaskNotificationSoundPreferenceEnabled,
- persistTaskNotificationEnabled,
- persistTaskNotificationSoundEnabled,
- } from "@/lib/taskNotificationPreferences.js";
- import type { Theme } from "../useTheme.js";
- import { applyTheme, normalizeThemePreference, resolveTheme } from "../useTheme.js";
- import { applySkin, loadPersistedSkinId, normalizeSkinId, persistSkinId, resolveSkinBase } from "../skins/applySkin.js";
- import {
- INTERFACE_MODE_STORAGE_KEY,
- normalizeInterfaceMode,
- type InterfaceMode,
- } from "@/lib/interfaceMode.js";
- import { logger } from "@/logger.js";
- export type LoginEntryPurpose = "app-login";
- // v4 重构:类型与默认值下沉到 @/lib/codePreviewSettings.ts,
- // 让纯展示组件不依赖 store;这里保留 re-export 兼容既有 import 路径。
- export type { CodePreviewSettings } from "@/lib/codePreviewSettings.js";
- export { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
- export type LoginEntryAttemptStatus =
- | "requested"
- | "waiting"
- | "succeeded"
- | "cancelled"
- | "failed";
- export interface LoginEntryAttempt {
- id: number;
- providerId?: OAuthProviderId;
- purpose?: LoginEntryPurpose;
- status: LoginEntryAttemptStatus;
- }
- const CODE_PREVIEW_SETTINGS_KEY = "zcode-code-preview-settings";
- const PERFORMANCE_MODE_STORAGE_KEY = "zcode-performance-mode";
- function loadCodePreviewSettings(): CodePreviewSettings {
- try {
- const raw = readSafeLocalStorage(CODE_PREVIEW_SETTINGS_KEY);
- if (!raw) {
- return DEFAULT_CODE_PREVIEW_SETTINGS;
- }
- const parsed = JSON.parse(raw) as Partial<CodePreviewSettings>;
- return {
- ...DEFAULT_CODE_PREVIEW_SETTINGS,
- ...parsed,
- fontSizePx:
- typeof parsed.fontSizePx === "number"
- ? Math.min(20, Math.max(12, Math.round(parsed.fontSizePx)))
- : DEFAULT_CODE_PREVIEW_SETTINGS.fontSizePx,
- };
- } catch {
- return DEFAULT_CODE_PREVIEW_SETTINGS;
- }
- }
- function loadPerformanceMode(): boolean {
- return readSafeLocalStorage(PERFORMANCE_MODE_STORAGE_KEY) === "true";
- }
- // ============================================================================
- // State 定义
- // ============================================================================
- export interface ZCodeState {
- /** 展示详情偏好,不改变 Agent 权限或执行能力。 */
- interfaceMode: InterfaceMode;
- setInterfaceMode: (mode: InterfaceMode) => void;
- /** 当前主题 */
- theme: Theme;
- setTheme: (theme: Theme) => void;
- skinId: string | null;
- setSkin: (skinId: string | null) => void;
- /** 当前语言 */
- locale: string;
- setLocale: (locale: string) => void;
- /** 代码预览设置 */
- codePreviewSettings: CodePreviewSettings;
- setCodePreviewSettings: (patch: Partial<CodePreviewSettings>) => void;
- /** UI 根 rem 字号(px) */
- uiFontSizePx: number;
- setUiFontSizePx: (fontSizePx: number) => void;
- /** 是否启用性能模式 */
- performanceMode: boolean;
- setPerformanceMode: (enabled: boolean) => void;
- /** 是否启用任务通知(桌面通知;提示音由子开关控制) */
- notificationEnabled: boolean;
- setNotificationEnabled: (enabled: boolean) => void;
- /** 是否启用任务通知声音(依附于任务通知总开关) */
- notificationSoundEnabled: boolean;
- setNotificationSoundEnabled: (enabled: boolean) => void;
- /** 当前用户信息 */
- user: UserInfo | null;
- /** 用户由未登录进入登录态时递增;连接额外 provider 不会误判为重新登录。 */
- authSessionSeq: number;
- setUser: (user: UserInfo | null) => void;
- /** 启动阶段是否仍在恢复 OAuth 登录态 */
- isRestoringOAuthSession: boolean;
- setIsRestoringOAuthSession: (restoring: boolean) => void;
- /** OAuth 回调错误(Root 层写入,统一登录入口读取) */
- oauthError: string | null;
- setOAuthError: (error: string | null) => void;
- oauthPollingActive: boolean;
- setOAuthPollingActive: (active: boolean) => void;
- oauthSuccessSeq: number;
- lastOAuthSuccessProvider: OAuthProviderId | null;
- markOAuthSuccess: (provider?: OAuthProviderId) => void;
- apiKeyLoginSuccessSeq: number;
- lastApiKeyLoginModel: string | null;
- markApiKeyLoginSuccess: (preferredModel?: string | null) => void;
- /** 请求打开统一登录入口,可携带需要自动发起登录/连接的 provider */
- loginEntryRequest: {
- id: number;
- providerId?: OAuthProviderId;
- purpose?: LoginEntryPurpose;
- } | null;
- /** 当前统一登录尝试;购买等后续动作通过 id 只续接自己发起的 OAuth。 */
- loginEntryAttempt: LoginEntryAttempt | null;
- requestLoginEntry: (providerId?: OAuthProviderId, purpose?: LoginEntryPurpose) => number;
- clearLoginEntryRequest: (requestId?: number) => void;
- markLoginEntryAttemptStatus: (
- requestId: number,
- status: Exclude<LoginEntryAttemptStatus, "requested">,
- ) => void;
- /** Coding Plan 额度重置 UI 状态;entry/观察记录只在当前窗口内共享,不持久化。 */
- codingPlanQuotaResetUiBySource: Record<string, CodingPlanQuotaResetUiEntries>;
- /** 自动/运营完成首次被观察时所属的鉴权会话,用于区分同会话后挂载和重新登录。 */
- codingPlanQuotaResetAutomaticObservationsBySource: Record<
- string,
- CodingPlanQuotaResetAutomaticObservations
- >;
- /** 自动完成提示"多窗口只播一次"的已播 used_at 记录;窗口内存态,可被广播合并。 */
- codingPlanQuotaResetAutoPlayedBySource: Record<string, CodingPlanQuotaResetAutoPlayedSlot>;
- /** 写入服务端 status / 手动 use 对账后的状态;entry 为 null 表示清空该类型。 */
- setCodingPlanQuotaResetUiEntry: (
- sourceKey: string,
- resetType: CodingPlanResetType,
- entry: CodingPlanQuotaResetUiEntry | null,
- authSessionSeq: number,
- ) => void;
- /** Composer 展示前申请临时 reservation;此阶段不写 played。 */
- reserveCodingPlanQuotaResetAutoPlay: (
- sourceKey: string,
- resetType: CodingPlanResetType,
- completedAt: number,
- ) => Promise<CodingPlanQuotaResetAutoPlayReservationAttempt>;
- /** 组件仍有效且即将展示时提交 reservation、played 与广播。 */
- commitCodingPlanQuotaResetAutoPlay: (
- reservation: CodingPlanQuotaResetAutoPlayReservation,
- ) => boolean;
- /** 组件失效时释放尚未 commit 的 reservation。 */
- releaseCodingPlanQuotaResetAutoPlay: (
- reservation: CodingPlanQuotaResetAutoPlayReservation,
- ) => Promise<void>;
- /** 手动请求打开 onboarding 弹窗 */
- newUserOnboardingOpen: boolean;
- setNewUserOnboardingOpen: (open: boolean) => void;
- onboardingDialogRequested: boolean | "migration";
- requestOnboardingDialog: (entry?: "migration") => void;
- clearOnboardingDialogRequest: () => void;
- }
- // ============================================================================
- // 需要广播的字段 —— 只有这些字段的变更会发送给其他窗口
- // ============================================================================
- const BROADCAST_FIELDS = new Set(["theme", "locale", "uiFontSizePx", "interfaceMode", "skinId"]);
- type BroadcastField = "theme" | "locale" | "uiFontSizePx" | "interfaceMode" | "skinId";
- /** 广播频道名前缀 */
- const STATE_CHANNEL_PREFIX = "state:";
- // ============================================================================
- // Store 创建工厂
- // ============================================================================
- /**
- * 创建 Zustand store,连接广播服务实现跨窗口状态同步
- *
- * @param broadcastService - 广播服务。Desktop 走 RPC,Web 可传 no-op 实现
- */
- export function createZCodeStore(
- broadcastService: IBroadcastService,
- options: {
- initialIsRestoringOAuthSession?: boolean;
- } = {},
- ) {
- /** 标记:正在应用来自广播的更新,此时不再重复广播(防止循环) */
- let applyingBroadcast = false;
- let loginEntryRequestSeq = 0;
- let cleanupSystemThemeListener: (() => void) | null = null;
- let syncSystemThemeListener = (_theme: Theme) => {};
- const useStore = create<ZCodeState>()((set, get) => ({
- interfaceMode: normalizeInterfaceMode(readSafeLocalStorage(INTERFACE_MODE_STORAGE_KEY)),
- setInterfaceMode: (mode) => {
- const interfaceMode = normalizeInterfaceMode(mode);
- if (get().interfaceMode !== interfaceMode) {
- logger.debug("[InterfaceMode] 切换界面模式", {
- interfaceMode,
- source: applyingBroadcast ? "broadcast" : "local",
- });
- }
- writeSafeLocalStorage(INTERFACE_MODE_STORAGE_KEY, interfaceMode);
- set({ interfaceMode });
- },
- // 默认主题统一收敛到 Zai dark,避免首次启动时 store 与其他主题入口表现不一致。
- // 仍然优先尊重 localStorage 中已保存的用户选择,不覆盖已有偏好。
- theme: normalizeThemePreference((readSafeLocalStorage("zcode-theme") as Theme) || "zai-dark"),
- setTheme: (theme: Theme) => {
- const normalizedTheme = normalizeThemePreference(theme);
- writeSafeLocalStorage("zcode-theme", normalizedTheme);
- syncSystemThemeListener(normalizedTheme);
- applyTheme(normalizedTheme);
- // base(亮/暗)变化可能令皮肤失去兼容性(manifest.bases),皮肤 class 必须重放。
- applySkin(get().skinId, resolveSkinBase(normalizedTheme));
- set({ theme: normalizedTheme });
- },
- // 皮肤 id 与主题分桶持久化(zcode-skin:v1),语义上「换风格」不是「换主题」。
- skinId: loadPersistedSkinId(),
- setSkin: (skinId: string | null) => {
- const normalizedSkinId = normalizeSkinId(skinId);
- persistSkinId(normalizedSkinId);
- applySkin(normalizedSkinId, resolveSkinBase(get().theme));
- set({ skinId: normalizedSkinId });
- },
- locale: readSafeLocalStorage("zcode-locale") || "zh-CN",
- setLocale: (locale: string) => {
- writeSafeLocalStorage("zcode-locale", locale);
- set({ locale });
- },
- codePreviewSettings: loadCodePreviewSettings(),
- setCodePreviewSettings: (patch: Partial<CodePreviewSettings>) =>
- set((state) => {
- const next = {
- ...state.codePreviewSettings,
- ...patch,
- fontSizePx:
- typeof patch.fontSizePx === "number"
- ? Math.min(20, Math.max(12, Math.round(patch.fontSizePx)))
- : state.codePreviewSettings.fontSizePx,
- };
- writeSafeLocalStorage(CODE_PREVIEW_SETTINGS_KEY, JSON.stringify(next));
- return { codePreviewSettings: next };
- }),
- uiFontSizePx: loadUiFontSizePx(),
- setUiFontSizePx: (fontSizePx: number) => {
- const normalizedFontSizePx = normalizeUiFontSizePx(fontSizePx);
- writeSafeLocalStorage(UI_FONT_SIZE_STORAGE_KEY, String(normalizedFontSizePx));
- applyUiFontSizePx(normalizedFontSizePx);
- applySkin(get().skinId, resolveSkinBase(get().theme));
- set({ uiFontSizePx: normalizedFontSizePx });
- },
- performanceMode: loadPerformanceMode(),
- setPerformanceMode: (enabled: boolean) => {
- writeSafeLocalStorage(PERFORMANCE_MODE_STORAGE_KEY, enabled ? "true" : "false");
- set({ performanceMode: enabled });
- },
- notificationEnabled: isTaskNotificationEnabled(),
- setNotificationEnabled: (enabled: boolean) => {
- persistTaskNotificationEnabled(enabled);
- set({ notificationEnabled: enabled });
- },
- notificationSoundEnabled: isTaskNotificationSoundPreferenceEnabled(),
- setNotificationSoundEnabled: (enabled: boolean) => {
- persistTaskNotificationSoundEnabled(enabled);
- set({ notificationSoundEnabled: enabled });
- },
- user: null,
- authSessionSeq: 0,
- setUser: (user: UserInfo | null) =>
- set((state) => ({
- user,
- authSessionSeq:
- state.user === null && user !== null ? state.authSessionSeq + 1 : state.authSessionSeq,
- })),
- isRestoringOAuthSession: options.initialIsRestoringOAuthSession ?? false,
- setIsRestoringOAuthSession: (restoring: boolean) => set({ isRestoringOAuthSession: restoring }),
- oauthError: null,
- setOAuthError: (error: string | null) => set({ oauthError: error }),
- oauthPollingActive: false,
- setOAuthPollingActive: (active: boolean) => set({ oauthPollingActive: active }),
- oauthSuccessSeq: 0,
- lastOAuthSuccessProvider: null,
- markOAuthSuccess: (provider?: OAuthProviderId) =>
- set((state) => ({
- oauthSuccessSeq: state.oauthSuccessSeq + 1,
- lastOAuthSuccessProvider: provider ?? state.lastOAuthSuccessProvider,
- })),
- apiKeyLoginSuccessSeq: 0,
- lastApiKeyLoginModel: null,
- markApiKeyLoginSuccess: (preferredModel?: string | null) =>
- set((state) => ({
- apiKeyLoginSuccessSeq: state.apiKeyLoginSuccessSeq + 1,
- lastApiKeyLoginModel: preferredModel?.trim() || null,
- })),
- loginEntryRequest: null,
- loginEntryAttempt: null,
- requestLoginEntry: (providerId?: OAuthProviderId, purpose?: LoginEntryPurpose) => {
- const id = ++loginEntryRequestSeq;
- const attempt: LoginEntryAttempt = {
- id,
- providerId,
- purpose,
- status: "requested",
- };
- set({
- loginEntryRequest: {
- id,
- providerId,
- purpose,
- },
- loginEntryAttempt: attempt,
- });
- return id;
- },
- clearLoginEntryRequest: (requestId?: number) =>
- set((state) => {
- if (requestId !== undefined && state.loginEntryRequest?.id !== requestId) {
- return {};
- }
- return { loginEntryRequest: null };
- }),
- markLoginEntryAttemptStatus: (requestId, status) =>
- set((state) => {
- if (state.loginEntryAttempt?.id !== requestId) {
- return {};
- }
- return {
- loginEntryAttempt: {
- ...state.loginEntryAttempt,
- status,
- },
- };
- }),
- codingPlanQuotaResetUiBySource: {},
- codingPlanQuotaResetAutomaticObservationsBySource: {},
- codingPlanQuotaResetAutoPlayedBySource: {},
- ...createCodingPlanQuotaResetStoreActions({
- broadcastService,
- readState: get,
- writeState: (updater) => set((state) => updater(state)),
- }),
- newUserOnboardingOpen: false,
- setNewUserOnboardingOpen: (open) => set({ newUserOnboardingOpen: open }),
- onboardingDialogRequested: false,
- requestOnboardingDialog: (entry) => set({ onboardingDialogRequested: entry ?? true }),
- clearOnboardingDialogRequest: () => set({ onboardingDialogRequested: false }),
- }));
- syncSystemThemeListener = (theme: Theme) => {
- cleanupSystemThemeListener?.();
- cleanupSystemThemeListener = null;
- if (theme !== "system" || typeof window === "undefined") {
- return;
- }
- const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
- const handleSystemThemeChange = () => {
- if (useStore.getState().theme !== "system") {
- return;
- }
- // system 模式需要持续订阅系统亮暗变化,不能只在切换到 system 的瞬间应用一次。
- // 否则用户后续切系统主题时,DOM 上的 dark class 不会同步更新,看起来就像“跟随系统失效”。
- applyTheme("system");
- };
- if (typeof mediaQuery.addEventListener === "function") {
- mediaQuery.addEventListener("change", handleSystemThemeChange);
- cleanupSystemThemeListener = () => {
- mediaQuery.removeEventListener("change", handleSystemThemeChange);
- };
- return;
- }
- // 某些 Electron / Chromium 组合仍然只支持旧版 MediaQueryList listener API。
- // 如果这里只调用 addEventListener,system 模式切系统主题时会完全收不到通知。
- mediaQuery.addListener(handleSystemThemeChange);
- cleanupSystemThemeListener = () => {
- mediaQuery.removeListener(handleSystemThemeChange);
- };
- };
- // Zustand v5 的 setState 在 replace=true/false 上使用了不同重载,
- // 之前直接包一层并透传 replace,会在严格类型下落到互不兼容的签名分支。
- // 这里改成订阅状态变化后再广播,只比较真正需要跨窗口同步的字段,逻辑更直观,也避免重载冲突。
- useStore.subscribe((state, prevState) => {
- if (applyingBroadcast) {
- return;
- }
- for (const field of BROADCAST_FIELDS as Set<BroadcastField>) {
- if (state[field] === prevState[field]) {
- continue;
- }
- broadcastService.send({
- channel: `${STATE_CHANNEL_PREFIX}${field}`,
- payload: state[field],
- });
- }
- });
- // 监听来自其他窗口的广播
- broadcastService.onMessage((msg: BroadcastMessage) => {
- // 自动完成"多窗口只播一次"。其他窗口广播已播 used_at 后,本窗口合并
- // played 记录并收起正在播放的同 used_at 提示;本地回声已在解析阶段被忽略。
- const autoPlayed = parseCodingPlanQuotaResetAutoPlayedBroadcastMessage(msg);
- if (autoPlayed) {
- useStore.setState((state) => applyCodingPlanQuotaResetAutoPlayedBroadcast(state, autoPlayed));
- return;
- }
- if (!msg.channel.startsWith(STATE_CHANNEL_PREFIX)) return;
- const field = msg.channel.slice(STATE_CHANNEL_PREFIX.length) as BroadcastField;
- if (!BROADCAST_FIELDS.has(field)) return;
- applyingBroadcast = true;
- try {
- // 调用对应的 setter,确保副作用(localStorage、DOM)也执行
- const state = useStore.getState();
- if (field === "theme" && typeof msg.payload === "string") {
- state.setTheme(msg.payload as Theme);
- } else if (field === "skinId" && (msg.payload === null || typeof msg.payload === "string")) {
- state.setSkin(msg.payload as string | null);
- } else if (field === "locale" && typeof msg.payload === "string") {
- state.setLocale(msg.payload);
- } else if (
- field === "interfaceMode" &&
- (msg.payload === "office" || msg.payload === "coding")
- ) {
- state.setInterfaceMode(normalizeInterfaceMode(msg.payload));
- } else if (field === "uiFontSizePx" && typeof msg.payload === "number") {
- state.setUiFontSizePx(msg.payload);
- }
- } finally {
- applyingBroadcast = false;
- }
- });
- syncSystemThemeListener(useStore.getState().theme);
- applyTheme(useStore.getState().theme);
- applyUiFontSizePx(useStore.getState().uiFontSizePx);
- applySkin(useStore.getState().skinId, resolveSkinBase(useStore.getState().theme));
- document.documentElement.classList.toggle(
- "dark",
- resolveTheme(useStore.getState().theme) === "dark",
- );
- return useStore;
- }
- export type ZCodeStore = ReturnType<typeof createZCodeStore>;
|