Voice Agent¶
End-to-end on-device voice assistant: VAD → STT → LLM → TTS, with neural end-of-turn detection (the smart-turn-v3 model), interruption handling, streaming transcription (live partial captions), and sentence-level streaming for sub-second time-to-first-audio.
The agent is implemented as a small actor-based node graph.
TheStageVoiceAgent is a thin builder that wires the nodes and bridges the
internal event bus onto a public events: Flow<TheStageAgentEvent> plus a
few typed output channels (LLM deltas, transcripts, VAD probability).
Main features
End-to-end on-device loop — VAD → STT → LLM → TTS, with sentence-level streaming for sub-second time-to-first-audio.
Neural end-of-turn (smart-turn-v3): waits through mid-sentence pauses, responds quickly once you’re done.
Barge-in: interrupt the assistant mid-reply, with AEC and tunable lockouts so it doesn’t trip on its own TTS.
Wake-word standby: rests in
sleepinguntil the wake word fires, and returns to it after each turn.Speaker gating hooks:
SPEAKER_VERIFIED/SPEAKER_REJECTEDgates on the internal event bus.Custom nodes: extend the agent graph with your own nodes on named ports and the shared event bus.
Deferred mic: load models with the mic closed (
auto_listen = false), open it viabegin_listening().Background operation: keeps running off-screen under a microphone foreground service.
In this page¶
Here we will cover the following topics:
Pipeline: the state machine a turn walks through, with and without a wake word.
API surface: every Kotlin / Flutter entry point in one table.
Quick start: the smallest useful runnable app in each language.
Capabilities: what a started agent can actually do, and
require_full_stack.Models: the bundles the agent composes and the config fields that select them.
Configuration: compute routing, LLM, endpointing, turn detection, ASR streaming, interruption, wake word.
Streaming: typed output channels and the
eventsvocabulary.Controls & session lifecycle: runtime methods, hot-apply knobs, the concurrency model.
Voices: switching the TTS voice, voice packs and language on a running agent.
Usage Guides & Troubleshooting: deferred mic, barge-in, live captions, tools, custom nodes, background operation, latency figures, and documented failure modes.
Pipeline¶
┌────────────────────────────────────────┐
│ if config.wake_word == null │
idle → loading ─────►│ listening ⇄ thinking → speaking │──► listening
│ │
│ else (wake-word configured) │
│ sleeping ─WW─► listening ⇄ thinking │──► speaking ──► sleeping
└────────────────────────────────────────┘
a tool call inside a turn:
thinking ─► tool_calling ─► thinking ─► speaking
speaking ─► tool_calling ─► speaking (a spoken filler first)
State |
Meaning |
|---|---|
|
Models not loaded |
|
Models being downloaded / loaded |
|
Wake-word standby. VAD/WW are live, ASR/LLM/TTS are gated off. Only
entered when |
|
Mic open, VAD scanning for speech |
|
Speech committed, LLM is generating |
|
The model called a tool and the agent is running it. Barge-in works
and cancels the tool. Left for |
|
TTS streaming audio to the speaker |
State is derived inside the orchestrator from the event stream and is the only
place the state machine lives. It is broadcast as a state_changed event and
also exposed as agent.state (StateFlow<TheStageAgentState> in Kotlin,
TSAgentState in Flutter).
API surface¶
Everything you touch on TheStageVoiceAgent (Kotlin) or
TSVoiceAgent (Flutter). Rows marked — are covered on
this page for the other language only.
Purpose |
Kotlin |
Flutter |
|---|---|---|
Build config |
|
config map passed to |
Construct |
|
|
Start pipeline |
|
|
Capabilities |
|
returned by |
Begin listening (deferred mic) |
|
|
Final user text |
|
|
LLM token deltas |
|
|
VAD probability |
|
|
Speech level (avatar) |
|
|
Current state |
|
|
Everything else |
|
|
Push a typed turn |
|
|
Speak text (skip LLM) |
|
|
Cancel current response |
|
|
Change TTS voice |
|
|
Change system prompt |
|
|
Chat memory |
|
|
Enroll speaker |
|
|
Hot re-tune |
|
|
Custom-node ports |
|
|
Stop |
|
|
Quick start¶
Kotlin:
// build.gradle.kts: implementation(files("libs/TheStageCore.aar"))
import ai.thestage.qlip.TheStageAI
import ai.thestage.qlip.voice_agent.TheStageVoiceAgent
import ai.thestage.qlip.voice_agent.TheStageAgentConfig
import ai.thestage.qlip.voice_agent.TheStageAgentEvent
// Inside a coroutine scope.
TheStageAI.registerContext(context)
TheStageAI.initialize(api_token = "your-api-token")
val config = TheStageAgentConfig(
vad = "TheStageAI/silero-vad",
stt = "TheStageAI/thewhisper-large-v3-turbo",
tts = "TheStageAI/neutts-multilingual",
llm_provider = "openai_compatible",
llm_endpoint = "https://api.openai.com/v1/chat/completions",
llm_api_key = "sk-...",
llm_model = "gpt-4o-mini",
system_prompt = "You are a helpful voice assistant. Keep replies short."
)
val agent = TheStageVoiceAgent(config)
// Event stream (state changes, transcripts, deltas, errors).
launch {
agent.events.collect { event ->
when (event.kind) {
TheStageAgentEvent.Kind.STATE_CHANGED ->
println("[STATE] ${event.data["state"]}")
TheStageAgentEvent.Kind.USER_REQUEST ->
println("[YOU] ${event.data["text"]}")
TheStageAgentEvent.Kind.RESPONSE_DELTA ->
print(event.data["delta"])
TheStageAgentEvent.Kind.RESPONSE_DONE ->
println("\n[ASSISTANT DONE]")
TheStageAgentEvent.Kind.ERROR ->
println("[ERROR] ${event.data["message"]}")
else -> {}
}
}
}
// Typed channels: each is a plain Kotlin Flow you can collect
// independently.
launch {
agent.llm_deltas.collect { delta ->
// Append delta to a chat bubble, etc.
}
}
agent.start()
// agent runs continuously — speak into the mic
For a fully on-device assistant, set llm_provider = "local" (the Kotlin
default; Flutter’s is "openai_compatible") and point llm at an on-device LLM bundle from the LLM (Language Model)
page (e.g. "TheStageAI/LFM2.5-1.2B"). The agent runs it with thinking off,
so replies go straight to speech, and gives it the built-in
tools (weather, time, search, phone actions) unless you set
llm_tools = "none".
Flutter:
import 'dart:io';
import 'package:thestage_android_sdk/thestage_android_sdk.dart';
await TheStageFlutterSDK.initialize(api_token: 'your-api-token');
final agent = TSVoiceAgent();
agent.events.listen((event) {
switch (event['kind']) {
case 'state_changed': print('STATE: ${event['state']}');
case 'user_request': print('YOU: ${event['text']}');
case 'response_delta': stdout.write(event['delta']);
case 'response_done': print('\nASSISTANT DONE');
}
});
// Typed broadcast streams (one EventChannel each).
agent.llm_deltas.listen((delta) { /* update assistant bubble */ });
agent.transcripts.listen((text) { /* show user turn */ });
agent.vad_probabilities.listen((p) { /* drive a level meter */ });
agent.tts_levels.listen((level) { /* animate an avatar */ });
final caps = await agent.start(config: {
'vad': 'TheStageAI/silero-vad',
'stt': 'TheStageAI/thewhisper-large-v3-turbo',
'tts': 'TheStageAI/neutts-multilingual',
'llm_provider': 'openai_compatible',
'llm_endpoint': 'https://api.openai.com/v1/chat/completions',
'llm_api_key': 'sk-...',
'llm_model': 'gpt-4o-mini',
'system_prompt': 'You are a helpful voice assistant.',
});
// Later:
await agent.interrupt(); // stop current response
await agent.say('Welcome back!'); // speak arbitrary text (skips LLM)
await agent.update_interrupt_config(interrupt_min_speech_ms: 200);
await agent.stop();
start() returns the agent’s capabilities and also
keeps them in agent.capabilities.
Capabilities¶
What a started agent can actually do depends on the stack it built: a config
without tts is transcription-only, a config without an LLM never answers.
The agent reports this after start():
Key |
Type |
|
|---|---|---|
|
bool |
The mic and VAD are running |
|
bool |
Speech recognition is running |
|
bool |
A spoken turn reaches an LLM that answers |
|
bool |
TTS is running: replies and |
|
|
One entry per |
responding is false without an LLM, for llm_provider = "local"
without an llm path, for an empty llm_endpoint, and with
turn_end_mode = TurnEndMode.NONE (Flutter 'turn_end_mode': 'none'; no
turn ever ends, so speech never reaches the LLM; send_request still does).
After start() with auto_listen = false, listening is false
(reason: the mic is not open yet) until begin_listening();
transcribing and responding follow it. In Flutter,
begin_listening() and set_voice() refresh agent.capabilities.
Set require_full_stack to make start() fail unless the configured
stack — audio input + VAD, speech recognition, LLM and TTS — was built. The
error lists the reasons, and nothing keeps running. A deferred mic
(auto_listen = false) does not fail it.
Kotlin:
val agent = TheStageVoiceAgent(config.copy(require_full_stack = true))
agent.start() // throws IllegalStateException
val caps = agent.capabilities // TheStageAgentCapabilities
if (!caps.speaking) println(caps.reasons["speaking"])
Flutter:
final caps = await agent.start(config: config);
if (caps['speaking'] != true) {
print((caps['reasons'] as Map)['speaking']);
}
// Or fail fast:
await agent.start(config: config, require_full_stack: true);
Models¶
The agent composes the fleet bundles below — pass the HF id (or a local path) on the config; the SDK downloads and caches each on first start.
Task |
HF engines |
Notes |
|---|---|---|
VAD |
|
512-sample chunks @ 16 kHz |
ASR |
|
any length; SDK windows long audio |
TTS |
|
multilingual phonemizes internally |
Turn detect |
|
DNN end-of-turn ( |
Chat LLM |
|
on-device chat LLM ( |
The config fields that select them:
Field |
Type |
Default |
Description |
|---|---|---|---|
|
String |
required |
HF id or local path of Silero VAD bundle |
|
String |
required |
HF id or local path of Whisper bundle |
|
String? |
|
HF id or local path of NeuTTS bundle (required to speak) |
|
String |
|
Voice id from the TTS bundle |
|
String? |
|
A voice pack folder holding |
|
String? |
|
TTS language override, e.g. |
|
String? |
|
Optional wake-word bundle — HF id or local path. When set, the agent
rests in |
|
String |
|
Whisper decode language (ISO-639-1, e.g. |
|
String |
|
STT commit; empty = the commit this SDK pins. Leave it empty |
|
String |
|
TTS commit; empty = the pinned commit. Leave it empty |
|
String |
|
VAD commit; empty = the pinned commit. Leave it empty |
|
String |
|
Wake-word commit; empty = the pinned commit |
|
String? |
|
Speaker-id bundle ( |
|
String |
|
Speaker-id commit; empty = the pinned commit |
|
Double |
0.75 |
Similarity to the enrolled embedding a speaker must reach to be verified |
|
|
|
The speaker to verify against; or call |
|
Boolean |
|
Fail |
|
Boolean |
|
Kotlin only. Memory optimization: swap STT (whisper) and the TTS-LLM in/out of memory across listening/speaking so they never co-reside. Default off = always-loaded. |
Configuration¶
Compute device routing (Snapdragon NPU)¶
Field |
Type |
Default |
Description |
|---|---|---|---|
|
String |
|
Silero VAD bundle class |
|
String |
|
Whisper bundle class |
|
|
|
Per-module override: |
|
String |
|
NeuTTS bundle class |
|
|
|
Per-module override: |
|
String |
|
On-device LLM bundle class ( |
|
String |
|
Wake-word bundle class |
|
String |
|
smart-turn bundle class |
|
String |
|
Speaker-id bundle class |
Leave every *_device empty: each component then loads the best bundle its
model publishes for the phone, exactly like start_model(device = "") (see
Model Management, Choosing the device). "npu" or
"cpu" is a constraint: start() fails if that model publishes no such
bundle — Silero VAD, for one, publishes only a CPU bundle.
The Qualcomm Snapdragon NPU (Hexagon) runs the heavy models: Whisper’s
encoder/decoder and the on-device LLM on TheStage’s own NPU runtime, the
NeuTTS LLM through QNN (compiled context binaries). Both keep running when the
app is in the background (with a foreground service). Small stateful models (Silero VAD, the smart-turn classifier) run
on ORT-CPU regardless — they don’t benefit from the NPU. Some sub-modules are
pinned per the bundle’s metadata.json (e.g. the Whisper mel front-end
stays on CPU).
LLM¶
Field |
Type |
Default |
Description |
|---|---|---|---|
|
String |
|
|
|
String? |
|
Local: engines path of the on-device LLM |
|
String |
|
Local LLM commit; empty = the pinned commit. Leave it empty |
|
String |
|
Remote model id |
|
String |
OpenAI chat-completions |
Remote endpoint URL |
|
String |
|
Remote API key |
|
|
|
Kotlin only. Inject a custom / mock provider (overrides the above) |
|
String |
|
Prepended as a system message. The default is a short spoken assistant that knows when and how to use tools. |
|
String |
|
Built-in tools of the on-device LLM: |
|
|
|
Kotlin only: your own tools for the on-device LLM; replaces the
|
|
Int |
256 |
Generation cap |
|
Double |
0.7 |
Sampling temperature |
|
|
|
Kotlin only. History strategy |
VAD / endpointing¶
Field |
Type |
Default |
Description |
|---|---|---|---|
|
Double |
0.8 |
Speech probability threshold |
|
Int |
96 |
Sustained voiced duration to trigger onset |
|
Int |
600 |
Trailing silence to commit turn |
|
Int |
30000 |
Hard cap on a single turn |
|
Int |
200 |
Pre-roll captured before onset |
All durations are in milliseconds; the nodes convert them to the live VAD
frame cadence (frame_samples / sample_rate, ≈32 ms for Silero @ 16 kHz)
internally. silence_timeout_ms only applies to the default VAD endpointer
(turn_end_mode = TurnEndMode.VAD); the DNN endpointer ignores it.
Turn detection (end-of-turn)¶
The endpointer is pluggable. "vad" (default) commits a turn after a fixed
silence gap (silence_timeout_ms). "dnn" replaces that with the pipecat
smart-turn-v3 model: at each pause it runs a learned end-of-turn check on
the trailing waveform, so the agent waits through mid-sentence pauses but
responds quickly once you’re actually done. "none" never fires end-of-turn
(continuous transcription).
VAD is the cheap gate (onset + pause detection); the DNN is the expensive
semantic check run single-flight, off-thread, only at pauses, with a hard
turn_max_silence_ms floor so it can never hang or classify an empty
window. The model sees the continuous waveform from onset (incl. pre-roll)
through the pause — never VAD-filtered audio.
Field |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
|
|
String |
|
smart-turn engines repo/path (used only for |
|
String |
|
smart-turn commit; empty = the pinned commit |
|
String |
|
Bundle class for the classifier; empty = best available (it runs on the CPU) |
|
Double |
0.85 |
Completion prob at/above which a checkpoint counts as “done” |
|
Int |
2 |
Consecutive “done” verdicts required before committing. Debounces a
single spike on a mid-sentence pause. |
|
Double |
1.0 |
Verdict prob that commits immediately, skipping confirmation.
|
|
Int |
256 |
Trailing silence before the first model call |
|
Int |
120 |
Re-run cadence on a sustained pause (0 disables) |
|
Int |
5000 |
Hard fallback; MUST be < |
|
Int |
8000 |
Trailing audio window fed to the model |
|
Int |
250 |
Minimum voiced speech before the model is consulted |
|
Int |
200 |
Trailing silence still fed to streaming ASR after speech stops (bounds “mm”/”?” filler; the turn model still sees the full pause) |
import ai.thestage.qlip.voice_agent.graph.TurnEndMode
val config = TheStageAgentConfig(
vad = ..., stt = ..., tts = ..., llm_provider = "local", llm = ...,
turn_end_mode = TurnEndMode.DNN,
turn_detector = "TheStageAI/smart-turn-v3" // or a local dir
)
The model is a two-module chain: a mel front-end feeding an int8-weight
Whisper-Tiny encoder + completion head, running on ORT-CPU on Android, shipped
as TheStageAI/smart-turn-v3 and downloaded/cached by the SDK on first use.
Knobs hot-apply at runtime via agent.update_turn_config(...).
Why a confirm count. A single model checkpoint can spike over
turn_eot_threshold on a brief mid-sentence pause. Requiring
turn_eot_confirm_count consecutive “done” verdicts (re-evaluated every
turn_reeval_interval_ms) debounces that, at the cost of a little latency.
The turn_eot_high_confidence fast-path (commit immediately on a very
confident single verdict) is off by default (1.0): it can commit
before enough trailing silence is buffered and clip the last ASR word, even on
0.99-confident verdicts. Lower it (e.g. 0.97) only
if you measure that it doesn’t truncate finals on your audio.
Using the turn detector on its own¶
The classifier is also a model you can start and call directly, if you are
building your own turn-taking rather than using the voice agent. Start
TheStageAI/smart-turn-v3 like any other model and pass the trailing
waveform:
{ "audio": [ ... 16 kHz mono floats in [-1, 1] ... ] }
It returns [{"probability": 0.91}] — the probability that the turn is
complete. Pass the continuous audio from speech onset through the current
pause; any length works, since the front-end keeps the last 8 s and left-pads
a shorter tail.
A second key, input_features, accepts a pre-computed 80 × 800 log-mel if
you already hold one. Pass one or the other — passing both is an error
rather than a precedence rule, because the two cannot be told apart at
runtime: 80 × 800 mel floats is exactly 4.0 s of 16 kHz PCM, so a length
heuristic would silently read audio as features and return a confident wrong
answer.
Streaming transcription (ASR)¶
Field |
Type |
Default |
Description |
|---|---|---|---|
|
Boolean |
|
Emit live partial captions ( |
|
Int |
600 |
Minimum new audio between caption passes (bounds redundant decoding). Streaming only. |
|
Boolean |
|
Decode a speculative full-utterance pass at the first VAD pause so the final transcript is warm by end-of-turn (low-latency final). |
The ASR node runs one unified path with two decoupled consumers that share a single serial inference chain (the model is never entered concurrently):
Captions (cosmetic). When
asr_streamingis on, the node re-decodes the growing turn buffer everyasr_partial_interval_ms(a VAD pause forces a pass early) and folds the result through LocalAgreement-2: only the prefix two consecutive hypotheses agree on is surfaced, so captions never flicker or retract. Committed text is published asuser_request_partial. These partials never feed the LLM.Authoritative. At end-of-turn the node emits exactly one
user_request(sourcespeech). It reuses the most recent full-buffer decode (a caption or the speculative pass) when the buffer hasn’t drifted; otherwise it decodes the whole buffer fresh. This is the only value that drives the LLM.
Because the authoritative decode is always a single full-utterance pass,
both modes produce identical final text; asr_streaming only decides
whether live captions are emitted along the way. When asr_streaming is
off, no caption passes run; the speculative pass (if speculative_whisper)
still warms the final at the VAD pause, so perceived STT latency stays near
0 ms in the steady state. The handoff is in-band: the turn node pushes
speculate / end-of-turn markers on the same wire that carries voiced frames,
so finalization stays in lock-step with the audio (no cross-channel race).
Interruption / AEC¶
Field |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
How (and whether) the user can barge in — see below. Flutter: the
mode’s string, e.g. |
|
Int |
600 |
Sustained speech needed to interrupt (Flutter slider goes down to 100 ms) |
|
Int |
0 |
Sustained positive-VAD duration to fire a barge-in. When |
|
Double |
0.9 |
VAD prob threshold for barge-in, independent of |
|
Int |
250 |
Grace at TTS turn start during which barge-in is suppressed (lets AEC re-converge) |
|
Int |
1000 |
One-time, longer barge-in lockout on the first TTS playback after
start (covers AEC cold-start). Should exceed |
|
Int |
600 |
Barge-in lockout while |
|
Int |
900 |
Barge-in lockout after a tool returns ( |
|
Boolean |
|
Platform acoustic echo cancellation, referencing the TTS playback output |
|
Int |
250 |
Silence pumped to the speaker on start so the AEC has reference samples |
|
Int |
80 |
Sink-drain grace at end of every TTS turn |
The AEC fields are accepted from the Flutter config under the same keys.
InterruptMode (ai.thestage.qlip.voice_agent.graph; Flutter string in
brackets):
Mode |
Barge-in when |
|---|---|
|
Never; the mic is hard-muted during playback |
|
Sustained user speech |
|
The wake word fires during sustained speech |
|
The enrolled speaker is verified during sustained speech |
|
Both of the above |
On a running agent, update_interrupt_config(mode = …) takes an
InterruptTrigger — NONE, SPEECH_ONLY or WAKE_WORD (Flutter
interrupt_mode: 'none' | 'speech_only' | 'wake_word').
On a device with unreliable echo cancellation, use NONE so the audio
engine drops mic samples while the speaker is playing and VAD never sees the
self-echo. Android echo cancellation uses the platform
AcousticEchoCanceler on the mic capture
(AudioInputConfig.voice_processing), referencing the TTS playback;
aec_enabled turns it on.
Volume¶
The agent plays in Android’s voice-call audio session, which echo cancellation needs, so the phone’s volume keys set its loudness while it runs (Android shows them as the in-call volume). The SDK widens that range so the lowest steps are quiet. The media volume does not affect the agent.
Wake-word standby¶
When wake_word is set, the orchestrator’s resting state is sleeping:
VAD still runs, and the wake-word node classifies the same voiced-audio
fan-out wire that feeds STT. Only a wake-word detection flips the agent to
listening. After a turn finishes (or is interrupted) the agent returns to
sleeping.
When wake_word is null, sleeping is never entered and the resting
state between turns is listening.
Field |
Type |
Default |
Description |
|---|---|---|---|
|
String? |
|
HF id or local path of the wake-word bundle. Enables |
|
Double |
0.5 |
Probability the wake-word classifier must reach for a positive detection. Tune per model. |
|
String |
|
Wake-word bundle class; empty = best available (see compute routing). |
|
|
|
What leaves |
Streaming¶
Typed channels¶
In addition to the events stream, the agent exposes typed fan-out
ports as plain Kotlin Flows. Each collector sees every value — perfect
for plugging UI widgets, log taps and speech-to-file recorders side-by-side
without intermediate bookkeeping.
Property |
Type |
What it carries |
|---|---|---|
|
|
Each LLM token delta as it is generated, in order |
|
|
One value per user turn (the finalized Whisper transcript; empty on aborted turns) |
|
|
Per-frame Silero probability ([0, 1]); roughly one value every 32 ms |
|
|
Loudness of the assistant’s speech as it plays ([0, 1]); about 30
values per second while speaking, then a single |
|
|
Current lifecycle state |
Stable partial captions (committed-so-far text while the user speaks) are
surfaced on the events stream as user_request_partial events when
asr_streaming is on — great for live captions. They never feed the LLM.
val tap = launch {
agent.vad_probabilities.collect { prob ->
// update a level meter on the main thread
}
}
// ...later
tap.cancel()
The same channels are exposed in Flutter as agent.llm_deltas
(Stream<String>), agent.transcripts (Stream<String>),
agent.vad_probabilities (Stream<double>) and agent.tts_levels
(Stream<double>), each backed by its own EventChannel.
Speech level for an avatar. tts_levels is computed from the audio
actually handed to the speaker, so it follows what the user hears, not how
fast the TTS synthesises. Each ~33 ms window of playback is measured as RMS
and mapped from decibels onto [0, 1]: −55 dBFS and below is 0.0,
−10 dBFS and above is 1.0, linear in between (normal speech sits around
0.6–0.9). Values are paced to playback, at most about 30 per second, and the
stream ends each reply with 0.0, so a mouth closes on its own after the
last word or a barge-in. Nothing is emitted while the agent is silent.
lifecycleScope.launch {
agent.tts_levels.collect { level ->
avatar.mouth_open = level.toFloat() // 0 = closed
}
}
final mouth = ValueNotifier<double>(0);
final sub = agent.tts_levels.listen((level) => mouth.value = level);
// In build():
ValueListenableBuilder<double>(
valueListenable: mouth,
builder: (_, level, __) => Transform.scale(
scaleY: 0.2 + 0.8 * level,
child: const Icon(Icons.circle, size: 48),
),
);
// ...later
await sub.cancel();
Events¶
The agent emits a modality-agnostic, lifecycle-oriented event vocabulary.
In Kotlin each event is a TheStageAgentEvent(kind, data); in Dart it is
one flat map, {'kind': …} plus the data keys below (e.g.
event['state']):
|
|
When |
|---|---|---|
|
|
State transition |
|
|
A stable partial caption was committed mid-turn (streaming ASR only). UI-only; does not drive the state machine. |
|
|
A user request was finalized. |
|
|
An LLM token arrived |
|
|
The response stream finished. |
|
|
The model called a tool. |
|
|
The tool returned |
|
— |
First TTS sample reached the speaker |
|
|
Speaker stopped. |
|
|
The wake-word classifier fired. |
|
— |
The turn-start policy accepted (the agent left |
|
|
Available TTS voices discovered in the loaded bundle, plus the
resolved active voice ( |
|
|
Heartbeat metrics |
|
|
Recoverable error |
The vocabulary is deliberately invariant to why playback stopped: playback
lifecycle (playback_started / playback_ended) is distinct from
synthesis — playback_ended(reason) is what tells the UI whether the agent
finished naturally (completed) or was cut off (interrupted), rather
than overloading “TTS done”.
For high-frequency or fan-out friendly signals, prefer the typed channels
(llm_deltas, transcripts, vad_probabilities, tts_levels)
over parsing events.
Controls¶
agent.interrupt() // cancel current response
agent.say("Hi there!") // speak text, skip LLM (suspend)
agent.send_request("What time is it?") // inject a typed user turn -> LLM
agent.set_voice(voice_id = "dave") // change TTS voice (suspend)
agent.set_system_prompt("Answer in French.")
val history = agent.history() // List<AgentMessage>
agent.clear_history()
agent.update_interrupt_config( // hot-apply on a running agent
min_speech_ms = 200,
mode = InterruptTrigger.SPEECH_ONLY
)
agent.update_turn_config( // "dnn" endpointer only; no-op otherwise
eot_threshold = 0.6,
pause_trigger_ms = 256
)
agent.stop() // unload models, release audio (suspend)
Flutter:
await agent.interrupt();
await agent.say('Hi there!');
await agent.send_request('What time is it?');
await agent.set_voice(voice_id: 'dave');
await agent.set_system_prompt('Answer in French.');
await agent.clear_history();
await agent.update_interrupt_config(
interrupt_min_speech_ms: 200,
interrupt_mode: 'speech_only',
);
await agent.enroll_speaker(embedding: reference); // null clears
await agent.stop();
update_interrupt_config(...), update_turn_config(...) (Kotlin),
set_voice(...) and set_system_prompt(...) change a running agent.
set_system_prompt applies from the next reply and keeps the history. All
other configuration is consumed at start() and changing it requires a
stop() + new TheStageVoiceAgent(config).
send_request(text) submits a typed turn that bypasses the mic and ASR: it
drives the exact same LLM → TTS path as a spoken turn (the finalized
user_request event carries source = "text"). It is a no-op when the
agent has no LLM responder (transcription-only). In Flutter the same call is
agent.send_request(text).
Voices¶
set_voice swaps the TTS voice on a running agent; it reloads the TTS
model, so call it between turns. Pass any subset of three fields — fields you
leave out keep their current value:
voice_idpicks a voice that ships in the TTS bundle (thevoicesevent lists them).voice_dirpoints at a voice pack: one folder holding avoice.json(yours, or a downloaded one). It wins overvoice_id. An emptyvoice_dirclears it and returns tovoice_id.languageswitches the language while keeping the speaker. An emptylanguagereturns to the voice’s own language.
Kotlin:
agent.set_voice(voice_id = "dave")
agent.set_voice(voice_id = "dave", language = "french")
agent.set_voice(voice_dir = packDir.path) // your own voice pack
agent.set_voice(voice_dir = "") // back to the bundle voice
Flutter:
await agent.set_voice(voice_id: 'dave', language: 'french');
await agent.set_voice(voice_dir: packDir.path);
await agent.set_voice(voice_dir: '');
To start with a voice pack or a language, set tts_voice_dir /
tts_language in the config. set_voice throws (Kotlin) or completes
with an error (Flutter) for a relative voice_dir (it must be an absolute
path), a folder without voice.json, an unknown voice_id or language,
or a failed reload; the current voice stays. If the previous voice cannot be
restored either, the agent emits an error event and stops speaking
(speaking capability false). stop() waits for an in-flight
start() or set_voice() to finish. Voice packs work on the
multilingual NeuTTS bundles; with the English-only nano bundle a
voice_dir fails the same way.
A NeuTTS multilingual voice pack is the folder the voice-pack tool writes from
a 3–10 s reference clip; its voice.json holds name, language,
ref_text and ref_codes (NeuCodec codes of the clip), plus optional
sampling defaults (temperature, top_k, min_new_tokens,
use_lang_token). Copy the folder to a real path on the device (out of app
assets, or download it) and pass that path.
Session lifecycle¶
TheStageAI.registerContext(context)+TheStageAI.initialize(...)once per process (Flutter:TheStageFlutterSDK.initialize).Build
TheStageAgentConfig→ constructTheStageVoiceAgent(config)→ subscribe to the streams you care about →start().start()downloads / loads the models (idle → loading); with the defaultauto_listen = truethe mic opens as soon as loading finishes.While running, only
update_interrupt_config(...),update_turn_config(...),set_voice(...)andset_system_prompt(...)hot-apply (see Controls); everything else needs astop()+ newTheStageVoiceAgent(config).On teardown:
agent.stop()unloads models and releases audio.
Concurrency¶
TheStageAI.infer and TheStageAI.infer_stream run on their own
coroutine dispatchers, so VAD, Whisper, NeuTTS and the LLM stream run on
independent tasks inside the agent — none of them serialize on the main
thread. Each node in the graph is its own serial-queue-backed inference loop;
the orchestrator is just an event router and never sits on the hot path. If
you build your own orchestrator on top of these APIs, don’t wrap inference
calls in withContext(Dispatchers.Main) { ... }; that re-introduces the
very serialization this design avoids.
Usage Guides¶
Jump to a recipe:
Deferred mic (auto_listen)¶
By default the agent opens the microphone and starts scanning for speech as
soon as start() finishes loading models (auto_listen = true). When you
want to load the models but hold the mic closed — e.g. to finish
downloading a heavy on-device LLM, or to show a “tap to talk” affordance
before capturing any audio — start with auto_listen = false and open the
mic yourself with begin_listening() once you’re ready.
Field |
Type |
Default |
Description |
|---|---|---|---|
|
Boolean |
|
|
begin_listening() is idempotent (a no-op once the agent is already
listening), so it is safe to call more than once.
Kotlin:
val agent = TheStageVoiceAgent(config.copy(auto_listen = false))
agent.start() // models load; mic stays closed
// ... finish any deferred setup (download local LLM, show UI) ...
agent.begin_listening() // open the mic + start scanning
Flutter:
await agent.start(config: {
...,
'auto_listen': false, // load models, keep mic closed
});
// ... later, when ready:
await agent.begin_listening();
Enable barge-in¶
Barge-in is on by default (interrupt_mode = InterruptMode.VAD):
sustained user speech during playback cancels the current response.
The knobs below make it fire faster; playback_ended /
response_done tell the UI a reply was cut off (see Interruption
/ AEC for the full knob table).
Kotlin:
val config = TheStageAgentConfig(
vad = ..., stt = ..., tts = ...,
interrupt_min_speech_ms = 200, // sustained speech to barge in
interrupt_threshold = 0.9 // strict: no TTS/AEC residue trips
)
val agent = TheStageVoiceAgent(config)
// Detect a cut-off reply.
launch {
agent.events.collect { event ->
if (event.kind == TheStageAgentEvent.Kind.RESPONSE_DONE &&
event.data["reason"] == "interrupted") {
// the user barged in
}
}
}
// Or re-tune on a running agent (hot-apply):
agent.update_interrupt_config(
min_speech_ms = 200,
mode = InterruptTrigger.SPEECH_ONLY
)
Flutter:
await agent.start(config: {
...,
'interrupt_mode': 'vad', // default; 'none' disables
'interrupt_min_speech_ms': 200,
});
agent.events.listen((event) {
if (event['kind'] == 'playback_ended' &&
event['reason'] == 'interrupted') {
// playback was cut off by a barge-in
}
});
// Hot-apply on a running agent:
await agent.update_interrupt_config(interrupt_min_speech_ms: 200);
On a device with unreliable echo cancellation, set
interrupt_mode = InterruptMode.NONE (Flutter 'none') instead so VAD never sees the self-echo
(see Troubleshooting).
Show live captions¶
With asr_streaming on (the default), stable partial captions are
emitted as user_request_partial events while the user speaks —
folded through LocalAgreement-2, so they never flicker or retract. At
end-of-turn a single user_request finalizes the text. Partials are
UI-only and never feed the LLM.
Flutter:
await agent.start(config: {
...,
'asr_streaming': true, // default
'asr_partial_interval_ms': 600, // caption refresh cadence
});
agent.events.listen((event) {
switch (event['kind']) {
case 'user_request_partial':
captions.value = event['text']; // grows as the user speaks
case 'user_request':
captions.value = event['text']; // finalized turn
}
});
Kotlin: collect the same user_request_partial /
user_request events from agent.events; the finalized turn is
also published on the typed flow:
launch {
agent.transcripts.collect { text ->
// one finalized transcript per user turn
}
}
Tools¶
With the on-device LLM (llm_provider = "local"), the model can call tools
in the middle of a reply: it speaks a short filler (“Let me check.”), calls the
tool, and answers from the result. The SDK runs the tool; the UI sees it as the
tool_calling state and a tool_started / tool_ended event pair. Only
the reply text is ever spoken or sent on llm_deltas: the call markup, the
arguments and the tool’s result never reach TTS.
Pick the tools with llm_tools (missing = voice):
|
Tools |
|---|---|
|
|
|
|
|
|
|
No tools |
Tools need an LLM that can call them: the LFM2.5, LFM3 and Qwen3.5 models
can. An OpenAI-compatible endpoint (llm_provider = "openai_compatible")
gets no tools.
Kotlin. Pass your own tools in tools (they replace the preset; add
DefaultTools.voice to keep all the built-ins, or a smaller set such as
DefaultTools.phone). A tool’s execute receives
the call and returns a short string the model reads. See
LLM (Language Model) for writing tools.
val order_status = Tool(
name = "order_status",
description = "Delivery status for an order number. " +
"Use when the user asks where an order is.",
parameters = mapOf(
"type" to "object",
"properties" to mapOf(
"order_id" to mapOf("type" to "string"),
),
"required" to listOf("order_id"),
),
execute = { call ->
orders.status_json(call.arguments["order_id"] as? String ?: "")
},
)
val config = TheStageAgentConfig(
vad = "TheStageAI/silero-vad",
stt = "TheStageAI/thewhisper-large-v3-turbo",
tts = "TheStageAI/neutts-multilingual",
llm_provider = "local",
llm = "TheStageAI/LFM2.5-1.2B",
tools = listOf(order_status) + DefaultTools.phone,
system_prompt = DefaultTools.voice_system_prompt +
"\nYou help Acme customers.",
)
launch {
agent.events.collect { e ->
when (e.kind) {
TheStageAgentEvent.Kind.TOOL_STARTED ->
chip.show(e.data["name"] as String)
TheStageAgentEvent.Kind.TOOL_ENDED -> chip.hide()
else -> {}
}
}
}
Flutter. Choose a built-in preset; custom tools are Kotlin only.
await agent.start(config: {
...agentConfig,
'llm_provider': 'local',
'llm': 'TheStageAI/LFM2.5-1.2B',
// voice (default) | web | phone | none
'llm_tools': 'web',
});
agent.events.listen((e) {
if (e['kind'] == 'tool_started') chip.show(e['name'] as String);
if (e['kind'] == 'tool_ended') chip.hide();
});
Spoken filler. The model only says something before a tool call if the
system prompt asks it to. DefaultTools.voice_system_prompt does (its
“Tool pattern (always):” block). A system prompt of your own without such a
pattern gets the SDK’s spoken-tool instruction appended while tools are on;
keep the line “Tool pattern (always):” in your prompt to write the pattern
yourself instead.
Barge-in. Speaking over the agent while a tool runs cancels the tool and
the reply (response_done with interrupted: true; the cut tool gets no
tool_ended). For interrupt_post_tool_lockout_ms after a tool returns,
barge-in waits so the answer can start.
Note
Privacy. The web tools send what the user asked about over the
network: city names to Open-Meteo (weather, local time) and search queries
to DuckDuckGo and Wikipedia. The phone tools open other apps (browser,
dialer, maps, SMS and e-mail composers) with fields filled in; the user
confirms the call or message there. The SDK never logs tool arguments or
results, and tool_started / tool_ended carry them only to your app.
Set llm_tools to none (or phone) to keep every request on the
device.
Custom nodes¶
The agent graph is extensible: you can append your own nodes that run on the
agent’s event loop, gated by lifecycle state, exchanging values on named ports
and reacting to the shared event bus. The SDK ships the primitives
(TSAgentNode, AgentNodeContext, extra_nodes); example nodes
(live captions, event logs) belong in the host app.
Flutter. Implement TSAgentNode in Dart and pass instances to
start(extra_nodes: [...]). Lifecycle hooks cross a native bridge by string
id:
class EventLogNode extends TSAgentNode {
EventLogNode({this.onBusEvent});
@override String get id => 'event_log';
@override List<String> get runWhen => const []; // empty = always open
final void Function(Map<String, dynamic>)? onBusEvent;
@override
Future<void> onEvent(AgentNodeContext ctx, Map<String, dynamic> e) async {
onBusEvent?.call(e); // e['kind'] = STATE | USER_REQUEST | BARGE_IN | …
}
}
await agent.start(config: baseConfig, extra_nodes: [EventLogNode(...)]);
AgentNodeContext carries the node’s current state, isGateOpen
(whether the gate is open for the current state per runWhen), and the
port/bus helpers:
ctx.sendPort(name, value)— push a value onto this node’s output port. It surfaces on the multiplexedagent.port_eventsstream (andagent.subscribe_port('$id.$name')) as{port: "$id.$name", value}.ctx.recvPort(name)—Stream<String>of values sent to this node’s named port.ctx.publishEvent(event)— inject a bus event. Supported today:{'kind': 'USER_REQUEST', 'text': '...'}(optional'source':speech|text|system), which drives the LLM → TTS path as if the user spoke. This is the node-scoped form ofsend_request.
Gate heavy work (e.g. a local VLM) to non-active states by setting
runWhen to quiet states — treat thinking, tool_calling and
speaking as active,
and prefer idle / sleeping / listening so you don’t contend with
ASR / LLM / TTS for the NPU.
Kotlin. Subclass TheStageAgentNode(id), override start() /
stop(), set run_when, and use the inherited publish(event),
subscribe() (the bus as a SharedFlow<AgentEvent>? — null until the
node is bound) and make_port(name). Append via
config.copy(extra_nodes = listOf(node)).
Internal bus vs public events. A custom node’s onEvent sees the
internal bus vocabulary with UPPERCASE kinds — distinct from the public
snake_case agent.events vocabulary (see Events above). Do not mix
them:
Bus |
Meaning |
|---|---|
|
Lifecycle transition ( |
|
VAD turn boundaries |
|
Sustained speech while interrupt policy evaluates |
|
User interrupted the assistant |
|
Wake-word positive |
|
Speaker-ID gates |
|
Left |
|
Live caption (UI); never drives the LLM |
|
Final request → LLM |
|
Reply lifecycle |
|
A tool call ( |
|
Last TTS sample produced (not yet drained) |
|
Speaker lifecycle |
|
Recoverable error string |
Background operation¶
To keep VAD / Whisper / TTS / wake word running while the app is backgrounded, run the agent under a foreground service with the microphone type. Declare the permissions and service in your app’s manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<service
android:name=".VoiceAgentService"
android:foregroundServiceType="microphone"
android:exported="false" />
Start the service (with an ongoing notification) before agent.start() so
the OS keeps the mic capture alive off-screen. The NPU keeps executing the
model graphs while backgrounded; the exact service lifecycle wiring is
app-side.
Latency¶
The figures below use a remote OpenAI gpt-4o-mini LLM, so “first audio”
(time between end-of-user-speech and the first sample reaching the speaker)
is dominated by the network LLM time-to-first-token — the on-device
Snapdragon pipeline adds a comparable ~100–200 ms on top.
Turn |
LLM 1st tok |
First audio |
Full speak |
|---|---|---|---|
Short reply |
~490 ms |
~520 ms |
~3.3 s |
Long monologue |
~575 ms |
~600 ms |
~53 s |
Mid-length |
~1230 ms |
~1500 ms |
~5.8 s |
The on-device pipeline (VAD + speculative Whisper + LLM-delta-streamed NeuTTS) adds only ~100–200 ms on top of the network round-trip. LLM deltas are plumbed straight into the TTS streaming session, so sentence segmentation and decoder context reuse happen inside TTS — the LLM node never has to wait for sentence boundaries.
Troubleshooting¶
Symptom |
Cause / Fix |
|---|---|
Agent trips on its own TTS during playback (self-echo barge-in) |
Unreliable device echo cancellation. Use |
Final transcript clips the last ASR word (DNN endpointer) |
|
|
It only applies to the |
Mic capture / models stop when the app is backgrounded |
No foreground service. Run under a foreground service with
|
Pipeline serializes / nodes stall on the main thread |
Inference wrapped in |
Upgrading from 1.0.x¶
Every
*_devicefield defaults to"", the best available bundle (they were"npu"/"cpu"). A 1.0.x config that forces"npu"fails for components that publish only a CPU bundle, such as VAD — drop those overrides.set_voicereports failures (see Voices); in 1.0.xsetVoiceerrors were swallowed.The Dart names
TheStageVoiceAgentFlutter,TheStageAgentNode,TheStageAgentState,TheStageAudioPlayerand camelCase members such asllmDeltasorsetVoicestill compile as deprecated aliases ofTSVoiceAgent,TSAgentNode,TSAgentState,TSAudioPlayerand the snake_case names on this page.The Flutter config keys
turn_detection_modeandallow_interruptions: falseare still read, asturn_end_modeandinterrupt_mode: 'none'. In Kotlin,turn_detection_mode,interrupt_triggerandallow_interruptionsare read-only properties derived fromturn_end_modeandinterrupt_mode.