code runs a TypeScript function in an isolated sandbox. Use it to reshape a
response, compute a value, or make a decision that one if comparison cannot
express. For calling an API, prefer http: its request shows in
the editor and in test runs.
The contract
source_ts must export default async function handler(input) and return an
object or array. publish_fields read out of the return value exactly as they
read an http response. Any other return value is wrapped, so return 42
publishes at the path result.
input carries the same six roots the rest of the tree templates:
- There is no template syntax. Write
input.system_vars.now, never{{system_vars.now}}; braces in the source are neither substituted nor checked. - Writing to
inputchanges nothing. Return a value and publish it. - Name secrets literally:
input.secrets.STRIPE_KEYorinput.secrets["STRIPE_KEY"]. Only the names found in the source are sent to the sandbox, so a name built at run time isundefined. A session’s own secrets are merged over the workspace’s. A name that is not a workspace secret is a publish warning: every session must supply it.
Config
The TypeScript is compiled at publish, so a script that does not compile is a
publish error. Types are stripped, not checked.
The sandbox
Each run gets a fresh V8 isolate. Nothing persists between runs or between twocode operations in one tree; pass values through tooldata.
Available: the JavaScript built-ins (JSON, Math, Date, Promise,
Map, Set, RegExp, Intl with timezone data), console and fetch.
Not available: require, import and every Node API — no process,
Buffer, URL, URLSearchParams, TextEncoder, crypto, atob,
structuredClone or timers. Request signing (an HMAC) cannot be done here.
The sandbox clock is UTC: new Date() is the right instant, but pass a
timeZone to Intl.DateTimeFormat whenever you need a local hour or weekday.
input.system_vars.now, .date and .time are absent when the calling agent
has no timezone — publish does not check reads inside a script. A complete
business-hours tool is in patterns.
fetch
A small shim, not the browser API:
http applies, checked at connection time; a
refused request throws fetch failed: <reason>.
Limits
A throw, a timeout (
execution exceeded 10000ms) or running out of memory fails
the operation as code operation failed: <message>, with the last ten console
lines appended when there are any. on_error decides what happens next.
Where console output goes
On a test run, each code step shows
its console lines beside its return value. On a live call they are not shown
anywhere you can read, except the last ten lines appended to a failure’s error.
Debug scripts in a test run.
Next
Conditionals
Branch on what the script returned.
Patterns
Business hours and other complete trees.
Testing and publishing
Run the script against real data and read its output.