Tulip Logo IconTulip
Recipes

Approvals Inbox

Notify approvers, let them decide on an approval page, and keep an overview of open and decided approvals.

Runs outside the chat that wait for an approval need three things in the app: a notification that tells the approvers, a page to decide on, and an overview of what is open and what was decided. See Approvals for how a run waits and continues.

Notify the approvers

export const agents = Agents.init({
  // ...
  approvals: {
    notify: async (approval) => {
      await notifications.send(agentApprovalRequestedNotification, {
        to: approval.approverIds,
        data: {
          approvalId: approval.id,
          agent: approval.agentTitle,
          tool: approval.toolTitle,
        },
        dedupeKey: `agent-approval:${approval.id}`,
      });
    },
  },
});

The approval carries the titles of its agent and tool (agentTitle, toolTitle). Link the notification to /approvals/[id].

The approval page

approval: protectedProcedure
  .input(z.object({ id: z.string() }))
  .handler(async ({ context, input }) => {
    const approval = await agents.approvals.get(context.user.id, input.id);
    if (!approval) throw new ORPCError("NOT_FOUND", { message: "Approval not found" });

    return approval;
  }),

decideApproval: protectedProcedure
  .input(z.object({ id: z.string(), approved: z.boolean(), reason: z.string().optional(), proof: z.unknown() }))
  .handler(({ context, input }) => agents.approvals.decide({ user: context.user, ...input })),

Show what will happen: "<agentTitle> wants to <toolTitle>", the input, when it was asked and when it expires, with Approve and Deny. For an approval with confirmation: "passkey", ask for the passkey before approving and send the assertion as proof.

Passkeys

Issue the WebAuthn challenge for the user and the approval id, and only for an approval the user may decide:

approvalChallenge: protectedProcedure
  .input(z.object({ approvalId: z.string(), chatId: z.uuid().optional() }))
  .handler(async ({ context, input }) => {
    const isWaiting = input.chatId
      ? await chat.waitsFor(context.user.id, input.chatId, input.approvalId)
      : (await agents.approvals.get(context.user.id, input.approvalId))?.status === "pending";
    if (!isWaiting) throw new ORPCError("NOT_FOUND", { message: "Nothing waits for approval." });

    return createApprovalPasskeyChallenge(context.user, input.approvalId);
  }),

The same challenge works in the chat, where the approval id is the tool part's approval.id. The tool base's verifyProof consumes the challenge for that user and approval id once, and checks the assertion against one of the user's own passkeys.

The overview

List the user's approvals on the server, a page at a time, with an Open and a Decided list:

approvals: protectedProcedure
  .input(z.object({ status: z.enum(["open", "decided"]), pagination: offsetPaginationSchema }))
  .handler(async ({ context, input }) => {
    const status: AiApprovalStatus[] =
      input.status === "open" ? ["pending"] : ["approved", "denied", "expired"];
    const [data, total] = await Promise.all([
      agents.approvals.list(context.user.id, {
        status,
        limit: input.pagination.limit,
        offset: input.pagination.page * input.pagination.limit,
      }),
      agents.approvals.count(context.user.id, { status }),
    ]);

    return offsetPaginationResponse({ data, pagination: input.pagination, total });
  }),

Show the decided list with who decided and when, so there is a record of every change an agent made with someone's approval.

Guidance

  • Expire approvals from a daily job (agents.approvals.expire()).
  • Never let the client pick the approvers or the run: Agents stores them.
  • Keep the notification as the signal and the overview as the list; one does not replace the other.

On this page