Add askForJson() to the Model Module

In the last lesson, you sent the appointment record’s schema with one request you built by hand in schema-request.js. That script named your provider’s endpoint, its fields, and a model ID, and a feature script shouldn’t have to know any of those. Meanwhile, read-notes.js still calls ask() and guards the parse itself.

In this lesson, you’ll give the model/ module a second function, askForJson(), that takes a schema and sends it through whichever provider you use. You’ll decide what it returns when a reply can’t become a record, and you’ll make the intake script call it and nothing else. Start with that return value, since it’s what every feature will rely on.

Decide What askForJson() Returns

For each note, the booking script needs either a record it can use or a reason to hold the note back. Your guard in read-notes.js already knows three reasons. A reply cut off at the output limit, a reply with stray text, and a value that isn’t an object all get held back.

Two more cases need names of their own. When a provider declines a request and sends its refusal signal, ask() returns a reply with cutOff set to false and, usually, reply.text set to "". The guard holds that back only because the text doesn’t parse. askForJson() will name it instead, as a refusal, so the front desk later sees “refused” rather than a confusing parse error.

The other case is a reply that stops early for a reason other than the output limit. The OpenAI version of the provider switch below shows one. cutOff is false then too, so if the partial text happens to parse, your guard lets it through. To catch it, askForJson() parses only a reply that finished normally and names any other early stop as unfinished, with the provider’s reason.

In all, askForJson() returns an object with three properties, the same for every provider:

  • record: The parsed record, or null when there isn’t one
  • failure: null for a record, or "cut off", "refused", "unfinished: " plus the provider’s reason, "not JSON", or "not an object"
  • usage: The reply’s token counts, so the script can still price every call

A schema doesn’t make a parse failure impossible, because it only constrains an answer that finishes. A reply the limit cuts off is unfinished JSON, and a refusal isn’t the schema’s shape at all.

So askForJson() checks cutOff, the refusal, and whether the reply finished before it calls JSON.parse(), and the parse still sits in a try block for anything else.

Open model/index.js. First, let ask() take an optional schema and pass it to the provider file with the rest of the request. Add it in two places, the parameters and the request object:

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.