Skip to content

Commit 3c92b34

Browse files
author
Theodore Li
committed
Switch back to inline imports
1 parent b12e07c commit 3c92b34

File tree

3 files changed

+16
-127
lines changed

3 files changed

+16
-127
lines changed

apps/sim/executor/handlers/agent/agent-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { mcpServers } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { and, eq, inArray, isNull } from 'drizzle-orm'
55
import { createMcpToolId } from '@/lib/mcp/utils'
6-
import { getCustomToolById } from '@/lib/workflows/custom-tools/operations'
76
import { getAllBlocks } from '@/blocks'
87
import type { BlockOutput } from '@/blocks/types'
98
import {
@@ -284,6 +283,7 @@ export class AgentBlockHandler implements BlockHandler {
284283
}
285284

286285
try {
286+
const { getCustomToolById } = await import('@/lib/workflows/custom-tools/operations')
287287
const tool = await getCustomToolById({
288288
toolId: customToolId,
289289
userId: ctx.userId,

apps/sim/tools/utils.server.ts

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { createLogger } from '@sim/logger'
2-
import { generateInternalToken } from '@/lib/auth/internal'
32
import {
43
secureFetchWithPinnedIP,
54
validateUrlWithDNS,
65
} from '@/lib/core/security/input-validation.server'
7-
import { getInternalApiBaseUrl } from '@/lib/core/utils/urls'
86
import { isCustomTool } from '@/executor/constants'
97
import type { CustomToolDefinition } from '@/hooks/queries/custom-tools'
108
import { 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
}

apps/sim/tools/utils.ts

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createLogger } from '@sim/logger'
22
import { getMaxExecutionTimeout } from '@/lib/core/execution-limits'
3-
import { getCustomToolById, listCustomTools } from '@/lib/workflows/custom-tools/operations'
43
import { AGENT, isCustomTool } from '@/executor/constants'
54
import type { CustomToolDefinition } from '@/hooks/queries/custom-tools'
65
import { useEnvironmentStore } from '@/stores/settings/environment'
@@ -295,24 +294,6 @@ export function getTool(toolId: string, _workspaceId?: string): ToolConfig | und
295294
return undefined
296295
}
297296

298-
// Get a tool by its ID asynchronously (supports server-side)
299-
export async function getToolAsync(
300-
toolId: string,
301-
workflowId?: string,
302-
userId?: string
303-
): Promise<ToolConfig | undefined> {
304-
// Check for built-in tools
305-
const builtInTool = tools[toolId]
306-
if (builtInTool) return builtInTool
307-
308-
// Check if it's a custom tool
309-
if (isCustomTool(toolId)) {
310-
return fetchCustomToolFromDB(toolId, workflowId, userId)
311-
}
312-
313-
return undefined
314-
}
315-
316297
// Helper function to create a tool config from a custom tool
317298
export function createToolConfig(
318299
customTool: CustomToolDefinition,
@@ -354,69 +335,4 @@ export function createToolConfig(
354335
}
355336
}
356337

357-
// Create a tool config from a custom tool definition by querying the database directly
358-
async function fetchCustomToolFromDB(
359-
customToolId: string,
360-
workflowId?: string,
361-
userId?: string
362-
): Promise<ToolConfig | undefined> {
363-
const identifier = customToolId.replace('custom_', '')
364-
365-
try {
366-
if (!userId) {
367-
logger.error(`Cannot fetch custom tool without userId: ${identifier}`)
368-
return undefined
369-
}
370-
371-
// Try to find by ID first, fall back to searching by title
372-
const customTool =
373-
(await getCustomToolById({ toolId: identifier, userId })) ??
374-
(await listCustomTools({ userId })).find((t) => t.title === identifier)
375-
376-
if (!customTool) {
377-
logger.error(`Custom tool not found: ${identifier}`)
378-
return undefined
379-
}
380-
381-
const schema = customTool.schema as Record<string, any>
382-
383-
// Create a parameter schema
384-
const params = createParamSchema(customTool)
385-
386-
// Create a tool config for the custom tool
387-
return {
388-
id: customToolId,
389-
name: customTool.title,
390-
description: schema.function?.description || '',
391-
version: '1.0.0',
392-
params,
393-
394-
// Request configuration - for custom tools we'll use the execute endpoint
395-
request: {
396-
url: '/api/function/execute',
397-
method: 'POST',
398-
headers: () => ({ 'Content-Type': 'application/json' }),
399-
body: createCustomToolRequestBody(customTool, false, workflowId),
400-
},
401-
402-
// Same response handling as client-side
403-
transformResponse: async (response: Response) => {
404-
const data = await response.json()
405-
406-
if (!data.success) {
407-
throw new Error(data.error || 'Custom tool execution failed')
408-
}
409-
410-
return {
411-
success: true,
412-
output: data.output.result || data.output,
413-
error: undefined,
414-
}
415-
},
416-
}
417-
} catch (error) {
418-
logger.error(`Error fetching custom tool ${identifier} from DB:`, error)
419-
return undefined
420-
}
421-
}
422338

0 commit comments

Comments
 (0)