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
refactor(webv2): share the layer-content predicates between plan and checks
joshistoastd0e8203abf2c
diff --git a/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.json b/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.jsonindex 836fbb577a..41b6c549c5 100644--- a/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.json+++ b/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.json@@ -82,6 +82,7 @@"WorkbenchWidgetRegistryContext.tsx": "workbench","autoRoutePolicy.ts": "workbench","canvasDimsSync.ts": "workbench",+ "canvasLayerContent.ts": "workbench","canvasLayerOps.ts": "workbench","canvasMigration.ts": "workbench","canvasProjectMutationPort.ts": "workbench",diff --git a/invokeai/frontend/webv2/src/workbench/canvas-operations/generationCompositePlan.ts b/invokeai/frontend/webv2/src/workbench/canvas-operations/generationCompositePlan.tsindex 658658642e..9a9001579e 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-operations/generationCompositePlan.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-operations/generationCompositePlan.ts@@ -13,6 +13,7 @@ import {getCompositeLayerBounds,planBaseRasterComposite,} from '@workbench/canvas-engine/api';+import { isCompositableControlLayer, isCompositableRegionalGuidanceLayer } from '@workbench/canvasLayerContent';import type {CompositeEntry,@@ -152,17 +153,6 @@ export const planComposites = (document: CanvasDocumentContractV2, bbox: Rect):return { bbox, entries };};-/** True when a control layer is enabled and holds rasterizable (non-empty) content. */-const hasControlContent = (layer: CanvasLayerContract): layer is CanvasControlLayerContract => {- if (!layer.isEnabled || layer.type !== 'control') {- return false;- }- if (layer.source.type === 'image') {- return true;- }- return layer.source.type === 'paint' && layer.source.bitmap !== null;-};-/** The native (unscaled) content rect of a control layer's source (layer-local). */const controlContentRect = (layer: CanvasControlLayerContract,@@ -220,7 +210,7 @@ export interface ControlCompositeEntry {* paint into the img2img/inpaint source.*/export const planControlComposites = (document: CanvasDocumentContractV2, bbox: Rect): ControlCompositeEntry[] =>- document.layers.filter(hasControlContent).map((layer) => {+ document.layers.filter(isCompositableControlLayer).map((layer) => {const ref = toControlLayerRef(layer, document);return {entry: {@@ -234,10 +224,6 @@ export const planControlComposites = (document: CanvasDocumentContractV2, bbox:};});-/** True when a regional-guidance layer is enabled and holds a persisted (non-empty) mask. */-const hasRegionalMaskContent = (layer: CanvasLayerContract): layer is CanvasRegionalGuidanceLayerContract =>- layer.isEnabled && layer.type === 'regional_guidance' && layer.mask.bitmap !== null;-/** The native content rect of a regional mask's persisted bitmap (layer-local, at its offset). */const regionalMaskContentRect = (layer: CanvasRegionalGuidanceLayerContract@@ -293,7 +279,7 @@ export const planRegionalMaskComposites = (document: CanvasDocumentContractV2,bbox: Rect): RegionalMaskCompositeEntry[] =>- document.layers.filter(hasRegionalMaskContent).map((layer) => {+ document.layers.filter(isCompositableRegionalGuidanceLayer).map((layer) => {const ref = toRegionalMaskRef(layer);return {entry: {diff --git a/invokeai/frontend/webv2/src/workbench/canvasLayerContent.test.ts b/invokeai/frontend/webv2/src/workbench/canvasLayerContent.test.tsnew file mode 100644index 0000000000..a58e9ca3ba--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/canvasLayerContent.test.ts@@ -0,0 +1,91 @@+import type {+ CanvasControlLayerContract,+ CanvasLayerContract,+ CanvasRegionalGuidanceLayerContract,+} from '@workbench/canvas-engine/api';++import { describe, expect, it } from 'vitest';++import {+ hasControlLayerContent,+ hasRegionalGuidanceMaskContent,+ isCompositableControlLayer,+ isCompositableRegionalGuidanceLayer,+} from './canvasLayerContent';++const controlLayer = (+ id: string,+ overrides: { isEnabled?: boolean; source?: CanvasControlLayerContract['source'] } = {}+): CanvasControlLayerContract => ({+ adapter: { beginEndStepPct: [0, 1], controlMode: null, kind: 'controlnet', model: null, weight: 1 },+ blendMode: 'normal',+ id,+ isEnabled: overrides.isEnabled ?? true,+ isLocked: false,+ name: id,+ opacity: 1,+ source: overrides.source ?? { image: { height: 64, imageName: `${id}.png`, width: 64 }, type: 'image' },+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },+ type: 'control',+ withTransparencyEffect: true,+});++const regionalLayer = (+ id: string,+ overrides: { isEnabled?: boolean; bitmap?: CanvasRegionalGuidanceLayerContract['mask']['bitmap'] } = {}+): CanvasRegionalGuidanceLayerContract => ({+ autoNegative: false,+ blendMode: 'normal',+ id,+ isEnabled: overrides.isEnabled ?? true,+ isLocked: false,+ mask: {+ bitmap: overrides.bitmap === undefined ? ({ height: 8, width: 8 } as never) : overrides.bitmap,+ fill: { color: '#ffffff', style: 'solid' },+ offset: { x: 0, y: 0 },+ },+ name: id,+ negativePrompt: null,+ opacity: 1,+ positivePrompt: null,+ referenceImages: [],+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },+ type: 'regional_guidance',+});++describe('hasControlLayerContent', () => {+ it('accepts an image source and committed paint pixels, rejects blank paint', () => {+ expect(hasControlLayerContent(controlLayer('image'))).toBe(true);+ expect(+ hasControlLayerContent(+ controlLayer('painted', { source: { bitmap: { height: 8, width: 8 } as never, type: 'paint' } })+ )+ ).toBe(true);+ expect(hasControlLayerContent(controlLayer('blank', { source: { bitmap: null, type: 'paint' } }))).toBe(false);+ });+});++describe('isCompositableControlLayer', () => {+ it('requires the control type, enablement, and content together', () => {+ expect(isCompositableControlLayer(controlLayer('ready'))).toBe(true);+ expect(isCompositableControlLayer(controlLayer('disabled', { isEnabled: false }))).toBe(false);+ expect(isCompositableControlLayer(controlLayer('blank', { source: { bitmap: null, type: 'paint' } }))).toBe(false);+ expect(isCompositableControlLayer(regionalLayer('not-control') as CanvasLayerContract)).toBe(false);+ });+});++describe('hasRegionalGuidanceMaskContent', () => {+ it('requires a persisted mask bitmap', () => {+ expect(hasRegionalGuidanceMaskContent(regionalLayer('masked'))).toBe(true);+ expect(hasRegionalGuidanceMaskContent(regionalLayer('empty', { bitmap: null }))).toBe(false);+ });+});++describe('isCompositableRegionalGuidanceLayer', () => {+ it('requires the regional type, enablement, and a mask together', () => {+ expect(isCompositableRegionalGuidanceLayer(regionalLayer('ready'))).toBe(true);+ expect(isCompositableRegionalGuidanceLayer(regionalLayer('disabled', { isEnabled: false }))).toBe(false);+ expect(isCompositableRegionalGuidanceLayer(regionalLayer('empty', { bitmap: null }))).toBe(false);+ expect(isCompositableRegionalGuidanceLayer(controlLayer('not-regional') as CanvasLayerContract)).toBe(false);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/canvasLayerContent.ts b/invokeai/frontend/webv2/src/workbench/canvasLayerContent.tsnew file mode 100644index 0000000000..b3c057634e--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/canvasLayerContent.ts@@ -0,0 +1,41 @@+import type {+ CanvasControlLayerContract,+ CanvasLayerContract,+ CanvasRegionalGuidanceLayerContract,+} from '@workbench/canvas-engine/api';++/**+ * The single source of truth for "does this layer carry pixels the generation+ * pipeline will actually use".+ *+ * Both the composite planner (which decides what reaches the backend) and the+ * invoke readiness checks (which decide what the user is told) gate on these+ * predicates. They used to be two hand-maintained copies; a desync there means+ * readiness silently disagrees with what the graph does — a layer reported ready+ * but dropped from the composite, or vice versa.+ *+ * Depends only on layer contracts, so the pure planner can import it without+ * pulling in models, graph, or React.+ */++/** True when a control layer holds an image source or committed paint pixels. */+export const hasControlLayerContent = (layer: CanvasControlLayerContract): boolean => {+ if (layer.source.type === 'image') {+ return true;+ }+ return layer.source.type === 'paint' && layer.source.bitmap !== null;+};++/** True when a regional-guidance layer holds a persisted (non-empty) mask. */+export const hasRegionalGuidanceMaskContent = (layer: CanvasRegionalGuidanceLayerContract): boolean =>+ layer.mask.bitmap !== null;++/** Narrows to the control layers that reach the generation pipeline: enabled and content-bearing. */+export const isCompositableControlLayer = (layer: CanvasLayerContract): layer is CanvasControlLayerContract =>+ layer.type === 'control' && layer.isEnabled && hasControlLayerContent(layer);++/** Narrows to the regional-guidance layers that reach the generation pipeline: enabled and mask-bearing. */+export const isCompositableRegionalGuidanceLayer = (+ layer: CanvasLayerContract+): layer is CanvasRegionalGuidanceLayerContract =>+ layer.type === 'regional_guidance' && layer.isEnabled && hasRegionalGuidanceMaskContent(layer);diff --git a/invokeai/frontend/webv2/src/workbench/controlLayerChecks.ts b/invokeai/frontend/webv2/src/workbench/controlLayerChecks.tsindex 485f52e0dc..c5a294b7ff 100644--- a/invokeai/frontend/webv2/src/workbench/controlLayerChecks.ts+++ b/invokeai/frontend/webv2/src/workbench/controlLayerChecks.ts@@ -6,6 +6,7 @@ import {getControlValidationReason,type ControlValidationReason,} from '@features/generation/graph';+import { isCompositableControlLayer } from '@workbench/canvasLayerContent';export interface ControlLayerIssue {code: ControlValidationReason;@@ -15,17 +16,7 @@ export interface ControlLayerIssue {message: string;}-/**- * Pure parity with the composite plan's content gate: a control layer only- * reaches the generation pipeline when it has an image source or committed- * paint pixels, so readiness checks must skip anything else.- */-export const hasControlLayerContent = (layer: CanvasControlLayerContract): boolean => {- if (layer.source.type === 'image') {- return true;- }- return layer.source.type === 'paint' && layer.source.bitmap !== null;-};+export { hasControlLayerContent } from '@workbench/canvasLayerContent';const resolveAdapterModel = (adapterModel: string | null,@@ -52,7 +43,7 @@ export const getBlockingControlLayerIssues = (params: {let zImageControlCount = 0;for (const layer of layers) {- if (layer.type !== 'control' || !layer.isEnabled || !hasControlLayerContent(layer)) {+ if (!isCompositableControlLayer(layer)) {continue;}const { adapter } = layer;