DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(chat): unlock iOS reply playback (#2020)
XZH4a49b5b38e04
diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsxindex 6e3ef7f1f..ed432b92d 100644--- a/web/features/chat/messages/ChatMessageList.tsx+++ b/web/features/chat/messages/ChatMessageList.tsx@@ -1340,6 +1340,8 @@ export function CopyActionButton({// Speaker button: synthesizes this one reply and plays it. Auto-play of later// replies is a Settings preference (`autoPlayFresh`), not a first-click prompt.let activePlayback: { owner: object; stop: () => void } | null = null;+const SILENT_UNLOCK_WAV =+ "data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQAAAAA=";async function ttsErrorMessage(resp: Response): Promise<string> {try {@@ -1402,6 +1404,16 @@ export function PlayAudioButton({abortRef.current = ac;activePlayback = { owner: playbackOwnerRef.current, stop };setState("loading");++ // WebKit evaluates media playback eligibility when play() is called. Keep+ // this element unlocked in the click task, then replace its source after+ // synthesis completes rather than constructing a new audio element then.+ const audio = new Audio(SILENT_UNLOCK_WAV);+ audioRef.current = audio;+ void Promise.resolve(audio.play()).catch(() => {+ /* Unlocking playback can fail on clients that already permit it. */+ });+try {const resp = await apiFetch(apiUrl("/api/voice/tts"), {method: "POST",@@ -1428,8 +1440,8 @@ export function PlayAudioButton({}const url = URL.createObjectURL(blob);urlRef.current = url;- const audio = new Audio(url);- audioRef.current = audio;+ audio.src = url;+ audio.load();audio.onended = () => {if (gen !== genRef.current) return;setState("idle");diff --git a/web/tests/chat-read-aloud-playback.spec.tsx b/web/tests/chat-read-aloud-playback.spec.tsxindex 33d22e650..3f4b8fdc8 100644--- a/web/tests/chat-read-aloud-playback.spec.tsx+++ b/web/tests/chat-read-aloud-playback.spec.tsx@@ -17,11 +17,16 @@ vi.mock("@/lib/notifications", () => ({ notify: vi.fn() }));initI18n("en");const instances: FakeAudio[] = [];class FakeAudio {+ src = "";pause = vi.fn();play = vi.fn().mockResolvedValue(undefined);+ load = vi.fn();onended: (() => void) | null = null;onerror: (() => void) | null = null;- constructor() { instances.push(this); }+ constructor(src?: string) {+ this.src = src ?? "";+ instances.push(this);+ }}function installAudio() {vi.stubGlobal("Audio", FakeAudio);@@ -45,6 +50,31 @@ it("switches between reply buttons without overlapping speech or a first-click pexpect(screen.queryByText(/Automatically read/)).toBeNull();});+it("unlocks the audio element during the click before speech synthesis resolves", async () => {+ installAudio();+ let resolveFetch!: (resp: Response) => void;+ const fetchBody = new Promise<Response>((yes) => { resolveFetch = yes; });+ vi.mocked(apiFetch).mockReturnValueOnce(fetchBody);+ render(<PlayAudioButton content="First" autoPlayFresh={false} />);+ const user = userEvent.setup();+ await user.click(screen.getByRole("button", { name: "Play aloud" }));++ expect(instances).toHaveLength(1);+ expect(instances[0].play).toHaveBeenCalledTimes(1);+ expect(instances[0].src).toContain("data:audio/wav");+ expect(URL.createObjectURL).not.toHaveBeenCalled();++ await act(async () => {+ resolveFetch({ ok: true, blob: async () => new Blob(["audio"]) } as Response);+ });++ expect(await screen.findByRole("button", { name: "Stop" })).toBeVisible();+ expect(instances).toHaveLength(1);+ expect(instances[0].load).toHaveBeenCalled();+ expect(instances[0].play).toHaveBeenCalledTimes(2);+ expect(instances[0].src).toBe("blob:test");+});+it("does not play a late response body after leaving the chat", async () => {installAudio();let resolve!: (blob: Blob) => void;@@ -54,11 +84,13 @@ it("does not play a late response body after leaving the chat", async () => {await userEvent.setup().click(screen.getByRole("button", { name: "Play aloud" }));unmount();await act(async () => { resolve(new Blob(["audio"])); });- expect(instances).toHaveLength(0);+ expect(instances).toHaveLength(1);+ expect(instances[0].pause).toHaveBeenCalled();expect(URL.createObjectURL).not.toHaveBeenCalled();});it("does not show a late speech error after playback is cancelled", async () => {+ installAudio();let resolve!: (body: { detail: string }) => void;const body = new Promise<{ detail: string }>((yes) => { resolve = yes; });vi.mocked(apiFetch).mockResolvedValueOnce({diff --git a/web/tests/voice-settings.spec.tsx b/web/tests/voice-settings.spec.tsxindex eaef1b744..2d7c87e8d 100644--- a/web/tests/voice-settings.spec.tsx+++ b/web/tests/voice-settings.spec.tsx@@ -291,6 +291,12 @@ it('lets a slow preview use the configured timeout and cancels at that deadline'})it('shows a playback timeout instead of silently returning to idle', async () => {+ class FakeAudio {+ play = vi.fn(() => Promise.resolve())+ pause = vi.fn()+ load = vi.fn()+ }+ vi.stubGlobal('Audio', FakeAudio)mock.fetch.mockResolvedValue({ ok: false, status: 504 })render(<PlayAudioButton content="Hello" autoPlayFresh={false} />)fireEvent.click(screen.getByRole('button', { name: 'Play aloud' }))