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.

5.1 KB
Raw Blame
import type { ModelConfig } from '@features/models';
import type { AccountScope } from '@platform/state/accountLifecycle';

import { flushGenerateDrafts } from '@features/generation/react';
import {
  assertAccountScopeCurrent,
  captureAccountScope,
  isAccountScopeCurrent,
} from '@platform/state/accountLifecycle';

import type { InvocationSourceId, ResultDestination } from './invocationContracts';
import type { SubmitResolvedInvocationDeps } from './invocationSubmit';
import type { PrepareCanvasInvocationArgs } from './widgets/canvas/invoke/prepareCanvasInvocation';
import type { WorkbenchCommands, WorkbenchQueries } from './workbenchStore';

import { createDeferredResource } from './deferredResource';
import { isInvocationRouteValid, resolveInvocationRoute } from './invocation';
import { beginInvocationPreparation, endInvocationPreparation } from './invocationPreparation';
import { submitResolvedInvocation } from './invocationSubmit';

export interface ActiveInvocationSubmissionArgs {
  commands: WorkbenchCommands;
  destinationOverride?: ResultDestination;
  formatCanvasInvocationFailure: PrepareCanvasInvocationArgs['formatCanvasInvocationFailure'];
  formatUpscaleMetadataFailure: SubmitResolvedInvocationDeps['formatUpscaleMetadataFailure'];
  getModels: () => readonly ModelConfig[] | undefined;
  /** Invoke Front: the backend runs this batch ahead of the user's other pending work. */
  prepend?: boolean;
  queries: WorkbenchQueries;
  /**
   * Runs this source's route, its source fixed, as a graph preview of that source does, rather than the active route.
   * Video uses Gallery; other sources keep the active route's destination.
   */
  sourceId?: InvocationSourceId;
  /** Hears that the submission has started: it is valid and holds the project's preparation lease. */
  onStarted?: () => void;
}

export interface ActiveInvocationSubmissionRuntime {
  assertCurrent: (owner: AccountScope) => void;
  capture: () => AccountScope;
  flushDrafts: () => void;
  isCurrent: (owner: AccountScope) => boolean;
  loadPrepareCanvasInvocation: () => Promise<{
    prepareCanvasInvocation: SubmitResolvedInvocationDeps['prepareCanvasInvocation'];
  }>;
  submit: (args: SubmitResolvedInvocationDeps) => Promise<void> | void;
}

type CanvasInvocationModule = {
  prepareCanvasInvocation: SubmitResolvedInvocationDeps['prepareCanvasInvocation'];
};

const canvasInvocationModule = createDeferredResource<CanvasInvocationModule>(
  () => import('./widgets/canvas/invoke/prepareCanvasInvocation')
);

const loadCanvasInvocationModule = (): Promise<CanvasInvocationModule> =>
  canvasInvocationModule.getStatus() === 'failed' ? canvasInvocationModule.retry() : canvasInvocationModule.load();

/** Warms the canvas-only invocation chunk without adding it to the eager workbench bundle. */
export const preloadCanvasInvocation = (): void => canvasInvocationModule.preload();

const runtime: ActiveInvocationSubmissionRuntime = {
  assertCurrent: assertAccountScopeCurrent,
  capture: captureAccountScope,
  flushDrafts: flushGenerateDrafts,
  isCurrent: isAccountScopeCurrent,
  loadPrepareCanvasInvocation: loadCanvasInvocationModule,
  submit: submitResolvedInvocation,
};

export const submitActiveInvocation = async (
  {
    commands,
    destinationOverride,
    formatCanvasInvocationFailure,
    formatUpscaleMetadataFailure,
    getModels,
    prepend,
    queries,
    sourceId,
    onStarted,
  }: ActiveInvocationSubmissionArgs,
  activeRuntime: ActiveInvocationSubmissionRuntime = runtime
): Promise<void> => {
  const owner = activeRuntime.capture();
  activeRuntime.flushDrafts();
  const snapshot = queries.getSnapshot();
  const active = snapshot.activeProject.invocation;
  const invocation = {
    ...active,
    ...(destinationOverride ? { destination: destinationOverride } : {}),
    ...(sourceId ? { sourceId, sourceLocked: true } : {}),
    ...(sourceId === 'video' ? { destination: 'gallery' as const } : {}),
  };
  const models = getModels();
  const route = resolveInvocationRoute(snapshot.activeProject, sourceId ? 'dialog' : 'global', invocation, models);

  if (!isInvocationRouteValid(route) || snapshot.backendConnection.status !== 'connected') {
    return;
  }

  const isCanvasSubmission = route.sourceId === 'canvas';
  // Every route may await (Canvas chunk, prompt expansion, workflow generators) on settings captured above.
  const preparationLease = beginInvocationPreparation(snapshot.activeProject.id);

  if (!preparationLease) {
    return;
  }

  onStarted?.();

  try {
    const prepareCanvasInvocation = isCanvasSubmission
      ? (await activeRuntime.loadPrepareCanvasInvocation()).prepareCanvasInvocation
      : () => Promise.resolve();

    activeRuntime.assertCurrent(owner);
    await activeRuntime.submit({
      commands,
      formatCanvasInvocationFailure,
      formatUpscaleMetadataFailure,
      models,
      owner,
      prepareCanvasInvocation,
      ...(prepend ? { prepend } : {}),
      project: snapshot.activeProject,
      route,
      scopedDefaults: snapshot.scopedDefaults,
    });
  } catch (error) {
    if (!activeRuntime.isCurrent(owner)) {
      return;
    }

    throw error;
  } finally {
    endInvocationPreparation(preparationLease);
  }
};