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
import { createSelector } from '@reduxjs/toolkit';
import type { RootState } from 'app/store/store';
import { getPrefixedId } from 'features/controlLayers/konva/util';
export { getPrefixedId };
import { selectSaveAllImagesToGallery } from 'features/controlLayers/store/canvasSettingsSlice';
import { selectCanvasSessionId } from 'features/controlLayers/store/canvasStagingAreaSlice';
import {
selectImg2imgStrength,
selectMainModelConfig,
selectOptimizedDenoisingEnabled,
selectParamsSlice,
selectRefinerModel,
selectRefinerStart,
} from 'features/controlLayers/store/paramsSlice';
import { selectCanvasSlice } from 'features/controlLayers/store/selectors';
import type { ParamsState } from 'features/controlLayers/store/types';
import type { BoardField } from 'features/nodes/types/common';
import type { Graph } from 'features/nodes/util/graph/generation/Graph';
import { buildPresetModifiedPrompt } from 'features/stylePresets/hooks/usePresetModifiedPrompts';
import { selectStylePresetSlice } from 'features/stylePresets/store/stylePresetSlice';
import { selectActiveTab } from 'features/ui/store/uiSelectors';
import { selectListStylePresetsRequestState } from 'services/api/endpoints/stylePresets';
import type { Invocation } from 'services/api/types';
import { assert } from 'tsafe';
import type { MainModelLoaderNodes } from './types';
/**
* Gets the board field, based on the autoAddBoardId setting.
*/
export const getBoardField = (state: RootState): BoardField | undefined => {
const { autoAddBoardId } = state.gallery;
if (autoAddBoardId === 'none') {
return undefined;
}
return { board_id: autoAddBoardId };
};
/**
* Builds the common fields for canvas output:
* - id
* - use_cache
* - is_intermediate
* - board
*/
export const selectCanvasOutputFields = (state: RootState) => {
const tab = selectActiveTab(state);
// This flag also has an effect on the canvas destination - see selectCanvasDestination below.
const saveAllImagesToGallery = selectSaveAllImagesToGallery(state);
// If we're on canvas and the save all images setting is enabled, save to gallery
const is_intermediate = tab === 'canvas' && !saveAllImagesToGallery;
const board = tab === 'canvas' && !saveAllImagesToGallery ? undefined : getBoardField(state);
return {
is_intermediate,
board,
use_cache: false,
id: getPrefixedId(CANVAS_OUTPUT_PREFIX),
};
};
/**
* Select the destination to use for canvas queue items.
*
*/
export const selectCanvasDestination = (state: RootState) => {
// The canvas will stage images that have its session ID as the destination. When the user has enabled saving all
// images to gallery, we want to bypass the staging area. So we use 'canvas' as a generic destination. Images will
// go directly to the gallery.
//
// This flag also has an effect on the canvas output fields - see selectCanvasOutputFields above.
const saveAllImagesToGallery = selectSaveAllImagesToGallery(state);
if (saveAllImagesToGallery) {
return 'canvas';
}
return selectCanvasSessionId(state);
};
/**
* Gets the prompts, modified for the active style preset.
*/
export const selectPresetModifiedPrompts = createSelector(
selectParamsSlice,
selectStylePresetSlice,
selectListStylePresetsRequestState,
(params, stylePresetSlice, listStylePresetsRequestState) => {
const negativePrompt = params.negativePrompt ?? '';
const { positivePrompt } = params;
const { activeStylePresetId } = stylePresetSlice;
if (activeStylePresetId) {
const { data } = listStylePresetsRequestState;
const activeStylePreset = data?.find((item) => item.id === activeStylePresetId);
if (activeStylePreset) {
const presetModifiedPositivePrompt = buildPresetModifiedPrompt(
activeStylePreset.preset_data.positive_prompt,
positivePrompt
);
const presetModifiedNegativePrompt = buildPresetModifiedPrompt(
activeStylePreset.preset_data.negative_prompt,
negativePrompt ?? ''
);
return {
positive: presetModifiedPositivePrompt,
negative: presetModifiedNegativePrompt,
};
}
}
return {
positive: positivePrompt,
negative: negativePrompt,
};
}
);
export const getOriginalAndScaledSizesForTextToImage = (state: RootState) => {
const tab = selectActiveTab(state);
const params = selectParamsSlice(state);
const canvas = selectCanvasSlice(state);
if (tab === 'canvas') {
const { rect, aspectRatio } = canvas.bbox;
const { width, height } = rect;
const originalSize = { width, height };
const scaledSize = ['auto', 'manual'].includes(canvas.bbox.scaleMethod) ? canvas.bbox.scaledSize : originalSize;
return { originalSize, scaledSize, aspectRatio };
} else if (tab === 'generate') {
const { width, height, aspectRatio } = params.dimensions;
return {
originalSize: { width, height },
scaledSize: { width, height },
aspectRatio,
};
}
assert(false, `Cannot get sizes for tab ${tab} - this function is only for the Canvas or Generate tabs`);
};
export const getOriginalAndScaledSizesForOtherModes = (state: RootState) => {
const tab = selectActiveTab(state);
const canvas = selectCanvasSlice(state);
assert(tab === 'canvas', `Cannot get sizes for tab ${tab} - this function is only for the Canvas tab`);
const { rect, aspectRatio } = canvas.bbox;
const { width, height } = rect;
const originalSize = { width, height };
const scaledSize = ['auto', 'manual'].includes(canvas.bbox.scaleMethod) ? canvas.bbox.scaledSize : originalSize;
return { originalSize, scaledSize, aspectRatio, rect };
};
export const getInfill = (
g: Graph,
params: ParamsState
): Invocation<'infill_patchmatch' | 'infill_cv2' | 'infill_lama' | 'infill_rgba' | 'infill_tile'> => {
const { infillMethod, infillColorValue, infillPatchmatchDownscaleSize, infillTileSize } = params;
// Add Infill Nodes
if (infillMethod === 'patchmatch') {
return g.addNode({
id: 'infill_patchmatch',
type: 'infill_patchmatch',
downscale: infillPatchmatchDownscaleSize,
});
}
if (infillMethod === 'lama') {
return g.addNode({
id: 'infill_lama',
type: 'infill_lama',
});
}
if (infillMethod === 'cv2') {
return g.addNode({
id: 'infill_cv2',
type: 'infill_cv2',
});
}
if (infillMethod === 'tile') {
return g.addNode({
id: 'infill_tile',
type: 'infill_tile',
tile_size: infillTileSize,
});
}
if (infillMethod === 'color') {
const { a, ...rgb } = infillColorValue;
const color = { ...rgb, a: Math.round(a * 255) };
return g.addNode({
id: 'infill_rgba',
type: 'infill_rgba',
color,
});
}
assert(false, 'Unknown infill method');
};
export const CANVAS_OUTPUT_PREFIX = 'canvas_output';
// Only the classic SD/SDXL loaders expose a `unet` output; every other main-model loader (FLUX,
// FLUX.2, SD3, CogView4, Qwen-Image, Z-Image, ERNIE-Image, Krea-2, Anima, Wan) is transformer-based
// and has no `unet`.
// Defining the predicate by this small allow-list means any newly added transformer loader is treated
// as unet-less automatically, and the negated branch narrows `modelLoader` to the unet-bearing loaders
// so `addEdge(modelLoader, 'unet', ...)` type-checks.
type MainModelLoaderWithUnetNodes = 'main_model_loader' | 'sdxl_model_loader';
export const isMainModelWithoutUnet = (
modelLoader: Invocation<MainModelLoaderNodes>
): modelLoader is Invocation<Exclude<MainModelLoaderNodes, MainModelLoaderWithUnetNodes>> => {
return modelLoader.type !== 'main_model_loader' && modelLoader.type !== 'sdxl_model_loader';
};
export const isCanvasOutputNodeId = (nodeId: string) => nodeId.split(':')[0] === CANVAS_OUTPUT_PREFIX;
export const getDenoisingStartAndEnd = (state: RootState): { denoising_start: number; denoising_end: number } => {
const optimizedDenoisingEnabled = selectOptimizedDenoisingEnabled(state);
const denoisingStrength = selectImg2imgStrength(state);
const model = selectMainModelConfig(state);
const refinerModel = selectRefinerModel(state);
const refinerDenoisingStart = selectRefinerStart(state);
switch (model?.base) {
case 'sd-3': {
// We rescale the img2imgStrength (with exponent 0.2) to effectively use the entire range [0, 1] and make the scale
// more user-friendly for SD3.5. Without this, most of the 'change' is concentrated in the high denoise strength
// range (>0.9).
const exponent = optimizedDenoisingEnabled ? 0.2 : 1;
return {
denoising_start: 1 - denoisingStrength ** exponent,
denoising_end: 1,
};
}
case 'flux':
case 'flux2': {
if (model.base === 'flux' && model.variant === 'dev_fill') {
// This is a FLUX Fill model - we always denoise fully
return {
denoising_start: 0,
denoising_end: 1,
};
} else {
// FLUX.1 and FLUX.2 Klein: We rescale the img2imgStrength (with exponent 0.2) to effectively use the entire
// range [0, 1] and make the scale more user-friendly. Without this, most of the 'change' is concentrated in
// the high denoise strength range (>0.9).
const exponent = optimizedDenoisingEnabled ? 0.2 : 1;
return {
denoising_start: 1 - denoisingStrength ** exponent,
denoising_end: 1,
};
}
}
case 'anima':
case 'sd-1':
case 'sd-2':
case 'cogview4':
case 'qwen-image':
case 'wan':
case 'z-image':
case 'krea-2': {
return {
denoising_start: 1 - denoisingStrength,
denoising_end: 1,
};
}
case 'sdxl': {
if (refinerModel) {
return {
denoising_start: Math.min(refinerDenoisingStart, 1 - denoisingStrength),
denoising_end: refinerDenoisingStart,
};
} else {
return {
denoising_start: 1 - denoisingStrength,
denoising_end: 1,
};
}
}
default: {
assert(false, `Unsupported base: ${model?.base}`);
}
}
};