From d7782ba6c821b13231d40635449e91ca83dc7187 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Tue, 30 Jun 2026 12:36:17 +0800 Subject: [PATCH] feat: implement isConditionBranchEdge function and update deleteConditionBranch logic; add tests for condition branch line matching --- .../workflow-condition-node-content.tsx | 8 ++++++++ .../_components/workflow-utils.test.mjs | 20 +++++++++++++++++++ .../_components/workflow-utils.ts | 10 +++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/web/app/dashboard/ai-workflows/_components/workflow-condition-node-content.tsx b/web/app/dashboard/ai-workflows/_components/workflow-condition-node-content.tsx index e4cb45a..9b4534e 100644 --- a/web/app/dashboard/ai-workflows/_components/workflow-condition-node-content.tsx +++ b/web/app/dashboard/ai-workflows/_components/workflow-condition-node-content.tsx @@ -1,10 +1,12 @@ import { PlusIcon, XIcon } from "lucide-react" +import { useService, WorkflowLinesManager } from "@flowgram.ai/free-layout-editor" import { Button } from "@/components/ui/button" import { cn } from "@/lib/utils" import { useWorkflowBranchSelection } from "./workflow-branch-selection" import { createConditionBranchID, + isConditionBranchEdge, isBranchRowActionTarget, normalizeNodeConfig, type WorkflowConditionBranch, @@ -19,6 +21,7 @@ export function WorkflowConditionNodeContent({ nodeId: string onChange: (value: Record) => void }) { + const linesManager = useService(WorkflowLinesManager) const { selectedBranch, onSelectBranch } = useWorkflowBranchSelection() const config = normalizeNodeConfig(configValue) const branches = ensureConditionBranches(config.branches ?? []) @@ -30,6 +33,11 @@ export function WorkflowConditionNodeContent({ }) } const deleteBranch = (branchId: string) => { + linesManager.getAllLines().forEach((line) => { + if (isConditionBranchEdge(line.toJSON(), nodeId, branchId)) { + line.dispose() + } + }) updateBranches(branches.filter((branch) => branch.id !== branchId)) if (selectedBranch?.nodeId === nodeId && selectedBranch.branchId === branchId) { onSelectBranch?.(null) diff --git a/web/app/dashboard/ai-workflows/_components/workflow-utils.test.mjs b/web/app/dashboard/ai-workflows/_components/workflow-utils.test.mjs index 186acd9..7a1ffc7 100644 --- a/web/app/dashboard/ai-workflows/_components/workflow-utils.test.mjs +++ b/web/app/dashboard/ai-workflows/_components/workflow-utils.test.mjs @@ -381,6 +381,26 @@ describe("workflow definition mutations", () => { assert.deepEqual(plain(deleted.edges.map((edge) => edge.sourcePortID)), ["default"]) }) + it("matches condition branch lines by source node and branch port", async () => { + const { isConditionBranchEdge } = await loadModule() + + assert.equal(isConditionBranchEdge( + workflowEdge("condition_1", "vip_reply", { sourcePortID: "vip" }), + "condition_1", + "vip" + ), true) + assert.equal(isConditionBranchEdge( + workflowEdge("condition_1", "default_reply", { sourcePortID: "default" }), + "condition_1", + "vip" + ), false) + assert.equal(isConditionBranchEdge( + workflowEdge("other_condition", "vip_reply", { sourcePortID: "vip" }), + "condition_1", + "vip" + ), false) + }) + it("adds FlowGram source ports for condition edges without removing existing lines", async () => { const { normalizeConditionPortsForFlowgram } = await loadModule() const definition = { diff --git a/web/app/dashboard/ai-workflows/_components/workflow-utils.ts b/web/app/dashboard/ai-workflows/_components/workflow-utils.ts index 6c83bd5..bffa294 100644 --- a/web/app/dashboard/ai-workflows/_components/workflow-utils.ts +++ b/web/app/dashboard/ai-workflows/_components/workflow-utils.ts @@ -297,10 +297,18 @@ export function deleteConditionBranch( }) return { ...nextDefinition, - edges: nextDefinition.edges.filter((edge) => !(edge.sourceNodeID === nodeId && edge.sourcePortID === branchId)), + edges: nextDefinition.edges.filter((edge) => !isConditionBranchEdge(edge, nodeId, branchId)), } } +export function isConditionBranchEdge( + edge: { sourceNodeID?: unknown; sourcePortID?: unknown }, + nodeId: string, + branchId: string +): boolean { + return String(edge.sourceNodeID ?? "") === nodeId && String(edge.sourcePortID ?? "") === branchId +} + export function normalizeConditionPortsForFlowgram( definition: AIWorkflowDefinition ): AIWorkflowDefinition {