feat: implement createWorkflowNodeFromSpec function for node creation and add tests for it

This commit is contained in:
mlogclub
2026-06-22 18:53:46 +08:00
parent d1f39ae57e
commit 7029ab47b7
3 changed files with 201 additions and 68 deletions
@@ -10,6 +10,7 @@ export type WorkflowEditorNode = {
data?: {
nodeType?: string
name?: string
label?: string
config?: Record<string, unknown>
inputs?: Record<string, WorkflowVariableSelector>
}
@@ -273,6 +274,36 @@ export function applyAutoInputMappings(
}
}
export function createWorkflowNodeFromSpec(
spec: WorkflowNodeSpec,
existingNodes: Pick<WorkflowEditorNode, "id">[],
position: WorkflowNodePosition
): WorkflowEditorNode {
const id = uniqueNodeId(existingNodes, spec.type)
return {
id,
type: "workflowNode",
position,
data: {
nodeType: spec.type,
name: spec.title ?? spec.type,
label: spec.title ?? spec.type,
config: {},
inputs: spec.defaultInputs ?? {},
},
}
}
function uniqueNodeId(existingNodes: Pick<WorkflowEditorNode, "id">[], nodeType: string) {
let nextIndex = existingNodes.length + 1
let id = `${nodeType}_${nextIndex}`
while (existingNodes.some((node) => node.id === id)) {
nextIndex += 1
id = `${nodeType}_${nextIndex}`
}
return id
}
function findPreferredOutput(
inputName: string,
inputType: WorkflowVariableType,