-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathexecute.ts
More file actions
97 lines (89 loc) · 2.72 KB
/
Copy pathexecute.ts
File metadata and controls
97 lines (89 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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',
description:
'Execute JavaScript code in a secure, sandboxed environment with proper isolation and resource limits.',
version: '1.0.0',
params: {
code: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The code to execute',
},
timeout: {
type: 'number',
required: false,
visibility: 'user-only',
description: 'Execution timeout in milliseconds',
default: DEFAULT_TIMEOUT,
},
envVars: {
type: 'object',
required: false,
visibility: 'user-only',
description: 'Environment variables to make available during execution',
default: {},
},
blockData: {
type: 'object',
required: false,
visibility: 'user-only',
description: 'Block output data for variable resolution',
default: {},
},
blockNameMapping: {
type: 'object',
required: false,
visibility: 'user-only',
description: 'Mapping of block names to block IDs',
default: {},
},
workflowVariables: {
type: 'object',
required: false,
visibility: 'user-only',
description: 'Workflow variables for <variable.name> resolution',
default: {},
},
},
request: {
url: '/api/function/execute',
method: 'POST',
headers: () => ({
'Content-Type': 'application/json',
}),
body: (params: CodeExecutionInput) => {
const codeContent = Array.isArray(params.code)
? params.code.map((c: { content: string }) => c.content).join('\n')
: params.code
return {
code: codeContent,
timeout: params.timeout || DEFAULT_TIMEOUT,
envVars: params.envVars || {},
workflowVariables: params.workflowVariables || {},
blockData: params.blockData || {},
blockNameMapping: params.blockNameMapping || {},
workflowId: params._context?.workflowId,
isCustomTool: params.isCustomTool || false,
}
},
},
transformResponse: async (response: Response): Promise<CodeExecutionOutput> => {
const result = await response.json()
return {
success: true,
output: {
result: result.output.result,
stdout: result.output.stdout,
},
}
},
outputs: {
result: { type: 'string', description: 'The result of the code execution' },
stdout: { type: 'string', description: 'The standard output of the code execution' },
},
}