Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Confirm Before Changing a Booking

In this video, Amara asked the booking assistant to move booking LF-2417 to Sunday morning’s LF103 sailing and add her brother. With the previous lesson’s code, the change ran the moment the model asked for it. Now your code stops at the change, shows her exactly what will change, and runs it only after she types yes.

Key Points

  • A change shouldn’t run before the passenger sees it. The model’s arguments are its reading of the passenger’s message. Even when your checks pass, a misreading, such as four passengers instead of three, would change the booking as quickly as a correct one.
  • The pause belongs in your code, not the prompt. Instructions that tell the model to ask first are text the model reads. The model would write the question and judge the answer, and your code couldn’t tell whether a yes ever came.
  • Check first, then ask, then check again. A new planChange() holds the checks, and both describeChange() and changeBooking() call it. A change the data can’t allow comes back as an error result before the passenger is asked anything. Moving LF-2417 to an earlier sailing (LF101) with a car, for example, returns No vehicle spaces left on LF101. After the yes, changeBooking() runs the checks again, because the data can change while the passenger decides.
  • Describe the change from the call’s own arguments. describeChange() builds the description from the arguments your code will run and the booking data, so what the passenger approves is exactly what runs:
Language: Program Output
Change booking LF-2417:
  sailing: LF102, 2026-12-05 14:30 → LF103, 2026-12-06 08:00
  passengers: 2 → 3
  • Wait for a yes. This script’s confirm() uses Node’s readline module to print the description and wait for a typed line at the terminal. In a web app, the server would keep the held call, the chat would show a yes and a no, and a yes would run that exact call, not a new one from the model.
  • Anything but a yes goes back as an error result. If the answer isn’t yes, the code throws inside the try block, so your catch block sends The passenger didn't confirm. Nothing changed. back as the call’s error result, with its ID. It’s marked as an error because the call didn’t do what the model asked. Every call still gets a result.
  • A yes in the chat doesn’t get around the gate. In this run, the model told Amara her booking was still on LF102 for two passengers and asked whether to move it. If she answers yes in the chat, the model makes a new changeBooking() call, which meets the gate again. Your own runs will word their answers differently.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your booking assistant’s tools now act only for the signed-in passenger, and they refuse changes the data can’t allow. In this video, you’ll make the assistant stop before it changes a booking and wait for the passenger’s yes.

00:14 Here’s Amara with a request. She wants to move her booking to the Sunday morning sailing and add her brother, so there are three of them. The model looks the booking up, then asks for a change with the new sailing and three passengers.

00:27 Your checks pass, and the change runs right away.

00:31 The reply says she’s all set. But Amara never saw what would change until it had. Those arguments are the model’s reading of her message. If it had misread her as four passengers, that change would have run as quickly.

00:46 You could tell the model, in its instructions, to ask first. But instructions are text the model reads, not a check your code makes. The model would write the question and judge the answer, and your code couldn’t tell whether a yes ever came.

01:00 So the pause goes in your code.

01:04 Here’s the loop. It runs every call the model asks for. Now the change tool gets a gate in front of it. When a call to changeBooking arrives, your code holds it at the gate.

01:16 First, it runs the same checks as the change itself. If she’d asked for an earlier sailing with a car, which has no vehicle space, the call would be refused before she’s asked anything.

01:27 Next, it describes the change, from the call’s arguments and the booking data. The sailing goes from Saturday afternoon to Sunday morning, and the passengers from two to three.

01:38 Your code writes this from the same arguments it will run, so what Amara approves is exactly what runs.

01:45 Then it shows her the description and waits. This course’s script waits at the terminal for a typed line. In a web app, the server would keep the held call, and a Yes button would run that exact call.

01:57 That’s all you need about the web version for now.

02:01 Here’s the gate in your loop’s code, inside the try block, before the line that runs a tool. An if statement checks the call’s name. For changeBooking, describeChange runs the checks and builds the description.

02:14 Then your own confirm function prints it and waits for an answer. In tools.js, a new planChange function holds the checks, and both describeChange and changeBooking call it.

02:26 So changeBooking checks again after her yes, since seats can go while she decides. The confirm function uses Node’s readline module to wait for a typed line.

02:37 If the answer isn’t yes, the code throws an error. Your turn: pause the video and predict what goes back to the model when Amara says no.

02:48 The thrown error lands in the catch block you already have. So the model gets an error result, with the call’s ID, saying the passenger didn’t confirm and nothing changed.

02:58 It’s an error because the call didn’t do what the model asked, and the result should say so.

03:04 Run it, and Amara types yes. Only now does the change tool run. Its result, the booking on Sunday morning for three, goes back with the call’s ID, and the model tells her it’s done.

03:18 Run it again, and she types no. The change never runs. The model reads the error result, tells her nothing changed, and asks again. A yes in the chat would only bring a new call back to your gate.

03:33 So the model asks for a change, and your code shows exactly what will change and runs it only after the passenger says yes.

03:42 Next, in the lesson Choose the Assistant’s Tools and Limits, you’ll decide which tools the assistant should have at all, and the limits around them.

Become a Member to join the conversation.