Skip to content
Draft
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
86 changes: 86 additions & 0 deletions src/components/shared/ReactFlow/FlowCanvas/FlowCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ import {
import { loadComponentAsRefFromText } from "@/utils/componentStore";
import { deselectAllNodes } from "@/utils/flowUtils";
import createNodesFromComponentSpec from "@/utils/nodes/createNodesFromComponentSpec";
import { extractPositionFromAnnotations } from "@/utils/nodes/extractPositionFromAnnotations";
import {
getSubgraphComponentSpec,
isSubgraph,
updateSubgraphSpec,
} from "@/utils/subgraphUtils";

Expand Down Expand Up @@ -632,6 +634,90 @@ const FlowCanvas = ({
}
}

// Drop a subgraph onto an empty canvas
if (
nodes.length === 0 &&
taskType === "task" &&
droppedTask &&
isSubgraph(droppedTask) &&
droppedTask.componentRef.spec &&
isGraphImplementation(droppedTask.componentRef.spec.implementation)
) {
const dialogDetails = {
title: "Import Subgraph as Pipeline",
description: `Dropping the subgraph "${droppedTask.componentRef.spec.name}" onto an empty canvas will unpack its internal components. Do you want to proceed?`,
};

const confirmed = await triggerConfirmation(dialogDetails);

if (!confirmed) {
return;
}

// Todo: copy over IO values
// Todo: output node connections
// Todo: Move logic into a utility
console.log("Unpacking subgraph onto empty canvas");
console.log("Dropped Task:", droppedTask);

const graphSpec = droppedTask.componentRef.spec?.implementation.graph;

let updatedSubgraphSpec = { ...currentSubgraphSpec };

Object.entries(graphSpec.tasks).forEach(([_, task]) => {
const { spec } = addTask(
"task",
task,
extractPositionFromAnnotations(task.annotations),
updatedSubgraphSpec,
);

updatedSubgraphSpec = spec;
});

droppedTask.componentRef.spec.inputs?.forEach((input) => {
const { spec } = addTask(
"input",
null,
extractPositionFromAnnotations(input.annotations),
updatedSubgraphSpec,
{
name: input.name,
type: input.type,
description: input.description,
},
);

updatedSubgraphSpec = spec;
});

droppedTask.componentRef.spec.outputs?.forEach((output) => {
const { spec } = addTask(
"output",
null,
extractPositionFromAnnotations(output.annotations),
updatedSubgraphSpec,
{
name: output.name,
type: output.type,
description: output.description,
},
);

updatedSubgraphSpec = spec;
});

const newRootSpec = updateSubgraphSpec(
componentSpec,
currentSubgraphPath,
updatedSubgraphSpec,
);

setComponentSpec(newRootSpec);

return;
}

// Replacing an existing node
if (replaceTarget) {
if (!droppedTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface AddTaskResult {
* Options for creating input/output nodes.
* Omits position-related fields (annotations) which are automatically set.
*/
type IONodeOptions = Omit<Partial<InputSpec>, "annotations">;
type IONodeOptions = Omit<Partial<InputSpec | OutputSpec>, "annotations">;

/**
* Creates a task, input, or output node and adds it to the component specification.
Expand Down