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.

1.4 KB
Raw Blame
import type { XYPosition } from '@xyflow/react';
import { reduce } from 'es-toolkit/compat';
import { SHARED_NODE_PROPERTIES } from 'features/nodes/types/constants';
import type { FieldInputInstance } from 'features/nodes/types/field';
import type { InvocationNode, InvocationTemplate } from 'features/nodes/types/invocation';
import { buildFieldInputInstance } from 'features/nodes/util/schema/buildFieldInputInstance';
import { v4 as uuidv4 } from 'uuid';

export const buildInvocationNode = (position: XYPosition, template: InvocationTemplate): InvocationNode => {
  const nodeId = uuidv4();
  const { type } = template;

  const inputs = reduce(
    template.inputs,
    (inputsAccumulator, inputTemplate, inputName) => {
      const fieldId = uuidv4();

      const inputFieldValue: FieldInputInstance = buildFieldInputInstance(fieldId, inputTemplate);

      inputsAccumulator[inputName] = inputFieldValue;

      return inputsAccumulator;
    },
    {} as Record<string, FieldInputInstance>
  );

  const node: InvocationNode = {
    ...SHARED_NODE_PROPERTIES,
    id: nodeId,
    type: 'invocation',
    position,
    data: {
      id: nodeId,
      type,
      version: template.version,
      label: '',
      notes: '',
      isOpen: true,
      isIntermediate: type === 'save_image' ? false : true,
      useCache: template.useCache,
      nodePack: template.nodePack,
      inputs,
      dynamicInputTemplates: {},
    },
  };

  return node;
};