Skip to content

Commit fa03d4d

Browse files
fix(copilot): canonical modes should be constructed on edit (#2989)
* fix(copilot): canonical modes should be constructed * reuse canonicalIndex * fix insert into subflow case:
1 parent e14cebe commit fa03d4d

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

apps/sim/lib/copilot/tools/server/workflow/edit-workflow.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { extractAndPersistCustomTools } from '@/lib/workflows/persistence/custom
1111
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
1212
import { isValidKey } from '@/lib/workflows/sanitization/key-validation'
1313
import { validateWorkflowState } from '@/lib/workflows/sanitization/validation'
14+
import { buildCanonicalIndex, isCanonicalPair } from '@/lib/workflows/subblocks/visibility'
1415
import { TriggerUtils } from '@/lib/workflows/triggers/triggers'
1516
import { getAllBlocks, getBlock } from '@/blocks/registry'
16-
import type { SubBlockConfig } from '@/blocks/types'
17+
import type { BlockConfig, SubBlockConfig } from '@/blocks/types'
1718
import { EDGE, normalizeName, RESERVED_BLOCK_NAMES } from '@/executor/constants'
1819
import { getUserPermissionConfig } from '@/executor/utils/permission-check'
1920
import { generateLoopBlocks, generateParallelBlocks } from '@/stores/workflows/workflow/utils'
@@ -667,11 +668,47 @@ function createBlockFromParams(
667668
}
668669
}
669670
})
671+
672+
if (validatedInputs) {
673+
updateCanonicalModesForInputs(blockState, Object.keys(validatedInputs), blockConfig)
674+
}
670675
}
671676

672677
return blockState
673678
}
674679

680+
function updateCanonicalModesForInputs(
681+
block: { data?: { canonicalModes?: Record<string, 'basic' | 'advanced'> } },
682+
inputKeys: string[],
683+
blockConfig: BlockConfig
684+
): void {
685+
if (!blockConfig.subBlocks?.length) return
686+
687+
const canonicalIndex = buildCanonicalIndex(blockConfig.subBlocks)
688+
const canonicalModeUpdates: Record<string, 'basic' | 'advanced'> = {}
689+
690+
for (const inputKey of inputKeys) {
691+
const canonicalId = canonicalIndex.canonicalIdBySubBlockId[inputKey]
692+
if (!canonicalId) continue
693+
694+
const group = canonicalIndex.groupsById[canonicalId]
695+
if (!group || !isCanonicalPair(group)) continue
696+
697+
const isAdvanced = group.advancedIds.includes(inputKey)
698+
const existingMode = canonicalModeUpdates[canonicalId]
699+
700+
if (!existingMode || isAdvanced) {
701+
canonicalModeUpdates[canonicalId] = isAdvanced ? 'advanced' : 'basic'
702+
}
703+
}
704+
705+
if (Object.keys(canonicalModeUpdates).length > 0) {
706+
if (!block.data) block.data = {}
707+
if (!block.data.canonicalModes) block.data.canonicalModes = {}
708+
Object.assign(block.data.canonicalModes, canonicalModeUpdates)
709+
}
710+
}
711+
675712
/**
676713
* Normalize tools array by adding back fields that were sanitized for training
677714
*/
@@ -1654,6 +1691,15 @@ function applyOperationsToWorkflowState(
16541691
block.data.collection = params.inputs.collection
16551692
}
16561693
}
1694+
1695+
const editBlockConfig = getBlock(block.type)
1696+
if (editBlockConfig) {
1697+
updateCanonicalModesForInputs(
1698+
block,
1699+
Object.keys(validationResult.validInputs),
1700+
editBlockConfig
1701+
)
1702+
}
16571703
}
16581704

16591705
// Update basic properties
@@ -2256,6 +2302,15 @@ function applyOperationsToWorkflowState(
22562302
existingBlock.subBlocks[key].value = sanitizedValue
22572303
}
22582304
})
2305+
2306+
const existingBlockConfig = getBlock(existingBlock.type)
2307+
if (existingBlockConfig) {
2308+
updateCanonicalModesForInputs(
2309+
existingBlock,
2310+
Object.keys(validationResult.validInputs),
2311+
existingBlockConfig
2312+
)
2313+
}
22592314
}
22602315
} else {
22612316
// Special container types (loop, parallel) are not in the block registry but are valid

bun.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)