Voice Agent Server

advanced15 minvoice-agent.ts

Browsers or phones should stream audio to your agent and hear it answer.

What it shows

  • Serve a VoiceAgent over WebSocket with Deepgram STT, ElevenLabs TTS and energy-based VAD

The code

voice-agent.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { DeepgramSTT, ElevenLabsTTS, EnergyVAD, VoiceAgent, createCogitatorRunner } from '@cogitator-ai/voice';

const googleKey = process.env.GOOGLE_API_KEY;
const deepgramKey = process.env.DEEPGRAM_API_KEY;
const elevenLabsKey = process.env.ELEVENLABS_API_KEY;
if (!googleKey || !deepgramKey || !elevenLabsKey) {
  throw new Error('Set GOOGLE_API_KEY, DEEPGRAM_API_KEY and ELEVENLABS_API_KEY');
}

const cog = new Cogitator({
  llm: { providers: { google: { apiKey: googleKey } } },
  memory: { adapter: 'memory' },
});

const agent = new Agent({
  name: 'voice-assistant',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You are a friendly voice assistant. Answer in one or two short sentences.',
});

const voiceAgent = new VoiceAgent({
  agent: createCogitatorRunner(cog, agent),
  mode: 'pipeline',
  stt: new DeepgramSTT({ apiKey: deepgramKey, model: 'nova-3', sampleRate: 16000 }),
  tts: new ElevenLabsTTS({ apiKey: elevenLabsKey }),
  vad: new EnergyVAD({ threshold: 0.02 }),
  transport: { path: '/voice', maxConnections: 10 },
});

voiceAgent.on('session_start', (id) => console.log(`session started: ${id}`));
voiceAgent.on('session_end', (id) => console.log(`session ended: ${id}`));
voiceAgent.on('error', (error) => console.error(error.message));

await voiceAgent.listen(8080);
console.log('Voice agent on ws://localhost:8080/voice — send PCM16 16kHz mono frames');

process.on('SIGINT', async () => {
  await voiceAgent.close();
  await cog.close();
  process.exit(0);
});

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/voice
EnvironmentGOOGLE_API_KEYDEEPGRAM_API_KEYELEVENLABS_API_KEY
run · bash
GOOGLE_API_KEY=your-key DEEPGRAM_API_KEY=your-key ELEVENLABS_API_KEY=your-key npx tsx voice-agent.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/voice/03-voice-agent.ts

Learn more