11import { createLogger } from '@sim/logger'
2- import { generateInternalToken } from '@/lib/auth/internal'
32import {
43 secureFetchWithPinnedIP ,
54 validateUrlWithDNS ,
65} from '@/lib/core/security/input-validation.server'
7- import { getInternalApiBaseUrl } from '@/lib/core/utils/urls'
86import { isCustomTool } from '@/executor/constants'
97import type { CustomToolDefinition } from '@/hooks/queries/custom-tools'
108import { extractErrorMessage } from '@/tools/error-extractors'
@@ -97,67 +95,42 @@ export async function getToolAsync(
9795 if ( builtInTool ) return builtInTool
9896
9997 if ( isCustomTool ( toolId ) ) {
100- return fetchCustomToolFromAPI ( toolId , context )
98+ return fetchCustomToolFromDB ( toolId , context )
10199 }
102100
103101 return undefined
104102}
105103
106- async function fetchCustomToolFromAPI (
104+ async function fetchCustomToolFromDB (
107105 customToolId : string ,
108106 context : GetToolAsyncContext
109107) : Promise < ToolConfig | undefined > {
110108 const { workflowId, userId, workspaceId } = context
111109 const identifier = customToolId . replace ( 'custom_' , '' )
112110
113111 try {
114- const baseUrl = getInternalApiBaseUrl ( )
115- const url = new URL ( '/api/tools/custom' , baseUrl )
116-
117- if ( workflowId ) {
118- url . searchParams . append ( 'workflowId' , workflowId )
119- }
120- if ( userId ) {
121- url . searchParams . append ( 'userId' , userId )
122- }
123- if ( workspaceId ) {
124- url . searchParams . append ( 'workspaceId' , workspaceId )
125- }
126-
127- const headers : Record < string , string > = { }
128-
129- try {
130- const internalToken = await generateInternalToken ( userId )
131- headers . Authorization = `Bearer ${ internalToken } `
132- } catch ( error ) {
133- logger . warn ( 'Failed to generate internal token for custom tools fetch' , { error } )
134- }
135-
136- const response = await fetch ( url . toString ( ) , { headers } )
137-
138- if ( ! response . ok ) {
139- await response . text ( ) . catch ( ( ) => { } )
140- logger . error ( `Failed to fetch custom tools: ${ response . statusText } ` )
112+ if ( ! userId ) {
113+ logger . error ( `Cannot fetch custom tool without userId: ${ identifier } ` )
141114 return undefined
142115 }
143116
144- const result = await response . json ( )
145-
146- if ( ! result . data || ! Array . isArray ( result . data ) ) {
147- logger . error ( `Invalid response when fetching custom tools: ${ JSON . stringify ( result ) } ` )
148- return undefined
149- }
117+ const { getCustomToolById, listCustomTools } = await import (
118+ '@/lib/workflows/custom-tools/operations'
119+ )
150120
151- const customTool = result . data . find (
152- ( tool : CustomToolDefinition ) => tool . id === identifier || tool . title === identifier
153- ) as CustomToolDefinition | undefined
121+ // Try to find by ID first, fall back to searching by title
122+ const customTool =
123+ ( await getCustomToolById ( { toolId : identifier , userId, workspaceId } ) ) ??
124+ ( await listCustomTools ( { userId, workspaceId } ) ) . find (
125+ ( t : { title : string } ) => t . title === identifier
126+ )
154127
155128 if ( ! customTool ) {
156129 logger . error ( `Custom tool not found: ${ identifier } ` )
157130 return undefined
158131 }
159132
160- const toolConfig = createToolConfig ( customTool , customToolId )
133+ const toolConfig = createToolConfig ( customTool as unknown as CustomToolDefinition , customToolId )
161134
162135 return {
163136 ...toolConfig ,
@@ -168,7 +141,7 @@ async function fetchCustomToolFromAPI(
168141 } ,
169142 }
170143 } catch ( error ) {
171- logger . error ( `Error fetching custom tool ${ identifier } from API :` , error )
144+ logger . error ( `Error fetching custom tool ${ identifier } from DB :` , error )
172145 return undefined
173146 }
174147}
0 commit comments