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.

19.5 KB
Raw Blame
import { logger } from 'app/logging/logger';
import { deepClone } from 'common/util/deepClone';
import { withResultAsync } from 'common/util/result';
import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
import { getPrefixedId } from 'features/controlLayers/konva/util';
import {
  type CanvasRegionalGuidanceState,
  isRegionalGuidanceFLUXReduxConfig,
  isRegionalGuidanceIPAdapterConfig,
  type Rect,
} from 'features/controlLayers/store/types';
import { getRegionalGuidanceWarnings } from 'features/controlLayers/store/validators';
import { IMAGE_INFLUENCE_TO_SETTINGS } from 'features/nodes/util/graph/generation/addFLUXRedux';
import type { Graph } from 'features/nodes/util/graph/generation/Graph';
import { serializeError } from 'serialize-error';
import type { Invocation, MainModelConfig } from 'services/api/types';
import { assert } from 'tsafe';

const log = logger('system');

type AddedRegionResult = {
  addedPositivePrompt: boolean;
  addedNegativePrompt: boolean;
  addedAutoNegativePositivePrompt: boolean;
  addedIPAdapters: number;
  addedFLUXReduxes: number;
};

type RegionalPositiveConditioning = Invocation<
  | 'compel'
  | 'sdxl_compel_prompt'
  | 'flux_text_encoder'
  | 'flux2_klein_text_encoder'
  | 'flux2_dev_text_encoder'
  | 'z_image_text_encoder'
  | 'anima_text_encoder'
  | 'krea2_text_encoder'
>;

type PositiveConditioningSource = Invocation<
  RegionalPositiveConditioning['type'] | 'krea2_conditioning_rebalance' | 'krea2_seed_variance'
>;

type AddRegionsArg = {
  manager: CanvasManager;
  regions: CanvasRegionalGuidanceState[];
  g: Graph;
  bbox: Rect;
  model: MainModelConfig;
  posCond: RegionalPositiveConditioning;
  negCond: Invocation<
    | 'compel'
    | 'sdxl_compel_prompt'
    | 'flux_text_encoder'
    | 'flux2_klein_text_encoder'
    | 'flux2_dev_text_encoder'
    | 'z_image_text_encoder'
    | 'anima_text_encoder'
    | 'krea2_text_encoder'
  > | null;
  posCondCollect: Invocation<'collect'>;
  negCondCollect: Invocation<'collect'> | null;
  ipAdapterCollect: Invocation<'collect'>;
  fluxReduxCollect: Invocation<'collect'> | null;
  transformRegionalPositiveConditioning?: (conditioning: RegionalPositiveConditioning) => PositiveConditioningSource;
};

/**
 * Adds regional guidance to the graph
 * @param manager The canvas manager
 * @param regions Array of regions to add
 * @param g The graph to add the layers to
 * @param bbox The bounding box
 * @param model The main model
 * @param posCond The positive conditioning node
 * @param negCond The negative conditioning node
 * @param posCondCollect The positive conditioning collector
 * @param negCondCollect The negative conditioning collector
 * @param ipAdapterCollect The IP adapter collector
 * @param fluxReduxConnect The IP adapter collector
 * @returns A promise that resolves to the regions that were successfully added to the graph
 */

export const addRegions = async ({
  manager,
  regions,
  g,
  bbox,
  model,
  posCond,
  negCond,
  posCondCollect,
  negCondCollect,
  ipAdapterCollect,
  fluxReduxCollect,
  transformRegionalPositiveConditioning,
}: AddRegionsArg): Promise<AddedRegionResult[]> => {
  const isSDXL = model.base === 'sdxl';
  const isFLUX = model.base === 'flux';
  const isFlux2 = model.base === 'flux2';
  // FLUX.2 Klein uses the Qwen3 encoder; FLUX.2 [dev] uses the Mistral encoder. The region
  // conditioning node type must match the main model's encoder.
  const isFlux2Dev = isFlux2 && 'variant' in model && model.variant === 'dev';
  const isFlux2Klein = isFlux2 && !isFlux2Dev;
  const isZImage = model.base === 'z-image';
  const isAnima = model.base === 'anima';
  const isKrea2 = model.base === 'krea-2';

  const validRegions = regions
    .filter((entity) => entity.isEnabled)
    .filter((entity) => getRegionalGuidanceWarnings(entity, model).length === 0);

  const results: AddedRegionResult[] = [];

  for (const region of validRegions) {
    const result: AddedRegionResult = {
      addedPositivePrompt: false,
      addedNegativePrompt: false,
      addedAutoNegativePositivePrompt: false,
      addedIPAdapters: 0,
      addedFLUXReduxes: 0,
    };

    const getImageDTOResult = await withResultAsync(() => {
      const adapter = manager.adapters.regionMasks.get(region.id);
      assert(adapter, 'Adapter not found');
      return adapter.renderer.rasterize({ rect: bbox, attrs: { opacity: 1, filters: [] } });
    });
    if (getImageDTOResult.isErr()) {
      log.warn({ error: serializeError(getImageDTOResult.error) }, 'Error rasterizing region mask');
      continue;
    }

    const imageDTO = getImageDTOResult.value;

    // The main mask-to-tensor node
    const maskToTensor = g.addNode({
      type: 'alpha_mask_to_tensor',
      id: getPrefixedId('prompt_region_mask_to_tensor'),
      image: {
        image_name: imageDTO.image_name,
      },
    });

    if (region.positivePrompt) {
      // The main positive conditioning node
      result.addedPositivePrompt = true;
      let regionalPosCond: RegionalPositiveConditioning;
      if (isSDXL) {
        regionalPosCond = g.addNode({
          type: 'sdxl_compel_prompt',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
          style: region.positivePrompt, // TODO: Should we put the positive prompt in both fields?
        });
      } else if (isFLUX) {
        regionalPosCond = g.addNode({
          type: 'flux_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      } else if (isFlux2Dev) {
        regionalPosCond = g.addNode({
          type: 'flux2_dev_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      } else if (isFlux2Klein) {
        regionalPosCond = g.addNode({
          type: 'flux2_klein_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      } else if (isZImage) {
        regionalPosCond = g.addNode({
          type: 'z_image_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      } else if (isAnima) {
        regionalPosCond = g.addNode({
          type: 'anima_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      } else if (isKrea2) {
        regionalPosCond = g.addNode({
          type: 'krea2_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      } else {
        regionalPosCond = g.addNode({
          type: 'compel',
          id: getPrefixedId('prompt_region_positive_cond'),
          prompt: region.positivePrompt,
        });
      }
      // Connect the mask to the conditioning
      g.addEdge(maskToTensor, 'mask', regionalPosCond, 'mask');
      // Copy the connections to the "global" positive conditioning node to the regional cond
      if (posCond.type === 'compel') {
        for (const edge of g.getEdgesTo(posCond, ['clip', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'sdxl_compel_prompt') {
        for (const edge of g.getEdgesTo(posCond, ['clip', 'clip2', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'flux_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['clip', 't5_encoder', 't5_max_seq_len', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'flux2_klein_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['qwen3_encoder', 'max_seq_len', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'flux2_dev_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['mistral_encoder', 'max_seq_len', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'z_image_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['qwen3_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'anima_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['qwen3_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'krea2_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['qwen3_vl_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCond.id;
          g.addEdgeFromObj(clone);
        }
      } else {
        assert(false, 'Unsupported positive conditioning node type.');
      }
      // Apply any model-specific conditioning transforms before collection.
      const conditioningSource = transformRegionalPositiveConditioning?.(regionalPosCond) ?? regionalPosCond;
      g.addEdge(conditioningSource, 'conditioning', posCondCollect, 'item');
    }

    if (region.negativePrompt) {
      // FLUX.2 regions with negative prompts are filtered out by getRegionalGuidanceWarnings; fail
      // loudly if that ever changes, because there is no flux2 branch below.
      assert(!isFlux2, 'Regional negative prompts are not supported for FLUX.2 Klein');
      assert(!isKrea2, 'Canvas regional negative prompts are not supported for Krea-2');
      assert(negCond, 'Negative conditioning node is required if there is a negative prompt');
      assert(negCondCollect, 'Negative conditioning collector is required if there is a negative prompt');

      // The main negative conditioning node
      result.addedNegativePrompt = true;
      let regionalNegCond: Invocation<
        'compel' | 'sdxl_compel_prompt' | 'flux_text_encoder' | 'z_image_text_encoder' | 'anima_text_encoder'
      >;
      if (isSDXL) {
        regionalNegCond = g.addNode({
          type: 'sdxl_compel_prompt',
          id: getPrefixedId('prompt_region_negative_cond'),
          prompt: region.negativePrompt,
          style: region.negativePrompt,
        });
      } else if (isFLUX) {
        regionalNegCond = g.addNode({
          type: 'flux_text_encoder',
          id: getPrefixedId('prompt_region_negative_cond'),
          prompt: region.negativePrompt,
        });
      } else if (isZImage) {
        regionalNegCond = g.addNode({
          type: 'z_image_text_encoder',
          id: getPrefixedId('prompt_region_negative_cond'),
          prompt: region.negativePrompt,
        });
      } else if (isAnima) {
        regionalNegCond = g.addNode({
          type: 'anima_text_encoder',
          id: getPrefixedId('prompt_region_negative_cond'),
          prompt: region.negativePrompt,
        });
      } else {
        regionalNegCond = g.addNode({
          type: 'compel',
          id: getPrefixedId('prompt_region_negative_cond'),
          prompt: region.negativePrompt,
        });
      }

      // Connect the mask to the conditioning
      g.addEdge(maskToTensor, 'mask', regionalNegCond, 'mask');
      // Connect the conditioning to the collector
      g.addEdge(regionalNegCond, 'conditioning', negCondCollect, 'item');
      // Copy the connections to the "global" negative conditioning node to the regional cond
      if (negCond.type === 'compel') {
        for (const edge of g.getEdgesTo(negCond, ['clip', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalNegCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (negCond.type === 'sdxl_compel_prompt') {
        for (const edge of g.getEdgesTo(negCond, ['clip', 'clip2', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalNegCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (negCond.type === 'flux_text_encoder') {
        for (const edge of g.getEdgesTo(negCond, ['clip', 't5_encoder', 't5_max_seq_len', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalNegCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (negCond.type === 'z_image_text_encoder') {
        for (const edge of g.getEdgesTo(negCond, ['qwen3_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalNegCond.id;
          g.addEdgeFromObj(clone);
        }
      } else if (negCond.type === 'anima_text_encoder') {
        for (const edge of g.getEdgesTo(negCond, ['qwen3_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalNegCond.id;
          g.addEdgeFromObj(clone);
        }
      } else {
        assert(false, 'Unsupported negative conditioning node type.');
      }
    }

    // If we are using the "invert" auto-negative setting, we need to add an additional negative conditioning node
    if (region.autoNegative && region.positivePrompt) {
      // See note on the negative prompt branch above — unreachable for FLUX.2 via validators.
      assert(!isFlux2, 'Auto-negative is not supported for FLUX.2 Klein');
      assert(!isKrea2, 'Canvas auto-negative is not supported for Krea-2');
      assert(negCondCollect, 'Negative conditioning collector is required if there is an auto-negative setting');

      result.addedAutoNegativePositivePrompt = true;
      // We re-use the mask image, but invert it when converting to tensor
      const invertTensorMask = g.addNode({
        id: getPrefixedId('prompt_region_invert_tensor_mask'),
        type: 'invert_tensor_mask',
      });
      // Connect the OG mask image to the inverted mask-to-tensor node
      g.addEdge(maskToTensor, 'mask', invertTensorMask, 'mask');
      // Create the conditioning node. It's going to be connected to the negative cond collector, but it uses the positive prompt
      let regionalPosCondInverted: Invocation<
        'compel' | 'sdxl_compel_prompt' | 'flux_text_encoder' | 'z_image_text_encoder' | 'anima_text_encoder'
      >;
      if (isSDXL) {
        regionalPosCondInverted = g.addNode({
          type: 'sdxl_compel_prompt',
          id: getPrefixedId('prompt_region_positive_cond_inverted'),
          prompt: region.positivePrompt,
          style: region.positivePrompt,
        });
      } else if (isFLUX) {
        regionalPosCondInverted = g.addNode({
          type: 'flux_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond_inverted'),
          prompt: region.positivePrompt,
        });
      } else if (isZImage) {
        regionalPosCondInverted = g.addNode({
          type: 'z_image_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond_inverted'),
          prompt: region.positivePrompt,
        });
      } else if (isAnima) {
        regionalPosCondInverted = g.addNode({
          type: 'anima_text_encoder',
          id: getPrefixedId('prompt_region_positive_cond_inverted'),
          prompt: region.positivePrompt,
        });
      } else {
        regionalPosCondInverted = g.addNode({
          type: 'compel',
          id: getPrefixedId('prompt_region_positive_cond_inverted'),
          prompt: region.positivePrompt,
        });
      }
      // Connect the inverted mask to the conditioning
      g.addEdge(invertTensorMask, 'mask', regionalPosCondInverted, 'mask');
      // Connect the conditioning to the negative collector
      g.addEdge(regionalPosCondInverted, 'conditioning', negCondCollect, 'item');
      // Copy the connections to the "global" positive conditioning node to our regional node
      if (posCond.type === 'compel') {
        for (const edge of g.getEdgesTo(posCond, ['clip', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCondInverted.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'sdxl_compel_prompt') {
        for (const edge of g.getEdgesTo(posCond, ['clip', 'clip2', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCondInverted.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'flux_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['clip', 't5_encoder', 't5_max_seq_len', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCondInverted.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'z_image_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['qwen3_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCondInverted.id;
          g.addEdgeFromObj(clone);
        }
      } else if (posCond.type === 'anima_text_encoder') {
        for (const edge of g.getEdgesTo(posCond, ['qwen3_encoder', 'mask'])) {
          const clone = deepClone(edge);
          clone.destination.node_id = regionalPosCondInverted.id;
          g.addEdgeFromObj(clone);
        }
      } else {
        assert(false, 'Unsupported positive conditioning node type.');
      }
    }

    for (const { id, config } of region.referenceImages) {
      if (isRegionalGuidanceIPAdapterConfig(config)) {
        assert(!isFLUX, 'Regional IP adapters are not supported for FLUX.');
        assert(!isKrea2, 'Regional IP adapters are not supported for Krea-2.');

        result.addedIPAdapters++;
        const { weight, model, clipVisionModel, method, beginEndStepPct, image } = config;
        assert(model, 'IP Adapter model is required');
        assert(image, 'IP Adapter image is required');

        const ipAdapterNode = g.addNode({
          id: `ip_adapter_${id}`,
          type: 'ip_adapter',
          weight,
          method,
          ip_adapter_model: model,
          clip_vision_model: clipVisionModel,
          begin_step_percent: beginEndStepPct[0],
          end_step_percent: beginEndStepPct[1],
          image: {
            image_name: image.image_name,
          },
        });

        // Connect the mask to the conditioning
        g.addEdge(maskToTensor, 'mask', ipAdapterNode, 'mask');
        g.addEdge(ipAdapterNode, 'ip_adapter', ipAdapterCollect, 'item');
      } else if (isRegionalGuidanceFLUXReduxConfig(config)) {
        assert(isFLUX, 'Regional FLUX Redux requires FLUX.');
        assert(fluxReduxCollect !== null, 'FLUX Redux collector is required.');
        result.addedFLUXReduxes++;
        const { model: fluxReduxModel, image } = config;
        assert(fluxReduxModel, 'FLUX Redux model is required');
        assert(image, 'FLUX Redux image is required');

        const fluxReduxNode = g.addNode({
          id: `flux_redux_${id}`,
          type: 'flux_redux',
          redux_model: fluxReduxModel,
          image: {
            image_name: image.image_name,
          },
          ...IMAGE_INFLUENCE_TO_SETTINGS[config.imageInfluence ?? 'highest'],
        });

        // Connect the mask to the conditioning
        g.addEdge(maskToTensor, 'mask', fluxReduxNode, 'mask');
        g.addEdge(fluxReduxNode, 'redux_cond', fluxReduxCollect, 'item');
      }
    }

    results.push(result);
  }

  g.upsertMetadata({ regions: validRegions });

  return results;
};