@@ -157,16 +157,16 @@ export async function POST(req: NextRequest) {
157157 const nowTime = new Date ( )
158158
159159 if ( tool . id ) {
160- // Check if tool exists and belongs to the workspace
161- const existingTool = await tx
160+ // First, check if tool exists in the workspace
161+ const existingWorkspaceTool = await tx
162162 . select ( )
163163 . from ( customTools )
164164 . where ( and ( eq ( customTools . id , tool . id ) , eq ( customTools . workspaceId , workspaceId ) ) )
165165 . limit ( 1 )
166166
167- if ( existingTool . length > 0 ) {
168- // Tool exists - check if name changed and if new name conflicts
169- if ( existingTool [ 0 ] . title !== tool . title ) {
167+ if ( existingWorkspaceTool . length > 0 ) {
168+ // Tool exists in workspace - check if name changed and if new name conflicts
169+ if ( existingWorkspaceTool [ 0 ] . title !== tool . title ) {
170170 // Check for duplicate name in workspace (excluding current tool)
171171 const duplicateTool = await tx
172172 . select ( )
@@ -190,7 +190,7 @@ export async function POST(req: NextRequest) {
190190 }
191191 }
192192
193- // Update existing tool
193+ // Update existing workspace tool
194194 await tx
195195 . update ( customTools )
196196 . set ( {
@@ -202,6 +202,35 @@ export async function POST(req: NextRequest) {
202202 . where ( and ( eq ( customTools . id , tool . id ) , eq ( customTools . workspaceId , workspaceId ) ) )
203203 continue
204204 }
205+
206+ // Check if this is a legacy tool (no workspaceId, belongs to user)
207+ const existingLegacyTool = await tx
208+ . select ( )
209+ . from ( customTools )
210+ . where (
211+ and (
212+ eq ( customTools . id , tool . id ) ,
213+ isNull ( customTools . workspaceId ) ,
214+ eq ( customTools . userId , userId )
215+ )
216+ )
217+ . limit ( 1 )
218+
219+ if ( existingLegacyTool . length > 0 ) {
220+ // Legacy tool found - update it without migrating to workspace
221+ await tx
222+ . update ( customTools )
223+ . set ( {
224+ title : tool . title ,
225+ schema : tool . schema ,
226+ code : tool . code ,
227+ updatedAt : nowTime ,
228+ } )
229+ . where ( eq ( customTools . id , tool . id ) )
230+
231+ logger . info ( `[${ requestId } ] Updated legacy tool ${ tool . id } ` )
232+ continue
233+ }
205234 }
206235
207236 // Creating new tool - check for duplicate names in workspace
0 commit comments