Skip to main content

Voice Agent (Conversational AI)

The VoiceAgentClient handles everything from audio streaming to conversational logic.
import { VoiceAgentClient, VoiceStyle, Language } from '@lokutor/sdk';

const client = new VoiceAgentClient({
  apiKey: 'your-api-key',
  prompt: 'You are a helpful and friendly AI assistant.',
  onTranscription: (text) => console.log('User:', text),
  onResponse: (text) => console.log('AI:', text)
});

await client.connect();

// Feed PCM audio data to the client:
// client.sendAudio(pcmData);

Standalone Streaming TTS

Use TTSClient to convert text into high-quality audio streams without the conversational overhead.
import { TTSClient, VoiceStyle } from '@lokutor/sdk';

const client = new TTSClient({ apiKey: 'your-api-key' });

await client.synthesize({
  text: 'Hello world, this is a test of Lokutor streaming TTS.',
  voice: VoiceStyle.F1,
  onAudio: (buffer) => {
    // Play the audio buffer (Node.js or Browser)
    console.log(`Received ${buffer.length} bytes of audio`);
  }
});

Node.js Example

Here is a complete example of a voice conversation in Node.js using node-record-lpcm16 and speaker.
import { VoiceAgentClient, VoiceStyle, Language, AUDIO_CONFIG } from '@lokutor/sdk';
import recorder from 'node-record-lpcm16';
import Speaker from 'speaker';

async function main() {
  const client = new VoiceAgentClient({
    apiKey: 'your-api-key',
    prompt: 'You are a helpful and friendly AI assistant.',
    onTranscription: (text) => console.log('User:', text),
    onResponse: (text) => console.log('AI:', text),
  });

  const connected = await client.connect();
  if (!connected) return;

  // 1. Setup Microphone Recording (Input)
  const mic = recorder.record({
    sampleRate: AUDIO_CONFIG.SAMPLE_RATE,
    channels: AUDIO_CONFIG.CHANNELS,
    threshold: 0,
  });

  mic.stream().on('data', (chunk) => {
    client.sendAudio(chunk);
  });

  // 2. Setup Speaker Playback (Output)
  const speaker = new Speaker({
    channels: AUDIO_CONFIG.CHANNELS,
    bitDepth: 16,
    sampleRate: AUDIO_CONFIG.SAMPLE_RATE,
  });

  client.onAudio((chunk) => {
    speaker.write(chunk);
  });
}

main().catch(console.error);