Skip to content

Commit b3fa3f3

Browse files
committed
Fix start block
1 parent 4b37f92 commit b3fa3f3

4 files changed

Lines changed: 162 additions & 4 deletions

File tree

apps/sim/hooks/use-collaborative-workflow.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,41 @@ export function useCollaborativeWorkflow() {
765765
// Create subBlocks from the block configuration
766766
if (blockConfig.subBlocks) {
767767
blockConfig.subBlocks.forEach((subBlock) => {
768+
// Resolve initial value using the same logic as the store
769+
let initialValue: unknown = null
770+
771+
// Handle function-based values
772+
if (typeof subBlock.value === 'function') {
773+
try {
774+
initialValue = subBlock.value({})
775+
} catch (error) {
776+
logger.warn('Failed to resolve dynamic sub-block default value', {
777+
subBlockId: subBlock.id,
778+
error: error instanceof Error ? error.message : String(error),
779+
})
780+
}
781+
} else if (subBlock.defaultValue !== undefined) {
782+
initialValue = subBlock.defaultValue
783+
} else if (subBlock.type === 'input-format') {
784+
// Initialize with a default field structure instead of empty array
785+
initialValue = [
786+
{
787+
id: crypto.randomUUID(),
788+
name: '',
789+
type: 'string',
790+
value: '',
791+
collapsed: false,
792+
},
793+
]
794+
} else if (subBlock.type === 'table') {
795+
// Special handling for table types
796+
initialValue = []
797+
}
798+
768799
subBlocks[subBlock.id] = {
769800
id: subBlock.id,
770801
type: subBlock.type,
771-
value: subBlock.defaultValue ?? null,
802+
value: initialValue,
772803
}
773804
})
774805
}
@@ -821,6 +852,17 @@ export function useCollaborativeWorkflow() {
821852
workflowStore.addEdge(autoConnectEdge)
822853
}
823854

855+
// Explicitly seed the subblock store to ensure values are available
856+
// This ensures the UI can read the initial values immediately
857+
if (activeWorkflowId && blockConfig.subBlocks) {
858+
blockConfig.subBlocks.forEach((subBlock) => {
859+
const subBlockValue = subBlocks[subBlock.id]?.value
860+
if (subBlockValue !== undefined) {
861+
useSubBlockStore.getState().setValue(id, subBlock.id, subBlockValue)
862+
}
863+
})
864+
}
865+
824866
// Record for undo AFTER adding (pass the autoConnectEdge explicitly)
825867
undoRedo.recordAddBlock(id, autoConnectEdge)
826868

apps/sim/lib/workflows/defaults.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,21 @@ function resolveInitialValue(subBlock: SubBlockConfig): unknown {
3737
return cloneDefaultValue(subBlock.defaultValue)
3838
}
3939

40-
// Ensure structured fields are initialized with empty collections by default
41-
if (subBlock.type === 'input-format' || subBlock.type === 'table') {
40+
// Ensure structured fields are initialized properly
41+
if (subBlock.type === 'input-format') {
42+
// Initialize with a default field structure instead of empty array
43+
return [
44+
{
45+
id: crypto.randomUUID(),
46+
name: '',
47+
type: 'string',
48+
value: '',
49+
collapsed: false,
50+
},
51+
]
52+
}
53+
54+
if (subBlock.type === 'table') {
4255
return []
4356
}
4457

apps/sim/lib/workflows/trigger-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ export function getAllTriggerBlocks(): TriggerInfo[] {
153153
* Check if a block has trigger capability (contains trigger mode subblocks)
154154
*/
155155
export function hasTriggerCapability(block: BlockConfig): boolean {
156+
// Pure trigger blocks (category === 'triggers') should NOT have trigger mode enabled
157+
// They ARE triggers, not blocks that can optionally become triggers
158+
if (block.category === 'triggers') {
159+
return false
160+
}
161+
162+
// Only non-trigger blocks that have trigger capability should enable trigger mode
156163
return (
157164
(block.triggers?.enabled === true && block.triggers.available.length > 0) ||
158165
block.subBlocks.some((subBlock) => subBlock.mode === 'trigger')

apps/sim/stores/workflows/workflow/store.ts

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { devtools } from 'zustand/middleware'
44
import { createLogger } from '@/lib/logs/console/logger'
55
import { getBlockOutputs } from '@/lib/workflows/block-outputs'
66
import { getBlock } from '@/blocks'
7+
import type { SubBlockConfig } from '@/blocks/types'
78
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
89
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
910
import {
@@ -21,6 +22,73 @@ import { generateLoopBlocks, generateParallelBlocks } from '@/stores/workflows/w
2122

2223
const logger = createLogger('WorkflowStore')
2324

25+
/**
26+
* Creates a deep clone of an initial sub-block value to avoid shared references.
27+
*
28+
* @param value - The value to clone.
29+
* @returns A cloned value suitable for initializing sub-block state.
30+
*/
31+
function cloneInitialSubblockValue(value: unknown): unknown {
32+
if (Array.isArray(value)) {
33+
return value.map((item) => cloneInitialSubblockValue(item))
34+
}
35+
36+
if (value && typeof value === 'object') {
37+
return Object.entries(value as Record<string, unknown>).reduce<Record<string, unknown>>(
38+
(acc, [key, entry]) => {
39+
acc[key] = cloneInitialSubblockValue(entry)
40+
return acc
41+
},
42+
{}
43+
)
44+
}
45+
46+
return value ?? null
47+
}
48+
49+
/**
50+
* Resolves the initial value for a sub-block based on its configuration.
51+
*
52+
* @param config - The sub-block configuration.
53+
* @returns The resolved initial value or null when no defaults are defined.
54+
*/
55+
function resolveInitialSubblockValue(config: SubBlockConfig): unknown {
56+
if (typeof config.value === 'function') {
57+
try {
58+
const resolved = config.value({})
59+
return cloneInitialSubblockValue(resolved)
60+
} catch (error) {
61+
logger.warn('Failed to resolve dynamic sub-block default value', {
62+
subBlockId: config.id,
63+
error: error instanceof Error ? error.message : String(error),
64+
})
65+
}
66+
}
67+
68+
if (config.defaultValue !== undefined) {
69+
return cloneInitialSubblockValue(config.defaultValue)
70+
}
71+
72+
if (config.type === 'input-format') {
73+
// Initialize with a default field structure instead of empty array
74+
return [
75+
{
76+
id: crypto.randomUUID(),
77+
name: '',
78+
type: 'string',
79+
value: '',
80+
collapsed: false,
81+
},
82+
]
83+
}
84+
85+
if (config.type === 'table') {
86+
return []
87+
}
88+
89+
return null
90+
}
91+
2492
const initialState = {
2593
blocks: {},
2694
edges: [],
@@ -106,12 +174,40 @@ export const useWorkflowStore = create<WorkflowStore>()(
106174
}
107175

108176
const subBlocks: Record<string, SubBlockState> = {}
177+
const subBlockStore = useSubBlockStore.getState()
178+
const activeWorkflowId = useWorkflowRegistry.getState().activeWorkflowId
179+
109180
blockConfig.subBlocks.forEach((subBlock) => {
110181
const subBlockId = subBlock.id
182+
const initialValue = resolveInitialSubblockValue(subBlock)
183+
const normalizedValue =
184+
initialValue !== undefined && initialValue !== null ? initialValue : null
185+
111186
subBlocks[subBlockId] = {
112187
id: subBlockId,
113188
type: subBlock.type,
114-
value: null,
189+
value: normalizedValue as SubBlockState['value'],
190+
}
191+
192+
// Always initialize the subblock store value, even if it's null or empty array
193+
// This ensures the editor panel can read the value correctly
194+
if (activeWorkflowId) {
195+
try {
196+
// Use the initial value if available, otherwise use null
197+
const valueToStore = initialValue !== undefined ? cloneInitialSubblockValue(initialValue) : null
198+
subBlockStore.setValue(id, subBlockId, valueToStore)
199+
} catch (error) {
200+
logger.warn('Failed to seed sub-block store value during block creation', {
201+
blockId: id,
202+
subBlockId,
203+
error: error instanceof Error ? error.message : String(error),
204+
})
205+
}
206+
} else {
207+
logger.warn('Cannot seed sub-block store value: activeWorkflowId not available', {
208+
blockId: id,
209+
subBlockId,
210+
})
115211
}
116212
})
117213

0 commit comments

Comments
 (0)