Example

Map availability to an action, not to a boolean

Run the availability view over five browser states and see each one become a level and an action, with an unknown state becoming an error rather than a guess.

“Built in” is not a boolean. The browser can offer the model now, offer it after a download, be part way through that download, refuse it on this device, or answer with something the extension has never seen. The chapter’s claim is that each of those is a different situation with a different next step, and that collapsing them into true or false is how a feature ends up showing a button that cannot work.

Run it

Save this as availability.mjs and run it with Node 22 or later. It uses no browser API: the view is a pure function, so the decision can be tested without a browser at all.

// Chapter 2: availability is a decision, not a boolean.

function availabilityView(state) {
  switch (state) {
    case "available":
      return { level: "ready", action: "Create session" };
    case "downloadable":
      return { level: "setup", action: "Download and enable" };
    case "downloading":
      return { level: "waiting", action: null };
    case "unavailable":
      return { level: "blocked", action: null };
    default:
      return { level: "error", action: null };
  }
}

for (const state of [
  "available",
  "downloadable",
  "downloading",
  "unavailable",
  "something-new",
]) {
  const view = availabilityView(state);
  console.log(`${state.padEnd(14)} -> ${view.level.padEnd(8)} ${view.action ?? "(no action)"}`);
}

Expected outcome

available      -> ready    Create session
downloadable   -> setup    Download and enable
downloading    -> waiting  (no action)
unavailable    -> blocked  (no action)
something-new  -> error    (no action)

Two of the five states have no action, and that is the point: a state that cannot be acted on should not be given a button. The last line is the interesting one. A state the code has never seen is not treated as available and not treated as unavailable; it is an error, because an unknown state is a fact about the extension, not about the browser.

The view

The chapter’s own function, in full:

function availabilityView(state) {
  switch (state) {
    case "available":
      return { level: "ready", action: "Create session" };
    case "downloadable":
      return { level: "setup", action: "Download and enable" };
    case "downloading":
      return { level: "waiting", action: null };
    case "unavailable":
      return { level: "blocked", action: null };
    default:
      return { level: "error", action: null };
  }

The switch has a default arm, and that arm is the design. Every state the browser can report is named, and anything else is routed to a level that offers no action, so an unrecognised state cannot be mistaken for a working one.

Mechanism and limitations

Documented: the availability states, the LanguageModel.availability call and the session options are in the chapter’s own text, and the view above is that text’s code. The chapter records an observed session configuration, a quota of 9,216 with usage zero, and is explicit that it is one session’s configuration rather than a fixed browser constant.

Observed: the run above. The five states produced five levels, two of them with an action, and the unrecognised state produced error.

Not shown. The states are the ones the chapter names; the real browser API may report others, and the default arm is what catches them. A quota read as zero usage, as here, says nothing about the quota a different device, browser version or model would report, and the view does not model a download that fails part way through. Nothing here shows that a “Download and enable” button is the right interface; it shows only that the state and the action are separate.

Read the chapter .

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.