@@ -4,16 +4,19 @@ import { type NextRequest, NextResponse } from 'next/server'
44import { z } from 'zod'
55import { env } from '@/lib/env'
66import { createLogger } from '@/lib/logs/console/logger'
7- import { db } from '@/db'
8- import { workflow as workflowTable , workflowCheckpoints , customTools } from '@/db/schema'
7+ import { simAgentClient } from '@/lib/sim-agent'
8+ import {
9+ loadWorkflowFromNormalizedTables ,
10+ saveWorkflowToNormalizedTables ,
11+ } from '@/lib/workflows/db-helpers'
12+ import { sanitizeAgentToolsInBlocks } from '@/lib/workflows/validation'
13+ import { getUserId } from '@/app/api/auth/oauth/utils'
914import { getAllBlocks , getBlock } from '@/blocks'
1015import type { BlockConfig } from '@/blocks/types'
11- import { generateLoopBlocks , generateParallelBlocks } from '@/stores/workflows/workflow/utils'
1216import { resolveOutputType } from '@/blocks/utils'
13- import { getUserId } from '@/app/api/auth/oauth/utils'
14- import { simAgentClient } from '@/lib/sim-agent'
15- import { sanitizeAgentToolsInBlocks } from '@/lib/workflows/validation'
16- import { loadWorkflowFromNormalizedTables , saveWorkflowToNormalizedTables } from '@/lib/workflows/db-helpers'
17+ import { db } from '@/db'
18+ import { customTools , workflowCheckpoints , workflow as workflowTable } from '@/db/schema'
19+ import { generateLoopBlocks , generateParallelBlocks } from '@/stores/workflows/workflow/utils'
1720
1821const logger = createLogger ( 'YamlWorkflowAPI' )
1922
@@ -35,9 +38,7 @@ function updateBlockReferences(
3538 // Replace references in string values
3639 for ( const [ oldId , newId ] of blockIdMapping . entries ( ) ) {
3740 if ( value . includes ( oldId ) ) {
38- value = value
39- . replaceAll ( `<${ oldId } .` , `<${ newId } .` )
40- . replaceAll ( `%${ oldId } .` , `%${ newId } .` )
41+ value = value . replaceAll ( `<${ oldId } .` , `<${ newId } .` ) . replaceAll ( `%${ oldId } .` , `%${ newId } .` )
4142 }
4243 }
4344 return value
@@ -171,7 +172,11 @@ async function upsertCustomToolsFromBlocks(
171172 tool . schema . function . name &&
172173 typeof tool . code === 'string'
173174 ) {
174- collected . push ( { title : tool . title || tool . schema . function . name , schema : tool . schema , code : tool . code } )
175+ collected . push ( {
176+ title : tool . title || tool . schema . function . name ,
177+ schema : tool . schema ,
178+ code : tool . code ,
179+ } )
175180 }
176181 }
177182 }
@@ -186,10 +191,7 @@ async function upsertCustomToolsFromBlocks(
186191 }
187192
188193 // Load existing user's tools
189- const existing = await db
190- . select ( )
191- . from ( customTools )
192- . where ( eq ( customTools . userId , userId ) )
194+ const existing = await db . select ( ) . from ( customTools ) . where ( eq ( customTools . userId , userId ) )
193195
194196 const existingByName = new Map < string , ( typeof existing ) [ number ] > ( )
195197 for ( const row of existing ) {
@@ -325,8 +327,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
325327
326328 const conversionResult = await conversionResponse . json ( )
327329
328- const workflowState =
329- conversionResult . workflowState || ( conversionResult . diff && conversionResult . diff . proposedState )
330+ const workflowState = conversionResult . workflowState || conversionResult . diff ?. proposedState
330331
331332 if ( ! conversionResult . success || ! workflowState ) {
332333 logger . error ( `[${ requestId } ] YAML conversion failed` , {
@@ -639,9 +640,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
639640 newWorkflowState . blocks
640641 )
641642 if ( sanitationWarnings . length > 0 ) {
642- logger . warn (
643- `[${ requestId } ] Tool sanitation produced ${ sanitationWarnings . length } warning(s)`
644- )
643+ logger . warn ( `[${ requestId } ] Tool sanitation produced ${ sanitationWarnings . length } warning(s)` )
645644 }
646645 newWorkflowState . blocks = sanitizedBlocks
647646
0 commit comments