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
- A valid question returns 200 with
rolessystem,user - A
modelsent by the browser is ignored - A
systemmessage from the browser is refused with 400 - An empty list, text that is not JSON and
nullall get 400 - A message of 600 characters gets 400, and the last turn must be the user's
- 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.
Your page
Console
Nothing logged yet.