Pipeline Mode
Process voice through STT -> Agent -> TTS with VoicePipeline for batch processing and PipelineSession for continuous streaming conversations with VAD-driven turn detection.
Overview
Pipeline mode routes audio through a three-stage loop: Speech-to-Text, Agent processing, Text-to-Speech. This works with any Cogitator agent regardless of the underlying LLM backend.
Two classes handle different use cases:
- VoicePipeline — batch processing: send a complete audio buffer, get back transcript + response + audio
- PipelineSession — streaming: push audio chunks continuously, get events as things happen
VoicePipeline
One-shot processing for complete audio buffers.
import { VoicePipeline, OpenAISTT, OpenAITTS, createCogitatorRunner } from '@cogitator-ai/voice';
const pipeline = new VoicePipeline({
stt: new OpenAISTT({ apiKey: process.env.OPENAI_API_KEY! }),
tts: new OpenAITTS({ apiKey: process.env.OPENAI_API_KEY! }),
agent: createCogitatorRunner(cogitator, agent),
});The agent is any VoiceAgentRunner — { run(input, context?) => Promise<{ content }> }. context carries the sessionId and an abort signal. createCogitatorRunner(cogitator, agent) builds one from a Cogitator runtime and agent (memory thread voice:<sessionId> per session).
process(audio, options?)
Transcribes the audio, runs the agent, synthesizes the response. Silent input (empty transcript) skips the agent and TTS and returns empty strings and an empty buffer; an empty agent reply skips TTS. Pass { sessionId } to forward a session id to the agent.
const result = await pipeline.process(audioBuffer);
console.log(result.transcript); // what the user said
console.log(result.response); // what the agent replied
// result.audio — synthesized response as BufferTTS options
ttsOptions is passed to every synthesize / streamSynthesize call made by the pipeline and its sessions. Use it to pick the output format or a per-pipeline voice, speed or instructions:
const pipeline = new VoicePipeline({
stt: new OpenAISTT({ apiKey: process.env.OPENAI_API_KEY! }),
tts: new OpenAITTS({ apiKey: process.env.OPENAI_API_KEY! }),
agent: createCogitatorRunner(cogitator, agent),
ttsOptions: { format: 'pcm16' },
});Without ttsOptions the TTS provider's defaults apply (MP3 output). format: 'pcm16' returns raw PCM16 mono at 24kHz. VoiceAgent accepts the same ttsOptions in pipeline mode.
openai is an optional peer dependency: install it only when you use OpenAISTT / OpenAITTS. They load it on the first transcribe / synthesize call, which throws with an install hint if it is missing.
createSession(options?)
Creates a PipelineSession for streaming use. Pass { sessionId } to set the id the agent sees (default: a random id). See below.
PipelineSession
Streaming mode for continuous conversations. Push audio chunks as they arrive, receive events for each stage of processing.
const session = pipeline.createSession();
session.id; // passed to the agent as context.sessionId
session.currentState; // 'idle' | 'listening' | 'processing' | 'speaking'Pushing Audio
// push PCM16 audio chunks as they arrive from the microphone
session.pushAudio(pcm16Chunk);If the pipeline has a VAD provider, the session automatically detects when speech starts and ends. Without VAD, it streams to STT immediately and the turn is committed with endAudio().
session.endAudio(); // commit buffered audio (no-VAD mode)
await session.sendText('Hi there'); // run a turn from text, bypassing STTEvents
session.on('speech_start', () => {
console.log('User started speaking');
});
session.on('speech_end', () => {
console.log('User stopped speaking, transcribing...');
});
session.on('transcript', (text: string, isFinal: boolean) => {
if (isFinal) {
console.log('Final transcript:', text);
} else {
console.log('Interim:', text);
}
});
session.on('agent_response_delta', (delta: string) => {
showCaption(delta); // a streaming runner's text as it is generated
});
session.on('agent_response', (text: string) => {
console.log('Agent:', text);
});
session.on('audio', (chunk: Buffer) => {
// streamed TTS audio — play it back to the user
playAudio(chunk);
});
session.on('turn_end', () => {
console.log('Agent finished speaking');
});
session.on('error', (error: Error) => {
console.error('Session error:', error);
});| Event | Payload | Description |
|---|---|---|
speech_start | - | VAD detected speech beginning (VAD only) |
speech_end | - | VAD detected speech ending (VAD only) |
transcript | (text, isFinal) | STT result (interim or final) |
agent_response | (text) | Agent's whole text response |
agent_response_delta | (delta) | A piece of a streamed response |
audio | (chunk: Buffer) | Streamed TTS audio chunk |
turn_end | - | Response fully spoken (or empty) |
error | (error: Error) | Processing error (STT, VAD, agent, TTS) |
Streaming replies
When the runner has stream(input, context), as createCogitatorRunner() does, the session speaks the answer while the model is still generating it. The text deltas are split into sentences and each sentence goes to tts.streamSynthesize in order, so the first audio leaves before the answer is complete.
The splitter keeps abbreviations (Dr., e.g., U.S.), initials after a capitalized word (J. R. R. Tolkien, but not vitamin C.), decimals (3.14) and list numbers at the start of a line or of the next list item (1. Open it. 2. Close it.) inside their sentence, keeps markdown marks after a stop (**Note.**) with it, splits after ?!, an ellipsis before a new sentence, CJK full stops and line breaks, flushes a tail without punctuation at the end, and cuts a piece longer than maxSentenceLength characters (default 250, at least 20) on whitespace. SentenceSplitter is exported for your own code.
agent_response_delta carries each piece of text that is spoken. agent_response carries the answer once the model has finished it, the same text run() returns (with tools, only the final model turn), which can be while its first sentences are still being spoken. Text the model says before a tool call is spoken on its own while the tool runs: createCogitatorRunner ends it with a line break. The stream ends with the final model turn, so the last sentence does not wait for what the run does after it. A middleware that rewrites a turn in afterModel without holdsStream: true is heard before its rewrite.
A runner without stream works as before: run() answers, then the whole text is synthesized at once.
A streaming runner returns a VoiceTurn, an AsyncIterable<string> of deltas that stops when context.signal aborts and may return the answer as its return value, with an optional settle({ spoken, interrupted }). The session calls settle once the turn is over (a second call returns the same promise), and starts the next streamed turn after it resolved, waiting at most settleTimeout ms (default 10 000). close() waits for it the same way.
Interruption
Interrupt the current processing at any stage - discards the STT stream, aborts the agent run (its signal fires) and stops TTS streaming. A stale turn never emits events or changes state after an interruption:
session.interrupt();After interruption, the session returns to idle and is ready for new audio input.
A streamed turn that stops early (an interruption, sendText() during a turn, a barge-in, closing, or a model or TTS error) is settled with spoken: the sentences whose audio was sent in full. A WebSocket client does not report how much of that audio it played, so keep its playback buffer short. A session with a playback knows: createSession({ playback }) takes a VoicePlayback (mark() resolving once the audio before it was played, clear() dropping what was not), the session marks the end of each spoken sentence, waits for the marks before the turn ends, clears on interruption, and settles with only the sentences that were played. A Twilio call works this way.
createSession also takes input (the format of the pushed audio, such as { encoding: 'mulaw', sampleRate: 8000 }: the STT stream is opened at that rate and gets μ-law when it lists mulaw in streamEncodings, the VAD gets decoded audio resampled to its sampleRate) and ttsOptions over the pipeline's own.
With createCogitatorRunner() the thread then holds what the user heard. A fully spoken answer leaves the thread as a plain run does. An answer interrupted while it was spoken is replaced by its spoken part, with metadata.interrupted: true. When the model was still generating, the spoken part is saved after the run's interrupt event, marked the same way. When nothing was spoken, there is no assistant message for the answer. Text said alongside tool calls stays as the run saved it. The model reads an interrupted reply with [interrupted by the user] after it.
The thread is changed only while the turn's last entry is still its active leaf: when something else wrote to the thread after the turn, such as another run on the same threadId, it is left as it is, so a newer message is never hidden. The runner fixes the thread as soon as the run saved its answer, without waiting for the run's afterRun hooks, settles the turns of one thread one after another, and starts a run once the previous run of its thread saved its answer, waiting for the rest of that run at most tailTimeout ms (a createCogitatorRunner option, default 10 000).
Barge-in
With bargeIn: true and a vad, on a VoicePipeline or a pipeline-mode VoiceAgent, speech heard while the agent is thinking or speaking stops the reply and starts listening right away, emitting speech_start (a client should drop the audio it still has queued). It is off by default, because without echo cancellation on the client the agent's own voice would interrupt it.
const pipeline = new VoicePipeline({ stt, tts, vad: new EnergyVAD(), agent, bargeIn: true });
const voiceAgent = new VoiceAgent({
mode: 'pipeline',
stt,
tts,
vad: new EnergyVAD(),
agent,
bargeIn: true,
});Closing
await session.close();Aborts any in-progress turn (without waiting for hung providers), waits for a streamed turn to settle, releases the STT stream and removes event listeners.
Pipeline with VAD
When a VAD provider is set, the session handles turn detection automatically:
import { VoicePipeline, OpenAISTT, OpenAITTS, EnergyVAD } from '@cogitator-ai/voice';
const pipeline = new VoicePipeline({
stt: new OpenAISTT({ apiKey: process.env.OPENAI_API_KEY! }),
tts: new OpenAITTS({ apiKey: process.env.OPENAI_API_KEY! }),
vad: new EnergyVAD({ threshold: 0.01, silenceDuration: 500 }),
agent: myAgent,
});
const session = pipeline.createSession();
session.on('speech_start', () => {
// VAD detected speech — session opens an STT stream
});
session.on('speech_end', () => {
// VAD detected silence — session closes STT, runs agent, streams TTS
});
// just keep pushing audio (PCM16 at the VAD's sample rate), VAD handles the rest
microphoneStream.on('data', (chunk) => {
session.pushAudio(chunk);
});VoiceAgent
VoiceAgent is the high-level class that combines pipeline/realtime processing with WebSocket transport and session management. For pipeline mode, it creates a VoicePipeline internally and manages per-client sessions.
import { VoiceAgent, OpenAISTT, OpenAITTS, EnergyVAD } from '@cogitator-ai/voice';
const voiceAgent = new VoiceAgent({
agent: createCogitatorRunner(cogitator, agent),
mode: 'pipeline',
stt: new OpenAISTT({ apiKey: process.env.OPENAI_API_KEY! }),
tts: new OpenAITTS({ apiKey: process.env.OPENAI_API_KEY! }),
vad: new EnergyVAD(),
transport: {
path: '/voice',
maxConnections: 50,
verifyClient: async (req) => isValidToken(req.headers.authorization), // false → 401; { code, message } for another status
},
});
voiceAgent.on('session_start', (id) => console.log(`Session: ${id}`));
voiceAgent.on('session_end', (id) => console.log(`Ended: ${id}`));
voiceAgent.on('error', (err) => console.error(err));
await voiceAgent.listen(8080);
console.log(`Active sessions: ${voiceAgent.activeSessions}`);
await voiceAgent.close();Use voiceAgent.attach(httpServer) instead of listen() to share an existing HTTP server; upgrades for other paths are left to other handlers (with listen() they get 404). voiceAgent.port is the bound port. Errors are only emitted when an error listener is attached — a misbehaving client cannot crash the process.
VoiceAgentConfig
| Field | Type | Required | Description |
|---|---|---|---|
agent | VoiceAgentRunner | Yes | createCogitatorRunner(...) or any { run(input, ctx) } |
mode | 'pipeline' | 'realtime' | Yes | Processing mode |
stt, tts | providers | Pipeline only | Speech-to-text and text-to-speech |
vad | VADProvider | No | Voice activity detection |
ttsOptions | TTSOptions | No | Pipeline: options for every TTS call |
bargeIn | boolean | No | Pipeline: speech during a reply stops it (default false, needs a vad) |
maxSentenceLength | number | No | Pipeline: longest streamed piece sent to TTS (default 250) |
settleTimeout | number | No | Pipeline: longest wait in ms for a streamed turn to settle (default 10 000) |
realtimeProvider, realtimeApiKey | 'openai' | 'gemini', string | Realtime only | Realtime provider and its key |
realtimeModel, voice | string | No | Realtime model and voice |
instructions | string | No | Realtime instructions (default agent.instructions) |
tools | RealtimeTool[] | No | Realtime tools, replace the agent's own ([] for none) |
agentTools | boolean | RealtimeToolsOptions | No | Realtime: the agent's own tools when tools is not set (default true, see Realtime) |
toolImages | boolean | No | Realtime: send images tools return to the model |
transport | WebSocketTransportConfig | No | Transport options, see below |
Transport options
| Option | Default | Description |
|---|---|---|
path | '/voice' | WebSocket path |
maxConnections | 100 | Further upgrades are rejected with 503 |
verifyClient | — | (req) => boolean | { code, message } (may be async): true accepts, false rejects with 401, { code, message } rejects with that status; a throw rejects with 500 |
transport: {
verifyClient: (req) => {
const token = new URL(req.url ?? '/', 'http://localhost').searchParams.get('token');
if (!token) return false;
return token === process.env.VOICE_TOKEN ? true : { code: 403, message: 'Forbidden' };
},
},Browsers cannot set headers on a WebSocket, so pass credentials in the query string or a cookie. Idle clients that stop answering pings (every 30s) are dropped. WebSocketTransport is exported too, for building your own server around VoiceClient connections (audio / message events, sendAudio(), sendMessage()).
WebSocket protocol
| Direction | Format | Content |
|---|---|---|
| Client -> Server | Binary | PCM16 mono (16kHz pipeline / Gemini, 24kHz OpenAI realtime) |
| Client -> Server | JSON | { type: 'interrupt' }, { type: 'end_of_speech' } (pipeline), { type: 'text', text } |
| Server -> Client | Binary | Response audio (TTS output format in pipeline mode, MP3 by default, PCM16 24kHz in realtime mode) |
| Server -> Client | JSON | { type: 'transcript', text, isFinal }, agent_response, agent_response_delta, speech_start, speech_end, turn_end |
In realtime mode transcript carries role ('user' or 'assistant') instead of isFinal, and there is no agent_response, agent_response_delta or speech_end. Invalid JSON and unknown control types are reported through the error event and otherwise ignored.
VoicePipelineConfig
| Field | Type | Required | Description |
|---|---|---|---|
stt | STTProvider | Yes | Speech-to-text provider |
tts | TTSProvider | Yes | Text-to-speech provider |
vad | VADProvider | No | Voice activity detection |
ttsOptions | TTSOptions | No | Options for every TTS call (format, voice, speed, instructions) |
agent | VoiceAgentRunner | Yes | createCogitatorRunner(...) or any { run(input, ctx) } |
bargeIn | boolean | No | Speech heard during a reply stops it (default false, needs a vad) |
maxSentenceLength | number | No | Longest streamed piece sent to TTS at once (default 250) |
settleTimeout | number | No | Longest wait in ms for a streamed turn to settle (default 10 000) |
sampleRate | number | No | Deprecated, unused (configure the STT/VAD providers instead) |
Voice Activity Detection
Detect when users start and stop speaking with @cogitator-ai/voice VAD providers — zero-dependency EnergyVAD for simple environments and neural Silero VAD for noisy conditions.
Realtime Mode
Native speech-to-speech with RealtimeSession — connect to OpenAI Realtime API or Gemini Live API for low-latency voice conversations with tool calling support.