1+ import { randomUUID } from 'crypto'
12import { db } from '@sim/db'
2- import { chat , workflow , workspace } from '@sim/db/schema'
3+ import { chat } from '@sim/db/schema'
34import { eq } from 'drizzle-orm'
45import { type NextRequest , NextResponse } from 'next/server'
5- import { v4 as uuidv4 } from 'uuid'
66import { z } from 'zod'
7+ import { preprocessExecution } from '@/lib/execution/preprocessing'
78import { createLogger } from '@/lib/logs/console/logger'
89import { LoggingSession } from '@/lib/logs/execution/logging-session'
910import { ChatFiles } from '@/lib/uploads'
@@ -93,7 +94,7 @@ export async function POST(
9394 if ( ! deployment . isActive ) {
9495 logger . warn ( `[${ requestId } ] Chat is not active: ${ identifier } ` )
9596
96- const executionId = uuidv4 ( )
97+ const executionId = randomUUID ( )
9798 const loggingSession = new LoggingSession (
9899 deployment . workflowId ,
99100 executionId ,
@@ -140,82 +141,35 @@ export async function POST(
140141 return addCorsHeaders ( createErrorResponse ( 'No input provided' , 400 ) , request )
141142 }
142143
143- const workflowResult = await db
144- . select ( {
145- isDeployed : workflow . isDeployed ,
146- workspaceId : workflow . workspaceId ,
147- variables : workflow . variables ,
148- } )
149- . from ( workflow )
150- . where ( eq ( workflow . id , deployment . workflowId ) )
151- . limit ( 1 )
144+ const executionId = randomUUID ( )
152145
153- if ( workflowResult . length === 0 || ! workflowResult [ 0 ] . isDeployed ) {
154- logger . warn ( `[${ requestId } ] Workflow not found or not deployed: ${ deployment . workflowId } ` )
155-
156- const executionId = uuidv4 ( )
157- const loggingSession = new LoggingSession (
158- deployment . workflowId ,
159- executionId ,
160- 'chat' ,
161- requestId
162- )
146+ const loggingSession = new LoggingSession ( deployment . workflowId , executionId , 'chat' , requestId )
163147
164- await loggingSession . safeStart ( {
165- userId : deployment . userId ,
166- workspaceId : workflowResult [ 0 ] ?. workspaceId || '' ,
167- variables : { } ,
168- } )
148+ const preprocessResult = await preprocessExecution ( {
149+ workflowId : deployment . workflowId ,
150+ userId : deployment . userId ,
151+ triggerType : 'chat' ,
152+ executionId,
153+ requestId,
154+ checkRateLimit : false , // Chat bypasses rate limits
155+ checkDeployment : true , // Chat requires deployed workflows
156+ loggingSession,
157+ } )
169158
170- await loggingSession . safeCompleteWithError ( {
171- error : {
172- message : 'Chat workflow is not available. The workflow is not deployed.' ,
173- stackTrace : undefined ,
174- } ,
175- traceSpans : [ ] ,
176- } )
177-
178- return addCorsHeaders ( createErrorResponse ( 'Chat workflow is not available' , 503 ) , request )
159+ if ( ! preprocessResult . success ) {
160+ logger . warn ( `[ ${ requestId } ] Preprocessing failed: ${ preprocessResult . error ?. message } ` )
161+ return addCorsHeaders (
162+ createErrorResponse (
163+ preprocessResult . error ?. message || 'Failed to process request' ,
164+ preprocessResult . error ?. statusCode || 500
165+ ) ,
166+ request
167+ )
179168 }
180169
181- let workspaceOwnerId = deployment . userId
182- if ( workflowResult [ 0 ] . workspaceId ) {
183- const workspaceData = await db
184- . select ( { ownerId : workspace . ownerId } )
185- . from ( workspace )
186- . where ( eq ( workspace . id , workflowResult [ 0 ] . workspaceId ) )
187- . limit ( 1 )
188-
189- if ( workspaceData . length === 0 ) {
190- logger . error ( `[${ requestId } ] Workspace not found for workflow ${ deployment . workflowId } ` )
191-
192- const executionId = uuidv4 ( )
193- const loggingSession = new LoggingSession (
194- deployment . workflowId ,
195- executionId ,
196- 'chat' ,
197- requestId
198- )
199-
200- await loggingSession . safeStart ( {
201- userId : deployment . userId ,
202- workspaceId : workflowResult [ 0 ] . workspaceId || '' ,
203- variables : { } ,
204- } )
205-
206- await loggingSession . safeCompleteWithError ( {
207- error : {
208- message : 'Workspace not found. Critical configuration error - please contact support.' ,
209- stackTrace : undefined ,
210- } ,
211- traceSpans : [ ] ,
212- } )
213-
214- return addCorsHeaders ( createErrorResponse ( 'Workspace not found' , 500 ) , request )
215- }
216-
217- workspaceOwnerId = workspaceData [ 0 ] . ownerId
218- }
170+ const { actorUserId, workflowRecord } = preprocessResult
171+ const workspaceOwnerId = actorUserId !
172+ const workspaceId = workflowRecord ?. workspaceId || ''
219173
220174 try {
221175 const selectedOutputs : string [ ] = [ ]
@@ -232,12 +186,10 @@ export async function POST(
232186 const { SSE_HEADERS } = await import ( '@/lib/utils' )
233187 const { createFilteredResult } = await import ( '@/app/api/workflows/[id]/execute/route' )
234188
235- const executionId = crypto . randomUUID ( )
236-
237189 const workflowInput : any = { input, conversationId }
238190 if ( files && Array . isArray ( files ) && files . length > 0 ) {
239191 const executionContext = {
240- workspaceId : workflowResult [ 0 ] . workspaceId || '' ,
192+ workspaceId,
241193 workflowId : deployment . workflowId ,
242194 executionId,
243195 }
@@ -257,20 +209,13 @@ export async function POST(
257209 } catch ( fileError : any ) {
258210 logger . error ( `[${ requestId } ] Failed to process chat files:` , fileError )
259211
260- const fileLoggingSession = new LoggingSession (
261- deployment . workflowId ,
262- executionId ,
263- 'chat' ,
264- requestId
265- )
266-
267- await fileLoggingSession . safeStart ( {
212+ await loggingSession . safeStart ( {
268213 userId : workspaceOwnerId ,
269- workspaceId : workflowResult [ 0 ] . workspaceId || '' ,
214+ workspaceId,
270215 variables : { } ,
271216 } )
272217
273- await fileLoggingSession . safeCompleteWithError ( {
218+ await loggingSession . safeCompleteWithError ( {
274219 error : {
275220 message : `File upload failed: ${ fileError . message || 'Unable to process uploaded files' } ` ,
276221 stackTrace : fileError . stack ,
@@ -285,9 +230,9 @@ export async function POST(
285230 const workflowForExecution = {
286231 id : deployment . workflowId ,
287232 userId : deployment . userId ,
288- workspaceId : workflowResult [ 0 ] . workspaceId ,
289- isDeployed : true ,
290- variables : workflowResult [ 0 ] . variables || { } ,
233+ workspaceId,
234+ isDeployed : workflowRecord ?. isDeployed ?? false ,
235+ variables : workflowRecord ? .variables || { } ,
291236 }
292237
293238 const stream = await createStreamingResponse ( {
0 commit comments