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.

13.5 KB
Raw Blame
import { objectEquals } from '@observ33r/object-equals';
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSelector, createSlice } from '@reduxjs/toolkit';
import { createMemoizedSelector } from 'app/store/createMemoizedSelector';
import type { RootState } from 'app/store/store';
import type { SliceConfig } from 'app/store/types';
import { clamp } from 'es-toolkit/compat';
import { logout } from 'features/auth/store/authSlice';
import { getPrefixedId } from 'features/controlLayers/konva/util';
import type {
  CroppableImageWithDims,
  FLUXReduxImageInfluence,
  RefImagesState,
} from 'features/controlLayers/store/types';
import { zModelIdentifierField } from 'features/nodes/types/common';
import type { FLUXKontextModelConfig, FLUXReduxModelConfig, IPAdapterModelConfig } from 'services/api/types';
import { assert } from 'tsafe';
import type { PartialDeep } from 'type-fest';

import type { CLIPVisionModelV2, IPMethodV2, RefImageState } from './types';
import {
  getInitialRefImagesState,
  isFlux2ReferenceImageConfig,
  isFLUXReduxConfig,
  isIPAdapterConfig,
  isKrea2ReferenceImageConfig,
  isMiniMaxH3ReferenceImageConfig,
  isQwenImageReferenceImageConfig,
  isWanReferenceImageConfig,
  zRefImagesState,
} from './types';
import { getReferenceImageState, initialFluxKontextReferenceImage, initialFLUXRedux, initialIPAdapter } from './util';

type PayloadActionWithId<T = void> = T extends void
  ? PayloadAction<{ id: string }>
  : PayloadAction<
      {
        id: string;
      } & T
    >;

/** Fingerprint used to match the same reference image entry after recall when ids are regenerated. */
/** Empty configs of the same type may collide; the worst case is selecting an equivalent empty entity. */
const getRefImageRecallMatchKey = (entity: RefImageState): string => {
  const { config } = entity;
  const imageName = config.image?.original.image.image_name ?? '';
  const modelKey = 'model' in config && config.model ? config.model.key : '';
  return `${config.type}\0${modelKey}\0${imageName}`;
};

const slice = createSlice({
  name: 'refImages',
  initialState: getInitialRefImagesState(),
  reducers: {
    refImageAdded: {
      reducer: (state, action: PayloadActionWithId<{ overrides?: PartialDeep<RefImageState> }>) => {
        const { id, overrides } = action.payload;
        const entityState = getReferenceImageState(id, overrides);

        state.entities.push(entityState);
        state.selectedEntityId = id;
        state.isPanelOpen = true;
      },
      prepare: (payload?: { overrides?: PartialDeep<RefImageState> }) => ({
        payload: { ...payload, id: getPrefixedId('reference_image') },
      }),
    },
    refImagesRecalled: (state, action: PayloadAction<{ entities: RefImageState[]; replace: boolean }>) => {
      const { entities, replace } = action.payload;
      if (!replace) {
        state.entities.push(...entities);
        return;
      }
      const wasPanelOpen = state.isPanelOpen;
      const previousSelectedId = state.selectedEntityId;
      let previousEntity: RefImageState | null = null;
      if (previousSelectedId !== null) {
        previousEntity = state.entities.find((e) => e.id === previousSelectedId) ?? null;
      }
      state.entities = entities;
      if (entities.length === 0) {
        state.selectedEntityId = null;
        state.isPanelOpen = false;
        return;
      }
      if (!wasPanelOpen) {
        state.selectedEntityId = null;
        return;
      }
      const firstEntity = entities[0];
      assert(firstEntity);
      if (previousSelectedId === null) {
        // Open panel must have a selection; otherwise, fall back to the first entity.
        state.selectedEntityId = firstEntity.id;
        return;
      }
      if (previousSelectedId !== null && entities.some((e) => e.id === previousSelectedId)) {
        state.selectedEntityId = previousSelectedId;
        return;
      }
      const previousKey = previousEntity ? getRefImageRecallMatchKey(previousEntity) : null;
      const matched =
        previousKey !== null ? entities.find((e) => getRefImageRecallMatchKey(e) === previousKey) : undefined;
      state.selectedEntityId = matched?.id ?? firstEntity.id;
    },
    refImageImageChanged: (state, action: PayloadActionWithId<{ croppableImage: CroppableImageWithDims | null }>) => {
      const { id, croppableImage } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      entity.config.image = croppableImage;
    },
    refImageIPAdapterMethodChanged: (state, action: PayloadActionWithId<{ method: IPMethodV2 }>) => {
      const { id, method } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (!isIPAdapterConfig(entity.config)) {
        return;
      }
      entity.config.method = method;
    },
    refImageFLUXReduxImageInfluenceChanged: (
      state,
      action: PayloadActionWithId<{ imageInfluence: FLUXReduxImageInfluence }>
    ) => {
      const { id, imageInfluence } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (!isFLUXReduxConfig(entity.config)) {
        return;
      }
      entity.config.imageInfluence = imageInfluence;
    },
    refImageModelChanged: (
      state,
      action: PayloadActionWithId<{
        modelConfig: IPAdapterModelConfig | FLUXKontextModelConfig | FLUXReduxModelConfig | null;
      }>
    ) => {
      const { id, modelConfig } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }

      // FLUX.2, Qwen Image Edit, Wan, Krea-2 and MiniMax H3 reference images don't have a model field - they use
      // built-in support
      if (
        isFlux2ReferenceImageConfig(entity.config) ||
        isQwenImageReferenceImageConfig(entity.config) ||
        isWanReferenceImageConfig(entity.config) ||
        isKrea2ReferenceImageConfig(entity.config) ||
        isMiniMaxH3ReferenceImageConfig(entity.config)
      ) {
        return;
      }

      const oldModel = entity.config.model;

      // First set the new model
      entity.config.model = modelConfig ? zModelIdentifierField.parse(modelConfig) : null;

      if (!entity.config.model) {
        return;
      }

      if (objectEquals(oldModel, entity.config.model)) {
        // Nothing changed, so we don't need to do anything
        return;
      }

      // The type of ref image depends on the model. When the user switches the model, we rebuild the ref image.
      // When we switch the model, we keep the image the same, but change the other parameters.

      if (entity.config.model.base === 'flux' && entity.config.model.name?.toLowerCase().includes('kontext')) {
        // Switching to flux kontext ref image
        entity.config = {
          ...initialFluxKontextReferenceImage,
          image: entity.config.image,
          model: entity.config.model,
        };
        return;
      }

      if (entity.config.model.type === 'flux_redux') {
        // Switching to flux_redux
        entity.config = {
          ...initialFLUXRedux,
          image: entity.config.image,
          model: entity.config.model,
        };
        return;
      }

      if (entity.config.model.type === 'ip_adapter') {
        // Switching to ip_adapter
        entity.config = {
          ...initialIPAdapter,
          image: entity.config.image,
          model: entity.config.model,
        };
        // Ensure that the IP Adapter model is compatible with the CLIP Vision model
        if (entity.config.model?.base === 'flux') {
          entity.config.clipVisionModel = 'ViT-L';
        } else if (entity.config.clipVisionModel === 'ViT-L') {
          // Fall back to ViT-H (ViT-G would also work)
          entity.config.clipVisionModel = 'ViT-H';
        }
        return;
      }
    },
    refImageIPAdapterCLIPVisionModelChanged: (
      state,
      action: PayloadActionWithId<{ clipVisionModel: CLIPVisionModelV2 }>
    ) => {
      const { id, clipVisionModel } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (!isIPAdapterConfig(entity.config)) {
        return;
      }
      entity.config.clipVisionModel = clipVisionModel;
    },
    refImageIPAdapterWeightChanged: (state, action: PayloadActionWithId<{ weight: number }>) => {
      const { id, weight } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (!isIPAdapterConfig(entity.config)) {
        return;
      }
      entity.config.weight = weight;
    },
    refImageKrea2StyleStrengthChanged: (state, action: PayloadActionWithId<{ styleStrength: number }>) => {
      const { id, styleStrength } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (!isKrea2ReferenceImageConfig(entity.config)) {
        return;
      }
      entity.config.styleStrength = styleStrength;
    },
    refImageIPAdapterBeginEndStepPctChanged: (
      state,
      action: PayloadActionWithId<{ beginEndStepPct: [number, number] }>
    ) => {
      const { id, beginEndStepPct } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (!isIPAdapterConfig(entity.config)) {
        return;
      }
      entity.config.beginEndStepPct = beginEndStepPct;
    },
    refImageDeleted: (state, action: PayloadActionWithId) => {
      const { id } = action.payload;
      const currentIndex = state.entities.findIndex((rg) => rg.id === id);
      state.entities = state.entities.filter((rg) => rg.id !== id);
      const nextIndex = clamp(currentIndex, 0, state.entities.length - 1);
      const nextEntity = state.entities[nextIndex];
      state.selectedEntityId = nextEntity?.id ?? null;
      if (state.selectedEntityId === null) {
        state.isPanelOpen = false;
      }
    },
    refImageSelected: (state, action: PayloadAction<{ id: string }>) => {
      const { id } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      if (state.isPanelOpen && state.selectedEntityId === id) {
        state.isPanelOpen = false;
      } else {
        state.isPanelOpen = true;
      }
      state.selectedEntityId = id;
    },
    refImageIsEnabledToggled: (state, action: PayloadActionWithId) => {
      const { id } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      entity.isEnabled = !entity.isEnabled;
    },
    refImageConfigChanged: (state, action: PayloadActionWithId<{ config: RefImageState['config'] }>) => {
      const { id, config } = action.payload;
      const entity = selectRefImageEntity(state, id);
      if (!entity) {
        return;
      }
      // Preserve the existing image when replacing the config
      entity.config = { ...config, image: entity.config.image };
    },
    refImagesReset: () => getInitialRefImagesState(),
    refImagesReordered: (state, action: PayloadAction<{ ids: string[] }>) => {
      const { ids } = action.payload;
      if (ids.length !== state.entities.length) {
        return;
      }
      if (new Set(ids).size !== ids.length) {
        return;
      }
      const byId = new Map(state.entities.map((e) => [e.id, e]));
      const next: RefImageState[] = [];
      for (const id of ids) {
        const entity = byId.get(id);
        if (!entity) {
          return;
        }
        next.push(entity);
      }
      state.entities = next;
    },
  },
  extraReducers(builder) {
    // See canvasSlice: reference images are both personal workspace state and a place
    // deleted-image references live; a deliberate sign-out clears them for the next account.
    // `sessionExpiredLogout` is deliberately not handled. Not undoable, so the reset alone is
    // the whole job here.
    builder.addCase(logout, () => getInitialRefImagesState());
  },
});

export const {
  refImageSelected,
  refImageAdded,
  refImageDeleted,
  refImageImageChanged,
  refImageIPAdapterMethodChanged,
  refImageModelChanged,
  refImageConfigChanged,
  refImageIPAdapterCLIPVisionModelChanged,
  refImageIPAdapterWeightChanged,
  refImageIPAdapterBeginEndStepPctChanged,
  refImageFLUXReduxImageInfluenceChanged,
  refImageKrea2StyleStrengthChanged,
  refImageIsEnabledToggled,
  refImagesRecalled,
  refImagesReordered,
} = slice.actions;

export const refImagesSliceConfig: SliceConfig<typeof slice> = {
  slice,
  schema: zRefImagesState,
  getInitialState: getInitialRefImagesState,
  persistConfig: {
    migrate: (state) => zRefImagesState.parse(state),
    persistDenylist: ['selectedEntityId', 'isPanelOpen'],
  },
};

export const selectRefImagesSlice = (state: RootState) => state.refImages;

export const selectReferenceImageEntities = createSelector(selectRefImagesSlice, (state) => state.entities);
export const selectSelectedRefEntityId = createSelector(selectRefImagesSlice, (state) => state.selectedEntityId);
export const selectIsRefImagePanelOpen = createSelector(selectRefImagesSlice, (state) => state.isPanelOpen);
export const selectRefImageEntityIds = createMemoizedSelector(selectReferenceImageEntities, (entities) =>
  entities.map((e) => e.id)
);
export const selectRefImageEntity = (state: RefImagesState, id: string) =>
  state.entities.find((entity) => entity.id === id) ?? null;

export function selectRefImageEntityOrThrow(state: RefImagesState, id: string, caller: string): RefImageState {
  const entity = selectRefImageEntity(state, id);
  assert(entity, `Entity with id ${id} not found in ${caller}`);
  return entity;
}