All files executionGraph.js

99.36% Statements 157/158
79.48% Branches 62/78
100% Functions 10/10
99.36% Lines 157/158

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 1x 5x 5x 5x 1x 22x 22x 22x 3x 3x 22x 22x 22x 22x 22x 22x 1x 1x 11x 11x 7x 7x 7x 7x 11x 11x 1x 11x 11x 11x 1x 9x 9x 5x 5x 9x 4x 4x   9x 1x 11x 11x 11x 11x 11x 25x 25x 6x 6x 25x 11x 11x 11x 1x 1x 7x 7x 7x 7x 16x 13x 13x 16x 16x 16x 13x 13x 16x 16x 16x 16x 16x 16x 16x 16x 18x 12x 18x 18x 7x 7x 7x 18x 13x 13x 13x 13x 16x 5x 5x 5x 5x 13x 13x 13x 7x 7x 7x 1x 1x 4x 4x 4x 1x 1x 5x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 7x 7x 6x 7x 2x 1x 1x 7x 4x 4x 3x 3x 3x 4x 7x 7x 3x 3x 5x  
import { getSpecTypeAndOptions, isDataSocketSpec } from './constants.js';
 
const OMITTED_WIDGET_INPUTS_BY_CLASS = {
  View3D: new Set([
    'camera_azimuth',
    'camera_polar',
    'camera_distance',
    'camera_target_x',
    'camera_target_y',
    'camera_target_z',
  ]),
};
 
function getInputName(handleId) {
  return handleId.split('::')[1];
}
 
function getOutputSlot(handleId) {
  return parseInt(handleId.split('::')[1], 10);
}
 
function resolveExecutionEdge(edge) {
  const original = edge?.data?.groupProxyOriginal;
  if (!original) return edge;
  return {
    ...edge,
    source: original.source || edge.source,
    sourceHandle: original.sourceHandle || edge.sourceHandle,
    target: original.target || edge.target,
    targetHandle: original.targetHandle || edge.targetHandle,
  };
}
 
export function getConnectedNodeIds(edges) {
  const connectedNodeIds = new Set();
  for (const edge of edges) {
    const resolved = resolveExecutionEdge(edge);
    connectedNodeIds.add(resolved.source);
    connectedNodeIds.add(resolved.target);
  }
  return connectedNodeIds;
}
 
function isPreviewLoadNode(node) {
  return ['Image', 'ImageDemo'].includes(node?.data?.className);
}
 
function hasPreviewLoadSelection(node) {
  if (node?.data?.className === 'Image') {
    return !!String(node.data?.widgetValues?.filename || '').trim();
  }
  if (node?.data?.className === 'ImageDemo') {
    return !!String(node.data?.widgetValues?.name || '').trim();
  }
  return false;
}
 
function getRunnableNodeIds(nodes, edges) {
  const connectedNodeIds = getConnectedNodeIds(edges);
 
  const runnableNodeIds = new Set(connectedNodeIds);
  for (const node of nodes) {
    if (connectedNodeIds.has(node.id)) continue;
    if (isPreviewLoadNode(node) && hasPreviewLoadSelection(node)) {
      runnableNodeIds.add(node.id);
    }
  }
 
  return runnableNodeIds;
}
 
export function serializeExecutionGraph(nodes, edges, { excludeManualTrigger = false } = {}) {
  const runnableNodeIds = getRunnableNodeIds(nodes, edges);
  const prompt = {};
 
  for (const node of nodes) {
    if (!runnableNodeIds.has(node.id)) continue;
 
    const { className, definition, widgetValues, runtimeValues } = node.data;
    if (className === 'Group') continue;
    if (!definition) continue;
    if (excludeManualTrigger && definition.manual_trigger) continue;
 
    const inputs = {};
    const valueBag = { ...(widgetValues || {}), ...(runtimeValues || {}) };
    const omittedInputs = OMITTED_WIDGET_INPUTS_BY_CLASS[className] || null;
 
    const allWidgets = {
      ...(definition.input.required || {}),
      ...(definition.input.optional || {}),
    };
    for (const [name, spec] of Object.entries(allWidgets)) {
      if (omittedInputs?.has(name)) continue;
      const [type] = getSpecTypeAndOptions(spec);
      if (isDataSocketSpec(spec)) continue;
      if (type === 'BUTTON') continue;
      if (valueBag[name] !== undefined) {
        inputs[name] = valueBag[name];
      }
    }
 
    const incoming = edges
      .map(resolveExecutionEdge)
      .filter((edge) => edge.target === node.id);
    for (const edge of incoming) {
      const inputName = getInputName(edge.targetHandle);
      const outputSlot = getOutputSlot(edge.sourceHandle);
      inputs[inputName] = [edge.source, outputSlot];
    }
 
    prompt[node.id] = { class_type: className, inputs };
  }
 
  return prompt;
}
 
export function getAutoRunnableNodes(nodes, edges) {
  const runnableNodeIds = getRunnableNodeIds(nodes, edges);
  return nodes.filter((node) => runnableNodeIds.has(node.id));
}
 
export function hasBlockingAutoRunInput(node, edges) {
  const def = node.data?.definition;
  if (!def || def.manual_trigger) return false;
 
  const required = def.input.required || {};
  for (const [name, spec] of Object.entries(required)) {
    const [type, opts] = getSpecTypeAndOptions(spec);
    const hiddenByConnectedInput = (() => {
      const raw = opts?.hide_when_input_connected;
      if (!raw) return false;
      const inputs = Array.isArray(raw) ? raw : [raw];
      return inputs.some((inputName) => edges.some(
        (edge) => {
          const resolved = resolveExecutionEdge(edge);
          return resolved.target === node.id && getInputName(resolved.targetHandle) === String(inputName);
        }
      ));
    })();
 
    if (hiddenByConnectedInput) continue;
 
    if (type === 'FILE_PICKER' || type === 'FOLDER_PICKER') {
      if (!node.data.widgetValues?.[name]) return true;
      continue;
    }
    if (!isDataSocketSpec(spec)) continue;
    const hasEdge = edges.some(
      (edge) => {
        const resolved = resolveExecutionEdge(edge);
        return resolved.target === node.id && getInputName(resolved.targetHandle) === name;
      }
    );
    if (!hasEdge) return true;
  }
 
  return false;
}