@@ -5,6 +5,7 @@ import { getSession } from '@/lib/auth'
55import { createLogger } from '@/lib/logs/console/logger'
66import { getUserEntityPermissions } from '@/lib/permissions/utils'
77import { saveWorkflowToNormalizedTables } from '@/lib/workflows/db-helpers'
8+ import { sanitizeAgentToolsInBlocks } from '@/lib/workflows/validation'
89import { db } from '@/db'
910import { workflow } from '@/db/schema'
1011
@@ -168,11 +169,14 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
168169 return NextResponse . json ( { error : 'Access denied' } , { status : 403 } )
169170 }
170171
172+ // Sanitize custom tools in agent blocks before saving
173+ const { blocks : sanitizedBlocks , warnings } = sanitizeAgentToolsInBlocks ( state . blocks as any )
174+
171175 // Save to normalized tables
172176 // Ensure all required fields are present for WorkflowState type
173177 // Filter out blocks without type or name before saving
174- const filteredBlocks = Object . entries ( state . blocks ) . reduce (
175- ( acc , [ blockId , block ] ) => {
178+ const filteredBlocks = Object . entries ( sanitizedBlocks ) . reduce (
179+ ( acc , [ blockId , block ] : [ string , any ] ) => {
176180 if ( block . type && block . name ) {
177181 // Ensure all required fields are present
178182 acc [ blockId ] = {
@@ -184,7 +188,6 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
184188 height : block . height !== undefined ? block . height : 0 ,
185189 subBlocks : block . subBlocks || { } ,
186190 outputs : block . outputs || { } ,
187- data : block . data || { } ,
188191 }
189192 }
190193 return acc
@@ -226,30 +229,21 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
226229 const elapsed = Date . now ( ) - startTime
227230 logger . info ( `[${ requestId } ] Successfully saved workflow ${ workflowId } state in ${ elapsed } ms` )
228231
229- return NextResponse . json (
230- {
231- success : true ,
232- blocksCount : Object . keys ( filteredBlocks ) . length ,
233- edgesCount : state . edges . length ,
234- } ,
235- { status : 200 }
236- )
237- } catch ( error : unknown ) {
232+ return NextResponse . json ( { success : true , warnings } , { status : 200 } )
233+ } catch ( error : any ) {
238234 const elapsed = Date . now ( ) - startTime
235+ logger . error (
236+ `[${ requestId } ] Error saving workflow ${ workflowId } state after ${ elapsed } ms` ,
237+ error
238+ )
239+
239240 if ( error instanceof z . ZodError ) {
240- logger . warn ( `[${ requestId } ] Invalid workflow state data for ${ workflowId } ` , {
241- errors : error . errors ,
242- } )
243241 return NextResponse . json (
244- { error : 'Invalid state data ' , details : error . errors } ,
242+ { error : 'Invalid request body ' , details : error . errors } ,
245243 { status : 400 }
246244 )
247245 }
248246
249- logger . error (
250- `[${ requestId } ] Error saving workflow ${ workflowId } state after ${ elapsed } ms` ,
251- error
252- )
253247 return NextResponse . json ( { error : 'Internal server error' } , { status : 500 } )
254248 }
255249}
0 commit comments