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.

3.2 KB
Raw Blame
import type { InvocationSourceId, ResultDestination } from '@workbench/invocationContracts';
import type { Project } from '@workbench/projectContracts';
import type { WidgetTypeId } from '@workbench/widgetContracts';

import { autoSwitchDestinations } from './autoRoutePolicy';
import { getSourceLabel, isInvocationSourceAvailable } from './invocation';

const graphWidgetTypeIds = ['generate', 'canvas', 'upscale', 'video', 'workflow'] as const;

export type GraphWidgetTypeId = (typeof graphWidgetTypeIds)[number];

const sourceIdsByWidgetTypeId: Record<GraphWidgetTypeId, InvocationSourceId> = {
  canvas: 'canvas',
  generate: 'generate',
  upscale: 'upscale',
  video: 'video',
  workflow: 'workflow',
};

const widgetTypeIdsBySourceId: Record<InvocationSourceId, GraphWidgetTypeId> = {
  canvas: 'canvas',
  generate: 'generate',
  upscale: 'upscale',
  video: 'video',
  workflow: 'workflow',
};

export const isGraphWidgetTypeId = (typeId: WidgetTypeId): typeId is GraphWidgetTypeId =>
  (graphWidgetTypeIds as readonly string[]).includes(typeId);

export const getSourceIdForWidgetTypeId = (typeId: WidgetTypeId): InvocationSourceId | null =>
  isGraphWidgetTypeId(typeId) ? sourceIdsByWidgetTypeId[typeId] : null;

export const getWidgetTypeIdForSourceId = (sourceId: InvocationSourceId): GraphWidgetTypeId =>
  widgetTypeIdsBySourceId[sourceId];

export const getNaturalDestination = (sourceId: InvocationSourceId): ResultDestination =>
  autoSwitchDestinations[sourceId];

export interface GraphWidgetSource {
  sourceId: InvocationSourceId;
  typeId: GraphWidgetTypeId;
  label: string;
}

export const graphWidgetSources: GraphWidgetSource[] = graphWidgetTypeIds
  .map((typeId) => ({
    label: getSourceLabel(sourceIdsByWidgetTypeId[typeId]),
    sourceId: sourceIdsByWidgetTypeId[typeId],
    typeId,
  }))
  .filter((source) => isInvocationSourceAvailable(source.sourceId));

/** Floating graph widgets are both visible and placed, so include them in invocation-source discovery. */
const addFloatingWidgetTypeIds = (project: Project, typeIds: Set<WidgetTypeId>): void => {
  for (const instanceId of Object.keys(project.floatingWidgets ?? {})) {
    const typeId = project.widgetInstances[instanceId]?.typeId;

    if (typeId) {
      typeIds.add(typeId);
    }
  }
};

// Collapsed regions still count as visible because disclosure does not change routing.
export const getVisibleWidgetTypeIds = (project: Project): Set<WidgetTypeId> => {
  const typeIds = new Set<WidgetTypeId>();

  for (const region of Object.values(project.widgetRegions)) {
    const typeId = project.widgetInstances[region.activeInstanceId]?.typeId;

    if (typeId) {
      typeIds.add(typeId);
    }
  }

  // Shaded and maximized windows count too, for the same reason.
  addFloatingWidgetTypeIds(project, typeIds);

  return typeIds;
};

export const getPlacedWidgetTypeIds = (project: Project): Set<WidgetTypeId> => {
  const typeIds = new Set<WidgetTypeId>();

  for (const region of Object.values(project.widgetRegions)) {
    for (const instanceId of region.instanceIds) {
      const typeId = project.widgetInstances[instanceId]?.typeId;

      if (typeId) {
        typeIds.add(typeId);
      }
    }
  }

  addFloatingWidgetTypeIds(project, typeIds);

  return typeIds;
};