Setting an alarm
An alarm wakes a workflow or an agent in the same project at a later time. Use one when the next step isn’t now: check up on someone in three days, or remind them the morning before an appointment.
Every workflow and agent has setAlarm. Alarms you set show up on the project’s Alarms tab, where you can also cancel them.
Wake a workflow
import { workflow } from "@loops/sdk";
type CheckUp = { phone: string };
export const checkUp = workflow<CheckUp>({
id: "check-up",
execute: async ({ step, event }) => {
await step.do("Send the check-up", ({ ctx }) =>
ctx.SMS.send({
to: event.body.phone,
content: "How are you feeling since your scan?",
}),
);
},
});import { workflow } from "@loops/sdk";
import { checkUp } from "./check-up";
type Intake = { phone: string };
export const intake = workflow<Intake>({
id: "intake",
execute: async ({ step, event }) => {
await step.do("Schedule the check-up", async ({ ctx }) => {
const user = await ctx.USER.getUser({ id: event.body.phone });
return checkUp.setAlarm({
in: "3 days",
body: { phone: event.body.phone },
label: "Check up after the scan",
user,
});
});
},
});When the alarm goes off, checkUp starts a run with body as its event.body. The run’s trigger is shown as Alarm.
Wake an agent
Give an agent a message instead of a body. The agent is woken with it as if a user had sent it:
await coach.setAlarm({
in: "1 week",
message: "It's been a week since this person started the plan. Check how it's going.",
sessionId: threadId,
user,
});Pass a sessionId to continue an existing conversation, or leave it out to start a new one. The agent’s reply stays in that conversation. To reach the person, give the agent a tool that sends them a message.
When it goes off
Pass exactly one of:
in: a delay from now, such as"30 minutes","3 days"or"2 weeks".at: a moment, as aDate, an ISO string or a timestamp.
A time in the past goes off right away. An alarm can be set at most a year ahead.
The options
| Option | For | What it does |
|---|---|---|
body | Workflows | The woken run’s event.body. |
message | Agents | What the agent is woken with. |
sessionId | Agents | The conversation to continue. Leave it out for a new one. |
label | Both | Shown next to the alarm on the Alarms tab. |
user | Both | The person it’s about. The woken run or conversation joins their timeline. |
Cancel an alarm
setAlarm resolves to { id, fireAt }. Keep the id to cancel the alarm before it goes off:
import { cancelAlarm } from "@loops/sdk";
const cancelled = await cancelAlarm(alarm.id);cancelAlarm resolves to false if the alarm already went off or was cancelled. Deleting a loop cancels its pending alarms.
Inside a step
Set alarms inside a step.do. A retried step gets back the alarm it already set instead of setting a second one. Outside a step, a retry can set the alarm twice.
The loop you wake has to be in the same project.