Build a voice agent (TypeScript) · Chapter 17 of 21

The call server

By the end of this chapter you can put Bolo on a phone number the way telephony providers do it: run a WebSocket server, let the line call it, give every call its own Bolo, take many calls at once, speak the phone network's mu-law, and close everything a call opened when it ends. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 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

Until now Bolo has dialled in: runCall('pandey-price') opened a socket to the line and asked for a caller. That is handy in a lab, and it is not how a phone number works. When somebody rings Kickstart's number, the telephony provider (Exotel, Twilio, Plivo and others all work this way) opens a WebSocket to a server you run and streams the call to it. Your code does not choose who calls or when. It waits, and answers.

Kavita puts it plainly: "At nine in the morning six people ring at once. Bolo can't say 'one at a time, please'." Tejas adds his own rule: "And one caller's words must never end up in another caller's call. Not the transcript, not the booking."

So Bolo needs a call server: a program that listens on a port, treats every connection as one call, gives each call its own speech-to-text, text-to-speech and history, and cleans up when the call ends. Everything else Bolo does stays exactly the same.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    first-dial.ts: the line dials you

    Start a WebSocketServer (from ws) on port 9170, then ask the line to call it: POST VOICE/v1/line/dial with { caller: 'pandey-price', url: 'ws://127.0.0.1:9170', codec: 'mulaw' }. On the connection, print connected for the connected message and start: CALL_ID, ENCODING at RATE Hz for the start message; count the media messages and send { event: 'hangup' } at the 150th. When the socket closes, print CALL_ID: N frames of B bytes, S s (B: one payload decoded from base64; S with 2 decimals) and close the server.

  2. Task 2

    call-server.ts: Bolo's call server

    Write and export accept(ws) (a Line on the socket, resolved at the start message with id and codec set; every later message to line.handle, close to line.stop), startServer(port, opts) ({ port, active(), close() }: every connection gets its own listen() and speaker(), runs converse(line, { stt, tts }, opts), then closes both) and dial(caller, port, codec) (POST /v1/line/dial; the call_id).

    Under if (import.meta.main), serve on 9171, dial 'pandey-price' on the mu-law line and print CALL_ID pandey-price CODEC: success, latency A / B / C ms, then active calls: N.

  3. Task 3 · interview question

    Interview: six-calls.ts, six calls at once

    Start your server on 9173 and dial the six callers in the starter at once, each with its codec. When every record is in, print CALLER CODEC ok|FAIL A/B/C ms per call, then OK/6 calls, p50 A ms, p95 B ms over N turns (nearest rank over every turn), then STT streams: N opened, M closed (from GET /lab/stt, the streams opened since you began), then active calls: N.

What you will learn

  • The telephony way round: the line dials your server (POST /v1/line/dial).
  • What arrives first: connected, then start with the call's id and audio format, then frames.
  • The mu-law line: 8 kHz, one byte a sample, 160 bytes a frame.
  • A server with ws: one connection is one call.
  • Per-call state: why nothing about a call may live outside it.
  • Cleanup: every stream a call opened is closed when it ends.

More in Production

Build The call server 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.