index
The always-available prelude: printing, input, file I/O, and the array helpers. Every .agency file auto-imports these, so you can call them without an import.
const names = ["Ada", "Grace"]
print(names)
print(range(3))Types
WriteMode
How an existing file is handled on write: "overwrite" replaces it, "append" adds to it, "create-only" fails if it already exists.
/** How an existing file is handled on write:
"overwrite" replaces it, "append" adds to it, "create-only" fails if it
already exists. */
export type WriteMode = "overwrite" | "append" | "create-only"(source)
Call
What a decorator learns about the function it wraps. The name is the definition's original name. Its args hold the parameters by name, with block parameters left out. The id is the function's opaque runtime identity, shared by partial, renamed, and preapproved copies. It is not a source path or an identifier guaranteed to stay the same across builds. The inToolCall flag records whether the invocation began inside a tool call made by llm() in this run, including calls from that tool's body or branches. Subprocess and MCP calls do not inherit this context. Use these fields for reporting. Direct callers and edited checkpoints can supply their values, so do not use them to grant permissions or skip approvals. See the Decorators guide page.
/** What a decorator learns about the function it wraps. The `name` is
the definition's original name. Its `args` hold the parameters by name,
with block parameters left out. The `id` is the function's opaque runtime
identity, shared by partial, renamed, and preapproved copies. It is not
a source path or an identifier guaranteed to stay the same across builds.
The `inToolCall` flag records whether the invocation began inside a
tool call made by `llm()` in this run, including calls from that tool's
body or branches. Subprocess and MCP calls do not inherit this context.
Use these fields for reporting. Direct callers and edited checkpoints
can supply their values, so do not use them to grant permissions or skip
approvals. See the Decorators guide page. */
export type Call = {
name: string;
args: object;
id: string;
inToolCall: boolean
}(source)
Effects
std::read
@alwaysUnder(dir)
effect std::read {
dir: string;
filename: string
}(source)
std::write
@alwaysUnder(dir)
effect std::write {
dir: string;
filename: string
}(source)
std::readImage
@alwaysUnder(dir)
effect std::readImage {
dir: string;
filename: string
}(source)
std::readBinary
@alwaysUnder(dir)
effect std::readBinary {
dir: string;
filename: string
}(source)
std::writeBinary
@alwaysUnder(dir)
effect std::writeBinary {
dir: string;
filename: string;
mode: WriteMode
}(source)
Functions
print
print(...messages: any[])Print a message to the console.
Parameters:
| Name | Type | Default |
|---|---|---|
| messages | any[] |
Needs: terminal
(source)
setAgentCwd
setAgentCwd(dir: string)Set the base for relative paths in tools called with useAgentCwd: true.
Set the agent working directory. Path-taking tools (read, write, edit, ls, glob, grep, exec, bash, ...) can resolve relative paths against the agent working directory if you pass in useAgentCwd: true to them.
This is useful if you're building a coding agent, to set the current working directory for the agent.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| dir | string | The absolute directory to use as the agent working directory |
(source)
getAgentCwd
getAgentCwd(): stringReturn the agent working directory, or an empty string if none is set.
Returns: string
(source)
applyAgentCwd
applyAgentCwd(dir: string): stringResolve a relative path against the agent working directory. Absolute paths and paths with no agent working directory set pass through unchanged.
File and shell tools call this when their useAgentCwd option is true.
Parameters:
| Name | Type | Default |
|---|---|---|
| dir | string |
Returns: string
(source)
printJSON
printJSON(obj: any, highlight: boolean = false)Print an object as formatted JSON to the console.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| obj | any | ||
| highlight | boolean | false | Whether to syntax-highlight the output |
Needs: terminal
(source)
input
input(prompt: string): stringPrompt the user for input and return their response.
Ctrl-C, race-loser, or time-guard abort releases a blocked input prompt, which surfaces as an AgencyCancelledError.
Parameters:
| Name | Type | Default |
|---|---|---|
| prompt | string |
Returns: string
Needs: terminal
(source)
sleep
sleep(ms: number)Pause execution for the given duration in milliseconds.
Use unit literals for clarity: sleep(1s), sleep(500ms), sleep(2m). A long sleep wakes immediately on Ctrl-C, race-loser, or time-guard abort.
Parameters:
| Name | Type | Default |
|---|---|---|
| ms | number |
(source)
saveDraft
saveDraft(value: any)Save the current scope's best answer so far for recovery if a guard stops it. Call again as the answer improves.
Use this to retain a partial answer when a guard stops unfinished work. The last saved value wins and must match the enclosing scope's return type. Drafts belong to their function, node, or guard block; a caller can pass a callee's draft onward with return callee(), or save a draft of its own. Calling at module scope is an error. Without a guard, saving a draft does not affect the normal return value.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| value | any | A partial result matching the enclosing scope's return type |
(source)
read
read(
filename: string,
dir: string = ".",
offset: number = 0,
limit: number = 0,
useAgentCwd: boolean = false,
): Result raises <std::read>Read the contents of a file and return it as a string. A path from a "saved as a file" notice, which holds output too long to return at once, can be read here too, as the whole path in filename; it needs no approval.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| filename | string | Relative path inside dir; absolute paths, ~, escapes, and symlink components are refused, except the path of saved output. To reach another directory, pass it as dir. | |
| dir | string | "." | Base directory for filename |
| offset | number | 0 | 1-indexed line to start at (0 means start of file) |
| limit | number | 0 | Maximum number of lines to return (0 means read to end of file) |
| useAgentCwd | boolean | false | Resolve a relative dir against the agent working directory |
Returns: Result
Throws: std::read
Needs: fileRead
(source)
write
write(
filename: string,
content: string,
dir: string = ".",
mode: WriteMode = "overwrite",
useAgentCwd: boolean = false,
): Result raises <std::write>Write content to a file. Folders in the path that do not exist are created.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| filename | string | Relative path inside dir; absolute paths, ~, escapes, and symlink components are refused. To reach another directory, pass it as dir. | |
| content | string | ||
| dir | string | "." | Base directory for filename |
| mode | WriteMode | "overwrite" | How to handle an existing file |
| useAgentCwd | boolean | false | Resolve a relative dir against the agent working directory |
Returns: Result
Throws: std::write
Needs: fileRead, fileWrite
(source)
writeBinary
writeBinary(
filename: string,
base64: string,
dir: string = ".",
mode: WriteMode = "overwrite",
useAgentCwd: boolean = false,
): Result raises <std::writeBinary>Decode base64 data and write the bytes to a file. Folders in the path that do not exist are created.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| filename | string | Relative path inside dir; absolute paths, ~, escapes, and symlink components are refused. To reach another directory, pass it as dir. | |
| base64 | string | ||
| dir | string | "." | Base directory for filename |
| mode | WriteMode | "overwrite" | How to handle an existing file |
| useAgentCwd | boolean | false | Resolve a relative dir against the agent working directory |
Returns: Result
Throws: std::writeBinary
Needs: fileRead, fileWrite
(source)
readBinary
readBinary(
filename: string,
dir: string = ".",
useAgentCwd: boolean = false,
): Result raises <std::readBinary>Read a binary file as a base64-encoded string.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| filename | string | Relative path inside dir; absolute paths, ~, escapes, and symlink components are refused. To reach another directory, pass it as dir. | |
| dir | string | "." | Base directory for filename |
| useAgentCwd | boolean | false | Resolve a relative dir against the agent working directory |
Returns: Result
Throws: std::readBinary
Needs: fileRead
(source)
range
range(start: number, end: number = -1): number[]Generate an array of numbers. With one argument, counts from 0 to start-1; with two, from start to end-1.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| start | number | The count with one argument, or the starting number with two | |
| end | number | -1 | The exclusive end number (omit to count up from 0) |
Returns: number[]
(source)
map
map(arr: any[], func: (any) -> any): any[]Map a function over an array, returning a new array of results.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: any[]
(source)
mapWithIndex
mapWithIndex(arr: any[], func: (any, any) -> any): any[]Apply a function to each item of an array along with its index, and return a new array of the results.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any, any) => any | Called with the item and its zero-based index |
Returns: any[]
(source)
filter
filter(arr: any[], func: (any) -> any): any[]Return a new array containing only the elements for which the function returns true.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element; return true to keep it |
Returns: any[]
(source)
exclude
exclude(arr: any[], func: (any) -> any): any[]Return a new array excluding elements for which the function returns true.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element; return true to exclude it |
Returns: any[]
(source)
find
find(arr: any[], func: (any) -> any): anyReturn the first element for which the function returns true, or null if none match.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: any
(source)
findIndex
findIndex(arr: any[], func: (any) -> any): numberReturn the index of the first element for which the function returns true, or -1 if none match.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: number
(source)
reduce
reduce(arr: any[], initial: any, func: (any, any) -> any): anyReduce an array to a single value by applying a function to an accumulator and each element.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| initial | any | The initial accumulator value | |
| func | (any, any) => any | The reducer function receiving (accumulator, element) |
Returns: any
(source)
flatMap
flatMap(arr: any[], func: (any) -> any): any[]Map a function over an array and flatten the results by one level.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: any[]
(source)
flatten
flatten(arr: any[]): any[]Flatten an array of arrays by one level.
Parameters:
| Name | Type | Default |
|---|---|---|
| arr | any[] |
Returns: any[]
(source)
every
every(arr: any[], func: (any) -> any): booleanReturn true if the function returns true for every element in the array.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: boolean
(source)
some
some(arr: any[], func: (any) -> any): booleanReturn true if the function returns true for at least one element in the array.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: boolean
(source)
count
count(arr: any[], func: (any) -> any): numberCount the number of elements in the array for which the function returns true.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Called with each element |
Returns: number
(source)
sortBy
sortBy(arr: any[], func: (any) -> any): any[]Return a new array sorted by the values returned by the function, in ascending order.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Returns the sort key for each element |
Returns: any[]
(source)
unique
unique(arr: any[], func: (any) -> any): any[]Return a new array with duplicate elements removed, using the function to determine the identity of each element.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Returns the identity key for each element |
Returns: any[]
(source)
groupBy
groupBy(arr: any[], func: (any) -> any): anyGroup elements of an array by the value returned by the function. Returns an object where keys are group names and values are arrays of elements.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| arr | any[] | ||
| func | (any) => any | Returns the group key for each element |
Returns: any
(source)
callback
callback(name: string, fn: any)Register a callback for a lifecycle event. A callback registered inside a function or node is removed when that returns. One registered at the top level stays active for the whole run.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| name | string | The callback hook name, e.g. "onNodeStart", "onFunctionStart", "onLLMCallEnd" | |
| fn | any | A function that receives the event data |
(source)