Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Show Read this aloud in the message hover bar once a voice is set up

AdityaUmale7480463e0214
diff --git a/src/components/ChatView.rows.test.ts b/src/components/ChatView.rows.test.ts
index cbc25d3dc9..0db6ab4a95 100644
--- a/src/components/ChatView.rows.test.ts
+++ b/src/components/ChatView.rows.test.ts
@@ -199,10 +199,13 @@ describe("chat transcript rows", () => {
await rowRendersAfter((current) => reducer(current, { type: "toggleSettings", open: false }));
};
try {
+ const barButtons = () => document.querySelectorAll(`[data-testid="message-actions"] > button[aria-label="${t("chat.speak.read")}"]`).length;
await switchTo("system");
- expect(await speak()).toEqual([t("chat.speak.read"), t("chat.speak.read")]);
+ expect(await speak()).toEqual([]);
+ expect(barButtons()).toBe(2);
await switchTo("host");
expect(await speak()).toEqual([`${t("chat.speak.needsKey")} (off)`, `${t("chat.speak.needsKey")} (off)`]);
+ expect(barButtons()).toBe(0);
} finally {
window.ogb = bridge;
}
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index 653c511313..fab7fc946e 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -98,7 +98,7 @@ import { ModelPicker } from "./ModelPicker";
import { ChatHeaderMenu, ChatMoreMenu } from "./ChatMoreMenu"; import { TeamOwnHelperHeader, TeamPrivateFirstLine, TeamPrivateHeader, TeamSharedHeader } from "./TeamPrivateChatsLine"; import { ownChatStarted } from "@/state/team-chats";
import { useChatFindRequests, useChatFindShortcut } from "@/lib/chat-find";
import { CitationSelectionToolbar, SentCitations } from "./CitationUI";
-import { SpeakButton } from "./SpeakButton";
+import { SpeakButton, speakReady } from "./SpeakButton";
import { CallOverlay } from "./CallView";
import { LiveCallBar } from "./LiveCallBar";
import { LiveCallChip } from "./LiveCallPill";
@@ -413,7 +413,7 @@ const Bubble = memo(function Bubble({
{canReact && !peer && <ReactAction threadId={threadId} message={message} />}
{!simple && text && <CopyButton text={text} className={messageActionClass} />}
{text && viewRaw && <RawToggleAction active onToggle={() => setViewRaw(false)} className={messageActionClass} />}
- {text && speaking && <SpeakButton text={text} botId={botId} messageId={message.id} voiceId={voiceId} tts={tts} localVoice={localVoice} className={cn(messageActionClass, "text-accent")} />}
+ {text && (speaking || (message.kind === "text" && !peer && speakReady(tts, voiceId, localVoice))) && <SpeakButton text={text} botId={botId} messageId={message.id} voiceId={voiceId} tts={tts} localVoice={localVoice} className={cn(messageActionClass, speaking && "text-accent")} />}
</MessageActions>
);
diff --git a/src/components/GroupView.message-actions.test.ts b/src/components/GroupView.message-actions.test.ts
index 3505adbba9..6b24b3e0cb 100644
--- a/src/components/GroupView.message-actions.test.ts
+++ b/src/components/GroupView.message-actions.test.ts
@@ -6,11 +6,11 @@ import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Bot, Group, Message } from "@/state/store";
-const fixture = vi.hoisted(() => ({ dispatch: vi.fn(), canWrite: true as boolean | null }));
+const fixture = vi.hoisted(() => ({ dispatch: vi.fn(), canWrite: true as boolean | null, config: null as { tts: { configured: boolean; ready: boolean; voice: string } } | null }));
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
return { ...original, useStore: () => ({
- state: { ...original.initialState, bots: [] as Bot[] },
+ state: { ...original.initialState, bots: [] as Bot[], config: fixture.config as typeof original.initialState.config },
dispatch: fixture.dispatch,
}) };
});
@@ -55,6 +55,7 @@ beforeEach(() => {
vi.stubGlobal("window", {});
fixture.dispatch.mockClear();
fixture.canWrite = true;
+ fixture.config = null;
host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
@@ -88,7 +89,7 @@ describe("room message actions", () => {
expect(closed).toMatch(/<span role="img" aria-label="Thumbs up: yes, go ahead: Ada"/);
});
- it("puts read aloud, raw markdown and pin in a room reply's more menu", () => {
+ it("puts read aloud (with its setup hint while no voice is set up), raw markdown and pin in a room reply's more menu", () => {
vi.unstubAllGlobals();
const messages = [text("a", "bot", "The build is green")];
flushSync(() => root.render(createElement(Transcript, {
@@ -100,6 +101,23 @@ describe("room message actions", () => {
const speak = items.find((item) => item.textContent === "Read this aloud");
expect(speak?.title).toBe("Add an ElevenLabs key in an agent profile to read messages aloud");
expect(speak?.disabled).toBe(true);
+ expect(host.querySelector('[data-testid="message-actions"] > button[aria-label="Read this aloud"]')).toBeNull();
+ });
+
+ it("gives a room reply its own Read this aloud button once a voice is set up, out of the more menu", () => {
+ vi.unstubAllGlobals();
+ fixture.config = { tts: { configured: true, ready: true, voice: "Ada" } };
+ const messages = [text("a", "bot", "The build is green")];
+ flushSync(() => root.render(createElement(Transcript, {
+ group: { ...room, messages }, members: [lead], locale: "en", messages, transcript: messages, onReply: () => {},
+ })));
+ const bar = host.querySelector('[data-testid="message-actions"]')!;
+ const button = bar.querySelector<HTMLButtonElement>('button[aria-label="Read this aloud"]');
+ expect(button?.disabled).toBe(false);
+ flushSync(() => host.querySelector<HTMLButtonElement>('button[aria-haspopup="menu"]')!.click());
+ const items = [...host.querySelectorAll<HTMLButtonElement>("[role=menuitem]")].map((item) => item.textContent);
+ expect(items).toEqual(expect.arrayContaining(["Show raw markdown", "Pin message"]));
+ expect(items).not.toContain("Read this aloud");
});
it("copies a person's message and folds a long one", () => {
diff --git a/src/components/GroupView.tsx b/src/components/GroupView.tsx
index 1ed4a1f9cc..d4b2d0d78c 100644
--- a/src/components/GroupView.tsx
+++ b/src/components/GroupView.tsx
@@ -42,7 +42,7 @@ import { replyAndCopy, roomPin, useBotMessageMenu, userMessageMenu } from "./mes
import { forkMessageItem, ForkLinks } from "./ForkMessage";
import { PHONE_LAYOUT, useMediaQuery } from "@/lib/use-media-query";
import { RawMarkdownView, RawToggleAction } from "./RawMarkdownToggle";
-import { SpeakButton } from "./SpeakButton";
+import { SpeakButton, speakReady } from "./SpeakButton";
import { useSpeech } from "@/lib/tts/useSpeech";
import { localSystemVoiceActive } from "@/lib/local-voice";
import { CancelledTurnRow } from "./CancelledTurnRow";
@@ -198,7 +198,7 @@ function RoomTextMessage({
{canReact && m.from && <ReactAction threadId={group.threadId} message={m} />}
{botText && <CopyButton text={botText} className={messageActionClass} />}
{botText && viewRaw && <RawToggleAction active onToggle={() => setViewRaw(false)} className={messageActionClass} />}
- {botText && speaking && <SpeakButton text={botText} botId={speakerBot?.id} messageId={m.id} voiceId={speakerBot?.voice} tts={state.config?.tts} localVoice={localSystemVoiceActive()} className={cn(messageActionClass, "text-accent")} />}
+ {botText && (speaking || speakReady(state.config?.tts, speakerBot?.voice, localSystemVoiceActive())) && <SpeakButton text={botText} botId={speakerBot?.id} messageId={m.id} voiceId={speakerBot?.voice} tts={state.config?.tts} localVoice={localSystemVoiceActive()} className={cn(messageActionClass, speaking && "text-accent")} />}
</MessageActions>
);
return (
diff --git a/src/components/SpeakButton.tsx b/src/components/SpeakButton.tsx
index 729d57a7d2..9630f1b80c 100644
--- a/src/components/SpeakButton.tsx
+++ b/src/components/SpeakButton.tsx
@@ -16,10 +16,15 @@ type SpeakArgs = {
localVoice: boolean;
};
+/** Whether a message can be read aloud now: this device's own voice, or a set-up voice service with a voice picked. */
+export function speakReady(tts: ConfigStatus["tts"], voiceId: string | undefined, localVoice: boolean): boolean {
+ return localVoice || (Boolean(tts?.configured) && Boolean(voiceId || tts?.voice));
+}
+
export function useSpeakAction({ text, botId, messageId, voiceId, tts, localVoice }: SpeakArgs) {
const speech = useSpeech();
const configured = localVoice || Boolean(tts?.configured);
- const ready = localVoice || (configured && Boolean(voiceId || tts?.voice));
+ const ready = speakReady(tts, voiceId, localVoice);
const mine = speech.messageId === messageId && speech.status !== "idle";
const preparing = mine && speech.status === "preparing";
const label = !configured
diff --git a/src/components/message-menu.tsx b/src/components/message-menu.tsx
index 8f7bf07304..332d8510ad 100644
--- a/src/components/message-menu.tsx
+++ b/src/components/message-menu.tsx
@@ -80,7 +80,8 @@ export function useBotMessageMenu({
const speak = useSpeakAction({ text, botId, messageId, voiceId, tts, localVoice });
const teamMemory = useTeamMemoryItem(text, messageId, botId, threadId);
const items: MessageMenuItem[] = [];
- if (canSpeak && text) {
+ // Once a voice is set up, Read this aloud is its own button in the hover bar; the menu keeps only the setup hint.
+ if (canSpeak && text && !speak.ready) {
items.push({
key: "speak",
icon: speak.mine ? <Square {...ACTION_ICON} /> : <Volume2 {...ACTION_ICON} />,