From 8b1e27b5ec5f4c54f851927d3a0f4ce615a954ef Mon Sep 17 00:00:00 2001 From: e2goon Date: Sat, 14 Nov 2026 20:50:36 +0900 Subject: [PATCH 1/5] feat: multilingual captions, live translation, and stt-rt-v6 Move to stt-rt-v6 (v5 is removed on 2026-12-15) and send the API key in the Authorization header; the start-request api_key field is deprecated. Captions - Split lines on a period only when the next token starts a new word, since Soniox puts spacing on the leading edge of tokens - Never leave a speaker tag alone on a row; wrap Japanese, Chinese, and Thai on word boundaries; end sentences on Hindi, Arabic, and Urdu marks; start a new line after a pause of more than two seconds - Enable language identification: tag lines with their language when more than one is in play, split on language changes, and filter with `l` Languages - Script-aware recognition context for every non-Latin language, not only Korean - Searchable language picker; defaults follow the system locale plus English - `--lang`, `--version`, and `earpop languages` Translation - One-way or two-way translation from settings or `--translate` - Translated lines appear under the speech they translate and are saved in the transcript Transcripts and terms - `--format txt|srt|vtt|md`, `--out`, and `latest` for view and export - `transcripts list` reads only file edges and shows the recorded length - `earpop terms` manages words and names sent as Soniox context terms Reliability - Do not reconnect on 400, 401, 402, or 403 - Report failed settings saves instead of crashing Add `pnpm test` (node:test) and a CI workflow for pushes and pull requests. --- .github/workflows/ci.yml | 28 +++ README.md | 68 ++++-- package.json | 1 + src/app/live-app.tsx | 43 +++- src/app/settings-app.tsx | 77 +++--- src/app/terms-command.ts | 56 +++++ src/app/transcripts-command.ts | 53 +++-- src/app/tui.tsx | 26 ++- src/components/language-select.tsx | 76 ++++-- src/index.tsx | 85 ++++++- src/lib/caption-layout.test.ts | 236 +++++++++++++++++++ src/lib/caption-layout.ts | 342 +++++++++++++++++++++++++++ src/lib/journal.test.ts | 55 +++++ src/lib/journal.ts | 14 +- src/lib/languages.test.ts | 118 ++++++++++ src/lib/languages.ts | 168 ++++++++++---- src/lib/segments.ts | 24 ++ src/lib/settings.test.ts | 41 ++++ src/lib/settings.ts | 45 +++- src/lib/soniox.ts | 34 ++- src/lib/transcript-formats.test.ts | 226 ++++++++++++++++++ src/lib/transcript-formats.ts | 215 +++++++++++++++++ src/lib/transcription.ts | 80 +++++-- src/lib/transcripts.test.ts | 50 ++++ src/lib/transcripts.ts | 157 ++++++------- src/lib/types.ts | 10 + src/screens/caption.tsx | 361 ++++++----------------------- src/screens/settings.tsx | 31 ++- src/test-utils/temp-home.ts | 18 ++ 29 files changed, 2188 insertions(+), 550 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 src/app/terms-command.ts create mode 100644 src/lib/caption-layout.test.ts create mode 100644 src/lib/caption-layout.ts create mode 100644 src/lib/journal.test.ts create mode 100644 src/lib/languages.test.ts create mode 100644 src/lib/segments.ts create mode 100644 src/lib/settings.test.ts create mode 100644 src/lib/transcript-formats.test.ts create mode 100644 src/lib/transcript-formats.ts create mode 100644 src/lib/transcripts.test.ts create mode 100644 src/test-utils/temp-home.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f0c021b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: pnpm/action-setup@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 24 + cache: pnpm + + - run: pnpm install --frozen-lockfile + - run: pnpm check + - run: pnpm lint + - run: pnpm fmt:check + - run: pnpm test diff --git a/README.md b/README.md index 48d9275..71b413e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,14 @@ npm only installs the microphone helper for your platform (one of the `earpop-ca Run `earpop`. The first time, it asks for your Soniox region and API key, verifies the key with Soniox, and then asks you to pick a microphone. After that it goes straight to captions. -Language hints default to English. Press `s` and choose "Choose languages" to change them. Pick one language to keep the output in that language, or several for a mixed conversation. +Language hints default to your system language plus English (English only on an English system). Press `s` and choose "Choose languages" to change them; type to search the list. Pick one language to keep the output in that language, or several for a mixed conversation. + +To use other languages for a single run without changing your settings, pass `--lang`: + +```bash +earpop --lang ja,en +earpop languages # list supported codes +``` ## Live screen @@ -34,18 +41,19 @@ Language hints default to English. Press `s` and choose "Choose languages" to ch p pause 1-9 speaker 0 all Up history m mic s settings ESC end ``` -Dimmed text is provisional and gets replaced once Soniox finalizes it. The live view shows the last six lines; press `Up` or scroll the mouse wheel to read earlier captions from this session. Everything also goes into the transcript file. +Dimmed text is provisional and gets replaced once Soniox finalizes it. A new line starts when the speaker or language changes, at the end of a sentence, or after a pause of more than two seconds. With more than one language selected, each line also shows the detected language, such as `[S1 ja]`. The live view shows the last six lines; press `Up` or scroll the mouse wheel to read earlier captions from this session. Everything also goes into the transcript file. -| Key | Action | -| ---------- | --------------------------------------------------------------------------------------------------- | -| `p` | Pause or resume. Pausing also copies the transcript path to the clipboard. | -| `1` to `9` | Show only that speaker. Press more numbers to add speakers, or the same number again to remove one. | -| `0` | Show everyone again | -| `m` | Switch microphone | -| `s` | Settings | -| `Up` | Open the session history. `Up`/`Down` and `PgUp`/`PgDn` scroll, `Home` jumps to the start | -| `End` | Leave the history and return to live captions (`ESC` does the same while the history is open) | -| `ESC` | Press twice to end the session, then once more to quit | +| Key | Action | +| ---------- | --------------------------------------------------------------------------------------------------------- | +| `p` | Pause or resume. Pausing also copies the transcript path to the clipboard. | +| `1` to `9` | Show only that speaker. Press more numbers to add speakers, or the same number again to remove one. | +| `0` | Show everyone again | +| `l` | With several languages or translation on, show only one language. Press again for the next; `0` shows all | +| `m` | Switch microphone | +| `s` | Settings | +| `Up` | Open the session history. `Up`/`Down` and `PgUp`/`PgDn` scroll, `Home` jumps to the start | +| `End` | Leave the history and return to live captions (`ESC` does the same while the history is open) | +| `ESC` | Press twice to end the session, then once more to quit | `Ctrl+C` does the same as `ESC`. When you quit, earpop waits for the last captions to be saved; press once more to quit right away. @@ -57,23 +65,49 @@ When you quit, earpop prints the transcript path and copies it to the clipboard. `earpop auth clear` deletes the saved key for the current region. +### Translation + +Soniox can translate while it transcribes. In settings, choose "Translation" and pick one language to translate everything into it, or two to translate between them (for example English and Spanish in a bilingual meeting). Each translated line appears in italics right under the speech it translates, tagged with its language. For a single run: + +```bash +earpop --translate ko # everything into Korean +earpop --translate en,es # English to Spanish and Spanish to English +``` + +Translations are saved in the transcript too. Text exports label them, such as `S1 (ko translation)`, and SRT and WebVTT exports show each translation at the same time as the speech, so players stack the two lines. + +### Terms + +Names and jargon that Soniox should spell your way, such as product names or people, go in a term list. Write each term in the language and script people actually say it in. The list is sent with every session, and changes apply the next time you run earpop. + +```bash +earpop terms add "Kold Brew" 웬민 Kubernetes +earpop terms # list +earpop terms remove Kubernetes +earpop terms clear +``` + ## Transcripts Each session is a JSONL file named after its start time, such as `2026-10-12_14-03-27.jsonl`. Final text is appended as it arrives, with timestamps, speaker, and language for every token. ```bash -earpop transcripts list # newest first -earpop transcripts view # print as plain text -earpop transcripts export # write .txt next to the JSONL file +earpop transcripts list # newest first, with start time and length +earpop transcripts view # print as plain text +earpop transcripts export # write .txt next to the JSONL file +earpop transcripts export latest -f srt # newest transcript as subtitles +earpop transcripts export -f md -o notes.md ``` -The plain text version starts a new paragraph when the speaker changes or after more than two seconds without speech: +`--format` (`-f`) takes `txt`, `srt`, `vtt`, or `md`; `--out` (`-o`) picks the output path. Use `latest` in place of an ID for the newest transcript. Subtitle cues end at sentence boundaries and stay under about two lines. + +The plain text version starts a new paragraph when the speaker or language changes, or after more than two seconds without speech. When a transcript mixes languages, each paragraph is labeled with its language code: ```text earpop transcript v1 id: 2026-10-12_14-03-27 started: 2026-10-12T14:03:27+09:00 -model: stt-rt-v5 +model: stt-rt-v6 [00:00] S1: So the plan is to ship the beta on Friday. [00:04] S2: Does that include the Windows build? diff --git a/package.json b/package.json index ec93d65..4d78b80 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "capture:check": "node scripts/publish-capture.mjs", "release": "node scripts/release.mjs", "check": "tsc --noEmit", + "test": "node --import tsx --test \"src/**/*.test.ts\"", "lint": "oxlint", "lint:fix": "oxlint --fix", "fmt": "oxfmt", diff --git a/src/app/live-app.tsx b/src/app/live-app.tsx index e8ca2c1..d0a6ab8 100644 --- a/src/app/live-app.tsx +++ b/src/app/live-app.tsx @@ -13,14 +13,14 @@ import { deleteApiKey, loadApiKey, saveApiKey } from "#/lib/api-key.js"; import { errorMessage } from "#/lib/errors.js"; import { openJournal } from "#/lib/journal.js"; import { - DEFAULT_LANGUAGE_HINTS, languageHintLabel, normalizeLanguageHints, + translationLabel, type SttLanguage, } from "#/lib/languages.js"; import { listMicrophones } from "#/lib/microphones.js"; import { regionLabel, resolveRegion } from "#/lib/region.js"; -import { saveSettings, loadSettings } from "#/lib/settings.js"; +import { fitTerms, saveSettings, loadSettings } from "#/lib/settings.js"; import { STT_MODEL } from "#/lib/soniox.js"; import { verifyApiKey } from "#/lib/verify-key.js"; import type { CliSettings, Microphone, SttRegion } from "#/lib/types.js"; @@ -49,7 +49,7 @@ function FatalScreen(props: { message: string; onQuit: () => void }) { type SettingsOverlay = Extract; -export function LiveApp() { +export function LiveApp(props: { languages?: SttLanguage[]; translation?: SttLanguage[] }) { const [phase, setPhase] = useState("boot"); const [verifying, setVerifying] = useState(false); const [keyError, setKeyError] = useState(); @@ -64,7 +64,10 @@ export function LiveApp() { const [finishing, setFinishing] = useState(false); const controllerRef = useRef(null); - const languageHintsRef = useRef([...DEFAULT_LANGUAGE_HINTS]); + // Session options read once at boot; the controller owns them afterwards (see snapshot). + const languageHintsRef = useRef([]); + const termsRef = useRef([]); + const translationRef = useRef([]); // Invalidate in-flight overlay loads when the overlay closes or is replaced. const overlayEpochRef = useRef(0); @@ -135,6 +138,8 @@ export function LiveApp() { clientReferenceId: journal.id, region: liveRegion, languageHints: languageHintsRef.current, + terms: termsRef.current, + translation: translationRef.current, onSnapshot: setSnapshot, onFatal: failFatal, }); @@ -170,7 +175,10 @@ export function LiveApp() { const boot = useEffectEvent(async (settings: CliSettings) => { const savedRegion = resolveRegion(settings); setRegion(savedRegion); - languageHintsRef.current = normalizeLanguageHints(settings.languages); + // --lang and --translate apply to this run only; picking them in settings still saves them. + languageHintsRef.current = props.languages ?? normalizeLanguageHints(settings.languages); + termsRef.current = fitTerms(settings.terms ?? []); + translationRef.current = props.translation ?? settings.translation ?? []; const apiKey = await loadApiKey(savedRegion); if (apiKey === null) { setPhase("region-select"); @@ -324,7 +332,8 @@ export function LiveApp() { microphonesLoading: true, currentMicrophone: snapshot.device, region, - languages: languageHintsRef.current, + languages: snapshot.languages, + translation: snapshot.translation, hasApiKey: true, }); void (async () => { @@ -339,8 +348,9 @@ export function LiveApp() { microphonesLoading: false, currentMicrophone: settings.microphone ?? snapshot.device, region: resolved, - // Live session hints are source of truth while connected. - languages: languageHintsRef.current, + // The controller owns languages and translation while connected. + languages: snapshot.languages, + translation: snapshot.translation, hasApiKey: key !== null, }); })(); @@ -359,7 +369,6 @@ export function LiveApp() { .catch(failSettings("set microphone")); }} onSettingsPickLanguages={(codes) => { - languageHintsRef.current = codes; void controllerRef.current ?.setLanguageHints(codes) .then(() => @@ -375,6 +384,22 @@ export function LiveApp() { }), ); }} + onSettingsPickTranslation={(codes) => { + void controllerRef.current + ?.setTranslation(codes) + .then(() => + patchSettings({ + translation: codes, + notice: `Translation: ${translationLabel(codes)}`, + }), + ) + .catch((error: unknown) => + patchSettings({ + translation: codes, + notice: `Failed to set translation: ${errorMessage(error)}`, + }), + ); + }} onSettingsPickRegion={(next) => { void saveSettings({ region: next }) .then(async () => { diff --git a/src/app/settings-app.tsx b/src/app/settings-app.tsx index 1f3528f..0feca11 100644 --- a/src/app/settings-app.tsx +++ b/src/app/settings-app.tsx @@ -4,9 +4,10 @@ import { useInterrupt } from "#/hooks/use-interrupt.js"; import { deleteApiKey, loadApiKey, saveApiKey } from "#/lib/api-key.js"; import { errorMessage } from "#/lib/errors.js"; import { - DEFAULT_LANGUAGE_HINTS, + defaultLanguageHints, languageHintLabel, normalizeLanguageHints, + translationLabel, type SttLanguage, } from "#/lib/languages.js"; import { listMicrophones } from "#/lib/microphones.js"; @@ -20,7 +21,8 @@ export function SettingsApp() { const [microphones, setMicrophones] = useState([]); const [loading, setLoading] = useState(true); const [currentMicrophone, setCurrentMicrophone] = useState(); - const [languages, setLanguages] = useState([...DEFAULT_LANGUAGE_HINTS]); + const [languages, setLanguages] = useState(defaultLanguageHints); + const [translation, setTranslation] = useState([]); const [region, setRegion] = useState(); const [hasApiKey, setHasApiKey] = useState(false); const [notice, setNotice] = useState(); @@ -38,6 +40,7 @@ export function SettingsApp() { const [key, found] = await Promise.all([loadApiKey(resolved), microphonesLoad]); setCurrentMicrophone(settings.microphone); setLanguages(normalizeLanguageHints(settings.languages)); + setTranslation(settings.translation ?? []); setRegion(resolved); setHasApiKey(key !== null); setMicrophones(found); @@ -48,39 +51,57 @@ export function SettingsApp() { }); }, []); + const failWith = (action: string) => (error: unknown) => + setNotice(`Failed to ${action}: ${errorMessage(error)}`); + return ( { - void saveSettings({ microphone: name }).then(() => { - setCurrentMicrophone(name); - setNotice(`Microphone set to ${name}`); - }); + void saveSettings({ microphone: name }) + .then(() => { + setCurrentMicrophone(name); + setNotice(`Microphone set to ${name}`); + }) + .catch(failWith("set microphone")); }} onPickLanguages={(codes) => { - void saveSettings({ languages: codes }).then(() => { - setLanguages(codes); - setNotice(`Languages set to ${languageHintLabel(codes)}`); - }); + void saveSettings({ languages: codes }) + .then(() => { + setLanguages(codes); + setNotice(`Languages set to ${languageHintLabel(codes)}`); + }) + .catch(failWith("set languages")); + }} + onPickTranslation={(codes) => { + void saveSettings({ translation: codes }) + .then(() => { + setTranslation(codes); + setNotice(`Translation: ${translationLabel(codes)}`); + }) + .catch(failWith("set translation")); }} onPickRegion={(next) => { - void saveSettings({ region: next }).then(async () => { - regionRef.current = next; - setRegion(next); - const key = await loadApiKey(next); - setHasApiKey(key !== null); - setNotice( - `Region set to ${regionLabel(next)}${ - key === null ? " (register an API key for this region)" : "" - }`, - ); - }); + void saveSettings({ region: next }) + .then(async () => { + regionRef.current = next; + setRegion(next); + const key = await loadApiKey(next); + setHasApiKey(key !== null); + setNotice( + `Region set to ${regionLabel(next)}${ + key === null ? " (register an API key for this region)" : "" + }`, + ); + }) + .catch(failWith("save region")); }} onChangeApiKey={(key) => { void saveApiKey({ key, region: regionRef.current }) @@ -88,15 +109,15 @@ export function SettingsApp() { setHasApiKey(true); setNotice(`Saved API key for ${regionLabel(regionRef.current)}`); }) - .catch((error: unknown) => { - setNotice(`Failed to save API key: ${errorMessage(error)}`); - }); + .catch(failWith("save API key")); }} onDeleteApiKey={() => { - void deleteApiKey(regionRef.current).then(() => { - setHasApiKey(false); - setNotice(`Deleted API key for ${regionLabel(regionRef.current)}`); - }); + void deleteApiKey(regionRef.current) + .then(() => { + setHasApiKey(false); + setNotice(`Deleted API key for ${regionLabel(regionRef.current)}`); + }) + .catch(failWith("delete API key")); }} onExit={() => { quit(); diff --git a/src/app/terms-command.ts b/src/app/terms-command.ts new file mode 100644 index 0000000..f56b8a8 --- /dev/null +++ b/src/app/terms-command.ts @@ -0,0 +1,56 @@ +import { + loadSettings, + MAX_TERMS_CHARS, + normalizeTerms, + saveSettings, + termsSize, +} from "#/lib/settings.js"; + +const USAGE = `Usage: earpop terms List saved terms + earpop terms add ... Add words or names to spell as listed + earpop terms remove ... Remove terms + earpop terms clear Remove every term`; + +export async function runTermsCommand(args: string[]) { + const [sub, ...values] = args; + const current = (await loadSettings()).terms ?? []; + + if (sub === undefined || sub === "list") { + if (current.length === 0) { + console.log("No saved terms. Add some with: earpop terms add ..."); + return; + } + console.log(current.join("\n")); + return; + } + + if (sub === "add" && values.length > 0) { + const next = normalizeTerms([...current, ...values]); + const size = termsSize(next); + if (size > MAX_TERMS_CHARS) { + throw new Error( + `Terms would total ${size} characters; the limit is ${MAX_TERMS_CHARS}. Remove terms people rarely say.`, + ); + } + await saveSettings({ terms: next }); + console.log(`Saved ${next.length - current.length} new term(s), ${next.length} in total.`); + return; + } + + if (sub === "remove" && values.length > 0) { + const drop = new Set(normalizeTerms(values)); + const next = current.filter((term) => !drop.has(term)); + await saveSettings({ terms: next }); + console.log(`Removed ${current.length - next.length} term(s), ${next.length} left.`); + return; + } + + if (sub === "clear") { + await saveSettings({ terms: [] }); + console.log("Removed every term."); + return; + } + + console.error(USAGE); + process.exitCode = 1; +} diff --git a/src/app/transcripts-command.ts b/src/app/transcripts-command.ts index 84f43dd..f0cb765 100644 --- a/src/app/transcripts-command.ts +++ b/src/app/transcripts-command.ts @@ -1,16 +1,35 @@ import { writeFile } from "node:fs/promises"; -import { join } from "node:path"; +import { parseArgs } from "node:util"; -import { exportText, listTranscripts } from "#/lib/transcripts.js"; -import { transcriptsDir } from "#/lib/journal.js"; +import { transcriptPath } from "#/lib/journal.js"; +import { + isTranscriptFormat, + renderTranscript, + TRANSCRIPT_FORMATS, +} from "#/lib/transcript-formats.js"; +import { listTranscripts, readTranscript, resolveTranscriptId } from "#/lib/transcripts.js"; + +const USAGE = `Usage: earpop transcripts list + earpop transcripts view [--format ${TRANSCRIPT_FORMATS.join("|")}] + earpop transcripts export [--format ${TRANSCRIPT_FORMATS.join("|")}] [--out ]`; + +export async function runTranscriptsCommand(args: string[]) { + let parsed; + try { + parsed = parseArgs({ + args, + allowPositionals: true, + options: { + format: { type: "string", short: "f", default: "txt" }, + out: { type: "string", short: "o" }, + }, + }); + } catch (error) { + throw new Error(`${error instanceof Error ? error.message : String(error)}\n${USAGE}`); + } + const [sub, rawId] = parsed.positionals; + const { format, out } = parsed.values; -export async function runTranscriptsCommand({ - sub, - id, -}: { - sub: string | undefined; - id: string | undefined; -}) { if (sub === "list") { const summaries = await listTranscripts(); if (summaries.length === 0) { @@ -18,23 +37,27 @@ export async function runTranscriptsCommand({ return; } for (const summary of summaries) { - console.log(`${summary.id}\t${summary.startedAt}\t${summary.tokenCount} tokens`); + console.log(`${summary.id}\t${summary.startedAt}\t${summary.duration}`); } return; } - if ((sub === "view" || sub === "export") && id !== undefined) { - const text = await exportText(id); + if ((sub === "view" || sub === "export") && rawId !== undefined) { + if (!isTranscriptFormat(format)) { + throw new Error(`Unknown format: ${format}. Use one of ${TRANSCRIPT_FORMATS.join(", ")}.`); + } + const id = await resolveTranscriptId(rawId); + const text = renderTranscript({ id, transcript: await readTranscript(id), format }); if (sub === "view") { process.stdout.write(text); return; } - const outPath = join(transcriptsDir(), `${id}.txt`); + const outPath = out ?? transcriptPath(id).replace(/\.jsonl$/, `.${format}`); await writeFile(outPath, text); console.log(outPath); return; } - console.error("Usage: earpop transcripts list | view | export "); + console.error(USAGE); process.exitCode = 1; } diff --git a/src/app/tui.tsx b/src/app/tui.tsx index efe87b0..d24f1aa 100644 --- a/src/app/tui.tsx +++ b/src/app/tui.tsx @@ -4,13 +4,29 @@ import { installInterruptListeners } from "#/app/interrupt.js"; import { LiveApp } from "#/app/live-app.js"; import { registerApp } from "#/app/runtime.js"; import { SettingsApp } from "#/app/settings-app.js"; +import type { SttLanguage } from "#/lib/languages.js"; -export function runTui(screen: "live" | "settings") { +export function runTui({ + screen, + languages, + translation, +}: { + screen: "live" | "settings"; + languages?: SttLanguage[]; + translation?: SttLanguage[]; +}) { installInterruptListeners(); registerApp( - render(screen === "live" ? : , { - exitOnCtrlC: false, - incrementalRendering: true, - }), + render( + screen === "live" ? ( + + ) : ( + + ), + { + exitOnCtrlC: false, + incrementalRendering: true, + }, + ), ); } diff --git a/src/components/language-select.tsx b/src/components/language-select.tsx index 940ca2d..e924025 100644 --- a/src/components/language-select.tsx +++ b/src/components/language-select.tsx @@ -1,43 +1,83 @@ import { MultiSelect } from "@inkjs/ui"; import { Box, Text, useInput } from "ink"; -import { useState } from "react"; +import { useMemo, useState } from "react"; import { - LANGUAGE_SELECT_OPTIONS, + languageSelectOptions, normalizeLanguageHints, type SttLanguage, } from "#/lib/languages.js"; interface LanguageSelectProps { current: SttLanguage[]; + /** Explains what the selection means; defaults to the language hint wording. */ + help?: string; + /** Allows saving with nothing selected. */ + allowEmpty?: boolean; + max?: number; onPick: (codes: SttLanguage[]) => void; onCancel: () => void; } export function LanguageSelect(props: LanguageSelectProps) { const [error, setError] = useState(); + const [query, setQuery] = useState(""); + // MultiSelect resets to defaultValue when its options change, so carry the selection across filters. + const [selected, setSelected] = useState(props.current); + // Order once on open so options do not jump while toggling. + const [allOptions] = useState(() => languageSelectOptions({ selected: props.current })); + const options = useMemo(() => { + const needle = query.toLowerCase(); + return needle === "" + ? allOptions + : allOptions.filter((option) => option.label.toLowerCase().includes(needle)); + }, [allOptions, query]); - useInput((_input, key) => { - if (key.escape) props.onCancel(); + useInput((input, key) => { + if (key.escape) { + if (query !== "") setQuery(""); + else props.onCancel(); + return; + } + if (key.backspace || key.delete) { + setQuery((prev) => prev.slice(0, -1)); + return; + } + // Space toggles in MultiSelect; any other printable character narrows the list. + if (input !== "" && input !== " " && !key.ctrl && !key.meta && /^\P{C}+$/u.test(input)) { + setQuery((prev) => prev + input); + } }); return ( - Space toggles, Enter saves. A single language uses strict restriction. + + {`Type to search, Space toggles, Enter saves. ${props.help ?? "A single language uses strict restriction."}`} + + {query !== "" && {`Search: ${query}`}} {error !== undefined && {error}} - { - if (values.length === 0) { - setError("Select at least one language"); - return; - } - props.onPick(normalizeLanguageHints(values)); - }} - /> - ESC cancel + {options.length === 0 ? ( + No matching languages + ) : ( + { + if (values.length === 0 && props.allowEmpty !== true) { + setError("Select at least one language"); + return; + } + if (props.max !== undefined && values.length > props.max) { + setError(`Select at most ${props.max} languages`); + return; + } + props.onPick(values.length === 0 ? [] : normalizeLanguageHints(values)); + }} + /> + )} + {query !== "" ? "ESC clear search" : "ESC cancel"} ); } diff --git a/src/index.tsx b/src/index.tsx index 7fe747a..f3dfcda 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,33 +1,104 @@ +import { parseArgs } from "node:util"; + +import { runTermsCommand } from "#/app/terms-command.js"; import { runTranscriptsCommand } from "#/app/transcripts-command.js"; import { deleteApiKey } from "#/lib/api-key.js"; import { errorMessage } from "#/lib/errors.js"; +import { listLanguages, parseLanguageList } from "#/lib/languages.js"; import { resolveRegion } from "#/lib/region.js"; import { loadSettings } from "#/lib/settings.js"; +import { version } from "../package.json"; const HELP = [ "earpop: live transcription", "", "Usage:", - " earpop Start live transcription (s opens settings)", + " earpop [--lang ] Start live transcription (s opens settings)", + " --lang ja,en sets languages for this run only", + " --translate ko translates into Korean;", + " --translate en,es translates between two", " earpop settings Settings (microphone, languages, API key)", " earpop transcripts list List transcripts", - " earpop transcripts view Print transcript as txt to stdout", - " earpop transcripts export Save transcript as a txt file", + " earpop transcripts view Print a transcript (id or latest)", + " earpop transcripts export Save a transcript next to its JSONL file", + " --format txt|srt|vtt|md, --out ", + " earpop languages List supported language codes", + " earpop terms [add|remove] Words and names to spell as listed", " earpop auth clear Delete the saved API key", + " earpop --version Print the version", ].join("\n"); function main() { - const [, , command, ...rest] = process.argv; + const [first, ...rest] = process.argv.slice(2); - if (command === undefined || command === "settings") { + // Live mode is the bare command, so its options come first; subcommands parse their own. + if (first === undefined || (first.startsWith("-") && first !== "-h" && first !== "--help")) { + let parsed; + try { + parsed = parseArgs({ + args: process.argv.slice(2), + options: { + lang: { type: "string" }, + translate: { type: "string" }, + version: { type: "boolean", short: "v" }, + }, + }).values; + } catch (error) { + console.error(`${errorMessage(error)}\n\n${HELP}`); + process.exitCode = 1; + return; + } + if (parsed.version === true) { + console.log(version); + return; + } + let languages; + let translation; + try { + languages = + parsed.lang !== undefined + ? parseLanguageList({ raw: parsed.lang, flag: "--lang" }) + : undefined; + translation = + parsed.translate !== undefined + ? parseLanguageList({ raw: parsed.translate, flag: "--translate" }) + : undefined; + if (translation !== undefined && translation.length > 2) { + throw new Error("--translate takes one language (one-way) or two (two-way)"); + } + } catch (error) { + console.error(errorMessage(error)); + process.exitCode = 1; + return; + } void import("#/app/tui.js").then(({ runTui }) => - runTui(command === undefined ? "live" : "settings"), + runTui({ screen: "live", languages, translation }), ); return; } + const command = first; + + if (command === "languages") { + console.log(listLanguages()); + return; + } + + if (command === "settings") { + void import("#/app/tui.js").then(({ runTui }) => runTui({ screen: "settings" })); + return; + } + if (command === "transcripts") { - void runTranscriptsCommand({ sub: rest[0], id: rest[1] }).catch((error: unknown) => { + void runTranscriptsCommand(rest).catch((error: unknown) => { + console.error(errorMessage(error)); + process.exitCode = 1; + }); + return; + } + + if (command === "terms") { + void runTermsCommand(rest).catch((error: unknown) => { console.error(errorMessage(error)); process.exitCode = 1; }); diff --git a/src/lib/caption-layout.test.ts b/src/lib/caption-layout.test.ts new file mode 100644 index 0000000..3395ecc --- /dev/null +++ b/src/lib/caption-layout.test.ts @@ -0,0 +1,236 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import stringWidth from "string-width"; + +import { toLines, wrapUtterances } from "#/lib/caption-layout.js"; +import type { Token } from "#/lib/types.js"; + +// Soniox sends spacing as a leading space on the next token ("How", " are", " you", "?"). +function finals(speaker: string, ...texts: string[]) { + return texts.map((text): Token => ({ text, isFinal: true, speaker })); +} + +function lines(finalTokens: Token[], pendingTokens: Token[] = []) { + return toLines({ finalTokens, pendingTokens, focusedSpeakers: new Set(), tailChars: Infinity }); +} + +function rowText(rows: ReturnType) { + return rows.map((row) => row.map((piece) => piece.text).join("")); +} + +function wrap(tokens: Token[], width: number) { + return rowText(wrapUtterances({ utterances: lines(tokens), width, maxLines: Infinity })); +} + +function stableTexts(tokens: Token[]) { + return lines(tokens).map((line) => line.stable.trim()); +} + +describe("toLines", () => { + it("splits on a period followed by a new token", () => { + const result = lines(finals("1", "We", " ship", " Friday", ".", " Any", " questions", "?")); + assert.deepEqual( + result.map((line) => line.stable), + ["We ship Friday.", " Any questions?"], + ); + }); + + it("keeps decimals and a trailing period on the same line", () => { + assert.deepEqual(stableTexts(finals("1", "Version", " 1", ".", "5", " ships", ".")), [ + "Version 1.5 ships.", + ]); + }); + + it("splits after a period followed by an unspaced script", () => { + assert.deepEqual(stableTexts(finals("1", "OK", ".", "了解", "。")), ["OK.", "了解。"]); + }); + + it("splits on sentence marks of other scripts", () => { + assert.deepEqual(stableTexts(finals("1", "नमस्ते", "।", " आप", " कैसे", " हैं", "?")), [ + "नमस्ते।", + "आप कैसे हैं?", + ]); + assert.deepEqual(stableTexts(finals("1", "مرحبا", "؟", " نعم")), ["مرحبا؟", "نعم"]); + assert.deepEqual(stableTexts(finals("1", "شکریہ", "۔", " جی")), ["شکریہ۔", "جی"]); + assert.deepEqual(stableTexts(finals("1", "はい", "。", "そうです")), ["はい。", "そうです"]); + }); + + it("looks past closing quotes", () => { + assert.deepEqual(stableTexts(finals("1", "He", " said", ' "why?"', " Then")), [ + 'He said "why?"', + "Then", + ]); + assert.deepEqual(stableTexts(finals("1", "「はい。」", "次")), ["「はい。」", "次"]); + }); + + it("starts a new line after a long pause, for scripts without sentence marks", () => { + const tokens: Token[] = [ + { text: "สวัสดี", isFinal: true, speaker: "1", startMs: 0, endMs: 500 }, + { text: "ครับ", isFinal: true, speaker: "1", startMs: 600, endMs: 900 }, + { text: "ไป", isFinal: true, speaker: "1", startMs: 3_500, endMs: 3_800 }, + ]; + assert.deepEqual(stableTexts(tokens), ["สวัสดีครับ", "ไป"]); + }); + + it("continues a period line into pending tokens", () => { + const result = lines(finals("1", "Done", "."), [ + { text: " Next", isFinal: false, speaker: "1" }, + ]); + assert.deepEqual( + result.map((line) => [line.stable.trim(), line.pending.trim()]), + [ + ["Done.", ""], + ["", "Next"], + ], + ); + }); +}); + +describe("wrapUtterances", () => { + it("keeps a long unspaced sentence on the speaker tag row", () => { + const utterances = lines(finals("1", "今日は", "とても", "良い", "天気", "ですね")); + const rows = rowText(wrapUtterances({ utterances, width: 12, maxLines: Infinity })); + assert.match(rows[0]!, /^\[S1\] \S/); + }); + + it("breaks unspaced scripts between words, never past the width", () => { + const text = "今日は良い天気ですね。明日も晴れるでしょう。"; + for (const width of [10, 14, 20]) { + const rows = wrap(finals("1", text), width); + assert.ok( + rows.every((row) => stringWidth(row) <= width), + rows.join("|"), + ); + assert.equal(rows.join("").replace(/\[S1\]|\s/g, ""), text); + } + assert.ok(!wrap(finals("1", text), 14).some((row) => row.trimStart().startsWith("。"))); + }); + + it("breaks Thai on word boundaries", () => { + const rows = wrap(finals("1", "สวัสดีครับวันนี้อากาศดีมาก"), 16); + assert.ok(rows.length > 1); + assert.ok( + rows.every((row) => stringWidth(row) <= 16), + rows.join("|"), + ); + }); + + it("wraps spaced words onto indented rows", () => { + assert.deepEqual( + wrap(finals("2", "alpha", " beta", " gamma", " delta"), 16).map((row) => row.trimEnd()), + ["[S2] alpha beta", " gamma delta"], + ); + }); + + it("splits a word that cannot fit beside the tag instead of orphaning the tag", () => { + const rows = wrap(finals("1", "internationalization"), 12); + assert.equal(rows[0], "[S1] interna"); + assert.ok(rows.every((row) => stringWidth(row) <= 12)); + }); +}); + +describe("languages", () => { + const mixed: Token[] = [ + { text: "Hello", isFinal: true, speaker: "1", language: "en" }, + { text: " there", isFinal: true, speaker: "1", language: "en" }, + { text: " ", isFinal: true, speaker: "1", language: "ko" }, + { text: "안녕", isFinal: true, speaker: "1", language: "ko" }, + { text: "하세요", isFinal: true, speaker: "1", language: "ko" }, + ]; + + it("starts a new line when the language changes", () => { + assert.deepEqual( + lines(mixed).map((line) => [line.language, line.stable.trim()]), + [ + ["en", "Hello there"], + ["ko", "안녕하세요"], + ], + ); + }); + + it("filters by language", () => { + const result = toLines({ + finalTokens: mixed, + pendingTokens: [], + focusedSpeakers: new Set(), + focusedLanguage: "ko", + tailChars: Infinity, + }); + assert.deepEqual( + result.map((line) => line.stable.trim()), + ["안녕하세요"], + ); + }); + + it("shows the language in the tag only when asked", () => { + const utterances = lines(mixed); + const render = (showLanguage: boolean) => + rowText(wrapUtterances({ utterances, width: 40, maxLines: Infinity, showLanguage })).map( + (row) => row.trimEnd(), + ); + const plain = render(false); + const tagged = render(true); + assert.deepEqual(plain, ["[S1] Hello there", "[S1] 안녕하세요"]); + assert.deepEqual(tagged, ["[S1 en] Hello there", "[S1 ko] 안녕하세요"]); + }); +}); + +describe("translations", () => { + const spoken = (text: string, language: string): Token => ({ + text, + isFinal: true, + speaker: "1", + language, + }); + const translated = (text: string, language: string): Token => ({ + ...spoken(text, language), + translation: true, + sourceLanguage: "en", + }); + + it("puts a translation on its own line under the speech", () => { + const tokens = [ + spoken("Hey", "en"), + spoken("?", "en"), + translated("Oye", "es"), + translated("?", "es"), + spoken(" Fine", "en"), + ]; + assert.deepEqual( + lines(tokens).map((line) => [line.translation === true, line.stable.trim()]), + [ + [false, "Hey?"], + [true, "Oye?"], + [false, "Fine"], + ], + ); + }); + + it("splits a one-way translation into the same language as the speech", () => { + const tokens = [spoken("Hello", "en"), translated(" Hello", "en")]; + assert.equal(lines(tokens).length, 2); + }); + + it("renders translations in italics", () => { + const rows = wrapUtterances({ + utterances: lines([spoken("Hey", "en"), translated("Oye", "es")]), + width: 40, + maxLines: Infinity, + showLanguage: true, + }); + assert.deepEqual( + rows.map((row) => row.map((piece) => [piece.text, piece.italic === true])), + [ + [ + ["[S1 en] ", false], + ["Hey", false], + ], + [ + ["[S1 es] ", false], + ["Oye", true], + ], + ], + ); + }); +}); diff --git a/src/lib/caption-layout.ts b/src/lib/caption-layout.ts new file mode 100644 index 0000000..6e17e97 --- /dev/null +++ b/src/lib/caption-layout.ts @@ -0,0 +1,342 @@ +import stringWidth from "string-width"; + +import { NO_SPACE_SCRIPT } from "#/lib/languages.js"; +import { PAUSE_BREAK_MS, sentenceMark, startsNewWord } from "#/lib/segments.js"; +import type { Token } from "#/lib/types.js"; + +/** Fixed palette for speaker tags S1–S9 (Ink named colors). */ +const SPEAKER_COLORS = [ + "cyan", + "yellow", + "magenta", + "green", + "blue", + "red", + "white", + "cyanBright", + "yellowBright", +] as const; + +type SpeakerColor = (typeof SPEAKER_COLORS)[number]; + +interface Utterance { + speaker?: string; + language?: string; + translation?: boolean; + stable: string; + pending: string; +} + +export interface Piece { + text: string; + dim: boolean; + italic?: boolean; + color?: SpeakerColor; + tag?: boolean; +} + +function speakerColor(speaker: string) { + const index = Number(speaker); + if (!Number.isInteger(index) || index < 1 || index > 9) return undefined; + return SPEAKER_COLORS[index - 1]; +} + +const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" }); + +function graphemes(text: string) { + return Array.from(graphemeSegmenter.segment(text), (part) => part.segment); +} + +const wordSegmenter = new Intl.Segmenter(undefined, { granularity: "word" }); + +// Line-break opportunities: whitespace runs, words, and dictionary words inside unspaced scripts +// (Japanese, Chinese, Thai). Punctuation stays glued to the word before it. +function breakUnits(text: string) { + const units: string[] = []; + for (const run of text.match(/\s+|\S+/g) ?? []) { + if (/^\s/.test(run) || !NO_SPACE_SCRIPT.test(run)) { + units.push(run); + continue; + } + let previous = ""; + for (const { segment, isWordLike } of wordSegmenter.segment(run)) { + if (isWordLike !== true && previous !== "") { + previous += segment; + units[units.length - 1] = previous; + continue; + } + previous = segment; + units.push(segment); + } + } + return units; +} + +export function toLines({ + finalTokens, + pendingTokens, + focusedSpeakers, + focusedLanguage, + tailChars, +}: { + finalTokens: Token[]; + pendingTokens: Token[]; + focusedSpeakers: ReadonlySet; + focusedLanguage?: string; + tailChars: number; +}) { + const lines: Utterance[] = []; + let speaker: string | undefined; + let language: string | undefined; + let translation = false; + let stable = ""; + let tail = ""; + let closed = false; + let afterPeriod = false; + let lastEndMs: number | undefined; + + const push = () => { + if (stable.trim() === "" && tail.trim() === "") return; + lines.push({ + speaker, + language, + ...(translation ? { translation: true } : {}), + stable, + pending: tail, + }); + language = undefined; + stable = ""; + tail = ""; + closed = false; + afterPeriod = false; + }; + + const eat = ({ token, pending }: { token: Token; pending: boolean }) => { + if ( + focusedSpeakers.size > 0 && + (token.speaker === undefined || !focusedSpeakers.has(token.speaker)) + ) { + return; + } + if (focusedLanguage !== undefined && token.language !== focusedLanguage) return; + const blank = token.text.trim() === ""; + const next = token.speaker; + const translated = token.translation === true; + const paused = + token.startMs !== undefined && + lastEndMs !== undefined && + token.startMs - lastEndMs > PAUSE_BREAK_MS; + if ( + closed || + paused || + (afterPeriod && startsNewWord(token.text)) || + (next !== undefined && next !== speaker) || + // Translations arrive right after the speech they translate; give them their own line. + (!blank && (stable !== "" || tail !== "") && translated !== translation) || + // Soniox keeps a sentence in one language, so a language switch starts a new line. + (!blank && + language !== undefined && + token.language !== undefined && + token.language !== language) + ) { + push(); + } + if (next !== undefined) speaker = next; + if (!blank && language === undefined) language = token.language; + if (!blank) translation = translated; + if (pending) tail += token.text; + else stable += token.text; + if (token.endMs !== undefined) lastEndMs = token.endMs; + if (!blank) { + const mark = sentenceMark(token.text); + closed = mark === "end"; + afterPeriod = mark === "period"; + } + }; + + let start = finalTokens.length; + for (let used = 0; start > 0 && used < tailChars; start -= 1) { + used += finalTokens[start - 1]!.text.length; + } + for (let i = start; i < finalTokens.length; i += 1) { + eat({ token: finalTokens[i]!, pending: false }); + } + for (const token of pendingTokens) { + eat({ token, pending: true }); + } + push(); + return lines; +} + +function wrapFlow({ pieces, width, indent }: { pieces: Piece[]; width: number; indent: number }) { + const lines: Piece[][] = []; + let line: Piece[] = []; + let lineWidth = 0; + + const padOnly = () => indent > 0 && line.length === 1 && line[0]!.text.trim() === ""; + + const newLine = (continuation: boolean) => { + if (line.length > 0 && !padOnly()) lines.push(line); + line = []; + lineWidth = 0; + if (continuation && indent > 0) { + line.push({ text: " ".repeat(indent), dim: false }); + lineWidth = indent; + } + }; + + const appendPiece = ({ piece, pieceWidth }: { piece: Piece; pieceWidth: number }) => { + line.push(piece); + lineWidth += pieceWidth; + }; + + const appendText = ({ + text, + textWidth, + piece, + }: { + text: string; + textWidth: number; + piece: Piece; + }) => { + if (text.length > 0) { + appendPiece({ + piece: { text, dim: piece.dim, italic: piece.italic, color: piece.color }, + pieceWidth: textWidth, + }); + } + }; + + const atLineStart = () => { + const last = line.at(-1); + return lineWidth === 0 || padOnly() || last?.tag === true; + }; + + for (const piece of pieces) { + if (piece.tag === true) { + appendPiece({ piece, pieceWidth: stringWidth(piece.text) }); + continue; + } + + for (const unit of breakUnits(piece.text)) { + const blank = /^\s+$/.test(unit); + if (blank && atLineStart()) continue; + + const unitWidth = stringWidth(unit); + // Never leave a speaker tag or indent alone on a row: a unit that cannot fit even there is split. + if (lineWidth + unitWidth > width && !atLineStart()) { + newLine(true); + if (blank) continue; + } + if (lineWidth + unitWidth <= width) { + appendText({ text: unit, textWidth: unitWidth, piece }); + continue; + } + + let chunk = ""; + let chunkWidth = 0; + for (const char of graphemes(unit)) { + const charWidthValue = stringWidth(char); + if (chunk !== "" && lineWidth + chunkWidth + charWidthValue > width) { + appendText({ text: chunk, textWidth: chunkWidth, piece }); + newLine(true); + chunk = ""; + chunkWidth = 0; + } + chunk += char; + chunkWidth += charWidthValue; + } + appendText({ text: chunk, textWidth: chunkWidth, piece }); + } + } + newLine(false); + return lines; +} + +export function wrapUtterances({ + utterances, + width, + maxLines, + showLanguage = false, +}: { + utterances: Utterance[]; + width: number; + maxLines: number; + showLanguage?: boolean; +}) { + const chunks: { pieces: Piece[]; tag?: Piece }[][] = []; + let rowCount = 0; + + for (let u = utterances.length - 1; u >= 0 && rowCount < maxLines; u -= 1) { + const utterance = utterances[u]!; + const pieces: Piece[] = []; + let tag: Piece | undefined; + let indent = 0; + const labels = [ + utterance.speaker !== undefined ? `S${utterance.speaker}` : undefined, + showLanguage ? utterance.language : undefined, + ].filter((label) => label !== undefined); + if (labels.length > 0) { + tag = { + text: `[${labels.join(" ")}] `, + dim: false, + tag: true, + color: utterance.speaker !== undefined ? speakerColor(utterance.speaker) : undefined, + }; + indent = stringWidth(tag.text); + pieces.push(tag); + } + const stable = utterance.stable.trimStart(); + const pending = stable.length === 0 ? utterance.pending.trimStart() : utterance.pending; + const italic = utterance.translation === true; + if (stable.length > 0) pieces.push({ text: stable, dim: false, italic }); + if (pending.length > 0) pieces.push({ text: pending, dim: true, italic }); + + const chunk = wrapFlow({ pieces, width, indent }).map((wrapped) => ({ pieces: wrapped, tag })); + chunks.push(chunk); + rowCount += chunk.length; + } + + const kept = chunks.reverse().flat().slice(-maxLines); + if (kept.length === 0) return []; + + const first = kept[0]!; + if (first.tag === undefined || first.pieces[0]?.tag === true) { + return kept.map((row) => row.pieces); + } + + const tagWidth = stringWidth(first.tag.text); + const bodyWidth = width - tagWidth; + if (bodyWidth <= 0) return [[first.tag], ...kept.slice(1).map((row) => row.pieces)]; + + const source = + first.pieces[0] !== undefined && /^\s+$/.test(first.pieces[0].text) + ? first.pieces.slice(1) + : first.pieces; + const body: Piece[] = []; + let remaining = bodyWidth; + for (let i = source.length - 1; i >= 0 && remaining > 0; i -= 1) { + const piece = source[i]!; + const pieceWidth = stringWidth(piece.text); + if (pieceWidth <= remaining) { + body.unshift(piece); + remaining -= pieceWidth; + continue; + } + const chars = graphemes(piece.text); + let start = chars.length; + let used = 0; + while (start > 0) { + const cw = stringWidth(chars[start - 1]!); + if (used + cw > remaining) break; + used += cw; + start -= 1; + } + if (start < chars.length) { + body.unshift({ ...piece, text: chars.slice(start).join("") }); + } + remaining = 0; + } + + return [[first.tag, ...body], ...kept.slice(1).map((row) => row.pieces)]; +} diff --git a/src/lib/journal.test.ts b/src/lib/journal.test.ts new file mode 100644 index 0000000..648c6f4 --- /dev/null +++ b/src/lib/journal.test.ts @@ -0,0 +1,55 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { describe, it } from "node:test"; + +import { openJournal } from "#/lib/journal.js"; +import { readTranscript } from "#/lib/transcripts.js"; +import { useTempHome } from "#/test-utils/temp-home.js"; + +describe("journal", () => { + useTempHome(); + + it("files translations at the end of the speech they follow", async () => { + const journal = openJournal("stt-rt-v6"); + journal.tokens({ + offsetMs: 10_000, + tokens: [ + { text: "Hi", isFinal: true, speaker: "1", language: "en", startMs: 100, endMs: 400 }, + { + text: "Hola", + isFinal: true, + speaker: "1", + language: "es", + translation: true, + sourceLanguage: "en", + }, + ], + }); + journal.close(); + + const lines = readFileSync(journal.path, "utf8") + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + assert.deepEqual(lines[2], { + kind: "token", + at_ms: 10_400, + end_ms: 10_400, + text: "Hola", + speaker: "1", + language: "es", + translation: true, + source_language: "en", + }); + + const transcript = await readTranscript(journal.id); + assert.deepEqual( + transcript.tokens.map((token) => [token.text, token.atMs, token.translation === true]), + [ + ["Hi", 10_100, false], + ["Hola", 10_400, true], + ], + ); + assert.equal(transcript.tokens[1]!.sourceLanguage, "en"); + }); +}); diff --git a/src/lib/journal.ts b/src/lib/journal.ts index 9318ca5..9bd432a 100644 --- a/src/lib/journal.ts +++ b/src/lib/journal.ts @@ -58,6 +58,8 @@ class FileJournal implements Journal { private closed = false; private warnedWriteFailure = false; + // Translations have no timestamps; they are filed at the end of the speech they follow. + private lastSpokenEndMs = 0; public constructor({ id, @@ -85,8 +87,14 @@ class FileJournal implements Journal { const fallbackMs = Date.now() - this.startedAtMs; let lines = ""; for (const token of tokens) { - const atMs = token.startMs !== undefined ? offsetMs + token.startMs : fallbackMs; - const endMs = token.endMs !== undefined ? offsetMs + token.endMs : atMs; + let atMs = this.lastSpokenEndMs; + let endMs = atMs; + if (token.translation !== true) { + atMs = token.startMs !== undefined ? offsetMs + token.startMs : fallbackMs; + endMs = token.endMs !== undefined ? offsetMs + token.endMs : atMs; + this.lastSpokenEndMs = endMs; + } + // Undefined fields drop out of the JSON, so plain speech lines keep their old shape. lines += JSON.stringify({ kind: "token", @@ -95,6 +103,8 @@ class FileJournal implements Journal { text: token.text, speaker: token.speaker, language: token.language, + translation: token.translation, + source_language: token.sourceLanguage, }) + "\n"; } this.write(lines); diff --git a/src/lib/languages.test.ts b/src/lib/languages.test.ts new file mode 100644 index 0000000..cbd2106 --- /dev/null +++ b/src/lib/languages.test.ts @@ -0,0 +1,118 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { + defaultLanguageHints, + languageGeneralContext, + languageSelectOptions, + localeLanguage, + normalizeLanguageHints, + normalizeTranslation, + parseLanguageList, + translationLabel, +} from "#/lib/languages.js"; + +function instructions(hints: Parameters[0]) { + return languageGeneralContext(hints).find((entry) => entry.key === "instructions")!.value; +} + +describe("locale defaults", () => { + it("maps system locales to Soniox codes", () => { + assert.equal(localeLanguage("ja-JP"), "ja"); + assert.equal(localeLanguage("nb-NO"), "no"); + assert.equal(localeLanguage("fil-PH"), "tl"); + assert.equal(localeLanguage("zz"), undefined); + assert.equal(localeLanguage("not a locale"), undefined); + }); + + it("pairs a non-English system language with English", () => { + assert.deepEqual(defaultLanguageHints("en-GB"), ["en"]); + assert.deepEqual(defaultLanguageHints("de-DE"), ["de", "en"]); + assert.deepEqual(defaultLanguageHints("zz"), ["en"]); + }); +}); + +describe("normalizeLanguageHints", () => { + it("drops unknown and duplicate codes", () => { + assert.deepEqual(normalizeLanguageHints(["ja", "xx", "ja", 3, "en"]), ["ja", "en"]); + }); +}); + +describe("parseLanguageList", () => { + it("accepts comma-separated codes in any case", () => { + assert.deepEqual(parseLanguageList({ raw: " JA, en ,", flag: "--lang" }), ["ja", "en"]); + }); + + it("rejects unknown or empty input", () => { + assert.throws( + () => parseLanguageList({ raw: "ja,xx", flag: "--lang" }), + /Unknown language code: xx/, + ); + assert.throws( + () => parseLanguageList({ raw: "", flag: "--translate" }), + /such as --translate en/, + ); + }); +}); + +describe("languageSelectOptions", () => { + it("puts selected, system, English, and Korean first", () => { + const values = languageSelectOptions({ selected: ["th", "ja"], locale: "de-DE" }) + .slice(0, 6) + .map((option) => option.value); + assert.deepEqual(values, ["th", "ja", "de", "en", "ko", "af"]); + }); + + it("lists every language once", () => { + const values = languageSelectOptions({ selected: ["en"], locale: "en-US" }).map( + (option) => option.value, + ); + assert.equal(new Set(values).size, values.length); + assert.equal(values.length, 60); + }); +}); + +describe("languageGeneralContext", () => { + it("keeps the Korean wording", () => { + assert.equal( + instructions(["ko"]), + "Speech is primarily Korean. Prefer Hangul. Keep English proper nouns, product names, and loanwords in Latin script.", + ); + assert.equal( + instructions(["ko", "en"]), + "Speech may mix Korean, English. Prefer Hangul for Korean. Keep other languages in their native scripts.", + ); + }); + + it("names the script for other non-Latin languages", () => { + assert.match(instructions(["ja"]), /Prefer kanji and kana\./); + assert.match( + instructions(["ru", "uk", "en"]), + /Prefer Cyrillic for Russian, Cyrillic for Ukrainian\./, + ); + }); + + it("restricts a single Latin-script language", () => { + assert.equal( + instructions(["de"]), + "Conversation is in German. Output transcription only in German.", + ); + assert.match(instructions(["de", "fr"]), /each language in its native script/); + }); +}); + +describe("translation settings", () => { + it("accepts one or two languages and turns off otherwise", () => { + assert.deepEqual(normalizeTranslation(["ko"]), ["ko"]); + assert.deepEqual(normalizeTranslation(["en", "es", "en"]), ["en", "es"]); + assert.deepEqual(normalizeTranslation(["en", "es", "ko"]), []); + assert.deepEqual(normalizeTranslation(["xx"]), []); + assert.deepEqual(normalizeTranslation(undefined), []); + }); + + it("describes the mode", () => { + assert.equal(translationLabel([]), "Off"); + assert.equal(translationLabel(["ko"]), "Into Korean (ko)"); + assert.equal(translationLabel(["en", "es"]), "Between English (en) and Spanish (es)"); + }); +}); diff --git a/src/lib/languages.ts b/src/lib/languages.ts index a4234f3..612e34a 100644 --- a/src/lib/languages.ts +++ b/src/lib/languages.ts @@ -1,16 +1,19 @@ -/** Soniox STT ISO codes: https://soniox.com/docs/stt/concepts/supported-languages */ +/** + * Soniox STT ISO codes: https://soniox.com/docs/stt/concepts/supported-languages + * `script` is set for languages not written in Latin script; it steers the recognition context. + */ const STT_LANGUAGES = [ { code: "af", name: "Afrikaans" }, { code: "sq", name: "Albanian" }, - { code: "ar", name: "Arabic" }, + { code: "ar", name: "Arabic", script: "Arabic script" }, { code: "az", name: "Azerbaijani" }, { code: "eu", name: "Basque" }, - { code: "be", name: "Belarusian" }, - { code: "bn", name: "Bengali" }, + { code: "be", name: "Belarusian", script: "Cyrillic" }, + { code: "bn", name: "Bengali", script: "Bengali script" }, { code: "bs", name: "Bosnian" }, - { code: "bg", name: "Bulgarian" }, + { code: "bg", name: "Bulgarian", script: "Cyrillic" }, { code: "ca", name: "Catalan" }, - { code: "zh", name: "Chinese" }, + { code: "zh", name: "Chinese", script: "Chinese characters" }, { code: "hr", name: "Croatian" }, { code: "cs", name: "Czech" }, { code: "da", name: "Danish" }, @@ -21,56 +24,63 @@ const STT_LANGUAGES = [ { code: "fr", name: "French" }, { code: "gl", name: "Galician" }, { code: "de", name: "German" }, - { code: "el", name: "Greek" }, - { code: "gu", name: "Gujarati" }, - { code: "he", name: "Hebrew" }, - { code: "hi", name: "Hindi" }, + { code: "el", name: "Greek", script: "Greek script" }, + { code: "gu", name: "Gujarati", script: "Gujarati script" }, + { code: "he", name: "Hebrew", script: "Hebrew script" }, + { code: "hi", name: "Hindi", script: "Devanagari" }, { code: "hu", name: "Hungarian" }, { code: "id", name: "Indonesian" }, { code: "it", name: "Italian" }, - { code: "ja", name: "Japanese" }, - { code: "kn", name: "Kannada" }, - { code: "kk", name: "Kazakh" }, - { code: "ko", name: "Korean" }, + { code: "ja", name: "Japanese", script: "kanji and kana" }, + { code: "kn", name: "Kannada", script: "Kannada script" }, + { code: "kk", name: "Kazakh", script: "Cyrillic" }, + { code: "ko", name: "Korean", script: "Hangul" }, { code: "lv", name: "Latvian" }, { code: "lt", name: "Lithuanian" }, - { code: "mk", name: "Macedonian" }, + { code: "mk", name: "Macedonian", script: "Cyrillic" }, { code: "ms", name: "Malay" }, - { code: "ml", name: "Malayalam" }, - { code: "mr", name: "Marathi" }, + { code: "ml", name: "Malayalam", script: "Malayalam script" }, + { code: "mr", name: "Marathi", script: "Devanagari" }, { code: "no", name: "Norwegian" }, - { code: "fa", name: "Persian" }, + { code: "fa", name: "Persian", script: "Persian script" }, { code: "pl", name: "Polish" }, { code: "pt", name: "Portuguese" }, - { code: "pa", name: "Punjabi" }, + { code: "pa", name: "Punjabi", script: "Gurmukhi" }, { code: "ro", name: "Romanian" }, - { code: "ru", name: "Russian" }, - { code: "sr", name: "Serbian" }, + { code: "ru", name: "Russian", script: "Cyrillic" }, + { code: "sr", name: "Serbian", script: "Cyrillic" }, { code: "sk", name: "Slovak" }, { code: "sl", name: "Slovenian" }, { code: "es", name: "Spanish" }, { code: "sw", name: "Swahili" }, { code: "sv", name: "Swedish" }, { code: "tl", name: "Tagalog" }, - { code: "ta", name: "Tamil" }, - { code: "te", name: "Telugu" }, - { code: "th", name: "Thai" }, + { code: "ta", name: "Tamil", script: "Tamil script" }, + { code: "te", name: "Telugu", script: "Telugu script" }, + { code: "th", name: "Thai", script: "Thai script" }, { code: "tr", name: "Turkish" }, - { code: "uk", name: "Ukrainian" }, - { code: "ur", name: "Urdu" }, + { code: "uk", name: "Ukrainian", script: "Cyrillic" }, + { code: "ur", name: "Urdu", script: "Urdu script" }, { code: "vi", name: "Vietnamese" }, { code: "cy", name: "Welsh" }, ] as const; export type SttLanguage = (typeof STT_LANGUAGES)[number]["code"]; -export const DEFAULT_LANGUAGE_HINTS: readonly SttLanguage[] = ["en"]; +// Scripts written without spaces between words. +export const NO_SPACE_SCRIPT = + /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}]/u; -const BY_CODE = new Map( +type LanguageEntry = { code: SttLanguage; name: string; script?: string }; + +const BY_CODE = new Map( STT_LANGUAGES.map((language) => [language.code, language]), ); -function isSttLanguage(value: string): value is SttLanguage { +// System locale subtags that Soniox lists under another code. +const LOCALE_ALIASES: Record = { nb: "no", nn: "no", fil: "tl", iw: "he" }; + +export function isSttLanguage(value: string): value is SttLanguage { return BY_CODE.has(value); } @@ -78,8 +88,25 @@ function languageName(code: SttLanguage) { return BY_CODE.get(code)?.name ?? code; } +export function localeLanguage(locale = new Intl.DateTimeFormat().resolvedOptions().locale) { + let language: string; + try { + language = new Intl.Locale(locale).language; + } catch { + return undefined; + } + const code = LOCALE_ALIASES[language] ?? language; + return isSttLanguage(code) ? code : undefined; +} + +// First run: the system language plus English, since meetings often mix in English terms. +export function defaultLanguageHints(locale?: string): SttLanguage[] { + const local = localeLanguage(locale); + return local === undefined || local === "en" ? ["en"] : [local, "en"]; +} + export function normalizeLanguageHints(raw: unknown) { - if (!Array.isArray(raw)) return [...DEFAULT_LANGUAGE_HINTS]; + if (!Array.isArray(raw)) return defaultLanguageHints(); const seen = new Set(); const hints: SttLanguage[] = []; for (const item of raw) { @@ -87,34 +114,79 @@ export function normalizeLanguageHints(raw: unknown) { seen.add(item); hints.push(item); } - return hints.length > 0 ? hints : [...DEFAULT_LANGUAGE_HINTS]; + return hints.length > 0 ? hints : defaultLanguageHints(); +} + +/** Translation targets: one language (one-way) or two (two-way); anything else turns it off. */ +export function normalizeTranslation(raw: unknown) { + if (!Array.isArray(raw)) return []; + const codes = [...new Set(raw)].filter( + (code): code is SttLanguage => typeof code === "string" && isSttLanguage(code), + ); + return codes.length <= 2 ? codes : []; +} + +export function translationLabel(codes: SttLanguage[]) { + if (codes.length === 0) return "Off"; + if (codes.length === 1) return `Into ${languageHintLabel(codes)}`; + return `Between ${codes.map((code) => languageHintLabel([code])).join(" and ")}`; } export function languageHintLabel(codes: SttLanguage[]) { return codes.map((code) => `${languageName(code)} (${code})`).join(", "); } -// Pin English/Korean first; the rest follow Soniox's alphabetical list. -export const LANGUAGE_SELECT_OPTIONS = [ - ...STT_LANGUAGES.filter((language) => language.code === "en" || language.code === "ko"), - ...STT_LANGUAGES.filter((language) => language.code !== "en" && language.code !== "ko"), -].map((language) => ({ - label: `${language.name} (${language.code})`, - value: language.code, -})); +/** Parses a comma-separated code list such as `ja,en`; throws on unknown codes. */ +export function parseLanguageList({ raw, flag }: { raw: string; flag: string }) { + const codes = raw + .split(",") + .map((code) => code.trim().toLowerCase()) + .filter((code) => code !== ""); + const unknown = codes.filter((code) => !isSttLanguage(code)); + if (unknown.length > 0) { + throw new Error( + `Unknown language code: ${unknown.join(", ")}. Run 'earpop languages' to list supported codes.`, + ); + } + if (codes.length === 0) throw new Error(`Specify at least one language code, such as ${flag} en`); + return [...new Set(codes)] as SttLanguage[]; +} + +export function listLanguages() { + return STT_LANGUAGES.map((language) => `${language.code}\t${language.name}`).join("\n"); +} + +// Selected languages first, then the system language, English, and Korean; the rest follow +// Soniox's alphabetical list. +export function languageSelectOptions({ + selected, + locale, +}: { + selected: SttLanguage[]; + locale?: string; +}) { + const pinned = [...selected, localeLanguage(locale), "en", "ko"]; + const rank = (code: string) => { + const index = pinned.indexOf(code); + return index === -1 ? pinned.length : index; + }; + return [...STT_LANGUAGES] + .sort((a, b) => rank(a.code) - rank(b.code)) + .map((language) => ({ label: `${language.name} (${language.code})`, value: language.code })); +} export function languageGeneralContext(hints: SttLanguage[]) { - const listed = hints.map(languageName).join(", "); - const koreanOnly = hints.length === 1 && hints[0] === "ko"; - const mixedWithKorean = hints.length > 1 && hints.includes("ko"); + const languages = hints.map((code) => BY_CODE.get(code)!); + const listed = languages.map((language) => language.name).join(", "); + const scripted = languages.filter((language) => language.script !== undefined); let instructions: string; - if (koreanOnly) { - instructions = - "Speech is primarily Korean. Prefer Hangul. Keep English proper nouns, product names, and loanwords in Latin script."; - } else if (hints.length === 1) { + if (languages.length === 1 && scripted.length === 1) { + instructions = `Speech is primarily ${listed}. Prefer ${scripted[0]!.script}. Keep English proper nouns, product names, and loanwords in Latin script.`; + } else if (languages.length === 1) { instructions = `Conversation is in ${listed}. Output transcription only in ${listed}.`; - } else if (mixedWithKorean) { - instructions = `Speech may mix ${listed}. Prefer Hangul for Korean. Keep other languages in their native scripts.`; + } else if (scripted.length > 0) { + const prefer = scripted.map((language) => `${language.script} for ${language.name}`).join(", "); + instructions = `Speech may mix ${listed}. Prefer ${prefer}. Keep other languages in their native scripts.`; } else { instructions = `Speech may mix ${listed}. Transcribe each language in its native script.`; } diff --git a/src/lib/segments.ts b/src/lib/segments.ts new file mode 100644 index 0000000..e42406b --- /dev/null +++ b/src/lib/segments.ts @@ -0,0 +1,24 @@ +import { NO_SPACE_SCRIPT } from "#/lib/languages.js"; + +// Boundary rules shared by live captions and transcript exports. + +// Marks that end a sentence on their own, across scripts (Latin, CJK, Devanagari, Arabic, Urdu). +const SENTENCE_END = /[?!。?!।॥؟۔]$/u; +// A sentence mark may sit inside closing quotes or brackets: `?"`, `。」`. +const CLOSING_MARKS = /[\s"'”’»)\]}」』》)】]+$/u; + +/** Silence that starts a new line or paragraph; also covers scripts without sentence marks (Thai). */ +export const PAUSE_BREAK_MS = 2_000; + +export function sentenceMark(text: string) { + const bare = text.replace(CLOSING_MARKS, ""); + if (SENTENCE_END.test(bare)) return "end"; + // "1.5" and "Dr. Smith" both arrive as "." then a token; only the next token can tell them apart. + if (bare.endsWith(".")) return "period"; + return null; +} + +// Soniox puts word spacing on the leading edge of the next token. +export function startsNewWord(text: string) { + return /^\s/.test(text) || NO_SPACE_SCRIPT.test(text.charAt(0)); +} diff --git a/src/lib/settings.test.ts b/src/lib/settings.test.ts new file mode 100644 index 0000000..d01f849 --- /dev/null +++ b/src/lib/settings.test.ts @@ -0,0 +1,41 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { + fitTerms, + loadSettings, + MAX_TERMS_CHARS, + normalizeTerms, + saveSettings, +} from "#/lib/settings.js"; +import { useTempHome } from "#/test-utils/temp-home.js"; + +describe("normalizeTerms", () => { + it("trims, collapses spaces, and drops blanks and repeats", () => { + assert.deepEqual(normalizeTerms([" Soniox Console ", "", "웬민", "Soniox Console", 3]), [ + "Soniox Console", + "웬민", + ]); + assert.deepEqual(normalizeTerms("nope"), []); + }); +}); + +describe("terms in settings", () => { + useTempHome(); + + it("round-trips and removes the key when emptied", async () => { + await saveSettings({ languages: ["ja"], terms: ["東京", " earpop "] }); + assert.deepEqual(await loadSettings(), { languages: ["ja"], terms: ["東京", "earpop"] }); + await saveSettings({ terms: [] }); + assert.deepEqual(await loadSettings(), { languages: ["ja"] }); + }); + + it("keeps hand-edited terms past the limit in the file", async () => { + const long = "x".repeat(MAX_TERMS_CHARS - 10); + await saveSettings({ terms: [long, "fits", "does not fit"] }); + await saveSettings({ microphone: "Other mic" }); + const terms = (await loadSettings()).terms!; + assert.equal(terms.length, 3); + assert.deepEqual(fitTerms(terms), [long, "fits"]); + }); +}); diff --git a/src/lib/settings.ts b/src/lib/settings.ts index dc2918c..13002f2 100644 --- a/src/lib/settings.ts +++ b/src/lib/settings.ts @@ -1,12 +1,41 @@ import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises"; import { dirname, join } from "node:path"; -import { normalizeLanguageHints } from "#/lib/languages.js"; +import { normalizeLanguageHints, normalizeTranslation } from "#/lib/languages.js"; import { appDataDir } from "#/lib/paths.js"; import { isRegion } from "#/lib/region.js"; import { createSerialQueue } from "#/lib/serial-queue.js"; import type { CliSettings } from "#/lib/types.js"; +// Soniox caps the whole context near 10,000 characters; leave room for the transcript tail +// and language notes that every session also sends. +export const MAX_TERMS_CHARS = 6_000; + +/** Trims, drops empty and duplicate entries, and keeps the first spelling of each term. */ +export function normalizeTerms(raw: unknown) { + if (!Array.isArray(raw)) return []; + const seen = new Set(); + const terms: string[] = []; + for (const item of raw) { + if (typeof item !== "string") continue; + const term = item.trim().replace(/\s+/g, " "); + if (term === "" || seen.has(term)) continue; + seen.add(term); + terms.push(term); + } + return terms; +} + +export function termsSize(terms: string[]) { + return terms.reduce((total, term) => total + term.length, 0); +} + +/** The leading terms that fit the limit; a hand-edited file can hold more than `terms add` allows. */ +export function fitTerms(terms: string[]) { + let size = 0; + return terms.filter((term) => (size += term.length) <= MAX_TERMS_CHARS); +} + // Serialize concurrent saves so writers do not clobber each other. const enqueueWrite = createSerialQueue(); @@ -29,6 +58,10 @@ export async function loadSettings() { if (isRegion(parsed.region)) settings.region = parsed.region; if (Array.isArray(parsed.languages)) settings.languages = normalizeLanguageHints(parsed.languages); + const terms = normalizeTerms(parsed.terms); + if (terms.length > 0) settings.terms = terms; + const translation = normalizeTranslation(parsed.translation); + if (translation.length > 0) settings.translation = translation; return settings; } catch { return {}; @@ -44,6 +77,16 @@ async function writeThrough(patch: Partial) { if (patch.languages !== undefined) { merged.languages = normalizeLanguageHints(patch.languages); } + if (patch.terms !== undefined) { + const terms = normalizeTerms(patch.terms); + if (terms.length > 0) merged.terms = terms; + else delete merged.terms; + } + if (patch.translation !== undefined) { + const translation = normalizeTranslation(patch.translation); + if (translation.length > 0) merged.translation = translation; + else delete merged.translation; + } const tempPath = `${path}.tmp-${process.pid}-${Math.random().toString(36).slice(2)}`; try { diff --git a/src/lib/soniox.ts b/src/lib/soniox.ts index c906c56..e67f96d 100644 --- a/src/lib/soniox.ts +++ b/src/lib/soniox.ts @@ -4,7 +4,7 @@ import { languageGeneralContext, normalizeLanguageHints } from "#/lib/languages. import { DEFAULT_REGION } from "#/lib/region.js"; import type { SttEvent, SttOptions, SttRegion, SttSession, Token } from "#/lib/types.js"; -export const STT_MODEL = "stt-rt-v5"; +export const STT_MODEL = "stt-rt-v6"; /** Keep a short rolling transcript for Soniox context on reconnect (matches desktop). */ const STT_CONTEXT_CHARS = 1_500; @@ -36,6 +36,8 @@ interface ServerToken { start_ms?: number; end_ms?: number; language?: string; + translation_status?: string; + source_language?: string; } interface ServerResponse { @@ -51,11 +53,19 @@ function toToken(raw: ServerToken) { if (raw.start_ms !== undefined) token.startMs = raw.start_ms; if (raw.end_ms !== undefined) token.endMs = raw.end_ms; if (raw.language !== undefined) token.language = raw.language; + if (raw.translation_status === "translation") { + token.translation = true; + if (raw.source_language !== undefined) token.sourceLanguage = raw.source_language; + } return token; } const END_TOKEN_TEXT = ""; +// Bad request, key, balance, or permission: reconnecting cannot fix these. +// 408, 413 (session length cap), 429, and 5xx are worth a reconnect. +const PERMANENT_ERROR_CODES = new Set([400, 401, 402, 403]); + const REGION_WEBSOCKET_URLS: Record = { us: "wss://stt-rt.soniox.com/transcribe-websocket", eu: "wss://stt-rt.eu.soniox.com/transcribe-websocket", @@ -68,13 +78,19 @@ export function startSession({ languageHints, clientReferenceId, contextText, + terms, + translation, onEvent, }: SttOptions) { - const hints = normalizeLanguageHints(languageHints); + const twoWay = translation?.length === 2 ? translation : undefined; + // Both sides of a two-way translation are spoken, so both belong in the hints. + const hints = normalizeLanguageHints([...(languageHints ?? []), ...(twoWay ?? [])]); return new Promise((resolve, reject) => { + // The start-request api_key field is deprecated; the key travels with the handshake. const socket = new WebSocket(REGION_WEBSOCKET_URLS[region ?? DEFAULT_REGION], { perMessageDeflate: false, handshakeTimeout: CONNECT_TIMEOUT_MS, + headers: { Authorization: `Bearer ${apiKey}` }, }); let settled = false; @@ -97,21 +113,29 @@ export function startSession({ socket.on("open", () => { // Endpoint off: provisional streams fast; finals come later with better word/diarization accuracy. const config = { - api_key: apiKey, model: STT_MODEL, audio_format: "pcm_s16le", sample_rate: 16000, num_channels: 1, language_hints: hints, // Strict is strongest with a single language; multiple hints stay a bias only. - language_hints_strict: hints.length === 1, + language_hints_strict: hints.length === 1 && twoWay === undefined, enable_speaker_diarization: true, + // Tags each token with its language; captions and exports use it to split mixed speech. + enable_language_identification: true, enable_endpoint_detection: false, context: { general: languageGeneralContext(hints), text: contextText || undefined, + terms: terms !== undefined && terms.length > 0 ? terms : undefined, }, client_reference_id: clientReferenceId, + translation: + twoWay !== undefined + ? { type: "two_way", language_a: twoWay[0], language_b: twoWay[1] } + : translation?.length === 1 + ? { type: "one_way", target_language: translation[0] } + : undefined, }; socket.send(JSON.stringify(config)); @@ -177,7 +201,7 @@ export function startSession({ state: "error", message: response.error_message ?? `Transcription server error (code ${response.error_code})`, - retryable: response.error_code !== 401 && response.error_code !== 402, + retryable: !PERMANENT_ERROR_CODES.has(response.error_code), }); return; } diff --git a/src/lib/transcript-formats.test.ts b/src/lib/transcript-formats.test.ts new file mode 100644 index 0000000..6df8421 --- /dev/null +++ b/src/lib/transcript-formats.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { renderTranscript, toCues, toParagraphs } from "#/lib/transcript-formats.js"; +import type { TranscriptToken } from "#/lib/transcripts.js"; + +function token(atMs: number, text: string, extra: Partial = {}): TranscriptToken { + return { text, isFinal: true, atMs, endMs: atMs + 100, speaker: "1", ...extra }; +} + +const transcript = (tokens: TranscriptToken[]) => ({ + startedAt: "2026-11-14T10:00:00+09:00", + model: "stt-rt-v6", + tokens, +}); + +describe("toParagraphs", () => { + it("splits on speaker change and long pauses", () => { + const paragraphs = toParagraphs([ + token(0, "Hi"), + token(200, " there"), + token(400, "Yo", { speaker: "2" }), + token(3_000, " again", { speaker: "2" }), + ]); + assert.deepEqual( + paragraphs.map((paragraph) => [paragraph.speaker, paragraph.text, paragraph.startMs]), + [ + ["1", "Hi there", 0], + ["2", "Yo", 400], + ["2", "again", 3_000], + ], + ); + assert.equal(paragraphs[0]!.endMs, 300); + }); + + it("splits when the language changes", () => { + const paragraphs = toParagraphs([ + token(0, "Hello", { language: "en" }), + token(100, ".", { language: "en" }), + token(200, " ", { language: "ko" }), + token(300, "안녕하세요", { language: "ko" }), + ]); + assert.deepEqual( + paragraphs.map((paragraph) => [paragraph.language, paragraph.text]), + [ + ["en", "Hello."], + ["ko", "안녕하세요"], + ], + ); + }); +}); + +describe("txt", () => { + it("omits language labels for a single-language transcript", () => { + const text = renderTranscript({ + id: "a", + transcript: transcript([token(0, "Hi", { language: "en" })]), + format: "txt", + }); + assert.equal( + text, + [ + "earpop transcript v1", + "id: a", + "started: 2026-11-14T10:00:00+09:00", + "model: stt-rt-v6", + "", + "[00:00] S1: Hi", + "", + ].join("\n"), + ); + }); + + it("labels languages when the transcript mixes them", () => { + const text = renderTranscript({ + id: "b", + transcript: transcript([ + token(0, "Hi", { language: "en" }), + token(61_000, "はい", { language: "ja", speaker: "2" }), + ]), + format: "txt", + }); + assert.match(text, /^languages: en, ja$/m); + assert.match(text, /^\[00:00\] S1 \(en\): Hi$/m); + assert.match(text, /^\[01:01\] S2 \(ja\): はい$/m); + }); +}); + +const conversation = transcript([ + token(0, "Hey", { language: "en" }), + token(200, ",", { language: "en" }), + token(300, " how", { language: "en" }), + token(500, " are", { language: "en" }), + token(700, " you", { language: "en" }), + token(800, "?", { language: "en", endMs: 900 }), + token(1_500, " Hola", { language: "es", speaker: "2" }), + token(1_700, ".", { language: "es", speaker: "2" }), + token(1_900, " Bien", { language: "es", speaker: "2" }), + token(2_100, " <3", { language: "es", speaker: "2", endMs: 2_400 }), +]); + +describe("toCues", () => { + it("splits subtitle cues at sentence ends and keeps short cues readable", () => { + assert.deepEqual( + toCues(conversation.tokens).map((cue) => [cue.speaker, cue.text, cue.startMs, cue.endMs]), + [ + ["1", "Hey, how are you?", 0, 900], + ["2", "Hola.", 1_500, 1_900], + ["2", "Bien <3", 1_900, 2_600], + ], + ); + }); + + it("caps cue length", () => { + const words = Array.from({ length: 40 }, (_, index) => + token(index * 100, index === 0 ? "word" : " word"), + ); + const cues = toCues(words); + assert.ok(cues.length > 1); + assert.ok(cues.every((cue) => cue.text.length <= 84)); + }); + + it("stretches cues that lack end times without overlapping the next cue", () => { + const cues = toCues([ + { text: "One", isFinal: true, atMs: 0, speaker: "1" }, + { text: ".", isFinal: true, atMs: 10, speaker: "1" }, + { text: " Two", isFinal: true, atMs: 400, speaker: "1" }, + ]); + assert.deepEqual( + cues.map((cue) => [cue.startMs, cue.endMs]), + [ + [0, 400], + [400, 1_100], + ], + ); + }); +}); + +describe("subtitle formats", () => { + it("renders SRT", () => { + const srt = renderTranscript({ id: "c", transcript: conversation, format: "srt" }); + assert.equal( + srt.split("\n").slice(0, 8).join("\n"), + [ + "1", + "00:00:00,000 --> 00:00:00,900", + "S1 (en): Hey, how are you?", + "", + "2", + "00:00:01,500 --> 00:00:01,900", + "S2 (es): Hola.", + "", + ].join("\n"), + ); + }); + + it("renders WebVTT with voice and language spans, escaping markup", () => { + const vtt = renderTranscript({ id: "c", transcript: conversation, format: "vtt" }); + assert.ok(vtt.startsWith("WEBVTT\n\n00:00:00.000 --> 00:00:00.900\n")); + assert.match(vtt, /^Hey, how are you\?<\/lang>$/m); + assert.match(vtt, /^Bien <3<\/lang>$/m); + }); + + it("renders Markdown", () => { + const md = renderTranscript({ id: "c", transcript: conversation, format: "md" }); + assert.equal( + md, + [ + "# Transcript c", + "", + "- Started: 2026-11-14T10:00:00+09:00", + "- Model: stt-rt-v6", + "- Languages: en, es", + "", + "**[00:00] S1 (en):** Hey, how are you?", + "", + "**[00:01] S2 (es):** Hola. Bien <3", + "", + ].join("\n"), + ); + }); +}); + +describe("translations", () => { + const translated = (atMs: number, text: string, extra: Partial = {}) => + token(atMs, text, { translation: true, language: "es", sourceLanguage: "en", ...extra }); + const bilingual = transcript([ + token(1_000, "Hey", { language: "en" }), + token(1_200, "?", { language: "en", endMs: 1_500 }), + translated(1_500, "Oye"), + translated(1_500, "?"), + token(4_000, " Bye", { language: "en", endMs: 4_600 }), + translated(4_600, " Adiós"), + ]); + + it("labels translated paragraphs and keeps the speech time", () => { + const text = renderTranscript({ id: "t", transcript: bilingual, format: "txt" }); + assert.deepEqual(text.trimEnd().split("\n").slice(-4), [ + "[00:01] S1 (en): Hey?", + "[00:01] S1 (es translation): Oye?", + "[00:04] S1 (en): Bye", + "[00:04] S1 (es translation): Adiós", + ]); + }); + + it("stacks translation cues on the speech cue in SRT", () => { + const srt = renderTranscript({ id: "t", transcript: bilingual, format: "srt" }); + const blocks = srt.trim().split("\n\n"); + assert.deepEqual(blocks.slice(0, 2), [ + "1\n00:00:01,000 --> 00:00:01,700\nS1 (en): Hey?", + "2\n00:00:01,000 --> 00:00:01,700\nS1 (es translation): Oye?", + ]); + }); + + it("spans a translation over every cue of a long sentence", () => { + const words = Array.from({ length: 30 }, (_, index) => + token(index * 300, index === 0 ? "word" : " word", { language: "en" }), + ); + const cues = toCues([...words, translated(9_000, "palabras")]); + const spoken = cues.filter((cue) => cue.translation !== true); + const translation = cues.find((cue) => cue.translation === true)!; + assert.ok(spoken.length > 1); + assert.equal(translation.startMs, spoken[0]!.startMs); + assert.equal(translation.endMs, spoken.at(-1)!.endMs); + }); +}); diff --git a/src/lib/transcript-formats.ts b/src/lib/transcript-formats.ts new file mode 100644 index 0000000..0fdba9d --- /dev/null +++ b/src/lib/transcript-formats.ts @@ -0,0 +1,215 @@ +import stringWidth from "string-width"; + +import { PAUSE_BREAK_MS, sentenceMark, startsNewWord } from "#/lib/segments.js"; +import type { Transcript, TranscriptToken } from "#/lib/transcripts.js"; + +export const TRANSCRIPT_FORMATS = ["txt", "srt", "vtt", "md"] as const; + +export type TranscriptFormat = (typeof TRANSCRIPT_FORMATS)[number]; + +export function isTranscriptFormat(value: string): value is TranscriptFormat { + return (TRANSCRIPT_FORMATS as readonly string[]).includes(value); +} + +// Subtitle cues: about two lines of 42 columns, readable within a few seconds. +const CUE_MAX_WIDTH = 84; +const CUE_MAX_MS = 7_000; +const CUE_MIN_MS = 700; + +export function formatStamp(ms: number) { + const totalSeconds = Math.floor(ms / 1000); + const ss = String(totalSeconds % 60).padStart(2, "0"); + const minutesTotal = Math.floor(totalSeconds / 60); + if (minutesTotal >= 60) { + const hh = Math.floor(minutesTotal / 60); + return `${hh}:${String(minutesTotal % 60).padStart(2, "0")}:${ss}`; + } + return `${String(minutesTotal).padStart(2, "0")}:${ss}`; +} + +function cueStamp({ ms, separator }: { ms: number; separator: "," | "." }) { + const pad = (value: number, length = 2) => String(value).padStart(length, "0"); + const hours = Math.floor(ms / 3_600_000); + const minutes = Math.floor(ms / 60_000) % 60; + const seconds = Math.floor(ms / 1000) % 60; + return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}${separator}${pad(ms % 1000, 3)}`; +} + +interface Paragraph { + startMs: number; + endMs: number; + speaker?: string; + language?: string; + translation?: boolean; + text: string; +} + +// Translations carry no timestamps: show each over all the speech since the previous +// translation, which may span several cues when a long sentence was split. +function alignTranslations(blocks: Paragraph[]) { + let first: Paragraph | undefined; + let last: Paragraph | undefined; + let untranslated = true; + for (const block of blocks) { + if (block.translation !== true) { + if (!untranslated) first = undefined; + untranslated = true; + first ??= block; + last = block; + } else if (first !== undefined && last !== undefined) { + untranslated = false; + block.startMs = first.startMs; + block.endMs = last.endMs; + } + } + return blocks; +} + +/** + * Groups tokens into blocks that share a speaker, language, and translation status, split at + * long pauses. Cues are also split at sentence ends and subtitle length limits. + */ +function groupTokens({ tokens, cues }: { tokens: TranscriptToken[]; cues: boolean }) { + const blocks: Paragraph[] = []; + let lastAtMs = 0; + for (const token of tokens) { + const previous = blocks.at(-1); + if (token.text.trim() === "") { + if (previous !== undefined) previous.text += token.text; + continue; + } + const spoken = token.translation !== true; + if ( + previous === undefined || + token.speaker !== previous.speaker || + spoken === (previous.translation === true) || + (spoken && token.atMs - lastAtMs > PAUSE_BREAK_MS) || + (token.language !== undefined && + previous.language !== undefined && + token.language !== previous.language) || + (cues && + startsNewWord(token.text) && + (sentenceMark(previous.text) !== null || + stringWidth(previous.text.trim()) + stringWidth(token.text) > CUE_MAX_WIDTH || + (spoken && token.atMs - previous.startMs > CUE_MAX_MS))) + ) { + blocks.push({ + startMs: token.atMs, + endMs: token.endMs ?? token.atMs, + speaker: token.speaker, + language: token.language, + ...(spoken ? {} : { translation: true }), + text: token.text, + }); + } else { + previous.text += token.text; + previous.endMs = Math.max(previous.endMs, token.endMs ?? token.atMs); + } + if (spoken) lastAtMs = token.atMs; + } + for (const block of blocks) block.text = block.text.trim(); + return blocks; +} + +/** Groups tokens by speaker, language, and pauses longer than two seconds. */ +export function toParagraphs(tokens: TranscriptToken[]) { + return alignTranslations(groupTokens({ tokens, cues: false })); +} + +/** Subtitle-sized blocks: paragraphs further split at sentence ends and length limits. */ +export function toCues(tokens: TranscriptToken[]) { + const cues = groupTokens({ tokens, cues: true }); + // Tokens without end times would give zero-length cues; stretch them without overlapping. + const spoken = cues.filter((cue) => cue.translation !== true); + spoken.forEach((cue, index) => { + const next = spoken[index + 1]; + const minimum = cue.startMs + CUE_MIN_MS; + if (cue.endMs < minimum) { + cue.endMs = + next === undefined ? minimum : Math.max(cue.endMs, Math.min(minimum, next.startMs)); + } + }); + return alignTranslations(cues); +} + +// Language labels only add noise when the whole transcript is in one language. +function labelLanguages(blocks: Paragraph[]) { + const languages = [ + ...new Set(blocks.flatMap((block) => (block.language !== undefined ? [block.language] : []))), + ]; + const show = languages.length > 1 || blocks.some((block) => block.translation === true); + return { languages, show }; +} + +function speakerLabel({ block, showLanguage }: { block: Paragraph; showLanguage: boolean }) { + const speaker = block.speaker !== undefined ? `S${block.speaker}` : "S?"; + if (block.translation === true) return `${speaker} (${block.language ?? "?"} translation)`; + return showLanguage && block.language !== undefined ? `${speaker} (${block.language})` : speaker; +} + +function renderParagraphs({ + id, + transcript, + markdown, +}: { + id: string; + transcript: Transcript; + markdown: boolean; +}) { + const paragraphs = toParagraphs(transcript.tokens); + const { languages, show } = labelLanguages(paragraphs); + const facts: Array<[string, string]> = []; + if (transcript.startedAt !== "") facts.push(["started", transcript.startedAt]); + if (transcript.model !== "") facts.push(["model", transcript.model]); + if (show) facts.push(["languages", languages.join(", ")]); + + const lines = paragraphs.map((paragraph) => { + const label = `[${formatStamp(paragraph.startMs)}] ${speakerLabel({ block: paragraph, showLanguage: show })}:`; + return markdown ? `**${label}** ${paragraph.text}` : `${label} ${paragraph.text}`; + }); + + if (!markdown) { + const head = [ + "earpop transcript v1", + `id: ${id}`, + ...facts.map(([key, value]) => `${key}: ${value}`), + ]; + return [...head, "", ...lines].join("\n") + "\n"; + } + const head = [`# Transcript ${id}`, ""]; + if (facts.length > 0) { + const title = (key: string) => key.charAt(0).toUpperCase() + key.slice(1); + head.push(...facts.map(([key, value]) => `- ${title(key)}: ${value}`), ""); + } + return [...head, lines.join("\n\n")].join("\n").trimEnd() + "\n"; +} + +function renderCues({ transcript, vtt }: { transcript: Transcript; vtt: boolean }) { + const cues = toCues(transcript.tokens); + const { show } = labelLanguages(cues); + const separator = vtt ? "." : ","; + const blocks = cues.map((cue, index) => { + const timing = `${cueStamp({ ms: cue.startMs, separator })} --> ${cueStamp({ ms: cue.endMs, separator })}`; + const label = speakerLabel({ block: cue, showLanguage: show }); + if (!vtt) return [String(index + 1), timing, `${label}: ${cue.text}`, ""].join("\n"); + // `` names the speaker; `` lets players pick fonts and direction per language. + const text = cue.text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); + const spoken = cue.language !== undefined ? `${text}` : text; + return [timing, `${spoken}`, ""].join("\n"); + }); + return vtt ? ["WEBVTT", "", ...blocks].join("\n") : blocks.join("\n"); +} + +export function renderTranscript({ + id, + transcript, + format, +}: { + id: string; + transcript: Transcript; + format: TranscriptFormat; +}) { + if (format === "srt" || format === "vtt") + return renderCues({ transcript, vtt: format === "vtt" }); + return renderParagraphs({ id, transcript, markdown: format === "md" }); +} diff --git a/src/lib/transcription.ts b/src/lib/transcription.ts index 92a426b..47fd814 100644 --- a/src/lib/transcription.ts +++ b/src/lib/transcription.ts @@ -1,6 +1,13 @@ import { startCapture } from "#/lib/audio.js"; import { errorMessage } from "#/lib/errors.js"; -import { languageHintLabel, normalizeLanguageHints, type SttLanguage } from "#/lib/languages.js"; +import { + languageHintLabel, + NO_SPACE_SCRIPT, + normalizeLanguageHints, + normalizeTranslation, + translationLabel, + type SttLanguage, +} from "#/lib/languages.js"; import { createSerialQueue } from "#/lib/serial-queue.js"; import { saveSettings } from "#/lib/settings.js"; import { appendSttContext, startSession } from "#/lib/soniox.js"; @@ -16,8 +23,6 @@ const BACKOFF_MAX_MS = 30_000; const BACKOFF_STABLE_MS = 30_000; const NOTICE_MS = 4_000; const MIC_LOST = "Press m to choose another microphone"; -const NO_SPACE_SCRIPT = - /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}]/u; type LiveState = Exclude; @@ -26,6 +31,8 @@ export interface TranscriptionSnapshot { stateMessage?: string; notice?: string; device: string; + languages: SttLanguage[]; + translation: SttLanguage[]; finalTokens: Token[]; pendingTokens: Token[]; elapsedSeconds: number; @@ -38,6 +45,8 @@ interface TranscriptionOptions { clientReferenceId: string; region?: SttRegion; languageHints?: SttLanguage[]; + terms?: string[]; + translation?: SttLanguage[]; onSnapshot: (snapshot: TranscriptionSnapshot) => void; onFatal: (message: string) => void; } @@ -51,11 +60,14 @@ export function createTranscription({ clientReferenceId, region, languageHints: languageHintsOption, + terms, + translation: translationOption, onSnapshot, onFatal, }: TranscriptionOptions) { let device = deviceOption ?? "default"; let languageHints = normalizeLanguageHints(languageHintsOption); + let translation = normalizeTranslation(translationOption); let capture: AudioCapture | null = null; let session: SttSession | null = null; @@ -69,7 +81,7 @@ export function createTranscription({ let pausedAtMs = 0; let pausedTotalMs = 0; - // Serialize togglePause/switchMicrophone/setLanguageHints/stop — overlapping ops can open capture atop teardown. + // Serialize togglePause/switchMicrophone/setLanguageHints/setTranslation/stop — overlapping ops can open capture atop teardown. const enqueue = createSerialQueue(); let state: LiveState = "connecting"; @@ -98,6 +110,8 @@ export function createTranscription({ stateMessage, notice, device, + languages: languageHints, + translation, finalTokens, pendingTokens, elapsedSeconds: @@ -215,6 +229,8 @@ export function createTranscription({ region, languageHints, contextText: speechContext, + terms, + translation, onEvent: (event) => { if (stopped) return; if (event.type === "tokens") { @@ -223,7 +239,11 @@ export function createTranscription({ sessionHasFinal = true; journal.tokens({ tokens: final, offsetMs }); addFinals(final); - speechContext = appendSttContext({ text: speechContext, tokens: final }); + // Only speech biases recognition; translated text would skew it toward the target. + speechContext = appendSttContext({ + text: speechContext, + tokens: final.filter((token) => token.translation !== true), + }); } // A replaced session still delivers its last finals; only the current one owns the provisional line. if (generation === connectGeneration) { @@ -320,6 +340,23 @@ export function createTranscription({ capture = null; } + // Session options changed: open a replacement session, keeping the microphone running. + async function restartSession() { + if (paused) return; + if (reconnectTimer !== null) { + clearTimeout(reconnectTimer); + reconnectTimer = null; + } + // Drop provisional captions from the previous session. + pendingTokens = []; + // Invalidate any in-flight connect() before opening a replacement session. + connectGeneration += 1; + connecting = false; + releaseSession(); + emit(true); + await connect(); + } + async function teardownIO() { await stopCapture(); releaseSession(); @@ -415,25 +452,26 @@ export function createTranscription({ return enqueue(async () => { if (stopped) return; const next = normalizeLanguageHints(codes); - if (sameLanguageHints(languageHints, next)) return; + if (sameCodes(languageHints, next)) return; - languageHints = next; + // Save first: a failed save leaves the running session and snapshot unchanged. await saveSettings({ languages: next }); + languageHints = next; flash(`Languages set to ${languageHintLabel(next)}`); - if (paused) return; + await restartSession(); + }); + }, - if (reconnectTimer !== null) { - clearTimeout(reconnectTimer); - reconnectTimer = null; - } - // Drop provisional captions from the previous language session. - pendingTokens = []; - // Invalidate any in-flight connect() before opening a replacement session. - connectGeneration += 1; - connecting = false; - releaseSession(); - emit(true); - await connect(); + setTranslation(codes: SttLanguage[]) { + return enqueue(async () => { + if (stopped) return; + const next = normalizeTranslation(codes); + if (sameCodes(translation, next)) return; + + await saveSettings({ translation: next }); + translation = next; + flash(`Translation: ${translationLabel(next)}`); + await restartSession(); }); }, @@ -453,7 +491,7 @@ export function createTranscription({ }; } -function sameLanguageHints(a: SttLanguage[], b: SttLanguage[]) { +function sameCodes(a: SttLanguage[], b: SttLanguage[]) { if (a.length !== b.length) return false; const other = new Set(b); return a.every((code) => other.has(code)); diff --git a/src/lib/transcripts.test.ts b/src/lib/transcripts.test.ts new file mode 100644 index 0000000..401283e --- /dev/null +++ b/src/lib/transcripts.test.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict"; +import { mkdirSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { before, describe, it } from "node:test"; + +import { transcriptsDir } from "#/lib/journal.js"; +import { listTranscripts, resolveTranscriptId } from "#/lib/transcripts.js"; +import { useTempHome } from "#/test-utils/temp-home.js"; + +describe("listTranscripts", () => { + useTempHome(); + + before(() => { + mkdirSync(transcriptsDir(), { recursive: true }); + const meeting = (startedAt: string) => + JSON.stringify({ kind: "meeting", started_at: startedAt, model: "stt-rt-v6" }); + const tokens = Array.from({ length: 2_000 }, (_, index) => + JSON.stringify({ + kind: "token", + at_ms: index * 100, + end_ms: index * 100 + 80, + text: " word", + }), + ); + writeFileSync( + join(transcriptsDir(), "2026-11-14_10-00-00.jsonl"), + [meeting("2026-11-14T10:00:00+09:00"), ...tokens, '{"kind":"ended","at_ms":200500}', ""].join( + "\n", + ), + ); + // A crashed session has no ended line; the last token still gives the length. + writeFileSync( + join(transcriptsDir(), "2026-11-15_09-00-00.jsonl"), + [meeting("2026-11-15T09:00:00+09:00"), ...tokens.slice(0, 650), ""].join("\n"), + ); + writeFileSync(join(transcriptsDir(), "notes.txt"), "ignored"); + }); + + it("lists newest first with the recorded length", async () => { + assert.deepEqual(await listTranscripts(), [ + { id: "2026-11-15_09-00-00", startedAt: "2026-11-15T09:00:00+09:00", duration: "01:04" }, + { id: "2026-11-14_10-00-00", startedAt: "2026-11-14T10:00:00+09:00", duration: "03:20" }, + ]); + }); + + it("resolves latest to the newest transcript", async () => { + assert.equal(await resolveTranscriptId("latest"), "2026-11-15_09-00-00"); + assert.equal(await resolveTranscriptId("other"), "other"); + }); +}); diff --git a/src/lib/transcripts.ts b/src/lib/transcripts.ts index 80ada1d..92f89b7 100644 --- a/src/lib/transcripts.ts +++ b/src/lib/transcripts.ts @@ -1,18 +1,19 @@ -import { readFile, readdir } from "node:fs/promises"; +import { open, readFile, readdir } from "node:fs/promises"; +import { join } from "node:path"; + import { transcriptPath, transcriptsDir } from "#/lib/journal.js"; +import { formatStamp } from "#/lib/transcript-formats.js"; import type { Token } from "#/lib/types.js"; -const PARAGRAPH_GAP_MS = 2_000; - interface TranscriptSummary { id: string; startedAt: string; - tokenCount: number; + duration: string; } -type TranscriptToken = Token & { atMs: number }; +export type TranscriptToken = Token & { atMs: number }; -interface Transcript { +export interface Transcript { startedAt: string; model: string; tokens: TranscriptToken[]; @@ -25,6 +26,8 @@ interface ParsedLine { text?: unknown; speaker?: unknown; language?: unknown; + translation?: unknown; + source_language?: unknown; started_at?: unknown; model?: unknown; } @@ -57,40 +60,69 @@ async function readLines(id: string) { .filter((line): line is ParsedLine => line !== null); } -export async function listTranscripts() { - const dir = transcriptsDir(); +// Listing reads only the meeting header and the last lines, so long transcripts stay cheap. +const EDGE_BYTES = 4_096; + +async function readEdges(path: string) { + const handle = await open(path, "r"); + try { + const { size } = await handle.stat(); + const head = Buffer.alloc(Math.min(size, EDGE_BYTES)); + await handle.read(head, 0, head.length, 0); + const tailStart = Math.max(0, size - EDGE_BYTES); + const tail = Buffer.alloc(size - tailStart); + await handle.read(tail, 0, tail.length, tailStart); + const tailLines = tail.toString("utf8").split("\n"); + // The first tail line may start mid-record. + if (tailStart > 0) tailLines.shift(); + return { head: head.toString("utf8").split("\n", 1)[0] ?? "", tailLines }; + } finally { + await handle.close(); + } +} + +async function transcriptFiles() { let names: string[]; try { - names = await readdir(dir); + names = await readdir(transcriptsDir()); } catch { return []; } + return names + .filter((name) => name.endsWith(".jsonl")) + .map((name) => ({ id: name.slice(0, -".jsonl".length), path: join(transcriptsDir(), name) })); +} +export async function listTranscripts() { const results = await Promise.all( - names - .filter((name) => name.endsWith(".jsonl")) - .map(async (name): Promise => { - const id = name.slice(0, -".jsonl".length); - try { - const lines = await readLines(id); - const meeting = lines.find((line) => line.kind === "meeting"); - return { - id, - startedAt: - typeof meeting?.started_at === "string" ? meeting.started_at : "(no timestamp)", - tokenCount: lines.filter((line) => line.kind === "token").length, - }; - } catch { - return null; + (await transcriptFiles()).map(async ({ id, path }): Promise => { + try { + const { head, tailLines } = await readEdges(path); + const meeting = parseLine(head); + let lastAtMs = 0; + for (const line of tailLines.map(parseLine)) { + if (typeof line?.at_ms === "number") lastAtMs = Math.max(lastAtMs, line.at_ms); + if (typeof line?.end_ms === "number") lastAtMs = Math.max(lastAtMs, line.end_ms); } - }), + return { + id, + startedAt: + meeting?.kind === "meeting" && typeof meeting.started_at === "string" + ? meeting.started_at + : "(no timestamp)", + duration: formatStamp(lastAtMs), + }; + } catch { + return null; + } + }), ); const summaries = results.filter((summary) => summary !== null); summaries.sort((a, b) => b.id.localeCompare(a.id)); return summaries; } -async function readTranscript(id: string) { +export async function readTranscript(id: string) { assertValidId(id); const lines = await readLines(id); const meeting = lines.find((line) => line.kind === "meeting"); @@ -103,6 +135,10 @@ async function readTranscript(id: string) { if (typeof line.speaker === "string") token.speaker = line.speaker; if (typeof line.language === "string") token.language = line.language; if (typeof line.end_ms === "number") token.endMs = line.end_ms; + if (line.translation === true) { + token.translation = true; + if (typeof line.source_language === "string") token.sourceLanguage = line.source_language; + } tokens.push(token); } @@ -113,64 +149,13 @@ async function readTranscript(id: string) { } satisfies Transcript; } -function formatStamp(ms: number) { - const totalSeconds = Math.floor(ms / 1000); - const ss = String(totalSeconds % 60).padStart(2, "0"); - const minutesTotal = Math.floor(totalSeconds / 60); - if (minutesTotal >= 60) { - const hh = Math.floor(minutesTotal / 60); - return `${hh}:${String(minutesTotal % 60).padStart(2, "0")}:${ss}`; - } - return `${String(minutesTotal).padStart(2, "0")}:${ss}`; -} - -export async function exportText(id: string) { - const transcript = await readTranscript(id); - - interface Paragraph { - startMs: number; - lastAtMs: number; - speaker?: string; - text: string; - } - - const paragraphs: Paragraph[] = []; - for (const token of transcript.tokens) { - const previous = paragraphs.at(-1); - if (token.text.trim() === "") { - if (previous !== undefined) previous.text += token.text; - continue; - } - if ( - previous === undefined || - token.speaker !== previous.speaker || - token.atMs - previous.lastAtMs > PARAGRAPH_GAP_MS - ) { - paragraphs.push({ - startMs: token.atMs, - lastAtMs: token.atMs, - speaker: token.speaker, - text: token.text, - }); - continue; - } - previous.text += token.text; - previous.lastAtMs = token.atMs; - } - - const head: string[] = ["earpop transcript v1", `id: ${id}`]; - if (transcript.startedAt !== "") { - head.push(`started: ${transcript.startedAt}`); - } - if (transcript.model !== "") { - head.push(`model: ${transcript.model}`); - } - head.push(""); - - const body = paragraphs.map((paragraph) => { - const speakerTag = paragraph.speaker !== undefined ? `S${paragraph.speaker}` : "S?"; - return `[${formatStamp(paragraph.startMs)}] ${speakerTag}: ${paragraph.text.trim()}`; - }); - - return [...head, ...body].join("\n") + "\n"; +/** Resolves `latest` to the newest transcript ID; IDs are start times, so the largest is newest. */ +export async function resolveTranscriptId(id: string) { + if (id !== "latest") return id; + const newest = (await transcriptFiles()) + .map((file) => file.id) + .sort() + .at(-1); + if (newest === undefined) throw new Error("No saved transcripts."); + return newest; } diff --git a/src/lib/types.ts b/src/lib/types.ts index 0cf8975..406dc0a 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -10,6 +10,10 @@ export interface Token { startMs?: number; endMs?: number; language?: string; + /** Set on translated text; translations carry no timestamps. */ + translation?: boolean; + /** Language the translation was made from. */ + sourceLanguage?: string; } export type SttState = "connecting" | "listening" | "paused" | "stopped" | "error"; @@ -30,6 +34,10 @@ export interface SttOptions { clientReferenceId?: string; /** Recent final transcript tail; biases recognition on reconnect. */ contextText?: string; + /** Words and names to spell as listed (Soniox context.terms). */ + terms?: string[]; + /** One language: translate everything into it. Two: translate between them. */ + translation?: SttLanguage[]; onEvent: (event: SttEvent) => void; } @@ -62,4 +70,6 @@ export interface CliSettings { microphone?: string; region?: SttRegion; languages?: SttLanguage[]; + terms?: string[]; + translation?: SttLanguage[]; } diff --git a/src/screens/caption.tsx b/src/screens/caption.tsx index cc46d64..8ad9301 100644 --- a/src/screens/caption.tsx +++ b/src/screens/caption.tsx @@ -1,9 +1,9 @@ import { Spinner } from "@inkjs/ui"; import { Text, useBoxMetrics, useInput, useWindowSize, Box, type DOMElement } from "ink"; import { useEffect, useMemo, useRef, useState } from "react"; -import stringWidth from "string-width"; import { MicrophoneSelect } from "#/components/microphone-select.js"; +import { toLines, wrapUtterances, type Piece } from "#/lib/caption-layout.js"; import { shortPath } from "#/lib/clipboard.js"; import type { SttLanguage } from "#/lib/languages.js"; import type { Microphone, SttRegion, SttState, Token } from "#/lib/types.js"; @@ -19,6 +19,7 @@ export type CaptionOverlay = | "currentMicrophone" | "region" | "languages" + | "translation" | "hasApiKey" | "notice" >); @@ -28,6 +29,8 @@ interface CaptionScreenProps { stateMessage?: string; notice?: string; device: string; + languages: SttLanguage[]; + translation: SttLanguage[]; finalTokens: Token[]; pendingTokens: Token[]; elapsedSeconds: number; @@ -44,6 +47,7 @@ interface CaptionScreenProps { onCloseOverlay: () => void; onSettingsPickMicrophone: (name: string) => void; onSettingsPickLanguages: (codes: SttLanguage[]) => void; + onSettingsPickTranslation: (codes: SttLanguage[]) => void; onSettingsPickRegion: (region: SttRegion) => void; onSettingsChangeApiKey: (key: string) => void; onSettingsDeleteApiKey: () => void; @@ -53,46 +57,6 @@ const CAPTION_LINE_COUNT = 6; const END_CONFIRM_MS = 2_000; const CAPTION_TAIL_CHARS = 1200; -/** Fixed palette for speaker tags S1–S9 (Ink named colors). */ -const SPEAKER_COLORS = [ - "cyan", - "yellow", - "magenta", - "green", - "blue", - "red", - "white", - "cyanBright", - "yellowBright", -] as const; - -type SpeakerColor = (typeof SPEAKER_COLORS)[number]; - -interface Utterance { - speaker?: string; - stable: string; - pending: string; -} - -interface Piece { - text: string; - dim: boolean; - color?: SpeakerColor; - tag?: boolean; -} - -function speakerColor(speaker: string) { - const index = Number(speaker); - if (!Number.isInteger(index) || index < 1 || index > 9) return undefined; - return SPEAKER_COLORS[index - 1]; -} - -const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" }); - -function graphemes(text: string) { - return Array.from(graphemeSegmenter.segment(text), (part) => part.segment); -} - function sortSpeakerIds(ids: Iterable) { return [...ids].sort((a, b) => Number(a) - Number(b)); } @@ -107,237 +71,19 @@ function heardSpeakerIds(tokenLists: Token[][]) { return sortSpeakerIds(heard); } -function formatSpeakerIds(ids: string[]) { - return ids.map((id) => `S${id}`).join(", "); -} - -function endsSentence(text: string) { - const trimmed = text.trimEnd(); - if (trimmed === "") return false; - - const last = trimmed.at(-1) ?? ""; - if ("?!。?!".includes(last)) return true; - - // "1." / "Dr." stay on this line; a period only ends a sentence when the token has trailing space. - return last === "." && text !== trimmed; -} - -function toLines({ - finalTokens, - pendingTokens, - focusedSpeakers, - tailChars, -}: { - finalTokens: Token[]; - pendingTokens: Token[]; - focusedSpeakers: ReadonlySet; - tailChars: number; -}) { - const lines: Utterance[] = []; - let speaker: string | undefined; - let stable = ""; - let tail = ""; - let closed = false; - - const push = () => { - if (stable.trim() === "" && tail.trim() === "") return; - lines.push({ speaker, stable, pending: tail }); - stable = ""; - tail = ""; - closed = false; - }; - - const eat = ({ token, pending }: { token: Token; pending: boolean }) => { - if ( - focusedSpeakers.size > 0 && - (token.speaker === undefined || !focusedSpeakers.has(token.speaker)) - ) { - return; - } - const next = token.speaker; - if (closed || (next !== undefined && next !== speaker)) push(); - if (next !== undefined) speaker = next; - if (pending) tail += token.text; - else stable += token.text; - closed = endsSentence(token.text); - }; - - let start = finalTokens.length; - for (let used = 0; start > 0 && used < tailChars; start -= 1) { - used += finalTokens[start - 1]!.text.length; - } - for (let i = start; i < finalTokens.length; i += 1) { - eat({ token: finalTokens[i]!, pending: false }); - } - for (const token of pendingTokens) { - eat({ token, pending: true }); - } - push(); - return lines; -} - -function wrapFlow({ pieces, width, indent }: { pieces: Piece[]; width: number; indent: number }) { - const lines: Piece[][] = []; - let line: Piece[] = []; - let lineWidth = 0; - - const padOnly = () => indent > 0 && line.length === 1 && line[0]!.text.trim() === ""; - - const newLine = (continuation: boolean) => { - if (line.length > 0 && !padOnly()) lines.push(line); - line = []; - lineWidth = 0; - if (continuation && indent > 0) { - line.push({ text: " ".repeat(indent), dim: false }); - lineWidth = indent; - } - }; - - const appendPiece = ({ piece, pieceWidth }: { piece: Piece; pieceWidth: number }) => { - line.push(piece); - lineWidth += pieceWidth; - }; - - const appendText = ({ - text, - textWidth, - piece, - }: { - text: string; - textWidth: number; - piece: Piece; - }) => { - if (text.length > 0) { - appendPiece({ piece: { text, dim: piece.dim, color: piece.color }, pieceWidth: textWidth }); - } - }; - - const atLineStart = () => { - const last = line.at(-1); - return lineWidth === 0 || padOnly() || last?.tag === true; - }; - - for (const piece of pieces) { - if (piece.tag === true) { - appendPiece({ piece, pieceWidth: stringWidth(piece.text) }); - continue; - } - - const units = piece.text.match(/\s+|\S+/g) ?? []; - for (const unit of units) { - if (/^\s+$/.test(unit) && atLineStart()) continue; - - const unitWidth = stringWidth(unit); - - if (unitWidth > width) { - newLine(true); - let chunk = ""; - let chunkWidth = 0; - for (const char of graphemes(unit)) { - const charWidthValue = stringWidth(char); - if (lineWidth + chunkWidth + charWidthValue > width) { - appendText({ text: chunk, textWidth: chunkWidth, piece }); - newLine(true); - chunk = ""; - chunkWidth = 0; - } - chunk += char; - chunkWidth += charWidthValue; - } - appendText({ text: chunk, textWidth: chunkWidth, piece }); - continue; - } - - if (lineWidth + unitWidth > width) { - newLine(true); - if (/^\s+$/.test(unit)) continue; - } - appendText({ text: unit, textWidth: unitWidth, piece }); +// In order of first appearance. +function heardLanguages(tokenLists: Token[][]) { + const heard = new Set(); + for (const tokens of tokenLists) { + for (const token of tokens) { + if (token.language !== undefined && token.text.trim() !== "") heard.add(token.language); } } - newLine(false); - return lines; + return [...heard]; } -function wrapUtterances({ - utterances, - width, - maxLines, -}: { - utterances: Utterance[]; - width: number; - maxLines: number; -}) { - const chunks: { pieces: Piece[]; tag?: Piece }[][] = []; - let rowCount = 0; - - for (let u = utterances.length - 1; u >= 0 && rowCount < maxLines; u -= 1) { - const utterance = utterances[u]!; - const pieces: Piece[] = []; - let tag: Piece | undefined; - let indent = 0; - if (utterance.speaker !== undefined) { - tag = { - text: `[S${utterance.speaker}] `, - dim: false, - tag: true, - color: speakerColor(utterance.speaker), - }; - indent = stringWidth(tag.text); - pieces.push(tag); - } - const stable = utterance.stable.trimStart(); - const pending = stable.length === 0 ? utterance.pending.trimStart() : utterance.pending; - if (stable.length > 0) pieces.push({ text: stable, dim: false }); - if (pending.length > 0) pieces.push({ text: pending, dim: true }); - - const chunk = wrapFlow({ pieces, width, indent }).map((wrapped) => ({ pieces: wrapped, tag })); - chunks.push(chunk); - rowCount += chunk.length; - } - - const kept = chunks.reverse().flat().slice(-maxLines); - if (kept.length === 0) return []; - - const first = kept[0]!; - if (first.tag === undefined || first.pieces[0]?.tag === true) { - return kept.map((row) => row.pieces); - } - - const tagWidth = stringWidth(first.tag.text); - const bodyWidth = width - tagWidth; - if (bodyWidth <= 0) return [[first.tag], ...kept.slice(1).map((row) => row.pieces)]; - - const source = - first.pieces[0] !== undefined && /^\s+$/.test(first.pieces[0].text) - ? first.pieces.slice(1) - : first.pieces; - const body: Piece[] = []; - let remaining = bodyWidth; - for (let i = source.length - 1; i >= 0 && remaining > 0; i -= 1) { - const piece = source[i]!; - const pieceWidth = stringWidth(piece.text); - if (pieceWidth <= remaining) { - body.unshift(piece); - remaining -= pieceWidth; - continue; - } - const chars = graphemes(piece.text); - let start = chars.length; - let used = 0; - while (start > 0) { - const cw = stringWidth(chars[start - 1]!); - if (used + cw > remaining) break; - used += cw; - start -= 1; - } - if (start < chars.length) { - body.unshift({ ...piece, text: chars.slice(start).join("") }); - } - remaining = 0; - } - - return [[first.tag, ...body], ...kept.slice(1).map((row) => row.pieces)]; +function formatSpeakerIds(ids: string[]) { + return ids.map((id) => `S${id}`).join(", "); } function formatElapsed(seconds: number) { @@ -352,24 +98,28 @@ function StatusLine(props: { notice?: string; device: string; elapsedSeconds: number; - focusedSpeakers: string[]; + focus: string[]; }) { return ( - - {props.focusedSpeakers.length > 0 && ( - {` focus: ${formatSpeakerIds(props.focusedSpeakers)}`} - )} + {/* Error text can be long, so only it may shrink; short labels stay whole. */} + + + + {props.focus.length > 0 && {` focus: ${props.focus.join(", ")}`}} {props.notice !== undefined && {` ${props.notice}`}} - + {/* Shrinks far faster than the state side, so a long device name truncates first. */} + {props.device !== "default" && ( {props.device} )} - {formatElapsed(props.elapsedSeconds)} + + {formatElapsed(props.elapsedSeconds)} + ); @@ -409,7 +159,7 @@ function CaptionRow(props: { line: Piece[] }) { return ( {props.line.map((piece, index) => ( - + {piece.text} ))} @@ -422,6 +172,12 @@ export function CaptionScreen(props: CaptionScreenProps) { const overlayOpen = props.overlay.kind !== "none"; // Empty set = show all speakers; 1–9 toggles membership, 0 clears. const [focusedSpeakers, setFocusedSpeakers] = useState(() => new Set()); + // undefined = every language; l cycles through the languages heard so far. + const [languageFocus, setFocusedLanguage] = useState(); + // Translation always mixes languages on screen. + const multilingual = props.languages.length > 1 || props.translation.length > 0; + // Switching to a single language hides the l key, so drop a leftover language filter too. + const focusedLanguage = multilingual ? languageFocus : undefined; // null = live view; otherwise finals frozen on entry, scrolled `offset` rows up from the bottom. const [history, setHistory] = useState<{ tokens: Token[]; offset: number } | null>(null); const [confirmingEnd, setConfirmingEnd] = useState(false); @@ -437,13 +193,26 @@ export function CaptionScreen(props: CaptionScreenProps) { finalTokens: props.finalTokens, pendingTokens: props.pendingTokens, focusedSpeakers, + focusedLanguage, tailChars: CAPTION_TAIL_CHARS, }); return { utterances, - captionLines: wrapUtterances({ utterances, width: columns, maxLines: CAPTION_LINE_COUNT }), + captionLines: wrapUtterances({ + utterances, + width: columns, + maxLines: CAPTION_LINE_COUNT, + showLanguage: multilingual, + }), }; - }, [props.finalTokens, props.pendingTokens, focusedSpeakers, columns]); + }, [ + props.finalTokens, + props.pendingTokens, + focusedSpeakers, + focusedLanguage, + multilingual, + columns, + ]); const historyTokens = history?.tokens; const historyRows = useMemo(() => { @@ -452,10 +221,16 @@ export function CaptionScreen(props: CaptionScreenProps) { finalTokens: historyTokens, pendingTokens: [], focusedSpeakers, + focusedLanguage, tailChars: Infinity, }); - return wrapUtterances({ utterances, width: columns, maxLines: Infinity }); - }, [historyTokens, focusedSpeakers, columns]); + return wrapUtterances({ + utterances, + width: columns, + maxLines: Infinity, + showLanguage: multilingual, + }); + }, [historyTokens, focusedSpeakers, focusedLanguage, multilingual, columns]); const showPath = props.journalPath !== undefined && (props.ended || props.state === "paused"); const historyBoxRef = useRef(null); @@ -500,6 +275,14 @@ export function CaptionScreen(props: CaptionScreenProps) { if (input === "0") { setFocusedSpeakers(new Set()); + setFocusedLanguage(undefined); + return; + } + if (input === "l" && multilingual) { + const heard = heardLanguages([props.getHistory(), props.pendingTokens]); + const cycle = heard.length > 0 ? heard : props.languages; + const index = focusedLanguage === undefined ? -1 : cycle.indexOf(focusedLanguage); + setFocusedLanguage(cycle[index + 1]); return; } if (/^[1-9]$/.test(input)) { @@ -526,8 +309,15 @@ export function CaptionScreen(props: CaptionScreenProps) { }); const focusedSpeakerIds = sortSpeakerIds(focusedSpeakers); - const focusEmpty = focusedSpeakers.size > 0 && utterances.length === 0 && !props.ended; - const heardIds = focusEmpty ? heardSpeakerIds([props.finalTokens, props.pendingTokens]) : []; + const focus = [ + ...focusedSpeakerIds.map((id) => `S${id}`), + ...(focusedLanguage !== undefined ? [focusedLanguage] : []), + ]; + const focusEmpty = focus.length > 0 && utterances.length === 0 && !props.ended; + const heardIds = + focusEmpty && focusedSpeakers.size > 0 + ? heardSpeakerIds([props.finalTokens, props.pendingTokens]) + : []; const hints = history !== null ? `History ${top + 1}-${Math.min(top + historyHeight, historyRows.length)}/${historyRows.length} Up/Down PgUp/PgDn scroll End live` @@ -537,7 +327,7 @@ export function CaptionScreen(props: CaptionScreenProps) { ? "Up history ESC quit" : confirmingEnd ? "Press ESC again to end the session" - : `p ${props.state === "paused" ? "resume" : "pause"} 1-9 speaker 0 all Up history m mic s settings ESC end`; + : `p ${props.state === "paused" ? "resume" : "pause"} 1-9 speaker ${multilingual ? "l language " : ""}0 all Up history m mic s settings ESC end`; return ( @@ -556,6 +346,7 @@ export function CaptionScreen(props: CaptionScreenProps) { exitLabel="ESC back" onPickMicrophone={props.onSettingsPickMicrophone} onPickLanguages={props.onSettingsPickLanguages} + onPickTranslation={props.onSettingsPickTranslation} onPickRegion={props.onSettingsPickRegion} onChangeApiKey={props.onSettingsChangeApiKey} onDeleteApiKey={props.onSettingsDeleteApiKey} @@ -578,7 +369,7 @@ export function CaptionScreen(props: CaptionScreenProps) { {focusEmpty ? ( - {`No speech from ${formatSpeakerIds(focusedSpeakerIds)}${ + {`No speech from ${focus.join(", ")}${ heardIds.length > 0 ? `, heard ${formatSpeakerIds(heardIds)}` : "" }. Press 0 to show all`} @@ -595,7 +386,7 @@ export function CaptionScreen(props: CaptionScreenProps) { notice={props.notice} device={props.device} elapsedSeconds={props.elapsedSeconds} - focusedSpeakers={focusedSpeakerIds} + focus={focus} /> {!overlayOpen && ( diff --git a/src/screens/settings.tsx b/src/screens/settings.tsx index 29fed30..fdb3025 100644 --- a/src/screens/settings.tsx +++ b/src/screens/settings.tsx @@ -5,7 +5,7 @@ import { useState } from "react"; import { LanguageSelect } from "#/components/language-select.js"; import { MicrophoneSelect } from "#/components/microphone-select.js"; import { Select } from "#/components/select.js"; -import { languageHintLabel, type SttLanguage } from "#/lib/languages.js"; +import { languageHintLabel, translationLabel, type SttLanguage } from "#/lib/languages.js"; import { DEFAULT_REGION, REGIONS } from "#/lib/region.js"; import type { Microphone, SttRegion } from "#/lib/types.js"; @@ -15,6 +15,7 @@ export interface SettingsScreenProps { currentMicrophone?: string; region?: SttRegion; languages: SttLanguage[]; + translation: SttLanguage[]; hasApiKey: boolean; notice?: string; /** Footer hint for ESC on the menu. Default: "ESC exit". */ @@ -22,12 +23,20 @@ export interface SettingsScreenProps { onPickMicrophone: (name: string) => void; onPickRegion: (region: SttRegion) => void; onPickLanguages: (codes: SttLanguage[]) => void; + onPickTranslation: (codes: SttLanguage[]) => void; onChangeApiKey: (key: string) => void; onDeleteApiKey: () => void; onExit: () => void; } -type MenuItem = "microphone" | "region" | "languages" | "api-key" | "api-key-delete" | "exit"; +type MenuItem = + | "microphone" + | "region" + | "languages" + | "translation" + | "api-key" + | "api-key-delete" + | "exit"; type View = "menu" | Exclude | "api-key-delete-confirm"; export function SettingsScreen(props: SettingsScreenProps) { @@ -36,6 +45,7 @@ export function SettingsScreen(props: SettingsScreenProps) { const options: Array<{ label: string; value: MenuItem }> = [ { label: "Choose microphone", value: "microphone" }, { label: "Choose languages", value: "languages" }, + { label: "Translation", value: "translation" }, { label: "Choose region", value: "region" }, { label: "Change API key", value: "api-key" }, ...(props.hasApiKey ? [{ label: "Delete API key", value: "api-key-delete" as const }] : []), @@ -44,7 +54,7 @@ export function SettingsScreen(props: SettingsScreenProps) { useInput((_input, key) => { if (!key.escape) return; - if (view === "microphone" || view === "languages") return; + if (view === "microphone" || view === "languages" || view === "translation") return; if (view !== "menu") setView("menu"); else props.onExit(); }); @@ -59,6 +69,7 @@ export function SettingsScreen(props: SettingsScreenProps) { <> {!props.hasApiKey && No saved key} Languages: {languageHintLabel(props.languages)} + Translation: {translationLabel(props.translation)} ({ - label: - microphone.name + - (microphone.isDefault ? " (default)" : "") + - (microphone.name === props.current ? " (current)" : ""), - value: microphone.name, - }))} + // Keep the list inside short terminals; the rest scrolls. + visibleOptionCount={listHeight({ rows: props.rows ?? windowRows, reserved: 4, max: 10 })} + // The system default first: it is the right pick for most people. + options={[...props.microphones] + .sort((a, b) => Number(b.isDefault) - Number(a.isDefault)) + .map((microphone) => ({ + label: + microphone.name + + (microphone.isDefault ? " (default)" : "") + + (microphone.name === props.current ? " (current)" : ""), + value: microphone.name, + }))} onChange={props.onPick} /> - {props.onCancel && ESC cancel} + {footer} ); } diff --git a/src/hooks/use-escape.ts b/src/hooks/use-escape.ts new file mode 100644 index 0000000..8b4e6aa --- /dev/null +++ b/src/hooks/use-escape.ts @@ -0,0 +1,11 @@ +import { useInput } from "ink"; + +/** Calls `handler` on ESC while `active` (default true). */ +export function useEscape({ handler, active = true }: { handler: () => void; active?: boolean }) { + useInput( + (_input, key) => { + if (key.escape) handler(); + }, + { isActive: active }, + ); +} diff --git a/src/lib/audio.ts b/src/lib/audio.ts index 4aa2917..aac39cb 100644 --- a/src/lib/audio.ts +++ b/src/lib/audio.ts @@ -8,7 +8,7 @@ const FIRST_FRAME_TIMEOUT_MS = 10_000; const TERM_GRACE_MS = 2_000; const STDERR_KEEP_CHARS = 4_096; -function micPermissionHint() { +export function micPermissionHint() { if (process.platform === "darwin") { return "Allow this terminal app under System Settings > Privacy & Security > Microphone"; } diff --git a/src/lib/caption-layout.ts b/src/lib/caption-layout.ts index 6e17e97..56b3fb6 100644 --- a/src/lib/caption-layout.ts +++ b/src/lib/caption-layout.ts @@ -4,15 +4,15 @@ import { NO_SPACE_SCRIPT } from "#/lib/languages.js"; import { PAUSE_BREAK_MS, sentenceMark, startsNewWord } from "#/lib/segments.js"; import type { Token } from "#/lib/types.js"; -/** Fixed palette for speaker tags S1–S9 (Ink named colors). */ +/** Fixed palette for speaker tags S1–S9 (Ink named colors); no white or dark blue, which vanish on light or dark themes. */ const SPEAKER_COLORS = [ "cyan", "yellow", "magenta", "green", - "blue", + "blueBright", "red", - "white", + "magentaBright", "cyanBright", "yellowBright", ] as const; diff --git a/src/lib/clipboard.ts b/src/lib/clipboard.ts index 22c4424..8cd27d0 100644 --- a/src/lib/clipboard.ts +++ b/src/lib/clipboard.ts @@ -1,7 +1,8 @@ -import { spawnSync } from "node:child_process"; +import { spawn, spawnSync } from "node:child_process"; import { basename } from "node:path"; -// Sync spawn: async would be killed by process.exit(0) before the copy finishes. +const COPY_TIMEOUT_MS = 1_000; + const CANDIDATES: Array<{ command: string; args: string[] }> = [ { command: "pbcopy", args: [] }, { command: "wl-copy", args: [] }, @@ -13,22 +14,48 @@ export function shortPath(path: string) { return basename(path); } -function copyToClipboard(text: string) { +function copyWithOsc52(text: string) { + process.stdout.write(`\x1b]52;c;${Buffer.from(text, "utf8").toString("base64")}\x07`); +} + +/** Sync, for the last moment before exit: an async copy would die with process.exit(0). */ +export function copyWithFallback(text: string) { for (const candidate of CANDIDATES) { try { const result = spawnSync(candidate.command, candidate.args, { input: text, - timeout: 1_000, + timeout: COPY_TIMEOUT_MS, stdio: ["pipe", "ignore", "ignore"], }); if (result.error === undefined && result.status === 0) return true; } catch {} } + copyWithOsc52(text); return false; } -export function copyWithFallback(text: string) { - if (copyToClipboard(text)) return true; - process.stdout.write(`\x1b]52;c;${Buffer.from(text, "utf8").toString("base64")}\x07`); +function tryCopy({ command, args, text }: { command: string; args: string[]; text: string }) { + return new Promise((resolve) => { + const child = spawn(command, args, { stdio: ["pipe", "ignore", "ignore"] }); + const timer = setTimeout(() => child.kill(), COPY_TIMEOUT_MS); + child.on("error", () => { + clearTimeout(timer); + resolve(false); + }); + child.on("close", (code) => { + clearTimeout(timer); + resolve(code === 0); + }); + child.stdin.on("error", () => {}); + child.stdin.end(text); + }); +} + +/** Async, for use during a session: a stuck clipboard tool must not stall audio or captions. */ +export async function copyInBackground(text: string) { + for (const candidate of CANDIDATES) { + if (await tryCopy({ ...candidate, text })) return true; + } + copyWithOsc52(text); return false; } diff --git a/src/lib/keys.test.ts b/src/lib/keys.test.ts new file mode 100644 index 0000000..ec4280e --- /dev/null +++ b/src/lib/keys.test.ts @@ -0,0 +1,44 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import stringWidth from "string-width"; + +import { fitHints, latinKey } from "#/lib/keys.js"; + +describe("latinKey", () => { + it("maps Hangul jamo to the shortcut on the same key", () => { + assert.equal(latinKey("ㅔ"), "p"); + assert.equal(latinKey("ㅡ"), "m"); + assert.equal(latinKey("ㄴ"), "s"); + assert.equal(latinKey("ㅣ"), "l"); + assert.equal(latinKey("ㅊ"), "c"); + }); + + it("passes other input through", () => { + for (const input of ["p", "?", "3", "", "가"]) assert.equal(latinKey(input), input); + }); +}); + +describe("fitHints", () => { + const hints = [ + { text: "p pause", priority: 8 }, + { text: "1-9 speaker", priority: 3 }, + { text: "m mic", priority: 6 }, + { text: "? keys", priority: 9 }, + { text: "ESC end", priority: 10 }, + ]; + + it("keeps every hint when they fit", () => { + assert.equal(fitHints({ hints, width: 80 }), "p pause 1-9 speaker m mic ? keys ESC end"); + }); + + it("drops the least important hints first and keeps the order", () => { + assert.equal(fitHints({ hints, width: 30 }), "p pause ? keys ESC end"); + assert.equal(fitHints({ hints, width: 10 }), "ESC end"); + }); + + it("never exceeds the width while more than one hint is left", () => { + for (let width = 8; width <= 60; width += 1) { + assert.ok(stringWidth(fitHints({ hints, width })) <= width, String(width)); + } + }); +}); diff --git a/src/lib/keys.ts b/src/lib/keys.ts new file mode 100644 index 0000000..c345968 --- /dev/null +++ b/src/lib/keys.ts @@ -0,0 +1,66 @@ +import stringWidth from "string-width"; + +// Dubeolsik (standard Korean) jamo on each QWERTY key, so shortcuts still work when the input +// method is left in Hangul mode. +const HANGUL_KEYS: Record = { + ㅂ: "q", + ㅈ: "w", + ㄷ: "e", + ㄱ: "r", + ㅅ: "t", + ㅛ: "y", + ㅕ: "u", + ㅑ: "i", + ㅐ: "o", + ㅔ: "p", + ㅁ: "a", + ㄴ: "s", + ㅇ: "d", + ㄹ: "f", + ㅎ: "g", + ㅗ: "h", + ㅓ: "j", + ㅏ: "k", + ㅣ: "l", + ㅋ: "z", + ㅌ: "x", + ㅊ: "c", + ㅍ: "v", + ㅠ: "b", + ㅜ: "n", + ㅡ: "m", + ㅃ: "Q", + ㅉ: "W", + ㄸ: "E", + ㄲ: "R", + ㅆ: "T", + ㅒ: "O", + ㅖ: "P", +}; + +/** The QWERTY key behind a Hangul jamo; other input passes through unchanged. */ +export function latinKey(input: string) { + return HANGUL_KEYS[input] ?? input; +} + +export interface KeyHint { + text: string; + /** Higher stays longer when the line is too narrow. */ + priority: number; +} + +const HINT_GAP = " "; + +/** Drops the lowest-priority hints until the rest fit, keeping their order. */ +export function fitHints({ hints, width }: { hints: KeyHint[]; width: number }) { + const kept = [...hints]; + const lineWidth = () => stringWidth(kept.map((hint) => hint.text).join(HINT_GAP)); + while (kept.length > 1 && lineWidth() > width) { + let lowest = 0; + kept.forEach((hint, index) => { + if (hint.priority < kept[lowest]!.priority) lowest = index; + }); + kept.splice(lowest, 1); + } + return kept.map((hint) => hint.text).join(HINT_GAP); +} diff --git a/src/lib/transcription.ts b/src/lib/transcription.ts index 47fd814..9c4ac0c 100644 --- a/src/lib/transcription.ts +++ b/src/lib/transcription.ts @@ -13,7 +13,8 @@ import { saveSettings } from "#/lib/settings.js"; import { appendSttContext, startSession } from "#/lib/soniox.js"; import type { AudioCapture, Journal, SttRegion, SttSession, SttState, Token } from "#/lib/types.js"; -const TAIL_CHARS = 2_000; +// Live captions fill the terminal; this covers a 200-column, 80-row screen of Latin text. +const TAIL_CHARS = 16_000; // Throttle non-token paints (elapsed clock). Token snapshots paint immediately. const UI_EMIT_INTERVAL_MS = 150; diff --git a/src/screens/caption.tsx b/src/screens/caption.tsx index 8ad9301..623ca07 100644 --- a/src/screens/caption.tsx +++ b/src/screens/caption.tsx @@ -1,10 +1,11 @@ import { Spinner } from "@inkjs/ui"; import { Text, useBoxMetrics, useInput, useWindowSize, Box, type DOMElement } from "ink"; -import { useEffect, useMemo, useRef, useState } from "react"; +import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { MicrophoneSelect } from "#/components/microphone-select.js"; import { toLines, wrapUtterances, type Piece } from "#/lib/caption-layout.js"; -import { shortPath } from "#/lib/clipboard.js"; +import { copyInBackground, shortPath } from "#/lib/clipboard.js"; +import { fitHints, latinKey, type KeyHint } from "#/lib/keys.js"; import type { SttLanguage } from "#/lib/languages.js"; import type { Microphone, SttRegion, SttState, Token } from "#/lib/types.js"; import { SettingsScreen, type SettingsScreenProps } from "#/screens/settings.js"; @@ -53,9 +54,24 @@ interface CaptionScreenProps { onSettingsDeleteApiKey: () => void; } -const CAPTION_LINE_COUNT = 6; const END_CONFIRM_MS = 2_000; -const CAPTION_TAIL_CHARS = 1200; +const COPY_NOTICE_MS = 2_000; +// Caption rows kept visible above an open menu, plus its top border and the status line. +const OVERLAY_RESERVED_ROWS = 5; + +const KEY_HELP: Array<[string, string]> = [ + ["p", "Pause or resume"], + ["1-9", "Show only these speakers; press again to remove"], + ["l", "Show one language at a time"], + ["0", "Show everyone and every language"], + ["Up, wheel", "Scroll back; Down at the bottom returns to live"], + ["PgUp/PgDn", "Page through history; Home jumps to the start"], + ["End", "Back to live captions"], + ["c", "Copy the transcript path"], + ["m", "Switch microphone"], + ["s", "Settings"], + ["ESC ESC", "End the session; ESC again quits (Ctrl+C too)"], +]; function sortSpeakerIds(ids: Iterable) { return [...ids].sort((a, b) => Number(a) - Number(b)); @@ -97,29 +113,42 @@ function StatusLine(props: { stateMessage?: string; notice?: string; device: string; + session: string; elapsedSeconds: number; focus: string[]; }) { return ( - - + // One row always: long notices truncate instead of pushing the captions up. + + {/* Error text can be long, so only it may shrink; short labels stay whole. */} - {props.focus.length > 0 && {` focus: ${props.focus.join(", ")}`}} - {props.notice !== undefined && {` ${props.notice}`}} + {props.focus.length > 0 && ( + {` focus: ${props.focus.join(", ")}`} + )} + {props.notice !== undefined && ( + {` ${props.notice}`} + )} - {/* Shrinks far faster than the state side, so a long device name truncates first. */} - - {props.device !== "default" && ( - - {props.device} + {/* Shrinks far faster than the state side: the device name goes first, then languages. */} + + + + {props.session} - )} - - {formatElapsed(props.elapsedSeconds)} + {props.device !== "default" && ( + + + {props.device} + + + )} + + + {formatElapsed(props.elapsedSeconds)} ); @@ -155,6 +184,30 @@ function StatusLabel(props: { state: SttState; stateMessage?: string }) { return Stopped; } +function HelpPanel(props: { rows: number }) { + const keyWidth = Math.max(...KEY_HELP.map(([key]) => key.length)) + 2; + // Short terminals get as many keys as fit, then a pointer to the rest. + const fits = props.rows - 1 >= KEY_HELP.length; + const shown = fits ? KEY_HELP : KEY_HELP.slice(0, Math.max(1, props.rows - 1)); + return ( + + {shown.map(([key, action]) => ( + + + {key} + + {action} + + ))} + + {fits + ? "Any key closes this. Hangul jamo work as the same keys." + : "More keys in the README. Any key closes this."} + + + ); +} + function CaptionRow(props: { line: Piece[] }) { return ( @@ -180,7 +233,11 @@ export function CaptionScreen(props: CaptionScreenProps) { const focusedLanguage = multilingual ? languageFocus : undefined; // null = live view; otherwise finals frozen on entry, scrolled `offset` rows up from the bottom. const [history, setHistory] = useState<{ tokens: Token[]; offset: number } | null>(null); + const inHistory = history !== null; const [confirmingEnd, setConfirmingEnd] = useState(false); + const [showHelp, setShowHelp] = useState(false); + // A fresh object per copy, so pressing c again restarts the timer even with the same text. + const [copyNotice, setCopyNotice] = useState<{ text: string } | undefined>(); useEffect(() => { if (!confirmingEnd) return; @@ -188,22 +245,38 @@ export function CaptionScreen(props: CaptionScreenProps) { return () => clearTimeout(timer); }, [confirmingEnd]); + useEffect(() => { + if (copyNotice === undefined) return; + const timer = setTimeout(() => setCopyNotice(undefined), COPY_NOTICE_MS); + return () => clearTimeout(timer); + }, [copyNotice]); + + const showPath = props.journalPath !== undefined && (props.ended || props.state === "paused"); + // Captions take every row the overlay, path line, status, and hints leave free. + const captionBoxRef = useRef(null); + const captionBox = useBoxMetrics(captionBoxRef); + const captionHeight = captionBox.hasMeasured && captionBox.height > 0 ? captionBox.height : rows; + const { utterances, captionLines } = useMemo(() => { const utterances = toLines({ finalTokens: props.finalTokens, pendingTokens: props.pendingTokens, focusedSpeakers, focusedLanguage, - tailChars: CAPTION_TAIL_CHARS, + // Enough text to fill the box even when every character takes one column. + tailChars: captionHeight * columns, }); return { utterances, - captionLines: wrapUtterances({ - utterances, - width: columns, - maxLines: CAPTION_LINE_COUNT, - showLanguage: multilingual, - }), + // The history view replaces live captions, so skip wrapping lines nobody sees. + captionLines: inHistory + ? [] + : wrapUtterances({ + utterances, + width: columns, + maxLines: captionHeight, + showLanguage: multilingual, + }), }; }, [ props.finalTokens, @@ -212,6 +285,8 @@ export function CaptionScreen(props: CaptionScreenProps) { focusedLanguage, multilingual, columns, + captionHeight, + inHistory, ]); const historyTokens = history?.tokens; @@ -232,17 +307,10 @@ export function CaptionScreen(props: CaptionScreenProps) { }); }, [historyTokens, focusedSpeakers, focusedLanguage, multilingual, columns]); - const showPath = props.journalPath !== undefined && (props.ended || props.state === "paused"); - const historyBoxRef = useRef(null); - const historyBox = useBoxMetrics(historyBoxRef); - const historyHeight = - historyBox.hasMeasured && historyBox.height > 0 - ? historyBox.height - : Math.max(CAPTION_LINE_COUNT, rows - (showPath ? 3 : 2)); - const maxOffset = Math.max(0, historyRows.length - historyHeight); + const maxOffset = Math.max(0, historyRows.length - captionHeight); const offset = Math.min(history?.offset ?? 0, maxOffset); - const top = Math.max(0, historyRows.length - historyHeight - offset); - const page = historyHeight - 1; + const top = Math.max(0, historyRows.length - captionHeight - offset); + const page = Math.max(1, captionHeight - 1); const scrollUp = (delta: number) => { if (history === null) { @@ -255,8 +323,26 @@ export function CaptionScreen(props: CaptionScreenProps) { setHistory(next < 0 ? null : { ...history, offset: Math.min(next, maxOffset) }); }; - useInput((input, key) => { - if (overlayOpen) return; + const copyPath = () => { + if (props.journalPath === undefined) return; + void copyInBackground(props.journalPath).then((copied) => + setCopyNotice({ + text: copied + ? "Copied the transcript path" + : "Sent the transcript path to the terminal clipboard", + }), + ); + }; + + useInput((rawInput, key) => { + // Ctrl+C arrives as "c" with ctrl set; it belongs to the interrupt handler, not to copy. + // Alt chords are ignored too, but two quick ESC presses can arrive as one meta+ESC. + if (overlayOpen || key.ctrl || (key.meta && !key.escape)) return; + if (showHelp) { + setShowHelp(false); + return; + } + const input = latinKey(rawInput); if (key.upArrow) return scrollUp(1); if (key.downArrow) return scrollUp(-1); @@ -267,6 +353,14 @@ export function CaptionScreen(props: CaptionScreenProps) { setHistory(null); return; } + if (input === "?") { + setShowHelp(true); + return; + } + if (input === "c") { + copyPath(); + return; + } if (props.ended) { if (key.escape) props.onQuit(); @@ -318,73 +412,136 @@ export function CaptionScreen(props: CaptionScreenProps) { focusEmpty && focusedSpeakers.size > 0 ? heardSpeakerIds([props.finalTokens, props.pendingTokens]) : []; - const hints = - history !== null - ? `History ${top + 1}-${Math.min(top + historyHeight, historyRows.length)}/${historyRows.length} Up/Down PgUp/PgDn scroll End live` - : props.finishing - ? "Saving transcript ESC force quit" - : props.ended - ? "Up history ESC quit" - : confirmingEnd - ? "Press ESC again to end the session" - : `p ${props.state === "paused" ? "resume" : "pause"} 1-9 speaker ${multilingual ? "l language " : ""}0 all Up history m mic s settings ESC end`; + + // Narrow terminals drop the least important hints; how to end and get help always stay. + let hintList: KeyHint[]; + if (history !== null) { + hintList = [ + { + text: `History ${top + 1}-${Math.min(top + captionHeight, historyRows.length)}/${historyRows.length}`, + priority: 3, + }, + { text: "Up/Down PgUp/PgDn scroll", priority: 1 }, + { text: "End live", priority: 2 }, + ]; + } else if (props.finishing) { + hintList = [{ text: "Saving transcript ESC force quit", priority: 1 }]; + } else if (props.ended) { + hintList = [ + { text: "Up history", priority: 1 }, + { text: "c copy path", priority: 2 }, + { text: "ESC quit", priority: 3 }, + ]; + } else if (confirmingEnd) { + hintList = [{ text: "Press ESC again to end the session", priority: 1 }]; + } else { + hintList = [ + { text: `p ${props.state === "paused" ? "resume" : "pause"}`, priority: 8 }, + { text: "1-9 speaker", priority: 3 }, + ...(multilingual ? [{ text: "l language", priority: 4 }] : []), + { text: "0 all", priority: 2 }, + { text: "Up history", priority: 5 }, + { text: "c copy path", priority: 1 }, + { text: "m mic", priority: 6 }, + { text: "s settings", priority: 7 }, + { text: "? keys", priority: 9 }, + { text: "ESC end", priority: 10 }, + ]; + } + const hints = fitHints({ hints: hintList, width: columns }); + + const overlayRows = rows - OVERLAY_RESERVED_ROWS - (showPath ? 1 : 0); + let overlay: ReactNode = null; + if (props.overlay.kind === "mic") { + overlay = ( + + ); + } else if (props.overlay.kind === "settings") { + overlay = ( + + ); + } else if (showHelp) { + overlay = ; + } + + const spoken = props.languages.join("/"); + const session = + props.translation.length === 1 + ? `${spoken} to ${props.translation[0]}` + : props.translation.length === 2 + ? `${spoken}, ${props.translation.join("/")} both ways` + : spoken; return ( - - {props.overlay.kind === "mic" ? ( - - ) : props.overlay.kind === "settings" ? ( - - ) : history !== null ? ( + + + {history !== null ? ( + historyRows + .slice(top, top + captionHeight) + .map((line, index) => ) + ) : focusEmpty ? ( + + {`No speech from ${focus.join(", ")}${ + heardIds.length > 0 ? `, heard ${formatSpeakerIds(heardIds)}` : "" + }. Press 0 to show all`} + + ) : ( + captionLines.map((line, index) => ) + )} + + + {/* Overlays sit under the captions, which keep running in the rows above. */} + {overlay !== null && ( - {historyRows.slice(top, top + historyHeight).map((line, index) => ( - - ))} - - ) : ( - - {focusEmpty ? ( - - {`No speech from ${focus.join(", ")}${ - heardIds.length > 0 ? `, heard ${formatSpeakerIds(heardIds)}` : "" - }. Press 0 to show all`} - - ) : ( - captionLines.map((line, index) => ) - )} + {overlay} )} - {showPath && {shortPath(props.journalPath!)} copied to clipboard} + {showPath && ( + + {`Transcript: ${shortPath(props.journalPath!)} (c to copy the path)`} + + )} diff --git a/src/screens/key-setup.tsx b/src/screens/key-setup.tsx index 2776f52..61f5590 100644 --- a/src/screens/key-setup.tsx +++ b/src/screens/key-setup.tsx @@ -1,14 +1,19 @@ import { PasswordInput, Spinner } from "@inkjs/ui"; import { Box, Text } from "ink"; +import { useEscape } from "#/hooks/use-escape.js"; + interface KeySetupProps { error?: string; verifying: boolean; regionLabel: string; + onBack: () => void; onSubmit: (key: string) => void; } export function KeySetup(props: KeySetupProps) { + useEscape({ handler: props.onBack, active: !props.verifying }); + return ( {`Enter your Soniox API key for ${props.regionLabel}`} @@ -20,6 +25,7 @@ export function KeySetup(props: KeySetupProps) { <> {props.error !== undefined && {props.error}} + ESC choose another region )} diff --git a/src/screens/region-select.tsx b/src/screens/region-select.tsx index 70ebda3..7b168a6 100644 --- a/src/screens/region-select.tsx +++ b/src/screens/region-select.tsx @@ -1,10 +1,13 @@ import { Box, Text } from "ink"; import { Select } from "#/components/select.js"; +import { useEscape } from "#/hooks/use-escape.js"; import { DEFAULT_REGION, REGIONS } from "#/lib/region.js"; import type { SttRegion } from "#/lib/types.js"; -export function RegionSelect(props: { onPick: (region: SttRegion) => void }) { +export function RegionSelect(props: { onPick: (region: SttRegion) => void; onCancel: () => void }) { + useEscape({ handler: props.onCancel }); + return ( Which Soniox region will you use? @@ -18,6 +21,7 @@ export function RegionSelect(props: { onPick: (region: SttRegion) => void }) { }))} onChange={(value) => props.onPick(value as SttRegion)} /> + ESC quit ); } diff --git a/src/screens/settings.tsx b/src/screens/settings.tsx index fdb3025..50ce488 100644 --- a/src/screens/settings.tsx +++ b/src/screens/settings.tsx @@ -1,8 +1,9 @@ import { ConfirmInput, PasswordInput } from "@inkjs/ui"; -import { Box, Text, useInput } from "ink"; +import { Box, Text, useInput, useWindowSize } from "ink"; import { useState } from "react"; import { LanguageSelect } from "#/components/language-select.js"; +import { listHeight, menuGap } from "#/components/list-height.js"; import { MicrophoneSelect } from "#/components/microphone-select.js"; import { Select } from "#/components/select.js"; import { languageHintLabel, translationLabel, type SttLanguage } from "#/lib/languages.js"; @@ -18,6 +19,8 @@ export interface SettingsScreenProps { translation: SttLanguage[]; hasApiKey: boolean; notice?: string; + /** Rows the settings may use; defaults to the terminal height. */ + rows?: number; /** Footer hint for ESC on the menu. Default: "ESC exit". */ exitLabel?: string; onPickMicrophone: (name: string) => void; @@ -41,6 +44,8 @@ type View = "menu" | Exclude | "api-key-delete-confirm"; export function SettingsScreen(props: SettingsScreenProps) { const [view, setView] = useState("menu"); + const windowRows = useWindowSize().rows; + const rows = props.rows ?? windowRows; const options: Array<{ label: string; value: MenuItem }> = [ { label: "Choose microphone", value: "microphone" }, @@ -60,7 +65,7 @@ export function SettingsScreen(props: SettingsScreenProps) { }); return ( - + Settings {props.notice !== undefined && view === "menu" && {props.notice}} @@ -72,6 +77,8 @@ export function SettingsScreen(props: SettingsScreenProps) { Translation: {translationLabel(props.translation)}