Command Palette

Search for a command to run...

Hectal
Phase 1: Calling a Model over HTTP
AI Frontend · Exercise 6 of 6 Previous
Intermediate

Write a Safe Route

Write the route handler for /api/chat. The checks call it directly with different requests. A valid request must return status 200 and JSON { model, roles }, where model is always provider/model-name and roles is the list of roles sent to the provider, joined by commas. The provider call is already written for you (callProvider).

Rules: the body must be JSON (else 400); read the body as text first and answer 413 when it is longer than 4000 characters; messages must be a non-empty array of at most 20 items; each item needs the role user or assistant and a non-empty string content of at most 500 characters; the last message must be from the user; otherwise 400. Ignore any model or system field in the body. Add the system message yourself: the provider must receive system,user for a single question.

What Check tests

  1. A valid question returns 200 with roles system,user
  2. A model sent by the browser is ignored
  3. A system message from the browser is refused with 400
  4. An empty list, text that is not JSON and null all get 400
  5. A message of 600 characters gets 400, and the last turn must be the user's
  6. A body longer than 4000 characters gets 413

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.