Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

32.1 KB
Raw Blame
import type { LogLevel, LogNamespace } from '@platform/logging/contracts';
import type {
  ProjectSortId,
  ProjectsViewId,
  ScopedDefaults,
  StoredAspectRatioPreset,
  StoredGeneratePreset,
  StoredRebalancePreset,
  WorkbenchPreferences,
  WorkflowLibrarySortId,
} from '@workbench/settings/contracts';

import { getUserStorageScope } from '@features/identity';
import { normalizeWorkbenchLanguage } from '@platform/i18n/languages';
import { isLogLevel, isLogNamespace, LOG_LEVELS, LOG_NAMESPACES } from '@platform/logging/contracts';
import {
  assertAccountScopeCurrent,
  captureAccountScope,
  isAccountScopeCurrent,
  registerAccountOwnedResource,
  type AccountScope,
} from '@platform/state/accountLifecycle';
import { createExternalStore } from '@platform/state/externalStore';
import { createSingleFlight } from '@platform/state/singleFlight';
import { isPromptFontSize } from '@theme/scale';
import { DEFAULT_THEME_ID, resolveWorkbenchThemeId } from '@theme/themes';
import { deleteClientStateValue, getClientStateValue, setClientStateValue } from '@workbench/projects/api';
import { fetchSessionBlob } from '@workbench/projects/session';
import { DEFAULT_SELECT_OBJECT_AUTO_UPDATE } from '@workbench/settings/contracts';
import { getLayeredKeys } from '@workbench/settings/projectSettings';

export { WORKBENCH_LANGUAGES } from '@platform/i18n/languages';

const SETTINGS_BASE_STORAGE_KEY = 'invokeai:v7:webv2:settings';
const LEGACY_WORKBENCH_BASE_STORAGE_KEY = 'invokeai:v7:webv2:workbench';
const SETTINGS_CLIENT_STATE_KEY = 'webv2:workbench-settings';

export const DEVELOPER_LOG_LEVELS: readonly LogLevel[] = LOG_LEVELS;

export const DEVELOPER_LOG_NAMESPACES: readonly LogNamespace[] = LOG_NAMESPACES;

export const DEFAULT_PREFERENCES: WorkbenchPreferences = {
  betaNoticeAcknowledged: false,
  antialiasProgressImages: false,
  aspectRatioPresets: [],
  autoSwitchInvocationRoute: true,
  canvasToolMemory: {},
  confirmImageDeletion: true,
  controlModelMemory: {},
  customHotkeys: {},
  developerConsoleOutputEnabled: false,
  developerLogEnabled: true,
  developerLogLevel: 'warn',
  developerLogNamespaces: [...LOG_NAMESPACES],
  developerPerformanceTimingsEnabled: false,
  enableInformationalPopovers: true,
  enableModelDescriptions: true,
  exportIncludesWorkspace: true,
  galleryDestinationNoticeShown: false,
  generatePresets: [],
  generatePresetsIncludePrompts: true,
  generateSectionsOpen: {},
  highContrast: false,
  installInPlace: true,
  krea2RebalancePresets: [],
  language: 'en',
  launchpadPinnedProjectIds: [],
  launchpadProjectsSort: 'edited',
  launchpadProjectsView: 'grid',
  notifyOnEnqueue: true,
  preferNumericAttentionStyle: false,
  promptFontSize: 'default',
  queueJobsScope: 'all',
  reduceMotion: false,
  scopedDefaults: {},
  selectObjectAutoUpdate: DEFAULT_SELECT_OBJECT_AUTO_UPDATE,
  showPromptSyntaxHighlighting: true,
  showFocusRegionHighlight: true,
  startupDestination: 'launchpad',
  themeId: DEFAULT_THEME_ID,
  whatsNewSeenVersion: null,
  workflowEdgeStyle: 'curved',
  workflowEdgesBehindNodes: false,
  workflowGroupNodesByCategory: true,
  workflowLibraryCardSize: 'small',
  workflowLibraryLayout: 'grid',
  workflowLibrarySort: 'updated',
  workflowLibrarySortDirection: 'desc',
  workflowLibraryThumbnailFit: 'cover',
  workflowShowMinimap: true,
  workflowSnapToGrid: false,
  workflowValidateConnections: true,
};

interface WorkbenchSettingsSnapshot {
  hydratedStorageKey: string | null;
  preferences: WorkbenchPreferences;
  scope: 'global' | 'user';
  status: 'idle' | 'loading' | 'ready' | 'error';
  error?: string;
}

const INITIAL_SETTINGS_SNAPSHOT: WorkbenchSettingsSnapshot = {
  hydratedStorageKey: null,
  preferences: DEFAULT_PREFERENCES,
  scope: 'global',
  status: 'idle',
};
const store = createExternalStore<WorkbenchSettingsSnapshot>(INITIAL_SETTINGS_SNAPSHOT);

interface SettingsMutationQueue {
  latestRevision: number;
  owner: AccountScope;
  tail: Promise<void>;
}

let settingsMutationQueue: SettingsMutationQueue | null = null;

registerAccountOwnedResource({
  clear: () => {
    settingsMutationQueue = null;
    store.setSnapshot(INITIAL_SETTINGS_SNAPSHOT);
  },
  name: 'workbench-settings',
});

const isBrowser = (): boolean => typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';

const getSettingsStorageKey = (storageSuffix = getUserStorageScope()): string =>
  `${SETTINGS_BASE_STORAGE_KEY}${storageSuffix}`;

const getLegacyWorkbenchStorageKey = (storageSuffix = getUserStorageScope()): string =>
  `${LEGACY_WORKBENCH_BASE_STORAGE_KEY}${storageSuffix}`;

const getSettingsScope = (storageSuffix = getUserStorageScope()): WorkbenchSettingsSnapshot['scope'] =>
  storageSuffix ? 'user' : 'global';

/** The pre-2026-09 default; installs that merely persisted it never chose a narrowed selection. */
const LEGACY_DEFAULT_LOG_NAMESPACES: readonly LogNamespace[] = ['queue', 'system', 'workflows'];

/** A user-narrowed selection stays narrowed, including namespaces added after it was saved; reset restores defaults. */
const normalizeDeveloperLogNamespaces = (values: unknown): LogNamespace[] => {
  if (!Array.isArray(values)) {
    return [...DEFAULT_PREFERENCES.developerLogNamespaces];
  }

  const enabled = new Set(values.filter(isLogNamespace));
  const selected = LOG_NAMESPACES.filter((namespace) => enabled.has(namespace));
  const isLegacyDefault =
    selected.length === LEGACY_DEFAULT_LOG_NAMESPACES.length &&
    selected.every((namespace, index) => namespace === LEGACY_DEFAULT_LOG_NAMESPACES[index]);

  return isLegacyDefault ? [...DEFAULT_PREFERENCES.developerLogNamespaces] : selected;
};

/** Keep view guards local so shared settings do not pull Launchpad code into the editor bundle. */
const isProjectsViewId = (value: unknown): value is ProjectsViewId => value === 'grid' || value === 'list';

const isProjectSortId = (value: unknown): value is ProjectSortId =>
  value === 'edited' || value === 'created' || value === 'name';

const isWorkflowLibrarySortId = (value: unknown): value is WorkflowLibrarySortId =>
  value === 'updated' || value === 'opened' || value === 'created' || value === 'name' || value === 'lastRun';

/** Accounts that chose a key before directions existed keep its natural order. */
const normalizeWorkflowLibrarySortDirection = (direction: unknown, sort: WorkflowLibrarySortId) =>
  direction === 'asc' || direction === 'desc' ? direction : sort === 'name' ? 'asc' : 'desc';

const normalizePinnedProjectIds = (values: unknown): string[] =>
  Array.isArray(values) ? [...new Set(values.filter((value): value is string => typeof value === 'string'))] : [];

const normalizeCustomHotkeys = (values: unknown): Record<string, string[]> => {
  if (!values || typeof values !== 'object' || Array.isArray(values)) {
    return {};
  }

  return Object.fromEntries(
    Object.entries(values)
      .filter(
        (entry): entry is [string, string[]] =>
          typeof entry[0] === 'string' &&
          Array.isArray(entry[1]) &&
          entry[1].every((hotkey) => typeof hotkey === 'string')
      )
      .map(([id, hotkeys]) => [id, hotkeys])
  );
};

const normalizeGenerateSectionsOpen = (values: unknown): Record<string, boolean> => {
  if (!values || typeof values !== 'object' || Array.isArray(values)) {
    return {};
  }

  return Object.fromEntries(
    Object.entries(values).filter(
      (entry): entry is [string, boolean] => typeof entry[0] === 'string' && typeof entry[1] === 'boolean'
    )
  );
};

const normalizeRebalancePresets = (values: unknown): StoredRebalancePreset[] => {
  if (!Array.isArray(values)) {
    return [];
  }

  const seen = new Set<string>();
  const presets: StoredRebalancePreset[] = [];

  for (const entry of values) {
    if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
      continue;
    }

    const { id, label, multiplier, weights } = entry as Partial<StoredRebalancePreset>;

    if (
      typeof id !== 'string' ||
      id.trim() === '' ||
      seen.has(id) ||
      typeof label !== 'string' ||
      label.trim() === '' ||
      typeof weights !== 'string' ||
      typeof multiplier !== 'number' ||
      !Number.isFinite(multiplier)
    ) {
      continue;
    }

    seen.add(id);
    presets.push({ id, label: label.trim(), multiplier, weights });
  }

  return presets;
};

const normalizeGeneratePresets = (values: unknown): StoredGeneratePreset[] => {
  if (!Array.isArray(values)) {
    return [];
  }

  const seen = new Set<string>();
  const presets: StoredGeneratePreset[] = [];

  for (const entry of values) {
    if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
      continue;
    }

    const { id, label, values: snapshot } = entry as Partial<StoredGeneratePreset>;

    if (
      typeof id !== 'string' ||
      id.trim() === '' ||
      seen.has(id) ||
      typeof label !== 'string' ||
      label.trim() === '' ||
      !snapshot ||
      typeof snapshot !== 'object' ||
      Array.isArray(snapshot)
    ) {
      continue;
    }

    seen.add(id);
    presets.push({ id, label: label.trim(), values: snapshot });
  }

  return presets;
};

/** Spelled out to keep the canvas engine out of Launchpad's bundle; a store test keeps it in step with `CONTROL_TYPES`. */
const CONTROL_MODEL_MEMORY_KEY = /^[^:]+:(?:pose|depth|edges|softedge|lineart|sketch|color|tile)$/;
const CONTROL_MODEL_MEMORY_MAX_ENTRIES = 256;
const CONTROL_MODEL_KEY_MAX_LENGTH = 255;

const isPlainRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
  typeof value === 'object' && value !== null && !Array.isArray(value);

const isStoredAspectRatioPreset = (entry: unknown): entry is StoredAspectRatioPreset => {
  if (!isPlainRecord(entry)) {
    return false;
  }

  const { height, id, label, width } = entry;

  return (
    typeof id === 'string' &&
    id !== '' &&
    typeof label === 'string' &&
    label.trim() !== '' &&
    typeof width === 'number' &&
    Number.isFinite(width) &&
    typeof height === 'number' &&
    Number.isFinite(height)
  );
};

/** Shape-only, so a newer build's wider ratio rules survive this one's writes; a valid list keeps its identity. */
const normalizeAspectRatioPresetRecords = (values: unknown): StoredAspectRatioPreset[] => {
  if (!Array.isArray(values)) {
    return DEFAULT_PREFERENCES.aspectRatioPresets;
  }

  const seen = new Set<string>();
  const kept = values.filter((entry): entry is StoredAspectRatioPreset => {
    if (!isStoredAspectRatioPreset(entry) || seen.has(entry.id)) {
      return false;
    }

    seen.add(entry.id);
    return true;
  });

  return kept.length === values.length ? (values as StoredAspectRatioPreset[]) : kept;
};

const CANVAS_TOOL_MEMORY_KEYS = ['brush', 'eraser', 'shape', 'text'] as const;

/** Known tools only, so the synced blob cannot grow; a valid record keeps its identity. */
const normalizeCanvasToolMemoryRecord = (value: unknown): Readonly<Record<string, unknown>> => {
  if (!isPlainRecord(value)) {
    return DEFAULT_PREFERENCES.canvasToolMemory;
  }
  const keys = Object.keys(value);
  if (keys.every((key) => (CANVAS_TOOL_MEMORY_KEYS as readonly string[]).includes(key) && isPlainRecord(value[key]))) {
    return value;
  }
  return Object.fromEntries(
    CANVAS_TOOL_MEMORY_KEYS.flatMap((key) => (isPlainRecord(value[key]) ? [[key, value[key]]] : []))
  );
};

/** Drops keys no owner layers; a clean record keeps its identity so resolved widget values stay stable. */
const normalizeScopedDefaults = (value: unknown): ScopedDefaults => {
  if (!isPlainRecord(value)) {
    return DEFAULT_PREFERENCES.scopedDefaults;
  }
  let changed = false;
  const kept: Record<string, Readonly<Record<string, unknown>>> = {};
  for (const [ownerId, record] of Object.entries(value)) {
    const layered = getLayeredKeys(ownerId);
    if (!layered || !isPlainRecord(record)) {
      changed = true;
      continue;
    }
    const entries = Object.entries(record);
    const layeredEntries = entries.filter(([key]) => Object.hasOwn(layered, key));
    if (layeredEntries.length !== entries.length) {
      changed = true;
    }
    if (layeredEntries.length > 0) {
      kept[ownerId] = layeredEntries.length === entries.length ? record : Object.fromEntries(layeredEntries);
    } else if (entries.length > 0) {
      changed = true;
    }
  }
  return changed ? kept : (value as ScopedDefaults);
};

/** Keeps the newest entries so the synced blob stays bounded; a valid record keeps its identity. */
const normalizeControlModelMemory = (values: unknown): Record<string, string> => {
  if (!values || typeof values !== 'object' || Array.isArray(values)) {
    return {};
  }

  const entries = Object.entries(values);
  const kept = entries
    .filter(
      (entry): entry is [string, string] =>
        CONTROL_MODEL_MEMORY_KEY.test(entry[0]) &&
        typeof entry[1] === 'string' &&
        entry[1].length > 0 &&
        entry[1].length <= CONTROL_MODEL_KEY_MAX_LENGTH
    )
    .slice(-CONTROL_MODEL_MEMORY_MAX_ENTRIES);

  return kept.length === entries.length ? (values as Record<string, string>) : Object.fromEntries(kept);
};

type WorkbenchPreferencesInput = Omit<
  Partial<WorkbenchPreferences>,
  | 'launchpadProjectsSort'
  | 'launchpadProjectsView'
  | 'queueJobsScope'
  | 'startupDestination'
  | 'workflowEdgeStyle'
  | 'workflowLibraryCardSize'
  | 'workflowLibraryLayout'
  | 'workflowLibrarySort'
  | 'workflowLibrarySortDirection'
  | 'workflowLibraryThumbnailFit'
> & {
  launchpadProjectsSort?: unknown;
  launchpadProjectsView?: unknown;
  queueJobsScope?: unknown;
  startupDestination?: unknown;
  workflowEdgeStyle?: unknown;
  workflowLibraryCardSize?: unknown;
  workflowLibraryLayout?: unknown;
  workflowLibrarySort?: unknown;
  workflowLibrarySortDirection?: unknown;
  workflowLibraryThumbnailFit?: unknown;
  /** Pre-beta name of `betaNoticeAcknowledged`; accounts that dismissed the alpha notice skip the beta one. */
  alphaNoticeAcknowledged?: unknown;
};

export const normalizeWorkbenchPreferences = (preferences?: WorkbenchPreferencesInput): WorkbenchPreferences => ({
  antialiasProgressImages: preferences?.antialiasProgressImages === true,
  aspectRatioPresets: normalizeAspectRatioPresetRecords(preferences?.aspectRatioPresets),
  autoSwitchInvocationRoute:
    typeof preferences?.autoSwitchInvocationRoute === 'boolean'
      ? preferences.autoSwitchInvocationRoute
      : DEFAULT_PREFERENCES.autoSwitchInvocationRoute,
  canvasToolMemory: normalizeCanvasToolMemoryRecord(preferences?.canvasToolMemory),
  confirmImageDeletion:
    typeof preferences?.confirmImageDeletion === 'boolean'
      ? preferences.confirmImageDeletion
      : DEFAULT_PREFERENCES.confirmImageDeletion,
  controlModelMemory: normalizeControlModelMemory(preferences?.controlModelMemory),
  customHotkeys: normalizeCustomHotkeys(preferences?.customHotkeys),
  developerLogEnabled:
    typeof preferences?.developerLogEnabled === 'boolean'
      ? preferences.developerLogEnabled
      : DEFAULT_PREFERENCES.developerLogEnabled,
  developerConsoleOutputEnabled:
    typeof preferences?.developerConsoleOutputEnabled === 'boolean'
      ? preferences.developerConsoleOutputEnabled
      : DEFAULT_PREFERENCES.developerConsoleOutputEnabled,
  developerLogLevel: isLogLevel(preferences?.developerLogLevel)
    ? preferences.developerLogLevel
    : DEFAULT_PREFERENCES.developerLogLevel,
  developerLogNamespaces: normalizeDeveloperLogNamespaces(preferences?.developerLogNamespaces),
  developerPerformanceTimingsEnabled:
    typeof preferences?.developerPerformanceTimingsEnabled === 'boolean'
      ? preferences.developerPerformanceTimingsEnabled
      : DEFAULT_PREFERENCES.developerPerformanceTimingsEnabled,
  enableInformationalPopovers:
    typeof preferences?.enableInformationalPopovers === 'boolean'
      ? preferences.enableInformationalPopovers
      : DEFAULT_PREFERENCES.enableInformationalPopovers,
  enableModelDescriptions:
    typeof preferences?.enableModelDescriptions === 'boolean'
      ? preferences.enableModelDescriptions
      : DEFAULT_PREFERENCES.enableModelDescriptions,
  exportIncludesWorkspace:
    typeof preferences?.exportIncludesWorkspace === 'boolean'
      ? preferences.exportIncludesWorkspace
      : DEFAULT_PREFERENCES.exportIncludesWorkspace,
  generatePresets: normalizeGeneratePresets(preferences?.generatePresets),
  generatePresetsIncludePrompts: preferences?.generatePresetsIncludePrompts !== false,
  generateSectionsOpen: normalizeGenerateSectionsOpen(preferences?.generateSectionsOpen),
  installInPlace:
    typeof preferences?.installInPlace === 'boolean' ? preferences.installInPlace : DEFAULT_PREFERENCES.installInPlace,
  krea2RebalancePresets: normalizeRebalancePresets(preferences?.krea2RebalancePresets),
  language: normalizeWorkbenchLanguage(preferences?.language) ?? DEFAULT_PREFERENCES.language,
  launchpadPinnedProjectIds: normalizePinnedProjectIds(preferences?.launchpadPinnedProjectIds),
  launchpadProjectsSort: isProjectSortId(preferences?.launchpadProjectsSort)
    ? preferences.launchpadProjectsSort
    : DEFAULT_PREFERENCES.launchpadProjectsSort,
  launchpadProjectsView: isProjectsViewId(preferences?.launchpadProjectsView)
    ? preferences.launchpadProjectsView
    : DEFAULT_PREFERENCES.launchpadProjectsView,
  notifyOnEnqueue:
    typeof preferences?.notifyOnEnqueue === 'boolean'
      ? preferences.notifyOnEnqueue
      : DEFAULT_PREFERENCES.notifyOnEnqueue,
  preferNumericAttentionStyle:
    typeof preferences?.preferNumericAttentionStyle === 'boolean'
      ? preferences.preferNumericAttentionStyle
      : DEFAULT_PREFERENCES.preferNumericAttentionStyle,
  promptFontSize: isPromptFontSize(preferences?.promptFontSize)
    ? preferences.promptFontSize
    : DEFAULT_PREFERENCES.promptFontSize,
  queueJobsScope:
    preferences?.queueJobsScope === 'all-projects'
      ? 'all'
      : preferences?.queueJobsScope === 'active-project' || preferences?.queueJobsScope === 'all'
        ? preferences.queueJobsScope
        : DEFAULT_PREFERENCES.queueJobsScope,
  highContrast:
    typeof preferences?.highContrast === 'boolean' ? preferences.highContrast : DEFAULT_PREFERENCES.highContrast,
  betaNoticeAcknowledged:
    typeof preferences?.betaNoticeAcknowledged === 'boolean'
      ? preferences.betaNoticeAcknowledged
      : preferences?.alphaNoticeAcknowledged === true || DEFAULT_PREFERENCES.betaNoticeAcknowledged,
  galleryDestinationNoticeShown: preferences?.galleryDestinationNoticeShown === true,
  reduceMotion:
    typeof preferences?.reduceMotion === 'boolean' ? preferences.reduceMotion : DEFAULT_PREFERENCES.reduceMotion,
  scopedDefaults: normalizeScopedDefaults(preferences?.scopedDefaults),
  selectObjectAutoUpdate:
    typeof preferences?.selectObjectAutoUpdate === 'boolean'
      ? preferences.selectObjectAutoUpdate
      : DEFAULT_PREFERENCES.selectObjectAutoUpdate,
  showFocusRegionHighlight:
    typeof preferences?.showFocusRegionHighlight === 'boolean'
      ? preferences.showFocusRegionHighlight
      : DEFAULT_PREFERENCES.showFocusRegionHighlight,
  showPromptSyntaxHighlighting:
    typeof preferences?.showPromptSyntaxHighlighting === 'boolean'
      ? preferences.showPromptSyntaxHighlighting
      : DEFAULT_PREFERENCES.showPromptSyntaxHighlighting,
  startupDestination:
    preferences?.startupDestination === 'launchpad' || preferences?.startupDestination === 'recentProject'
      ? preferences.startupDestination
      : DEFAULT_PREFERENCES.startupDestination,
  themeId: resolveWorkbenchThemeId(preferences?.themeId) ?? DEFAULT_PREFERENCES.themeId,
  whatsNewSeenVersion:
    typeof preferences?.whatsNewSeenVersion === 'string'
      ? preferences.whatsNewSeenVersion
      : DEFAULT_PREFERENCES.whatsNewSeenVersion,
  workflowEdgeStyle:
    preferences?.workflowEdgeStyle === 'square' || preferences?.workflowEdgeStyle === 'straight'
      ? 'square'
      : preferences?.workflowEdgeStyle === 'curved'
        ? preferences.workflowEdgeStyle
        : DEFAULT_PREFERENCES.workflowEdgeStyle,
  workflowEdgesBehindNodes:
    typeof preferences?.workflowEdgesBehindNodes === 'boolean'
      ? preferences.workflowEdgesBehindNodes
      : DEFAULT_PREFERENCES.workflowEdgesBehindNodes,
  workflowGroupNodesByCategory:
    typeof preferences?.workflowGroupNodesByCategory === 'boolean'
      ? preferences.workflowGroupNodesByCategory
      : DEFAULT_PREFERENCES.workflowGroupNodesByCategory,
  workflowLibraryCardSize:
    preferences?.workflowLibraryCardSize === 'large' ? 'large' : DEFAULT_PREFERENCES.workflowLibraryCardSize,
  workflowLibraryLayout:
    preferences?.workflowLibraryLayout === 'list' ? 'list' : DEFAULT_PREFERENCES.workflowLibraryLayout,
  workflowLibrarySort: isWorkflowLibrarySortId(preferences?.workflowLibrarySort)
    ? preferences.workflowLibrarySort
    : DEFAULT_PREFERENCES.workflowLibrarySort,
  workflowLibrarySortDirection: normalizeWorkflowLibrarySortDirection(
    preferences?.workflowLibrarySortDirection,
    isWorkflowLibrarySortId(preferences?.workflowLibrarySort)
      ? preferences.workflowLibrarySort
      : DEFAULT_PREFERENCES.workflowLibrarySort
  ),
  workflowLibraryThumbnailFit:
    preferences?.workflowLibraryThumbnailFit === 'contain'
      ? 'contain'
      : DEFAULT_PREFERENCES.workflowLibraryThumbnailFit,
  workflowShowMinimap:
    typeof preferences?.workflowShowMinimap === 'boolean'
      ? preferences.workflowShowMinimap
      : DEFAULT_PREFERENCES.workflowShowMinimap,
  workflowSnapToGrid:
    typeof preferences?.workflowSnapToGrid === 'boolean'
      ? preferences.workflowSnapToGrid
      : DEFAULT_PREFERENCES.workflowSnapToGrid,
  workflowValidateConnections:
    typeof preferences?.workflowValidateConnections === 'boolean'
      ? preferences.workflowValidateConnections
      : DEFAULT_PREFERENCES.workflowValidateConnections,
});

const parsePreferences = (raw: string | null): WorkbenchPreferences | null => {
  if (!raw) {
    return null;
  }

  try {
    return normalizeWorkbenchPreferences(JSON.parse(raw) as Partial<WorkbenchPreferences>);
  } catch {
    return null;
  }
};

/** A pending local copy outranks the server at the next load, so going offline cannot silently revert a change. */
interface StoredSettings {
  preferences: WorkbenchPreferences;
  pendingPush?: boolean;
}

const readLocalSettings = (storageKey: string): StoredSettings | null => {
  if (!isBrowser()) {
    return null;
  }

  try {
    const parsed = JSON.parse(window.localStorage.getItem(storageKey) ?? 'null') as
      | (Partial<StoredSettings> & Partial<WorkbenchPreferences>)
      | null;

    if (!parsed || typeof parsed !== 'object') {
      return null;
    }

    // Early builds stored the bare preferences object.
    if (!parsed.preferences) {
      return { preferences: normalizeWorkbenchPreferences(parsed) };
    }

    return {
      pendingPush: parsed.pendingPush === true || undefined,
      preferences: normalizeWorkbenchPreferences(parsed.preferences),
    };
  } catch {
    return null;
  }
};

const writeLocalSettings = (storageKey: string, preferences: WorkbenchPreferences, pendingPush?: boolean): void => {
  if (!isBrowser()) {
    return;
  }

  const payload: StoredSettings = pendingPush ? { pendingPush, preferences } : { preferences };

  window.localStorage.setItem(storageKey, JSON.stringify(payload));
};

const removeLocalSettings = (storageKey: string): void => {
  if (!isBrowser()) {
    return;
  }

  window.localStorage.removeItem(storageKey);
};

const readLegacyLocalPreferences = (storageSuffix: string): WorkbenchPreferences | null => {
  if (!isBrowser()) {
    return null;
  }

  try {
    const raw = window.localStorage.getItem(getLegacyWorkbenchStorageKey(storageSuffix));
    const parsed = raw ? (JSON.parse(raw) as { state?: { account?: { preferences?: unknown } } }) : null;

    return parsed?.state?.account?.preferences
      ? normalizeWorkbenchPreferences(parsed.state.account.preferences as Partial<WorkbenchPreferences>)
      : null;
  } catch {
    return null;
  }
};

const loadLegacySessionPreferences = async (owner: AccountScope): Promise<WorkbenchPreferences | null> => {
  const blob = await fetchSessionBlob(owner.signal);

  assertAccountScopeCurrent(owner);
  return blob?.account.preferences ? normalizeWorkbenchPreferences(blob.account.preferences) : null;
};

const replaceSnapshot = (
  preferences: WorkbenchPreferences,
  status: WorkbenchSettingsSnapshot['status'],
  hydratedStorageKey: string | null = store.getSnapshot().hydratedStorageKey,
  storageSuffix = getUserStorageScope()
): void => {
  store.setSnapshot({ hydratedStorageKey, preferences, scope: getSettingsScope(storageSuffix), status });
};

const pushPreferences = (preferences: WorkbenchPreferences, signal: AbortSignal): Promise<void> =>
  setClientStateValue(SETTINGS_CLIENT_STATE_KEY, JSON.stringify(preferences), signal);

const getSettingsMutationQueue = (owner: AccountScope): SettingsMutationQueue => {
  if (settingsMutationQueue?.owner === owner) {
    return settingsMutationQueue;
  }

  settingsMutationQueue = { latestRevision: 0, owner, tail: Promise.resolve() };

  return settingsMutationQueue;
};

const enqueueSettingsMutation = (
  owner: AccountScope,
  operation: (queue: SettingsMutationQueue, revision: number) => Promise<void>
): Promise<void> => {
  const queue = getSettingsMutationQueue(owner);
  const revision = (queue.latestRevision += 1);
  const run = (): Promise<void> => operation(queue, revision);
  const result = queue.tail.then(run, run);

  queue.tail = result.catch(() => undefined);

  return result;
};

const resolveSettings = async (
  local: StoredSettings | null,
  owner: AccountScope,
  storageSuffix: string
): Promise<WorkbenchPreferences> => {
  if (local?.pendingPush) {
    await pushPreferences(local.preferences, owner.signal);
    assertAccountScopeCurrent(owner);

    return local.preferences;
  }

  const backendPreferences = parsePreferences(await getClientStateValue(SETTINGS_CLIENT_STATE_KEY, owner.signal));

  assertAccountScopeCurrent(owner);

  if (backendPreferences) {
    return backendPreferences;
  }

  const preferences =
    (await loadLegacySessionPreferences(owner)) ??
    local?.preferences ??
    readLegacyLocalPreferences(storageSuffix) ??
    normalizeWorkbenchPreferences();

  await pushPreferences(preferences, owner.signal);
  assertAccountScopeCurrent(owner);

  return preferences;
};

const settingsLoad = createSingleFlight<WorkbenchPreferences>();

/** Once per account scope; offline, the local copy serves and the next load retries the backend. */
export const loadWorkbenchSettings = (): Promise<WorkbenchPreferences> => {
  const owner = captureAccountScope();
  const storageSuffix = owner.accountId === null ? getUserStorageScope() : owner.storageSuffix;
  const storageKey = getSettingsStorageKey(storageSuffix);

  if (store.getSnapshot().hydratedStorageKey === storageKey) {
    return Promise.resolve(getWorkbenchPreferences());
  }

  return settingsLoad.run(`${storageKey}:${owner.epoch}`, async () => {
    store.patchSnapshot({ scope: getSettingsScope(storageSuffix), status: 'loading' });
    const local = readLocalSettings(storageKey);

    try {
      const preferences = await resolveSettings(local, owner, storageSuffix);

      assertAccountScopeCurrent(owner);
      writeLocalSettings(storageKey, preferences);
      replaceSnapshot(preferences, 'ready', storageKey, storageSuffix);

      return preferences;
    } catch (error) {
      if (!isAccountScopeCurrent(owner)) {
        throw error;
      }

      const fallback = local?.preferences ?? readLegacyLocalPreferences(storageSuffix);
      const preferences = fallback ?? normalizeWorkbenchPreferences();

      writeLocalSettings(storageKey, preferences, local?.pendingPush);
      store.setSnapshot({
        error: error instanceof Error ? error.message : 'Failed to load settings from the backend.',
        hydratedStorageKey: null,
        preferences,
        scope: getSettingsScope(storageSuffix),
        status: fallback ? 'ready' : 'error',
      });

      return preferences;
    }
  });
};

export const getWorkbenchSettingsSnapshot = store.getSnapshot;
export const subscribeWorkbenchSettings = store.subscribe;

export const useWorkbenchSettings = (): WorkbenchSettingsSnapshot => store.useSnapshot();

export const useWorkbenchPreferences = (): WorkbenchPreferences =>
  store.useSelector((snapshot) => snapshot.preferences);

type EqualityFn<T> = (left: T, right: T) => boolean;

export const useWorkbenchSettingsSelector = <Selected>(
  selector: (snapshot: WorkbenchSettingsSnapshot) => Selected,
  isEqual: EqualityFn<Selected> = Object.is
): Selected => store.useSelector(selector, isEqual);

export const useWorkbenchPreferenceSelector = <Selected>(
  selector: (preferences: WorkbenchPreferences) => Selected,
  isEqual?: EqualityFn<Selected>
): Selected => useWorkbenchSettingsSelector((snapshot) => selector(snapshot.preferences), isEqual);

export const getWorkbenchPreferences = (): WorkbenchPreferences => store.getSnapshot().preferences;

export const areWorkbenchPreferencesReady = (): boolean => store.getSnapshot().status === 'ready';

export const subscribeWorkbenchPreferences = (listener: (preferences: WorkbenchPreferences) => void): (() => void) => {
  listener(store.getSnapshot().preferences);

  return store.subscribe(() => listener(store.getSnapshot().preferences));
};

export const getWorkbenchReduceMotion = (): boolean => store.getSnapshot().preferences.reduceMotion;

export const useWorkbenchReduceMotion = (): boolean =>
  store.useSelector((snapshot) => snapshot.preferences.reduceMotion);

export const patchWorkbenchPreferences = async (preferences: Partial<WorkbenchPreferences>): Promise<void> => {
  const owner = captureAccountScope();
  const storageSuffix = owner.accountId === null ? getUserStorageScope() : owner.storageSuffix;
  const storageKey = getSettingsStorageKey(storageSuffix);
  const next = normalizeWorkbenchPreferences({ ...store.getSnapshot().preferences, ...preferences });

  replaceSnapshot(next, 'ready', store.getSnapshot().hydratedStorageKey, storageSuffix);
  writeLocalSettings(storageKey, next, true);

  await enqueueSettingsMutation(owner, async (queue, revision) => {
    if (!isAccountScopeCurrent(owner)) {
      return;
    }

    try {
      await pushPreferences(next, owner.signal);

      if (!isAccountScopeCurrent(owner)) {
        return;
      }
      if (queue.latestRevision === revision) {
        writeLocalSettings(storageKey, next);
      }
    } catch (error) {
      if (!isAccountScopeCurrent(owner) || queue.latestRevision !== revision) {
        return;
      }

      store.patchSnapshot({
        error: error instanceof Error ? error.message : 'Failed to save settings.',
        status: 'error',
      });
    }
  });
};

export const patchControlModelMemory = (key: string, modelKey: string | null): Promise<void> => {
  const controlModelMemory = { ...store.getSnapshot().preferences.controlModelMemory };
  delete controlModelMemory[key];
  if (modelKey !== null) {
    controlModelMemory[key] = modelKey;
  }
  return patchWorkbenchPreferences({ controlModelMemory });
};

export const patchScopedDefault = (ownerId: string, values: Readonly<Record<string, unknown>>): Promise<void> => {
  const { scopedDefaults } = store.getSnapshot().preferences;
  return patchWorkbenchPreferences({
    scopedDefaults: { ...scopedDefaults, [ownerId]: { ...scopedDefaults[ownerId], ...values } },
  });
};

export const clearScopedDefault = (ownerId: string, keys: readonly string[]): Promise<void> => {
  const { scopedDefaults } = store.getSnapshot().preferences;
  const current = scopedDefaults[ownerId];
  if (!current || !keys.some((key) => Object.hasOwn(current, key))) {
    return Promise.resolve();
  }
  const owner = { ...current };
  for (const key of keys) {
    delete owner[key];
  }
  const { [ownerId]: _cleared, ...others } = scopedDefaults;
  return patchWorkbenchPreferences({
    scopedDefaults: Object.keys(owner).length > 0 ? { ...others, [ownerId]: owner } : others,
  });
};

export const clearWorkbenchSettings = async (): Promise<void> => {
  const owner = captureAccountScope();
  const storageSuffix = owner.accountId === null ? getUserStorageScope() : owner.storageSuffix;
  const storageKey = getSettingsStorageKey(storageSuffix);

  removeLocalSettings(storageKey);
  replaceSnapshot(normalizeWorkbenchPreferences(), 'ready', storageKey, storageSuffix);

  await enqueueSettingsMutation(owner, async () => {
    if (!isAccountScopeCurrent(owner)) {
      return;
    }

    try {
      await deleteClientStateValue(SETTINGS_CLIENT_STATE_KEY, owner.signal);
    } catch {
      // Backend persistence is best-effort; the local reset should still complete.
    }
  });
};