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.

11.6 KB
Raw Blame
import type { ModelConfig } from '@features/models';
import type { CanvasControlLayerContract, ControlType } from '@workbench/canvas-engine/api';
import type { ControlDetectionStatus } from '@workbench/canvas-operations/api';
import type { ControlImageState } from '@workbench/canvasLayerContent';
import type { ControlMapState } from '@workbench/controlLayerChecks';

import { createControlLayer } from '@workbench/canvas-engine/api';
import { getControlImageState } from '@workbench/canvasLayerContent';
import { getTypeAvailability, resolveControlModel, type ControlModelMemory } from '@workbench/controlModelResolution';
import { createInstance } from 'i18next';
import { describe, expect, it } from 'vitest';

import {
  deriveControlPanel,
  type ControlPanelInput,
  type ControlPanelModel,
  type PanelText,
} from './controlPanelModel';

const model = (key: string, base: string, type = 'controlnet', extra: Partial<ModelConfig> = {}): ModelConfig =>
  ({ base, key, name: key, source: `hf/${key}`, type, ...extra }) as ModelConfig;

const depthA = model('sdxl-depth-a', 'sdxl');
const depthB = model('sdxl-depth-b', 'sdxl');
const pose = model('sdxl-openpose', 'sdxl');
const t2iCanny = model('t2i-adapter-canny-sdxl', 'sdxl', 't2i_adapter');
const fluxUnion = model('flux-controlnet-union-pro', 'flux');

const IMAGE = { image: { height: 64, imageName: 'photo.png', width: 96 }, type: 'image' } as const;
const IDLE: ControlDetectionStatus = { status: 'idle' };

const controlLayer = (
  overrides: Partial<CanvasControlLayerContract> & { model?: string | null } = {}
): CanvasControlLayerContract => {
  const { model: adapterModel, ...rest } = overrides;
  const created = createControlLayer({ base: 'sdxl', id: 'control-1', name: 'Control Layer 1' });
  const adapter = { ...created.adapter, ...rest.adapter };
  return { ...created, ...rest, adapter: adapterModel === undefined ? adapter : { ...adapter, model: adapterModel } };
};

const detected = (controlType: ControlType, detector: string | null, extra: Partial<CanvasControlLayerContract> = {}) =>
  controlLayer({
    controlType,
    detection: detector ? { settings: {}, type: detector } : null,
    ...(detector ? { filter: { settings: {}, type: detector }, original: IMAGE } : {}),
    source: IMAGE,
    ...extra,
  });

const derive = (
  layer: CanvasControlLayerContract,
  {
    base = 'sdxl',
    imageState,
    map = 'current',
    models = [depthA, pose],
    reason = null,
    remembered = {},
    status = IDLE,
  }: {
    base?: string | null;
    imageState?: ControlImageState;
    map?: ControlMapState;
    models?: readonly ModelConfig[];
    reason?: ControlPanelInput['reason'];
    remembered?: ControlModelMemory;
    status?: ControlDetectionStatus;
  } = {}
): ControlPanelModel => {
  const supportsKind = () => true;
  return deriveControlPanel({
    availability: getTypeAvailability(base, models, supportsKind),
    base: base ? { id: base, label: base.toUpperCase() } : null,
    imageState: imageState ?? getControlImageState(layer, false),
    layer,
    map,
    reason,
    resolution: resolveControlModel({ base, layer, models, remembered, supportsKind }),
    status,
  });
};

const seen: PanelText[] = [];
const collect = (panel: ControlPanelModel): ControlPanelModel => {
  const texts = [
    ...Object.values(panel.type.hints).map((hint) => hint.text),
    panel.detection.valueText,
    panel.detection.settingsUnavailable,
    ...panel.detection.options.flatMap((option) => [option.label, option.hint]),
    panel.model.valueText,
    panel.model.caption,
    panel.adapter.stepsReadout,
  ];
  seen.push(...texts.filter((text): text is PanelText => text !== null));
  return panel;
};
const panelOf = (...args: Parameters<typeof derive>) => collect(derive(...args));

describe('control panel design states', () => {
  it('1: a new empty layer asks for nothing yet and waits for a type', () => {
    const panel = panelOf(controlLayer());

    expect(panel.image).toEqual({ imageName: null, state: 'empty' });
    expect(panel.type).toMatchObject({ invalid: false, value: null });
    expect(panel.detection).toMatchObject({
      disabled: true,
      options: [],
      valueText: { key: 'detectionSetByType' },
    });
    expect(panel.model).toMatchObject({
      action: null,
      caption: { key: 'typeMissing' },
      captionTone: 'muted',
      valueText: { key: 'modelValue.setByType' },
    });
  });

  it('2: an image without a type warns on Type and says the model follows it', () => {
    const panel = panelOf(controlLayer({ source: IMAGE }));

    expect(panel.image).toEqual({ imageName: 'photo.png', state: 'image' });
    expect(panel.type.invalid).toBe(true);
    expect(panel.model).toMatchObject({ caption: { key: 'modelCaption.chooseType' }, captionTone: 'warning' });
  });

  it('3: a depth layer with one depth model runs its default detector and names the only model', () => {
    const panel = panelOf(detected('depth', 'depth_anything_depth_estimation'));

    expect(panel.detection).toMatchObject({
      action: null,
      disabled: false,
      settings: { detector: 'depth_anything_depth_estimation', values: {} },
      settingsUnavailable: null,
      value: 'depth_anything_depth_estimation',
      valueText: { key: 'filters.depth_anything_depth_estimation' },
    });
    expect(panel.detection.options.map((option) => [option.value, option.disabled])).toEqual([
      ['depth_anything_depth_estimation', false],
      ['normal_map', false],
      ['none', false],
    ]);
    expect(panel.model).toMatchObject({
      action: null,
      caption: { key: 'modelCaption.only', refs: { kind: 'kinds.controlnet', type: 'typeNouns.depth' } },
      name: 'sdxl-depth-a',
      tone: 'default',
    });
  });

  it('5: an explicit or remembered pick says which it is and offers Reset', () => {
    const explicit = panelOf(
      detected('depth', 'depth_anything_depth_estimation', {
        adapter: { ...controlLayer().adapter, model: 'sdxl-depth-b' },
      }),
      { models: [depthA, depthB] }
    );
    const remembered = panelOf(detected('depth', 'depth_anything_depth_estimation'), {
      models: [depthA, depthB],
      remembered: { 'sdxl:depth': 'sdxl-depth-b' },
    });

    expect(explicit.model).toMatchObject({
      action: 'reset',
      caption: { key: 'modelCaption.pick', refs: { kind: 'kinds.controlnet', type: 'typeNouns.depth' } },
      name: 'sdxl-depth-b',
    });
    expect(remembered.model).toMatchObject({
      action: 'reset',
      caption: { key: 'modelCaption.remembered', refs: { kind: 'kinds.controlnet', type: 'typeNouns.depth' } },
      name: 'sdxl-depth-b',
    });
  });
});

describe('control panel detection', () => {
  it('shows the running detector, queued or processing', () => {
    const layer = detected('depth', 'depth_anything_depth_estimation');

    expect(panelOf(layer, { status: { status: 'queued' } }).detection.busy).toEqual({
      detector: 'depth_anything_depth_estimation',
    });
    expect(panelOf(layer, { status: { detector: 'normal_map', status: 'processing' } }).detection.busy).toEqual({
      detector: 'normal_map',
    });
    expect(panelOf(layer).detection.busy).toBeNull();
  });

  it('offers Run again after a failure, and Detect again for an out-of-date map at rest', () => {
    const layer = detected('depth', 'depth_anything_depth_estimation');
    const failed = panelOf(layer, { map: 'stale', status: { error: { code: 'process-failed' }, status: 'error' } });

    expect(failed.detection).toMatchObject({ action: 'retry', failed: { code: 'process-failed' }, tone: 'error' });
    expect(panelOf(layer, { map: 'stale' }).detection.action).toBe('detect');
    expect(panelOf(layer, { map: 'stale', status: { status: 'queued' } }).detection.action).toBeNull();
  });

  it('keeps detectors out of reach until there is something to detect from', () => {
    const empty = panelOf(controlLayer({ controlType: 'depth', detection: null }));
    const mapOnly = panelOf(
      controlLayer({
        controlType: 'depth',
        detection: { settings: {}, type: 'depth_anything_depth_estimation' },
        filter: { settings: {}, type: 'depth_anything_depth_estimation' },
        source: IMAGE,
      }),
      { map: 'no-input' }
    );

    expect(empty.detection.options.map((option) => [option.value, option.disabled, option.hint?.key ?? null])).toEqual([
      ['depth_anything_depth_estimation', true, 'detectionNeedsImage'],
      ['normal_map', true, 'detectionNeedsImage'],
      ['none', false, null],
    ]);
    expect(mapOnly.detection.options[0]).toMatchObject({ disabled: true, hint: { key: 'detectionNoInput' } });
    expect(mapOnly.detection.settings).toBeNull();
    expect(mapOnly.detection.settingsUnavailable).toEqual({ key: 'detectionNoInput' });
    const emptied = panelOf(
      controlLayer({ controlType: 'depth', detection: { settings: {}, type: 'depth_anything_depth_estimation' } })
    );
    expect(emptied.detection.settingsUnavailable).toEqual({ key: 'detectionNeedsImage' });
  });
});

describe('control panel model caption', () => {
  it('says what a model is made for when it is not made for what the detector produces', () => {
    const normalModel = model('sdxl-normal', 'sdxl');
    const panel = panelOf(
      detected('depth', 'depth_anything_depth_estimation', {
        adapter: { ...controlLayer().adapter, model: 'sdxl-normal' },
      }),
      { models: [depthA, normalModel] }
    );

    expect(panel.model).toMatchObject({
      caption: { key: 'modelCaption.mismatch', refs: { family: 'families.depth', madeFor: 'families.normal' } },
      captionTone: 'warning',
    });
  });
});

describe('control panel adapter rows', () => {
  it('makes Priority unavailable for a non-ControlNet model and for a FLUX ControlNet, which sends no mode', () => {
    const sdxl = panelOf(detected('depth', 'depth_anything_depth_estimation'));
    const t2i = panelOf(detected('edges', 'canny_edge_detection'), { models: [t2iCanny] });
    const flux = panelOf(detected('depth', 'depth_anything_depth_estimation'), { base: 'flux', models: [fluxUnion] });

    expect(sdxl.adapter).toMatchObject({ kind: 'controlnet', priority: { unavailable: null, value: 'balanced' } });
    expect(t2i.adapter).toMatchObject({ kind: 't2i_adapter', priority: { unavailable: 'kind' } });
    expect(flux.adapter).toMatchObject({ kind: 'controlnet', priority: { unavailable: 'flux' } });
  });

  it('keeps a stored legacy unbalanced mode as the value, so the panel can show it', () => {
    const legacy = panelOf(
      detected('depth', 'depth_anything_depth_estimation', {
        adapter: { ...controlLayer().adapter, beginEndStepPct: [0.1, 0.75], controlMode: 'unbalanced' },
      })
    );

    expect(legacy.adapter).toEqual({
      kind: 'controlnet',
      priority: { unavailable: null, value: 'unbalanced' },
      stepsReadout: { key: 'stepsReadout', values: { begin: 10, end: 75 } },
    });
  });
});

describe('control panel type hints', () => {
  it('counts models, names union coverage and flags what is missing', () => {
    const { hints } = panelOf(controlLayer(), { models: [depthA, depthB, pose] }).type;

    expect(hints.depth).toEqual({ text: { key: 'typeHint.models', values: { count: 2 } }, tone: 'muted' });
    expect(hints.pose).toEqual({ text: { key: 'typeHint.models', values: { count: 1 } }, tone: 'muted' });
    expect(panelOf(controlLayer(), { base: 'flux', models: [fluxUnion] }).type.hints.edges).toEqual({
      text: { key: 'typeHint.union' },
      tone: 'muted',
    });
  });
});

const englishCatalog = import.meta.glob('../../../../public/locales/en.json', { eager: true, import: 'default' });
const english = createInstance();
await english.init({
  initAsync: false,
  lng: 'en',
  resources: { en: { translation: Object.values(englishCatalog)[0] as Record<string, unknown> } },
});