refactor: overhaul home UI and audio playback reliability

Redesign the home page and voice stream panel into a compact dock
layout to improve the chat experience. Implement a flush mechanism in
the PCM playback worklet to handle the resampler tail, preventing
premature session termination due to phantom outstanding playback.
Fix document filter visibility in settings to ensure controls remain
accessible when no results match, and refactor transcript insertion
to maintain correct user/assistant ordering.
This commit is contained in:
PeymanHonarjoo
2026-10-10 17:47:51 +03:30
parent be17544f2d
commit 28590c6dce
16 changed files with 1092 additions and 534 deletions
@@ -137,6 +137,7 @@ export class VoiceStreamService {
private session = 0;
private streaming = false;
private turnEnded = false;
private audioGenerationDone = false; // server finished sending this audio_ref's frames
private interruptTimer: ReturnType<typeof setTimeout> | null = null;
private settleTimer: ReturnType<typeof setTimeout> | null = null;
private errorTimer: ReturnType<typeof setTimeout> | null = null;
@@ -490,11 +491,16 @@ export class VoiceStreamService {
case 'audio_started':
this.currentAudioRef = event.audio_ref;
this.audioGenerationDone = false; // frames for this ref are now streaming in
break;
case 'audio_ended':
// Generation ended; the ref's audio may still be playing — its final progress is
// reported when the queue drains or the turn is interrupted.
// Generation finished: emit the worklet's deferred tail and drain what is buffered so
// the ref's FINAL playback progress reaches its true duration (no phantom outstanding
// sample the backend would read as a stall). This drives the drain on generation
// completion, cooperating with — not depending solely on — the completed transcript.
this.audioGenerationDone = true;
this.engine.flushPlayback();
break;
case 'turn_interrupted':
@@ -524,9 +530,10 @@ export class VoiceStreamService {
}
if (event.status === 'completed') {
// All text for this turn is out. Play what is buffered, then return to LISTENING.
// All text for this turn is out. Drain the queue only once generation has ended
// (audio_ended); otherwise the tail would be finalized while audio is still streamed.
this.turnEnded = true;
this.engine.flushPlayback();
if (this.audioGenerationDone) this.engine.flushPlayback();
}
}
@@ -616,17 +623,14 @@ export class VoiceStreamService {
const index = list.findIndex((m) => m.id === id);
if (index === -1) {
if (!event.text.trim()) return list;
return [
...list,
{
id,
turnRef: event.turn_ref,
role: event.role,
text: event.text,
final,
interrupted: interrupted || undefined,
},
];
return this.insertTranscript(list, {
id,
turnRef: event.turn_ref,
role: event.role,
text: event.text,
final,
interrupted: interrupted || undefined,
});
}
const current = list[index];
@@ -679,13 +683,38 @@ export class VoiceStreamService {
}
const next = { ...current, sources };
if (index === -1) return [...list, next];
if (index === -1) return this.insertTranscript(list, next);
const copy = [...list];
copy[index] = next;
return copy;
});
}
/**
* Inserts a new transcript bubble so the invariant always holds: WITHIN a turn the user
* request precedes the AI response, and turns keep their first-seen order. This matters when
* an assistant partial/response for a turn arrives before that turn's user transcript (the
* server streams transcripts per role and does not guarantee the user text lands first) —
* a plain append would then show the AI answer above the user's question.
*/
private insertTranscript(list: VoiceMessage[], message: VoiceMessage): VoiceMessage[] {
const roleRank = message.role === 'user' ? 0 : 1;
// Stable turn order = the order turns first appear in the current list; a turn we have
// not seen yet sorts after all existing ones.
const turns: string[] = [];
for (const m of list) if (!turns.includes(m.turnRef)) turns.push(m.turnRef);
const turnRank = turns.includes(message.turnRef) ? turns.indexOf(message.turnRef) : turns.length;
for (let i = 0; i < list.length; i++) {
const existingTurn = turns.indexOf(list[i].turnRef);
const existingRank = list[i].role === 'user' ? 0 : 1;
const comesAfter =
existingTurn > turnRank || (existingTurn === turnRank && existingRank > roleRank);
if (comesAfter) return [...list.slice(0, i), message, ...list.slice(i)];
}
return [...list, message];
}
private markInterrupted(turnRef: string): void {
const id = `${turnRef}:assistant`;
this.messages.update((list) => {
@@ -882,6 +911,7 @@ export class VoiceStreamService {
private resetTurnFlags(): void {
this.turnEnded = false;
this.audioGenerationDone = false;
}
private clearSettleTimer(): void {