AI booking agent project (TypeScript) · Chapter 7 of 12
Human approval
By the end of this chapter your agent stops before it moves money. It asks Neha "Shall I pay ₹400 for booking 2 (Ghar Wapsi Express, Sat 7 Nov, 6:45 pm: E1, E2) from your Parda wallet? Answer yes or no.", keeps the waiting call in Postgres until she answers, runs it on yes, tells the model no on no, and pays once even when she taps Yes twice in the same second. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Remembering
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of AI booking agent project (TypeScript) are free. This one comes with the all labs plan.
Why you need this
One of the 17 wrong WhatsApp bookings in the trial weekend was a booking paid twice: a member sent "pay" again because the first reply felt slow, and Datta spent an afternoon getting the money back. Sayali's rule for the agent is short: it never takes money without the member's yes, and one yes pays once.
The agent from chapter 6 cannot keep that rule. Since chapter 3 the loop runs every call the model asks for, and pay_booking is one of them. "Pay for it from my wallet" makes the model ask for pay_booking, the loop runs it, and the money is gone before anyone was asked. A sentence in the system message ("always ask first") would not change that: the model only writes calls, and your loop runs them. The stop has to be in your code.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
approval.ts: which calls wait, and what to ask
In src/agent/approval.ts, write needsApproval(call): true when call.name is in APPROVAL_TOOLS, else false. Then question(ctx, pending): pending is a list of { id, name, arguments } whose arguments hold a booking_id. Look the member's bookings up once with listMyBookings(ctx.api). For each call say 'pay ₹<total> for booking <id> (<name>: <seats_booked>) from your Parda wallet', or 'pay for booking <id>' when the booking is not in the list; join them with ' and ' and return 'Shall I <them>? Answer yes or no.'
The Check asks needsApproval about pay_booking (true) and book_seats, find_showtimes and list_my_bookings (false), asks your question about Neha's pending booking of E1 and E2 ("Shall I pay ₹400 for booking <id> (Ghar Wapsi Express, Sat 7 Nov, 6:45 pm: E1, E2) from your Parda wallet? Answer yes or no."), about booking 1 (Pranav's) and about both at once. Then, through the app, Neha books and says "Pay for it from my wallet": the agent must stop with the status needs_approval and your question, and nothing may be paid.
- Task 2
approval.ts: wait, then answer the calls
In src/agent/approval.ts, write pause(conversationId, pending, ask): set the conversation's pending column to { calls: pending, question: ask }. Then answersFor(ctx, pending, approved, reason = DECLINED): a tool message for each call of pending.calls, in order, { role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) }, where result is await runTool(ctx, call.name, call.arguments) when approved and { error: reason } when not.
The Check pauses a conversation (the pending column), answers a pay_booking call with no (the DECLINED error, then a reason of its own, and the booking still pending) and with yes (the tool's result, and the booking confirmed).
- Task 3 · interview question
Interview: approval.ts, answered once
In src/agent/approval.ts, write takePending(conversationId), in one transaction: lock the conversation's row (a select with .for('update')), take its pending (set the column to null), and return what was pending, or null when nothing was waiting.
The Check takes a pending (returned, then gone; a second call gives null). Then a transaction of its own locks the conversation, takes the calls and stays open while your takePending runs as the second Yes; it commits, and your call must return null. Then, through the app, Neha books, asks to pay and sends two POST /agent/approve at the same moment: one 200, one 409, and exactly one payment.
Nikhil will ask: Neha taps Yes twice, and two requests reach the API in the same second. How do you make sure her wallet pays once?
What you will learn
- Human approval: which calls wait for a person, decided by the tool's name.
- The question: what the member is agreeing to, in rupees, seats and show.
- Pausing a run: the waiting calls kept in a JSON column, so the answer can come minutes later, in another request.
- Answering every call: a tool message for each call id, on yes and on no, and the 400 when one is missing.
- Two Yes taps at once: the race that pays twice, for real, and the row lock (SELECT ... FOR UPDATE) that stops it.
More in Remembering
Build "Human approval" yourself
Parda members want to book by chat. You build the agent that finds the show, holds the seats through Parda's own API, and asks before it pays. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI booking agent project (TypeScript) are free. This one comes with the all labs plan.