Files
chatkit/scripts/build-chat-css.mjs
CatoandClaude 8452fb12ab 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>
2026-11-13 08:18:12 +01:00

39 lines
1.8 KiB
JavaScript

import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const sourcePath = resolve(root, "styles.css");
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);
if (boundary < 0) {
throw new Error(`Missing ChatApp stylesheet boundary in ${sourcePath}`);
}
if (messenger < boundary) {
throw new Error(`Missing Messenger section after the ChatApp boundary in ${sourcePath}`);
}
const embedded = source.slice(0, boundary).trimEnd() + "\n";
mkdirSync(dirname(outputPath), { recursive: true });
writeFileSync(outputPath, embedded);
// The Messenger without ChatApp: the embedded sheet (the composer, the sheet,
// 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");