diff --git a/apps/sim/app/api/workflows/[id]/route.ts b/apps/sim/app/api/workflows/[id]/route.ts index 0fe25809a3..1139b2b62a 100644 --- a/apps/sim/app/api/workflows/[id]/route.ts +++ b/apps/sim/app/api/workflows/[id]/route.ts @@ -110,14 +110,14 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{ parallelsCount: Object.keys(normalizedData.parallels).length, loops: normalizedData.loops, }) - + // Debug: Log sample block data from normalized tables const sampleBlockId = Object.keys(normalizedData.blocks)[0] if (sampleBlockId) { logger.debug(`[${requestId}] Sample block from normalized tables:`, { blockId: sampleBlockId, block: normalizedData.blocks[sampleBlockId], - subBlocks: normalizedData.blocks[sampleBlockId]?.subBlocks + subBlocks: normalizedData.blocks[sampleBlockId]?.subBlocks, }) } // Use normalized table data - reconstruct complete state object diff --git a/apps/sim/app/api/workflows/[id]/state/route.ts b/apps/sim/app/api/workflows/[id]/state/route.ts index 508366f8d7..cb992331f7 100644 --- a/apps/sim/app/api/workflows/[id]/state/route.ts +++ b/apps/sim/app/api/workflows/[id]/state/route.ts @@ -85,19 +85,19 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{ return NextResponse.json({ error: 'Access denied' }, { status: 403 }) } - // Save to normalized tables + // Save to normalized tables logger.info(`[${requestId}] Saving workflow ${workflowId} state to normalized tables`) - + // Debug: Log sample block data being received const sampleBlockId = Object.keys(state.blocks)[0] if (sampleBlockId) { logger.debug(`[${requestId}] Sample block data received:`, { blockId: sampleBlockId, block: state.blocks[sampleBlockId], - subBlocks: state.blocks[sampleBlockId]?.subBlocks + subBlocks: state.blocks[sampleBlockId]?.subBlocks, }) } - + // Ensure all required fields are present for WorkflowState type const workflowState = { blocks: state.blocks, @@ -111,7 +111,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{ hasActiveSchedule: state.hasActiveSchedule || false, hasActiveWebhook: state.hasActiveWebhook || false, } - + const saveResult = await saveWorkflowToNormalizedTables(workflowId, workflowState) if (!saveResult.success) { diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/control-bar/components/import-controls/import-controls.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/control-bar/components/import-controls/import-controls.tsx index 307b425633..cacf63881c 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/control-bar/components/import-controls/import-controls.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/control-bar/components/import-controls/import-controls.tsx @@ -117,22 +117,26 @@ export function ImportControls({ disabled = false }: ImportControlsProps) { // Import the YAML into the new workflow BEFORE navigation (creates complete state and saves directly to DB) // This avoids timing issues with workflow reload during navigation logger.info('Importing YAML into new workflow before navigation') - const result = await importWorkflowFromYaml(yamlContent, { - addBlock: collaborativeAddBlock, - addEdge: collaborativeAddEdge, - applyAutoLayout: () => { - // Trigger auto layout - window.dispatchEvent(new CustomEvent('trigger-auto-layout')) + const result = await importWorkflowFromYaml( + yamlContent, + { + addBlock: collaborativeAddBlock, + addEdge: collaborativeAddEdge, + applyAutoLayout: () => { + // Trigger auto layout + window.dispatchEvent(new CustomEvent('trigger-auto-layout')) + }, + setSubBlockValue: (blockId: string, subBlockId: string, value: any) => { + // Use the collaborative function - the same one called when users type into fields + collaborativeSetSubblockValue(blockId, subBlockId, value) + }, + getExistingBlocks: () => { + // For a new workflow, we'll get the starter block from the server + return {} + }, }, - setSubBlockValue: (blockId: string, subBlockId: string, value: any) => { - // Use the collaborative function - the same one called when users type into fields - collaborativeSetSubblockValue(blockId, subBlockId, value) - }, - getExistingBlocks: () => { - // For a new workflow, we'll get the starter block from the server - return {} - }, - }, newWorkflowId) // Pass the new workflow ID to import into + newWorkflowId + ) // Pass the new workflow ID to import into // Navigate to the new workflow AFTER import is complete if (result.success) { diff --git a/apps/sim/lib/workflows/db-helpers.ts b/apps/sim/lib/workflows/db-helpers.ts index cfe1b31d33..4f460794c1 100644 --- a/apps/sim/lib/workflows/db-helpers.ts +++ b/apps/sim/lib/workflows/db-helpers.ts @@ -69,12 +69,12 @@ export async function loadWorkflowFromNormalizedTables( parentId, extent, } - + // Debug: Log sample block subBlocks from database if (block.type === 'agent') { logger.debug(`Loaded ${block.type} block from database:`, { blockId: block.id, - subBlocks: block.subBlocks + subBlocks: block.subBlocks, }) } }) @@ -175,7 +175,7 @@ export async function saveWorkflowToNormalizedTables( logger.debug(`Saving ${blockInserts.length} blocks. Sample block:`, { blockId: blockInserts[0].id, type: blockInserts[0].type, - subBlocks: blockInserts[0].subBlocks + subBlocks: blockInserts[0].subBlocks, }) } diff --git a/apps/sim/stores/workflows/registry/store.ts b/apps/sim/stores/workflows/registry/store.ts index 10c4205b3e..a2b55d6cbe 100644 --- a/apps/sim/stores/workflows/registry/store.ts +++ b/apps/sim/stores/workflows/registry/store.ts @@ -475,7 +475,10 @@ export const useWorkflowRegistry = create()( }) // Debug: Log what values are being extracted from the database - logger.debug(`Extracted subblock values from database for workflow ${id}:`, subblockValues) + logger.debug( + `Extracted subblock values from database for workflow ${id}:`, + subblockValues + ) // Update subblock store for this workflow useSubBlockStore.setState((state) => ({ @@ -484,7 +487,7 @@ export const useWorkflowRegistry = create()( [id]: subblockValues, }, })) - + // Debug: Verify SubBlockStore was updated const updatedValues = useSubBlockStore.getState().workflowValues[id] logger.debug(`SubBlockStore updated for workflow ${id}:`, updatedValues) diff --git a/apps/sim/stores/workflows/yaml/importer.ts b/apps/sim/stores/workflows/yaml/importer.ts index bb1eedbf55..3a357e7e5e 100644 --- a/apps/sim/stores/workflows/yaml/importer.ts +++ b/apps/sim/stores/workflows/yaml/importer.ts @@ -378,7 +378,7 @@ export async function importWorkflowFromYaml( // Get the existing workflow state (to preserve starter blocks if they exist) let existingBlocks: Record = {} - + if (targetWorkflowId) { // For target workflow, fetch from API try { @@ -386,7 +386,10 @@ export async function importWorkflowFromYaml( if (response.ok) { const workflowData = await response.json() existingBlocks = workflowData.data?.state?.blocks || {} - logger.debug(`Fetched existing blocks for target workflow ${targetWorkflowId}:`, Object.keys(existingBlocks)) + logger.debug( + `Fetched existing blocks for target workflow ${targetWorkflowId}:`, + Object.keys(existingBlocks) + ) } } catch (error) { logger.warn(`Failed to fetch existing blocks for workflow ${targetWorkflowId}:`, error) @@ -395,7 +398,7 @@ export async function importWorkflowFromYaml( // For active workflow, use from store existingBlocks = workflowActions.getExistingBlocks() } - + const existingStarterBlocks = Object.values(existingBlocks).filter( (block: any) => block.type === 'starter' ) @@ -413,8 +416,10 @@ export async function importWorkflowFromYaml( if (!activeWorkflowId) { return { success: false, errors: ['No active workflow'], warnings: [] } } - - logger.info(`Importing YAML into workflow: ${activeWorkflowId} ${targetWorkflowId ? '(specified target)' : '(active workflow)'}`) + + logger.info( + `Importing YAML into workflow: ${activeWorkflowId} ${targetWorkflowId ? '(specified target)' : '(active workflow)'}` + ) // Build complete blocks object const completeBlocks: Record = {} @@ -615,11 +620,11 @@ export async function importWorkflowFromYaml( // Save directly to database via API logger.info('Saving complete workflow state directly to database...') logger.debug('Sample block being saved:', { - firstBlockId: Object.keys(completeBlocks)[0], + firstBlockId: Object.keys(completeBlocks)[0], firstBlock: Object.values(completeBlocks)[0], - firstBlockSubBlocks: Object.values(completeBlocks)[0]?.subBlocks + firstBlockSubBlocks: Object.values(completeBlocks)[0]?.subBlocks, }) - + const response = await fetch(`/api/workflows/${activeWorkflowId}/state`, { method: 'PUT', headers: { @@ -654,7 +659,7 @@ export async function importWorkflowFromYaml( [activeWorkflowId]: completeSubBlockValues, }, })) - + // Verify SubBlockStore was updated const subBlockStoreValues = useSubBlockStore.getState().workflowValues[activeWorkflowId] logger.debug('SubBlockStore after update:', subBlockStoreValues)