Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/sim/app/api/function/execute/route.ts
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
import { createContext, Script } from 'vm'
import { type NextRequest, NextResponse } from 'next/server'
import { env, isTruthy } from '@/lib/env'
import { MAX_EXECUTION_DURATION } from '@/lib/execution/constants'
import { executeInE2B } from '@/lib/execution/e2b'
import { CodeLanguage, DEFAULT_CODE_LANGUAGE, isValidCodeLanguage } from '@/lib/execution/languages'
import { createLogger } from '@/lib/logs/console/logger'
import { validateProxyUrl } from '@/lib/security/input-validation'
import { generateRequestId } from '@/lib/utils'
export const dynamic = 'force-dynamic'
export const runtime = 'nodejs'
export const maxDuration = 60
export const maxDuration = MAX_EXECUTION_DURATION

const logger = createLogger('FunctionExecuteAPI')

Expand DownExpand Up@@ -649,10 +650,12 @@ export async function POST(req: NextRequest) {
try {
const body = await req.json()

const { DEFAULT_EXECUTION_TIMEOUT_MS } = await import('@/lib/execution/constants')

const {
code,
params = {},
timeout = 5000,
timeout = DEFAULT_EXECUTION_TIMEOUT_MS,
language = DEFAULT_CODE_LANGUAGE,
useLocalVM = false,
envVars = {},
Expand Down
9 changes: 5 additions & 4 deletions apps/sim/executor/handlers/function/function-handler.test.ts
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { BlockType } from '@/executor/consts'
import { FunctionBlockHandler } from '@/executor/handlers/function/function-handler'
import type { ExecutionContext } from '@/executor/types'
Expand DownExpand Up@@ -82,7 +83,7 @@ describe('FunctionBlockHandler', () => {
workflowVariables: {},
blockData: {},
blockNameMapping: {},
_context: { workflowId: mockContext.workflowId },
_context: { workflowId: mockContext.workflowId, workspaceId: mockContext.workspaceId },
}
const expectedOutput: any = { result: 'Success' }

Expand DownExpand Up@@ -116,7 +117,7 @@ describe('FunctionBlockHandler', () => {
workflowVariables: {},
blockData: {},
blockNameMapping: {},
_context: { workflowId: mockContext.workflowId },
_context: { workflowId: mockContext.workflowId, workspaceId: mockContext.workspaceId },
}
const expectedOutput: any = { result: 'Success' }

Expand All@@ -138,12 +139,12 @@ describe('FunctionBlockHandler', () => {
code: inputs.code,
language: 'javascript',
useLocalVM: true,
timeout: 5000, // Default timeout
timeout: DEFAULT_EXECUTION_TIMEOUT_MS,
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
_context: { workflowId: mockContext.workflowId },
_context: { workflowId: mockContext.workflowId, workspaceId: mockContext.workspaceId },
}

await handler.execute(mockBlock, inputs, mockContext)
Expand Down
3 changes: 2 additions & 1 deletion apps/sim/executor/handlers/function/function-handler.ts
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { DEFAULT_CODE_LANGUAGE } from '@/lib/execution/languages'
import { createLogger } from '@/lib/logs/console/logger'
import { BlockType } from '@/executor/consts'
Expand DownExpand Up@@ -61,7 +62,7 @@ export class FunctionBlockHandler implements BlockHandler {
code: codeContent,
language: inputs.language || DEFAULT_CODE_LANGUAGE,
useLocalVM: !inputs.remoteExecution,
timeout: inputs.timeout || 5000,
timeout: inputs.timeout || DEFAULT_EXECUTION_TIMEOUT_MS,
envVars: context.environmentVariables || {},
workflowVariables: context.workflowVariables || {},
blockData: blockData, // Pass block data for variable resolution
Expand Down
10 changes: 10 additions & 0 deletions apps/sim/lib/execution/constants.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
/**
* Execution timeout constants
*
* These constants define the timeout values for code execution.
* - DEFAULT_EXECUTION_TIMEOUT_MS: The default timeout for executing user code (3 minutes)
* - MAX_EXECUTION_DURATION: The maximum duration for the API route (adds 30s buffer for overhead)
*/

export const DEFAULT_EXECUTION_TIMEOUT_MS = 180000 // 3 minutes (180 seconds)
export const MAX_EXECUTION_DURATION = 210 // 3.5 minutes (210 seconds) - includes buffer for sandbox creation
3 changes: 2 additions & 1 deletion apps/sim/tools/function/execute.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
* which runs JavaScript code in a secure sandbox.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { ToolTester } from '@/tools/__test-utils__/test-tools'
import { functionExecuteTool } from '@/tools/function/execute'

Expand DownExpand Up@@ -95,7 +96,7 @@ describe('Function Execute Tool', () => {

expect(body).toEqual({
code: 'return 42',
timeout: 10000,
timeout: DEFAULT_EXECUTION_TIMEOUT_MS,
envVars: {},
workflowVariables: {},
blockData: {},
Expand Down
7 changes: 3 additions & 4 deletions apps/sim/tools/function/execute.ts
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { DEFAULT_CODE_LANGUAGE } from '@/lib/execution/languages'
import type { CodeExecutionInput, CodeExecutionOutput } from '@/tools/function/types'
import type { ToolConfig } from '@/tools/types'

const DEFAULT_TIMEOUT = 10000 // 10 seconds

export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOutput> = {
id: 'function_execute',
name: 'Function Execute',
Expand DownExpand Up@@ -38,7 +37,7 @@ export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOu
required: false,
visibility: 'user-only',
description: 'Execution timeout in milliseconds',
default: DEFAULT_TIMEOUT,
default: DEFAULT_EXECUTION_TIMEOUT_MS,
},
envVars: {
type: 'object',
Expand DownExpand Up@@ -85,7 +84,7 @@ export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOu
code: codeContent,
language: params.language || DEFAULT_CODE_LANGUAGE,
useLocalVM: params.useLocalVM || false,
timeout: params.timeout || DEFAULT_TIMEOUT,
timeout: params.timeout || DEFAULT_EXECUTION_TIMEOUT_MS,
envVars: params.envVars || {},
workflowVariables: params.workflowVariables || {},
blockData: params.blockData || {},
Expand Down
Loading