Command Palette

Search for a command to run...

Hectal
Phase 2: Streaming Fundamentals
AI Frontend · Exercise 2 of 4 PreviousNext
Beginner

Buffer the Events

The route /api/chat streams the answer Chai is tea. in the chat format, cut into small packets so lines arrive in pieces.

When #ask is clicked, read the stream and put the answer text into #answer. Each event ends at a blank line, and its JSON holds the next piece at choices[0].delta.content. The last event is [DONE], which is not JSON.

What Check tests

  1. #answer is empty before the click
  2. After the click, #answer shows Chai is tea.
  3. A second click clears the old answer and shows it once, not twice
  4. The code keeps a buffer between packets

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.