Realtime Session
medium10 minrealtime-session.ts
You want low-latency speech-to-speech with tool calling.
What it shows
- Open a Gemini Live
RealtimeSessionwith a tool - Listen for audio, transcripts and tool calls
The code
realtime-session.ts
import { RealtimeSession } from '@cogitator-ai/voice';
const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');
const session = new RealtimeSession({
provider: 'gemini',
apiKey,
instructions: 'You are a helpful voice assistant. Keep responses brief.',
voice: 'Puck',
tools: [
{
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: { location: { type: 'string' } },
required: ['location'],
},
execute: async (args: unknown) => {
const { location } = args as { location: string };
return { location, temperature: 22, unit: 'celsius', condition: 'sunny' };
},
},
],
});
let audioBytes = 0;
session.on('audio', (chunk) => {
audioBytes += chunk.length;
});
session.on('transcript', (text, role) => console.log(`[${role}] ${text}`));
session.on('tool_call', (name, args) => console.log(`tool: ${name}(${JSON.stringify(args)})`));
session.on('error', (error) => console.error(error.message));
await session.connect();
session.sendText('What is the weather in San Francisco?');
const deadline = Date.now() + 30_000;
while (audioBytes === 0 && Date.now() < deadline) {
await new Promise<void>((resolve) => {
const timer = setTimeout(resolve, deadline - Date.now());
session.once('turn_end', () => {
clearTimeout(timer);
resolve();
});
});
}
console.log(`Received ${audioBytes} bytes of PCM16 24kHz audio`);
session.close();Run it
install · bash
pnpm add @cogitator-ai/voiceEnvironment
GOOGLE_API_KEYrun · bash
GOOGLE_API_KEY=your-key npx tsx realtime-session.tsOr run the full example from a clone of the repository (keys in .env):
repository · bash
npx tsx examples/voice/02-realtime.ts