Project: Draft Replies for a Tea Inbox

Your ask() now does everything a careful call needs. It reports each reply’s token usage, says when a reply was cut off, retries rate-limited requests with growing waits, and stops an attempt that takes too long. So far, though, you’ve only seen those pieces work on three emails, one lesson at a time.

In this project, you’ll put them together in one script that drafts a reply to every email in Leaf & Kettle Tea’s inbox. You’ll write it first in the next exercise, where tests check each step against a stand-in model. Then you’ll bring it into your own project, check it, and run it for about a cent. Start with what the shop needs from it.

The Brief

Every morning, someone at Leaf & Kettle Tea works through the same kinds of email: a parcel that hasn’t arrived, a question about caffeine, and a café asking about wholesale. They’d like a script that drafts a reply to each one, so their job becomes reading, fixing, and sending instead of writing from scratch.

The script drafts replies and never sends them. A model predicts a likely reply without looking up the order, the stock, or the shop’s policies, so every draft goes to a person who checks it before anything reaches a customer.

Here’s what the finished script, draft-inbox.js, has to do:

  • Draft a reply to every email in data/inbox.json through ask(), and print each draft with its token usage and its cost in cents.
  • Total the cost of reported usage in cents, including drafts that were held back, because the provider bills every reply it generates, cut off or not.
  • Hold back a cut-off or empty draft. Print a line that flags it, and never save it.
  • Save the finished drafts to drafts.json, for the person who reviews them. A test run saves to its own file.
  • Wait out a rate limit or a server error and stop a slow call. Those are jobs for ask(), so the script adds no retries of its own.
  • Keep going after one failed email, and stop when the next email fails the same way. Before it stops, save what it drafted, list the emails it never tried, and end with a clear message instead of Node.js’s uncaught error.

The inbox in data/inbox.json contains a dozen emails, each with an id, a from, a subject, and a body:

id From Subject
1 Priya Raman Caffeine in the sencha?
2 Marcus Bell, Corner Cup Café Wholesale prices
3 Hannah Okafor Parcel still not here
4 Diego Fernández Pedido incompleto
5 Aisha Khan Gift card went to me, not my sister
6 Tom Nguyen Gyokuro tastes bitter
7 Grace Whitfield Dented tin, lid won’t close
8 Kenji Watanabe Pausing my subscription
9 Olivia Brooks Nuts in the masala chai?
10 Samuel Adeyemi Wrong address on my order
11 Mei Lin Chen Is last year’s oolong still OK?
12 Laura Simmons, Brightline Dental Holiday gift boxes for our team

The first three are the emails you’ve drafted replies to throughout the course. Diego’s is a complaint written in Spanish about a missing tin, and the shop wants its reply in Spanish too.

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

Become a Member to join the conversation.