|
7 | 7 | type BaseServerTool, |
8 | 8 | type ServerToolContext, |
9 | 9 | } from '@/lib/copilot/tools/server/base-tool' |
10 | | -import { applyTargetedLayout } from '@/lib/workflows/autolayout' |
| 10 | +import { applyTargetedLayout, getTargetedLayoutImpact } from '@/lib/workflows/autolayout' |
11 | 11 | import { |
12 | 12 | DEFAULT_HORIZONTAL_SPACING, |
13 | 13 | DEFAULT_VERTICAL_SPACING, |
@@ -233,29 +233,18 @@ export const editWorkflowServerTool: BaseServerTool<EditWorkflowParams, unknown> |
233 | 233 | // Persist the workflow state to the database |
234 | 234 | const finalWorkflowState = validation.sanitizedState || modifiedWorkflowState |
235 | 235 |
|
236 | | - // Identify blocks that need layout by comparing against the pre-operation |
237 | | - // state. New blocks and blocks inserted into subflows (position reset to |
238 | | - // 0,0) need repositioning. Extracted blocks are excluded — their handler |
239 | | - // already computed valid absolute positions from the container offset. |
240 | | - const preOperationBlockIds = new Set(Object.keys(workflowState.blocks || {})) |
241 | | - const blocksNeedingLayout = Object.keys(finalWorkflowState.blocks).filter((id) => { |
242 | | - if (!preOperationBlockIds.has(id)) return true |
243 | | - const prevParent = workflowState.blocks[id]?.data?.parentId ?? null |
244 | | - const currParent = finalWorkflowState.blocks[id]?.data?.parentId ?? null |
245 | | - if (prevParent === currParent) return false |
246 | | - // Parent changed — only needs layout if position was reset to (0,0) |
247 | | - // by insert_into_subflow. extract_from_subflow computes absolute |
248 | | - // positions directly, so those blocks don't need repositioning. |
249 | | - const pos = finalWorkflowState.blocks[id]?.position |
250 | | - return pos?.x === 0 && pos?.y === 0 |
| 236 | + const { layoutBlockIds, shiftSourceBlockIds } = getTargetedLayoutImpact({ |
| 237 | + before: workflowState, |
| 238 | + after: finalWorkflowState, |
251 | 239 | }) |
252 | 240 |
|
253 | 241 | let layoutedBlocks = finalWorkflowState.blocks |
254 | 242 |
|
255 | | - if (blocksNeedingLayout.length > 0) { |
| 243 | + if (layoutBlockIds.length > 0 || shiftSourceBlockIds.length > 0) { |
256 | 244 | try { |
257 | 245 | layoutedBlocks = applyTargetedLayout(finalWorkflowState.blocks, finalWorkflowState.edges, { |
258 | | - changedBlockIds: blocksNeedingLayout, |
| 246 | + changedBlockIds: layoutBlockIds, |
| 247 | + shiftSourceBlockIds, |
259 | 248 | horizontalSpacing: DEFAULT_HORIZONTAL_SPACING, |
260 | 249 | verticalSpacing: DEFAULT_VERTICAL_SPACING, |
261 | 250 | }) |
|
0 commit comments