ui-calling

Follow up when the user stops

Use it when it matters that the user finishes: a return, a booking, a verification. If they open the screen and go quiet, the agent writes once, knowing where they stopped.

Full example: abandonAfterMs in the demo's server/ui.ts, and abandoned in scriptedFollowUp in server/agent.ts.

Setup

Off by default. Turn it on with how long the user may be idle:

export const ui = createUiCalling({
  // ...
  abandonAfterMs: 10 * 60_000,
});

What the agent gets

The finish line is any event whose contract says wake: true (even one recorded quietly past maxWakesPerMinute), or a failed action that woke the agent (an outage: the agent already heard about it). A refusal, like a wrong code, doesn't count: the user may have given up after it. If neither happened and the user has been idle for abandonAfterMs, the runtime records one abandoned event, which wakes the agent:

What the user didWhat the agent reads
Opened the screen, nothing else"User opened the screen but took no action"
Saved some steps, then went quiet"User stopped partway without finishing. Last step: Step 2 of 5: gave reasons: …", with payload.lastStep and the screen's state
Still moving through the stepsNothing yet: the timer restarts from their last step

Prompt

A woken turn has no new message from the user. Offer stay_silent on it (woken on the adapter on every turn, see GUIDE.md, "A woken turn may say nothing"), and send the events with formatEventsForAgent(events, { woken: true }): after an abandoned event it tells the model to reply this turn, and it keeps silence for a success the screen showed. Without that line, many models stayed silent on abandons too. For the reply itself, add to the system prompt:

If the screen shows the user stopped partway, offer once to help with the step they stopped at, in one sentence. Don't repeat the whole screen.

Count the silences with ui.onSilentWake: if most abandons end in stay_silent, the wait is too short or the surface is one people only read (set abandonAfterMs: false on it).

In the demo the reply is specific because the event names the step: "I saw you stopped at the pickup step of your return. Need a hand? Your progress is saved, so the same link picks up where you left off."

After WhatsApp's 24-hour window

Many people come back the next day, and by then WhatsApp only delivers approved templates: a free-form follow-up fails with error 131047. Check insideServiceWindow(lastUserMessageAt) before the woken turn writes. Outside it, send a template with a fresh link to the same saved progress, and let any other wake end in stay_silent:

import { insideServiceWindow, whatsappTemplateMessage } from "@ui-calling/core/whatsapp";

ui.onEvent(async (e) => {
  if (!e.wake) return;
  const convo = await conversations.get(e.conversationId);
  if (insideServiceWindow(convo.lastUserMessageAt)) return runAgent(convo, e);   // free-form, as above
  if (e.type !== "abandoned" || !convo.phone) return;                            // wait for the user to write
  const session = await ui.store.getSession(e.sessionId);
  // A new link: the old one may have expired, and the progress lives on the server, not in the link.
  const link = await ui.call(`show_${e.surface}`, { ...session!.input, locale: session!.locale }, { conversationId: convo.id, subject: e.subject });
  await sendToWhatsApp(
    whatsappTemplateMessage({
      to: convo.phone,
      name: "application_follow_up",        // "Your application for {{1}} is saved…" + a URL button ending in {{1}}
      language: session!.locale,
      body: [jobTitle],
      urlSuffix: link.url.split("/s/")[1],  // the button's address is https://screens.acme.com/s/{{1}}
    }),
  );
});

Create the template in WhatsApp Manager first (category utility, a URL button whose address ends in a variable) and wait for Meta to approve it. Templates are paid, and they go to a phone number: if Meta never shared one, wait for the user to write.

Watch out

In the ui-calling repo

Not in the package; for reading if you have the repo.

Source: packages/cli/recipes/follow-up-abandoned.md