Skip to content

Commit f2450d3

Browse files
author
priyanshu.solanki
committed
refactored code to use hasstartblock from the tirgger utils
1 parent cfbe4a4 commit f2450d3

File tree

5 files changed

+67
-133
lines changed

5 files changed

+67
-133
lines changed

apps/sim/app/api/mcp/workflow-servers/[id]/tools/route.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { createLogger } from '@/lib/logs/console/logger'
66
import { getParsedBody, withMcpAuth } from '@/lib/mcp/middleware'
77
import { createMcpErrorResponse, createMcpSuccessResponse } from '@/lib/mcp/utils'
88
import { sanitizeToolName } from '@/lib/mcp/workflow-tool-schema'
9-
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
9+
import { hasValidStartBlock } from '@/lib/workflows/triggers/trigger-utils'
1010

1111
const logger = createLogger('WorkflowMcpToolsAPI')
1212

@@ -16,36 +16,6 @@ interface RouteParams {
1616
id: string
1717
}
1818

19-
/**
20-
* Check if a workflow has a valid start block that can accept inputs
21-
*/
22-
async function hasValidStartBlock(workflowId: string): Promise<boolean> {
23-
try {
24-
const normalizedData = await loadWorkflowFromNormalizedTables(workflowId)
25-
if (!normalizedData?.blocks) {
26-
return false
27-
}
28-
29-
// Look for a start block
30-
const startBlock = Object.values(normalizedData.blocks).find((block: any) => {
31-
const blockType = block?.type
32-
return (
33-
blockType === 'starter' ||
34-
blockType === 'start' ||
35-
blockType === 'start_trigger' ||
36-
blockType === 'api' ||
37-
blockType === 'api_trigger' ||
38-
blockType === 'input_trigger'
39-
)
40-
})
41-
42-
return !!startBlock
43-
} catch (error) {
44-
logger.warn('Error checking for start block:', error)
45-
return false
46-
}
47-
}
48-
4919
/**
5020
* GET - List all tools for a workflow MCP server
5121
*/

apps/sim/app/api/workflows/[id]/deploy/route.ts

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { generateRequestId } from '@/lib/core/utils/request'
55
import { createLogger } from '@/lib/logs/console/logger'
66
import { deployWorkflow, loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
77
import { validateWorkflowPermissions } from '@/lib/workflows/utils'
8+
import {
9+
hasValidStartBlock,
10+
isValidStartBlockType,
11+
} from '@/lib/workflows/triggers/trigger-utils'
812
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
913

1014
const logger = createLogger('WorkflowDeployAPI')
@@ -24,15 +28,7 @@ async function generateMcpToolSchema(workflowId: string): Promise<Record<string,
2428

2529
// Find the start block
2630
const startBlock = Object.values(normalizedData.blocks).find((block: any) => {
27-
const blockType = block?.type
28-
return (
29-
blockType === 'starter' ||
30-
blockType === 'start' ||
31-
blockType === 'start_trigger' ||
32-
blockType === 'api' ||
33-
blockType === 'api_trigger' ||
34-
blockType === 'input_trigger'
35-
)
31+
return isValidStartBlockType(block?.type)
3632
}) as any
3733

3834
if (!startBlock?.subBlocks?.inputFormat?.value) {
@@ -88,35 +84,6 @@ async function generateMcpToolSchema(workflowId: string): Promise<Record<string,
8884
}
8985
}
9086

91-
/**
92-
* Check if a workflow has a valid start block
93-
*/
94-
async function hasValidStartBlock(workflowId: string): Promise<boolean> {
95-
try {
96-
const normalizedData = await loadWorkflowFromNormalizedTables(workflowId)
97-
if (!normalizedData?.blocks) {
98-
return false
99-
}
100-
101-
const startBlock = Object.values(normalizedData.blocks).find((block: any) => {
102-
const blockType = block?.type
103-
return (
104-
blockType === 'starter' ||
105-
blockType === 'start' ||
106-
blockType === 'start_trigger' ||
107-
blockType === 'api' ||
108-
blockType === 'api_trigger' ||
109-
blockType === 'input_trigger'
110-
)
111-
})
112-
113-
return !!startBlock
114-
} catch (error) {
115-
logger.warn('Error checking for start block:', error)
116-
return false
117-
}
118-
}
119-
12087
/**
12188
* Update all MCP tools that reference this workflow with the latest parameter schema.
12289
* If the workflow no longer has a start block, remove all MCP tools.

apps/sim/app/api/workflows/[id]/deployments/[version]/activate/route.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import type { NextRequest } from 'next/server'
44
import { generateRequestId } from '@/lib/core/utils/request'
55
import { createLogger } from '@/lib/logs/console/logger'
66
import { validateWorkflowPermissions } from '@/lib/workflows/utils'
7+
import {
8+
hasValidStartBlockInState,
9+
isValidStartBlockType,
10+
} from '@/lib/workflows/triggers/trigger-utils'
711
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
812

913
const logger = createLogger('WorkflowActivateDeploymentAPI')
@@ -22,15 +26,7 @@ function generateMcpToolSchemaFromState(state: any): Record<string, unknown> {
2226

2327
// Find the start block in the deployed state
2428
const startBlock = Object.values(state.blocks).find((block: any) => {
25-
const blockType = block?.type
26-
return (
27-
blockType === 'starter' ||
28-
blockType === 'start' ||
29-
blockType === 'start_trigger' ||
30-
blockType === 'api' ||
31-
blockType === 'api_trigger' ||
32-
blockType === 'input_trigger'
33-
)
29+
return isValidStartBlockType(block?.type)
3430
}) as any
3531

3632
if (!startBlock?.subBlocks?.inputFormat?.value) {
@@ -86,29 +82,6 @@ function generateMcpToolSchemaFromState(state: any): Record<string, unknown> {
8682
}
8783
}
8884

89-
/**
90-
* Check if a version state has a valid start block
91-
*/
92-
function hasValidStartBlockInState(state: any): boolean {
93-
if (!state?.blocks) {
94-
return false
95-
}
96-
97-
const startBlock = Object.values(state.blocks).find((block: any) => {
98-
const blockType = block?.type
99-
return (
100-
blockType === 'starter' ||
101-
blockType === 'start' ||
102-
blockType === 'start_trigger' ||
103-
blockType === 'api' ||
104-
blockType === 'api_trigger' ||
105-
blockType === 'input_trigger'
106-
)
107-
})
108-
109-
return !!startBlock
110-
}
111-
11285
/**
11386
* Sync MCP tools when activating a deployment version.
11487
* If the version has no start block, remove all MCP tools.

apps/sim/app/api/workflows/[id]/deployments/[version]/revert/route.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { generateRequestId } from '@/lib/core/utils/request'
66
import { createLogger } from '@/lib/logs/console/logger'
77
import { saveWorkflowToNormalizedTables } from '@/lib/workflows/persistence/utils'
88
import { validateWorkflowPermissions } from '@/lib/workflows/utils'
9+
import {
10+
hasValidStartBlockInState,
11+
isValidStartBlockType,
12+
} from '@/lib/workflows/triggers/trigger-utils'
913
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
1014

1115
const logger = createLogger('RevertToDeploymentVersionAPI')
@@ -24,15 +28,7 @@ function generateMcpToolSchemaFromState(state: any): Record<string, unknown> {
2428

2529
// Find the start block in the deployed state
2630
const startBlock = Object.values(state.blocks).find((block: any) => {
27-
const blockType = block?.type
28-
return (
29-
blockType === 'starter' ||
30-
blockType === 'start' ||
31-
blockType === 'start_trigger' ||
32-
blockType === 'api' ||
33-
blockType === 'api_trigger' ||
34-
blockType === 'input_trigger'
35-
)
31+
return isValidStartBlockType(block?.type)
3632
}) as any
3733

3834
if (!startBlock?.subBlocks?.inputFormat?.value) {
@@ -88,29 +84,6 @@ function generateMcpToolSchemaFromState(state: any): Record<string, unknown> {
8884
}
8985
}
9086

91-
/**
92-
* Check if a version state has a valid start block
93-
*/
94-
function hasValidStartBlockInState(state: any): boolean {
95-
if (!state?.blocks) {
96-
return false
97-
}
98-
99-
const startBlock = Object.values(state.blocks).find((block: any) => {
100-
const blockType = block?.type
101-
return (
102-
blockType === 'starter' ||
103-
blockType === 'start' ||
104-
blockType === 'start_trigger' ||
105-
blockType === 'api' ||
106-
blockType === 'api_trigger' ||
107-
blockType === 'input_trigger'
108-
)
109-
})
110-
111-
return !!startBlock
112-
}
113-
11487
/**
11588
* Sync MCP tools when reverting to a deployment version.
11689
* If the version has no start block, remove all MCP tools.

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,60 @@ import {
77
import { getAllBlocks, getBlock } from '@/blocks'
88
import type { BlockConfig } from '@/blocks/types'
99
import { getTrigger } from '@/triggers'
10+
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
1011

1112
const logger = createLogger('TriggerUtils')
1213

14+
/**
15+
* Valid start block types that can trigger a workflow
16+
*/
17+
export const VALID_START_BLOCK_TYPES = [
18+
'starter',
19+
'start',
20+
'start_trigger',
21+
'api',
22+
'api_trigger',
23+
'input_trigger',
24+
] as const
25+
26+
export type ValidStartBlockType = (typeof VALID_START_BLOCK_TYPES)[number]
27+
28+
/**
29+
* Check if a block type is a valid start block type
30+
*/
31+
export function isValidStartBlockType(blockType: string): blockType is ValidStartBlockType {
32+
return VALID_START_BLOCK_TYPES.includes(blockType as ValidStartBlockType)
33+
}
34+
35+
/**
36+
* Check if a workflow state has a valid start block
37+
*/
38+
export function hasValidStartBlockInState(state: any): boolean {
39+
if (!state?.blocks) {
40+
return false
41+
}
42+
43+
const startBlock = Object.values(state.blocks).find((block: any) => {
44+
const blockType = block?.type
45+
return isValidStartBlockType(blockType)
46+
})
47+
48+
return !!startBlock
49+
}
50+
51+
/**
52+
* Check if a workflow has a valid start block by loading from database
53+
*/
54+
export async function hasValidStartBlock(workflowId: string): Promise<boolean> {
55+
try {
56+
const normalizedData = await loadWorkflowFromNormalizedTables(workflowId)
57+
return hasValidStartBlockInState(normalizedData)
58+
} catch (error) {
59+
logger.warn('Error checking for start block:', error)
60+
return false
61+
}
62+
}
63+
1364
/**
1465
* Generates mock data based on the output type definition
1566
*/

0 commit comments

Comments
 (0)