Build a voice agent (TypeScript) · Chapter 12 of 21
Turn-taking and silence
By the end of this chapter you can tell a caller's "haan" from a real turn, keep a silent line from staying open, end a call politely when nobody is there, and stay quiet while a caller goes to find a number. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real time
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Build a voice agent (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Bolo can now hear the caller while it talks, stop when they cut in, and keep the conversation true to what they heard. That makes it sensitive, and Kavita has seen what a sensitive agent gets wrong. "People make noises while you talk. Meena says haan, achha, the whole time. That is her saying go on, not stop." She has two more: "Sometimes the line connects and nobody is there: a pocket dial, a bad network. I say hello twice, then I put the phone down. And when someone says ek minute, I wait. I don't keep asking if they're there."
Bolo does none of that yet. On Meena's call it stops at her first "haan", answers it as if it were a question, and she never hears how payment works. On a silent line it greets, then waits, and waits: the line stays open for the full 90 seconds until the phone network gives up. Rashmi does the sum: an open line is a phone channel and a speech-to-text stream, paid by the minute, doing nothing, and a real caller in the queue behind it.
None of this is about words. It is about who has the floor: whose turn it is to speak, and what silence means.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
backchannel.ts: "haan" is not a turn
Write and export isBackchannel(words: string[]): boolean. It is true when the words are only a listening noise: one or two words, every one of them in this list: haan, haanji, ji, achha, accha, acha, hmm, hm, ok, okay, theek, right, yes, yeah. It is false for no words at all, and for anything with another word in it ("haan ek minute" is a real turn).
The given agent.ts asks it about what the caller says while Bolo talks. Meena's call (meena-first-time) through agent.ts must succeed with no clears: Bolo never stops for her "haan" and "achha".
- Task 2
silence.ts: the silent line
Write and export REPROMPT ('Hello? Kya aap mujhe sun pa rahe hain?'), LEAVING ('Lagta hai line mein dikkat hai. Main call rakh rahi hoon. Dhanyavaad!') and class SilenceWatch, made with new SilenceWatch({ say, hangup, busy }, waitMs = 6000). quiet() starts the clock again; when waitMs pass: if busy() is true, it starts the clock again; else, while there have been fewer than 2 reprompts since the caller last spoke, it awaits say(REPROMPT) and calls quiet(); else it awaits say(LEAVING) and only then calls hangup(). heard() stops the clock and sets the count of reprompts back to 0. hold(ms) makes the next wait ms long, once. stop() stops the clock for good.
The silent line (silent-line) through agent.ts must end by Bolo's hang-up within 45 s, after two reprompts and the whole goodbye.
- Task 3 · interview question
Interview: hold.ts, "ek minute"
Write and export holdFor(text: string): number. It gives 20000 (20 seconds) when the caller asks Bolo to wait: "ek minute", "one minute", "just a second", "hold on", "ruko" or "rukiye", or any word that starts with "dhoondh" (looking for something). It gives 0 for everything else: "kitne minute lagenge" is a question, not a hold.
The given agent.ts passes a wait above 0 to your SilenceWatch's hold(ms). Mr Joshi (joshi-hold, a 7 s hold) and a caller who takes 15 s to find her number must hear no reprompt while they look, and both calls must succeed.
What you will learn
- Backchannels: the "haan" and "achha" that mean I am listening, and why they are not turns.
- A silent caller: reprompt after six seconds, say goodbye after two, hang up yourself.
- Silence that is not silence: Bolo thinking, Bolo talking, the caller mid-sentence.
- The goodbye has to be heard: why you hang up after the goodbye has played, not after you sent it.
- Holds: "ek minute" means wait longer, once.
More in Real time
Build "Turn-taking and silence" yourself
Kickstart services scooters at the door in Nagpur, and its phone line gets 600 calls a day in Hindi, English and Hinglish. You build Bolo, its voice agent, and measure every call: who it understood, how long callers waited and who it cut off. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Build a voice agent (TypeScript) are free. This one comes with the all labs plan.