1- import { db , workflow as workflowTable } from '@sim/db'
21import { createLogger } from '@sim/logger'
3- import { eq } from 'drizzle-orm'
42import { type NextRequest , NextResponse } from 'next/server'
53import { v4 as uuidv4 } from 'uuid'
64import { z } from 'zod'
75import { checkHybridAuth } from '@/lib/auth/hybrid'
86import { generateRequestId } from '@/lib/core/utils/request'
97import { SSE_HEADERS } from '@/lib/core/utils/sse'
108import { markExecutionCancelled } from '@/lib/execution/cancellation'
9+ import { preprocessExecution } from '@/lib/execution/preprocessing'
1110import { LoggingSession } from '@/lib/logs/execution/logging-session'
1211import { executeWorkflowCore } from '@/lib/workflows/executor/execution-core'
1312import { createSSECallbacks } from '@/lib/workflows/executor/execution-events'
@@ -75,12 +74,31 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
7574 const { startBlockId, sourceSnapshot, input } = validation . data
7675 const executionId = uuidv4 ( )
7776
78- const [ workflowRecord ] = await db
79- . select ( { workspaceId : workflowTable . workspaceId , userId : workflowTable . userId } )
80- . from ( workflowTable )
81- . where ( eq ( workflowTable . id , workflowId ) )
82- . limit ( 1 )
77+ // Run preprocessing checks (billing, rate limits, usage limits)
78+ const preprocessResult = await preprocessExecution ( {
79+ workflowId,
80+ userId,
81+ triggerType : 'manual' ,
82+ executionId,
83+ requestId,
84+ checkRateLimit : false , // Manual executions don't rate limit
85+ checkDeployment : false , // Run-from-block doesn't require deployment
86+ } )
87+
88+ if ( ! preprocessResult . success ) {
89+ const { error } = preprocessResult
90+ logger . warn ( `[${ requestId } ] Preprocessing failed for run-from-block` , {
91+ workflowId,
92+ error : error ?. message ,
93+ statusCode : error ?. statusCode ,
94+ } )
95+ return NextResponse . json (
96+ { error : error ?. message || 'Execution blocked' } ,
97+ { status : error ?. statusCode || 500 }
98+ )
99+ }
83100
101+ const workflowRecord = preprocessResult . workflowRecord
84102 if ( ! workflowRecord ?. workspaceId ) {
85103 return NextResponse . json ( { error : 'Workflow not found or has no workspace' } , { status : 404 } )
86104 }
@@ -92,6 +110,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
92110 workflowId,
93111 startBlockId,
94112 executedBlocksCount : sourceSnapshot . executedBlocks . length ,
113+ billingActorUserId : preprocessResult . actorUserId ,
95114 } )
96115
97116 const loggingSession = new LoggingSession ( workflowId , executionId , 'manual' , requestId )
0 commit comments