Skip to content

Commit cdd8c38

Browse files
authored
Send while dictating stops it and sends the words, in one press (#2824)
* Send while dictating stops it and sends the words, in one press Send was off while live dictation listened, so a dictated message took Stop, then Send. Send now stops listening, waits for the recognizer's last words, and sends through the same submit path and checks as any other send. If dictation fails, or nothing was heard and the box is empty, nothing is sent and the notice above the box says why. Enter still only stops, so the person can read what was heard first. * Test Send while dictating in the Desktop window with its channel's messages out of order * Wait for the page, not the clock, in the Send-while-dictating browser test
1 parent ecbaaf2 commit cdd8c38

7 files changed

Lines changed: 704 additions & 16 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ jobs:
682682
app/tests/component-bots-layout.test.tsx
683683
app/tests/composer-dictation-language.test.tsx
684684
app/tests/composer-keeps-draft-browser.test.tsx
685+
app/tests/composer-send-while-dictating.test.tsx
685686
app/tests/composer-touch-return.test.tsx
686687
app/tests/computer-panel-phone-layout.test.tsx
687688
app/tests/computer-starting-browser.test.tsx

‎app/src/components/channels/composer/composer.tsx‎

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ import { useDictation } from "./use-dictation";
9090
/** One shared empty list, so a box with no commands does not rebuild its triggers per render. */
9191
const NO_COMMANDS: readonly CommandOption[] = [];
9292

93+
/** Send was pressed while live dictation listened, it heard nothing, and the box had nothing else. */
94+
const NOTHING_HEARD = "No words were heard, so nothing was sent. Try again.";
95+
9396
/**
9497
* The SDK's upload failure, derived rather than imported for the reason `attachments.ts` records
9598
* against the same derivation: `@copilotkit/shared` declares `AttachmentUploadError` and is not a
@@ -504,6 +507,12 @@ export function Composer({
504507
valueRef.current = value;
505508
/** The box as it was when live dictation began: each transcript replaces what came after it. */
506509
const liveBase = useRef<Segment[]>([]);
510+
/**
511+
* The box as live dictation last wrote it, kept as it is written rather than read back from
512+
* `value`: Send waits for the last words, and sends these even if the render showing them has not
513+
* happened yet. Typing lets dictation go, so nothing else writes the box while this is in use.
514+
*/
515+
const liveLatest = useRef<Segment[]>([]);
507516
const dictation = useDictation(
508517
{
509518
onTranscript: (transcript, intent) => {
@@ -513,9 +522,12 @@ export function Composer({
513522
},
514523
onLiveStart: () => {
515524
liveBase.current = valueRef.current;
525+
liveLatest.current = valueRef.current;
516526
},
517527
onLiveText: (transcript) => {
518-
setValue(appendDictation(liveBase.current, transcript));
528+
const next = appendDictation(liveBase.current, transcript);
529+
liveLatest.current = next;
530+
setValue(next);
519531
},
520532
},
521533
disabled || voiceCall?.active === true,
@@ -1540,7 +1552,7 @@ export function Composer({
15401552
// sends: the person reads what was heard first, and the next Enter sends it. Asked before
15411553
// anything else, so an empty box still stops listening.
15421554
if (dictation.live.session.active) {
1543-
dictation.live.session.stop();
1555+
void dictation.live.session.stop();
15441556
return;
15451557
}
15461558
// `canSendDraft` rather than `isEmpty`: a screenshot with no words is a message, and an
@@ -1737,6 +1749,39 @@ export function Composer({
17371749
void submitDraft(value);
17381750
}, [value, submitDraft]);
17391751

1752+
/**
1753+
* SEND WHILE LIVE DICTATION LISTENS STOPS IT AND SENDS, IN ONE PRESS.
1754+
*
1755+
* Enter only stops (see `submitDraft`), so the person can read what was heard first. The Send
1756+
* button is the other choice: it stops listening, waits for the recognizer's last words, then sends
1757+
* through `submitDraft` like any other send, with every one of its checks. "waiting" while the last
1758+
* words are on their way, "heard" once they are in. A failure sends nothing, and the notice above
1759+
* the box says why; so does a stop that heard nothing, with nothing else to send.
1760+
*/
1761+
const [dictatedSend, setDictatedSend] = useState<"waiting" | "heard" | null>(
1762+
null,
1763+
);
1764+
const sendWhileDictating = () => {
1765+
if (dictatedSend) return;
1766+
// Still asking the system for permission: nothing has been heard, and the box is what goes.
1767+
if (dictation.live.state.phase === "preparing")
1768+
liveLatest.current = valueRef.current;
1769+
setDictatedSend("waiting");
1770+
void dictation.live.session
1771+
.stop()
1772+
.then((heard) => setDictatedSend(heard ? "heard" : null));
1773+
};
1774+
useEffect(() => {
1775+
if (dictatedSend !== "heard") return;
1776+
setDictatedSend(null);
1777+
const segments = liveLatest.current;
1778+
if (!canSendDraft(toDraft(segments, staged))) {
1779+
dictation.live.session.report(NOTHING_HEARD);
1780+
return;
1781+
}
1782+
void submitDraft(segments);
1783+
}, [dictatedSend, dictation.live.session, staged, submitDraft]);
1784+
17401785
const wasDictating = useRef(false);
17411786
/**
17421787
* Put the caret back the moment the composer can accept it again.
@@ -1771,8 +1816,13 @@ export function Composer({
17711816
promptAreaRef.current?.focus();
17721817
}, [autoFocus, disabled, isBusy, dictation.busy, touchTyping]);
17731818

1819+
/** The Send button: the one way into the form's submit. Enter goes to `submitDraft` directly. */
17741820
const handleFormSubmit = (event: FormEvent<HTMLFormElement>) => {
17751821
event.preventDefault();
1822+
if (dictation.live.session.active) {
1823+
sendWhileDictating();
1824+
return;
1825+
}
17761826
void submitDraft(value);
17771827
};
17781828

@@ -1816,12 +1866,13 @@ export function Composer({
18161866
const sendable = canSendDraft(draft) && !tooManyStaged;
18171867
/** Something to send, mid-turn, with a queue to put it in. */
18181868
const parking = canQueue && sendable;
1869+
/*
1870+
* Live dictation does not turn Send off: the words are in the box as they are heard, and Send stops
1871+
* listening and sends them (`sendWhileDictating`). A recording does, because the box is hidden and
1872+
* its own controls have their own send.
1873+
*/
18191874
const canSend =
1820-
!disabled &&
1821-
!dictation.busy &&
1822-
!dictation.live.active &&
1823-
sendable &&
1824-
(!isBusy || canQueue);
1875+
!disabled && !dictation.busy && sendable && (!isBusy || canQueue);
18251876
/** Live dictation began from the microphone: the caret goes to the end of the box, where Enter stops it. */
18261877
const focusForDictation = useCallback(() => {
18271878
promptAreaRef.current?.focus();

‎app/src/components/channels/composer/dictation-controls.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export function DictationButton({
5959
/**
6060
* LIVE DICTATION'S ONE BUTTON. The microphone starts it, and while it listens the same button is Stop,
6161
* in the same place and size, so the way out is where the way in was. Stopping keeps every word in
62-
* the box and sends nothing; Enter in the box does the same.
62+
* the box and sends nothing; Enter in the box does the same. Send stops it too, waits for the last
63+
* words, and sends them, in one press.
6364
*/
6465
function LiveDictationButton({
6566
dictation,
@@ -99,7 +100,7 @@ function LiveDictationButton({
99100
title="Stop dictation"
100101
className="relative size-8 shrink-0 self-end rounded-full bg-muted text-foreground hover:bg-muted"
101102
disabled={stopping}
102-
onClick={session.stop}
103+
onClick={() => void session.stop()}
103104
size="icon"
104105
type="button"
105106
variant="ghost"

‎app/src/lib/dictation/live.ts‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
* numbers each phrase as its own result, and rebuilding covers both.
2121
*
2222
* The person stops with the Stop button or Enter. The recognizer is asked to hand over its last words,
23-
* which keep landing in the box until it ends (a short wait at most), and nothing is sent. Typing in the
24-
* box while listening also stops it, at once, and keeps everything on screen as it is.
23+
* which keep landing in the box until it ends (a short wait at most), and nothing is sent. Send stops it
24+
* the same way, waits for those last words, then sends the message, in one press. Typing in the box
25+
* while listening also stops it, at once, and keeps everything on screen as it is.
2526
*/
2627
import {
2728
type Recognition,
@@ -240,6 +241,8 @@ export class LiveDictation {
240241
private generation = 0;
241242
private recognition?: Recognition;
242243
private grace?: ReturnType<typeof setTimeout>;
244+
/** Stop was asked for: settles once the last words are in, or listening ended some other way. */
245+
private stopped?: ReturnType<typeof Promise.withResolvers<boolean>>;
243246

244247
constructor(
245248
private readonly deps: {
@@ -323,23 +326,49 @@ export class LiveDictation {
323326
}
324327
};
325328

326-
/** Stop listening and keep the words, including the last ones still on their way. */
327-
stop = () => {
329+
/**
330+
* Stop listening and keep the words, including the last ones still on their way.
331+
*
332+
* Settles true once they are all in the box (the recognizer ended, or the grace ran out), and false
333+
* when listening ended any other way: it failed, and the notice says why, or something else let it
334+
* go (typing in the box). Send waits on it to send the whole message in one press.
335+
*/
336+
stop = (): Promise<boolean> => {
328337
if (this.state.phase === "preparing") {
329338
this.release();
330-
return;
339+
return Promise.resolve(true);
331340
}
332-
if (this.state.phase !== "listening" || !this.recognition) return;
341+
if (this.state.phase === "stopping" && this.stopped)
342+
return this.stopped.promise;
343+
if (this.state.phase !== "listening" || !this.recognition)
344+
return Promise.resolve(this.state.phase === "idle");
345+
const stopped = Promise.withResolvers<boolean>();
346+
this.stopped = stopped;
333347
this.update({ phase: "stopping" });
334348
const generation = this.generation;
335349
this.recognition.stop();
336350
this.grace = setTimeout(() => {
337-
if (generation === this.generation) this.release();
351+
if (generation !== this.generation) return;
352+
this.settle(true);
353+
this.release();
338354
}, STOP_GRACE_MS);
355+
return stopped.promise;
339356
};
340357

358+
/** Say why nothing came of listening, where a problem with the recognizer would be said. */
359+
report = (error: string) => {
360+
if (this.state.phase === "idle") this.update({ phase: "error", error });
361+
};
362+
363+
private settle(clean: boolean) {
364+
const stopped = this.stopped;
365+
this.stopped = undefined;
366+
stopped?.resolve(clean);
367+
}
368+
341369
/** Stop at once and take no more words. What is in the box stays. */
342370
release = () => {
371+
this.settle(false);
343372
this.generation++;
344373
clearTimeout(this.grace);
345374
const recognition = this.recognition;
@@ -358,6 +387,7 @@ export class LiveDictation {
358387
clearTimeout(this.grace);
359388
this.recognition = undefined;
360389
this.update(state);
390+
this.settle(state.phase === "idle");
361391
}
362392

363393
/**

‎app/tests/composer-live-dictation.test.tsx‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,156 @@ for (const compact of [true, false]) {
122122
});
123123
}
124124

125+
/**
126+
* FOUND IN DOGFOODING (2026-12-02). Send while dictating did nothing: the button was off until the
127+
* person pressed Stop. Send now stops listening, waits for the last words, and sends them, in one
128+
* press, as Claude Code does. Enter still only stops (above), so the person can read what was heard.
129+
*/
130+
for (const compact of [true, false]) {
131+
test(`${compact ? "compact" : "default"} desktop: Send while listening stops, waits for the last words, and sends them in one press`, async () => {
132+
noProvider();
133+
const { calls } = installDesktopShell(desktop);
134+
const sent: string[] = [];
135+
const { view, box } = renderComposer(compact, sent);
136+
fireEvent.click(
137+
await view.findByRole("button", { name: "Dictate a message" }),
138+
);
139+
await waitFor(() =>
140+
expect(box().textContent).toBe("Existing draft book a table for two"),
141+
);
142+
const send = view.getByRole("button", {
143+
name: "Send message",
144+
}) as HTMLButtonElement;
145+
expect(send.disabled).toBe(false);
146+
147+
fireEvent.click(send);
148+
// "tonight" is the desktop's last word, handed over only once it was asked to stop.
149+
await waitFor(() =>
150+
expect(sent).toEqual(["Existing draft book a table for two tonight"]),
151+
);
152+
await view.findByRole("button", { name: "Dictate a message" });
153+
expect(view.queryByRole("button", { name: "Stop dictation" })).toBeNull();
154+
expect(calls.map((call) => call.command)).toContain(
155+
"plugin:openbot-desktop|speech_stop",
156+
);
157+
expect(box().textContent).toBe("");
158+
});
159+
}
160+
161+
test("desktop: Send while listening sends the last words even when the Desktop window's channel delivers them out of order", async () => {
162+
noProvider();
163+
const { calls } = installDesktopShell({ ...desktop, reorder: true });
164+
const sent: string[] = [];
165+
const { view, box } = renderComposer(true, sent);
166+
fireEvent.click(
167+
await view.findByRole("button", { name: "Dictate a message" }),
168+
);
169+
await waitFor(() =>
170+
expect(box().textContent).toBe("Existing draft book a table for two"),
171+
);
172+
fireEvent.click(view.getByRole("button", { name: "Send message" }));
173+
await waitFor(() =>
174+
expect(sent).toEqual(["Existing draft book a table for two tonight"]),
175+
);
176+
await view.findByRole("button", { name: "Dictate a message" });
177+
expect(calls.map((call) => call.command)).toEqual([
178+
"plugin:openbot-desktop|speech_available",
179+
"plugin:openbot-desktop|speech_start",
180+
"plugin:openbot-desktop|speech_stop",
181+
]);
182+
});
183+
184+
test("web: Send while Chrome's Web Speech API listens stops it and sends the words", async () => {
185+
noProvider();
186+
installChrome(null);
187+
const sent: string[] = [];
188+
const { view, box } = renderComposer(false, sent);
189+
fireEvent.click(
190+
await view.findByRole("button", { name: "Dictate a message" }),
191+
);
192+
await waitFor(() =>
193+
expect(box().textContent).toBe(
194+
"Existing draft I am opening the expenses page",
195+
),
196+
);
197+
fireEvent.click(view.getByRole("button", { name: "Send message" }));
198+
await waitFor(() =>
199+
expect(sent).toEqual(["Existing draft I am opening the expenses page"]),
200+
);
201+
await view.findByRole("button", { name: "Dictate a message" });
202+
});
203+
204+
test("desktop: Send while listening sends nothing when dictation fails, and says why", async () => {
205+
noProvider();
206+
installDesktopShell({
207+
...desktop,
208+
atStop: [{ type: "error", error: "audio-capture" }],
209+
});
210+
const sent: string[] = [];
211+
const { view, box } = renderComposer(true, sent);
212+
fireEvent.click(
213+
await view.findByRole("button", { name: "Dictate a message" }),
214+
);
215+
await waitFor(() =>
216+
expect(box().textContent).toBe("Existing draft book a table for two"),
217+
);
218+
fireEvent.click(view.getByRole("button", { name: "Send message" }));
219+
const alert = await view.findByRole("alert");
220+
expect(alert.textContent).toBe(liveProblem("audio-capture", "mac") ?? "");
221+
expect(box().textContent).toBe("Existing draft book a table for two");
222+
await new Promise((resolve) => setTimeout(resolve, 20));
223+
expect(sent).toEqual([]);
224+
});
225+
226+
test("desktop: Send while listening sends nothing when nothing was heard and the box is empty, and says why", async () => {
227+
noProvider();
228+
installDesktopShell({ heard: [said(0, "um")], atStop: [said(0, "", true)] });
229+
const sent: string[] = [];
230+
const view = render(
231+
<Composer
232+
compact
233+
onSubmit={(draft) => {
234+
sent.push(draft.text);
235+
}}
236+
/>,
237+
);
238+
const box = () => view.getByRole("textbox", { name: "Message" });
239+
fireEvent.click(
240+
await view.findByRole("button", { name: "Dictate a message" }),
241+
);
242+
await waitFor(() => expect(box().textContent).toBe("um"));
243+
fireEvent.click(view.getByRole("button", { name: "Send message" }));
244+
const alert = await view.findByRole("alert");
245+
expect(alert.textContent).toBe(
246+
"No words were heard, so nothing was sent. Try again.",
247+
);
248+
await new Promise((resolve) => setTimeout(resolve, 20));
249+
expect(sent).toEqual([]);
250+
});
251+
252+
test("desktop: Send while the system still asks for permission stops waiting and sends the box as it is", async () => {
253+
noProvider();
254+
const { calls } = installDesktopShell({
255+
...desktop,
256+
availability: "downloading",
257+
});
258+
const sent: string[] = [];
259+
const { view } = renderComposer(true, sent);
260+
fireEvent.click(
261+
await view.findByRole("button", { name: "Dictate a message" }),
262+
);
263+
await view.findByText(
264+
"Waiting for permission to use the microphone and speech recognition.",
265+
);
266+
fireEvent.click(view.getByRole("button", { name: "Send message" }));
267+
await waitFor(() => expect(sent).toEqual(["Existing draft"]));
268+
await view.findByRole("button", { name: "Dictate a message" });
269+
expect(view.queryByRole("alert")).toBeNull();
270+
expect(calls.some((call) => call.command.endsWith("|speech_start"))).toBe(
271+
false,
272+
);
273+
});
274+
125275
test("desktop: Stop keeps the words in the box and sends nothing", async () => {
126276
noProvider();
127277
installDesktopShell(desktop);

0 commit comments

Comments
 (0)