// ── Continuous voice on the approved conversation (R5, HEL-878). ── // The server opens a room and hands out a short join token (POST /api/voice/session, keys never reach the page); the // browser joins with the existing LiveKit client and publishes the microphone. Her words are spoken by the dedicated // worker, which goes through the same /api/chat as text; the page never confirms a receipt for voice (the worker does, // only after real playout) and never turns subtitles into chat rows: the record is re-read from /api/messages. // // One rule holds the whole lifecycle together: every call starts a new generation, and anything that answers for an // older generation (a late token, a late join, a late microphone, a late room event) is cleaned up, never shown. import type { Message } from '../domain/model'; import { api, toMessage, within, type MessagePage } from '../adapters/real'; import { serverId } from '../adapters/inbox'; export type VoicePhase = 'off' | 'opening' | 'joining' | 'listening' | 'thinking' | 'speaking'; export interface VoiceView { phase: VoicePhase; // What the recogniser heard of you so far (a draft; it is replaced, never stored). heard: string; // Her line spoken after a failure (φ): heard, not part of the record. spoken: string; // Why voice is off or what went wrong; empty while it simply runs. notice: string; // The browser held her audio back until a tap (autoplay rules); `resume` lets it out. blocked: boolean; } export const OFF: VoiceView = { phase: 'off', heard: '', spoken: '', notice: '', blocked: false }; export interface Subtitle { kind: 'interim' | 'user_final' | 'assistant_final' | 'error'; role: 'user' | 'assistant'; text: string } export function subtitle(v: unknown): Subtitle | null { const s = v as Record | null; if (!s || typeof s !== 'object' || typeof s.text !== 'string' || !['interim', 'user_final', 'assistant_final', 'error'].includes(s.kind as string)) return null; return { kind: s.kind as Subtitle['kind'], role: s.role === 'assistant' ? 'assistant' : 'user', text: s.text }; } // ── What the controller needs from a real-time client. livekit.ts implements it with livekit-client; tests use fakes. ── export interface MicTrack { stop(): void; onEnded(cb: () => void): void } export interface RoomEvents { agentJoined(): void; // The worker's own state attribute (lk.agent.state): initializing / listening / thinking / speaking. agentState(state: string): void; subtitle(payload: unknown): void; // The room went away without us closing it (server ended it, network, kicked). dropped(): void; audioBlocked(blocked: boolean): void; } export interface VoiceRoom { publish(mic: MicTrack): Promise; hush(on: boolean): void; resumeAudio(): Promise; close(): Promise } export interface VoiceTransport { capture(): Promise; join(url: string, token: string, on: RoomEvents, signal: AbortSignal): Promise } export interface VoiceAvailability { available: boolean; reason: string } export interface SessionGrant { url: string; token: string; room: string; session_id: string; epoch: number; expires_at: number } export const WORDS = { notConfigured: '语音还没有在这台服务上配置,文字仍可使用。', ttsDown: '她的声音服务这会儿不在,稍后再试试。文字仍可使用。', noRoute: '这台服务还没有语音通路,文字仍可使用。', unknown: '暂时读不到语音状态,文字仍可使用。', insecure: '这个地址不是安全连接,浏览器不允许使用麦克风。需要从 HTTPS 入口打开。', denied: '没有拿到麦克风权限,语音没有开始。文字仍可使用。', noMic: '没有找到可用的麦克风,语音没有开始。', micBusy: '麦克风被别的程序占用了,语音没有开始。', client: '语音组件没有加载出来,文字仍可使用。', unreachable: '通话服务暂时连不上,稍后再试试。', noAgent: '她没有接上这次通话,麦克风已经关闭。文字仍可使用。', dropped: '通话断开了,麦克风已经关闭。文字仍可使用。', micGone: '麦克风被关掉了,语音已结束。', offline: '网络断开了,语音已结束,麦克风已关闭。', } as const; export interface VoiceDeps { transport: () => Promise; onView: (view: VoiceView) => void; // The record may have changed (a voice turn began, ended or was cut): re-read /api/messages. onRecord: () => void; // You finished saying something by voice: a text reply still streaming has been cancelled by the server. onSpoke: () => void; fetch?: typeof fetch; // How long the room may wait for her worker to join before giving up honestly. agentWaitMs?: number; secure?: () => boolean; } interface Live { gen: number; abort: AbortController; mic?: MicTrack; room?: VoiceRoom; session?: string; agent: boolean; stated: boolean; hushed: boolean; timer?: ReturnType } const AGENT_PHASE: Record = { initializing: 'joining', listening: 'listening', thinking: 'thinking', speaking: 'speaking' }; export async function voiceStatus(signal: AbortSignal, f: typeof fetch = fetch): Promise { let response: Response; try { response = await f('/api/voice/status', { signal }); } catch (e) { if (signal.aborted) throw e; return { available: false, reason: WORDS.unknown }; } // A server from before R5 has no such route: that is "not wired", not a failure. if (response.status === 404 || response.status === 405) return { available: false, reason: WORDS.noRoute }; const data = await response.json().catch(() => null) as { configured?: unknown; tts_ok?: unknown } | null; if (!response.ok || !data) return { available: false, reason: WORDS.unknown }; if (data.configured !== true) return { available: false, reason: WORDS.notConfigured }; if (data.tts_ok !== true) return { available: false, reason: WORDS.ttsDown }; return { available: true, reason: '' }; } function micFailure(e: unknown, secure: boolean) { const name = (e as { name?: string } | null)?.name || ''; if (!secure) return WORDS.insecure; if (name === 'NotAllowedError' || name === 'SecurityError' || name === 'PermissionDeniedError') return WORDS.denied; if (name === 'NotFoundError' || name === 'DevicesNotFoundError' || name === 'OverconstrainedError') return WORDS.noMic; if (name === 'NotReadableError' || name === 'AbortError') return WORDS.micBusy; return WORDS.denied; } const OWED = 'cg.voice.owed'; function owedSessions(): string[] { try { const v = JSON.parse(localStorage.getItem(OWED) || '[]'); return Array.isArray(v) ? v.filter((s): s is string => typeof s === 'string' && !!s && s.length < 100) : []; } catch { return []; } } export class VoiceController { private gen = 0; private live: Live | null = null; private view: VoiceView = OFF; private f: typeof fetch; constructor(private deps: VoiceDeps) { this.f = deps.fetch ?? ((...a) => fetch(...a)); } get phase() { return this.view.phase; } get active() { return this.live !== null; } private show(next: Partial, gen: number) { if (gen !== this.gen) return; this.view = { ...this.view, ...next }; this.deps.onView(this.view); } async start(): Promise { // 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 }; this.live = live; this.view = { ...OFF, phase: 'opening' }; this.deps.onView(this.view); const stale = () => gen !== this.gen; if (prev) { await this.teardown(prev); this.deps.onRecord(); } if (stale()) return; void this.settle(); // Everything a failure says is said while this call still owns the page; its cleanup goes on afterwards and, // however late it answers, only releases this call's own microphone, room and server room. const fail = (notice: string) => { if (stale()) return Promise.resolve(); this.live = null; this.gen++; const released = this.teardown(live); this.view = { ...OFF, notice }; this.deps.onView(this.view); return released; }; const secure = (this.deps.secure ?? (() => window.isSecureContext && !!navigator.mediaDevices?.getUserMedia))(); if (!secure) return fail(WORDS.insecure); // 1. The honest switch: configured and her voice service reachable (no room, no microphone yet). let status: VoiceAvailability; try { status = await voiceStatus(live.abort.signal, this.f); } catch { return; } if (stale()) return; if (!status.available) return fail(status.reason); // 2. The client, then the microphone. A refusal here opens nothing on the server. let transport: VoiceTransport; try { transport = await this.deps.transport(); } catch { return fail(WORDS.client); } if (stale()) return; let mic: MicTrack; try { mic = await transport.capture(); } catch (e) { return fail(micFailure(e, true)); } if (stale()) { mic.stop(); return; } live.mic = mic; mic.onEnded(() => { if (!stale()) void fail(WORDS.micGone); }); // 3. A room of her own for this call. This request is not cut short by ending: if it was already under way, its // answer still owns a room on the server, so it is waited for and given back. let response: Response, data: (SessionGrant & { detail?: unknown }) | null; try { response = await this.f('/api/voice/session', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}', signal: AbortSignal.timeout(15000) }); data = await response.json().catch(() => null); } catch { return fail(WORDS.unreachable); } if (response.ok && typeof data?.session_id === 'string') live.session = data.session_id; if (stale()) { await this.teardown(live); return; } if (!response.ok || !data?.token || !data.url || !live.session) return fail(typeof data?.detail === 'string' && data.detail ? data.detail : WORDS.unreachable); const grant = data; // 4. Join and speak. 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); }, agentState: state => { if (stale()) return; const phase = AGENT_PHASE[state]; if (!phase) return; live.stated = true; if (phase !== 'joining' && !live.agent) events.agentJoined(); // After text took over, her old voice stays silent until she is back to listening. if (live.hushed && phase !== 'speaking') { live.hushed = false; live.room?.hush(false); } this.show({ phase, ...(phase === 'thinking' ? { spoken: '' } : {}) }, gen); }, subtitle: payload => { if (stale()) return; const s = subtitle(payload); if (!s) return; if (s.kind === 'interim') { this.show({ heard: s.text, spoken: '', ...(live.stated ? {} : { phase: 'listening' as const }) }, gen); if (live.hushed && !live.stated) { live.hushed = false; live.room?.hush(false); } return; } if (s.kind === 'user_final') { this.show({ heard: s.text, spoken: '', ...(live.stated ? {} : { phase: 'thinking' as const }) }, gen); this.deps.onSpoke(); this.deps.onRecord(); return; } if (s.kind === 'assistant_final') { this.show({ heard: '', spoken: '', ...(live.stated ? {} : { phase: 'listening' as const }) }, gen); this.deps.onRecord(); return; } this.show({ heard: '', spoken: s.text, ...(live.stated ? {} : { phase: 'listening' as const }) }, gen); this.deps.onRecord(); }, dropped: () => { if (!stale()) void fail(WORDS.dropped); }, audioBlocked: blocked => this.show({ blocked }, gen), }; let room: VoiceRoom; try { room = await transport.join(grant.url, grant.token, events, live.abort.signal); } catch { return fail(WORDS.unreachable); } if (stale()) { live.room = room; await this.teardown(live); return; } live.room = room; try { await room.publish(mic); } catch { return fail(WORDS.unreachable); } if (stale()) { await this.teardown(live); return; } if (!live.agent) live.timer = setTimeout(() => { if (!stale() && !live.agent) void fail(WORDS.noAgent); }, this.deps.agentWaitMs ?? 20000); } // Stop everything now: microphone first, then the room and its audio, then the server-side room. Safe to call twice. async end(notice = ''): Promise { const live = this.live; this.gen++; this.live = null; const was = this.view.phase !== 'off' || this.view.notice !== notice; this.view = { ...OFF, notice }; if (was) this.deps.onView(this.view); if (live) { await this.teardown(live); this.deps.onRecord(); } } // Page is going away: same order, without waiting; the DELETE survives the unload. unload() { const live = this.live; this.gen++; this.live = null; this.view = OFF; if (live) void this.teardown(live, true); } // You sent text while she was answering by voice: the server cancels her voice turn; keep its last audio silent. hush() { const live = this.live; if (!live?.room || (this.view.phase !== 'speaking' && this.view.phase !== 'thinking')) return; live.hushed = true; live.room.hush(true); this.show({ spoken: '' }, live.gen); } async resumeAudio() { const live = this.live; if (!live?.room) return; try { await live.room.resumeAudio(); this.show({ blocked: false }, live.gen); } catch { /* Still blocked; the tap stays offered. */ } } private async teardown(live: Live, keepalive = false) { live.abort.abort(); clearTimeout(live.timer); try { live.mic?.stop(); } catch { /* Already stopped. */ } const room = live.room; live.room = undefined; const session = live.session; live.session = undefined; const closing = room ? room.close().catch(() => {}) : Promise.resolve(); // The server deletes the room: her worker leaves and its job ends even if the page could not say goodbye itself. // A DELETE that could not get through (offline) is kept and sent again when the network is back. const ending = session ? this.release(session, keepalive) : Promise.resolve(); await Promise.all([closing, ending]); } // Kept in this browser (not only in memory), so leaving the conversation or reloading does not forget them. Until // one gets through, the server side still ends on its own: the worker's job ends when the page's participant is // gone from the room, and the session registry expires (runbook-hel877-voice §3.4/§3.5). private owe(session: string, on: boolean) { const list = new Set(owedSessions()); if (on) list.add(session); else list.delete(session); try { if (list.size) localStorage.setItem(OWED, JSON.stringify([...list].slice(-20))); else localStorage.removeItem(OWED); } catch { /* Memory only for this visit. */ } if (on) this.owed.add(session); else this.owed.delete(session); } private owed = new Set(); private release(session: string, keepalive = false) { // Noted before sending: if the page is gone before the answer, the next visit sends it again (404 = done). this.owe(session, true); return this.f(`/api/voice/session/${encodeURIComponent(session)}`, { method: 'DELETE', keepalive }) .then(r => { this.owe(session, !(r.ok || r.status === 404)); }, () => { this.owe(session, true); }); } // Back online, arriving, or starting again: give back rooms an earlier end could not reach the server to delete. async settle() { await Promise.all([...new Set([...this.owed, ...owedSessions()])].map(s => this.release(s))); } } // The record's view of voice: rows of your and her voice turns, re-read from the server. Her reach-outs (proactive) // are left to the inbox reader so they keep their own entrance; text turns already on the page are matched by turn id. export function mergeTurns(known: Message[], rows: Message[]): Message[] | null { const byId = new Map(known.map((m, i) => [m.id, i])), turns = new Set(known.flatMap(m => m.turnId ? [`${m.role}:${m.turnId}`] : [])); let next: Message[] | null = null; for (const m of [...rows].sort((a, b) => serverId(a) - serverId(b))) { if (m.proactive || !serverId(m)) continue; const at = byId.get(m.id); if (at !== undefined) { const old = (next ?? known)[at]; if (old.status !== m.status || old.text !== m.text) { next ??= [...known]; next[at] = { ...old, status: m.status, text: m.text }; } continue; } if (m.turnId && turns.has(`${m.role}:${m.turnId}`)) continue; next ??= [...known]; next.push(m); byId.set(m.id, next.length - 1); if (m.turnId) turns.add(`${m.role}:${m.turnId}`); } return next; } // Newest rows down to `since` (exclusive), at most `pages` pages of 50. Voice turns are few per read; a longer gap is // left to the next re-read rather than read without bound. export async function recentRows(since: number, signal: AbortSignal, pages = 4): Promise { const rows: Message[] = []; let before: number | null = null; for (let i = 0; i < pages; i++) { const page: MessagePage = await api(`messages?limit=50${before ? `&before=${before}` : ''}`, { signal: within(signal, 10000) }); const list = (Array.isArray(page.messages) ? page.messages : []).filter(m => Number.isSafeInteger(m?.id) && m.id > 0 && typeof m.text === 'string'); rows.push(...list.filter(m => m.id > since).map(toMessage)); if (list.some(m => m.id <= since) || !page.next_before || !Number.isSafeInteger(page.next_before)) break; before = page.next_before; } return rows; }