Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug with loop block children order on save #1154

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function FlowRenderer({
}, [nodesInitialized]);

async function handleSave() {
const blocks = getWorkflowBlocks(nodes);
const blocks = getWorkflowBlocks(nodes, edges);
const parametersInYAMLConvertibleJSON = convertToParametersYAML(parameters);
const filteredParameters = workflow.workflow_definition.parameters.filter(
(parameter) => {
Expand Down
64 changes: 48 additions & 16 deletions skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,48 @@ function getWorkflowBlock(node: WorkflowBlockNode): BlockYAML {
}
}

function getWorkflowBlocksUtil(nodes: Array<AppNode>): Array<BlockYAML> {
function getOrderedChildrenBlocks(
nodes: Array<AppNode>,
edges: Array<Edge>,
parentId: string,
): Array<BlockYAML> {
const parentNode = nodes.find((node) => node.id === parentId);
if (!parentNode) {
return [];
}
const blockStartNode = nodes.find(
(node) => node.type === "start" && node.parentId === parentId,
);
if (!blockStartNode) {
return [];
}
const firstChildId = edges.find(
(edge) => edge.source === blockStartNode.id,
)?.target;
const firstChild = nodes.find((node) => node.id === firstChildId);
if (!firstChild || !isWorkflowBlockNode(firstChild)) {
return [];
}

const children: Array<BlockYAML> = [];
let currentNode: WorkflowBlockNode | undefined = firstChild;
while (currentNode) {
children.push(getWorkflowBlock(currentNode));
const nextId = edges.find(
(edge) => edge.source === currentNode?.id,
)?.target;
const next = nodes.find((node) => node.id === nextId);
currentNode = next && isWorkflowBlockNode(next) ? next : undefined;
}
return children;
}

function getWorkflowBlocksUtil(
nodes: Array<AppNode>,
edges: Array<Edge>,
): Array<BlockYAML> {
return nodes.flatMap((node) => {
if (node.parentId) {
if (node.parentId || node.type === "start" || node.type === "nodeAdder") {
return [];
}
if (node.type === "loop") {
Expand All @@ -647,26 +686,19 @@ function getWorkflowBlocksUtil(nodes: Array<AppNode>): Array<BlockYAML> {
label: node.data.label,
continue_on_failure: node.data.continueOnFailure,
loop_over_parameter_key: node.data.loopValue,
loop_blocks: nodes
.filter((n) => n.parentId === node.id)
.map((n) => {
return getWorkflowBlock(
n as Exclude<AppNode, LoopNode | NodeAdderNode | StartNode>,
);
}),
loop_blocks: getOrderedChildrenBlocks(nodes, edges, node.id),
},
];
}
return [
getWorkflowBlock(
node as Exclude<AppNode, LoopNode | NodeAdderNode | StartNode>,
),
];
return [getWorkflowBlock(node as Exclude<WorkflowBlockNode, LoopNode>)];
});
}

function getWorkflowBlocks(nodes: Array<AppNode>): Array<BlockYAML> {
return getWorkflowBlocksUtil(nodes.filter(isWorkflowBlockNode));
function getWorkflowBlocks(
nodes: Array<AppNode>,
edges: Array<Edge>,
): Array<BlockYAML> {
return getWorkflowBlocksUtil(nodes, edges);
}

function generateNodeLabel(existingLabels: Array<string>) {
Expand Down
Loading