Command Palette

Search for a command to run...

Hectal
Phase 4: Conversation State
AI Frontend · Exercise 4 of 5 PreviousNext
Intermediate

A Safe Reducer

The chat state is handled by reduce(state, action). Four buttons drive it: #send (a question), #token (a piece for the latest answer), #stale (a piece for an answer that is no longer current) and #stop.

Make the reducer refuse what doesn't fit. A send while the chat is busy is ignored. A token or stop whose id is not state.currentId is ignored. After stop, the answer's id is no longer current, so a late token cannot change it.

#status shows Status: <status>, #count the number of messages and #text the text of the last answer.

What Check tests

  1. Before anything: Status: idle and no messages
  2. A send adds two messages and the status is waiting
  3. A second send while busy is ignored
  4. A token adds its text to the last answer
  5. A token for an old id is ignored
  6. After stop, the status is done and a late token changes nothing

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.