AI CHOREOGRAPHY / THE AI / LEVEL 3 OF 5

From a sentence
to a routine.

Typing in VR and typing on a computer call the same function, generateFromText. It writes a small ai record into the shared Coach state, so both headsets always know whether the AI is thinking, ready, or whether the request was cancelled. Here, A types a prompt while B dances; the network between them adds a little delay.

thinkingprompt and request id shared; the old routine keeps playing
→
the AI answers5–20 s later, checked by validateCoachReply
→
still current?if the id changed meanwhile, the answer is thrown away
→
readyeight new poses, starting from pose 1 on both headsets

Two headsets

Headset A · types the prompt

Headset B · just dancing

Do things

the AI's answer here is a saved reply

What happened

coach.mode on A

shared with B through send('mode')

Try: press GO, then press DANCE on B before the answer arrives. Or press GO twice quickly. Watch the id.

The real code

coach.js

Check first (empty text, or a recording or round in progress, is refused). Remember the previous routine. Share thinking with the prompt. Ask, validate, and only if coach.mode.ai.id is still this request's id, share ready with the new poses. On any failure, put the previous routine back.


      

Starting on the first pose

coach.js

startK is the pose number at which the new routine begins. Subtracting it makes every headset play pose 1 first, whatever the time is.


      
    

What you should be able to explain

  1. One function, two inputs. The VR keyboard and the page's panel both call generateFromText(text, setStatus); only where the status is shown differs. So both give exactly the same result.
  2. The state is shared, not the request. Only A talks to the AI. B learns everything from coach.mode.ai: the prompt for its board, "thinking", and finally the poses.
  3. No dead time. While the AI thinks, the previous routine keeps playing; START is hidden so a round cannot begin with a routine about to change.
  4. Stale answers. Every request has a unique id. If someone presses DANCE or sends a new prompt, the id in the shared state changes, and the late answer is ignored instead of overwriting the newer choice.
  5. Starting together. startK = kNow + 1 and poseAt(k − startK) make the routine begin at its first pose on the next pose boundary, on both headsets.

Exercises: ① Tick "the AI fails" and press GO: what does each headset dance afterwards? ② Press GO, then GO again 2 s later: which answer is used, and why? ③ What would go wrong without startK?