finish method
Native interleaved decoding, matching the upstream six/twelve schedule. Only a complete, nonempty text+speech reply commits history. Cancellation, callback failure, context limits or output limits discard the current turn.
Implementation
Future<AudioConversationReply> finish({
int seed = 0,
bool deferCommit = false,
void Function(String text)? onText,
void Function(Float32List pcm)? onAudio,
void Function(int token, List<int>? codes)? onSlot,
}) async {
_open();
final pending = _pending;
if (pending == null || _rows == 0) throw StateError('No audio received');
_busy = true;
try {
final suffix = _assistant;
await _forward(suffix);
pending.addAll(suffix);
final reply = await decodeSpeechReply(
model: model,
tokenizer: tokenizer,
generator: generator,
mode: AudioReplyMode.mixed,
maxSlots: _maxSlots,
maxFrames: _maxFrames,
seed: seed,
check: _check,
onText: onText,
onAudio: onAudio,
onSlot: onSlot,
feed: (id, embedding, {required logits, meanwhile}) async {
final slot = _Slot(id, embedding);
await _forward([slot], logits: logits, meanwhile: meanwhile);
pending.add(slot);
},
);
if (reply.text.isEmpty || reply.frames == 0) {
throw StateError(
'Model returned no complete spoken reply. Please try again.',
);
}
await model.checkpoint();
_check();
if (pending.last.id != _id('<|im_end|>')) {
pending.add(_Slot(_id('<|im_end|>')));
}
_candidate = pending;
if (!deferCommit) {
_history
..clear()
..addAll(pending);
_candidate = null;
_turns++;
}
return AudioConversationReply(
reply.text,
reply.samples,
reply.sampleRate,
reply.frames,
reply.generatedSlots,
pending.length,
);
} finally {
_pending = null;
_busy = false;
}
}