You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/docs/content/docs/workflows/blocks/function.mdx
+31-4Lines changed: 31 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,15 +148,42 @@ workspace path instead, or keep the secret out of the output.
148
148
149
149
## Language
150
150
151
-
JavaScript without imports runs in a fast local sandbox. JavaScript with `import` or `require`, Python, and Shell run in the configured remote sandbox provider.
151
+
JavaScript without imports, mounted files, or a selected sandbox runs in a fast local sandbox. JavaScript with `import` or `require`, Python, and Shell run in the configured remote sandbox provider.
152
152
153
153
| Feature | JavaScript | Python | Shell |
154
154
| --- | --- | --- | --- |
155
-
|**Execution**| Local when there are no imports; remote with imports | Always remote | Always remote |
155
+
|**Execution**| Local by default; remote with imports, mounted files, or a selected sandbox| Always remote | Always remote |
'This is a core workflow block. Execute custom JavaScript, Python, or Shell code within your workflow. JavaScript without imports runs locally for fast execution, while code with imports, Python, and Shell run in a remote sandbox.',
14
14
bestPractices: `
15
15
- JavaScript code without external imports runs in a local VM for fastest execution.
16
+
- Local JavaScript includes Buffer, atob, btoa, TextEncoder, and TextDecoder for data conversion without imports. Use Buffer.from(text, 'utf8').toString('base64') for Unicode text; btoa only accepts binary strings.
16
17
- JavaScript code with import/require statements runs in a remote sandbox.
17
18
- Python code always runs in a remote sandbox.
18
19
- Shell code runs CLI commands in a remote sandbox.
@@ -63,7 +64,7 @@ IMPORTANT FORMATTING RULES:
63
64
1. Reference Environment Variables: Use the exact syntax {{VARIABLE_NAME}}. In JavaScript and Python, prefer the unquoted form when the placeholder is the complete expression (for example, 'const apiKey = {{SERVICE_API_KEY}};'). Quoted and embedded string forms such as '"Bearer {{SERVICE_API_KEY}}"', template literals, and JavaScript regex literals are also supported. In Shell, prefer '"{{SERVICE_API_KEY}}"' when the secret should be one scalar argument; use a bare placeholder only when Bash word-splitting or pattern semantics are intentional. Sim binds the resolved value separately from the source at execution time, preserving its exact string contents.
64
65
2. Reference Input Parameters/Workflow Variables: Use the exact syntax <variable_name>. Do NOT wrap it in quotes (e.g., use 'userId = <userId>;' not 'userId = "<userId>";'). This includes parameters defined in the block's schema and outputs from previous blocks.
65
66
3. Function Body ONLY: Do NOT include the function signature (e.g., 'async function myFunction() {' or the surrounding '}').
66
-
4. Imports: Standard Node.js built-in modules (e.g., 'crypto', 'fs') are always available. Third-party packages are available ONLY when the block has a sandbox selected — the sandbox's package list is appended below when one is. Never import a package that is not on that list.
67
+
4. Runtime APIs: Buffer, atob, btoa, TextEncoder, and TextDecoder are available without imports. Use Buffer.from(text, 'utf8').toString('base64') for Unicode text; btoa only accepts binary strings. Importing Node.js built-in modules (e.g., 'crypto', 'fs') runs the code in a remote sandbox. Third-party packages are available ONLY when the block has a sandbox selected — the sandbox's package list is appended below when one is. Never import a package that is not on that list.
67
68
5. Output: Ensure the code returns a value if the function is expected to produce output. Use 'return'.
68
69
6. Clarity: Write clean, readable code.
69
70
7. No Explanations: Do NOT include markdown formatting, comments explaining the rules, or any text other than the raw JavaScript code for the function body.
thrownewError('Expected one Function globals bundle')
121
138
}
122
-
thrownewError(`Failed to build sandbox bundle: ${spec.name}`)
123
-
}
139
+
code=result.outputFiles[0].text
140
+
}else{
141
+
constresult=awaitBun.build({
142
+
entrypoints: [entryPath],
143
+
target: 'browser',
144
+
format: 'iife',
145
+
minify: true,
146
+
sourcemap: 'none',
147
+
root: APP_SIM_ROOT,
148
+
})
124
149
125
-
if(result.outputs.length===0){
126
-
thrownewError(`No output produced for sandbox bundle: ${spec.name}`)
150
+
if(!result.success){
151
+
for(constlogofresult.logs){
152
+
logger.error(String(log))
153
+
}
154
+
thrownewError(`Failed to build sandbox bundle: ${spec.name}`)
155
+
}
156
+
if(result.outputs.length===0){
157
+
thrownewError(`No output produced for sandbox bundle: ${spec.name}`)
158
+
}
159
+
code=awaitresult.outputs[0].text()
127
160
}
128
161
129
-
constcode=awaitresult.outputs[0].text()
130
162
constbanner=`// sandbox bundle: ${spec.name}\n// generated by apps/sim/lib/execution/sandbox/bundles/build.ts\n// do not edit by hand. run \`bun run build:sandbox-bundles\` to regenerate.\n`
131
163
constoutput=banner+code
132
164
try{
133
-
evaluateSandboxBundle(output,spec.name)
165
+
if(spec.name==='function-globals'){
166
+
evaluateFunctionGlobals(output)
167
+
}else{
168
+
evaluateSandboxBundle(output,spec.name)
169
+
}
134
170
}catch(error){
135
171
thrownewError(
136
172
`Sandbox bundle ${spec.name} does not evaluate in a bare isolate context: ${String(error)}`
0 commit comments