A platform for community discussion. Free, open, simple.
FEATURE: simplify and enable merge node (#40805)
Joffrey JAFFEUXdf73c14ceec1
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/custom-renderer.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/custom-renderer.jsindex 32b814a562f..c30c596d0ef 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/custom-renderer.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/custom-renderer.js@@ -189,7 +189,9 @@ export function createCustomRenderer(Scope, getElementCenter, classicPath) {sourceClientId: payload.source,targetClientId: payload.target,sourceOutput: payload.sourceOutput,+ sourceOutputIndex: payload.sourceOutputIndex,targetInput: payload.targetInput,+ targetInputIndex: payload.targetInputIndex,},pathD: "",arrowTransform: "",diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjsindex 682657c67af..e3712bfdf45 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/index.gjs@@ -418,8 +418,10 @@ export default class WorkflowCanvas extends Component {this.args.onOpenNodePanel?.({connectionSource: connectionInfo.sourceClientId,connectionSourceOutput: connectionInfo.sourceOutput,+ connectionSourceOutputIndex: connectionInfo.sourceOutputIndex,connectionTarget: connectionInfo.targetClientId,connectionTargetInput: connectionInfo.targetInput,+ connectionTargetInputIndex: connectionInfo.targetInputIndex,});}@@ -429,7 +431,9 @@ export default class WorkflowCanvas extends Component {connectionInfo.sourceClientId,connectionInfo.sourceOutput,connectionInfo.targetClientId,- connectionInfo.targetInput+ connectionInfo.targetInput,+ connectionInfo.sourceOutputIndex,+ connectionInfo.targetInputIndex);}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/rete-editor.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/rete-editor.jsindex 48877a2f678..ed8df040091 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/rete-editor.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/canvas/rete-editor.js@@ -5,13 +5,17 @@ import {buildWorkflowGraphIndex,graphConnectionKey,LOOP_OUTPUT,+ nextAvailableTargetInputIndex,normalizeSourceOutput,normalizeSourceOutputIndex,normalizeTargetInput,normalizeTargetInputIndex,+ portIndexFromKey,} from "../../../lib/workflows/graph-constants";import {+ nodeTypeInputAcceptsMultipleConnections,nodeTypeInputs,+ nodeTypeInputUsesConnectionIndexes,nodeTypeOutputKeys,} from "../../../lib/workflows/node-types";import {@@ -80,9 +84,11 @@ export class ReteEditorBridge {new ClassicPreset.Input(socket,key === "main" ? "" : key,- data.type === "flow:loop_over_items" ||- (data.type === "flow:merge" &&- (data.configuration?.mode || "append") === "append")+ nodeTypeInputAcceptsMultipleConnections(+ nodeTypesByIdentifier.get(data.type) || data.type,+ key,+ data+ )));}@@ -192,6 +198,7 @@ export class ReteEditorBridge {getNodeHeight,getNodeLabel,inputKeysFor,+ nodeTypesByIdentifier,});renderer.onNodeDelete = (clientId) => {@@ -223,6 +230,7 @@ export class ReteEditorBridge {getNodeHeight,getNodeLabel,inputKeysFor,+ nodeTypesByIdentifier,}) {this.editor = editor;this.area = area;@@ -242,6 +250,7 @@ export class ReteEditorBridge {this.getNodeHeight = getNodeHeight;this.getNodeLabel = getNodeLabel;this.inputKeysFor = inputKeysFor;+ this.nodeTypesByIdentifier = nodeTypesByIdentifier;this.isSyncing = false;this.isAutoArranging = false;this.wasDragging = false;@@ -345,7 +354,8 @@ export class ReteEditorBridge {conn.sourceOutput,conn.target,conn.targetInput,- conn.sourceOutputIndex+ conn.sourceOutputIndex,+ conn.targetInputIndex);}}@@ -452,18 +462,55 @@ export class ReteEditorBridge {inputKeyFor(targetNode, connection) {return (- Object.keys(targetNode.inputs || {})[+ Object.keys(targetNode?.inputs || {})[normalizeTargetInputIndex(connection)] || normalizeTargetInput(connection.targetInput));}+ targetInputIndexFor(targetNode, connection) {+ if (connection.targetInputIndex != null) {+ return connection.targetInputIndex;+ }++ const inputKey = this.inputKeyFor(targetNode, connection);+ const targetNodeType =+ this.nodeTypesByIdentifier.get(targetNode?.workflowData?.type) ||+ targetNode?.workflowData?.type;++ if (+ nodeTypeInputUsesConnectionIndexes(+ targetNodeType,+ inputKey,+ targetNode?.workflowData+ )+ ) {+ return nextAvailableTargetInputIndex(+ this.editor.getConnections(),+ targetNode.id,+ connection+ );+ }++ return portIndexFromKey(+ connection.targetInput,+ Object.keys(targetNode?.inputs || {})+ );+ }+annotateConnectionIndexes(connection) {const sourceNode = this.editor.getNode(connection.sourceClientId || connection.source);+ const targetNode = this.editor.getNode(+ connection.targetClientId || connection.target+ );connection.sourceOutputIndex = this.outputIndexFor(sourceNode, connection);+ connection.targetInputIndex = this.targetInputIndexFor(+ targetNode,+ connection+ );return connection;}@@ -564,6 +611,10 @@ export class ReteEditorBridge {sourceOutput,sourceOutputIndex,});+ connection.targetInputIndex = this.targetInputIndexFor(targetNode, {+ targetInput,+ targetInputIndex,+ });await this.editor.addConnection(connection);}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/graph-utils.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/graph-utils.jsindex cfe86e24a3e..c90cf2973f6 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/graph-utils.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/graph-utils.js@@ -3,11 +3,17 @@ import {collectLoopBodyNodeIds,graphConnectionKey,LOOP_NODE_TYPE,+ nextAvailableTargetInputIndex,normalizeSourceOutput,normalizeSourceOutputIndex,normalizeTargetInputIndex,- portIndexFromKey,} from "../../../lib/workflows/graph-constants";+import { NODE_DIRECT_SETTING_KEYS } from "../../../lib/workflows/node-data-shape";+import {+ nodeTypeConnectionIndexedInputKey,+ nodeTypeHasConfigurationFields,+ nodeTypeInputUsesConnectionIndexes,+} from "../../../lib/workflows/node-types";export {buildOutgoingIndex,@@ -23,15 +29,15 @@ function clientConnectionKey(connection) {});}-function mergeModeFor(node) {- return node?.configuration?.mode || "append";-}--function cloneConnectionWithTargetInputIndex(connection, targetInputIndex) {+function cloneConnectionWithTargetInputIndex(+ connection,+ targetInput,+ targetInputIndex+) {return {...connection,+ targetInput,targetInputIndex,- targetInput: `input_${targetInputIndex + 1}`,};}@@ -42,137 +48,101 @@ function hasExplicitTargetInput(connection) {);}-function nextUnusedInputIndex(used) {- let index = 0;- while (used.has(index)) {- index++;- }- return index;-}--export function normalizeConnectionsForNodes(connections, nodes) {+export function normalizeConnectionsForNodes(+ connections,+ nodes,+ nodeTypeForNode = (node) => node?.type+) {const nodeByClientId = new Map(nodes.map((node) => [node.clientId, node]));- const usedMergeInputs = new Map();-- for (const connection of connections) {+ const explicitIndexedConnections = connections.flatMap((connection) => {const targetNode = nodeByClientId.get(connection.targetClientId);if (- targetNode?.type === "flow:merge" &&- hasExplicitTargetInput(connection)+ !inputUsesIndexedConnections(targetNode, nodeTypeForNode) ||+ !hasExplicitTargetInput(connection)) {- const used = usedMergeInputs.get(connection.targetClientId) || new Set();- used.add(normalizeTargetInputIndex(connection));- usedMergeInputs.set(connection.targetClientId, used);+ return [];}- }- return connections.flatMap((connection) => {+ return [+ cloneConnectionWithTargetInputIndex(+ connection,+ indexedConnectionInputKey(targetNode, nodeTypeForNode),+ normalizeTargetInputIndex(connection)+ ),+ ];+ });+ const normalizedConnections = [];+ const allocatedIndexedConnections = [...explicitIndexedConnections];++ for (const connection of connections) {const targetNode = nodeByClientId.get(connection.targetClientId);- if (targetNode?.type !== "flow:merge") {- return [connection];+ if (!inputUsesIndexedConnections(targetNode, nodeTypeForNode)) {+ normalizedConnections.push(connection);+ continue;}- const mode = mergeModeFor(targetNode);const targetInputIndex = normalizeTargetInputIndex(connection);-- if (mode === "append") {- if (!hasExplicitTargetInput(connection)) {- const used =- usedMergeInputs.get(connection.targetClientId) || new Set();- const nextInputIndex = nextUnusedInputIndex(used);- used.add(nextInputIndex);- usedMergeInputs.set(connection.targetClientId, used);- return [- cloneConnectionWithTargetInputIndex(connection, nextInputIndex),- ];- }-- return [- cloneConnectionWithTargetInputIndex(connection, targetInputIndex),- ];- }+ const targetInput = indexedConnectionInputKey(targetNode, nodeTypeForNode);if (hasExplicitTargetInput(connection)) {- return targetInputIndex < 2- ? [cloneConnectionWithTargetInputIndex(connection, targetInputIndex)]- : [];- }-- const used = usedMergeInputs.get(connection.targetClientId) || new Set();- const nextInputIndex = [0, 1].find((input) => !used.has(input));- if (nextInputIndex == null) {- return [];+ normalizedConnections.push(+ cloneConnectionWithTargetInputIndex(+ connection,+ targetInput,+ targetInputIndex+ )+ );+ continue;}- used.add(nextInputIndex);- usedMergeInputs.set(connection.targetClientId, used);-- return [cloneConnectionWithTargetInputIndex(connection, nextInputIndex)];- });-}--export function normalizeMergeConfiguration(configuration = {}) {- const mode = configuration.mode || "append";-- if (mode === "append") {- return {- mode,- number_inputs: Math.max(- parseInt(configuration.number_inputs, 10) || 2,- 2- ),- };+ const normalizedConnection = cloneConnectionWithTargetInputIndex(+ connection,+ targetInput,+ nextAvailableTargetInputIndex(+ allocatedIndexedConnections,+ connection.targetClientId+ )+ );+ normalizedConnections.push(normalizedConnection);+ allocatedIndexedConnections.push(normalizedConnection);}- if (mode === "choose_branch") {- return {- mode,- use_data_of_input: configuration.use_data_of_input || "input_1",- use_data_of_input_index:- configuration.use_data_of_input_index ??- portIndexFromKey(configuration.use_data_of_input),- choose_output: configuration.choose_output || "specified_input",- };- }-- const combineBy = configuration.combine_by || "matching_fields";- const normalized = {- mode,- combine_by: combineBy,- resolve_clash: configuration.resolve_clash || "prefer_last",- merge_mode: configuration.merge_mode || "deep_merge",- override_empty: Boolean(configuration.override_empty),- fuzzy_compare: Boolean(configuration.fuzzy_compare),- };+ return normalizedConnections;+}- if (combineBy === "matching_fields") {- return {- ...normalized,- fields_to_match: configuration.fields_to_match || [],- join_mode: configuration.join_mode || "keep_matches",- output_data_from: configuration.output_data_from || "both",- multiple_matches: configuration.multiple_matches || "all",- };- }+function indexedConnectionInputKey(targetNode, nodeTypeForNode) {+ return (+ nodeTypeConnectionIndexedInputKey(+ nodeTypeForNode(targetNode),+ targetNode+ ) || "main"+ );+}- if (combineBy === "position") {- return {- ...normalized,- include_unpaired: Boolean(configuration.include_unpaired),- };+function inputUsesIndexedConnections(targetNode, nodeTypeForNode) {+ if (!targetNode) {+ return false;}- return normalized;+ return nodeTypeInputUsesConnectionIndexes(+ nodeTypeForNode(targetNode),+ indexedConnectionInputKey(targetNode, nodeTypeForNode),+ targetNode+ );}-export function normalizeNodeConfiguration(node) {- if (node?.type !== "flow:merge") {+export function normalizeNodeConfiguration(node, nodeType = node?.type) {+ if (nodeTypeHasConfigurationFields(nodeType, node)) {return node;}return {...node,- configuration: normalizeMergeConfiguration(node.configuration),+ configuration: Object.fromEntries(+ NODE_DIRECT_SETTING_KEYS.filter((key) =>+ Object.hasOwn(node.configuration || {}, key)+ ).map((key) => [key, node.configuration[key]])+ ),};}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjsindex 1977796f1d9..84402c9ae46 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs@@ -10,10 +10,12 @@ import { i18n } from "discourse-i18n";import WorkflowEditorSession from "../../../lib/workflows/editor-session";import {connectionMatchesEndpoint,+ nextAvailableTargetInputIndex,normalizeSourceOutputIndex,portIndexFromKey,} from "../../../lib/workflows/graph-constants";import {+ nodeTypeInputUsesConnectionIndexes,nodeTypeLabel,nodeTypeOutputKeys,nodeTypePrimaryOutputKey,@@ -275,7 +277,8 @@ export default class WorkflowsEditor extends Component {const nodes = this.#initNodes();return normalizeConnectionsForNodes(deserializeConnections(this.args.workflow.connections || {}, nodes),- nodes+ nodes,+ (node) => this.#nodeTypeFor(node));}@@ -460,16 +463,38 @@ export default class WorkflowsEditor extends Component {nodes = this.formApi.get("nodes")) {const sourceNode = nodes?.find((node) => node.clientId === sourceClientId);- const sourceNodeType =- this.workflowsNodeTypes.findNodeType(sourceNode?.type) ||- sourceNode?.type;return portIndexFromKey(sourceOutput,- nodeTypeOutputKeys(sourceNodeType, sourceNode)+ nodeTypeOutputKeys(this.#nodeTypeFor(sourceNode), sourceNode));}+ #nodeTypeFor(node) {+ return this.workflowsNodeTypes.findNodeType(node?.type) || node?.type;+ }++ #targetInputIndexFor(+ targetClientId,+ targetInput,+ nodes = this.formApi.get("nodes"),+ connections = this.formApi.get("connections")+ ) {+ const targetNode = nodes?.find((node) => node.clientId === targetClientId);++ if (+ !nodeTypeInputUsesConnectionIndexes(+ this.#nodeTypeFor(targetNode),+ targetInput,+ targetNode+ )+ ) {+ return portIndexFromKey(targetInput);+ }++ return nextAvailableTargetInputIndex(connections, targetClientId);+ }+#addNewNode(nodeType, position, configOverrides, wireConnections) {const existingNodes = this.formApi.get("nodes");if (existingNodes.length >= MAX_NODES) {@@ -596,7 +621,9 @@ export default class WorkflowsEditor extends Component {targetClientId,nodeType,configOverrides = null,- targetInput = "main"+ targetInput = "main",+ sourceOutputIndex = null,+ targetInputIndex = null) {const existingNodes = this.formApi.get("nodes");const sourceNode = existingNodes.find((n) => n.clientId === sourceClientId);@@ -618,8 +645,10 @@ export default class WorkflowsEditor extends Component {connectionMatchesEndpoint(connection, {sourceClientId,sourceOutput,+ sourceOutputIndex,targetClientId,targetInput,+ targetInputIndex,}));if (existingIdx >= 0) {@@ -630,11 +659,13 @@ export default class WorkflowsEditor extends Component {sourceClientId,targetClientId: newNode.clientId,sourceOutput,- sourceOutputIndex: this.#sourceOutputIndexFor(- sourceClientId,- sourceOutput,- existingNodes- ),+ sourceOutputIndex:+ sourceOutputIndex ??+ this.#sourceOutputIndexFor(+ sourceClientId,+ sourceOutput,+ existingNodes+ ),targetInput: "main",});connections.push({@@ -642,6 +673,7 @@ export default class WorkflowsEditor extends Component {targetClientId,sourceOutput: nodeTypePrimaryOutputKey(nodeType),targetInput,+ targetInputIndex,});return true;}@@ -725,7 +757,8 @@ export default class WorkflowsEditor extends Component {sourceOutput,targetClientId,targetInput = "main",- sourceOutputIndex = null+ sourceOutputIndex = null,+ targetInputIndex = null) {this.#captureUndo();const nodes = this.formApi.get("nodes");@@ -735,6 +768,12 @@ export default class WorkflowsEditor extends Component {sourceOutput,nodes);+ targetInputIndex ??= this.#targetInputIndexFor(+ targetClientId,+ targetInput,+ nodes,+ connections+ );// Don't create duplicate connectionsconst exists = connections.some((connection) =>@@ -744,6 +783,7 @@ export default class WorkflowsEditor extends Component {sourceOutputIndex,targetClientId,targetInput,+ targetInputIndex,}));if (exists) {@@ -756,6 +796,7 @@ export default class WorkflowsEditor extends Component {sourceOutput,sourceOutputIndex,targetInput,+ targetInputIndex,});this.#ensureLoopSelfConnection(connections,@@ -777,7 +818,8 @@ export default class WorkflowsEditor extends Component {sourceOutput,targetClientId,targetInput = "main",- sourceOutputIndex = null+ sourceOutputIndex = null,+ targetInputIndex = null) {this.#captureUndo();const nodes = this.formApi.get("nodes");@@ -794,6 +836,7 @@ export default class WorkflowsEditor extends Component {sourceOutputIndex,targetClientId,targetInput,+ targetInputIndex,}));@@ -964,11 +1007,14 @@ export default class WorkflowsEditor extends Component {const nodes = this.formApi.get("nodes");const updatedNodes = nodes.map((n) =>n.clientId === clientId- ? normalizeNodeConfiguration({- ...n,- configuration,- name: name || n.name,- })+ ? normalizeNodeConfiguration(+ {+ ...n,+ configuration,+ name: name || n.name,+ },+ this.#nodeTypeFor(n)+ ): n);this.formApi.set("nodes", updatedNodes);@@ -976,7 +1022,8 @@ export default class WorkflowsEditor extends Component {"connections",normalizeConnectionsForNodes(this.formApi.get("connections"),- updatedNodes+ updatedNodes,+ (node) => this.#nodeTypeFor(node)));return this.handleSubmit(options);@@ -1260,7 +1307,9 @@ export default class WorkflowsEditor extends Component {ctx.connectionTarget,nodeType,configOverrides,- ctx.connectionTargetInput+ ctx.connectionTargetInput,+ ctx.connectionSourceOutputIndex,+ ctx.connectionTargetInputIndex);} else if (ctx.canvasX != null) {this.addNodeAtPosition(@@ -1358,7 +1407,8 @@ export default class WorkflowsEditor extends Component {const nodes = this.formApi.get("nodes");const connections = normalizeConnectionsForNodes(this.formApi.get("connections"),- nodes+ nodes,+ (node) => this.#nodeTypeFor(node));const stickyNotes = this.formApi.get("stickyNotes");this.formApi.set("connections", connections);diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/graph-constants.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/graph-constants.jsindex 692abecf9d3..4675683fffe 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/graph-constants.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/graph-constants.js@@ -52,6 +52,35 @@ export function normalizeTargetInputIndex(connection) {);}+export function nextAvailableTargetInputIndex(+ connections,+ targetClientId,+ ignoredConnection = null+) {+ const used = new Set();++ for (const connection of connections) {+ if (+ connection === ignoredConnection ||+ (ignoredConnection?.id && connection.id === ignoredConnection.id)+ ) {+ continue;+ }++ if ((connection.targetClientId ?? connection.target) !== targetClientId) {+ continue;+ }++ used.add(normalizeTargetInputIndex(connection));+ }++ let index = 0;+ while (used.has(index)) {+ index++;+ }+ return index;+}+export function graphConnectionKey({source,sourceOutput,@@ -75,6 +104,7 @@ export function connectionMatchesEndpoint(sourceOutputIndex,targetClientId,targetInput = "main",+ targetInputIndex = null,}) {return (@@ -82,7 +112,8 @@ export function connectionMatchesEndpoint(normalizeSourceOutputIndex(connection) ===(sourceOutputIndex ?? portIndexFromKey(sourceOutput)) &&(connection.targetClientId ?? connection.target) === targetClientId &&- normalizeTargetInputIndex(connection) === portIndexFromKey(targetInput)+ normalizeTargetInputIndex(connection) ===+ (targetInputIndex ?? portIndexFromKey(targetInput)));}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-types.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-types.jsindex f73b81866a0..30558f87ccd 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-types.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-types.js@@ -1,5 +1,6 @@import { trustHTML } from "@ember/template";import { i18n } from "discourse-i18n";+import { LOOP_NODE_TYPE } from "./graph-constants";const DEFAULT_COLOR = "var(--primary-medium)";const DEFAULT_I18N_PREFIX = "discourse_workflows";@@ -194,41 +195,12 @@ function indexedPort(port, index) {};}-function mergeInputs(node) {- const mode = node?.configuration?.mode || "append";-- if (mode === "append") {- const count = Math.max(- parseInt(node?.configuration?.number_inputs, 10) || 2,- 2- );- return Array.from({ length: count }, (_, index) =>- indexedPort(- {- key: `input_${index + 1}`,- display_name: `Input ${index + 1}`,- label: `Input ${index + 1}`,- },- index- )- );- }-- return ["input_1", "input_2"].map((key, index) =>- indexedPort({ key, label: `Input ${index + 1}` }, index)- );-}-export function nodeTypeInputs(nodeTypeOrIdentifier, node = null) {const nodeType = resolveNodeTypeVersion(nodeTypeOrIdentifier,node?.typeVersion);- if (nodeTypeIdentifier(nodeType) === "flow:merge") {- return mergeInputs(node);- }-const inputs = nodeType?.inputs;if (Array.isArray(inputs)) {@@ -238,15 +210,74 @@ export function nodeTypeInputs(nodeTypeOrIdentifier, node = null) {return [{ key: "main", index: 0, required: true }];}-export function nodeTypeInputLabel(+export function nodeTypeInput(nodeTypeOrIdentifier, keyOrIndex, node = null) {+ return nodeTypeInputs(nodeTypeOrIdentifier, node).find(+ (input) => input.key === keyOrIndex || input.index === keyOrIndex+ );+}++export function nodeTypeInputAcceptsMultipleConnections(nodeTypeOrIdentifier,keyOrIndex,node = null) {- const input = nodeTypeInputs(nodeTypeOrIdentifier, node).find(- (item) => item.key === keyOrIndex || item.index === keyOrIndex+ return Boolean(+ nodeTypeInput(nodeTypeOrIdentifier, keyOrIndex, node)?.multiple+ );+}++export function nodeTypeInputUsesConnectionIndexes(+ nodeTypeOrIdentifier,+ keyOrIndex,+ node = null+) {+ if (nodeTypeIdentifier(nodeTypeOrIdentifier) === LOOP_NODE_TYPE) {+ return false;+ }++ const input = nodeTypeInput(nodeTypeOrIdentifier, keyOrIndex, node);+ return Boolean(input?.multiple);+}++export function nodeTypeConnectionIndexedInputKey(+ nodeTypeOrIdentifier,+ node = null+) {+ if (nodeTypeIdentifier(nodeTypeOrIdentifier) === LOOP_NODE_TYPE) {+ return null;+ }++ return nodeTypeInputs(nodeTypeOrIdentifier, node).find(+ (input) => input.multiple+ )?.key;+}++export function nodeTypeHasConfigurationFields(+ nodeTypeOrIdentifier,+ node = null+) {+ if (!nodeTypeOrIdentifier || typeof nodeTypeOrIdentifier === "string") {+ return true;+ }++ const nodeType = resolveNodeTypeVersion(+ nodeTypeOrIdentifier,+ node?.typeVersion);+ return (+ Object.keys(nodeType?.properties || {}).length > 0 ||+ (nodeType?.credentials || []).length > 0+ );+}++export function nodeTypeInputLabel(+ nodeTypeOrIdentifier,+ keyOrIndex,+ node = null+) {+ const input = nodeTypeInput(nodeTypeOrIdentifier, keyOrIndex, node);+if (input?.label_key) {return (translatedOrNull(input.label_key) ||diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex db373e3970d..f4486e4b148 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -662,46 +662,6 @@ en:descending: "Descending"code_field: "JavaScript code"code_description: "Write a comparator function body. Variables a and b are item objects. Return -1, 0, or 1."- merge:- mode: "Mode"- number_inputs: "Number of inputs"- append: "Append"- combine: "Combine"- choose_branch: "Choose branch"- combine_by: "Combine by"- matching_fields: "Matching fields"- position: "Position"- all: "All possible combinations"- fields_to_match: "Fields to match"- field_1: "Input 1 field"- field_2: "Input 2 field"- join_mode: "Output type"- keep_matches: "Keep matches"- keep_non_matches: "Keep non-matches"- keep_everything: "Keep everything"- enrich_input_1: "Enrich input 1"- enrich_input_2: "Enrich input 2"- output_data_from: "Output data from"- both: "Both inputs"- input_1: "Input 1"- input_2: "Input 2"- multiple_matches: "Multiple matches"- first: "First match"- use_data_of_input: "Use data of input"- choose_output: "Output"- specified_input: "Specified input"- empty: "A single, empty item"- include_unpaired: "Include unpaired items"- resolve_clash: "When field values clash"- prefer_last: "Prefer last input"- prefer_input_1: "Prefer input 1"- prefer_input_2: "Prefer input 2"- add_suffix: "Add suffix"- merge_mode: "Merging nested fields"- deep_merge: "Deep merge"- shallow_merge: "Shallow merge"- override_empty: "Minimize empty fields"- fuzzy_compare: "Fuzzy compare"log:mode: "Mode"mode_description: "Choose whether entries are logged once per item or once for all items"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex 9101e81264d..ef008bd132c 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -57,11 +57,6 @@ en:operation_failed: "Tag operation failed: %{errors}"sort:code_missing_return: "Code must contain a return statement."- merge:- invalid_fields_to_match: "Invalid fields to match"- fields_to_match_pair_missing_fields: "Fields to match pair %{index} must define both input fields"- missing_fields_to_match: "Missing fields to match"- fields_to_match_required: 'You need to define at least one pair of fields in "Fields to match"'split_out:destination_count_mismatch: "Destination field name count must match field count."fields_to_include_required: "No fields specified."diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor.rbindex 9b523473da6..e1b24ffe6fa 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb@@ -55,6 +55,7 @@ module DiscourseWorkflows)@steps = []@queue = []+ @queue_index = 0@waiting_inputs = {}@waiting_input_sources = {}@waiting_input_targets = {}@@ -195,15 +196,15 @@ module DiscourseWorkflowsdef process_queueiterations = 0- queue_index = 0+ @queue_index = 0loop do- while queue_index < @queue.length+ while @queue_index < @queue.lengthiterations += 1raise "Max iterations (#{MAX_ITERATIONS}) exceeded" if iterations > MAX_ITERATIONS- node, input_groups, input_sources = @queue[queue_index]- queue_index += 1+ node, input_groups, input_sources = @queue[@queue_index]+ @queue_index += 1execute_node(node, input_groups, input_sources || {})end@@ -819,6 +820,12 @@ module DiscourseWorkflowsnow = Time.currentceiling = now + MAX_WAIT_DURATION_SECONDSresolved = waiting_until.blank? ? ceiling : [waiting_until, ceiling].min+ @context.store_pending_input_groups(+ inputs: @waiting_inputs,+ sources: @waiting_input_sources,+ target_ids: @waiting_input_targets.keys,+ )+ @context.store_pending_queue(@queue.drop(@queue_index || 0))execution =@store.pause_waiting_execution!(node: @waiting_node, waiting_until: resolved, steps: @steps)@@ -840,6 +847,7 @@ module DiscourseWorkflows@snapshot = @store.workflow_snapshot@steps = []@queue = []+ @queue_index = 0@waiting_inputs = {}@waiting_input_sources = {}@waiting_input_targets = {}@@ -851,10 +859,13 @@ module DiscourseWorkflows@snapshot = @store.workflow_snapshot@steps = restore_steps_from(execution)@queue = []+ @queue_index = 0@waiting_inputs = {}@waiting_input_sources = {}@waiting_input_targets = {}@input_wait_requirements = {}+ restore_pending_queue!+ restore_pending_input_groups!enddef clear_waiting!@@ -868,6 +879,40 @@ module DiscourseWorkflowsentries.values.flatten.map { |h| Step.from_h(h) }end+ def restore_pending_input_groups!+ @context.consume_pending_input_groups.each do |target_id, payload|+ target = @snapshot.find_node(target_id)+ next if target.nil?++ @waiting_input_targets[target.id] = target+ @waiting_inputs[target.id] = indexed_values_from_payload(payload["inputs"], "items")+ @waiting_input_sources[target.id] = indexed_values_from_payload(+ payload["sources"],+ "source",+ )+ end+ end++ def restore_pending_queue!+ @queue =+ @context.consume_pending_queue.filter_map do |payload|+ node = @snapshot.find_node(payload["node_id"])+ next if node.nil?++ [+ node,+ indexed_values_from_payload(payload["inputs"], "items"),+ indexed_values_from_payload(payload["sources"], "source"),+ ]+ end+ end++ def indexed_values_from_payload(payload, value_key)+ Array(payload).each_with_object({}) do |entry, values|+ values[entry["index"].to_i] = entry[value_key]+ end+ end+def build_resolver_context(node, input_groups, node_context, input_sources)input_items = primary_input_items(input_groups)current_item = input_items.first || { "json" => {} }diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_context.rbindex b087de17951..59123906d54 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_context.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_context.rb@@ -18,6 +18,8 @@ module DiscourseWorkflows__current_node_id__node_parameters_by_name__waiting_input_sources+ __pending_input_groups+ __pending_queue$json$itemIndex].freeze@@ -106,6 +108,50 @@ module DiscourseWorkflows@context.delete("__waiting_input_sources") || []end+ def store_pending_input_groups(inputs:, sources:, target_ids:)+ payload =+ target_ids.each_with_object({}) do |target_id, result|+ target_inputs = inputs[target_id] || {}+ next if target_inputs.blank?++ result[target_id.to_s] = {+ "inputs" => indexed_payload(target_inputs, "items"),+ "sources" => indexed_payload(sources[target_id] || {}, "source"),+ }+ end++ if payload.present?+ @context["__pending_input_groups"] = payload+ else+ @context.delete("__pending_input_groups")+ end+ end++ def consume_pending_input_groups+ @context.delete("__pending_input_groups") || {}+ end++ def store_pending_queue(entries)+ payload =+ entries.map do |node, inputs, sources|+ {+ "node_id" => node.id.to_s,+ "inputs" => indexed_payload(inputs, "items"),+ "sources" => indexed_payload(sources || {}, "source"),+ }+ end++ if payload.present?+ @context["__pending_queue"] = payload+ else+ @context.delete("__pending_queue")+ end+ end++ def consume_pending_queue+ @context.delete("__pending_queue") || []+ end+def resolver_context(extra_context = {})@context.merge(**extra_context,@@ -222,6 +268,10 @@ module DiscourseWorkflowsdef graph_nodes@workflow_nodes || workflow.nodesend++ def indexed_payload(indexed_values, value_key)+ indexed_values.map { |index, value| { "index" => index.to_i, value_key => value } }+ endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/loop_over_items/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/loop_over_items/v1.rbindex c737fa66940..b1a14e73e5f 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/loop_over_items/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/loop_over_items/v1.rb@@ -12,6 +12,7 @@ module DiscourseWorkflowscolor: "brown",},palette_visible: false,+ inputs: [{ key: "main", type: "main", required: true, multiple: true }],outputs: [{ key: "done", label_key: "discourse_workflows.branch.done" },{ key: "loop", label_key: "discourse_workflows.branch.loop" },diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rbindex f04a5f67614..a49646f310b 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb@@ -11,584 +11,23 @@ module DiscourseWorkflowsicon: "arrows-turn-to-dots",color: "blue",},- palette_visible: false,capabilities: {run_scope: "all_items",},- inputs:- lambda do |configuration = {}|- mode = configuration.fetch("mode") { "append" }- if mode == "append"- count = [configuration.fetch("number_inputs") { 2 }.to_i, 2].max- count.times.map { |index| input_port(index, required: false) }- else- [input_port(0, required: false), input_port(1, required: false)]- end- end,- required_inputs:- lambda do |configuration = {}|- configuration.fetch("mode") { "append" } == "choose_branch" ? [0, 1] : 1- end,- properties: {- mode: {- type: :options,- required: true,- options: %w[append combine choose_branch],- default: "append",- },- number_inputs: {- type: :integer,- required: false,- default: 2,- min: 2,- display_options: {- show: {- mode: ["append"],- },- },- },- combine_by: {- type: :options,- required: false,- options: %w[matching_fields position all],- default: "matching_fields",- display_options: {- show: {- mode: ["combine"],- },- },- },- fields_to_match: {- type: :fixed_collection,- required: false,- display_options: {- show: {- mode: ["combine"],- combine_by: ["matching_fields"],- },- },- type_options: {- multiple_values: true,- },- options: [- {- name: "values",- values: {- field_1: {- type: :string,- required: true,- no_data_expression: true,- },- field_2: {- type: :string,- required: true,- no_data_expression: true,- },- },- },- ],- },- join_mode: {- type: :options,- required: false,- options: %w[- keep_matches- keep_non_matches- keep_everything- enrich_input_1- enrich_input_2- ],- default: "keep_matches",- display_options: {- show: {- mode: ["combine"],- combine_by: ["matching_fields"],- },- },- },- output_data_from: {- type: :options,- required: false,- options: %w[both input_1 input_2],- default: "both",- display_options: {- show: {- mode: ["combine"],- combine_by: ["matching_fields"],- },- },- },- multiple_matches: {- type: :options,- required: false,- options: %w[all first],- default: "all",- display_options: {- show: {- mode: ["combine"],- combine_by: ["matching_fields"],- },- },- },- use_data_of_input: {- type: :options,- required: false,- options: %w[input_1 input_2],- default: "input_1",- display_options: {- show: {- mode: ["choose_branch"],- },- },- },- choose_output: {- type: :options,- required: false,- options: %w[specified_input empty],- default: "specified_input",- display_options: {- show: {- mode: ["choose_branch"],- },- },- },- include_unpaired: {- type: :boolean,- required: false,- default: false,- display_options: {- show: {- mode: ["combine"],- combine_by: ["position"],- },- },- },- resolve_clash: {- type: :options,- required: false,- options: %w[prefer_last prefer_input_1 prefer_input_2 add_suffix],- default: "prefer_last",- display_options: {- show: {- mode: ["combine"],- },- },- },- merge_mode: {- type: :options,- required: false,- options: %w[deep_merge shallow_merge],- default: "deep_merge",- display_options: {- show: {- mode: ["combine"],- },- },- },- override_empty: {- type: :boolean,- required: false,- default: false,- display_options: {- show: {- mode: ["combine"],- },- },- },- fuzzy_compare: {- type: :boolean,- required: false,- default: false,- display_options: {- show: {- mode: ["combine"],- },- },- },- },+ inputs: [+ { key: "main", type: "main", display_name: "Input", required: false, multiple: true },+ ],+ required_inputs: 1,)def execute(exec_ctx)- config = {- "mode" => exec_ctx.get_node_parameter("mode", 0, default: "append"),- "number_inputs" => exec_ctx.get_node_parameter("number_inputs", 0, default: 2),- "combine_by" =>- exec_ctx.get_node_parameter("combine_by", 0, default: "matching_fields"),- "join_mode" => exec_ctx.get_node_parameter("join_mode", 0, default: "keep_matches"),- "output_data_from" =>- exec_ctx.get_node_parameter("output_data_from", 0, default: "both"),- "multiple_matches" =>- exec_ctx.get_node_parameter("multiple_matches", 0, default: "all"),- "use_data_of_input" =>- exec_ctx.get_node_parameter("use_data_of_input", 0, default: "input_1"),- "choose_output" =>- exec_ctx.get_node_parameter("choose_output", 0, default: "specified_input"),- "include_unpaired" =>- exec_ctx.get_node_parameter("include_unpaired", 0, default: false),- "resolve_clash" =>- exec_ctx.get_node_parameter("resolve_clash", 0, default: "prefer_last"),- "merge_mode" => exec_ctx.get_node_parameter("merge_mode", 0, default: "deep_merge"),- "override_empty" => exec_ctx.get_node_parameter("override_empty", 0, default: false),- "fuzzy_compare" => exec_ctx.get_node_parameter("fuzzy_compare", 0, default: false),- }- fields_to_match = exec_ctx.get_node_parameter("fields_to_match.values", 0, default: [])- @input_item_indexes = item_indexes_by_input(exec_ctx.inputs)-- items =- case config.fetch("mode") { "append" }- when "append"- append_inputs(exec_ctx)- when "choose_branch"- choose_branch(two_inputs(exec_ctx), config)- when "combine"- combine(two_inputs(exec_ctx), config, fields_to_match)- else- exec_ctx.input_items("main")- end-- [items]+ [append_inputs(exec_ctx)]endprivate- def self.input_port(index, required: true)- input_number = index + 1- {- key: "input_#{input_number}",- type: "main",- display_name: "Input #{input_number}",- label_key: "discourse_workflows.merge.input_#{input_number}",- required: required,- }- end-def append_inputs(exec_ctx)- indexed_items = exec_ctx.inputs.flatten(1)- return indexed_items if indexed_items.present?-- exec_ctx.input_items("main")- end-- def two_inputs(exec_ctx)- [exec_ctx.input_items(0), exec_ctx.input_items(1)]- end-- def choose_branch(inputs, config)- return [wrap({})] if config.fetch("choose_output") { "specified_input" } == "empty"-- selected_input_index(config) == 0 ? inputs[0] : inputs[1]- end-- def combine(inputs, config, fields_to_match)- case config.fetch("combine_by") { "matching_fields" }- when "position"- combine_by_position(inputs, config)- when "all"- combine_all(inputs, config)- else- combine_by_fields(inputs, config, fields_to_match)- end- end-- def combine_by_position(inputs, config)- preferred_index = preferred_input_index(config, inputs.length)- count =- if config["include_unpaired"]- inputs.map(&:length).max || 0- else- inputs.map(&:length).min || 0- end-- count.times.map do |index|- entries = inputs.map { |input| input[index] || { "json" => {} } }- merge_entries(entries, config, preferred_index:)- end- end-- def combine_all(inputs, config)- input_1, input_2 = inputs- return [] if input_1.blank? || input_2.blank?-- preferred_index = preferred_input_index(config, inputs.length)- input_1.flat_map do |entry_1|- input_2.map { |entry_2| merge_entries([entry_1, entry_2], config, preferred_index:) }- end- end-- def combine_by_fields(inputs, config, fields_to_match)- input_1, input_2 = inputs- join_mode = config.fetch("join_mode") { "keep_matches" }- output_data_from = config.fetch("output_data_from") { "both" }- fields = match_fields(fields_to_match)- validate_match_fields!(fields)- if input_1.blank? || input_2.blank?- return combine_by_fields_with_empty_input(input_1, input_2, join_mode, output_data_from)- end-- matches = find_matches(input_1, input_2, fields, config)-- case join_mode- when "keep_non_matches"- non_matches(matches, output_data_from)- when "keep_everything"- matched_output(matches, output_data_from, config) + matches[:unmatched_1] +- matches[:unmatched_2]- when "enrich_input_1"- merge_matched(matches[:matched], config) + matches[:unmatched_1]- when "enrich_input_2"- merge_matched(matches[:matched], config, preferred_index: 1) + matches[:unmatched_2]- else- matched_output(matches, output_data_from, config)- end- end-- def combine_by_fields_with_empty_input(input_1, input_2, join_mode, output_data_from)- case join_mode- when "keep_non_matches"- case output_data_from- when "input_1"- input_1- when "input_2"- input_2- else- input_1 + input_2- end- when "keep_everything"- input_1 + input_2- when "enrich_input_1"- input_1.presence || []- when "enrich_input_2"- input_2.presence || []- else- []- end- end-- def match_fields(fields_to_match)- fields_to_match.map.with_index do |field, index|- field_1 = field["field_1"].presence- field_2 = field["field_2"].presence || field_1- if field_1.blank? || field_2.blank?- raise_node_error!(- I18n.t("discourse_workflows.errors.merge.invalid_fields_to_match"),- description:- I18n.t(- "discourse_workflows.errors.merge.fields_to_match_pair_missing_fields",- index: index + 1,- ),- )- end-- [field_1, field_2]- end- end-- def validate_match_fields!(fields)- if fields.blank?- raise_node_error!(- I18n.t("discourse_workflows.errors.merge.missing_fields_to_match"),- description: I18n.t("discourse_workflows.errors.merge.fields_to_match_required"),- )- end- end-- def find_matches(input_1, input_2, fields, config)- matched_input_2_indexes = Set.new- result = { matched: [], matched_2: [], unmatched_1: [], unmatched_2: [] }-- input_1.each do |entry_1|- matches =- input_2.each_with_index.filter_map do |entry_2, index|- next unless fields_match?(entry_1, entry_2, fields, config)-- matched_input_2_indexes << index- entry_2- end- matches = matches.first(1) if config.fetch("multiple_matches") { "all" } == "first"-- if matches.any?- result[:matched] << [entry_1, matches]- else- result[:unmatched_1] << entry_1- end- end-- input_2.each_with_index do |entry, index|- if matched_input_2_indexes.include?(index)- result[:matched_2] << entry- else- result[:unmatched_2] << entry- end- end-- result- end-- def fields_match?(entry_1, entry_2, fields, config)- fields.all? do |field_1, field_2|- present_1, value_1 = field_lookup(entry_1, field_1)- present_2, value_2 = field_lookup(entry_2, field_2)- next false if !present_1 || !present_2-- compare_values(value_1, value_2, fuzzy: config["fuzzy_compare"])- end- end-- def compare_values(value_1, value_2, fuzzy:)- fuzzy ? value_1.to_s == value_2.to_s : value_1 == value_2- end-- def matched_output(matches, output_data_from, config)- case output_data_from- when "input_1"- matches[:matched].map(&:first)- when "input_2"- matches[:matched_2]- else- merge_matched(matches[:matched], config)- end- end-- def non_matches(matches, output_data_from)- case output_data_from- when "input_1"- matches[:unmatched_1]- when "input_2"- matches[:unmatched_2]- else- matches[:unmatched_1].map { |item| add_source(item, "input_1") } +- matches[:unmatched_2].map { |item| add_source(item, "input_2") }- end- end-- def merge_matched(matched, config, preferred_index: nil)- preferred_index ||= preferred_input_index(config, 2)- matched.flat_map do |entry_1, matches|- matches.map { |entry_2| merge_entries([entry_1, entry_2], config, preferred_index:) }- end- end-- def merge_entries(entries, config, preferred_index:)- paired_items = paired_items(entries)- entries = suffix_entries(entries) if config["resolve_clash"] == "add_suffix"- ordered_entries = entries.dup- preferred = ordered_entries.delete_at(preferred_index) || { "json" => {} }- ordered_entries << preferred-- wrap(- merge_hashes(ordered_entries.map { |entry| entry.fetch("json") { {} } }, config),- ).merge("pairedItem" => paired_items)- end-- def merge_hashes(hashes, config)- hashes.each_with_object({}) do |hash, result|- if config.fetch("merge_mode") { "deep_merge" } == "shallow_merge"- merge_shallow!(result, hash, config)- else- merge_deep!(result, hash, config)- end- end- end-- def merge_shallow!(target, source, config)- source.each { |key, value| target[key] = merged_value(target[key], value, config) }- target- end-- def merge_deep!(target, source, config)- source.each do |key, value|- target[key] = if target[key].is_a?(Hash) && value.is_a?(Hash)- merge_deep!(target[key].dup, value, config)- else- merged_value(target[key], value, config)- end- end- target- end-- def merged_value(existing, incoming, config)- return existing if config["override_empty"] && blank_merge_value?(incoming)-- incoming- end-- def blank_merge_value?(value)- value.nil? || value == ""- end-- def suffix_entries(entries)- entries.map.with_index do |entry, index|- json =- entry- .fetch("json") { {} }- .each_with_object({}) do |(key, value), result|- result["#{key}_#{index + 1}"] = value- end- entry.merge("json" => json)- end- end-- def add_source(item, source)- wrap(item.fetch("json") { {} }.merge("_source" => source)).merge(- "pairedItem" => paired_item_for_entry(item, input_index(source)),- ).compact- end-- def paired_items(entries)- entries.each_with_index.filter_map do |entry, input_index|- paired_item_for_entry(entry, input_index)- end- end-- def paired_item_for_entry(entry, input_index)- item_index = @input_item_indexes&.dig(input_index, entry.object_id)- return unless item_index-- { "input" => input_index, "item" => item_index }- end-- def item_indexes_by_input(inputs)- inputs- .each_with_index- .each_with_object({}) do |(items, input_index), indexes|- indexes[input_index] = items.each_with_index.to_h do |item, item_index|- [item.object_id, item_index]- end- end- end-- def preferred_input_index(config, input_count)- case config["resolve_clash"]- when "prefer_input_1"- 0- when "prefer_input_2"- 1- else- input_count - 1- end- end-- def input_index(input_name)- input_name.to_s == "input_2" ? 1 : 0- end-- def selected_input_index(config)- config["use_data_of_input_index"] ||- input_index(config.fetch("use_data_of_input") { "input_1" })- end-- def field_lookup(item, field)- value = item.fetch("json") { {} }- field- .to_s- .split(".")- .each do |key|- return false, nil unless value.is_a?(Hash) && value.key?(key)-- value = value[key]- end-- [true, value]+ exec_ctx.inputs.flatten(1)endendenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_item_linking_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_item_linking_spec.rbindex 813dc303258..878433fdfb3 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_item_linking_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_item_linking_spec.rb@@ -138,7 +138,7 @@ RSpec.describe DiscourseWorkflows::Executor doexpect(context_output(execution, "Set").first.dig("json", "linked_label")).to eq("second")end- it "does not resolve ambiguous previous-node .item after a merge" do+ it "resolves previous-node .item through append merge lineage" doexecution =run_workflow do |g|g.node "trigger-1", "trigger:manual"@@ -184,13 +184,7 @@ RSpec.describe DiscourseWorkflows::Executor do],"combinator" => "and",}- g.node "merge",- "flow:merge",- name: "Merge",- configuration: {- "mode" => "combine",- "combine_by" => "all",- }+ g.node "merge", "flow:merge", name: "Merge"g.node "set","action:set_fields",configuration: {@@ -214,10 +208,12 @@ RSpec.describe DiscourseWorkflows::Executor dog.chain "merge", "set"end- expect(execution.status).to eq("error")- expect(execution.error).to include("Multiple matching items for expression")- expect(context_output(execution, "Merge").first["pairedItem"]).to eq(- [{ "input" => 0, "item" => 0 }, { "input" => 1, "item" => 0 }],+ expect(execution.status).to eq("success")+ expect(context_output(execution, "Set").map { |item| item.dig("json", "linked_label") }).to eq(+ %w[first second],+ )+ expect(context_output(execution, "Merge").map { |item| item["pairedItem"] }).to eq(+ [{ "item" => 0 }, { "input" => 1, "item" => 0 }],)endenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_loop_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_loop_spec.rbindex 83bc2bffbd4..2d2c8b70827 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_loop_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_loop_spec.rb@@ -110,16 +110,7 @@ RSpec.describe DiscourseWorkflows::Executor do"include_other_fields" => false,"json_output" => '{"topic_id": 1, "right": "yes"}',}- g.node "merge-1",- "flow:merge",- name: "Merge branches",- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => {- "values" => [{ "field_1" => "id", "field_2" => "topic_id" }],- },- }+ g.node "merge-1", "flow:merge", name: "Merge branches"g.connect "trigger-1", "left-1"g.connect "trigger-1", "right-1"g.connect "left-1", "merge-1", input: "input_1"@@ -134,11 +125,9 @@ RSpec.describe DiscourseWorkflows::Executor domerge_steps =execution.execution_data.steps_array.select { |step| step["node_id"] == "merge-1" }expect(merge_steps.length).to eq(1)- expect(merge_steps.first["output"].first["json"]).to include(- "id" => 1,- "left" => "yes",- "topic_id" => 1,- "right" => "yes",+ expect(merge_steps.first["output"].map { |item| item["json"] }).to contain_exactly(+ include("id" => 1, "left" => "yes"),+ include("topic_id" => 1, "right" => "yes"),)end@@ -167,12 +156,7 @@ RSpec.describe DiscourseWorkflows::Executor do"include_other_fields" => false,"json_output" => '{"source": "third"}',}- g.node "merge-1",- "flow:merge",- name: "Append branches",- configuration: {- "mode" => "append",- }+ g.node "merge-1", "flow:merge", name: "Append branches"g.connect "trigger-1", "first-1"g.connect "trigger-1", "second-1"g.connect "trigger-1", "third-1"@@ -214,13 +198,7 @@ RSpec.describe DiscourseWorkflows::Executor do"include_other_fields" => false,"json_output" => '{"source": "second"}',}- g.node "merge-1",- "flow:merge",- name: "Append branches",- configuration: {- "mode" => "append",- "number_inputs" => 3,- }+ g.node "merge-1", "flow:merge", name: "Append branches"g.connect "trigger-1", "first-1"g.connect "trigger-1", "second-1"g.connect "first-1", "merge-1", input: "input_1"@@ -282,12 +260,7 @@ RSpec.describe DiscourseWorkflows::Executor do"include_other_fields" => true,"json_output" => '{"source": "right"}',}- g.node "merge-1",- "flow:merge",- name: "Append branches",- configuration: {- "mode" => "append",- }+ g.node "merge-1", "flow:merge", name: "Append branches"g.chain "trigger-1", "source-1", "branch-1"g.connect "branch-1", "left-1", output: "true"g.connect "branch-1", "right-1", output: "false"@@ -331,18 +304,8 @@ RSpec.describe DiscourseWorkflows::Executor do"include_other_fields" => false,"json_output" => '{"source": "never"}',}- g.node "merge-a",- "flow:merge",- name: "Parent merge",- configuration: {- "mode" => "append",- }- g.node "merge-b",- "flow:merge",- name: "Downstream merge",- configuration: {- "mode" => "append",- }+ g.node "merge-a", "flow:merge", name: "Parent merge"+ g.node "merge-b", "flow:merge", name: "Downstream merge"g.connect "trigger-1", "upstream-1"g.connect "trigger-1", "direct-1"g.connect "direct-1", "merge-b", input: "input_1"@@ -363,70 +326,5 @@ RSpec.describe DiscourseWorkflows::Executor do%w[direct upstream],)end-- it "does not run choose branch merge with a missing connected branch" do- graph =- build_workflow_graph do |g|- g.node "trigger-1", "trigger:manual"- g.node "source-1",- "action:set_fields",- configuration: {- "mode" => "raw",- "include_other_fields" => false,- "json_output" => '{"route": "left"}',- }- g.node "branch-1",- "condition:if",- configuration: {- "conditions" => [- {- "id" => "1",- "leftValue" => "={{ $json.route }}",- "rightValue" => "left",- "operator" => {- "type" => "string",- "operation" => "equals",- },- },- ],- "combinator" => "and",- }- g.node "left-1",- "action:set_fields",- configuration: {- "mode" => "raw",- "include_other_fields" => true,- "json_output" => '{"source": "left"}',- }- g.node "right-1",- "action:set_fields",- configuration: {- "mode" => "raw",- "include_other_fields" => true,- "json_output" => '{"source": "right"}',- }- g.node "merge-1",- "flow:merge",- name: "Choose branch",- configuration: {- "mode" => "choose_branch",- "use_data_of_input" => "input_1",- }- g.chain "trigger-1", "source-1", "branch-1"- g.connect "branch-1", "left-1", output: "true"- g.connect "branch-1", "right-1", output: "false"- g.connect "left-1", "merge-1", input: "input_1"- g.connect "right-1", "merge-1", input: "input_2"- end- workflow =- Fabricate(:discourse_workflows_workflow, created_by: user, published: true, **graph)-- execution = described_class.new(workflow, "trigger-1", {}).run-- expect(execution.status).to eq("success")- merge_steps =- execution.execution_data.steps_array.select { |step| step["node_id"] == "merge-1" }- expect(merge_steps).to be_empty- endendenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_wait_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_wait_spec.rbindex 515600a5dd4..5fe75bb0aba 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_wait_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_wait_spec.rb@@ -210,6 +210,136 @@ RSpec.describe DiscourseWorkflows::Executor doexpect(after_output.first["json"]).to include("approved" => true, "done" => "true")end+ it "preserves pending merge inputs across a wait resume" do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:manual"+ g.node "list-users",+ "action:set_fields",+ configuration: {+ "mode" => "raw",+ "include_other_fields" => false,+ "json_output" => '{"source": "users"}',+ }+ g.node "wait-1", "flow:wait", configuration: { "resume" => "webhook" }+ g.node "list-groups",+ "action:set_fields",+ configuration: {+ "mode" => "raw",+ "include_other_fields" => false,+ "json_output" => '{"source": "groups"}',+ }+ g.node "merge-1", "flow:merge", name: "Merge branches"+ g.connect "trigger-1", "list-users"+ g.connect "trigger-1", "wait-1"+ g.connect "wait-1", "list-groups"+ g.connect "list-users", "merge-1", input: "input_1"+ g.connect "list-groups", "merge-1", input: "input_2"+ end+ workflow =+ Fabricate(:discourse_workflows_workflow, created_by: user, published: true, **graph)++ execution = described_class.new(workflow, "trigger-1", {}).run+ expect(execution.status).to eq("waiting")++ claimed = DiscourseWorkflows::Execution.claim_for_resume(execution.reload)+ resumed = DiscourseWorkflows::Executor.resume(claimed, [{ "json" => { "resumed" => true } }])++ expect(resumed.status).to eq("success")+ merge_run = resumed.execution_data.run_data["Merge branches"].first+ expect(merge_run["inputs"].map { |input| input["item_count"] }).to eq([1, 1])++ merge_output = resumed.execution_data.context_data["Merge branches"]+ expect(merge_output.map { |item| item["json"]["source"] }).to contain_exactly(+ "users",+ "groups",+ )+ end++ it "preserves queued sibling branches across a wait resume" do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:manual"+ g.node "wait-1", "flow:wait", configuration: { "resume" => "webhook" }+ g.node "list-users",+ "action:set_fields",+ configuration: {+ "mode" => "raw",+ "include_other_fields" => false,+ "json_output" => '{"source": "users"}',+ }+ g.node "list-groups",+ "action:set_fields",+ configuration: {+ "mode" => "raw",+ "include_other_fields" => false,+ "json_output" => '{"source": "groups"}',+ }+ g.node "merge-1", "flow:merge", name: "Merge branches"+ g.connect "trigger-1", "wait-1"+ g.connect "trigger-1", "list-users"+ g.connect "wait-1", "list-groups"+ g.connect "list-users", "merge-1", input: "input_1"+ g.connect "list-groups", "merge-1", input: "input_2"+ end+ workflow =+ Fabricate(:discourse_workflows_workflow, created_by: user, published: true, **graph)++ execution = described_class.new(workflow, "trigger-1", {}).run+ expect(execution.status).to eq("waiting")++ claimed = DiscourseWorkflows::Execution.claim_for_resume(execution.reload)+ resumed = DiscourseWorkflows::Executor.resume(claimed, [{ "json" => { "resumed" => true } }])++ merge_output = resumed.execution_data.context_data["Merge branches"]+ expect(merge_output.map { |item| item["json"]["source"] }).to contain_exactly(+ "users",+ "groups",+ )+ end++ it "preserves queued merge nodes across a wait resume" do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:manual"+ g.node "list-users",+ "action:set_fields",+ configuration: {+ "mode" => "raw",+ "include_other_fields" => false,+ "json_output" => '{"source": "users"}',+ }+ g.node "list-groups",+ "action:set_fields",+ configuration: {+ "mode" => "raw",+ "include_other_fields" => false,+ "json_output" => '{"source": "groups"}',+ }+ g.node "wait-1", "flow:wait", configuration: { "resume" => "webhook" }+ g.node "merge-1", "flow:merge", name: "Merge branches"+ g.connect "trigger-1", "list-users"+ g.connect "trigger-1", "list-groups"+ g.connect "trigger-1", "wait-1"+ g.connect "list-users", "merge-1", input: "input_1"+ g.connect "list-groups", "merge-1", input: "input_2"+ end+ workflow =+ Fabricate(:discourse_workflows_workflow, created_by: user, published: true, **graph)++ execution = described_class.new(workflow, "trigger-1", {}).run+ expect(execution.status).to eq("waiting")++ claimed = DiscourseWorkflows::Execution.claim_for_resume(execution.reload)+ resumed = DiscourseWorkflows::Executor.resume(claimed, [{ "json" => { "resumed" => true } }])++ merge_output = resumed.execution_data.context_data["Merge branches"]+ expect(merge_output.map { |item| item["json"]["source"] }).to contain_exactly(+ "users",+ "groups",+ )+ end+it "raises when the execution has not been claimed for resume" doresponse_items = [{ "json" => { "approved" => true } }]diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/merge_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/merge_spec.rbindex abd88eb63ad..0314e6475db 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/merge_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/merge_spec.rb@@ -5,15 +5,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Merge::V1 do{ "json" => json }end- def json_items(items)- items.map { |entry| { "json" => entry["json"] } }- end-- def fields_to_match(*fields)- { "values" => fields }- end-- def execute_merge(configuration:, input_1:, input_2:, input_groups: {})+ def execute_merge(input_1:, input_2:, input_groups: {}, configuration: {})execute_node_output(configuration: configuration,input_items: input_1,@@ -22,21 +14,15 @@ RSpec.describe DiscourseWorkflows::Nodes::Merge::V1 doendit "uses imported input wait semantics" do- expect(described_class.input_ports("mode" => "append", "number_inputs" => 3)).to all(- include(required: false),+ expect(described_class.input_ports).to contain_exactly(+ include(key: "main", required: false, multiple: true),)- expect(described_class.input_ports("mode" => "combine")).to all(include(required: false))- expect(described_class.required_inputs("mode" => "append")).to eq(1)- expect(described_class.required_inputs("mode" => "combine")).to eq(1)- expect(described_class.required_inputs("mode" => "choose_branch")).to eq([0, 1])+ expect(described_class.required_inputs).to eq(1)end- it "appends items from both inputs" do+ it "appends items from internal input groups" dooutput =execute_merge(- configuration: {- "mode" => "append",- },input_1: [item("a" => 1)],input_2: [item("b" => 2)],input_groups: {@@ -47,13 +33,9 @@ RSpec.describe DiscourseWorkflows::Nodes::Merge::V1 doexpect(output).to eq([item("a" => 1), item("b" => 2)])end- it "appends items from indexed append inputs" do+ it "appends items from more than two internal input groups" dooutput =execute_merge(- configuration: {- "mode" => "append",- "number_inputs" => 3,- },input_1: [item("a" => 1)],input_2: [item("b" => 2)],input_groups: {@@ -63,157 +45,4 @@ RSpec.describe DiscourseWorkflows::Nodes::Merge::V1 doexpect(output).to eq([item("a" => 1), item("b" => 2), item("c" => 3)])end-- it "chooses a specific input branch" do- output =- execute_merge(- configuration: {- "mode" => "choose_branch",- "use_data_of_input" => "input_2",- },- input_1: [item("a" => 1)],- input_2: [item("b" => 2)],- )-- expect(output).to eq([item("b" => 2)])- end-- it "combines items by position" do- output =- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "position",- },- input_1: [item("id" => 1, "a" => "A")],- input_2: [item("b" => "B")],- )-- expect(json_items(output)).to eq([item("id" => 1, "a" => "A", "b" => "B")])- end-- it "combines all possible item pairs" do- output =- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "all",- },- input_1: [item("a" => 1), item("a" => 2)],- input_2: [item("b" => 3), item("b" => 4)],- )-- expect(json_items(output)).to eq(- [- item("a" => 1, "b" => 3),- item("a" => 1, "b" => 4),- item("a" => 2, "b" => 3),- item("a" => 2, "b" => 4),- ],- )- end-- it "combines matching items by fields" do- output =- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => fields_to_match({ "field_1" => "id", "field_2" => "topic_id" }),- },- input_1: [item("id" => 1, "title" => "One"), item("id" => 2, "title" => "Two")],- input_2: [item("topic_id" => 1, "tag" => "bug")],- )-- expect(json_items(output)).to eq(- [item("id" => 1, "title" => "One", "topic_id" => 1, "tag" => "bug")],- )- expect(output.first["pairedItem"]).to eq(- [{ "input" => 0, "item" => 0 }, { "input" => 1, "item" => 0 }],- )- end-- it "keeps everything from the available input when matching fields has an empty input" do- output =- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => fields_to_match({ "field_1" => "id", "field_2" => "topic_id" }),- "join_mode" => "keep_everything",- },- input_1: [item("id" => 1, "title" => "One")],- input_2: [],- )-- expect(output).to eq([item("id" => 1, "title" => "One")])- end-- it "honors the requested non-match input when matching fields has an empty input" do- output =- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => fields_to_match({ "field_1" => "id", "field_2" => "topic_id" }),- "join_mode" => "keep_non_matches",- "output_data_from" => "input_2",- },- input_1: [item("id" => 1, "title" => "One")],- input_2: [],- )-- expect(output).to eq([])- end-- it "treats missing match fields as non-matches" do- output =- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => fields_to_match({ "field_1" => "id", "field_2" => "topic_id" }),- "join_mode" => "keep_non_matches",- "output_data_from" => "input_1",- },- input_1: [item("id" => 1), item("title" => "Missing id")],- input_2: [item("topic_id" => 1)],- )-- expect(output).to eq([item("title" => "Missing id")])- end-- it "raises node errors when no matching fields are configured" do- expect {- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => fields_to_match,- },- input_1: [item("id" => 1)],- input_2: [item("id" => 1)],- )- }.to raise_error(- DiscourseWorkflows::NodeError,- 'Missing fields to match: You need to define at least one pair of fields in "Fields to match"',- )- end-- it "raises node errors when a matching field pair is incomplete" do- expect {- execute_merge(- configuration: {- "mode" => "combine",- "combine_by" => "matching_fields",- "fields_to_match" => fields_to_match({ "field_1" => "", "field_2" => "topic_id" }),- },- input_1: [item("id" => 1)],- input_2: [item("topic_id" => 1)],- )- }.to raise_error(- DiscourseWorkflows::NodeError,- "Invalid fields to match: Fields to match pair 1 must define both input fields",- )- endenddiff --git a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/canvas-bridge-test.js b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/canvas-bridge-test.jsindex d370aefa0e4..d36c5b670b3 100644--- a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/canvas-bridge-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/canvas-bridge-test.js@@ -21,11 +21,23 @@ const NODE_TYPES = [},{identifier: "flow:loop_over_items",+ inputs: [{ key: "main", multiple: true }],ports: [{ key: "done", primary: true },{ key: "loop", primary: false },],},+ {+ identifier: "flow:merge",+ inputs: [+ {+ key: "main",+ required: false,+ multiple: true,+ },+ ],+ ports: [{ key: "main", primary: true }],+ },];function noopCallbacks(overrides = {}) {@@ -298,13 +310,21 @@ module("Unit | Canvas Bridge", function (hooks) {const bridge = await createReteEditor(container, {callbacks: noopCallbacks({- onConnectionCreated(source, sourceOutput, target, targetInput, index) {+ onConnectionCreated(+ source,+ sourceOutput,+ target,+ targetInput,+ sourceOutputIndex,+ targetInputIndex+ ) {createdConnection = {source,sourceOutput,target,targetInput,- index,+ sourceOutputIndex,+ targetInputIndex,};},}),@@ -334,9 +354,85 @@ module("Unit | Canvas Bridge", function (hooks) {sourceOutput: "no_results",target: "empty",targetInput: "main",- index: 1,+ sourceOutputIndex: 1,+ targetInputIndex: 0,+ });++ bridge.destroy();+ });++ test("connectioncreated reports the next merge input index", async function (assert) {+ let createdConnection;++ const bridge = await createReteEditor(container, {+ callbacks: noopCallbacks({+ onConnectionCreated(+ source,+ sourceOutput,+ target,+ targetInput,+ sourceOutputIndex,+ targetInputIndex+ ) {+ createdConnection = {+ source,+ sourceOutput,+ target,+ targetInput,+ sourceOutputIndex,+ targetInputIndex,+ };+ },+ }),+ nodeTypes: NODE_TYPES,+ });++ await bridge.syncState(+ [+ {+ clientId: "users",+ type: "action:http_request",+ position: { x: 0, y: 0 },+ },+ {+ clientId: "groups",+ type: "action:http_request",+ position: { x: 0, y: 200 },+ },+ {+ clientId: "merge",+ type: "flow:merge",+ position: { x: 200, y: 100 },+ },+ ],+ [+ {+ sourceClientId: "users",+ targetClientId: "merge",+ targetInput: "main",+ targetInputIndex: 0,+ },+ ]+ );++ await bridge.addConnection("groups", "main", "merge");++ assert.deepEqual(createdConnection, {+ source: "groups",+ sourceOutput: "main",+ target: "merge",+ targetInput: "main",+ sourceOutputIndex: 0,+ targetInputIndex: 1,});+ assert.strictEqual(+ bridge.editor.getConnections().find((connection) => {+ return connection.source === "groups" && connection.target === "merge";+ }).targetInputIndex,+ 1+ );+bridge.destroy();});diff --git a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/rete-graph-index-test.js b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/rete-graph-index-test.jsindex c7bfafa3c22..a2abfe486c7 100644--- a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/rete-graph-index-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/canvas/rete-graph-index-test.js@@ -87,6 +87,14 @@ module("Unit | Utility | workflows rete graph index", function () {targetInput: "main",targetInputIndex: 0,};+ const storedSecondMergeInputConnection = {+ sourceClientId: "action-1",+ sourceOutput: "main",+ sourceOutputIndex: 0,+ targetClientId: "merge-1",+ targetInput: "main",+ targetInputIndex: 1,+ };assert.true(connectionMatchesEndpoint(storedTrueBranchConnection, {@@ -112,5 +120,14 @@ module("Unit | Utility | workflows rete graph index", function () {targetInput: "main",}));+ assert.true(+ connectionMatchesEndpoint(storedSecondMergeInputConnection, {+ sourceClientId: "action-1",+ sourceOutput: "main",+ targetClientId: "merge-1",+ targetInput: "main",+ targetInputIndex: 1,+ })+ );});});diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-graph-utils-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-graph-utils-test.jsindex d27fd846510..4950f7c0e21 100644--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-graph-utils-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-graph-utils-test.js@@ -1,7 +1,7 @@import { module, test } from "qunit";import {normalizeConnectionsForNodes,- normalizeMergeConfiguration,+ normalizeNodeConfiguration,removeNodesFromGraph,} from "discourse/plugins/discourse-workflows/admin/components/workflows/editor/graph-utils";@@ -369,37 +369,56 @@ module("Unit | Utility | workflows graph utils", function () {});});- test("normalizes merge target inputs for the selected mode", function (assert) {+ test("normalizes indexed target inputs as hidden indexes", function (assert) {+ const indexedInputNodeType = {+ identifier: "flow:indexed_input",+ inputs: [+ {+ key: "main",+ multiple: true,+ },+ ],+ };+ const nodes = [+ {+ clientId: "indexed",+ type: "flow:indexed_input",+ },+ ];+ const nodeTypeForNode = () => indexedInputNodeType;const connections = [{sourceClientId: "a",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInput: "main",},{sourceClientId: "b",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInput: "main",},{sourceClientId: "c",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInput: "main",},];assert.deepEqual(- normalizeConnectionsForNodes(connections, [- {- clientId: "merge",- type: "flow:merge",- configuration: { mode: "combine" },- },- ]).map((connection) => connection.targetInput),- ["input_1", "input_2"]+ normalizeConnectionsForNodes(connections, nodes, nodeTypeForNode).map(+ (connection) => ({+ targetInput: connection.targetInput,+ targetInputIndex: connection.targetInputIndex,+ })+ ),+ [+ { targetInput: "main", targetInputIndex: 0 },+ { targetInput: "main", targetInputIndex: 1 },+ { targetInput: "main", targetInputIndex: 2 },+ ]);assert.deepEqual(@@ -408,25 +427,26 @@ module("Unit | Utility | workflows graph utils", function () {{sourceClientId: "a",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInput: "main",},{sourceClientId: "b",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInput: "main",},],- [- {- clientId: "merge",- type: "flow:merge",- configuration: { mode: "append" },- },- ]- ).map((connection) => connection.targetInput),- ["input_1", "input_2"]+ nodes,+ nodeTypeForNode+ ).map((connection) => ({+ targetInput: connection.targetInput,+ targetInputIndex: connection.targetInputIndex,+ })),+ [+ { targetInput: "main", targetInputIndex: 0 },+ { targetInput: "main", targetInputIndex: 1 },+ ]);assert.deepEqual(normalizeConnectionsForNodes(@@ -434,50 +454,78 @@ module("Unit | Utility | workflows graph utils", function () {{sourceClientId: "a",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInputIndex: 1,},{sourceClientId: "b",sourceOutput: "main",- targetClientId: "merge",+ targetClientId: "indexed",targetInput: "main",},],- [- {- clientId: "merge",- type: "flow:merge",- configuration: { mode: "append" },- },- ]- ).map((connection) => connection.targetInputIndex),- [1, 0]+ nodes,+ nodeTypeForNode+ ).map((connection) => ({+ targetInput: connection.targetInput,+ targetInputIndex: connection.targetInputIndex,+ })),+ [+ { targetInput: "main", targetInputIndex: 1 },+ { targetInput: "main", targetInputIndex: 0 },+ ]);});- test("normalizes merge configuration to the selected mode", function (assert) {+ test("normalizes configuration from node type metadata", function (assert) {assert.deepEqual(- normalizeMergeConfiguration({- mode: "choose_branch",- use_data_of_input: "input_2",- combine_by: "matching_fields",- fields_to_match: [{ field_1: "id", field_2: "id" }],- }),+ normalizeNodeConfiguration(+ {+ type: "flow:no_configuration",+ configuration: {+ mode: "append",+ notes: "Keep this visible on the canvas",+ notesInFlow: true,+ },+ },+ {+ identifier: "flow:no_configuration",+ properties: {},+ credentials: [],+ }+ ),{- mode: "choose_branch",- use_data_of_input: "input_2",- use_data_of_input_index: 1,- choose_output: "specified_input",+ type: "flow:no_configuration",+ configuration: {+ notes: "Keep this visible on the canvas",+ notesInFlow: true,+ },});assert.deepEqual(- normalizeMergeConfiguration({- mode: "append",- use_data_of_input: "input_2",- }),- { mode: "append", number_inputs: 2 }+ normalizeNodeConfiguration(+ {+ type: "action:configured",+ configuration: {+ operation: "list",+ },+ },+ {+ identifier: "action:configured",+ properties: {+ operation: {+ type: "options",+ },+ },+ }+ ),+ {+ type: "action:configured",+ configuration: {+ operation: "list",+ },+ });});});diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-node-types-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-node-types-test.jsindex f061d636909..662e64a41ad 100644--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-node-types-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-node-types-test.js@@ -4,10 +4,12 @@ import pretender, { response } from "discourse/tests/helpers/create-pretender";import ComboBoxField from "discourse/plugins/discourse-workflows/admin/components/workflows/configurators/combo-box";import WorkflowEditorSession from "discourse/plugins/discourse-workflows/admin/lib/workflows/editor-session";import {+ nodeTypeHasConfigurationFields,nodeTypeI18nPrefix,nodeTypeI18nScope,nodeTypeInputLabel,nodeTypeInputs,+ nodeTypeInputUsesConnectionIndexes,nodeTypeOperationLabel,nodeTypeOutputKeys,nodeTypePaletteGroup,@@ -130,42 +132,69 @@ module("Unit | Utility | workflows node types", function () {assert.deepEqual(nodeTypeOutputKeys(nodeType), ["new"]);});- test("uses merge mode configuration for input sockets", function (assert) {+ test("detects configurable node type fields from metadata", function (assert) {+ assert.false(+ nodeTypeHasConfigurationFields({+ identifier: "flow:no_configuration",+ properties: {},+ credentials: [],+ })+ );+ assert.true(+ nodeTypeHasConfigurationFields({+ identifier: "action:configured",+ properties: {+ operation: {+ type: "options",+ },+ },+ })+ );+ assert.true(nodeTypeHasConfigurationFields("action:unknown"));+ });++ test("uses the merge node type input socket", function (assert) {const mergeNodeType = {identifier: "flow:merge",inputs: [- { key: "main" },- { key: "input_1", required: true },- { key: "input_2", required: true },+ {+ key: "main",+ display_name: "Input",+ required: false,+ multiple: true,+ },],};+ const inputs = nodeTypeInputs(mergeNodeType, { configuration: {} });assert.deepEqual(- nodeTypeInputs(mergeNodeType, {- configuration: { mode: "append" },- }).map((input) => input.key),- ["input_1", "input_2"]- );- assert.deepEqual(- nodeTypeInputs(mergeNodeType, {- configuration: { mode: "append", number_inputs: 3 },- }).map((input) => input.key),- ["input_1", "input_2", "input_3"]- );- assert.deepEqual(- nodeTypeInputs(mergeNodeType, {- configuration: { mode: "combine" },- }).map((input) => input.key),- ["input_1", "input_2"]+ inputs.map((input) => input.key),+ ["main"]);+ assert.true(inputs[0].multiple);+ assert.true(nodeTypeInputUsesConnectionIndexes(mergeNodeType, "main"));assert.strictEqual(- nodeTypeInputLabel(mergeNodeType, "input_1", {- configuration: { mode: "combine" },+ nodeTypeInputLabel(mergeNodeType, "main", {+ configuration: {},}),- "Input 1"+ "Input");});+ test("does not index loop node connections", function (assert) {+ const loopNodeType = {+ identifier: "flow:loop_over_items",+ inputs: [+ {+ key: "main",+ multiple: true,+ },+ ],+ };++ assert.false(nodeTypeInputUsesConnectionIndexes(loopNodeType, "main"));+ });+test("resolves run scope labels from capabilities", function (assert) {const nodeType = {identifier: "action:code",