Trigger API Reference
Reference for repository trigger descriptors, event envelopes, condition matching, retry controls, and declarative app-tool actions.
Repository triggers use the generated
ansight/triggers/ansight-trigger.d.ts
contract. The copy generated by your installed CLI is authoritative for that
version. The linked source and this guide provide an online reference.
Module shape
Every trigger exports a JSON-compatible descriptor and one default function:
import type {
TriggerContext,
TriggerDefinition
} from "./ansight-trigger.d.ts";
type Payload = {
priority: string;
message?: string;
};
export const trigger = {
schemaVersion: 1,
eventKind: "session.log.received",
conditions: [
{
field: "payload.priority",
operator: "equals",
value: "Error"
}
]
} satisfies TriggerDefinition<"session.log.received">;
export default function run({ event, app }: TriggerContext<Payload>) {
return app.artifacts.request({
providerId: "example.diagnostics",
artifactId: "state",
arguments: { message: event.payload.message ?? "" }
});
}
The function returns nothing or exactly one declarative action. Version 1 supports app-tool actions; version 2 also supports host evidence actions. The function does not execute the action itself.
TriggerDefinition<TEventKind>
| Field | Type | Required | Contract |
|---|---|---|---|
schemaVersion | 1 | 2 | No | Defaults to version 1, which requires an SDK app-tool provider. Use version 2 for host evidence actions and capability requirements. |
requires | { capabilities?: string[]; appTools?: string[] } | No | Version 2 only: refuse execution when a required capability or executable app tool is unavailable. |
enabled | boolean | No | Defaults to true. Disabled triggers remain discoverable but never match events. |
appId | string | Sometimes | Optional when the repository has a registered app scope; otherwise required. |
eventKind | EventKind | Yes | Exact normalized event kind. |
eventSchema | JsonSchema | No | Describes event.payload; version 1 does not use it for matching. |
functionTimeoutMs | number | No | TypeScript function bound: 10–1,000 ms; default 100. |
actionTimeoutSeconds | number | No | Separate app action bound: 1–300 seconds; default 30. |
retry | RetryPolicy | No | Host-owned policy with 1–5 total attempts. |
conditions | Condition[] | No | At most 16 conditions evaluated by the CLI; every condition must pass. |
The descriptor is extracted without importing the module. Keep it a static object literal without variables, functions, spreads, or computed values.
TriggerContext<TPayload, TEventKind>
| Property | Type | Meaning |
|---|---|---|
run | Readonly<TriggerRun> | Host-owned identity, attempt number, queue time, and effective limits. |
event | Readonly<EventEnvelope<TPayload, TEventKind>> | Immutable normalized event that matched. |
app | AppContext | Factories for the one optional app-tool action. |
ansight | Host evidence context | Capability snapshot and factories for screenshot, file capture, and annotation actions. |
Host evidence actions without the SDK
Version 2 triggers can return one of these host-owned actions:
| Factory | When to return it |
|---|---|
ansight.screenshots.capture(args?) | Preserve the screen at the matched event, such as a failure log. Declare ui.screenshot; the session must still have a live capture provider when the action runs. |
ansight.files.capture({ root?, path, maximumBytes? }) | Copy a known app-sandbox file into retained session evidence. Declare files.capture; choose a root-relative path and a bounded maximum size. |
ansight.annotations.create(args) | Mark the event’s time or screenshot with a short explanation when the existing evidence needs review context. |
Declare requirements in the trigger descriptor. ansight.capabilities is the
snapshot supplied to that invocation, with mode, app availability, and provider
details; it is not the task API’s asynchronous snapshot() method. SDK app
actions remain unavailable without an SDK provider. Missing capabilities are
reported as capability_unavailable with no retry.
See the native-log trigger example
for android-logcat and apple-unified-log conditions. Host capture actions
still require their provider to be available when the action runs; a finalized
session cannot supply a new live screenshot or sandbox read.
TriggerRun
| Property | Type | Meaning |
|---|---|---|
runId | string | Stable across retry attempts. |
triggerId | string | Repository-relative trigger ID. |
repositoryRootPath | string | Absolute connected repository root. |
enqueuedAtUtc | string | ISO-8601 queue timestamp. |
functionTimeoutMs | number | Effective TypeScript function bound. |
actionTimeoutSeconds | number | Effective returned-action bound. |
attemptNumber | number | One-based current attempt. |
maximumAttempts | number | Total attempts permitted. |
EventEnvelope<TPayload, TEventKind>
| Property | Type | Meaning |
|---|---|---|
eventId | string | Stable source-event identity. |
kind | TEventKind | Normalized event kind. |
occurredAtUtc | string | ISO-8601 occurrence time. |
appId | string | App scope used for matching. |
sessionId | string | null | Live or captured session when present. |
correlationId | string | Propagated into a returned app action. |
causationId | string | null | Optional causing event or operation. |
payload | TPayload | Event-specific normalized data. |
EventKind
Each event links to its timing, payload, and usage guidance.
Condition
The CLI evaluates conditions before running the trigger.
interface Condition {
field: ConditionField;
operator: ConditionOperator;
value?: unknown;
ignoreCase?: boolean;
}
Top-level field values are eventId, kind, occurredAtUtc, appId,
sessionId, correlationId, and causationId. Nested payload fields use
payload.<path>.
| Operator | Needs value | Behavior |
|---|---|---|
equals | Yes | Exact equality. |
notEquals | Yes | Exact inequality. |
contains | Yes | String or supported collection containment. |
startsWith | Yes | String prefix. |
endsWith | Yes | String suffix. |
exists | No | Field is present. |
notExists | No | Field is absent. |
ignoreCase enables ordinal case-insensitive string comparison. Every condition
must pass; there is no executable predicate or OR group in contract version 1.
RetryPolicy
| Field | Range | Default |
|---|---|---|
maxAttempts | 1–5, including the first | 1 |
initialDelayMs | 10–60,000 ms | 250 when retries are enabled |
backoffMultiplier | 1–10 | 2 |
maxDelayMs | At least the initial delay; at most 60,000 ms | Eight times initial delay, capped at 60,000 |
Only failed and timed-out attempts retry. Each attempt runs the handler again and keeps the same run, event, and correlation identity.
app: action factories
These methods return an AppToolAction; they do not return a promise and do not
call the app inside TypeScript. Return one action from the function so the CLI
can validate, authorize, audit, and dispatch it.
| Namespace | Methods |
|---|---|
app.artifacts | query(args?), request(args) |
app.ui | getVisualTree(args?), getScreenshot(args?), inspectNode(args?), showOverlay(args?), getOverlay(args?), queryOverlays(args?), updateOverlay(args?), removeOverlay(args?), clearOverlays(args?) |
app.files | listDirectory(args?), readFile(args?), getChecksum(args?), download(args?), beginBinaryDownload(args?), push(args?), copy(args?), move(args?), delete(args?) |
app.fileDescriptors | listOpen(args?), countOpen(args?), inspect(args?), getUsage(args?) |
app.jniReferences | captureGraph(args?) |
app.preferences | listKeys(args?), get(args?), set(args?), remove(args?) |
app.clipboard | getText(), hasText(), setText(args), clear() |
app.secureStorage | get(args?), set(args?), remove(args?) |
app.data | listDatabases(args?), describeSchema(args?), query(args?) |
app.reflection | listRoots(args?), inspectObject(args?), describeType(args?), setMemberValue(args?), invokeMethod(args?) |
app.maui | getCurrentPage(args?), getVisualTree(args?), findElements(args?), getElement(args?), getBindableProperty(args?), setBindableProperty(args?), clearBindableProperty(args?), inflateXaml(args?), addElement(args?), removeElement(args?), setAppTheme(args?), getBindingContext(args?), getBindings(args?), getResourceState(args?), getNavigationState(args?), invokeElementAction(args?), waitForUi(args?), getLayoutDiagnostics(args?), getHandlerDiagnostics(args?), invokeBindingContextCommand(args?), setBindingContextProperty(args?) |
app.react | getComponentTree(args?), getShadowTree(args?), findComponents(args?), getComponent(args?), getNavigationState(args?), invokeComponentAction(args?) |
app.flutter | getWidgetTree(args?), inspectWidget(args?), findWidgets(args?), getNavigationState(args?) |
app.capacitor | getDocument(args?), inspectNode(args?), querySelector(args?), invokeAction(args?) |
app | callTool(toolId, args?) for an app-defined tool |
Clipboard factories return declarative actions backed by the native Clipboard suite. Reads require Read policy; set and clear require Write policy. See Clipboard Tools for registration, limits, and platform behavior.
The App Tool API explains what each method does. In a trigger, return one factory result; the host executes it after the function returns.
Returning app.files.beginBinaryDownload({ root, path }) streams the selected
sandboxed file to the host. After the binary transfer completes, the host retains
the file as an artifact on the originating session.
AppToolAction<TArguments>
Every factory produces the same declarative shape:
interface AppToolAction<TArguments extends AppToolArguments> {
type: "appTool";
toolId: string;
arguments: TArguments;
}
The host preserves the matched event’s session and correlation identity. The action is rejected if no live session is available, the tool is absent, its arguments are invalid, or the app’s guard denies it.
Return contract
type TriggerFunction<TPayload, TEventKind> = (
invocation: TriggerContext<TPayload, TEventKind>
) =>
| AppToolAction
| null
| void
| Promise<AppToolAction | null | void>;
Return null or nothing for a match that needs local computation only. A
trigger cannot execute host-owned UI APIs, dispatch multiple app actions,
register new event sources, or supply an executable predicate.
Execution rules
- The exact App ID, event kind, and every condition are matched before the trigger runs.
- The function timeout and action timeout are separate.
- Repository trigger code is trusted local code, not a filesystem or network sandbox.
- App actions retain the connected app’s grants and critical-tool policy.
- Trigger catalogs must be explicitly connected before they receive events.
- A repository can define up to 256 triggers; each module is limited to 1 MiB.