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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(ui): normalize preset state
joshistoastf477a44432c8
diff --git a/invokeai/frontend/webv2/src/workbench/layoutContracts.ts b/invokeai/frontend/webv2/src/workbench/layoutContracts.tsindex d50982dbb1..fa7ad4ae70 100644--- a/invokeai/frontend/webv2/src/workbench/layoutContracts.ts+++ b/invokeai/frontend/webv2/src/workbench/layoutContracts.ts@@ -75,10 +75,10 @@ export interface LayoutPreset {* preset bodies are code, so "Save changes" cannot mutate them in place; the* override is what the drift comparison and `Revert to saved layout` read.*/-export type LayoutPresetOverrides = Record<string, LayoutPresetSnapshot>;+export type LayoutPresetOverrides = Partial<Record<BuiltInLayoutPresetId, LayoutPresetSnapshot>>;/** Per-account edits to the default routes shipped with built-in presets. */-export type LayoutPresetRouteOverrides = Record<string, LayoutPresetRoute>;+export type LayoutPresetRouteOverrides = Partial<Record<BuiltInLayoutPresetId, LayoutPresetRoute>>;/** Per-account edits to the identity shipped with a built-in preset. */export interface LayoutPresetMetadataOverride {@@ -86,4 +86,4 @@ export interface LayoutPresetMetadataOverride {label?: string;}-export type LayoutPresetMetadataOverrides = Record<string, LayoutPresetMetadataOverride>;+export type LayoutPresetMetadataOverrides = Partial<Record<BuiltInLayoutPresetId, LayoutPresetMetadataOverride>>;diff --git a/invokeai/frontend/webv2/src/workbench/layoutPresetSnapshots.ts b/invokeai/frontend/webv2/src/workbench/layoutPresetSnapshots.tsindex 69fb924edb..cac1777df2 100644--- a/invokeai/frontend/webv2/src/workbench/layoutPresetSnapshots.ts+++ b/invokeai/frontend/webv2/src/workbench/layoutPresetSnapshots.ts@@ -9,7 +9,7 @@ import type {import type { AccountState, Project } from '@workbench/projectContracts';import type { WidgetInstanceId } from '@workbench/widgetContracts';-import { getLayoutPreset } from '@workbench/layoutPresets';+import { getLayoutPreset, isBuiltInLayoutPresetId, resolveLayoutPresetId } from '@workbench/layoutPresets';const widgetRegions: WidgetRegion[] = ['left', 'right', 'bottom', 'center'];@@ -21,13 +21,16 @@ const widgetRegions: WidgetRegion[] = ['left', 'right', 'bottom', 'center'];* would appear to do nothing.*/export const resolveSavedLayoutPreset = (account: AccountState, presetId: LayoutPresetId): LayoutPreset => {- const customPreset = account.customLayoutPresets?.find((preset) => preset.id === presetId);+ const resolvedPresetId = resolveLayoutPresetId(presetId);+ const customPreset = isBuiltInLayoutPresetId(resolvedPresetId)+ ? undefined+ : account.customLayoutPresets?.find((preset) => preset.id === presetId);if (customPreset) {return customPreset;}- const builtInPreset = getLayoutPreset(presetId);+ const builtInPreset = getLayoutPreset(resolvedPresetId);const metadata = account.layoutPresetMetadataOverrides?.[builtInPreset.id];const override = account.layoutPresetOverrides?.[builtInPreset.id];const defaultRoute = account.layoutPresetRouteOverrides?.[builtInPreset.id] ?? builtInPreset.defaultRoute;diff --git a/invokeai/frontend/webv2/src/workbench/layoutPresets.ts b/invokeai/frontend/webv2/src/workbench/layoutPresets.tsindex a3300deecf..2cc64250a5 100644--- a/invokeai/frontend/webv2/src/workbench/layoutPresets.ts+++ b/invokeai/frontend/webv2/src/workbench/layoutPresets.ts@@ -94,7 +94,13 @@ const createSnapshot = ({export interface BuiltInLayoutPresetDescriptor {defaultKeys: readonly string[];hotkeyId: string;- preset: LayoutPreset;+ preset: BuiltInLayoutPreset;+}++export interface BuiltInLayoutPreset extends LayoutPreset {+ defaultRoute: LayoutPresetRoute;+ id: BuiltInLayoutPresetId;+ isBuiltIn: true;}const createPresetDescriptor = ({@@ -238,9 +244,9 @@ export const builtInLayoutPresetDescriptors: BuiltInLayoutPresetDescriptor[] = [}),];-export const layoutPresets: LayoutPreset[] = builtInLayoutPresetDescriptors.map(({ preset }) => preset);+export const layoutPresets: BuiltInLayoutPreset[] = builtInLayoutPresetDescriptors.map(({ preset }) => preset);-export const defaultLayoutPreset = layoutPresets[0];+export const defaultLayoutPreset = layoutPresets[0]!;/*** Preset ids persisted before the three-preset model. `gallery` had no successor@@ -261,7 +267,7 @@ const legacyLayoutPresetIds: Record<string, BuiltInLayoutPresetId> = {*/export const resolveLayoutPresetId = (presetId: string): string => legacyLayoutPresetIds[presetId] ?? presetId;-export const getLayoutPreset = (presetId: string) =>+export const getLayoutPreset = (presetId: string): BuiltInLayoutPreset =>layoutPresets.find((preset) => preset.id === resolveLayoutPresetId(presetId)) ?? defaultLayoutPreset;export const isBuiltInLayoutPresetId = (presetId: string): presetId is BuiltInLayoutPresetId =>diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.tsindex 9c06bcd978..bf1a2ec05f 100644--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts@@ -905,6 +905,77 @@ describe('workbench layout presets', () => {expect(state.account.layoutPresetOrder).toEqual(['automate', 'compose', 'edit', 'custom-layout-1']);});+ it('rejects reserved, empty, and duplicate custom preset ids during hydration', () => {+ const initial = createInitialWorkbenchState();+ const customPreset = (id: string, label: string) => ({ id, label, snapshot: layoutPresets[0]!.snapshot });+ const state = workbenchReducer(initial, {+ state: {+ ...initial,+ account: {+ ...initial.account,+ activeLayoutPresetId: 'missing',+ customLayoutPresets: [+ customPreset(' ', 'Empty'),+ customPreset('compose', 'Built in'),+ customPreset(' canvas ', 'Legacy alias'),+ customPreset('custom-duplicate', 'First'),+ customPreset('custom-duplicate', 'Second'),+ customPreset(' custom-valid ', 'Valid'),+ ],+ },+ },+ type: 'hydrateWorkbench',+ });++ expect(state.account.activeLayoutPresetId).toBe('compose');+ expect(state.account.customLayoutPresets?.map(({ id, label }) => ({ id, label }))).toEqual([+ { id: 'custom-duplicate', label: 'First' },+ { id: 'custom-valid', label: 'Valid' },+ ]);+ });++ it('does not create a custom preset in a built-in or legacy id namespace', () => {+ const initial = createInitialWorkbenchState();+ const builtInCollision = workbenchReducer(initial, {+ label: 'Shadow Compose',+ presetId: 'compose',+ type: 'addLayoutPreset',+ });+ const legacyCollision = workbenchReducer(initial, {+ label: 'Shadow Edit',+ presetId: 'canvas',+ type: 'addLayoutPreset',+ });++ expect(builtInCollision).toBe(initial);+ expect(legacyCollision).toBe(initial);+ });++ it('keeps only built-in preset overrides during hydration', () => {+ const initial = createInitialWorkbenchState();+ const snapshot = layoutPresets[0]!.snapshot;+ const route = { destination: 'canvas' as const, sourceId: 'canvas' as const };+ const state = workbenchReducer(initial, {+ state: {+ ...initial,+ account: {+ ...initial.account,+ layoutPresetMetadataOverrides: {+ canvas: { label: 'Editing' },+ unknown: { label: 'Unknown' },+ },+ layoutPresetOverrides: { canvas: snapshot, unknown: snapshot },+ layoutPresetRouteOverrides: { canvas: route, unknown: route },+ },+ } as unknown as WorkbenchState,+ type: 'hydrateWorkbench',+ });++ expect(state.account.layoutPresetMetadataOverrides).toEqual({ edit: { label: 'Editing' } });+ expect(state.account.layoutPresetOverrides).toEqual({ edit: snapshot });+ expect(state.account.layoutPresetRouteOverrides).toEqual({ edit: route });+ });+it('captures the live source and destination without routing locks when creating a custom preset', () => {let state = createInitialWorkbenchState();diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.tsindex 34a44031eb..a49314b2e2 100644--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts@@ -12,6 +12,7 @@ import type { DeveloperLogNamespace } from '@workbench/diagnostics/contracts';import type { GraphContract } from '@workbench/graphContracts';import type { InvocationRoute, InvocationSourceId, ResultDestination } from '@workbench/invocationContracts';import type {+ BuiltInLayoutPresetId,CenterViewId,LayoutPreset,LayoutPresetId,@@ -19,6 +20,7 @@ import type {LayoutPresetMetadataOverrides,LayoutPresetOverrides,LayoutPresetRoute,+ LayoutPresetRouteOverrides,LayoutPresetSnapshot,ProjectLayoutState,WidgetRegion,@@ -1603,6 +1605,8 @@ const normalizeCustomLayoutPresets = (presets: unknown): LayoutPreset[] => {return [];}+ const seenIds = new Set<string>();+return presets.flatMap((preset): LayoutPreset[] => {if (!preset || typeof preset !== 'object') {return [];@@ -1614,13 +1618,21 @@ const normalizeCustomLayoutPresets = (presets: unknown): LayoutPreset[] => {return [];}+ const id = record.id.trim();++ if (!id || isBuiltInLayoutPresetId(resolveLayoutPresetId(id)) || seenIds.has(id)) {+ return [];+ }++ seenIds.add(id);+const defaultRoute = normalizeLayoutPresetRoute(record.defaultRoute);return [{...(defaultRoute ? { defaultRoute } : {}),...(typeof record.iconId === 'string' ? { iconId: record.iconId } : {}),- id: record.id,+ id,label: record.label,snapshot: cloneLayoutPresetSnapshot(record.snapshot),},@@ -1628,16 +1640,17 @@ const normalizeCustomLayoutPresets = (presets: unknown): LayoutPreset[] => {});};-const normalizeLayoutPresetRouteOverrides = (overrides: unknown) => {+const normalizeLayoutPresetRouteOverrides = (overrides: unknown): LayoutPresetRouteOverrides => {if (!overrides || typeof overrides !== 'object') {return {};}return Object.fromEntries(Object.entries(overrides as Record<string, unknown>).flatMap(([presetId, route]) => {+ const resolvedPresetId = resolveLayoutPresetId(presetId);const normalizedRoute = normalizeLayoutPresetRoute(route);- return normalizedRoute ? [[resolveLayoutPresetId(presetId), normalizedRoute]] : [];+ return isBuiltInLayoutPresetId(resolvedPresetId) && normalizedRoute ? [[resolvedPresetId, normalizedRoute]] : [];}));};@@ -1673,17 +1686,27 @@ const normalizeLayoutPresetOverrides = (overrides: unknown): LayoutPresetOverrid}return Object.fromEntries(- Object.entries(overrides as Record<string, unknown>).flatMap(([presetId, snapshot]) =>- isLayoutPresetSnapshot(snapshot) ? [[resolveLayoutPresetId(presetId), cloneLayoutPresetSnapshot(snapshot)]] : []- )+ Object.entries(overrides as Record<string, unknown>).flatMap(([presetId, snapshot]) => {+ const resolvedPresetId = resolveLayoutPresetId(presetId);++ return isBuiltInLayoutPresetId(resolvedPresetId) && isLayoutPresetSnapshot(snapshot)+ ? [[resolvedPresetId, cloneLayoutPresetSnapshot(snapshot)]]+ : [];+ }));};const normalizeAccount = (account: Partial<WorkbenchState['account']> | undefined): WorkbenchState['account'] => {const customLayoutPresets = normalizeCustomLayoutPresets(account?.customLayoutPresets);+ const resolvedActivePresetId = resolveLayoutPresetId(account?.activeLayoutPresetId ?? defaultLayoutPreset.id);+ const activeLayoutPresetId =+ isBuiltInLayoutPresetId(resolvedActivePresetId) ||+ customLayoutPresets.some((preset) => preset.id === resolvedActivePresetId)+ ? resolvedActivePresetId+ : defaultLayoutPreset.id;return {- activeLayoutPresetId: resolveLayoutPresetId(account?.activeLayoutPresetId ?? defaultLayoutPreset.id),+ activeLayoutPresetId,customLayoutPresets,layoutPresetMetadataOverrides: normalizeLayoutPresetMetadataOverrides(account?.layoutPresetMetadataOverrides),layoutPresetOrder: normalizeLayoutPresetOrder(account?.layoutPresetOrder, [@@ -1732,7 +1755,7 @@ const getAvailableLayoutPreset = (state: WorkbenchState, presetId: LayoutPresetIconst setBuiltInLayoutPresetMetadata = (state: WorkbenchState,- presetId: LayoutPresetId,+ presetId: BuiltInLayoutPresetId,metadata: Required<LayoutPresetMetadataOverride>): WorkbenchState => {const shippedPreset = getLayoutPreset(presetId);@@ -2684,8 +2707,9 @@ export const __workbenchReducerInternal = (}case 'addLayoutPreset': {const activeProject = state.projects.find((project) => project.id === state.activeProjectId);+ const presetId = action.presetId.trim();- if (!activeProject) {+ if (!activeProject || !presetId || isBuiltInLayoutPresetId(resolveLayoutPresetId(presetId))) {return state;}@@ -2701,12 +2725,12 @@ export const __workbenchReducerInternal = (},}),iconId: action.iconId,- id: action.presetId,+ id: presetId,label: action.label.trim() || 'Custom layout',snapshot: createLayoutPresetSnapshot(normalizeWorkbenchProject(activeProject)),};const customLayoutPresets = [- ...(state.account.customLayoutPresets ?? []).filter((candidate) => candidate.id !== action.presetId),+ ...(state.account.customLayoutPresets ?? []).filter((candidate) => candidate.id !== presetId),preset,];const layoutPresetOrder = [@@ -2750,6 +2774,10 @@ export const __workbenchReducerInternal = (return { ...state, account: { ...state.account, customLayoutPresets } };}case 'restoreLayoutPresetDefault': {+ if (!isBuiltInLayoutPresetId(action.presetId)) {+ return state;+ }+const { [action.presetId]: removedMetadata, ...layoutPresetMetadataOverrides } =state.account.layoutPresetMetadataOverrides ?? {};const { [action.presetId]: removed, ...layoutPresetOverrides } = state.account.layoutPresetOverrides ?? {};