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.

10.9 KB
Raw Blame
import type { WorkflowGraphPreviewPort, WorkflowProjectPersistence, WorkflowUiAdapter } from '@features/workflow/react';
import type { ProjectSyncSnapshot } from '@workbench/projects/syncStore';
import type { WorkbenchPreferences } from '@workbench/settings/contracts';
import type { WorkbenchSnapshot } from '@workbench/workbenchStore';
import type { ReactNode } from 'react';

import { getAuthSession, getCapabilities, subscribeAuthSession } from '@features/identity';
import { ensureModelsLoaded, openModelManager } from '@features/models';
import { nodeExecutionStore } from '@features/nodes';
import { hasPendingWorkflowQueueItem } from '@features/queue';
import { WorkflowGraphPreviewProvider, WorkflowUiProvider } from '@features/workflow/react';
import { useMountEffect } from '@platform/react/useMountEffect';
import { createProjectedExternalStore } from '@platform/state/projectedExternalStore';
import { shallowEqual } from '@platform/state/selectors';
import { useWorkbenchFocus } from '@workbench/focusRegions';
import { useFindGalleryItem } from '@workbench/image-actions/useFindGalleryItem';
import { markWorkbenchPerf, measureWorkbenchPerf, timeWorkbenchPerf } from '@workbench/performanceMarks';
import { getProjectSyncSnapshot, subscribeProjectSync } from '@workbench/projects/syncStore';
import { getActiveProjectWorkflow, getProjectWorkflowHistory } from '@workbench/projectWorkflows';
import {
  getWorkbenchPreferences,
  patchWorkbenchPreferences,
  subscribeWorkbenchPreferences,
} from '@workbench/settings/store';
import { useNotify } from '@workbench/useNotify';
import { useOpenWorkbenchWidget } from '@workbench/useOpenWorkbenchWidget';
import { getProjectWidgetValues, getResolvedProjectWidgetValues } from '@workbench/widgetState';
import { useWorkbenchCommands, useWorkbenchInternalStore, useWorkbenchQueries } from '@workbench/WorkbenchContext';
import { useMemo } from 'react';

import { WorkflowCommandShortcut } from './WorkflowCommandShortcut';

const selectWorkflowPreferences = (preferences: WorkbenchPreferences) => ({
  reduceMotion: preferences.reduceMotion,
  themeId: preferences.themeId,
  workflowEdgeStyle: preferences.workflowEdgeStyle,
  workflowEdgesBehindNodes: preferences.workflowEdgesBehindNodes,
  workflowGroupNodesByCategory: preferences.workflowGroupNodesByCategory,
  workflowLibraryCardSize: preferences.workflowLibraryCardSize,
  workflowLibraryLayout: preferences.workflowLibraryLayout,
  workflowLibrarySort: preferences.workflowLibrarySort,
  workflowLibrarySortDirection: preferences.workflowLibrarySortDirection,
  workflowLibraryThumbnailFit: preferences.workflowLibraryThumbnailFit,
  workflowShowMinimap: preferences.workflowShowMinimap,
  workflowSnapToGrid: preferences.workflowSnapToGrid,
  workflowValidateConnections: preferences.workflowValidateConnections,
});

/**
 * The active project's own persistence, read from the same sync service the conflict banner uses. "Saved" means the
 * server acknowledged the document; a pending push with browser recovery is still only local.
 */
const selectProjectPersistence = ([workbench, sync]: readonly [
  WorkbenchSnapshot,
  ProjectSyncSnapshot,
]): WorkflowProjectPersistence => {
  const projectId = workbench.activeProject.id;
  const info = sync.projects[projectId];
  const autosave = workbench.autosave;
  const isUnacknowledged = info === undefined || info.revision === null || info.isPendingPush;
  const status: WorkflowProjectPersistence['status'] = info?.conflict
    ? 'conflict'
    : info?.schemaRefusal
      ? 'error'
      : autosave.status === 'error'
        ? 'error'
        : autosave.status === 'saving'
          ? 'saving'
          : autosave.status === 'pending' || isUnacknowledged
            ? 'pending'
            : 'saved';

  return {
    error: autosave.error ?? null,
    hasLocalRecovery: sync.localDraftStatus === 'ok',
    lastSavedAt: status === 'saved' ? (autosave.lastSavedAt ?? sync.lastSyncedAt) : null,
    status,
  };
};

/** One source over two stores: a change in either republishes a fresh pair, an unchanged pair keeps identity. */
const createPairedSource = <A, B>(
  a: { getSnapshot: () => A; subscribe: (listener: () => void) => () => void },
  b: { getSnapshot: () => B; subscribe: (listener: () => void) => () => void }
) => {
  const cache = { pair: [a.getSnapshot(), b.getSnapshot()] as readonly [A, B] };

  return {
    getSnapshot: (): readonly [A, B] => {
      const next = [a.getSnapshot(), b.getSnapshot()] as const;

      if (next[0] !== cache.pair[0] || next[1] !== cache.pair[1]) {
        cache.pair = next;
      }

      return cache.pair;
    },
    subscribe: (listener: () => void) => {
      const unsubscribers = [a.subscribe(listener), b.subscribe(listener)];

      return () => unsubscribers.forEach((unsubscribe) => unsubscribe());
    },
  };
};

const WorkflowGraphPreviewAdapterProvider = ({ children }: { children: ReactNode }) => {
  const commands = useWorkbenchCommands();
  const openWidget = useOpenWorkbenchWidget();

  const adapter = useMemo<WorkflowGraphPreviewPort>(
    () => ({
      focusSource: (sourceId) => {
        if (sourceId) {
          openWidget(sourceId);
        }
      },
      openDocumentInNewProject: (document, label, source) => {
        // Create first: the new project's blank placeholder is what the document takes over.
        commands.projects.create();
        commands.workflows.add(document, { label, reusePlaceholder: true, source });
        openWidget('workflow');
      },
      openWorkflowEditor: () => {
        openWidget('workflow');
      },
    }),
    [commands, openWidget]
  );

  return <WorkflowGraphPreviewProvider adapter={adapter}>{children}</WorkflowGraphPreviewProvider>;
};

/** Production binding of Workflow's stable services and narrow read ports. */
export const WorkflowUiAdapterProvider = ({ children }: { children: ReactNode }) => {
  const store = useWorkbenchInternalStore();
  const commands = useWorkbenchCommands();
  const queries = useWorkbenchQueries();
  const notify = useNotify();
  const { focusRegion } = useWorkbenchFocus();

  useMountEffect(() => {
    void ensureModelsLoaded();
  });

  const project = useMemo(
    () =>
      createProjectedExternalStore({
        source: store,
        select: (snapshot) => {
          const activeWorkflow = getActiveProjectWorkflow(snapshot.activeProject);
          const history = getProjectWorkflowHistory(snapshot.activeProject, activeWorkflow.document.id);

          return {
            activeWorkflow,
            activeWorkflowId: activeWorkflow.document.id,
            canRedo: history.future.length > 0,
            canUndo: history.past.length > 0,
            galleryValues: getResolvedProjectWidgetValues(snapshot.activeProject, 'gallery', snapshot.scopedDefaults),
            id: snapshot.activeProject.id,
            isWorkflowRunning: hasPendingWorkflowQueueItem(snapshot.activeProject.queue.items),
            projectGraph: activeWorkflow.document,
            workflowValues: getProjectWidgetValues(snapshot.activeProject, 'workflow'),
            workflows: snapshot.activeProject.workflows.entries,
          };
        },
        isEqual: shallowEqual,
      }),
    [store]
  );
  const persistence = useMemo(
    () =>
      createProjectedExternalStore({
        isEqual: shallowEqual,
        select: selectProjectPersistence,
        source: createPairedSource(store, { getSnapshot: getProjectSyncSnapshot, subscribe: subscribeProjectSync }),
      }),
    [store]
  );
  const preferences = useMemo(
    () =>
      createProjectedExternalStore({
        source: { getSnapshot: getWorkbenchPreferences, subscribe: subscribeWorkbenchPreferences },
        select: selectWorkflowPreferences,
        isEqual: shallowEqual,
      }),
    []
  );
  const capabilities = useMemo(
    () =>
      createProjectedExternalStore({
        source: { getSnapshot: getAuthSession, subscribe: subscribeAuthSession },
        select: (session) => ({
          canManageNodes: getCapabilities(session).canManageNodes,
          canUseCache: !session.multiuserEnabled || session.user?.is_admin === true,
        }),
        isEqual: shallowEqual,
      }),
    []
  );

  const findInGallery = useFindGalleryItem();
  const adapter = useMemo<WorkflowUiAdapter>(
    () => ({
      CommandShortcut: WorkflowCommandShortcut,
      capabilities,
      commands: {
        addWorkflow: (document, options) => commands.workflows.add(document, options),
        createWorkflow: () => commands.workflows.create(),
        duplicateWorkflow: (workflowId, copyName) => commands.workflows.duplicate(workflowId, copyName),
        editGraph: commands.workflows.editGraph,
        redo: commands.workflows.redo,
        removeWorkflow: (workflowId) => commands.workflows.remove(workflowId),
        renameWorkflow: (workflowId, name, projectId) => {
          commands.workflows.rename(workflowId, name, projectId);
        },
        replaceWorkflow: (target, document, options) => commands.workflows.replaceDocument(target, document, options),
        selectWorkflow: (workflowId) => commands.workflows.select(workflowId),
        setWorkflowSource: (target, source) =>
          commands.workflows.setSource(target.projectId, target.workflowId, source),
        undo: commands.workflows.undo,
      },
      findInGallery,
      getProjectGraph: () => getActiveProjectWorkflow(queries.getSnapshot().activeProject).document,
      nodeExecution: {
        get: nodeExecutionStore.get,
        getOrigin: nodeExecutionStore.getOrigin,
        subscribe: nodeExecutionStore.subscribe,
        subscribeOrigin: nodeExecutionStore.subscribeOrigin,
      },
      notifications: { error: notify.error, info: notify.info, success: notify.success },
      openAddModels: (query) => openModelManager({ projectId: queries.getSnapshot().activeProject.id, search: query }),
      openNodesManager: () => {
        window.location.hash = '#/nodes';
      },
      performance: {
        mark: (name, source) => markWorkbenchPerf(name, source),
        measure: (name, start, source, end) => measureWorkbenchPerf(name, start, source, end),
        time: (name, source, callback) => timeWorkbenchPerf(name, source, callback),
      },
      persistence,
      preferences,
      project,
      setLibraryPreferences: (patch) => {
        void patchWorkbenchPreferences(patch);
      },
      widgets: {
        // Workflow opens widgets from its buttons: the opened widget takes focus and the region highlight.
        open: (options) => {
          commands.widgets.open(options);
          focusRegion(options.region, options.widgetId);
        },
        patchValues: (widgetId, values) => commands.widgets.patchValues(widgetId, values),
      },
    }),
    [
      capabilities,
      commands,
      findInGallery,
      focusRegion,
      notify.error,
      notify.info,
      notify.success,
      persistence,
      preferences,
      project,
      queries,
    ]
  );

  return (
    <WorkflowUiProvider adapter={adapter}>
      <WorkflowGraphPreviewAdapterProvider>{children}</WorkflowGraphPreviewAdapterProvider>
    </WorkflowUiProvider>
  );
};