HEL-898: preserve director three-fix rework verbatim on b0dcb17

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
总管
2026-10-27 07:09:23 +08:00
co-authored by multica-agent
parent b0dcb17e1a
commit 9254c17316
12 changed files with 527 additions and 115 deletions
+14 -8
View File
@@ -56,6 +56,8 @@ export function subtitle(v: unknown): Subtitle | null {
export interface MicTrack { stop(): void; onEnded(cb: () => void): void }
export interface RoomEvents {
agentJoined(): void;
// Her worker left the room while it stays open (its job ended or crashed): nobody is answering any more.
agentLeft(): void;
// The worker's own state attribute (lk.agent.state): initializing / listening / thinking / speaking.
agentState(state: string): void;
subtitle(payload: unknown): void;
@@ -81,6 +83,7 @@ export const WORDS = {
client: '语音组件没有加载出来,文字仍可使用。',
unreachable: '通话服务暂时连不上,稍后再试试。',
noAgent: '她没有接上这次通话,麦克风已经关闭。文字仍可使用。',
agentLeft: '她那边的语音中断了,这次通话已结束,麦克风已经关闭。文字仍可使用。',
dropped: '通话断开了,麦克风已经关闭。文字仍可使用。',
micGone: '麦克风被关掉了,语音已结束。',
offline: '网络断开了,语音已结束,麦克风已关闭。',
@@ -100,9 +103,9 @@ export interface VoiceDeps {
}
// seq: the highest subtitle sequence applied in this call (one worker job, one sequence). speech: her speech open now;
// closed: speeches that ended, were cancelled or were superseded here; nothing for them is shown again.
interface Live { gen: number; abort: AbortController; mic?: MicTrack; room?: VoiceRoom; session?: string; agent: boolean; stated: boolean; hushed: boolean; timer?: ReturnType<typeof setTimeout>; seq: number; speech: string; pro: boolean; message?: number; closed: string[] }
const CLOSED_KEPT = 64;
// closed: speeches that ended, were cancelled or were superseded in this call; nothing for them is shown again. It is
// kept for the whole call (no window a long call could push a retired speech out of) and goes with the call.
interface Live { gen: number; abort: AbortController; mic?: MicTrack; room?: VoiceRoom; session?: string; agent: boolean; stated: boolean; hushed: boolean; timer?: ReturnType<typeof setTimeout>; seq: number; speech: string; pro: boolean; message?: number; closed: Set<string> }
const AGENT_PHASE: Record<string, VoicePhase> = { initializing: 'joining', listening: 'listening', thinking: 'thinking', speaking: 'speaking' };
@@ -152,7 +155,7 @@ export class VoiceController {
// The generation is taken before anything is awaited, so a start that wakes up after a newer one never takes
// the call over; the call it replaces is released first, as end() would.
const prev = this.live;
const gen = ++this.gen, live: Live = { gen, abort: new AbortController(), agent: false, stated: false, hushed: false, seq: 0, speech: '', pro: false, closed: [] };
const gen = ++this.gen, live: Live = { gen, abort: new AbortController(), agent: false, stated: false, hushed: false, seq: 0, speech: '', pro: false, closed: new Set() };
this.live = live;
this.view = { ...OFF, phase: 'opening' }; this.deps.onView(this.view);
const stale = () => gen !== this.gen;
@@ -203,6 +206,9 @@ export class VoiceController {
this.show({ phase: 'joining' }, gen);
const events: RoomEvents = {
agentJoined: () => { if (stale() || live.agent) return; live.agent = true; clearTimeout(live.timer); if (!live.stated) this.show({ phase: 'listening' }, gen); },
// Her worker gone with the room still open: no one would ever answer, so the call ends honestly, exactly as a
// failure does (her line, microphone, her audio, room and server room all go; an unsent DELETE is owed).
agentLeft: () => { if (!stale() && live.agent) void fail(WORDS.agentLeft); },
agentState: state => {
if (stale()) return;
const phase = AGENT_PHASE[state]; if (!phase) return;
@@ -267,7 +273,7 @@ export class VoiceController {
}
const pro = s.kind.startsWith('proactive_');
if (s.kind === 'turn_start' || s.kind === 'proactive_start') {
if (live.closed.includes(s.speech) || live.speech === s.speech) return;
if (live.closed.has(s.speech) || live.speech === s.speech) return;
if (live.speech) this.close(live, live.speech);
live.speech = s.speech; live.pro = pro; live.message = s.messageId;
// A reach-out arrives whole (its stored text); a reply starts empty and grows.
@@ -306,7 +312,7 @@ export class VoiceController {
}
private close(live: Live, speech: string) {
if (live.speech === speech) { live.speech = ''; live.pro = false; live.message = undefined; }
if (!live.closed.includes(speech)) { live.closed.push(speech); if (live.closed.length > CLOSED_KEPT) live.closed.shift(); }
live.closed.add(speech);
}
// You sent text while she was answering by voice: the server cancels her voice turn; keep its last audio silent.
@@ -384,8 +390,8 @@ export function voiceLine(her: HerLine | null, messages: Message[], since: numbe
if (her.state !== 'open' && messages.slice(since).some(m => !own(m))) return null;
// A reach-out keeps its stored row's identity (id, turn), so the row taking over keeps the same lines on the stage.
const row = her.proactive ? [...messages, ...incoming].find(own) : undefined;
// Her reply still growing holds back its newest line, as a streamed text reply does.
return { id: row?.id ?? `voice-${her.speech}`, role: 'companion', text: her.text, lifeIds: [], turnId: row ? row.turnId : her.turnId, ...(her.proactive ? { proactive: true } : {}), ...(her.state === 'open' && !her.proactive ? { live: 'generating' as const } : {}) };
// Her reply while she says it: every sentence she has finished is on the stage, only the one still forming waits.
return { id: row?.id ?? `voice-${her.speech}`, role: 'companion', text: her.text, lifeIds: [], turnId: row ? row.turnId : her.turnId, ...(her.proactive ? { proactive: true } : {}), ...(her.state === 'open' && !her.proactive ? { live: 'speaking' as const } : {}) };
}
// Newest rows down to `since` (exclusive), at most `pages` pages of 50. Voice turns are few per read; a longer gap is