createSession method

Future<VoiceSession> createSession({
  1. required ModelRef stt,
  2. required ModelRef llm,
  3. required ModelRef tts,
  4. VadOptions? vad,
  5. TurnHandlingOptions? turnHandling,
  6. LlmOptions? generation,
  7. bool downloadIfNeeded = true,
})

Build a session over stt, llm, and tts, downloading what is absent.

final s = await RunAnywhere.voice.createSession(
    stt: ModelRef('whisper-tiny'), llm: ModelRef('qwen3-0.6b'),
    tts: ModelRef('piper-en'));
await s.start();

Throws SDKException when a model cannot be fetched, loaded, or wired.

Implementation

Future<VoiceSession> createSession({
  required ModelRef stt,
  required ModelRef llm,
  required ModelRef tts,
  VadOptions? vad,
  TurnHandlingOptions? turnHandling,
  LlmOptions? generation,
  bool downloadIfNeeded = true,
}) async {
  if (!DartBridge.isInitialized) {
    throw SDKException.notInitialized();
  }
  await DartBridge.ensureServicesReady();

  await ModelGate.ensureLoaded(
    modelId: stt.id,
    category: ModelCategory.MODEL_CATEGORY_SPEECH_RECOGNITION,
    downloadIfNeeded: downloadIfNeeded,
  );
  await ModelGate.ensureLoaded(
    modelId: llm.id,
    category: ModelCategory.MODEL_CATEGORY_LANGUAGE,
    downloadIfNeeded: downloadIfNeeded,
  );
  await ModelGate.ensureLoaded(
    modelId: tts.id,
    category: ModelCategory.MODEL_CATEGORY_SPEECH_SYNTHESIS,
    downloadIfNeeded: downloadIfNeeded,
  );
  await _ensureVad(downloadIfNeeded: downloadIfNeeded);

  final config = VoiceAgentComposeConfig(
    sttModelId: stt.id,
    llmModelId: llm.id,
    ttsVoiceId: tts.voice ?? '',
  );
  // `VoiceAgentComposeConfig.sessionConfig` (`VoiceSessionConfig`) was
  // deleted outright and replaced by `turnDetection` (`TurnDetection`)
  // (idl/voice_agent_service.proto): `autoPlayTts`/`continuousMode` have
  // no wire home anymore (the compose entry point always plays TTS and
  // runs continuously), and `silenceDurationMs` moved onto the new
  // message alongside the VAD-driven turn-detection knobs.
  final turns = turnHandling ?? const TurnHandlingOptions();
  config.turnDetection = TurnDetection(
    type: TurnDetection_Type.TURN_DETECTION_TYPE_VAD,
    silenceDurationMs: turns.endpointing.minDelayMs,
  );
  if (generation != null) {
    config.llmGeneration = generation.toProto();
  }
  await DartBridge.voiceAgent.initializeProto(config);
  return VoiceSession._();
}