From 60ea3bb30ae538d97489f2bc14c9d543f481824f Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 17 Oct 2026 09:33:43 +0000 Subject: [PATCH] feat(web): read any chat message aloud in one click A speaker button on every question and answer reads it through POST /voice/speak, the configured speech model and the voice picked in Settings, else the browser's speech. One message at a time, long answers in pieces under every provider's limit with the next prefetched, audio kept in the page so replays cost nothing. Each call is a model.speech row in the activity ledger. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_0113pz5KMp2GDme1G4WqJbAo --- CHANGELOG.md | 5 + DEVLOG.md | 11 ++ README.md | 2 +- docs/assistant.md | 11 ++ docs/requirements.md | 1 + .../__tests__/chat-read-aloud.test.tsx | 187 ++++++++++++++++++ nextjs-frontend/__tests__/chat.test.tsx | 1 + nextjs-frontend/components/chat/answer.tsx | 2 + .../components/chat/read-aloud.tsx | 25 +++ .../components/chat/user-bubble.tsx | 2 + nextjs-frontend/lib/voice.ts | 177 ++++++++++++++++- 11 files changed, 421 insertions(+), 3 deletions(-) create mode 100644 nextjs-frontend/__tests__/chat-read-aloud.test.tsx create mode 100644 nextjs-frontend/components/chat/read-aloud.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index aac866543..21aa736eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Read any message aloud.** Every question and answer in a conversation has a speaker button: one click reads it + aloud by the speech model and voice picked in Settings → AI assistant → Voice (else the browser's own speech), + another stops it. One message plays at a time, long answers go in pieces under every provider's limit, and each + piece's audio is kept in the page, so playing it again doesn't call the model again. Each call is logged in the + activity ledger ([Read a message aloud](docs/assistant.md#read-a-message-aloud)). - **Specs from transcripts.** Turn a voice session or a meeting into a technical spec, saved as a note linked to it: the model writes the spec, then lists every named decision, every "no" and "not that, this", every constraint and every gap the speaker left to decide later, each with its quote and a link to the moment it was said; a quote the diff --git a/DEVLOG.md b/DEVLOG.md index a92cdce0a..79ec6424a 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -2,6 +2,17 @@ Plans and progress for work in flight. Newest first. +## 2026-10-17 · Read any chat message aloud + +Adeel: play any message in the chat through the TTS model. A speaker button on every question and answer reads it by +`POST /voice/speak` (so the provider layer, the voice picker's voice and the activity ledger are voice mode's), one +message at a time, in pieces of at most 1,500 characters (Deepgram's limit is 2,000; `speech.speak` cuts at 4,000), +with the next piece asked for while one plays. Audio is kept in the page by text, the 32 most recent pieces. + +Refine later: keep audio across reloads (encrypted on the server, or the browser's Cache Storage); a shorter first +piece so long answers start sooner; voice mode reading answers through the same pieces; pause and resume rather than +stop; the speaker button in an asset's Chat tab; drop kept audio when the voice is changed. + ## 2026-10-17 · Dates read as days Adeel: dates extraction. `dates.py` reads the dates each line gives (written out, numeric, and relative to the day) diff --git a/README.md b/README.md index 3532f9807..3998ca62b 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p of each conversation's own. Regenerating an answer or editing a question makes a new version beside the old one, and nothing is lost. Each answer keeps a record of what produced it (the model, its tools, how long it took and what it cost), opened from its header. It reads answers aloud with a speech provider, macOS `say` or StyleTTS2 on this machine - (opt-in), and `/clear` and `/compact` keep long conversations short. Each namespace can have its own assistant with + (opt-in), any message in one click, and `/clear` and `/compact` keep long conversations short. Each namespace can have its own assistant with its own name, instructions and memory, answering in Telegram and Slack (built in, with voice notes and files), in Discord, Matrix, WhatsApp and other chat rooms through Matterbridge, and by email at an address of its own that you email or forward things to. ([Chat](docs/assistant.md), [web search](docs/assistant.md#web-search-and-reading-pages), diff --git a/docs/assistant.md b/docs/assistant.md index 4ab1e52b0..528912814 100644 --- a/docs/assistant.md +++ b/docs/assistant.md @@ -193,6 +193,17 @@ Voice goes through one hook, `useVoice()` in `nextjs-frontend/lib/voice.ts` (`li or by ElevenLabs, Deepgram, Google Gemini or macOS `say` ([Speech providers](speech-providers.md#voices)); else by the browser. +#### Read a message aloud + +Every question and answer in a conversation has a speaker button: one click reads it aloud, another stops it. It goes +through the same `POST /voice/speak` as voice mode, so it uses the speech model and the voice picked in Settings → AI +assistant → Voice, without citation marks or Markdown, and falls back to the browser's own speech when the server has +none. One message is read at a time (starting another stops the first, and so does leaving the conversation). Long +answers are sent in pieces of at most 1,500 characters, cut after a sentence, under every provider's limit; the next +piece is asked for while one plays. Each piece's audio is kept in the page (the 32 most recently played), so playing +a message again doesn't call the speech model again. Each call is a `model.speech` row in the +[activity ledger](activity.md), like voice mode's. + Settings → AI assistant → Voice holds these. The API: `GET /voice` (what the server does; it also starts loading the engine), `POST /voice/transcribe` (a clip as the raw body; nothing is kept), `POST /voice/speak` (`{text, voice?}`; 204 when the browser should read it), `GET /voice/voices` (the voices a provider reads in). diff --git a/docs/requirements.md b/docs/requirements.md index 02a380d46..d458d2fd3 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -83,6 +83,7 @@ request, to another check that proves the same thing; loosening a requirement or | Regenerating an answer or editing a question makes a new version beside the old one | [test_chat_versions.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chat_versions.py) `test_regenerate_edit_switch_and_go_on`; [chat-versions.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-versions.test.tsx) `regenerating an answer`, `the version switch` | | Each answer keeps a record of what produced it: the model, its tools, how long it took and what it cost | [test_chat_records.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chat_records.py) `test_an_answer_records_what_produced_it`, `test_a_regenerated_answer_has_its_own_record`; [chat-details.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-details.test.tsx) `says what produced it: model and host, scope, tools, use, tokens, cost, time and how it ended` | | Answers read aloud by a speech provider, macOS `say` or StyleTTS2 (opt-in) | [test_voice.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_voice.py) `test_answers_are_read_aloud_by_a_speech_model`; [test_speech.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_speech.py) `test_macos_say_reads_aloud_on_a_mac`, `test_styletts2_reads_aloud_from_its_own_service`; [styletts2.yml](https://github.com/adeelahmad/lense/blob/main/.github/workflows/styletts2.yml) `Read a sentence aloud` | +| Any message read aloud in one click, one at a time, its audio kept so a replay costs nothing | [chat-read-aloud.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-read-aloud.test.tsx) `plays an answer by the speech model in one click, stops on the next, and keeps its audio`, `reads one message at a time: starting another stops the first`, `reads a long answer piece by piece, asking for the next while one plays` | | `/clear` and `/compact` | [test_compaction.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_compaction.py) `test_clear_starts_afresh_and_compact_folds_everything_now`, `test_commands_in_the_app` | | A namespace's own assistant, with its own name, instructions and memory | [test_ns_assistant.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_ns_assistant.py) `test_off_by_default_and_owners_turn_it_on`, `test_remembers_across_conversations_with_citations`; [ns-assistant.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/ns-assistant.test.tsx) `saving a name and forgetting a memory` | | Telegram and Slack built in, with voice notes and files | [test_telegram.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_telegram.py) `test_it_answers_private_chats_and_what_is_said_to_it_in_groups`, `test_voice_notes_are_heard`, `test_files_are_held_for_the_assistant_to_import`; [test_slack.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_slack.py) `test_it_answers_direct_messages_mentions_and_the_slash_command`, `test_voice_clips_are_heard_and_files_held` | diff --git a/nextjs-frontend/__tests__/chat-read-aloud.test.tsx b/nextjs-frontend/__tests__/chat-read-aloud.test.tsx new file mode 100644 index 000000000..7d95f1253 --- /dev/null +++ b/nextjs-frontend/__tests__/chat-read-aloud.test.tsx @@ -0,0 +1,187 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Voice as VoiceApi } from "@/app/openapi-client"; +import { Answer } from "@/components/chat/answer"; +import { UserBubble } from "@/components/chat/user-bubble"; +import { TooltipProvider } from "@/components/ui/tooltip"; +import { forgetVoice, pieces, PIECE_CHARS, stopReading } from "@/lib/voice"; + +jest.mock("@/app/openapi-client", () => ({ + Voice: { voiceInfo: jest.fn(), speakText: jest.fn() }, + Chats: { checkMessage: jest.fn() }, + Search: {}, +})); +jest.mock("@/lib/api/browser", () => ({ + useApiClient: () => ({}), + data: (p: Promise<{ data: unknown }>) => p.then((r) => r.data), +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +jest.mock("@/lib/hooks/session", () => ({ + useArchive: () => ({ can: () => true, namespaces: [], roleIn: () => "editor" }), +})); +jest.mock("@/components/chat/attachments", () => ({ SentFiles: () => null })); + +const m = (f: unknown) => f as jest.Mock; + +class FakeAudio { + static playing: FakeAudio[] = []; + static played: string[] = []; + onended: (() => void) | null = null; + onerror: (() => void) | null = null; + paused = false; + constructor(public src: string) {} + play() { + FakeAudio.played.push(this.src); + FakeAudio.playing.push(this); + return Promise.resolve(); + } + pause() { + this.paused = true; + } + /** It plays to the end. */ + end() { + this.onended?.(); + } +} + +let clipN = 0; +beforeAll(() => { + window.matchMedia = ((query: string) => ({ + matches: false, + media: query, + addEventListener: () => {}, + removeEventListener: () => {}, + })) as unknown as typeof window.matchMedia; +}); +beforeEach(() => { + jest.clearAllMocks(); + forgetVoice(); + stopReading(); + FakeAudio.playing = []; + FakeAudio.played = []; + (window as unknown as Record).Audio = FakeAudio; + URL.createObjectURL = jest.fn(() => `blob:${++clipN}`); + URL.revokeObjectURL = jest.fn(); + m(VoiceApi.voiceInfo).mockResolvedValue({ data: { transcribe: false, speak: true }, response: { status: 200 } }); + m(VoiceApi.speakText).mockImplementation(() => + Promise.resolve({ data: new Blob(["mp3"]), response: { status: 200 } }), + ); +}); + +function show(ui: React.ReactNode) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + return render( + + {ui} + , + ); +} + +const answer = (text: string, messageId = 7) => ( + {}} + onPreview={() => {}} + /> +); + +describe("reading a message aloud", () => { + it("cuts long text after sentences, under every provider's limit", () => { + const sentence = "The ship leaves on Friday at noon. "; + const long = sentence.repeat(100); + const out = pieces(long); + expect(out.length).toBeGreaterThan(1); + expect(out.every((p) => p.length <= PIECE_CHARS && p.endsWith("."))).toBe(true); + expect(out.join(" ")).toBe(long.trim()); + expect(pieces("one two three four", 9)).toEqual(["one two", "three", "four"]); + expect(pieces(" ")).toEqual([]); + }); + + it("has nothing to read in an answer that is only code", () => { + show(answer("```\nls -la\n```")); + expect(screen.queryByRole("button", { name: "Read aloud" })).not.toBeInTheDocument(); + }); + + it("plays an answer by the speech model in one click, stops on the next, and keeps its audio", async () => { + show(answer("It leaves on **Friday** [1].")); + fireEvent.click(screen.getByRole("button", { name: "Read aloud" })); + await waitFor(() => expect(FakeAudio.played).toHaveLength(1)); + expect(m(VoiceApi.speakText).mock.calls[0][0].body).toEqual({ text: "It leaves on Friday." }); + const stop = screen.getByRole("button", { name: "Stop reading aloud" }); + expect(stop).toHaveAttribute("aria-pressed", "true"); + + fireEvent.click(stop); + expect(FakeAudio.playing[0].paused).toBe(true); + expect(screen.getByRole("button", { name: "Read aloud" })).toBeInTheDocument(); + + fireEvent.click(screen.getByRole("button", { name: "Read aloud" })); // again: the kept audio, no second call + await waitFor(() => expect(FakeAudio.played).toHaveLength(2)); + expect(VoiceApi.speakText).toHaveBeenCalledTimes(1); + act(() => FakeAudio.playing[1].end()); + await waitFor(() => expect(screen.getByRole("button", { name: "Read aloud" })).toBeInTheDocument()); + }); + + it("reads one message at a time: starting another stops the first", async () => { + show( + <> + + {answer("It leaves on Friday.")} + , + ); + const [question, reply] = screen.getAllByRole("button", { name: "Read aloud" }); + fireEvent.click(reply); + await waitFor(() => expect(FakeAudio.played).toHaveLength(1)); + fireEvent.click(question); + await waitFor(() => expect(FakeAudio.played).toHaveLength(2)); + expect(FakeAudio.playing[0].paused).toBe(true); + expect(m(VoiceApi.speakText).mock.calls[1][0].body).toEqual({ text: "When does the ship leave?" }); + expect(screen.getAllByRole("button", { name: "Stop reading aloud" })).toHaveLength(1); + expect(screen.getAllByRole("button", { name: "Read aloud" })).toHaveLength(1); + }); + + it("stops when the message leaves the page", async () => { + const { unmount } = show(answer("It leaves on Friday.")); + fireEvent.click(screen.getByRole("button", { name: "Read aloud" })); + await waitFor(() => expect(FakeAudio.played).toHaveLength(1)); + unmount(); + expect(FakeAudio.playing[0].paused).toBe(true); + }); + + it("reads a long answer piece by piece, asking for the next while one plays", async () => { + show(answer(Array.from({ length: 120 }, (_, i) => `The ship number ${i} leaves on Friday at noon.`).join(" "))); + fireEvent.click(screen.getByRole("button", { name: "Read aloud" })); + await waitFor(() => expect(FakeAudio.played).toHaveLength(1)); + await waitFor(() => expect(VoiceApi.speakText).toHaveBeenCalledTimes(2)); // the next piece, while this one plays + act(() => FakeAudio.playing[0].end()); + await waitFor(() => expect(FakeAudio.played).toHaveLength(2)); + for (const [{ body }] of m(VoiceApi.speakText).mock.calls) + expect(body.text.length).toBeLessThanOrEqual(PIECE_CHARS); + }); + + it("is read by the browser when the server has no speech model", async () => { + m(VoiceApi.voiceInfo).mockResolvedValue({ data: { transcribe: false, speak: false }, response: { status: 200 } }); + const said: string[] = []; + const speak = jest.fn((u: { text: string; onend?: () => void }) => said.push(u.text)); + Object.assign(window, { speechSynthesis: { speak, cancel: jest.fn() } }); + (window as unknown as Record).SpeechSynthesisUtterance = class { + lang = ""; + onend: (() => void) | null = null; + onerror: (() => void) | null = null; + constructor(public text: string) {} + }; + show(answer("It leaves on Friday [1].")); + fireEvent.click(screen.getByRole("button", { name: "Read aloud" })); + await waitFor(() => expect(speak).toHaveBeenCalled()); + expect(said).toEqual(["It leaves on Friday."]); + }); +}); diff --git a/nextjs-frontend/__tests__/chat.test.tsx b/nextjs-frontend/__tests__/chat.test.tsx index 9ff10fdb8..56abd03d0 100644 --- a/nextjs-frontend/__tests__/chat.test.tsx +++ b/nextjs-frontend/__tests__/chat.test.tsx @@ -16,6 +16,7 @@ import { parseBlocks, RichText } from "@/components/chat/rich-text"; import { applyEvent, newTurn, savedSteps, stepCall } from "@/components/chat/stream"; import { UserBubble } from "@/components/chat/user-bubble"; +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); jest.mock("@/components/chat/attachments", () => ({ SentFiles: ({ files }: { files: { filename: string }[] }) => (
    diff --git a/nextjs-frontend/components/chat/answer.tsx b/nextjs-frontend/components/chat/answer.tsx index b8790bdb0..5ecc1379f 100644 --- a/nextjs-frontend/components/chat/answer.tsx +++ b/nextjs-frontend/components/chat/answer.tsx @@ -20,6 +20,7 @@ import { CitationChip } from "@/components/chat/citation"; import { AnswerDetails } from "@/components/chat/details"; import { AnswerMenu, type ExportFormat } from "@/components/chat/export"; import { keywords } from "@/components/chat/keywords"; +import { ReadAloud } from "@/components/chat/read-aloud"; import { RichText } from "@/components/chat/rich-text"; import { scopeWords, type Scope } from "@/components/chat/scope"; import { SharedFiles } from "@/components/chat/shared-files"; @@ -364,6 +365,7 @@ export function Answer(p: AnswerProps) { {check.isPending ? "Checking sources…" : "Check sources"} )} + {!streaming && !blank && !noModel && !nothing && !failed && } {!streaming && !blank && !noModel && } {check.isError && ( diff --git a/nextjs-frontend/components/chat/read-aloud.tsx b/nextjs-frontend/components/chat/read-aloud.tsx new file mode 100644 index 000000000..f403d3efc --- /dev/null +++ b/nextjs-frontend/components/chat/read-aloud.tsx @@ -0,0 +1,25 @@ +"use client"; + +import { Loader2, Square, Volume2 } from "lucide-react"; + +import { IconButton } from "@/components/ui/button"; +import { cn } from "@/lib/utils"; +import { speakable, useReadAloud } from "@/lib/voice"; + +/** The play button on a chat message (docs/assistant.md, "Read a message aloud"): one click reads it aloud by the + * text-to-speech model in the voice picked in Settings (else the browser), another stops it. */ +export function ReadAloud({ text, size = 32, className }: { text: string; size?: number; className?: string }) { + const r = useReadAloud(text); + if (!speakable(text)) return null; // only code, say: nothing to read + return ( + + {r.loading ? : r.on ? : } + + ); +} diff --git a/nextjs-frontend/components/chat/user-bubble.tsx b/nextjs-frontend/components/chat/user-bubble.tsx index cf217e7eb..64a33b623 100644 --- a/nextjs-frontend/components/chat/user-bubble.tsx +++ b/nextjs-frontend/components/chat/user-bubble.tsx @@ -5,6 +5,7 @@ import { useState } from "react"; import { SentFiles } from "@/components/chat/attachments"; import { CopyQuestion } from "@/components/chat/copy"; +import { ReadAloud } from "@/components/chat/read-aloud"; import { Button, IconButton } from "@/components/ui/button"; import { cn } from "@/lib/utils"; @@ -74,6 +75,7 @@ export function UserBubble({ } return (
    + {!filesOnly && } {!filesOnly && } {onEdit && ( max) { + const head = rest.slice(0, max + 1); + const ends = [...head.matchAll(/[.!?…](?=\s)/g)].map((m) => (m.index ?? 0) + 1); + let at = ends.length ? ends[ends.length - 1] : head.lastIndexOf(" "); + if (at < max / 3) at = head.lastIndexOf(" ") > max / 3 ? head.lastIndexOf(" ") : max; + out.push(rest.slice(0, at).trim()); + rest = rest.slice(at).trim(); + } + if (rest) out.push(rest); + return out; +} + +/** How many pieces of audio are kept in this page, the most recently played first. */ +export const CLIPS_KEPT = 32; +const clips = new Map>(); + +/** The server's audio for a piece of text, asked once and kept (null when the browser should read it). */ +function clipOf(client: Client, text: string): Promise { + const kept = clips.get(text); + if (kept) { + clips.delete(text); + clips.set(text, kept); + return kept; + } + const asked = VoiceApi.speakText({ client, body: { text }, parseAs: "blob" }) + .then((r) => (r.response?.status === 200 && r.data instanceof Blob && r.data.size ? r.data : null)) + .catch(() => null) + .then((b) => { + if (!b) clips.delete(text); // asked again next time + return b; + }); + clips.set(text, asked); + while (clips.size > CLIPS_KEPT) clips.delete(clips.keys().next().value as string); + return asked; +} + +/** Forget the kept audio and what the server does, as a fresh page would (tests). */ +export function forgetVoice(): void { + clips.clear(); + serverVoice = null; +} + +type Reading = { key: string | null; phase: "loading" | "playing" | null }; +let reading: Reading = { key: null, phase: null }; +let hush: (() => void) | null = null; +const watchers = new Set<() => void>(); +const show = (r: Reading) => { + reading = r; + watchers.forEach((w) => w()); +}; +const watch = (w: () => void) => { + watchers.add(w); + return () => watchers.delete(w); +}; +const NOBODY: Reading = { key: null, phase: null }; + +/** Stops whatever message is being read aloud. */ +export function stopReading(): void { + hush?.(); + hush = null; + if (reading.key) show(NOBODY); +} + +/** Plays a clip; resolves when it ends, fails or is stopped. */ +function playClip(clip: Blob, onStop: (f: () => void) => void): Promise { + const url = URL.createObjectURL(clip); + return new Promise((resolve) => { + const a = new Audio(url); + const done = () => { + URL.revokeObjectURL(url); + resolve(); + }; + onStop(() => { + a.pause(); + done(); + }); + a.onended = done; + a.onerror = done; + a.play().catch(done); + }); +} + +/** The browser reads a piece; resolves when it's done or stopped. */ +function sayHere(words: string, onStop: (f: () => void) => void): Promise { + const synth = typeof window !== "undefined" ? window.speechSynthesis : undefined; + if (!synth || typeof SpeechSynthesisUtterance === "undefined") return Promise.resolve(); + return new Promise((resolve) => { + const u = new SpeechSynthesisUtterance(words); + u.lang = navigator.language || "en-US"; + onStop(() => { + synth.cancel(); + resolve(); + }); + u.onend = () => resolve(); + u.onerror = () => resolve(); + synth.speak(u); + }); +} + +/** Reads one message aloud by the server's speech model (in the voice picked in Settings), else the browser: piece by + * piece, the next one asked for while this one plays. Whatever was being read stops first. */ +async function read(client: Client, key: string, text: string): Promise { + stopReading(); + const parts = pieces(speakable(text)); + if (!parts.length) return; + let stopped = false; + let cut: (() => void) | null = null; + const onStop = (f: () => void) => { + cut = f; + }; + const mine = () => { + stopped = true; + cut?.(); + }; + hush = mine; + show({ key, phase: "loading" }); + const server = (await askServer(client)).speak; + for (let i = 0; i < parts.length && !stopped; i++) { + const clip = server ? await clipOf(client, parts[i]) : null; + if (stopped) return; + if (server && i + 1 < parts.length) void clipOf(client, parts[i + 1]); + show({ key, phase: "playing" }); + await (clip ? playClip(clip, onStop) : sayHere(parts[i], onStop)); + cut = null; + } + if (hush === mine) { + hush = null; + show(NOBODY); + } +} + +export type ReadAloud = { + /** Asking for the audio, before it plays. */ + loading: boolean; + /** This message is being read aloud (or its audio asked for). */ + on: boolean; + /** Plays it, or stops it when it's on. */ + toggle: () => void; +}; + +/** The play button on one chat message: reads `text` aloud (no citation marks or Markdown), or stops it. Starting + * another message stops this one, and leaving the page stops it too. */ +export function useReadAloud(text: string): ReadAloud { + const client = useApiClient(); + const key = useId(); + const now = useSyncExternalStore( + watch, + () => reading, + () => NOBODY, + ); + useEffect( + () => () => { + if (reading.key === key) stopReading(); + }, + [key], + ); + const on = now.key === key; + const toggle = useCallback(() => { + if (reading.key === key) stopReading(); + else void read(client, key, text); + }, [client, key, text]); + return { loading: on && now.phase === "loading", on, toggle }; +}