11import { db } from '@sim/db'
22import { customTools } from '@sim/db/schema'
33import { and , desc , eq , isNull } from 'drizzle-orm'
4+ import { nanoid } from 'nanoid'
45import { createLogger } from '@/lib/logs/console/logger'
56import { generateRequestId } from '@/lib/utils'
67
@@ -23,35 +24,18 @@ export async function upsertCustomTools(params: {
2324} ) {
2425 const { tools, workspaceId, userId, requestId = generateRequestId ( ) } = params
2526
26- // Use a transaction for multi-step database operations
2727 return await db . transaction ( async ( tx ) => {
28- // Process each tool: either update existing or create new
2928 for ( const tool of tools ) {
3029 const nowTime = new Date ( )
3130
3231 if ( tool . id ) {
33- // First, check if tool exists in the workspace
3432 const existingWorkspaceTool = await tx
3533 . select ( )
3634 . from ( customTools )
3735 . where ( and ( eq ( customTools . id , tool . id ) , eq ( customTools . workspaceId , workspaceId ) ) )
3836 . limit ( 1 )
3937
4038 if ( existingWorkspaceTool . length > 0 ) {
41- // Tool exists in workspace
42- const newFunctionName = tool . schema ?. function ?. name
43- if ( ! newFunctionName ) {
44- throw new Error ( 'Tool schema must include a function name' )
45- }
46-
47- // Check if function name has changed
48- if ( tool . id !== newFunctionName ) {
49- throw new Error (
50- `Cannot change function name from "${ tool . id } " to "${ newFunctionName } ". Please create a new tool instead.`
51- )
52- }
53-
54- // Update existing workspace tool
5539 await tx
5640 . update ( customTools )
5741 . set ( {
@@ -64,7 +48,6 @@ export async function upsertCustomTools(params: {
6448 continue
6549 }
6650
67- // Check if this is a legacy tool (no workspaceId, belongs to user)
6851 const existingLegacyTool = await tx
6952 . select ( )
7053 . from ( customTools )
@@ -78,7 +61,6 @@ export async function upsertCustomTools(params: {
7861 . limit ( 1 )
7962
8063 if ( existingLegacyTool . length > 0 ) {
81- // Legacy tool found - update it without migrating to workspace
8264 await tx
8365 . update ( customTools )
8466 . set ( {
@@ -94,28 +76,18 @@ export async function upsertCustomTools(params: {
9476 }
9577 }
9678
97- // Creating new tool - use function name as ID for consistency
98- const functionName = tool . schema ?. function ?. name
99- if ( ! functionName ) {
100- throw new Error ( 'Tool schema must include a function name' )
101- }
102-
103- // Check for duplicate function names in workspace
104- const duplicateFunction = await tx
79+ const duplicateTitle = await tx
10580 . select ( )
10681 . from ( customTools )
107- . where ( and ( eq ( customTools . workspaceId , workspaceId ) , eq ( customTools . id , functionName ) ) )
82+ . where ( and ( eq ( customTools . workspaceId , workspaceId ) , eq ( customTools . title , tool . title ) ) )
10883 . limit ( 1 )
10984
110- if ( duplicateFunction . length > 0 ) {
111- throw new Error (
112- `A tool with the function name "${ functionName } " already exists in this workspace`
113- )
85+ if ( duplicateTitle . length > 0 ) {
86+ throw new Error ( `A tool with the title "${ tool . title } " already exists in this workspace` )
11487 }
11588
116- // Create new tool using function name as ID
11789 await tx . insert ( customTools ) . values ( {
118- id : functionName ,
90+ id : nanoid ( ) ,
11991 workspaceId,
12092 userId,
12193 title : tool . title ,
@@ -126,7 +98,6 @@ export async function upsertCustomTools(params: {
12698 } )
12799 }
128100
129- // Fetch and return the created/updated tools
130101 const resultTools = await tx
131102 . select ( )
132103 . from ( customTools )
0 commit comments