Voice Pipeline

medium10 minvoice-pipeline.ts

You want to answer a spoken question with a spoken reply, using your Cogitator agent in the middle.

What it shows

  • Combine OpenAISTT, an agent runner and OpenAITTS in VoicePipeline

The code

voice-pipeline.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { OpenAISTT, OpenAITTS, VoicePipeline, createCogitatorRunner } from '@cogitator-ai/voice';

const openaiKey = process.env.OPENAI_API_KEY;
const googleKey = process.env.GOOGLE_API_KEY;
if (!openaiKey || !googleKey) throw new Error('Set OPENAI_API_KEY and GOOGLE_API_KEY');

const stt = new OpenAISTT({ apiKey: openaiKey });
const tts = new OpenAITTS({ apiKey: openaiKey, voice: 'coral' });

const cog = new Cogitator({ llm: { providers: { google: { apiKey: googleKey } } } });
const agent = new Agent({
  name: 'voice-assistant',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You are a voice assistant. Answer in one short sentence.',
});

const pipeline = new VoicePipeline({ stt, tts, agent: createCogitatorRunner(cog, agent) });

const question = await tts.synthesize('What is the capital of France?', { format: 'wav' });
const result = await pipeline.process(question, { sessionId: 'example' });

console.log(`Heard:   "${result.transcript}"`);
console.log(`Replied: "${result.response}" (${result.audio.length} bytes of MP3)`);

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/voice openai
EnvironmentOPENAI_API_KEYGOOGLE_API_KEY
run · bash
OPENAI_API_KEY=your-key GOOGLE_API_KEY=your-key npx tsx voice-pipeline.ts

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

repository · bash
npx tsx examples/voice/01-pipeline.ts

Learn more