forked from simstudioai/sim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.ts
More file actions
66 lines (57 loc) · 1.69 KB
/
Copy pathexecute.ts
File metadata and controls
66 lines (57 loc) · 1.69 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
import { ToolConfig } from '../types'
import { CodeExecutionInput, CodeExecutionOutput } from './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,
description: 'The code to execute',
},
timeout: {
type: 'number',
required: false,
description: 'Execution timeout in milliseconds',
default: DEFAULT_TIMEOUT,
},
},
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,
}
},
isInternalRoute: true,
},
transformResponse: async (response: Response): Promise<CodeExecutionOutput> => {
const result = await response.json()
if (!response.ok || !result.success) {
throw new Error(result.error || 'Code execution failed')
}
return {
success: true,
output: {
result: result.output.result,
stdout: result.output.stdout,
executionTime: result.output.executionTime,
},
}
},
transformError: (error: any) => {
return error.message || 'Code execution failed'
},
}