npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@reaatech/voice-agent-telephony

v0.1.0

Published

Twilio Media Streams WebSocket handler for voice AI agents with barge-in detection and full call lifecycle management

Readme

@reaatech/voice-agent-telephony

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Multi-provider telephony WebSocket handler for voice AI agents, supporting Twilio, Telnyx, SignalWire, and Vonage. Handles bidirectional streaming protocols: start/media/stop/mark/dtmf events, barge-in detection, audio buffering, and base64 encoding/decoding.

Installation

npm install @reaatech/voice-agent-telephony
pnpm add @reaatech/voice-agent-telephony

Feature Overview

  • Twilio Media Streams protocol — Full support for start, media, stop, mark, and DTMF events
  • Barge-in detection — Configurable interruption during TTS playback based on speech duration and confidence
  • Audio send/clear — Send TTS audio chunks and clear the output buffer via Twilio clear event
  • Mark tracking — Send and track Twilio mark events for audio position synchronization
  • Call lifecycle — CallSid and StreamSid tracking, connected/disconnected events, graceful close
  • Base64 encode/decode — Static utility methods for Twilio's base64 audio payload format
  • Typed message interfaces — Full TypeScript types for all Twilio inbound and outbound messages
  • Multi-provider transport — Telnyx, SignalWire, and Vonage adapters implementing the Transport interface
  • Reconnection-safe — Handles already-open WebSocket connections gracefully

Quick Start

import { createTwilioHandler } from '@reaatech/voice-agent-telephony';
import WebSocket from 'ws';

// In your WebSocket server
wss.on('connection', async (ws) => {
  const handler = createTwilioHandler({
    bargeInEnabled: true,
    minSpeechDuration: 300,
    confidenceThreshold: 0.7,
    silenceThreshold: 0.3,
  });

  await handler.acceptConnection(ws);

  handler.on('audio:received', (chunk) => {
    // Forward to STT provider
    sttProvider.streamAudio(chunk);
  });

  handler.on('barge-in:detected', () => {
    // Stop TTS, clear audio buffer
    ttsProvider.cancel();
    handler.clearAudio();
  });

  handler.on('call:end', () => {
    handler.close();
  });
});

API Reference

TwilioMediaStreamHandler

class TwilioMediaStreamHandler extends EventEmitter {
  constructor(config?: Partial<TwilioHandlerConfig>);

  // Connection
  acceptConnection(ws: WebSocket): Promise<void>;

  // Audio
  sendAudio(chunk: AudioChunk): void;
  clearAudio(): Promise<void>;

  // Mark tracking
  sendMark(): Promise<string>;

  // TTS state
  setTTSPlaying(playing: boolean): void;
  isTTSActive(): boolean;

  // Session info
  getCallSid(): string | null;
  getStreamSid(): string | null;

  // Barge-in
  isBargeInEnabled(): boolean;
  getBargeInThresholds(): { minSpeechDuration, confidenceThreshold, silenceThreshold };
  onInterimTranscript(transcript: string, confidence: number): void;

  // Cleanup
  close(): Promise<void>;

  // Static utilities
  static encodeForTwilio(buffer: Buffer): string;
  static decodeFromTwilio(base64: string): Buffer;
}

TwilioHandlerConfig

| Property | Type | Default | Description | |----------|------|---------|-------------| | bargeInEnabled | boolean | false | Enable barge-in detection during TTS | | minSpeechDuration | number | 300 | Minimum speech duration in ms to trigger barge-in | | confidenceThreshold | number | 0.7 | Minimum STT confidence (0–1) to count as speech | | silenceThreshold | number | 0.3 | Reserved for future silence-based interruption |

Events

| Event | Payload | Description | |-------|---------|-------------| | connected | — | WebSocket connection opened | | disconnected | — | WebSocket connection closed | | call:start | { callSid, streamSid, codec, customParameters } | Inbound call started | | call:end | { callSid } | Inbound call ended | | audio:received | AudioChunk | Base64-decoded audio buffer from caller | | barge-in:detected | BargeInEvent | User interrupted during TTS playback | | mark:played | { streamSid } | Twilio confirmed mark was played | | dtmf:received | { digit, streamSid } | DTMF digit pressed by caller | | error | Error | WebSocket or protocol error |

Twilio Message Types

type TwilioMessage =
  | TwilioStartMessage
  | TwilioMediaMessage
  | TwilioStopMessage
  | TwilioMarkMessage
  | TwilioDTMFMessage;

interface TwilioOutboundMessage {
  event: 'media' | 'clear' | 'mark' | 'start';
  streamSid?: string;
  media?: { payload: string };
  mark?: { name: string };
}

Transport Interface (from @reaatech/voice-agent-core)

All telephony handlers implement the Transport interface:

interface Transport extends EventEmitter {
  readonly name: string;
  acceptConnection(connection: unknown): Promise<void>;
  sendAudio(chunk: AudioChunk): void;
  clearAudio(): Promise<void>;
  getSessionId(): string | null;
  close(): Promise<void>;
}

Provider Factory

import { createTransport } from '@reaatech/voice-agent-telephony';

const transport = createTransport({
  type: 'twilio', // 'twilio' | 'telnyx' | 'signalwire' | 'vonage'
  bargeInEnabled: true,
});

Usage Patterns

Sending TTS Audio to Twilio

for await (const chunk of ttsProvider.synthesize(text, config)) {
  handler.sendAudio(chunk);
}

// Signal TTS is done
handler.setTTSPlaying(false);

Barge-In Integration

// Configure barge-in on handler creation
const handler = createTwilioHandler({
  bargeInEnabled: true,
  minSpeechDuration: 300,
  confidenceThreshold: 0.7,
});

// Feed interim transcripts from STT into barge-in detector
sttProvider.onUtterance((utterance) => {
  if (!utterance.isFinal) {
    handler.onInterimTranscript(utterance.transcript, utterance.confidence);
  }
});

// Handle barge-in
handler.on('barge-in:detected', (event) => {
  ttsProvider.cancel();       // Stop current TTS
  handler.clearAudio();       // Clear Twilio audio buffer
  handler.setTTSPlaying(false);
});

Mark Events for Audio Position

handler.setTTSPlaying(true);

// Insert a mark before sending audio
const markName = await handler.sendMark();

handler.on('mark:played', ({ streamSid }) => {
  console.log(`Mark ${markName} was played to stream ${streamSid}`);
});

// Send audio...
handler.sendAudio(chunk);

Full Call Lifecycle

handler.on('call:start', ({ callSid, streamSid, customParameters }) => {
  console.log(`Call ${callSid} started`);
  // Create session, connect providers, start pipeline
});

handler.on('call:end', ({ callSid }) => {
  console.log(`Call ${callSid} ended`);
  // Clean up session, disconnect providers
});

Related Packages

License

MIT