fire(name, payload), typically from inside a webhook handler or queue consumer. Both run under the same tracing as request-driven agents.
Defining a trigger
Create atrigger.ts file anywhere under a background-tasks directory (conventionally server/triggers/):
server/triggers/order-created/trigger.ts
The task body. Receives a
TaskContext with now (when fire was called), payload (whatever you passed to fire), and trace.Registering and firing triggers
TriggerRegistry API
Run the named trigger’s
run function, passing payload through ctx.payload. Returns whatever run returns. Throws if no trigger is registered under name.fire() call opens a trace span named trigger <name>. If run throws, the span closes with status error and the error propagates back to the caller — unlike scheduled tasks, a triggered task’s failure is not swallowed, since the caller (your webhook handler) usually needs to know it failed to return the right HTTP status.
fire() runs the trigger synchronously, in the same request that called it. For triggers backed by slow or unreliable side effects, queue the actual work inside run (or hand off to a job queue) rather than blocking the webhook response on it.See also
- Scheduled agents — the cron-driven counterpart
- Multi-agent orchestration — call a registered
AgentRegistryagent from inside a trigger’srunfunction - Observability — triggered runs appear in the same trace dashboard as request-driven agents
fire(name, payload), typically from inside a webhook handler or queue consumer. Both run under the same tracing as request-driven agents.
Defining a trigger
Create atrigger.ts file anywhere under a background-tasks directory (conventionally server/triggers/):
server/triggers/order-created/trigger.ts
The task body. Receives a
TaskContext with now (when fire was called), payload (whatever you passed to fire), and trace.Registering and firing triggers
TriggerRegistry API
Run the named trigger’s
run function, passing payload through ctx.payload. Returns whatever run returns. Throws if no trigger is registered under name.fire() call opens a trace span named trigger <name>. If run throws, the span closes with status error and the error propagates back to the caller — unlike scheduled tasks, a triggered task’s failure is not swallowed, since the caller (your webhook handler) usually needs to know it failed to return the right HTTP status.
fire() runs the trigger synchronously, in the same request that called it. For triggers backed by slow or unreliable side effects, queue the actual work inside run (or hand off to a job queue) rather than blocking the webhook response on it.See also
- Scheduled agents — the cron-driven counterpart
- Multi-agent orchestration — call a registered
AgentRegistryagent from inside a trigger’srunfunction - Observability — triggered runs appear in the same trace dashboard as request-driven agents