VoiceMode: talking with a chat, and a microphone that cannot be opened twice (0.30.0) (#89)

Cat Bot has to be a complete equivalent of Grok Bot, which has a voice mode.
The chat could take dictation and read an answer aloud, but not hold a
spoken conversation.

- VoiceMode (the screen), useVoiceMode (the loop), voiceModeReducer (the
  rules, pure): listening -> thinking -> speaking -> listening. One big
  button ends the person's turn (a pause ends it by itself where the
  browser's recogniser runs) and cuts in while the answer is read. The app
  passes onTurn(text) => Promise<answer> and, if it has one, a studio
  speaker.
- The microphone and the voice follow the phase and nothing else
  (voiceModeWants): never both open, neither open when the call is off. Two
  empty takes, a failed transcription or an answer that never comes each end
  the call in words, with "Talk again".
- Fix, useDictation: a second start() before the browser had handed the
  microphone over opened a second microphone and orphaned the first, whose
  track stayed live with nothing left to stop it. One request at a time now,
  and a take cancelled while the microphone was still opening closes it
  where it arrives. Found by this change's browser test.
- Its styles are their own section and styles/voice.css, so the embedded
  chat sheet stays inside its 11 KB budget.

test/voice-mode.test.js (8) and test/browser-voice.mjs (a real browser, a
fake-device microphone, every opened track counted). pnpm run verify passes.


Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
CatoandClaude authored and GitHub committed 2026-11-13 08:18:12 +01:00
1 parent e690b2254a
commit 8452fb12ab
40 files changed
+1706 -10

No files matched your search

+9
View File
@@ -1,5 +1,14 @@
# Changelog
## 0.30.0 — `VoiceMode`: talking with a chat, and a microphone that cannot be opened twice
Owner, 2026-11-13: Cat Bot has to be a complete equivalent of Grok Bot, which has a voice mode. The chat could take dictation and read an answer aloud, but not hold a spoken conversation.
- `VoiceMode` (the screen), `useVoiceMode` (the loop) and `voiceModeReducer` (the rules, pure): listening → thinking → speaking → listening. One big button: while listening it ends the person's turn (where the browser's own recogniser runs, a pause ends it by itself); while the answer is read it cuts in. The app passes `onTurn(text) => Promise<answer>` and, if it has one, a studio `speaker`; the microphone options are the composer's own.
- The microphone and the voice follow the phase and nothing else (`voiceModeWants`): never both open, neither open when the call is off. Two takes in a row with nothing in them close the call ("Nothing was heard"), and so do a failed transcription and an answer that never comes, each with its sentence and a "Talk again".
- **Fix, `useDictation`:** a second `start()` before the browser had handed the microphone over opened a second microphone and orphaned the first, whose track stayed live with nothing left to stop it (a double tap on the mic could do it). One request at a time now, and a take cancelled while the microphone was still opening closes it where it arrives.
- `test/voice-mode.test.js` (8, the rules in Node) and `test/browser-voice.mjs` (a real browser with a fake-device microphone, every opened track counted: the loop, cutting in, End, silence, no answer, a dead transcription, 390px and 320px). `demo/voice.html`.
## 0.29.0 — `Messenger`: people talking to people, and to the agents among them
Owner, 2026-11-12, looking at OrangeCat's direct messages on a phone: the Cat had answered in a conversation between two people and "it's hard to tell whether it was the cat or my friend"; nothing said an AI was taking part; "you scroll down and then the half screen is taken"; and nothing could be attached. That messenger was built in the app (about 8,700 lines), so none of it could be fixed for the next product either. This is the messenger as a package.
+22
View File
@@ -679,6 +679,28 @@ punctuation but a closing colon, plus an `---` beside it. A bold sentence
it is, so it never flashes. `dropReplyLabel(text)` is exported for apps that
pass their own `replies`.
### Voice mode — talking with a chat
```tsx
import { VoiceMode } from "@bitbaum/chatkit/react";
<VoiceMode
open={talking}
onClose={() => setTalking(false)}
name="Day Planner"
dictation={{ transcribeUrl: "/api/transcribe" }}
onTurn={async (text) => (await send(text)).answer}
/>;
```
A spoken conversation, full screen: listening → thinking → speaking →
listening. One big button ends the person's turn (a pause ends it by itself
where the browser's own recogniser runs) and cuts in while the answer is read.
Open it from a tap. The microphone and the voice are never both open, and
neither is open once the call is over; nothing heard twice, a failed
transcription or an answer that never comes each end the call in words.
`useVoiceMode` is the loop without the screen; `voiceModeReducer` is its rules.
### Under every answer — Copy, Listen, Share
`AnswerActions` is the row under an answer, the same in every product:
+2
View File
@@ -2,6 +2,7 @@ import { mountScrollFixture } from "./scroll.js";
import { mountAppFixture } from "./app.js";
import { mountFieldFixture } from "./field.js";
import { mountMessengerFixture } from "./messenger.js";
import { mountVoiceFixture } from "./voice.js";
// The visual check: every part of the chat on one page, in light and dark.
// `pnpm run demo` builds demo/dist/; open demo/index.html. Screenshot it at
// 390px and at desktop width before calling a change to chatkit done.
@@ -402,3 +403,4 @@ mountScrollFixture();
mountAppFixture();
mountFieldFixture();
mountMessengerFixture();
mountVoiceFixture();
+20
View File
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voice mode</title>
<link rel="stylesheet" href="../node_modules/@bitbaum/design-tokens/tokens.css" />
<link rel="stylesheet" href="../styles.css" />
<style>
body {
margin: 0;
font: 16px system-ui;
}
</style>
</head>
<body>
<div id="voice-fixture"></div>
<script src="dist/demo.js"></script>
</body>
</html>
+82
View File
@@ -0,0 +1,82 @@
// The fixture behind demo/voice.html and test/browser-voice.mjs: VoiceMode the
// way an app mounts it. The microphone is real (the browser's fake device in
// the test); the transcription, the answer and the voice are stubs the page's
// URL steers, so the test is deterministic:
// ?heard=<text> what the "server" says was said ("" for nothing)
// ?answer=<text> what the chat answers ("" for no answer)
// ?fail=mic the transcription fails
import { StrictMode, useState } from "react";
import { createRoot } from "react-dom/client";
import { VoiceMode, type Speaker } from "../src/react/index.js";
declare global {
interface Window {
__voice: { turns: string[]; spoken: string[]; speaking: boolean };
}
}
function Fixture() {
const q = new URLSearchParams(location.search);
const heard = q.get("heard") ?? "what is next today";
const answer = q.get("answer") ?? "The **report**, at five.";
const [open, setOpen] = useState(false);
window.__voice ??= { turns: [], spoken: [], speaking: false };
// A voice that takes a moment, so "speaking" can be seen and cut into.
const speaker: Speaker = {
speak(text) {
window.__voice.spoken.push(text);
window.__voice.speaking = true;
return new Promise<void>((resolve) => {
const t = setTimeout(
() => {
window.__voice.speaking = false;
resolve();
},
Number(q.get("voiceMs") ?? 600),
);
speaker.cancel = () => {
clearTimeout(t);
window.__voice.speaking = false;
resolve();
};
});
},
cancel() {},
};
return (
<div style={{ padding: 16 }}>
<button type="button" id="open-voice" onClick={() => setOpen(true)}>
Talk to Day Planner
</button>
<VoiceMode
open={open}
onClose={() => setOpen(false)}
name="Day Planner"
speaker={speaker}
dictation={{
prefer: "server",
rememberKey: null,
transcribe: async () => {
if (q.get("fail") === "mic") throw new Error("down");
return heard;
},
}}
onTurn={async (text) => {
window.__voice.turns.push(text);
await new Promise((r) => setTimeout(r, 150));
return answer;
}}
/>
</div>
);
}
export function mountVoiceFixture() {
const el = document.getElementById("voice-fixture");
if (el)
createRoot(el).render(
<StrictMode>
<Fixture />
</StrictMode>,
);
}
+1
View File
@@ -11,6 +11,7 @@ export * from "./attachments.js";
export * from "./image.js";
export * from "./replies.js";
export * from "./speech.js";
export * from "./voice-mode.js";
export * from "./rich.js";
export * from "./review.js";
export * from "./conversations.js";
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC"}
+1
View File
@@ -11,6 +11,7 @@ export * from "./attachments.js";
export * from "./image.js";
export * from "./replies.js";
export * from "./speech.js";
export * from "./voice-mode.js";
export * from "./rich.js";
export * from "./review.js";
export * from "./conversations.js";
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC"}
+37
View File
@@ -0,0 +1,37 @@
import type { DictationProblem } from "../dictation.js";
import type { VoiceModeEnd } from "../voice-mode.js";
import { type UseVoiceModeOptions } from "./use-voice-mode.js";
export type VoiceModeLabels = {
title: string;
/** Under the button, per phase: what is happening and what a press does. */
listening: string;
thinking: string;
speaking: string;
/** The big button's name for a screen reader, per phase. */
pressListening: string;
pressSpeaking: string;
pressThinking: string;
end: string;
again: string;
/** Why the call is over. */
ended: Record<Exclude<VoiceModeEnd, "ended">, string>;
dictation: Record<DictationProblem, string>;
};
export declare const DEFAULT_VOICE_MODE_LABELS: VoiceModeLabels;
export type VoiceModeProps = UseVoiceModeOptions & {
open: boolean;
onClose: () => void;
/** Who is being talked to, shown above the button. */
name?: string;
labels?: Partial<VoiceModeLabels>;
};
/**
* A spoken conversation, full screen: one big button, what was heard, what is
* being said back, and End. Open it from a tap (browsers open a microphone
* and a voice only inside one); closing it shuts both.
*
* The loop and its rules are `useVoiceMode` and `voiceModeReducer`. This is
* only the screen, so every chat that offers voice looks and behaves the same.
*/
export declare function VoiceMode({ open, onClose, name, labels: given, ...opts }: VoiceModeProps): import("react").JSX.Element;
//# sourceMappingURL=VoiceMode.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"VoiceMode.d.ts","sourceRoot":"","sources":["../../src/react/VoiceMode.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,OAAO,KAAK,EAAE,YAAY,EAAkB,MAAM,kBAAkB,CAAC;AAGrE,OAAO,EAAgB,KAAK,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAE7E,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,4EAA4E;IAC5E,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,4DAA4D;IAC5D,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,4BAA4B;IAC5B,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,YAAY,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,CAAC;IACtD,SAAS,EAAE,MAAM,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;CAC7C,CAAC;AAEF,eAAO,MAAM,yBAAyB,EAAE,eAgBvC,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,mBAAmB,GAAG;IACjD,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,sDAAsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;CACnC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE,cAAc,+BA0GxF"}
+79
View File
@@ -0,0 +1,79 @@
"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useEffect, useRef } from "react";
import { plainTextForSpeech } from "../speech.js";
import { IconX } from "./icons.js";
import { DEFAULT_DICTATION_MESSAGES } from "../dictation.js";
import { useVoiceMode } from "./use-voice-mode.js";
export const DEFAULT_VOICE_MODE_LABELS = {
dictation: DEFAULT_DICTATION_MESSAGES,
title: "Talk",
listening: "Listening. Tap when you have finished.",
thinking: "Thinking…",
speaking: "Tap to cut in.",
pressListening: "I have finished speaking",
pressSpeaking: "Stop reading and listen to me",
pressThinking: "Waiting for the answer",
end: "End",
again: "Talk again",
ended: {
silence: "Nothing was heard, so the microphone is closed.",
microphone: "The microphone stopped.",
"no-answer": "No answer came. Your words are in the chat; try again there.",
},
};
/**
* A spoken conversation, full screen: one big button, what was heard, what is
* being said back, and End. Open it from a tap (browsers open a microphone
* and a voice only inside one); closing it shuts both.
*
* The loop and its rules are `useVoiceMode` and `voiceModeReducer`. This is
* only the screen, so every chat that offers voice looks and behaves the same.
*/
export function VoiceMode({ open, onClose, name, labels: given, ...opts }) {
const labels = { ...DEFAULT_VOICE_MODE_LABELS, ...given };
const vm = useVoiceMode(opts);
const ref = useRef(null);
const { start, end, state } = vm;
useEffect(() => {
const d = ref.current;
if (!d)
return;
if (open && !d.open && typeof d.showModal === "function") {
d.showModal();
start();
}
else if (!open && d.open) {
end();
d.close();
}
}, [open, start, end]);
// Unmounting ends the call too: a microphone that outlives its screen is the worst bug a chat can have.
useEffect(() => () => end(), [end]);
const phase = state.phase;
const line = phase === "listening"
? vm.dictation.liveText || labels.listening
: phase === "thinking"
? state.said
: phase === "speaking"
? plainTextForSpeech(state.answer)
: "";
const reason = state.ended && state.ended !== "ended"
? vm.dictation.problem
? labels.dictation[vm.dictation.problem]
: labels.ended[state.ended]
: null;
return (_jsxs("dialog", { ref: ref, className: "ck-voicemode", "aria-label": name ? `${labels.title}: ${name}` : labels.title, onClose: () => {
end();
onClose();
}, children: [_jsxs("div", { className: "ck-voicemode-top", children: [_jsx("span", { className: "ck-voicemode-name", children: name ?? labels.title }), _jsx("button", { type: "button", className: "ck-icon-btn", "aria-label": labels.end, onClick: onClose, children: _jsx(IconX, {}) })] }), _jsx("div", { className: "ck-voicemode-body", "data-phase": phase, "data-mic": vm.dictation.status, children: phase === "off" ? (_jsxs(_Fragment, { children: [reason && (_jsx("p", { className: "ck-voicemode-reason", role: "status", children: reason })), _jsx("button", { type: "button", className: "ck-voicemode-again", onClick: start, children: labels.again })] })) : (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "ck-voicemode-orb", "data-phase": phase, disabled: phase === "thinking", "aria-label": phase === "listening"
? labels.pressListening
: phase === "speaking"
? labels.pressSpeaking
: labels.pressThinking, onClick: vm.press, children: _jsx("span", { className: "ck-voicemode-ring", "aria-hidden": "true" }) }), _jsx("p", { className: "ck-voicemode-status", role: "status", children: phase === "listening"
? labels.listening
: phase === "thinking"
? labels.thinking
: labels.speaking }), line && line !== labels.listening && (_jsx("p", { className: "ck-voicemode-line", "data-phase": phase, children: line }))] })) }), _jsx("div", { className: "ck-voicemode-foot", children: _jsx("button", { type: "button", className: "ck-voicemode-end", onClick: onClose, children: labels.end }) })] }));
}
//# sourceMappingURL=VoiceMode.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"VoiceMode.js","sourceRoot":"","sources":["../../src/react/VoiceMode.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,YAAY,EAA4B,MAAM,qBAAqB,CAAC;AAmB7E,MAAM,CAAC,MAAM,yBAAyB,GAAoB;IACxD,SAAS,EAAE,0BAA0B;IACrC,KAAK,EAAE,MAAM;IACb,SAAS,EAAE,wCAAwC;IACnD,QAAQ,EAAE,WAAW;IACrB,QAAQ,EAAE,gBAAgB;IAC1B,cAAc,EAAE,0BAA0B;IAC1C,aAAa,EAAE,+BAA+B;IAC9C,aAAa,EAAE,wBAAwB;IACvC,GAAG,EAAE,KAAK;IACV,KAAK,EAAE,YAAY;IACnB,KAAK,EAAE;QACL,OAAO,EAAE,iDAAiD;QAC1D,UAAU,EAAE,yBAAyB;QACrC,WAAW,EAAE,8DAA8D;KAC5E;CACF,CAAC;AAUF;;;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,EAAE,KAAK,YAChE,MAAM,CAAC,KAAK,GACN,IACR,CACJ,CAAC,CAAC,CAAC,CACF,8BACE,iBACE,IAAI,EAAC,QAAQ,EACb,SAAS,EAAC,kBAAkB,gBAChB,KAAK,EACjB,QAAQ,EAAE,KAAK,KAAK,UAAU,gBAE5B,KAAK,KAAK,WAAW;gCACnB,CAAC,CAAC,MAAM,CAAC,cAAc;gCACvB,CAAC,CAAC,KAAK,KAAK,UAAU;oCACpB,CAAC,CAAC,MAAM,CAAC,aAAa;oCACtB,CAAC,CAAC,MAAM,CAAC,aAAa,EAE5B,OAAO,EAAE,EAAE,CAAC,KAAK,YAEjB,eAAM,SAAS,EAAC,mBAAmB,iBAAa,MAAM,GAAG,GAClD,EACT,YAAG,SAAS,EAAC,qBAAqB,EAAC,IAAI,EAAC,QAAQ,YAC7C,KAAK,KAAK,WAAW;gCACpB,CAAC,CAAC,MAAM,CAAC,SAAS;gCAClB,CAAC,CAAC,KAAK,KAAK,UAAU;oCACpB,CAAC,CAAC,MAAM,CAAC,QAAQ;oCACjB,CAAC,CAAC,MAAM,CAAC,QAAQ,GACnB,EACH,IAAI,IAAI,IAAI,KAAK,MAAM,CAAC,SAAS,IAAI,CACpC,YAAG,SAAS,EAAC,mBAAmB,gBAAa,KAAK,YAC/C,IAAI,GACH,CACL,IACA,CACJ,GACG,EAEN,cAAK,SAAS,EAAC,mBAAmB,YAChC,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,kBAAkB,EAAC,OAAO,EAAE,OAAO,YAChE,MAAM,CAAC,GAAG,GACJ,GACL,IACC,CACV,CAAC;AACJ,CAAC"}
+4
View File
@@ -20,6 +20,10 @@ export { Markdown, SourcesRow, defaultRenderLink, defaultRenderCitation } from "
export { AnswerActions, DEFAULT_ANSWER_ACTIONS_LABELS } from "./AnswerActions.js";
export type { AnswerActionsLabels } from "./AnswerActions.js";
export { useSpeech } from "./use-speech.js";
export { useVoiceMode } from "./use-voice-mode.js";
export type { UseVoiceModeOptions, VoiceModeController } from "./use-voice-mode.js";
export { VoiceMode, DEFAULT_VOICE_MODE_LABELS } from "./VoiceMode.js";
export type { VoiceModeProps, VoiceModeLabels } from "./VoiceMode.js";
export type { SpeechController } from "./use-speech.js";
export { Sheet, prefersSheet } from "./Sheet.js";
export type { CitationMap, RenderLink, RenderCitation, RenderSources } from "./Markdown.js";
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAClE,YAAY,EACV,aAAa,EACb,cAAc,EACd,sBAAsB,EACtB,aAAa,EACb,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,eAAe,EACf,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,YAAY,EACZ,YAAY,GACb,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,8BAA8B,EAAE,MAAM,qBAAqB,CAAC;AACrF,YAAY,EACV,mBAAmB,EACnB,oBAAoB,EACpB,wBAAwB,EACxB,UAAU,GACX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,aAAa,EACb,cAAc,EACd,cAAc,EACd,6BAA6B,GAC9B,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,M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AC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC9F,YAAY,EACV,aAAa,EACb,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,WAAW,EACX,iBAAiB,GAClB,MAAM,mBAAmB,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAClE,YAAY,EACV,aAAa,EACb,cAAc,EACd,sBAAsB,EACtB,aAAa,EACb,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,eAAe,EACf,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,YAAY,EACZ,YAAY,GACb,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,8BAA8B,EAAE,MAAM,qBAAqB,CAAC;AACrF,YAAY,EACV,mBAAmB,EACnB,oBAAoB,EACpB,wBAAwB,EACxB,UAAU,GACX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,aAAa,EACb,cAAc,EACd,cAAc,EACd,6BAA6B,GAC9B,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAA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AAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACxE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,uBAAuB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC9E,YAAY,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,cAAc,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC9F,YAAY,EACV,aAAa,EACb,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,WAAW,EACX,iBAAiB,GAClB,MAAM,mBAAmB,CAAC"}
+2
View File
@@ -15,6 +15,8 @@ export { ChatScreen } from "./Screen.js";
export { Markdown, SourcesRow, defaultRenderLink, defaultRenderCitation } from "./Markdown.js";
export { AnswerActions, DEFAULT_ANSWER_ACTIONS_LABELS } from "./AnswerActions.js";
export { useSpeech } from "./use-speech.js";
export { useVoiceMode } from "./use-voice-mode.js";
export { VoiceMode, DEFAULT_VOICE_MODE_LABELS } from "./VoiceMode.js";
export { Sheet, prefersSheet } from "./Sheet.js";
export { useDictation } from "./use-dictation.js";
export { AttachMenu, DEFAULT_ATTACH_MENU_LABELS } from "./AttachMenu.js";
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAQlE,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAA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 export function useDictation(opts) {
const recorderRef = useRef(null);
const streamRef = useRef(null);
const cancelledRef = useRef(false);
/** True from the request for a microphone until the browser answers it. */
const openingRef = useRef(false);
const capTimer = useRef(null);
const locale = useCallback(() => {
const l = optsRef.current.lang ??
@@ -206,6 +208,14 @@ export function useDictation(opts) {
setStatus("idle");
return;
}
// One microphone at a time. `status` stays "idle" until the browser hands
// the microphone over, so a second start in that gap (a double tap, an
// effect that runs twice) used to open a second microphone and orphan the
// first: its track stayed live with nothing left to stop it. Found by
// voice mode's browser test, 2026-11-13.
if (openingRef.current)
return;
openingRef.current = true;
setProblem(null);
cancelledRef.current = false;
let stream;
@@ -213,10 +223,19 @@ export function useDictation(opts) {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
}
catch (e) {
openingRef.current = false;
setProblem(problemForRecording(e));
setStatus("idle");
return;
}
openingRef.current = false;
// Cancelled while the browser was still opening it: this microphone was
// never wanted, so it is closed here, where it arrives.
if (cancelledRef.current) {
for (const t of stream.getTracks())
t.stop();
return;
}
streamRef.current = stream;
let rec;
try {
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"use-dictation.js","sourceRoot":"","sources":["../../src/react/use-dictation.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AACvF,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,yBAAyB,EACzB,gBAAgB,EAChB,aAAa,EACb,0BAA0B,EAC1B,YAAY,EACZ,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACT,yBAAyB,EACzB,UAAU,EACV,uBAAuB,EACvB,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,QAAQ,EACR,kBAAkB,GAInB,MAAM,iBAAiB,CAAC;AAiBzB,SAAS,UAAU;IACjB,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO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AC,IAAI;YACpB,CAAC,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YACtE,EAAE,CAAC;QACL,OAAO,CAAC,IAAI,CAAC,OAAO,SAAS,KAAK,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;IACrF,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE;QACnC,MLine truncated
{"version":3,"file":"use-dictation.js","sourceRoot":"","sources":["../../src/react/use-dictation.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AACvF,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,yBAAyB,EACzB,gBAAgB,EAChB,aAAa,EACb,0BAA0B,EAC1B,YAAY,EACZ,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACT,yBAAyB,EACzB,UAAU,EACV,uBAAuB,EACvB,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,QAAQ,EACR,kBAAkB,GAInB,MAAM,iBAAiB,CAAC;AAiBzB,SAAS,UAAU;IACjB,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO,SAAS,CAAC;IACpD,MAAM,CAAC,GAAG,MAGT,CAAC;IACF,OAAO,CAAC,CAAC,iBAAiB,IAAI,CAAC,CAAC,uBAAuB,CAAC;AAC1D,CAAC;AAED,SAAS,SAAS;IAChB,OAAO,CACL,OAAO,MAAM,KAAK,WAAW;QAC7B,OAAO,aAAa,KAAK,WAAW;QACpC,CAAC,CAAC,SAAS,CAAC,YAAY,EAAE,YAAY,CACvC,CAAC;AACJ,CAAC;AAED,MAAM,WAA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,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;IACrF,CAAC,EAAE,EAALine truncated
+40
View File
@@ -0,0 +1,40 @@
import { type VoiceModeState } from "../voice-mode.js";
import type { Speaker } from "../speech.js";
import { type DictationController, type UseDictationOptions } from "./use-dictation.js";
export type UseVoiceModeOptions = {
/**
* Send the person's turn and resolve with the answer's words (markdown is
* fine; it is flattened before it is read). Resolve with "" or reject when
* no answer came: the call then ends and says so.
*/
onTurn: (text: string) => Promise<string>;
/** The same options the composer's microphone takes (`transcribeUrl`, `lang`…). */
dictation?: Omit<UseDictationOptions, "onText">;
/** A studio voice; the browser's own voice when omitted. */
speaker?: Speaker | null;
};
export type VoiceModeController = {
/** False where this device can neither listen nor speak. */
supported: boolean;
state: VoiceModeState;
dictation: DictationController;
/** Open the call. Must run inside a tap: browsers open a microphone and a voice only then. */
start: () => void;
end: () => void;
/**
* The one big button. Listening: "I have finished speaking". Speaking: cut
* in and listen again. Thinking: nothing to do.
*/
press: () => void;
};
/**
* A spoken conversation over any chat: listen, send, read the answer aloud,
* listen again (`voiceModeReducer` holds the rules). Where the browser's own
* recogniser runs, a pause ends the person's turn by itself; everywhere else
* a press of the button does, which is also how a person takes their time.
*
* The microphone and the voice follow the phase and nothing else
* (`voiceModeWants`), and both are shut when the component unmounts.
*/
export declare function useVoiceMode(opts: UseVoiceModeOptions): VoiceModeController;
//# sourceMappingURL=use-voice-mode.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"use-voice-mode.d.ts","sourceRoot":"","sources":["../../src/react/use-voice-mode.ts"],"names":[],"mappings":"AAGA,OAAO,EAIL,KAAK,cAAc,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACzB,MAAM,oBAAoB,CAAC;AAG5B,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1C,mFAAmF;IACnF,SAAS,CAAC,EAAE,IAAI,CAAC,mBAAmB,EAAE,QAAQ,CAAC,CAAC;IAChD,4DAA4D;IAC5D,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,4DAA4D;IAC5D,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,cAAc,CAAC;IACtB,SAAS,EAAE,mBAAmB,CAAC;IAC/B,8FAA8F;IAC9F,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,GAAG,EAAE,MAAM,IAAI,CAAC;IAChB;;;OAGG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,mBAAmB,CA6G3E"}
+131
View File
@@ -0,0 +1,131 @@
"use client";
import { useCallback, useEffect, useReducer, useRef } from "react";
import { VOICE_MODE_OFF, voiceModeReducer, voiceModeWants, } from "../voice-mode.js";
import { useDictation, } from "./use-dictation.js";
import { useSpeech } from "./use-speech.js";
/**
* A spoken conversation over any chat: listen, send, read the answer aloud,
* listen again (`voiceModeReducer` holds the rules). Where the browser's own
* recogniser runs, a pause ends the person's turn by itself; everywhere else
* a press of the button does, which is also how a person takes their time.
*
* The microphone and the voice follow the phase and nothing else
* (`voiceModeWants`), and both are shut when the component unmounts.
*/
export function useVoiceMode(opts) {
const [state, dispatch] = useReducer(voiceModeReducer, VOICE_MODE_OFF);
const onTurn = useRef(opts.onTurn);
useEffect(() => {
onTurn.current = opts.onTurn;
}, [opts.onTurn]);
/** Guards against an answer that arrives after the call moved on. */
const turn = useRef(0);
const dictation = useDictation({
// A pause should end the turn where the browser can hear one; a press
// ends it everywhere else.
prefer: "browser",
...opts.dictation,
onText: (text) => dispatch({ type: "heard", text }),
});
// The newest controller, for effects: set before any of them runs.
const dictationRef = useRef(dictation);
useEffect(() => {
dictationRef.current = dictation;
});
const speech = useSpeech(opts.speaker);
const { microphone, voice } = voiceModeWants(state.phase);
// The microphone follows the phase.
const micStatus = dictation.status;
useEffect(() => {
const d = dictationRef.current;
// Listening and the microphone is free: open it. This runs again when a
// take that held nothing has been written down (status back to idle), so
// a second take follows an empty first one.
if (microphone && micStatus === "idle")
d.start();
// Not the person's turn: a live take is thrown away. A take that is being
// written down is left alone; it is the turn on its way.
if (!microphone && micStatus === "listening")
d.cancel();
}, [microphone, micStatus, state.silentTakes]);
// A microphone or transcription problem ends the call, with its reason on screen.
// "silence" is a take with nothing in it: the rules count those and close
// the microphone after two. Anything else is the microphone itself.
const problem = dictation.problem;
useEffect(() => {
if (!problem || state.phase === "off")
return;
if (problem === "silence") {
dictationRef.current.clearProblem();
dispatch({ type: "heard", text: "" });
}
else {
dispatch({ type: "problem" });
}
}, [problem, state.phase]);
// The turn is sent once, when the phase becomes "thinking".
useEffect(() => {
if (state.phase !== "thinking")
return;
const mine = ++turn.current;
let live = true;
onTurn
.current(state.said)
.then((answer) => {
if (live && mine === turn.current)
dispatch({ type: "answer", text: answer ?? "" });
})
.catch(() => {
if (live && mine === turn.current)
dispatch({ type: "no-answer" });
});
return () => {
live = false;
};
}, [state.phase, state.said]);
// The voice follows the phase; when the reading ends, the person's turn begins.
const speechRef = useRef(speech);
useEffect(() => {
speechRef.current = speech;
});
const reading = useRef(false);
useEffect(() => {
if (voice && state.answer) {
reading.current = false;
speechRef.current.speak(state.answer);
}
else {
reading.current = false;
speechRef.current.stop();
}
}, [voice, state.answer]);
useEffect(() => {
if (!voice)
return;
if (speech.speaking)
reading.current = true;
// Reading began and is over (or the device has no voice at all): listen.
else if (reading.current || !speech.supported)
dispatch({ type: "spoken" });
}, [voice, speech.speaking, speech.supported]);
const start = useCallback(() => dispatch({ type: "start" }), []);
const end = useCallback(() => {
turn.current++;
dispatch({ type: "end" });
}, []);
const press = useCallback(() => {
if (state.phase === "listening")
dictationRef.current.stop();
else if (state.phase === "speaking")
dispatch({ type: "interrupt" });
}, [state.phase]);
return {
supported: dictation.supported,
state,
dictation,
start,
end,
press,
};
}
//# sourceMappingURL=use-voice-mode.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"use-voice-mode.js","sourceRoot":"","sources":["../../src/react/use-voice-mode.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACnE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,cAAc,GAEf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,YAAY,GAGb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AA8B5C;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,IAAyB;IACpD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAC;IACvE,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACnC,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC;IAC/B,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAClB,qEAAqE;IACrE,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAEvB,MAAM,SAAS,GAAG,YAAY,CAAC;QAC7B,sEAAsE;QACtE,2BAA2B;QAC3B,MAAM,EAAE,SAAS;QACjB,GAAG,IAAI,CAAC,SAAS;QACjB,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;KACp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,KAAK,CAAC,CAAC,CAAC;IAElB,OAAO;QACL,SAAS,EAAE,SAAS,CAAC,SAAS;QAC9B,KAAK;QACL,SAAS;QACT,KAAK;QACL,GAAG;QACH,KAAK;KACN,CAAC;AACJ,CAAC"}
+81
View File
@@ -0,0 +1,81 @@
/**
* Voice mode: talking with a chat instead of typing into it.
*
* The whole conversation is four states and the rules between them, as one
* pure function, so the rules are tests that run in Node and every surface
* (a chat with a bot, the Cat, a widget) takes its turns the same way:
*
* listening the microphone is open; what is heard is the person's turn
* thinking the turn was sent; the answer is on its way
* speaking the answer is being read aloud
* off nothing is open
*
* The loop is listening → thinking → speaking → listening. A person can
* always cut in: speaking + "interrupt" goes straight back to listening.
* The microphone is never left open by accident: two takes in a row with
* nothing in them end the call, and so does any problem, with its reason.
*/
export type VoiceModePhase = "off" | "listening" | "thinking" | "speaking";
export type VoiceModeEnd =
/** The person ended it. */
"ended"
/** Nothing was heard, twice in a row. */
| "silence"
/** The microphone or the transcription failed. */
| "microphone"
/** The answer did not come. */
| "no-answer";
export type VoiceModeState = {
phase: VoiceModePhase;
/** Takes in a row with nothing in them. */
silentTakes: number;
/** Why the call is over, when it is; null while it runs or before it starts. */
ended: VoiceModeEnd | null;
/** What the person last said, shown while the answer is on its way. */
said: string;
/** What is being read aloud. */
answer: string;
};
export type VoiceModeEvent = {
type: "start";
}
/** A take ended; `text` is what was heard ("" for nothing). */
| {
type: "heard";
text: string;
}
/** The answer to the turn that was sent. */
| {
type: "answer";
text: string;
}
/** The answer did not come (the send failed, or it came back empty). */
| {
type: "no-answer";
}
/** The reading finished. */
| {
type: "spoken";
}
/** The person cut in while the answer was being read. */
| {
type: "interrupt";
} | {
type: "problem";
} | {
type: "end";
};
/** Two empty takes in a row and the microphone closes. */
export declare const VOICE_MODE_MAX_SILENT_TAKES = 2;
export declare const VOICE_MODE_OFF: VoiceModeState;
export declare function voiceModeReducer(state: VoiceModeState, event: VoiceModeEvent): VoiceModeState;
/**
* What each phase asks of the device. The hook does exactly this and nothing
* else, so the microphone and the voice can never both be open, and neither
* is open when the call is off.
*/
export declare function voiceModeWants(phase: VoiceModePhase): {
microphone: boolean;
voice: boolean;
};
//# sourceMappingURL=voice-mode.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"voice-mode.d.ts","sourceRoot":"","sources":["../src/voice-mode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,WAAW,GAAG,UAAU,GAAG,UAAU,CAAC;AAE3E,MAAM,MAAM,YAAY;AACtB,2BAA2B;AACzB,OAAO;AACT,yCAAyC;GACvC,SAAS;AACX,kDAAkD;GAChD,YAAY;AACd,+BAA+B;GAC7B,WAAW,CAAC;AAEhB,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,cAAc,CAAC;IACtB,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,gFAAgF;IAChF,KAAK,EAAE,YAAY,GAAG,IAAI,CAAC;IAC3B,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAC;IACb,gCAAgC;IAChC,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE;AACnB,+DAA+D;GAC7D;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AACjC,4CAA4C;GAC1C;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AAClC,wEAAwE;GACtE;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE;AACvB,4BAA4B;GAC1B;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE;AACpB,yDAAyD;GACvD;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC;AAEpB,0DAA0D;AAC1D,eAAO,MAAM,2BAA2B,IAAI,CAAC;AAE7C,eAAO,MAAM,cAAc,EAAE,cAM5B,CAAC;AAIF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,cAAc,GAAG,cAAc,CA8B7F;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,cAAc,GAAG;IAAE,UAAU,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAE7F"}
+71
View File
@@ -0,0 +1,71 @@
/**
* Voice mode: talking with a chat instead of typing into it.
*
* The whole conversation is four states and the rules between them, as one
* pure function, so the rules are tests that run in Node and every surface
* (a chat with a bot, the Cat, a widget) takes its turns the same way:
*
* listening the microphone is open; what is heard is the person's turn
* thinking the turn was sent; the answer is on its way
* speaking the answer is being read aloud
* off nothing is open
*
* The loop is listening → thinking → speaking → listening. A person can
* always cut in: speaking + "interrupt" goes straight back to listening.
* The microphone is never left open by accident: two takes in a row with
* nothing in them end the call, and so does any problem, with its reason.
*/
/** Two empty takes in a row and the microphone closes. */
export const VOICE_MODE_MAX_SILENT_TAKES = 2;
export const VOICE_MODE_OFF = {
phase: "off",
silentTakes: 0,
ended: null,
said: "",
answer: "",
};
const over = (ended) => ({ ...VOICE_MODE_OFF, ended });
export function voiceModeReducer(state, event) {
if (event.type === "start") {
return state.phase === "off" ? { ...VOICE_MODE_OFF, phase: "listening" } : state;
}
if (state.phase === "off")
return state;
switch (event.type) {
case "end":
return over("ended");
case "problem":
return over("microphone");
case "heard": {
if (state.phase !== "listening")
return state;
const text = event.text.trim();
if (text)
return { ...state, phase: "thinking", silentTakes: 0, said: text, answer: "" };
const silentTakes = state.silentTakes + 1;
return silentTakes >= VOICE_MODE_MAX_SILENT_TAKES
? over("silence")
: { ...state, silentTakes };
}
case "answer": {
if (state.phase !== "thinking")
return state;
const text = event.text.trim();
return text ? { ...state, phase: "speaking", answer: text } : over("no-answer");
}
case "no-answer":
return state.phase === "thinking" ? over("no-answer") : state;
case "spoken":
case "interrupt":
return state.phase === "speaking" ? { ...state, phase: "listening" } : state;
}
}
/**
* What each phase asks of the device. The hook does exactly this and nothing
* else, so the microphone and the voice can never both be open, and neither
* is open when the call is off.
*/
export function voiceModeWants(phase) {
return { microphone: phase === "listening", voice: phase === "speaking" };
}
//# sourceMappingURL=voice-mode.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"voice-mode.js","sourceRoot":"","sources":["../src/voice-mode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAyCH,0DAA0D;AAC1D,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,CAAC;AAE7C,MAAM,CAAC,MAAM,cAAc,GAAmB;IAC5C,KAAK,EAAE,KAAK;IACZ,WAAW,EAAE,CAAC;IACd,KAAK,EAAE,IAAI;IACX,IAAI,EAAE,EAAE;IACR,MAAM,EAAE,EAAE;CACX,CAAC;AAEF,MAAM,IAAI,GAAG,CAAC,KAAmB,EAAkB,EAAE,CAAC,CAAC,EAAE,GAAG,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC;AAErF,MAAM,UAAU,gBAAgB,CAAC,KAAqB,EAAE,KAAqB;IAC3E,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,cAAc,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IACnF,CAAC;IACD,IAAI,KAAK,CAAC,KAAK,KAAK,KAAK;QAAE,OAAO,KAAK,CAAC;IACxC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,KAAK;YACR,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,KAAK,SAAS;YACZ,OAAO,IAAI,CAAC,YAAY,CAAC,CAAC;QAC5B,KAAK,OA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c,CAAC,KAAqB;IAClD,OAAO,EAAE,UAAU,EAAE,KAAK,KAAK,WAAW,EAAE,KAAK,EAAE,KAAK,KAAK,UAAU,EAAE,CAAC;AAC5E,CAAC"}
+6 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@bitbaum/chatkit",
"version": "0.29.0",
"version": "0.30.0",
"description": "One chat for every product: a composer with a microphone that always works, a thread that does not fight the reader, messages with copy and retry, LLM markdown, and a messenger for people and the agents among them \u2014 the fleet's chat standard as a package.",
"license": "MIT",
"author": "Cato",
@@ -43,7 +43,8 @@
"bin",
"styles.css",
"styles/chat.css",
"styles/messenger.css"
"styles/messenger.css",
"styles/voice.css"
],
"bin": {
"chatkit-check-file-inputs": "./bin/chatkit-check-file-inputs.mjs",
@@ -76,7 +77,8 @@
},
"./styles.css": "./styles.css",
"./styles/chat.css": "./styles/chat.css",
"./styles/messenger.css": "./styles/messenger.css"
"./styles/messenger.css": "./styles/messenger.css",
"./styles/voice.css": "./styles/voice.css"
},
"scripts": {
"build": "node scripts/build-chat-css.mjs && rm -rf dist && tsc -p tsconfig.json",
@@ -88,7 +90,7 @@
"format": "prettier --write .",
"format:check": "prettier --check .",
"demo": "esbuild demo/demo.tsx --bundle --jsx=automatic --format=iife --define:process.env.NODE_ENV='\"production\"' --outfile=demo/dist/demo.js --log-level=warning && esbuild demo/demo.tsx --bundle --jsx=automatic --format=iife --define:process.env.NODE_ENV='\"development\"' --outfile=demo/dist/demo-dev.js --log-level=warning",
"test:browser": "pnpm run demo && node test/browser.mjs && node test/browser-scroll.mjs && node test/browser-app.mjs && node test/browser-pdf.mjs && node test/browser-field.mjs && node test/browser-messenger.mjs"
"test:browser": "pnpm run demo && node test/browser.mjs && node test/browser-scroll.mjs && node test/browser-app.mjs && node test/browser-pdf.mjs && node test/browser-field.mjs && node test/browser-messenger.mjs && node test/browser-voice.mjs"
},
"devDependencies": {
"@bitbaum/design-tokens": "^1.5.2",
+10
View File
@@ -8,6 +8,8 @@ const outputPath = resolve(root, "styles/chat.css");
const messengerPath = resolve(root, "styles/messenger.css");
const appMarker = "/* ── ChatApp: the whole app";
const messengerMarker = "/* ── Messenger: people talking to people";
const voiceMarker = "/* ── VoiceMode: a spoken conversation";
const voicePath = resolve(root, "styles/voice.css");
const source = readFileSync(sourcePath, "utf8");
const boundary = source.indexOf(appMarker);
const messenger = source.indexOf(messengerMarker);
@@ -26,3 +28,11 @@ writeFileSync(outputPath, embedded);
// the marks it shares with a chat) and the Messenger's own section, which is
// last in styles.css so it runs to the end of the file.
writeFileSync(messengerPath, embedded + "\n" + source.slice(messenger).trimEnd() + "\n");
// Voice mode alone, for an app that embeds only the chat sheet and offers a
// call: its section sits between ChatApp and the Messenger in styles.css.
const voice = source.indexOf(voiceMarker);
if (voice < boundary || voice > messenger) {
throw new Error(`Missing VoiceMode section between ChatApp and Messenger in ${sourcePath}`);
}
writeFileSync(voicePath, source.slice(voice, messenger).trimEnd() + "\n");
+1
View File
@@ -11,6 +11,7 @@ export * from "./attachments.js";
export * from "./image.js";
export * from "./replies.js";
export * from "./speech.js";
export * from "./voice-mode.js";
export * from "./rich.js";
export * from "./review.js";
export * from "./conversations.js";
+168
View File
@@ -0,0 +1,168 @@
"use client";
import { useEffect, useRef } from "react";
import type { DictationProblem } from "../dictation.js";
import { plainTextForSpeech } from "../speech.js";
import type { VoiceModeEnd, VoiceModePhase } from "../voice-mode.js";
import { IconX } from "./icons.js";
import { DEFAULT_DICTATION_MESSAGES } from "../dictation.js";
import { useVoiceMode, type UseVoiceModeOptions } from "./use-voice-mode.js";
export type VoiceModeLabels = {
title: string;
/** Under the button, per phase: what is happening and what a press does. */
listening: string;
thinking: string;
speaking: string;
/** The big button's name for a screen reader, per phase. */
pressListening: string;
pressSpeaking: string;
pressThinking: string;
end: string;
again: string;
/** Why the call is over. */
ended: Record<Exclude<VoiceModeEnd, "ended">, string>;
dictation: Record<DictationProblem, string>;
};
export const DEFAULT_VOICE_MODE_LABELS: VoiceModeLabels = {
dictation: DEFAULT_DICTATION_MESSAGES,
title: "Talk",
listening: "Listening. Tap when you have finished.",
thinking: "Thinking…",
speaking: "Tap to cut in.",
pressListening: "I have finished speaking",
pressSpeaking: "Stop reading and listen to me",
pressThinking: "Waiting for the answer",
end: "End",
again: "Talk again",
ended: {
silence: "Nothing was heard, so the microphone is closed.",
microphone: "The microphone stopped.",
"no-answer": "No answer came. Your words are in the chat; try again there.",
},
};
export type VoiceModeProps = UseVoiceModeOptions & {
open: boolean;
onClose: () => void;
/** Who is being talked to, shown above the button. */
name?: string;
labels?: Partial<VoiceModeLabels>;
};
/**
* A spoken conversation, full screen: one big button, what was heard, what is
* being said back, and End. Open it from a tap (browsers open a microphone
* and a voice only inside one); closing it shuts both.
*
* The loop and its rules are `useVoiceMode` and `voiceModeReducer`. This is
* only the screen, so every chat that offers voice looks and behaves the same.
*/
export function VoiceMode({ open, onClose, name, labels: given, ...opts }: VoiceModeProps) {
const labels = { ...DEFAULT_VOICE_MODE_LABELS, ...given };
const vm = useVoiceMode(opts);
const ref = useRef<HTMLDialogElement>(null);
const { start, end, state } = vm;
useEffect(() => {
const d = ref.current;
if (!d) return;
if (open && !d.open && typeof d.showModal === "function") {
d.showModal();
start();
} else if (!open && d.open) {
end();
d.close();
}
}, [open, start, end]);
// Unmounting ends the call too: a microphone that outlives its screen is the worst bug a chat can have.
useEffect(() => () => end(), [end]);
const phase: VoiceModePhase = state.phase;
const line =
phase === "listening"
? vm.dictation.liveText || labels.listening
: phase === "thinking"
? state.said
: phase === "speaking"
? plainTextForSpeech(state.answer)
: "";
const reason =
state.ended && state.ended !== "ended"
? vm.dictation.problem
? labels.dictation[vm.dictation.problem]
: labels.ended[state.ended]
: null;
return (
<dialog
ref={ref}
className="ck-voicemode"
aria-label={name ? `${labels.title}: ${name}` : labels.title}
onClose={() => {
end();
onClose();
}}
>
<div className="ck-voicemode-top">
<span className="ck-voicemode-name">{name ?? labels.title}</span>
<button type="button" className="ck-icon-btn" aria-label={labels.end} onClick={onClose}>
<IconX />
</button>
</div>
<div className="ck-voicemode-body" data-phase={phase} data-mic={vm.dictation.status}>
{phase === "off" ? (
<>
{reason && (
<p className="ck-voicemode-reason" role="status">
{reason}
</p>
)}
<button type="button" className="ck-voicemode-again" onClick={start}>
{labels.again}
</button>
</>
) : (
<>
<button
type="button"
className="ck-voicemode-orb"
data-phase={phase}
disabled={phase === "thinking"}
aria-label={
phase === "listening"
? labels.pressListening
: phase === "speaking"
? labels.pressSpeaking
: labels.pressThinking
}
onClick={vm.press}
>
<span className="ck-voicemode-ring" aria-hidden="true" />
</button>
<p className="ck-voicemode-status" role="status">
{phase === "listening"
? labels.listening
: phase === "thinking"
? labels.thinking
: labels.speaking}
</p>
{line && line !== labels.listening && (
<p className="ck-voicemode-line" data-phase={phase}>
{line}
</p>
)}
</>
)}
</div>
<div className="ck-voicemode-foot">
<button type="button" className="ck-voicemode-end" onClick={onClose}>
{labels.end}
</button>
</div>
</dialog>
);
}
+4
View File
@@ -61,6 +61,10 @@ export { Markdown, SourcesRow, defaultRenderLink, defaultRenderCitation } from "
export { AnswerActions, DEFAULT_ANSWER_ACTIONS_LABELS } from "./AnswerActions.js";
export type { AnswerActionsLabels } from "./AnswerActions.js";
export { useSpeech } from "./use-speech.js";
export { useVoiceMode } from "./use-voice-mode.js";
export type { UseVoiceModeOptions, VoiceModeController } from "./use-voice-mode.js";
export { VoiceMode, DEFAULT_VOICE_MODE_LABELS } from "./VoiceMode.js";
export type { VoiceModeProps, VoiceModeLabels } from "./VoiceMode.js";
export type { SpeechController } from "./use-speech.js";
export { Sheet, prefersSheet } from "./Sheet.js";
export type { CitationMap, RenderLink, RenderCitation, RenderSources } from "./Markdown.js";
+17
View File
@@ -238,6 +238,8 @@ export function useDictation(opts: UseDictationOptions): DictationController {
const recorderRef = useRef<MediaRecorder | null>(null);
const streamRef = useRef<MediaStream | null>(null);
const cancelledRef = useRef(false);
/** True from the request for a microphone until the browser answers it. */
const openingRef = useRef(false);
const capTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const locale = useCallback(() => {
@@ -333,16 +335,31 @@ export function useDictation(opts: UseDictationOptions): DictationController {
setStatus("idle");
return;
}
// One microphone at a time. `status` stays "idle" until the browser hands
// the microphone over, so a second start in that gap (a double tap, an
// effect that runs twice) used to open a second microphone and orphan the
// first: its track stayed live with nothing left to stop it. Found by
// voice mode's browser test, 2026-11-13.
if (openingRef.current) return;
openingRef.current = true;
setProblem(null);
cancelledRef.current = false;
let stream: MediaStream;
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (e) {
openingRef.current = false;
setProblem(problemForRecording(e));
setStatus("idle");
return;
}
openingRef.current = false;
// Cancelled while the browser was still opening it: this microphone was
// never wanted, so it is closed here, where it arrives.
if (cancelledRef.current) {
for (const t of stream.getTracks()) t.stop();
return;
}
streamRef.current = stream;
let rec: MediaRecorder;
try {
+164
View File
@@ -0,0 +1,164 @@
"use client";
import { useCallback, useEffect, useReducer, useRef } from "react";
import {
VOICE_MODE_OFF,
voiceModeReducer,
voiceModeWants,
type VoiceModeState,
} from "../voice-mode.js";
import type { Speaker } from "../speech.js";
import {
useDictation,
type DictationController,
type UseDictationOptions,
} from "./use-dictation.js";
import { useSpeech } from "./use-speech.js";
export type UseVoiceModeOptions = {
/**
* Send the person's turn and resolve with the answer's words (markdown is
* fine; it is flattened before it is read). Resolve with "" or reject when
* no answer came: the call then ends and says so.
*/
onTurn: (text: string) => Promise<string>;
/** The same options the composer's microphone takes (`transcribeUrl`, `lang`…). */
dictation?: Omit<UseDictationOptions, "onText">;
/** A studio voice; the browser's own voice when omitted. */
speaker?: Speaker | null;
};
export type VoiceModeController = {
/** False where this device can neither listen nor speak. */
supported: boolean;
state: VoiceModeState;
dictation: DictationController;
/** Open the call. Must run inside a tap: browsers open a microphone and a voice only then. */
start: () => void;
end: () => void;
/**
* The one big button. Listening: "I have finished speaking". Speaking: cut
* in and listen again. Thinking: nothing to do.
*/
press: () => void;
};
/**
* A spoken conversation over any chat: listen, send, read the answer aloud,
* listen again (`voiceModeReducer` holds the rules). Where the browser's own
* recogniser runs, a pause ends the person's turn by itself; everywhere else
* a press of the button does, which is also how a person takes their time.
*
* The microphone and the voice follow the phase and nothing else
* (`voiceModeWants`), and both are shut when the component unmounts.
*/
export function useVoiceMode(opts: UseVoiceModeOptions): VoiceModeController {
const [state, dispatch] = useReducer(voiceModeReducer, VOICE_MODE_OFF);
const onTurn = useRef(opts.onTurn);
useEffect(() => {
onTurn.current = opts.onTurn;
}, [opts.onTurn]);
/** Guards against an answer that arrives after the call moved on. */
const turn = useRef(0);
const dictation = useDictation({
// A pause should end the turn where the browser can hear one; a press
// ends it everywhere else.
prefer: "browser",
...opts.dictation,
onText: (text) => dispatch({ type: "heard", text }),
});
// The newest controller, for effects: set before any of them runs.
const dictationRef = useRef(dictation);
useEffect(() => {
dictationRef.current = dictation;
});
const speech = useSpeech(opts.speaker);
const { microphone, voice } = voiceModeWants(state.phase);
// The microphone follows the phase.
const micStatus = dictation.status;
useEffect(() => {
const d = dictationRef.current;
// Listening and the microphone is free: open it. This runs again when a
// take that held nothing has been written down (status back to idle), so
// a second take follows an empty first one.
if (microphone && micStatus === "idle") d.start();
// Not the person's turn: a live take is thrown away. A take that is being
// written down is left alone; it is the turn on its way.
if (!microphone && micStatus === "listening") d.cancel();
}, [microphone, micStatus, state.silentTakes]);
// A microphone or transcription problem ends the call, with its reason on screen.
// "silence" is a take with nothing in it: the rules count those and close
// the microphone after two. Anything else is the microphone itself.
const problem = dictation.problem;
useEffect(() => {
if (!problem || state.phase === "off") return;
if (problem === "silence") {
dictationRef.current.clearProblem();
dispatch({ type: "heard", text: "" });
} else {
dispatch({ type: "problem" });
}
}, [problem, state.phase]);
// The turn is sent once, when the phase becomes "thinking".
useEffect(() => {
if (state.phase !== "thinking") return;
const mine = ++turn.current;
let live = true;
onTurn
.current(state.said)
.then((answer) => {
if (live && mine === turn.current) dispatch({ type: "answer", text: answer ?? "" });
})
.catch(() => {
if (live && mine === turn.current) dispatch({ type: "no-answer" });
});
return () => {
live = false;
};
}, [state.phase, state.said]);
// The voice follows the phase; when the reading ends, the person's turn begins.
const speechRef = useRef(speech);
useEffect(() => {
speechRef.current = speech;
});
const reading = useRef(false);
useEffect(() => {
if (voice && state.answer) {
reading.current = false;
speechRef.current.speak(state.answer);
} else {
reading.current = false;
speechRef.current.stop();
}
}, [voice, state.answer]);
useEffect(() => {
if (!voice) return;
if (speech.speaking) reading.current = true;
// Reading began and is over (or the device has no voice at all): listen.
else if (reading.current || !speech.supported) dispatch({ type: "spoken" });
}, [voice, speech.speaking, speech.supported]);
const start = useCallback(() => dispatch({ type: "start" }), []);
const end = useCallback(() => {
turn.current++;
dispatch({ type: "end" });
}, []);
const press = useCallback(() => {
if (state.phase === "listening") dictationRef.current.stop();
else if (state.phase === "speaking") dispatch({ type: "interrupt" });
}, [state.phase]);
return {
supported: dictation.supported,
state,
dictation,
start,
end,
press,
};
}
+110
View File
@@ -0,0 +1,110 @@
/**
* Voice mode: talking with a chat instead of typing into it.
*
* The whole conversation is four states and the rules between them, as one
* pure function, so the rules are tests that run in Node and every surface
* (a chat with a bot, the Cat, a widget) takes its turns the same way:
*
* listening the microphone is open; what is heard is the person's turn
* thinking the turn was sent; the answer is on its way
* speaking the answer is being read aloud
* off nothing is open
*
* The loop is listening → thinking → speaking → listening. A person can
* always cut in: speaking + "interrupt" goes straight back to listening.
* The microphone is never left open by accident: two takes in a row with
* nothing in them end the call, and so does any problem, with its reason.
*/
export type VoiceModePhase = "off" | "listening" | "thinking" | "speaking";
export type VoiceModeEnd =
/** The person ended it. */
| "ended"
/** Nothing was heard, twice in a row. */
| "silence"
/** The microphone or the transcription failed. */
| "microphone"
/** The answer did not come. */
| "no-answer";
export type VoiceModeState = {
phase: VoiceModePhase;
/** Takes in a row with nothing in them. */
silentTakes: number;
/** Why the call is over, when it is; null while it runs or before it starts. */
ended: VoiceModeEnd | null;
/** What the person last said, shown while the answer is on its way. */
said: string;
/** What is being read aloud. */
answer: string;
};
export type VoiceModeEvent =
| { type: "start" }
/** A take ended; `text` is what was heard ("" for nothing). */
| { type: "heard"; text: string }
/** The answer to the turn that was sent. */
| { type: "answer"; text: string }
/** The answer did not come (the send failed, or it came back empty). */
| { type: "no-answer" }
/** The reading finished. */
| { type: "spoken" }
/** The person cut in while the answer was being read. */
| { type: "interrupt" }
| { type: "problem" }
| { type: "end" };
/** Two empty takes in a row and the microphone closes. */
export const VOICE_MODE_MAX_SILENT_TAKES = 2;
export const VOICE_MODE_OFF: VoiceModeState = {
phase: "off",
silentTakes: 0,
ended: null,
said: "",
answer: "",
};
const over = (ended: VoiceModeEnd): VoiceModeState => ({ ...VOICE_MODE_OFF, ended });
export function voiceModeReducer(state: VoiceModeState, event: VoiceModeEvent): VoiceModeState {
if (event.type === "start") {
return state.phase === "off" ? { ...VOICE_MODE_OFF, phase: "listening" } : state;
}
if (state.phase === "off") return state;
switch (event.type) {
case "end":
return over("ended");
case "problem":
return over("microphone");
case "heard": {
if (state.phase !== "listening") return state;
const text = event.text.trim();
if (text) return { ...state, phase: "thinking", silentTakes: 0, said: text, answer: "" };
const silentTakes = state.silentTakes + 1;
return silentTakes >= VOICE_MODE_MAX_SILENT_TAKES
? over("silence")
: { ...state, silentTakes };
}
case "answer": {
if (state.phase !== "thinking") return state;
const text = event.text.trim();
return text ? { ...state, phase: "speaking", answer: text } : over("no-answer");
}
case "no-answer":
return state.phase === "thinking" ? over("no-answer") : state;
case "spoken":
case "interrupt":
return state.phase === "speaking" ? { ...state, phase: "listening" } : state;
}
}
/**
* What each phase asks of the device. The hook does exactly this and nothing
* else, so the microphone and the voice can never both be open, and neither
* is open when the call is off.
*/
export function voiceModeWants(phase: VoiceModePhase): { microphone: boolean; voice: boolean } {
return { microphone: phase === "listening", voice: phase === "speaking" };
}
+155
View File
@@ -2727,6 +2727,161 @@ a.ck-turn-chip:hover {
}
}
/* ── VoiceMode: a spoken conversation ─────────────────────────────────────
Its own section (and styles/voice.css) so a widget that embeds only the
chat sheet does not carry a full-screen call it never opens. */
/* One big button whose look is
the phase (listening pulses, thinking waits, speaking is filled), the words
under it, and End. A native <dialog>, so it is in the top layer everywhere. */
.ck-voicemode {
position: fixed;
inset: 0;
width: 100%;
max-width: 100%;
height: 100%;
max-height: 100%;
margin: 0;
padding: 0;
border: 0;
background: var(--ck-bg);
color: var(--ck-fg);
}
.ck-voicemode[open] {
display: flex;
flex-direction: column;
}
.ck-voicemode::backdrop {
background: var(--ck-bg);
}
.ck-voicemode-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
padding: calc(0.75rem + env(safe-area-inset-top, 0px)) 1rem 0.75rem;
}
.ck-voicemode-name {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 1.0625rem;
font-weight: 600;
}
.ck-voicemode-body {
display: flex;
flex: 1;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1.25rem;
min-height: 0;
padding: 1rem 1.5rem;
text-align: center;
}
.ck-voicemode-orb {
position: relative;
display: grid;
width: 9rem;
height: 9rem;
flex-shrink: 0;
place-items: center;
border: 2px solid var(--ck-accent);
border-radius: 999px;
background: var(--ck-accent-soft);
cursor: pointer;
}
.ck-voicemode-orb:disabled {
cursor: default;
border-color: var(--ck-line-2);
background: var(--ck-bg);
}
.ck-voicemode-orb[data-phase="speaking"] {
background: var(--ck-accent);
}
.ck-voicemode-orb:focus-visible {
outline: 2px solid var(--ck-accent);
outline-offset: 4px;
}
.ck-voicemode-ring {
position: absolute;
inset: -0.75rem;
border: 2px solid var(--ck-accent);
border-radius: 999px;
opacity: 0;
}
.ck-voicemode-orb[data-phase="listening"] .ck-voicemode-ring {
animation: ck-voicemode-ring 1.6s ease-out infinite;
}
.ck-voicemode-orb[data-phase="thinking"] .ck-voicemode-ring {
inset: 0.5rem;
border-color: var(--ck-fg-3);
border-top-color: transparent;
opacity: 1;
animation: ck-spin 1s linear infinite;
}
@keyframes ck-voicemode-ring {
from {
transform: scale(0.85);
opacity: 0.7;
}
to {
transform: scale(1.15);
opacity: 0;
}
}
.ck-voicemode-status {
margin: 0;
font-size: 0.9375rem;
color: var(--ck-fg-2);
}
.ck-voicemode-line {
max-width: 34rem;
max-height: 34vh;
margin: 0;
overflow-y: auto;
font-size: 1.125rem;
line-height: 1.5;
color: var(--ck-fg);
}
.ck-voicemode-line[data-phase="thinking"] {
color: var(--ck-fg-2);
}
.ck-voicemode-reason {
max-width: 28rem;
margin: 0;
font-size: 1rem;
line-height: 1.5;
color: var(--ck-fg);
}
.ck-voicemode-foot {
display: flex;
justify-content: center;
padding: 0.75rem 1rem calc(1rem + env(safe-area-inset-bottom, 0px));
}
.ck-voicemode-end,
.ck-voicemode-again {
min-width: 8rem;
min-height: 2.75rem;
padding: 0 1.25rem;
border: 1px solid var(--ck-line-2);
border-radius: 999px;
background: var(--ck-bg);
color: var(--ck-fg);
font: inherit;
font-weight: 600;
cursor: pointer;
}
.ck-voicemode-again {
border-color: var(--ck-accent);
}
@media (prefers-reduced-motion: reduce) {
.ck-voicemode-orb .ck-voicemode-ring {
animation: none;
opacity: 0.6;
}
}
/* ── Messenger: people talking to people ───────────────────────────────────
After the ChatApp boundary, so the embedded styles/chat.css keeps its
budget. styles/messenger.css is that embedded sheet plus this section: the
+154
View File
@@ -0,0 +1,154 @@
/* ── VoiceMode: a spoken conversation ─────────────────────────────────────
Its own section (and styles/voice.css) so a widget that embeds only the
chat sheet does not carry a full-screen call it never opens. */
/* One big button whose look is
the phase (listening pulses, thinking waits, speaking is filled), the words
under it, and End. A native <dialog>, so it is in the top layer everywhere. */
.ck-voicemode {
position: fixed;
inset: 0;
width: 100%;
max-width: 100%;
height: 100%;
max-height: 100%;
margin: 0;
padding: 0;
border: 0;
background: var(--ck-bg);
color: var(--ck-fg);
}
.ck-voicemode[open] {
display: flex;
flex-direction: column;
}
.ck-voicemode::backdrop {
background: var(--ck-bg);
}
.ck-voicemode-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
padding: calc(0.75rem + env(safe-area-inset-top, 0px)) 1rem 0.75rem;
}
.ck-voicemode-name {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 1.0625rem;
font-weight: 600;
}
.ck-voicemode-body {
display: flex;
flex: 1;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1.25rem;
min-height: 0;
padding: 1rem 1.5rem;
text-align: center;
}
.ck-voicemode-orb {
position: relative;
display: grid;
width: 9rem;
height: 9rem;
flex-shrink: 0;
place-items: center;
border: 2px solid var(--ck-accent);
border-radius: 999px;
background: var(--ck-accent-soft);
cursor: pointer;
}
.ck-voicemode-orb:disabled {
cursor: default;
border-color: var(--ck-line-2);
background: var(--ck-bg);
}
.ck-voicemode-orb[data-phase="speaking"] {
background: var(--ck-accent);
}
.ck-voicemode-orb:focus-visible {
outline: 2px solid var(--ck-accent);
outline-offset: 4px;
}
.ck-voicemode-ring {
position: absolute;
inset: -0.75rem;
border: 2px solid var(--ck-accent);
border-radius: 999px;
opacity: 0;
}
.ck-voicemode-orb[data-phase="listening"] .ck-voicemode-ring {
animation: ck-voicemode-ring 1.6s ease-out infinite;
}
.ck-voicemode-orb[data-phase="thinking"] .ck-voicemode-ring {
inset: 0.5rem;
border-color: var(--ck-fg-3);
border-top-color: transparent;
opacity: 1;
animation: ck-spin 1s linear infinite;
}
@keyframes ck-voicemode-ring {
from {
transform: scale(0.85);
opacity: 0.7;
}
to {
transform: scale(1.15);
opacity: 0;
}
}
.ck-voicemode-status {
margin: 0;
font-size: 0.9375rem;
color: var(--ck-fg-2);
}
.ck-voicemode-line {
max-width: 34rem;
max-height: 34vh;
margin: 0;
overflow-y: auto;
font-size: 1.125rem;
line-height: 1.5;
color: var(--ck-fg);
}
.ck-voicemode-line[data-phase="thinking"] {
color: var(--ck-fg-2);
}
.ck-voicemode-reason {
max-width: 28rem;
margin: 0;
font-size: 1rem;
line-height: 1.5;
color: var(--ck-fg);
}
.ck-voicemode-foot {
display: flex;
justify-content: center;
padding: 0.75rem 1rem calc(1rem + env(safe-area-inset-bottom, 0px));
}
.ck-voicemode-end,
.ck-voicemode-again {
min-width: 8rem;
min-height: 2.75rem;
padding: 0 1.25rem;
border: 1px solid var(--ck-line-2);
border-radius: 999px;
background: var(--ck-bg);
color: var(--ck-fg);
font: inherit;
font-weight: 600;
cursor: pointer;
}
.ck-voicemode-again {
border-color: var(--ck-accent);
}
@media (prefers-reduced-motion: reduce) {
.ck-voicemode-orb .ck-voicemode-ring {
animation: none;
opacity: 0.6;
}
}
+205
View File
@@ -0,0 +1,205 @@
// Voice mode, driven in a real browser with a real (fake-device) microphone.
//
// pnpm run demo && node test/browser-voice.mjs
//
// What each check is for: the call must open listening from a tap, a press
// must end the person's turn and send what was heard, the answer must be read
// (as prose, not markdown) and the floor handed back, a press while it reads
// must cut in, End must close the microphone and the voice, and every way a
// call can fail (nothing heard, no answer, a dead transcription) must end it
// with words on screen, never a silent open microphone.
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { extname, join, normalize } from "node:path";
import { fileURLToPath } from "node:url";
import { chromium } from "playwright";
const root = fileURLToPath(new URL("..", import.meta.url));
const types = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css" };
const server = createServer(async (req, res) => {
const path = normalize(join(root, decodeURIComponent(new URL(req.url, "http://x").pathname)));
if (!path.startsWith(root)) return res.writeHead(403).end();
try {
const body = await readFile(path);
res
.writeHead(200, { "content-type": types[extname(path)] ?? "application/octet-stream" })
.end(body);
} catch {
res.writeHead(404).end();
}
}).listen(0);
const base = `http://localhost:${server.address().port}/demo/voice.html`;
let failures = 0;
const check = (ok, what) => {
console.log(`${ok ? "PASS" : "FAIL"} ${what}`);
if (!ok) failures++;
};
const browser = await chromium.launch({
...(process.env.CHROMIUM_PATH ? { executablePath: process.env.CHROMIUM_PATH } : {}),
args: ["--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream"],
});
/** A page with every microphone track it ever opened counted, so "closed" is a fact. */
async function open(query, width = 390) {
const context = await browser.newContext({
viewport: { width, height: 800 },
permissions: ["microphone"],
});
const page = await context.newPage();
await page.addInitScript(() => {
window.__tracks = [];
const real = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
navigator.mediaDevices.getUserMedia = async (c) => {
const s = await real(c);
window.__tracks.push(...s.getTracks());
return s;
};
});
await page.goto(`${base}?${query}`);
await page.click("#open-voice");
return { context, page };
}
const phase = (page) =>
page.locator(".ck-voicemode-body").getAttribute("data-phase", { timeout: 5000 });
const until = (page, want, ms = 8000) =>
page.waitForSelector(`.ck-voicemode-body[data-phase="${want}"]`, { timeout: ms });
const liveTracks = (page) =>
page.evaluate(() => window.__tracks.filter((t) => t.readyState === "live").length);
try {
// ── the loop ────────────────────────────────────────────────────────────
{
const { context, page } = await open("voiceMs=1200");
await until(page, "listening");
check((await phase(page)) === "listening", "the call opens listening, from a tap");
await page.waitForFunction(() => window.__tracks.length > 0);
check((await liveTracks(page)) > 0, "the microphone is open while listening");
const orb = page.locator(".ck-voicemode-orb");
const box = await orb.boundingBox();
check(box && box.width >= 88 && box.height >= 88, "the button is a thumb's size and more");
await page.waitForTimeout(700);
await orb.click();
await until(page, "thinking");
check(
(await page.locator(".ck-voicemode-line").innerText()).includes("what is next today"),
"what was heard is shown while the answer is on its way",
);
check((await liveTracks(page)) === 0, "the microphone is closed while thinking");
await until(page, "speaking");
const line = await page.locator(".ck-voicemode-line").innerText();
check(
line.includes("The report, at five.") && !line.includes("**"),
"the answer is shown as prose, not markdown",
);
const spoken = await page.evaluate(() => window.__voice.spoken.at(-1));
check(spoken === "The report, at five.", "and read aloud as prose");
check((await liveTracks(page)) === 0, "the microphone is closed while the answer is read");
await until(page, "listening");
check(
(await page.evaluate(() => window.__voice.turns.length)) === 1,
"one turn was sent, once",
);
check(true, "the floor goes back to the person when the reading ends");
// cut in
await page.waitForTimeout(700);
await orb.click();
await until(page, "speaking");
await orb.click();
await until(page, "listening", 2000);
check(
(await page.evaluate(() => window.__voice.speaking)) === false,
"a press while it reads cuts in: the voice stops and it listens",
);
// end
await page.waitForFunction(() => window.__tracks.some((t) => t.readyState === "live"));
await page.locator(".ck-voicemode-end").click();
await page.waitForFunction(() => !document.querySelector(".ck-voicemode")?.open);
await page.waitForTimeout(300);
check((await liveTracks(page)) === 0, "End closes the microphone");
check((await page.evaluate(() => window.__voice.speaking)) === false, "and the voice");
const overflow = await page.evaluate(
() => document.documentElement.scrollWidth > window.innerWidth + 1,
);
check(!overflow, "nothing scrolls sideways at 390px");
await context.close();
}
// ── nothing heard, twice ────────────────────────────────────────────────
{
const { context, page } = await open("heard=");
const orb = page.locator(".ck-voicemode-orb");
for (let i = 0; i < 2; i++) {
await until(page, "listening");
await page.waitForTimeout(700);
await orb.click();
await page.waitForTimeout(600);
}
await until(page, "off");
check(
(await page.locator(".ck-voicemode-reason").innerText()).includes("Nothing was heard"),
"two empty takes close the call, and say why",
);
const closed = await page
.waitForFunction(() => window.__tracks.every((t) => t.readyState !== "live"), null, {
timeout: 4000,
})
.then(() => true)
.catch(() => false);
check(closed, "with the microphone closed");
check(
(await page.evaluate(() => window.__voice.turns.length)) === 0,
"and nothing was sent for silence",
);
await page.locator(".ck-voicemode-again").click();
await until(page, "listening");
check(true, "Talk again reopens it");
await context.close();
}
// ── no answer ───────────────────────────────────────────────────────────
{
const { context, page } = await open("answer=");
await until(page, "listening");
await page.waitForTimeout(700);
await page.locator(".ck-voicemode-orb").click();
await until(page, "off");
check(
(await page.locator(".ck-voicemode-reason").innerText()).includes("No answer came"),
"an answer that never comes ends the call, and says so",
);
await context.close();
}
// ── a dead transcription ────────────────────────────────────────────────
{
const { context, page } = await open("fail=mic", 320);
await until(page, "listening");
await page.waitForTimeout(700);
await page.locator(".ck-voicemode-orb").click();
await until(page, "off");
const reason = await page.locator(".ck-voicemode-reason").innerText();
check(
reason.trim().length > 10,
`a failed transcription ends the call in words ("${reason.trim().slice(0, 60)}")`,
);
await page.waitForTimeout(300);
check((await liveTracks(page)) === 0, "with the microphone closed");
const overflow = await page.evaluate(
() => document.documentElement.scrollWidth > window.innerWidth + 1,
);
check(!overflow, "nothing scrolls sideways at 320px");
await context.close();
}
} finally {
await browser.close();
server.close();
}
if (failures) {
console.error(`${failures} check(s) failed`);
process.exit(1);
}
console.log("voice mode: all checks passed");
+99
View File
@@ -0,0 +1,99 @@
// Voice mode's rules: who has the floor, and when the microphone closes.
// The loop is listening → thinking → speaking → listening; a person can cut
// in; two empty takes or any problem end the call with its reason; and the
// microphone and the voice are never both open.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
VOICE_MODE_MAX_SILENT_TAKES,
VOICE_MODE_OFF,
voiceModeReducer,
voiceModeWants,
} from "../dist/index.js";
const run = (events, from = VOICE_MODE_OFF) => events.reduce(voiceModeReducer, from);
test("a call opens listening, and only from off", () => {
const s = run([{ type: "start" }]);
assert.equal(s.phase, "listening");
assert.equal(run([{ type: "start" }], { ...s, phase: "speaking" }).phase, "speaking");
});
test("what is heard is sent; the answer is read; then the person has the floor again", () => {
let s = run([{ type: "start" }, { type: "heard", text: " what is next? " }]);
assert.equal(s.phase, "thinking");
assert.equal(s.said, "what is next?");
s = run([{ type: "answer", text: "The report, at five." }], s);
assert.equal(s.phase, "speaking");
assert.equal(s.answer, "The report, at five.");
s = run([{ type: "spoken" }], s);
assert.equal(s.phase, "listening");
});
test("a person can cut in while the answer is read", () => {
const speaking = run([
{ type: "start" },
{ type: "heard", text: "hello" },
{ type: "answer", text: "A very long answer." },
]);
assert.equal(run([{ type: "interrupt" }], speaking).phase, "listening");
// …but not while it is still thinking: there is nothing to cut into.
const thinking = run([{ type: "start" }, { type: "heard", text: "hello" }]);
assert.equal(run([{ type: "interrupt" }], thinking).phase, "thinking");
});
test("empty takes in a row close the microphone, and say why", () => {
let s = run([{ type: "start" }]);
for (let i = 1; i < VOICE_MODE_MAX_SILENT_TAKES; i++) {
s = run([{ type: "heard", text: " " }], s);
assert.equal(s.phase, "listening", `still listening after ${i}`);
}
s = run([{ type: "heard", text: "" }], s);
assert.equal(s.phase, "off");
assert.equal(s.ended, "silence");
});
test("a take with words in it clears the count of empty ones", () => {
const s = run([
{ type: "start" },
{ type: "heard", text: "" },
{ type: "heard", text: "hello" },
{ type: "answer", text: "Hi." },
{ type: "spoken" },
{ type: "heard", text: "" },
]);
assert.equal(s.phase, "listening");
assert.equal(s.silentTakes, 1);
});
test("no answer, an empty answer and a microphone problem each end the call with their reason", () => {
const thinking = run([{ type: "start" }, { type: "heard", text: "hello" }]);
assert.equal(run([{ type: "no-answer" }], thinking).ended, "no-answer");
assert.equal(run([{ type: "answer", text: " " }], thinking).ended, "no-answer");
assert.equal(run([{ type: "problem" }], thinking).ended, "microphone");
assert.equal(run([{ type: "end" }], thinking).ended, "ended");
for (const s of [run([{ type: "no-answer" }], thinking), run([{ type: "problem" }], thinking)]) {
assert.equal(s.phase, "off");
}
});
test("events that belong to another phase change nothing", () => {
const listening = run([{ type: "start" }]);
assert.deepEqual(run([{ type: "answer", text: "late" }], listening), listening);
assert.deepEqual(run([{ type: "spoken" }], listening), listening);
const off = VOICE_MODE_OFF;
for (const e of [{ type: "heard", text: "x" }, { type: "answer", text: "x" }, { type: "end" }]) {
assert.deepEqual(run([e], off), off);
}
});
test("the microphone and the voice are never both open, and neither when the call is off", () => {
for (const phase of ["off", "listening", "thinking", "speaking"]) {
const w = voiceModeWants(phase);
assert.equal(w.microphone && w.voice, false, phase);
}
assert.deepEqual(voiceModeWants("off"), { microphone: false, voice: false });
assert.deepEqual(voiceModeWants("thinking"), { microphone: false, voice: false });
assert.deepEqual(voiceModeWants("listening"), { microphone: true, voice: false });
assert.deepEqual(voiceModeWants("speaking"), { microphone: false, voice: true });
});